Repository navigation
Conversation
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
This was referenced Aug 27, 2026
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.
Summary
scripts/session-end-hook.sh, wired intohooks/hooks.json's newSessionEndentry, which forkssave-session.sh --forceinto 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.remember.md):/remembercomposes it from the model's own first-person recollection of the session, and there is no model turn running atSessionEndfor 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 newSessionEndsection and the changelog fragment for the full reasoning.SessionEndactually fires on a crash/kill/usage-cap ending is NOT established by the current Claude Code hooks reference (checked via theclaude-code-guideagent, 2026-08) -- it documentsclear/logout/prompt_input_exit/resumeand 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'), andfeatures.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: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), whilesave-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.REMEMBER_DIRmissing,save-session.shmissing) now callreport_error()before exiting -- previously indistinguishable inhook-errors.logfrom the ordinary 'nothing new to flush' case.--forceflush:save-session.shacquiressave.lockwith 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 whenSessionEndfires moments later; the flush then loses the race and exits 0 having done nothing. Fixing this touchessave-session.sh's shared lock semantics, used by every caller (includingpost-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./remember:doctorhas noSessionEndliveness check, unlike its detailedPostToolUsealive-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, notdoctor.sh.Test plan
tests/test_session_end_hook_345.py, 6 tests, TDD: red (all 6 fail withFileNotFoundErrorbefore the hook script existed) then green (6/6 pass) -- re-verified red/green again after the background-flush redesign.save-session.shcall, no--force, against the identical fixture, to prove the cooldown marker is actually load-bearing)..rememberstore never existed -- all still exit 0.tests/test_hooks_json.py(wiring guard) green..oss/assemble_changelog.py --check.