Repository navigation
fix(memory): run SessionEnd capture detached so the exit hook can't be cancelled - #190
Merged
Merged
Conversation
…e cancelled
The SessionEnd hook (`kit memory hook session-end`) did its work synchronously
in the foreground: `runSessionEndIndex()` (which, once per 6h, sweeps every
harness) plus the opt-in `tryAutoPush()` (a network push). Claude Code puts a
SessionEnd hook on a short leash and cancels it if it doesn't return promptly,
so on exit users saw:
SessionEnd hook [.../kit memory hook session-end] failed: Hook cancelled
The mid-session index already avoids this by spawning a detached, unref'd
worker; SessionEnd did not. Mirror that pattern: `session-end` now fires a
detached `kit memory hook session-end-run` and returns instantly, and the new
internal `session-end-run` event does the real (slow / networked) capture in
the background where nothing is waiting on it. The just-ended transcript is
already on disk, so capture is unchanged.
Fail-safe: if there's no entry path to re-exec, it indexes inline rather than
not at all, and a SessionEnd hook still never throws.
Test: hook.test.ts covers the inline-fallback branch (no detach, no throw).
Full suite green (1977 tests).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbF4MFePXWNCd4dxBQvqH2
|
✅ Docker image built successfully
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On exit, Claude Code reports:
The
session-endbranch ofmemHook()does its work synchronously in the foreground:runSessionEndIndex()(which once per 6h sweeps every harness — codex/cursor/gemini/cline/amazon-q/opencode) plus the opt-intryAutoPush()(a network push whenpushOnEnd+KIT_MEMORY_PASSPHRASEare set). Claude Code keeps a SessionEnd hook on a short leash and cancels it if it doesn't return promptly — so a slow index or a hanging push surfaces as "Hook cancelled".The mid-session index already sidesteps this by spawning a detached, unref'd worker (
maybeStartMidSessionIndex); SessionEnd did not. This PR makes SessionEnd follow the same pattern.No memory is lost today (mid-session indexing runs every ~10 min detached), but the exit-time capture/push was unreliable and noisy.
Type of Change
Related Issues
SessionEnd hook [...kit memory hook session-end] failed: Hook cancelled.Changes Made
src/memory/hook.ts: addstartDetachedSessionEnd()— fire-and-forgetkit memory hook session-end-run(detached,stdio: "ignore",unref'd), mirroringmaybeStartMidSessionIndex. Fail-safe: if there's noprocess.argv[1]to re-exec, it indexes inline instead of skipping, and never throws.src/commands/memory.ts: thesession-endevent now callsstartDetachedSessionEnd()and returns instantly; a new internalsession-end-runevent runs the real (slow / networked) capture —runSessionEndIndex()+tryAutoPush()— in the detached worker where nothing is waiting on it. Behavior is otherwise identical (same index logic, same 6h sweep debounce, same opt-in push gate).src/memory/hook.test.ts: cover the inline-fallback branch (no entry path → does not detach, does not throw).The just-ended transcript is already on disk when SessionEnd fires, so the detached worker captures the same session a synchronous run would.
Testing
Test Coverage
startDetachedSessionEnd)npm test) — 1977 passed, 0 failedManual Testing
npm run build→ exit 0.node --test dist/memory/hook.test.js→ 13 pass (incl. the new SessionEnd test).Breaking Changes
None / N/A —
session-end-runis an internal, undocumented hook event; the installed hooks (UserPromptSubmit/SessionEnd/SessionStart) are unchanged.Checklist
maybeStartMidSessionIndexdetached pattern)npm test)Performance Impact
Migration Guide
N/A
Reviewer Notes
Two judgment calls worth a look: (1) detaching at SessionEnd relies on the unref'd child outliving the parent (same assumption the mid-session index already makes); (2) the opt-in
tryAutoPushnow runs in the detached worker, so its stderr note is no longer surfaced to the foreground — acceptable since the foreground is exiting, but flagging it.🤖 Generated with Claude Code
Generated by Claude Code