Skip to content

feat(voice): scope http_context to AgentSession when no job context#5608

Open
longcw wants to merge 3 commits intomainfrom
longc/agent-session-owns-http-context
Open

feat(voice): scope http_context to AgentSession when no job context#5608
longcw wants to merge 3 commits intomainfrom
longc/agent-session-owns-http-context

Conversation

@longcw
Copy link
Copy Markdown
Contributor

@longcw longcw commented Apr 30, 2026

Summary

When AgentSession runs outside a job process (tests, scripts, ad-hoc usage) there is no process-level http session bound to the event loop, so utils.http_context.http_session() raises and STT/TTS that rely on it can't connect. AgentSession now creates the http session ctx in start() (when get_job_context() is None and no outer ctx is already set) and tears it down in aclose(), so the same plugins that work inside a job process also work standalone with an AgentSession.

Outside a job process (tests, scripts, ad-hoc usage) there is no
process-level http session bound to the event loop, so plugins calling
utils.http_context.http_session() would raise. AgentSession.start now
sets up the context (via _new_session_ctx) when get_job_context() is
None, and aclose tears it down — including on a failed start so the
factory isn't leaked.

Also makes inference STT SpeechStream fetch the session lazily at
ws_connect time instead of caching it at construction, so it picks up
the AgentSession's session correctly and doesn't hold a stale reference.
@chenghao-mou chenghao-mou requested a review from a team April 30, 2026 08:30
devin-ai-integration[bot]

This comment was marked as resolved.

When two AgentSessions run in the same task (e.g. nested), the inner
start() previously overwrote the outer's contextvar factory because the
ownership check only looked at job_ctx and the inner's own flag. Add an
_is_http_session_ctx_set helper and consult it before claiming
ownership, so the inner session reuses the outer's session and doesn't
close it on aclose.
aiohttp.WSMsgType.CLOSE,
aiohttp.WSMsgType.CLOSING,
):
if closing_ws or self._session.closed:
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.

I believe we have the same pattern in other files as well.

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 this back with a local http_session var

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.

Should we fix all "or self._session.closed:" usage?

Capture the http session once at the start of _run and pass it into
_connect_ws. Restore the http_session.closed check in recv_task so a
normal shutdown via AgentSession.aclose (which closes the underlying
ClientSession) doesn't get reported as an unexpected gateway close.
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