diff options
| author | Craig Jennings <c@cjennings.net> | 2026-06-02 20:36:18 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-06-02 20:36:18 -0500 |
| commit | 526df6dd7872890e525b9af33917169cfdf705d8 (patch) | |
| tree | 0db8e87a7099f525a45d4592269368109b70e615 /hooks | |
| parent | 4a5a30280c541006be04f47ebb45c8aab28e9f3c (diff) | |
| download | rulesets-526df6dd7872890e525b9af33917169cfdf705d8.tar.gz rulesets-526df6dd7872890e525b9af33917169cfdf705d8.zip | |
feat(flush): add /flush skill and SessionStart(clear) resume hook
Flush is the checkpoint half of the wrap/restart rhythm. It refreshes the session-context anchor in place, the user runs /clear, and the session resumes from the anchor instead of starting cold. One logical session stays alive across a /clear boundary without the archive-and-commit of wrap-it-up or the full cold boot of startup, which buys cheaper tokens and a sharper context window.
The mechanism splits into two halves around /clear, which wipes the conversation so nothing runs straight through it. The /flush skill is the pre-clear half: dump live state, refresh the anchor's Summary, append a dated flush marker, verify the write landed, then prompt the user to /clear. The agent can initiate at a clean task boundary on its own judgment, but /clear is user-only, so the agent does the work and the user supplies the single keystroke. The session-clear-resume.sh hook is the post-clear half, a SessionStart matcher=clear hook that points the fresh session at the anchor to resume, or at startup when no anchor exists.
I packaged the pre-clear half as a skill rather than a project-workflow doc so both halves are global. The hook was already global, so /flush is now callable by name from any project with no per-project sync.
The hook is canonicalized under hooks/ and symlinked into ~/.claude/hooks/, matching precompact-priorities.sh. settings.json wires the SessionStart entry, and settings-snippet.json carries it so a fresh machine wires the hook on make install-hooks.
Diffstat (limited to 'hooks')
| -rwxr-xr-x | hooks/session-clear-resume.sh | 55 | ||||
| -rw-r--r-- | hooks/settings-snippet.json | 8 |
2 files changed, 63 insertions, 0 deletions
diff --git a/hooks/session-clear-resume.sh b/hooks/session-clear-resume.sh new file mode 100755 index 0000000..6692f54 --- /dev/null +++ b/hooks/session-clear-resume.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# SessionStart(clear) hook: after /clear, point the fresh context at the +# session-context anchor so a "flush" resumes the same logical session +# instead of starting cold. +# +# Pairs with the /flush skill (rulesets flush/SKILL.md): the skill's +# pre-clear half refreshes .ai/session-context.org in place; this hook is the +# post-clear half that /clear's blank session can't run itself. +# +# General by design — not flush-specific: +# - anchor present -> resume (read it, say "flushed.", continue) +# - anchor absent -> run the startup workflow (covers a stray /clear) +# +# SessionStart hooks reach the model ONLY through +# hookSpecificOutput.additionalContext — plain stdout goes to the debug log. +# After /clear the injected context loads but the model still waits for the +# user's next message before it acts; that one keystroke is expected. +# +# Wire in ~/.claude/settings.json: +# +# { +# "hooks": { +# "SessionStart": [ +# { +# "matcher": "clear", +# "hooks": [ +# { "type": "command", +# "command": "~/.claude/hooks/session-clear-resume.sh" } +# ] +# } +# ] +# } +# } +set -euo pipefail + +# Resolve the AI_AGENT_ID-aware anchor path — mirror of +# .ai/scripts/session-context-path, evaluated against the project cwd the +# hook inherits. Fall back to the singleton path when the id is unset. +id="${AI_AGENT_ID:-}" +if [ -n "$id" ]; then + safe=$(printf '%s' "$id" | tr -c 'A-Za-z0-9._-' '_') + sc=".ai/session-context.d/${safe}.org" +else + sc=".ai/session-context.org" +fi + +if [ -f "$sc" ]; then + ctx="A context flush just happened (/clear). The session-context anchor is at ${sc}. Read it now — the * Summary (Active Goal, Next Steps) plus the most recent * Session Log entries — then reply with 'flushed.' on its own line, followed by one line restating the Active Goal and the immediate Next Step, and resume that work. This is a mid-session resume, NOT a fresh start: do not run the startup workflow, do not re-pull rulesets or re-sync templates." +else + ctx="Session started with no session-context anchor present (.ai/session-context.org absent). Run the startup workflow at .ai/workflows/startup.org." +fi + +# Build the whole object in python3 so the additionalContext string is +# JSON-escaped correctly regardless of its content. +python3 -c 'import json,sys; print(json.dumps({"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":sys.argv[1]}}))' "$ctx" diff --git a/hooks/settings-snippet.json b/hooks/settings-snippet.json index 11459f2..1be5f00 100644 --- a/hooks/settings-snippet.json +++ b/hooks/settings-snippet.json @@ -1,5 +1,13 @@ { "hooks": { + "SessionStart": [ + { + "matcher": "clear", + "hooks": [ + { "type": "command", "command": "~/.claude/hooks/session-clear-resume.sh" } + ] + } + ], "PreCompact": [ { "hooks": [ |
