Skip to content

fix(engine): agent loaders skip sys.path setup, breaking package-relative imports #686

Description

@ahmedennaifer

Summary

Both ADK and LangGraph dynamic agent loaders use importlib.util.spec_from_file_location to import the user's agent file directly. That call bypasses Python's normal import machinery and never adds the project root to sys.path. Any agent file that uses absolute imports relative to its project root (e.g. from app.config import config) fails to boot with ModuleNotFoundError.

Affected code

  • libs/idun_agent_engine/src/idun_agent_engine/agent/adk/adk.py lines 435-462 (_load_agent)
  • libs/idun_agent_engine/src/idun_agent_engine/agent/langgraph/langgraph.py lines 458-514 (_load_graph_builder)

Reproduction

Project layout:

/path/to/myproject/
├── app/
│   ├── __init__.py
│   ├── agent.py        # contains: from app.config import config
│   └── config.py
└── config.yaml          # agent: app/agent.py:root_agent

From inside /path/to/myproject/:

idun init

Engine reload fails with:

ValueError: Failed to load agent from app/agent.py:root_agent: No module named 'app'

Why running from the project cwd does not help

idun init is an installed console script. Python adds the entry-point script's own directory (the venv bin/) to sys.path[0], not the shell's cwd. So launching the server from the project root still doesn't put it on sys.path.

Workaround

PYTHONPATH=. uv run idun init

Suggested fix directions

Several viable strategies, each with trade-offs around namespace packages, programmatic-config callers (ConfigBuilder.from_dict), and hot-reload behavior. Worth a brief design discussion before landing:

  1. Walk up to a project-root marker (pyproject.toml, setup.py, setup.cfg) and sys.path.insert it before exec_module.
  2. Use the directory of the resolved config.yaml as the project root and add it to sys.path once at boot.
  3. Add an explicit project_root field to the agent config.
  4. Convert the file path to a dotted module name relative to the project root and call importlib.import_module instead of spec_from_file_location.

Affected versions

Observed on idun-agent-engine==0.6.2. Reproducible against the current docs/standalone-api-and-demo branch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingengineEngine specific issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions