Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Cannot Read Annotation Value at Runtime When a Function Pointer is Passed to an Object Defined at the Module Level or Service Level #43855

Open
MohamedSabthar opened this issue Feb 27, 2025 · 1 comment
Labels
Priority/Blocker Priority/High Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@MohamedSabthar
Copy link
Member

MohamedSabthar commented Feb 27, 2025

Description

In the ballerinax/ai.agent package, there's a requirement to read the annotation value of a function at runtime. However, when the agent is defined at the module level, it is not possible to retrieve the annotation value at runtime.

Steps to Reproduce

The following stripped-down version of the code illustrates the problem:

import ballerina/io;

type ToolConfig record {|
    string name?;
|};

annotation ToolConfig Tool on function;

class Agent {
    private (isolated function)[] tools = [];
    isolated function init((isolated function)[] tools) {
        self.tools = tools;
    }

    isolated function run() returns ToolConfig|error {
        typedesc<isolated function> typedesVal = typeof self.tools.pop();
        ToolConfig toolConfig = check typedesVal.@Tool.ensureType();
        return toolConfig;
    }
}

@Tool{name: "name"}
isolated function sum(int a, int b) returns int {
    return a + b;
}

Agent agent = new([sum]); // This doesn't work

public function main() {
    // Agent agent = new(sum); // This works
    io:println(agent.run());
}
service on new http:Listener(9090) {
    private final Agent agent;

    function init() {
       self.agent = new([sum]);
    }

    resource function get .() returns ToolConfig|error {
        return self.agent.run();
    }
}

The issue is also reproducible when the agent defined as a field in service class.

Affected Version(s)

No response

OS, DB, other environment details and versions

No response

Related area

-> Runtime

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label Feb 27, 2025
@MaryamZi
Copy link
Member

For different function pointers referring to the same function we have different BFunctionTypes (annotations are bound the type atm) and therefore, we set the annotations whenever an FPValue is created - codegen to io.ballerina.runtime.internal.utils.AnnotationUtils#processFPValueAnnotations. Here, the annotation map isn't populated when processFPValueAnnotations is called for the function pointer at module-level.

Related to the known ordering/typedesc + annotation resolution issue we have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority/Blocker Priority/High Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
None yet
Development

No branches or pull requests

3 participants