Skip to content

a2a: fix delegation from a worker run as a script - #8602

Merged
longcw merged 1 commit into
v2.0from
longc/self-hosted-delegation
Nov 20, 2026
Merged

longcw merged 1 commit into
v2.0from
longc/self-hosted-delegation

Conversation

@longcw

@longcw longcw commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

A RemoteAgent on a call shares the call's thread with its delegate (auth.md), reached through LiveKit Cloud or, on a self-hosted server, at the worker's own listener (tools.md). This fixes what still keeps that from working for a worker run with lk agent dev:

  • The job process reads LIVEKIT_AGENT_ENDPOINTS_URL when it delegates. A script runs as __main__, so a spawned job process re-imports it as __mp_main__ before job_main applies the worker's environment, and AgentServer() copies settings without the endpoints URL. On Cloud and self-hosted alike, the call fails at session start with endpoint "backend" is not reachable yet: the server has not started serving.
  • The e2e runner runs a path= app as __main__. It preloaded the app as a module after the environment was applied, so the self-hosted delegation journey passed while lk agent dev failed. It now takes the same path as livekit.agents start main.py.
  • A call without a thread fails clearly. It warns at start about the delegates it cannot reach, and the delegate's 403 becomes a logged error saying to dispatch the call with thread_id.
Context for reviewing and coding agents

How to see it

On the v2.0 tip, tests/e2e/test_delegation_voice.py -k self_hosted fails with the runner change and without the host.rs change, with the same error lk agent dev gives. tests/host/test_delegation.py::test_a_job_process_delegates_to_endpoints_its_worker_named_after_the_import builds the server first, sets LIVEKIT_AGENT_ENDPOINTS_URL after, and fails the same way without the fix.

What v2.0 already covers

73ad5baf6 signs a grant for the worker's own listener, and its Project::new reads the endpoints URL when it delegates. That fixes signing the call, not finding its URL: host.rs delegation_url still read the settings copied at import.

@longcw
longcw requested a review from a team as a code owner November 17, 2026 13:28

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@longcw longcw changed the title a2a: delegate from a worker on a self-hosted server a2a: fix delegation from a worker in development Nov 17, 2026
Comment thread examples/frontdesk/main.py Outdated
Comment on lines +10 to +16
from livekit.agents.storage import AgentDBStorage # noqa: E402
from livekit.agents.storage import AgentDBStorage, SQLiteStorage # noqa: E402

server = AgentServer(storage=AgentDBStorage(), agent_name="dentist")
# AgentDB needs a LiveKit Cloud project; AGENT_STORAGE=sqlite keeps threads in a local file
storage = SQLiteStorage() if os.environ.get("AGENT_STORAGE") == "sqlite" else AgentDBStorage()
server = AgentServer(storage=storage, agent_name="dentist")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the user shouldn't specify any storage by default?

And we automatically picked based on whether they use cloud or not?

This way, we can just remove the above code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same idea initially, we may automatically select the storage for users based where the worker is running and if they connected to lk cloud.

but on the other hand, it feels like the storage should be set explicitly since it controls where the data persisted to, otherwise you may write the data to an unexpected location...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, I like the idea of always having storage very explicit. Tho most users are going to use Cloud, so I don't think it's the end of the world if we pick automatically. It's going to be right 99% of the time.

Tho in some scenarios, we should also short-circuit this decision, e.g when using lk agents console

Maybe a good middleground is to log the active storage at the sever startup?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added that in #8675

A worker run as a script (`lk agent dev`, `python -m livekit.agents
start main.py`) could not delegate from a job process: the spawned
process re-imports main.py as __mp_main__ before job_main applies the
worker's environment, so AgentServer() copies settings without
LIVEKIT_AGENT_ENDPOINTS_URL and delegation_url fails with "the server
has not started serving".

delegation_url reads LIVEKIT_AGENT_ENDPOINTS_URL again when the copy has
none. The e2e runner now runs a path= app as __main__ the way
`livekit.agents start` does, instead of preloading it as a module after
the environment is applied, so the self-hosted delegation journey takes
this path. A call that starts without a thread warns about the delegates
it cannot reach, and a delegate's 403 becomes a logged error that says
to dispatch the call with thread_id; the model gets only that the
delegate is not available.
@longcw
longcw force-pushed the longc/self-hosted-delegation branch from ff875f1 to 0b0ae05 Compare November 19, 2026 11:09
@longcw longcw changed the title a2a: fix delegation from a worker in development a2a: fix delegation from a worker run as a script Nov 19, 2026
devin-ai-integration[bot]

This comment was marked as resolved.

@longcw
longcw requested a review from theomonnom November 19, 2026 11:15
@longcw
longcw force-pushed the longc/self-hosted-delegation branch from 0a96b64 to 62c6daa Compare November 19, 2026 12:32
@longcw
longcw added this pull request to stack #8676 November 19, 2026 12:35
@longcw
longcw merged commit 8c00869 into v2.0 Nov 20, 2026
9 of 16 checks passed
@longcw
longcw deleted the longc/self-hosted-delegation branch November 20, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants