Skip to content

✨ Self-referencing modules for llm/env #10336

Closed

Description

@eunomie

What are you trying to do?

Environments are a way to define a specific set of functions an LLM will be able to see as tools. This allows to reduce the number of tools the LLM will see or to provide specific tools, both in order to improve the ability to complete a task.

Note

This is extracted from #8030 but focusing only the LLM/Env/type awareness and not on the self calling aspects.

It's currently possible to use a primitive type as an environment input (like Container or Directory for instance). But to use a specific type, a workspace for instance, that embeds primitives and expose only certain functions, this needs to be declared in a different module, and used as a dependency.

This makes the usage of environments more complex where it could be interesting to have the code of the type we want to expose to the LLM at the same place than the rest of the code calling the LLM.

The current limitation is the generated code on the Env type (like withXXXInput, for instance withContainerInput) is not aware of the actual types defined by the module. The generated code is only aware of the types that are exposed to the engine, including dependencies of the current module, but not the module itself.

Use Case

Context:

  • a type Workspace containing several functions to be seen as tools for the LLM

Currently:

  • dag.LLM().WithEnv(dag.Env().WithWorkspaceInput("input", dag.Workspace(src), "..."))....
  • dag.Workspace refers to a workspace module, as a dependency of the current module

Goal:

  • dag.LLM().WithEnv(dag.Env().WithWorkspaceInput("input", Workspace{src}, "..."))....
  • Workspace here is a local type

Technical feasibility

This means the generated code on Env is aware of the local types. If not possible then it means we have to generate types as we do usually for the dependencies and have something more like dag.Env().WithWorkspaceInput("input", dag.Self().Workspace(src), "...")but that would be more confusing for the users.

Activity

  1. eunomie commented on May 6, 2025

    @eunomie
    MemberAuthor

    Let's say I have a module my-agent that contains a type workspace (a struct in Go, a class in Java, etc).

    What I want is:

    dag.Env().WithWorkspaceInput("input", &Workspace{}, "the workspace")

    Type Exposure

    By default the Workspace type is not exposed. It needs to be exposed by a function of the module to be exposed. So it needs somethings like that:

    func (m *MyAgent) Workspace() *Workspace { return &Workspace{} }

    Then we can imagine to keep the above call or to replace it by

    dag.Env().WithWorkspaceInput("input", m.Workspace(), "the workspace")

    Note

    In Java all types are exposed if they have the @Object annotation. So we don't need this kind of function. Not sure that's the way to go or not.

    Circular Dependency

    The WithWorkspaceInput function is defined at the level of the Env type. In Go this is in dagger/my-agent/internal/dagger. (I'm using Go as a reference, dependency management vary between languages but Go doesn't handle circular dependencies)
    This type needs to have access to the package where Workspace is defined, dagger/my-agent.
    But dagger/my-agent imports dagger/my-agent/internal/dagger to get the Env type.
    So we just created a circular dependency here.

    Generated Types

    One way to solve this circular dependency issue is to never have internal/dagger importing code defined at the module level.
    In this case WithWorkspaceInput should only refer to a generated type. So we need to create a type at the Dagger client level, like dag.Workspace.
    But then, there's the question of how to create an instance. If we reference the module from an other one, we would have something like:

    dag.MyAgent().Workspace()

    If the the module is registered and exposed under a Self function/keyword, then we can imagine to have:

    func (m *MyAgent) Workspace() *Workspace {
            return &Workspace{}
    }
    
    func (m *MyAgent) env() *dagger.Env {
            return dag.Env().WithWorkspaceInput("input", dag.Self().Workspace(), "the workspace")
    }

    In this case we can't directly use the type we defined neither the Workspace function above.

    Should also the WithWorkspaceInput be something like WithSelfWorkspaceInput?

  2. shykes commented on May 6, 2025

    @shykes
    Contributor

    @eunomie I don't have the complete answer, but wanted to make a more general note on self-calls: if possible, I think we should avoid dag.Self() and instead use dag.Foo() where foo is the name of the module. In other words, the naming of the binding should be the same as for regular dependencies.

    I think there are several reasons for this. But one specific reason for me, is that I want to leave the door open to having more than one runtime per module in the future ("runtime stacking") - which means there would no longer be a single "self" to reference.

  3. vito commented on May 6, 2025

    @vito
    Contributor

    @eunomie The dag.Self().Workspace() approach (or dag.MyModule().Workspace() per #10336 (comment)) makes the most sense to me, at a technical level.

    The problem with directly passing a &Workspace{} is that all object arguments work by passing the object's ID, which is generated through the process of making API calls. If you directly create a &Workspace{} and pass it in, we don't have an easy way to translate that to an ID at the API layer.

    Building on that, I would also expect a single WithWorkspaceInput that takes a *dagger.Workspace instead of a *Workspace. I don't think we need a WithSelf variant. 🤔


    Context dumping:

    In many cases the motivation for self calls is actually to avoid natively using the local code bindings. In Go, for example, you might find yourself with this type of signature:

    func (m *MyMod) MyFunc(
      foo int,
      // +default="hey"
      bar string,
      // +default="world"
      baz string,
    )

    If I were to use the native code, I would have to repeat those defaults at each call site (myMod.MyFunc(42, "hey", "world")). With self-calls I can rely on the API defaults (dag.MyMod().MyFunc(42)).

    A separate motivator for using the API instead of native code is caching. Since it goes out and back in through the API layer, dag.MyMod().MyFunc(42) will be cached independently, whereas myMod.MyFunc(42, "hey", "world") will not. In a lot of cases this doesn't actually matter much, since MyFunc might just return a lazy *Container or something, but still.

  4. shykes commented on May 6, 2025

    @shykes
    Contributor

    A separate motivator for using the API instead of native code is caching

    @vito also tracing 😍

  5. eunomie commented on May 15, 2025

    @eunomie
    MemberAuthor

    All right, I have a first PoC ~working. I'll open a draft PR with it. It's just a PoC, there's multiple issues and improvements required:

    • drop in speed
    • codegen doesn't work in all cases (but module call works)
    • the display of the conversation with the model disappeared
    • this currently only works with the java SDK and it requires changes on the SDK side

    But I'm able to run my previous demo with the workspace within the main module, so without the need of a second/external one.

    @Object
    public class SelfAgent {
      @Function
      public Container devEnvironment(Directory source) {
        var environment =
            dag()
                .env()
                .withSelfAgentAlpineWorkspaceInput(
                    "workspace",
                    dag().selfAgent().alpineWorkspace(source),
                    "Alpine workspace with the source codebase mounted")
                .withSelfAgentAlpineWorkspaceOutput(
                    "result", "Workspace with the development environment set up");
    
        return dag()
            .llm()
            .withEnv(environment)
            .withPrompt("...")
            .env()
            .output("result")
            .asSelfAgentAlpineWorkspace()
            .container();
      }
    
      @Function
      public AlpineWorkspace alpineWorkspace(Directory source) {
        return new AlpineWorkspace(source);
      }
    }

    AlpineWorkspace is a java class defined in the same package.

    A few notes based on this example:

    • the object type is prefixed by the module name. Like when it's used from the outside. This is because the module sees itself as a dependency, not as... itself
    • as we can't have constructors for objects other than the main one, a specific function (the second one in the example) is required to create the workspace
    • this means we need dag().selfAgent().alpineWorkspace() instead of passing a simple new AlpineWorkspace()
  6. kpenfound commented on May 15, 2025

    @kpenfound
    Contributor

    Excited to get this in!

    My main feedback is agreeing with what @vito said earlier

    Building on that, I would also expect a single WithWorkspaceInput that takes a *dagger.Workspace instead of a *Workspace. I don't think we need a WithSelf variant.

    I think the withSelfAgentAlpineWorkspaceInput instead of withAlpineWorkspaceInput is too confusing. Users don't want to care about our internal technical constraints of passing objects into the API, they just want to create a class and put it in an environment.

    I also still really want a way to pass in the object directly. I understand the reasons why that's hard, but it would be great if we had some SDK side translation that can construct the object the dagger way and hide that from the user.

    Finally, unlocking this capability really advances the DX of passing objects in and out of the Dagger API, and I think we must solve the constructor issue. Requiring the user to make a constructor for the secondary object inside of the main object is going to be too confusing to everyone

  7. jpadams commented on May 15, 2025

    @jpadams
    Contributor

    +1 to dag.MyModule().AlpineWorkspace() where MyModule == self

    Thinking on this more...

  8. kpenfound commented on May 15, 2025

    @kpenfound
    Contributor

    As I understand it, the current proposal would look like this in Go:

    func (m *HelloDagger) Agent(ctx context.Context, prompt string) (string, error) {
        ws := dag.HelloDagger().Workspace()
        return dag.LLM().
            WithHelloDaggerWorkspaceInput(ws).
            WithPrompt(prompt).
            LastReply(ctx)
    }
    
    func (m *HelloDagger) Workspace() *Workspace {
        return &Workspace{
            ctr: dag.Container().From("alpine"),
        }
    }
    
    type Workspace struct {
        ctr *dagger.Container
    }
    
    func (f *Workspace) ReadFile()  {}
    func (f *Workspace) WriteFile() {}
    func (f *Workspace) Check()     {}

    I'm advocating for this instead for the reasons stated in my previous comment:

    func (m *HelloDagger) Agent(ctx context.Context, prompt string) (string, error) {
        ws := &Workspace{
            ctr: dag.Container().From("alpine"),
        }
        
        return dag.LLM().
            WithWorkspaceInput(ws).
            WithPrompt(prompt).
            LastReply(ctx)
    }
    
    type Workspace struct {
        ctr *dagger.Container
    }
    
    func (f *Workspace) ReadFile()  {}
    func (f *Workspace) WriteFile() {}
    func (f *Workspace) Check()     {}
  9. eunomie commented on May 16, 2025

    @eunomie
    MemberAuthor

    I agree, that would be a better solution from a DX perspective for sure 👍
    An intermediate step would be to remove the need of the function (to replace the constructor) then to remove the module prefix.
    Then the question of the local type, I have some ideas, but I don't know yet how much they are good ideas or not...

    In the above example I shared, one confusion could be my module is named SelfAgent. I should have avoid self 😅

  10. locked and limited conversation to collaborators on Mar 25, 2026
  11. converted this issue into a discussion #12678 on Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions