Skip to content

W2-T16: add async _compress_context variant for use from async call sites - #13

Merged
blaketyndall-eng merged 1 commit into
pr-25159from
post-audit-sprint/W2-T16
May 14, 2026
Merged

blaketyndall-eng merged 1 commit into
pr-25159from
post-audit-sprint/W2-T16

Conversation

@blaketyndall-eng

Copy link
Copy Markdown
Owner

Audit W2-T16. Adds AIAgent._async_compress_context which runs the sync _compress_context via asyncio.to_thread with contextvars.copy_context() propagation (same pattern as run_agent.py:11725). All 5 current _compress_context call sites are in sync run_conversation — async variant is preemptive for future async-context use (e.g. gateway server). Note: the real blocking call_llm lives in context_compressor.py:951 which is out of scope; asyncio.to_thread wrapping the whole sync method achieves the non-block goal.

@blaketyndall-eng
blaketyndall-eng merged commit 8eb01ba into pr-25159 May 14, 2026
3 checks passed
@blaketyndall-eng
blaketyndall-eng deleted the post-audit-sprint/W2-T16 branch May 14, 2026 16:50

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread run_agent.py
Comment on lines +11202 to +11204
return await asyncio.to_thread(
ctx.run,
self._compress_context,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@sonarqubecloud

Copy link
Copy Markdown

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.

1 participant