Skip to content

Add a SessionEnd hook that flushes what PostToolUse missed - #368

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/345
Aug 27, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/345

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Summary

  • Closes Register a SessionEnd hook so the tail of a session is never lost #345. Adds scripts/session-end-hook.sh, wired into hooks/hooks.json's new SessionEnd entry, which forks save-session.sh --force into the background at session end -- bypassing the save cooldown and min-human-message gate that exist to throttle a live session, not its last moment.
  • Deliberately does not write a handoff note (remember.md): /remember composes it from the model's own first-person recollection of the session, and there is no model turn running at SessionEnd for a hook to narrate from. A fabricated placeholder would silently overwrite a real handoff with something worth less than what it replaced. See README's new SessionEnd section and the changelog fragment for the full reasoning.
  • Open question settled first, per the brief: whether SessionEnd actually fires on a crash/kill/usage-cap ending is NOT established by the current Claude Code hooks reference (checked via the claude-code-guide agent, 2026-08) -- it documents clear/logout/prompt_input_exit/resume and is silent on the abrupt paths. Built anyway, since it demonstrably helps the common graceful-ending case (the one the issue itself is mostly about -- 'a session that ends in conversation'), and features.recovery's next-session-start repair is left in place unchanged, since this hook structurally cannot reach what it cannot reach.

Self-review changed the design

Two agents (Explore reviewer, oss:auditor) reviewed the first commit. Their findings moved this from a foreground/blocking flush to a backgrounded one, in a second commit:

  • Fixed: the flush now forks into the background (matching post-tool-hook.sh's own pattern) rather than blocking in the foreground. Claude Code kills a hook process at ~60s of its own accord (this repo's own README documents it), while save-session.sh's own Haiku call already asks for up to 120s (180s for NDC compression) -- comfortably past 60s on exactly the long, content-heavy sessions this hook exists to rescue. A foreground wait risked losing the entire flush to that kill, silently.
  • Fixed: the two early exits (REMEMBER_DIR missing, save-session.sh missing) now call report_error() before exiting -- previously indistinguishable in hook-errors.log from the ordinary 'nothing new to flush' case.
  • Filed below, not fixed here -- lock contention can silently defeat the --force flush: save-session.sh acquires save.lock with a 0-second timeout before parsing --force, so a save-session.sh forked in the background by the session's last tool call can still hold the lock when SessionEnd fires moments later; the flush then loses the race and exits 0 having done nothing. Fixing this touches save-session.sh's shared lock semantics, used by every caller (including post-tool-hook.sh's own background fork and the NDC subshell) -- a real design decision (retry? extend the --force bypass to the lock too? at what cost to the other callers?), not a one-line change, and outside this issue's blast radius.
  • Filed below, not fixed here -- /remember:doctor has no SessionEnd liveness check, unlike its detailed PostToolUse alive-marker logic (scripts/doctor.sh:266-298). Adding one needs its own marker convention and verdict-logic integration inside a large, carefully reasoned diagnostic script -- a design decision beyond Register a SessionEnd hook so the tail of a session is never lost #345's acceptance criteria, which name README/docs, not doctor.sh.

Test plan

  • New test file tests/test_session_end_hook_345.py, 6 tests, TDD: red (all 6 fail with FileNotFoundError before the hook script existed) then green (6/6 pass) -- re-verified red/green again after the background-flush redesign.
  • Every 'must fire' case paired with a 'must not fire' positive control in the same fixture (a plain save-session.sh call, no --force, against the identical fixture, to prove the cooldown marker is actually load-bearing).
  • Fail-soft contract: empty stdin, no project at all, and a project whose .remember store never existed -- all still exit 0.
  • tests/test_hooks_json.py (wiring guard) green.
  • Changelog fragment passes .oss/assemble_changelog.py --check.
  • Full suite run twice (once before, once after the background-flush redesign): 1628 passed, 43 skipped, 0 failed both times.

fdaviddpt and others added 2 commits August 27, 2026 13:02
session-end-hook.sh calls save-session.sh --force once, in the
foreground, at session end -- bypassing the save cooldown and the
min-human-message gate that exist to throttle a live session's
routine saves, not its last moment. A session that ends in
conversation rather than tool calls can otherwise lose its entire
final stretch if nothing after the last save cleared those gates.

It deliberately does not write a handoff note: /remember composes
remember.md from the model's own first-person recollection of the
session, and there is no model turn running at SessionEnd for a hook
to narrate from. A fabricated placeholder would silently overwrite a
real handoff with something worth less than what it replaced.

Whether SessionEnd fires on a crash, a killed terminal, or a session
ending at the usage cap is not established by the current Claude
Code hooks reference -- it documents the graceful exits (clear,
logout, prompt_input_exit, resume) and is silent on the abrupt ones.
features.recovery's next-session-start repair stays in place
unchanged for exactly that gap.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FLHVEH4aeVK93939UYnfFP
Self-review (Explore + oss:auditor) surfaced that Claude Code kills a
hook process at ~60s of its own accord (documented in this repo's own
README), while save-session.sh's own Haiku call already asks for up
to 120s (180s for NDC compression) -- comfortably past 60s on exactly
the long, content-heavy sessions this hook exists to rescue. A
foreground, blocking call risked losing the entire flush to that
kill, silently, which undercut the feature's own guarantee.

session-end-hook.sh now forks save-session.sh --force into a
background subshell and returns immediately, reusing the same
tmp/save-session.pid marker post-tool-hook.sh's own background fork
already writes. A flush failure is still reported loudly via
report_error(), once the subshell finishes, rather than lost with no
trace. Also makes the two early "nothing to flush to" exits
(REMEMBER_DIR / save-session.sh missing) report_error() instead of
exiting silently indistinguishable from "nothing new to save" --
flagged by the auditor as a real absence in the log a reader could
not otherwise tell apart.

Tests updated to reap the backgrounded flush (same shape as
tests/test_post_tool_cooldown.py::_reap) before asserting on the
stubbed pipeline.shell call log. README and the changelog fragment
updated to describe the backgrounded design instead of the earlier
foreground one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FLHVEH4aeVK93939UYnfFP
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.

Register a SessionEnd hook so the tail of a session is never lost

1 participant