Skip to content

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

Closed

Description

@fdaviddpt

The problem

hooks/hooks.json registers SessionStart, UserPromptSubmit and PostToolUse. There is no hook on the way out, so the end of a session is saved only by luck.

Saving is driven by post-tool-hook.sh, gated on thresholds.delta_lines_trigger and cooldowns.save_seconds (120s). Whatever happens after the last save that clears those gates is never written. A session that ends in conversation rather than tool calls — a design discussion, a review, a decision — can lose its entire final stretch, which is often the part worth keeping.

features.recovery exists to soften this: the next session start looks for what the previous one missed. That is a repair positioned after the loss instead of before it, and it only helps if there is a next session in the same project.

The same gap hits the handoff. /remember is manual, so it is skipped exactly when it matters most — long sessions that end abruptly. Related: #341, where the handoff banner re-announces a stale note because nothing has replaced it.

Proposed change

Register a SessionEnd hook that unconditionally:

  1. flushes the current buffer to now.md, ignoring the cooldown and the delta threshold — this is the last chance, not a routine tick;
  2. writes the handoff note, so /remember becomes an override rather than the only path;
  3. exits 0 on every failure path, like the other hooks.

Open question — settle this first

When does SessionEnd actually fire? Specifically: /clear, a clean quit, a crash, a killed terminal, and a session that ends by hitting the usage cap.

This determines whether the change is worth building. If it only fires on a graceful exit, it fixes the polite case and misses the one that loses the most work — and the honest conclusion may be that recovery must stay regardless. Check the current hook reference before writing code; do not infer the behaviour from the name.

Also worth confirming whether the payload distinguishes the reason for ending, since a /clear and a crash may deserve different handling.

Acceptance

  • Behaviour confirmed against the hook documentation and recorded in this issue before implementation starts.
  • A test asserting the flush happens with the cooldown unexpired and the delta below threshold.
  • Tests for empty input and a missing data dir, matching the existing hooks fail-soft contract.
  • README and docs/ updated with the new hook in the table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority:high

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions