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

agent macro inheritance doesn't work correctly if two types are defined in different modules with the same name #1114

Open
Tortar opened this issue Dec 14, 2024 · 6 comments
Labels
agent-construction About making agents bug Something isn't working quality of life QoL enhancements that make user experience smoother

Comments

@Tortar
Copy link
Member

Tortar commented Dec 14, 2024

E.g. something like

julia> module A
           export X
           using Agents
           @agent struct X(NoSpaceAgent) end
       end
Main.A

julia> module B
           using Agents
           @agent struct X(NoSpaceAgent) a::Int end
       end
Main.B

julia> using Agents

julia> @agent struct Y(X) end

julia> Y(1) # should work, but it doesn't
ERROR: MethodError: no method matching Y(::Int64)
...

julia> Y(1, 1) # shouldn't work, but it does
Y(1, 1)

That is because our book-keeping doesn't preserve the module where the inherited agent belongs to:

julia> Agents.__AGENT_GENERATOR__
Dict{Symbol, Expr} with 8 entries:
  :GraphAgent      => :(mutable struct GraphAgent <: Agents.AbstractAgent
  :GridAgent       => :(mutable struct GridAgent{D} <: Agents.AbstractAgent
  :A               => :(mutable struct A <: Agents.AbstractAgent
  :X               => :(mutable struct X <: Agents.AbstractAgent
  :OSMAgent        => :(mutable struct OSMAgent <: Agents.AbstractAgent
  :NoSpaceAgent    => :(mutable struct NoSpaceAgent <: AbstractAgent
  :Y               => :(mutable struct Y <: Agents.AbstractAgent
  :ContinuousAgent => :(mutable struct ContinuousAgent{D, T} <: Agents.AbstractAgent
@Tortar Tortar added bug Something isn't working agent-construction About making agents labels Dec 14, 2024
@Datseris
Copy link
Member

In the example above which module did you use? There is no using statement beyond Agents.

@Datseris
Copy link
Member

I would think that the Y(X) would error because X is an undefined variable.

@Tortar
Copy link
Member Author

Tortar commented Dec 15, 2024

ohh yeah there is also this other problem, since we access X from Agents.__AGENT_GENERATOR__ we can access it if an agent with that name was defined in any module, even if that agent type wasn't exported. Same fix for the problem I found would apply to this case too, namely preserving the module information in the book-keeping

@Tortar Tortar added the quality of life QoL enhancements that make user experience smoother label Dec 15, 2024
@Datseris
Copy link
Member

But still, what is the plan here? To check if the calling code contains the module that defined the agent? I don't know how you check whether a module has been used or not, programmatically. You can't use isdefined, because in the above examples modules A, B are defined, they just aren't using.

@Tortar
Copy link
Member Author

Tortar commented Dec 15, 2024

It should be built at runtime so that we can obtain the module location of the variable e.g.

julia> module A
           export X
           struct X end
       end;

julia> using .A

julia> macro m(x)
           quote parentmodule($x) end
       end;

julia> @m(X)
Main.A

@Tortar
Copy link
Member Author

Tortar commented Dec 15, 2024

and the book-keeping should be like

julia> Agents.__AGENT_GENERATOR__
Dict{Symbol, Expr} with 8 entries:
  (Agents, :GraphAgent)  => :(mutable struct GraphAgent <: Agents.AbstractAgent
  (Agents, :GridAgent)       => :(mutable struct GridAgent{D} <: Agents.AbstractAgent
  (A, :X)             => :(mutable struct X <: Agents.AbstractAgent
  (B, :X)             => :(mutable struct X <: Agents.AbstractAgent
  (Main, :Y)              => :(mutable struct Y <: Agents.AbstractAgent
  ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-construction About making agents bug Something isn't working quality of life QoL enhancements that make user experience smoother
Projects
None yet
Development

No branches or pull requests

2 participants