Skip to content

fix: close Codex app-server session on agent teardown - #2

Merged
Shimada666 merged 1 commit into
agent/codex-long-sessions-cleanfrom
agent/fix-codex-session-teardown
Aug 6, 2026
Merged

Shimada666 merged 1 commit into
agent/codex-long-sessions-cleanfrom
agent/fix-codex-session-teardown

Conversation

@Shimada666

Copy link
Copy Markdown
Member

What changed

  • Port upstream commit e623432b8 onto the Qortex long-session fork.
  • Close and clear _codex_session from AIAgent.close().
  • Add lifecycle coverage for idempotent close, raising close, and non-Codex agents.

Why

Background review agents use the Codex app-server runtime and call AIAgent.close() after each review. The missing teardown leaked one Node/Codex/MCP process tree per review.

Validation

  • uv run --extra dev pytest -q tests/run_agent/test_codex_app_server_lifecycle.py — 3 passed

Salvage of NousResearch#65260's b7d7cfd (ported — the PR's close() predates ~4K
commits of teardown-step churn, so the hunk is re-anchored after step
6b rather than cherry-picked).

agent/codex_runtime.py already drops _codex_session on turn crash and
on retirement, but AIAgent.close() — the hard teardown for /new,
/reset, and session expiry — had no owner for it, so the app-server
child process survived until interpreter exit. Long-lived gateways
accumulate one leaked subprocess per ended Codex session.

The attribute is cleared BEFORE close() so a concurrent reader can't
observe a half-closed session and a raising close() can't strand a
stale reference (tested).

Tests extend the author's original lifecycle test with the
raising-close and no-codex-session cases.

(cherry picked from commit e623432)
@Shimada666
Shimada666 merged commit 50165eb into agent/codex-long-sessions-clean Aug 6, 2026
@Shimada666
Shimada666 deleted the agent/fix-codex-session-teardown branch August 6, 2026 09:06
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