Skip to content

fix(memory): run SessionEnd capture detached so the exit hook can't be cancelled - #190

Merged
sandstream merged 1 commit into
mainfrom
claude/memory-session-end-detached
Jun 30, 2026
Merged

sandstream merged 1 commit into
mainfrom
claude/memory-session-end-detached

Conversation

@sandstream

Copy link
Copy Markdown
Owner

Description

On exit, Claude Code reports:

SessionEnd hook [.../kit memory hook session-end] failed: Hook cancelled

The session-end branch of memHook() 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-in tryAutoPush() (a network push when pushOnEnd + KIT_MEMORY_PASSPHRASE are 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

  • Bug fix (non-breaking change that fixes an issue)

Related Issues

  • Reported in the wild: SessionEnd hook [...kit memory hook session-end] failed: Hook cancelled.

Changes Made

  • src/memory/hook.ts: add startDetachedSessionEnd() — fire-and-forget kit memory hook session-end-run (detached, stdio: "ignore", unref'd), mirroring maybeStartMidSessionIndex. Fail-safe: if there's no process.argv[1] to re-exec, it indexes inline instead of skipping, and never throws.
  • src/commands/memory.ts: the session-end event now calls startDetachedSessionEnd() and returns instantly; a new internal session-end-run event 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

  • Added new tests (inline-fallback branch of startDetachedSessionEnd)
  • All tests pass (npm test) — 1977 passed, 0 failed

Manual Testing

  1. npm run build → exit 0.
  2. node --test dist/memory/hook.test.js → 13 pass (incl. the new SessionEnd test).
  3. Full suite: 1977 pass / 0 fail.

Breaking Changes

None / N/A — session-end-run is an internal, undocumented hook event; the installed hooks (UserPromptSubmit / SessionEnd / SessionStart) are unchanged.

Checklist

  • My code follows the project's style guidelines (mirrors the existing maybeStartMidSessionIndex detached pattern)
  • I've updated documentation as needed (n/a — internal behavior)
  • All new code has test coverage
  • All tests pass locally (npm test)
  • No new warnings or errors introduced
  • Commit messages follow conventions
  • No hardcoded secrets or sensitive data
  • Code has been self-reviewed

Performance Impact

  • Improves performance — the exit hook returns immediately instead of blocking on an index / network push.

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 tryAutoPush now 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

…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
@github-actions

Copy link
Copy Markdown

✅ Docker image built successfully

  • CLI image: docker.io/sandstream/kit:test

@sandstream
sandstream merged commit b6e8a36 into main Jun 30, 2026
17 checks passed
@sandstream
sandstream deleted the claude/memory-session-end-detached branch July 1, 2026 09:35
@sandstream sandstream mentioned this pull request Jul 1, 2026
2 of 5 tasks
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