gstack/hosts/claude/hooks
Garry Tan d3aceb6c51
feat(hooks): Layer 8 memory injection via per-session cache
Plan-tune cathedral T12. Extends the PreToolUse hook to inject matching
free-text-memory.json nuggets into AskUserQuestion responses, giving the
agent + user the distilled context from past 'Other' answers right when
the related question fires.

Per-session cache (D13 perf): first read of free-text-memory.json writes
~/.gstack/sessions/<id>/memory-cache.json. Subsequent hooks on the same
session take the cached path. Invalidation is by file-missing: when the
canonical file changes (via gstack-distill-apply), the per-session cache
either reflects the staler view for the rest of the session or the
session restarts and the cache rebuilds. Cheap, correct enough for v1.

Matching logic:
  - Walk this AUQ batch's questions, extract marker question_ids.
  - Look up signal_key in scripts/question-registry.ts.
  - Collect nuggets whose applies_to_signal_keys include any of the
    matched signal_keys.
  - Cap to 3 most-recent (by applied_at) so the additionalContext stays
    short.
  - Surface as additionalContext on the hookSpecificOutput response.

Memory + enforcement interact cleanly: the same hook can both surface
nuggets AND deny the tool when a never-ask preference matches. Memory
context isn't doubled in the deny reason — the auto-decided option name
in the deny path is sufficient signal.

6 new tests cover injection on defer, no-match silence, 3-most-recent cap,
memory-alongside-deny enforcement, cache file write-through, empty-canonical
graceful degradation. Existing 15 preference-hook tests still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 07:51:00 -07:00
..
question-log-hook feat(hooks): PostToolUse capture hook for AskUserQuestion 2026-05-27 07:42:46 -07:00
question-log-hook.ts feat(hooks): PostToolUse capture hook for AskUserQuestion 2026-05-27 07:42:46 -07:00
question-preference-hook feat(hooks): PreToolUse enforcement hook for AskUserQuestion preferences 2026-05-27 07:44:26 -07:00
question-preference-hook.ts feat(hooks): Layer 8 memory injection via per-session cache 2026-05-27 07:51:00 -07:00