W2-T16: add async _compress_context variant for use from async call sites - #13
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4c4338d183
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return await asyncio.to_thread( | ||
| ctx.run, | ||
| self._compress_context, |
There was a problem hiding this comment.
Preserve session ContextVar updates after async compression
When this wrapper is used from a gateway async task and _compress_context performs a session split, the _SESSION_ID.set(self.session_id) done by the sync method runs inside the copied worker-thread context, so that ContextVar update is lost when control returns to the awaiting task. In that scenario agent.session_id has the new compressed session, but later async code that reads get_session_env("HERMES_SESSION_ID") in the original task still sees the old session; the wrapper should mirror the rotated session id back into the caller context after the thread completes.
Useful? React with 👍 / 👎.
|



Audit W2-T16. Adds
AIAgent._async_compress_contextwhich runs the sync_compress_contextviaasyncio.to_threadwithcontextvars.copy_context()propagation (same pattern asrun_agent.py:11725). All 5 current_compress_contextcall sites are in syncrun_conversation— async variant is preemptive for future async-context use (e.g. gateway server). Note: the real blockingcall_llmlives incontext_compressor.py:951which is out of scope;asyncio.to_threadwrapping the whole sync method achieves the non-block goal.