1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! `mimir context-guard <subcommand>` — the hook-event entry points behind
//! [`mimir_core::context_guard`] (context-window pause/handoff nudges) and
//! [`mimir_core::anchors`] (guard anchors). Each subcommand reads one hook
//! event's JSON from stdin.
//!
//! Field names and the "how does this reach the model" mechanics below were
//! verified against the live Claude Code hooks docs while implementing
//! this (not assumed):
//! - `UserPromptSubmit` and `SessionStart` are the two events where plain
//! stdout (no JSON envelope) is injected as context.
//! - `PreToolUse` supports `hookSpecificOutput.additionalContext` to add
//! context WITHOUT touching the allow/deny decision — it is not limited
//! to `permissionDecision`/`permissionDecisionReason`.
//! - Blocking (`PreCompact`'s `{"decision":"block","reason":...}`) must be
//! printed to stdout on exit 0. Exit 2 is a separate, mutually exclusive
//! mechanism that reads stderr instead and ignores any JSON — mixing the
//! two does nothing useful, so these subcommands always exit 0.
//!
//! Fail-open throughout: a parse error, missing field, or DB error is
//! swallowed (treated as "say nothing"), never a nonzero exit — a hook that
//! can break a prompt/tool-call/compact by erroring is worse than one that
//! occasionally stays quiet.
use Read as _;
use Result;
use context_guard;
use Scope;
use Mimir;
/// Scope inferred from a hook event's `cwd` field: the project it maps to,
/// or `Scope::Global` if there isn't one (no project marker at/above cwd,
/// or `cwd` missing). Mirrors `commands::read_scope`'s project-detection
/// fallback, but hooks never pass `--global`/`--all`, so there is nothing
/// to thread through beyond the one lookup.
/// `UserPromptSubmit`: nudge toward a deliberate `/clear`/`/compact` (or a
/// handoff-then-clear) once the transcript crosses the configured
/// threshold. Plain stdout, no JSON — see the module doc comment. Silent
/// whenever `[hooks] context_guard = "off"` (the default), below
/// threshold, or already nagged at this +10% band this session
/// (`context_guard::should_emit`).
/// `PreCompact`: on an AUTO-triggered compaction only — never a user's
/// explicit `/compact`, which always stays silent — blocks it with the
/// guard message as the block reason once over threshold. Unlike
/// [`prompt`], there is no +10% nag cadence here: every auto-compact
/// attempt while over threshold is blocked, since letting even one through
/// silently would hand control of *when* a compact happens back to Claude
/// Code, defeating the point of `"pause"`/`"handoff"` mode.
/// `SessionStart`: updates the transcript-marker/nag bookkeeping
/// (`context_guard::handle_session_start`) for every `source`, and in
/// `"handoff"` mode, restores the most recent `session-handoff`-tagged
/// memory as plain-stdout context right after a `/clear` or a `compact`.
/// Entirely skipped — no DB writes either — when `context_guard = "off"`,
/// so an off install leaves zero new `session_state` rows behind.
/// `PreToolUse` (Bash/Edit/Write matcher): guard anchors — see
/// `mimir_core::anchors`. Independent of `[hooks] context_guard`: dormant
/// until a memory declares `meta.anchors` via `mimir remember --anchor`.