Skip to content

fix(hooks): stop also closes session for Codex (closes #493)#579

Open
rohitg00 wants to merge 1 commit into
mainfrom
fix/codex-stop-session-end
Open

fix(hooks): stop also closes session for Codex (closes #493)#579
rohitg00 wants to merge 1 commit into
mainfrom
fix/codex-stop-session-end

Conversation

@rohitg00
Copy link
Copy Markdown
Owner

@rohitg00 rohitg00 commented May 20, 2026

Summary

Closes #493.

Codex sessions stayed status: "active" in the viewer forever because Codex doesn't fire a separate SessionEnd hook and the existing Stop handler only called /agentmemory/summarize.

session-end.mjs already POSTs /agentmemory/session/end, but it's only wired to Claude Code's SessionEnd event. Codex has no equivalent.

Fix

src/hooks/stop.ts now also POSTs /agentmemory/session/end after the summarize call, best-effort with a 5s timeout. For Claude Code this is a harmless idempotent second call (the dedicated SessionEnd hook still runs and sets the same endedAt + status fields). For Codex it's the only path that closes the lifecycle.

Diff

  • src/hooks/stop.ts+18 adds the second fetch
  • plugin/scripts/stop.mjs+8 regenerated by tsdown

Validation

  • npm test → 97/97 test files, 1081/1081 tests pass
  • npm run build → bundle clean

Notes on idempotency

POST /agentmemory/session/end is a kv.update with two {type: "set"} ops on endedAt and status. Repeating it is safe and overwrites with the current time + "completed" — desired behavior since Stop fires after the user-visible session is over.

If a follow-up sets a stricter "don't overwrite endedAt if already set" guard at the server side, that's orthogonal and won't regress this fix.

Summary by CodeRabbit

  • Improvements
    • Sessions are now properly terminated when stopping the agent, with enhanced reliability.

Review Change Stack

Codex does not fire a separate SessionEnd event, so its Stop hook is
the only signal we get when a Codex session terminates. The current
stop hook only POSTs /agentmemory/summarize, leaving the session row
stuck on status:"active" in the viewer for Codex users (#493).

Stop now ALSO POSTs /agentmemory/session/end, best-effort with a
short 5s timeout. For Claude Code this is a harmless idempotent second
call (session-end.mjs runs on the dedicated SessionEnd hook and sets
the same endedAt + status fields). For Codex it's the only path that
closes the lifecycle.

Tests (1081) + build pass. plugin/scripts/stop.mjs regenerated by tsdown.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agentmemory Ready Ready Preview, Comment May 20, 2026 4:10pm

Request Review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ed5b18fe-8ce3-4f20-b2b0-74b1e3d040d0

📥 Commits

Reviewing files that changed from the base of the PR and between edd1ceb and c42b16a.

📒 Files selected for processing (2)
  • plugin/scripts/stop.mjs
  • src/hooks/stop.ts

📝 Walkthrough

Walkthrough

The PR adds session cleanup requests to both the plugin stop script and TypeScript stop hook. After each sends a summarize request to agentmemory, they now issue a best-effort POST to /agentmemory/session/end with a 5-second timeout, silently ignoring failures.

Changes

Session cleanup on stop

Layer / File(s) Summary
Session-end POST after summarize
plugin/scripts/stop.mjs, src/hooks/stop.ts
Both the plugin stop script and TypeScript stop hook now POST to /agentmemory/session/end with sessionId after the summarize call, using a 5-second AbortSignal.timeout and silent error handling via try/catch.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

  • rohitg00/agentmemory#493: Adds the suggested session-end call after summarize to mark Codex sessions as ended in both stop implementations.

Possibly related PRs

  • rohitg00/agentmemory#501: Directly conflicts—reverts the Codex Stop hook's session-end behavior while this PR adds it.
  • rohitg00/agentmemory#495: Wires Codex's Stop hook to run session-end.mjs after stop.mjs, while this PR adds the session-end call inline within the stop flow.

Poem

🐰 Sessions now close with grace and care,
Two stops ensure the cleanup's there,
With five-swift seconds and errors denied,
The memory session bids goodbye! 🌙

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(hooks): stop also closes session for Codex (closes #493)' directly describes the main change: adding session-end functionality to the stop hook for Codex integration.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/codex-stop-session-end

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Codex Stop hook does not mark sessions ended

1 participant