rungate 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hooks/ActionVerificationGate.hook.ts +96 -0
- package/hooks/AgentBriefGuard.hook.ts +38 -0
- package/hooks/AgentVerdictCapture.hook.ts +87 -0
- package/hooks/AutoVerifyGate.hook.ts +79 -0
- package/hooks/BashToolGuard.hook.ts +65 -0
- package/hooks/BashWriteGuard.hook.ts +60 -0
- package/hooks/CommitEnforcement.hook.ts +130 -0
- package/hooks/DocHygiene.hook.ts +148 -0
- package/hooks/GateEnforcement.hook.ts +58 -0
- package/hooks/GateResultProtection.hook.ts +69 -0
- package/hooks/HarnessAFKGate.hook.ts +85 -0
- package/hooks/HookCurrency.hook.ts +99 -0
- package/hooks/IssueCloseGuard.hook.ts +149 -0
- package/hooks/MergeGuard.hook.ts +58 -0
- package/hooks/PostCompact.hook.ts +60 -0
- package/hooks/ProjectStateSync.hook.ts +103 -0
- package/hooks/SkillGuard.hook.ts +69 -0
- package/hooks/SpecConformityTrigger.hook.ts +43 -0
- package/hooks/SpecSCGuard.hook.ts +123 -0
- package/hooks/StaleTTLCleanup.hook.ts +141 -0
- package/hooks/TaskCompleted.hook.ts +51 -0
- package/hooks/TeamInit.hook.ts +85 -0
- package/hooks/TestSuiteGuard.hook.ts +70 -0
- package/hooks/TestSuiteRelease.hook.ts +85 -0
- package/hooks/TopLevelCdGuard.hook.ts +108 -0
- package/hooks/VerifyPhaseLock.hook.ts +84 -0
- package/hooks/WorkflowStateGuard.hook.ts +40 -0
- package/hooks/lib/agent-config.json +9 -0
- package/hooks/lib/agentDetection.ts +201 -0
- package/hooks/lib/comment-template.ts +86 -0
- package/hooks/lib/findWorkflow.ts +101 -0
- package/hooks/lib/parseStdin.ts +37 -0
- package/hooks/lib/paths.ts +111 -0
- package/hooks/lib/utils.ts +338 -0
- package/lib/affected-tests.ts +29 -0
- package/lib/bash-write-guard.ts +373 -0
- package/lib/branch-cleanup.ts +279 -7
- package/lib/config-trackable.ts +9 -0
- package/lib/distribution-currency.ts +55 -5
- package/lib/failure-intake.ts +7 -1
- package/lib/gate-enforcement.ts +222 -28
- package/lib/gate-names.ts +72 -0
- package/lib/gate-result-protection.ts +427 -0
- package/lib/intake-sources.ts +380 -0
- package/lib/late-env-spawn.ts +331 -0
- package/lib/launch-base.ts +128 -5
- package/lib/paths.ts +96 -3
- package/lib/phase2-decomposition.ts +1 -1
- package/lib/published-artifact.ts +162 -18
- package/lib/sc-id-cross-tree.ts +84 -8
- package/lib/scaffold/ai-review-findings.ts +7 -1
- package/lib/scaffold/ai-review-step.ts +861 -116
- package/lib/scaffold/defaults.ts +82 -1
- package/lib/scaffold/steps.ts +436 -44
- package/lib/security-verdict.ts +1 -1
- package/lib/shell-path-interpolation.ts +594 -0
- package/lib/text-dependents.ts +875 -0
- package/lib/workflow-security.ts +29 -0
- package/lib/workflow-taint.ts +732 -0
- package/lib/worktree-cleanup.ts +65 -38
- package/lib/worktree-repo.ts +32 -2
- package/package.json +3 -1
- package/scripts/check-installed-hook-currency.ts +825 -0
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gate result protection — the decision behind `GateResultProtection.hook.ts`.
|
|
3
|
+
*
|
|
4
|
+
* Deep module: the hook file is a thin trigger (SC-371) that parses stdin and
|
|
5
|
+
* prints; every reading of the tool call happens here, so there is one site
|
|
6
|
+
* to mutate and one site to test.
|
|
7
|
+
*
|
|
8
|
+
* Issue: #1137. Original hook: #471, migrated by #713.
|
|
9
|
+
*
|
|
10
|
+
* ## The property
|
|
11
|
+
*
|
|
12
|
+
* A gate verdict must not be writable by the thing being gated. The guard
|
|
13
|
+
* therefore has to be at least as good at reading JSON as the readers of
|
|
14
|
+
* `workflow-state.json` are, and the version this replaces was not.
|
|
15
|
+
*
|
|
16
|
+
* ## Three ways the first draft could be stepped around, and the shape of each fix
|
|
17
|
+
*
|
|
18
|
+
* **Spelling.** It enumerated two spacings of one key with `String.includes`,
|
|
19
|
+
* so `"result" : "PASS"` walked past. Fixed by deciding on parsed structure
|
|
20
|
+
* where there is one, and on a whitespace-tolerant, escape-decoded pattern
|
|
21
|
+
* where there is not.
|
|
22
|
+
*
|
|
23
|
+
* **A list that could go stale.** It enumerated `scope`, `verify`, `ship`.
|
|
24
|
+
* `gates/gate-executor.ts` also calls `writeGateResult` with `merge`, `gaps`
|
|
25
|
+
* and (through its dynamic call) `prove`, so three persisted gate results
|
|
26
|
+
* were unguarded. The fix is NOT a longer list — it is no list: a `result`
|
|
27
|
+
* under ANY key of `gates` is refused. An allowlist on a security boundary is
|
|
28
|
+
* correct until someone adds a gate, and nothing makes the next person update
|
|
29
|
+
* it. Gate-agnostic cannot go stale, so "is the list complete?" stops being
|
|
30
|
+
* askable. See `lib/gate-names.ts` for why the lists that remain are not this.
|
|
31
|
+
*
|
|
32
|
+
* **Erasure.** It asked "does the incoming content contain a result?", so a
|
|
33
|
+
* bypass only had to make the answer no. A forged PASS was the attack it was
|
|
34
|
+
* written against; DELETING a FAIL reaches the same end and was invisible to
|
|
35
|
+
* any content-only reading. So the decision now also reads what is already at
|
|
36
|
+
* `file_path` and refuses a write that drops a result that is there.
|
|
37
|
+
*
|
|
38
|
+
* ## Two inputs, two readings, and why they differ
|
|
39
|
+
*
|
|
40
|
+
* **`Write` hands us the whole file**, so it is parsed, and the decision is
|
|
41
|
+
* taken on the parsed structure. That is strictly better than any pattern:
|
|
42
|
+
* `JSON.parse` normalises whitespace, key order, and `\uXXXX` escapes, so
|
|
43
|
+
* `{"phase": "DONE"}` arrives as `phase` with no decoder of our own.
|
|
44
|
+
*
|
|
45
|
+
* **`Edit` hands us a fragment** — `new_string` is spliced into the file, and
|
|
46
|
+
* a fragment is usually not valid JSON — so there is nothing to parse and the
|
|
47
|
+
* reading falls back to a pattern. Two consequences follow:
|
|
48
|
+
*
|
|
49
|
+
* - The pattern tolerates whitespace, because JSON does.
|
|
50
|
+
* - `\uXXXX` IS decoded first, by hand. On the Write path the parser does
|
|
51
|
+
* this for us; here nothing does, and the escape is a live bypass rather
|
|
52
|
+
* than a curiosity: the Edit tool splices literally, so `"result"`
|
|
53
|
+
* lands in the file as those twelve characters and the next reader's
|
|
54
|
+
* `JSON.parse` turns it back into `result`. A guard that reads the text
|
|
55
|
+
* less carefully than the consumer does is not a guard.
|
|
56
|
+
*
|
|
57
|
+
* The decoder is deliberately naive — it does not track whether the
|
|
58
|
+
* backslash was itself escaped, so the literal six characters `\\u0072` in a
|
|
59
|
+
* JSON string are decoded too. That over-blocks a string nobody writes into
|
|
60
|
+
* this file, and the direction is the correct one to be wrong in.
|
|
61
|
+
*
|
|
62
|
+
* ## Both branches read the disk, because they are one rule
|
|
63
|
+
*
|
|
64
|
+
* An earlier draft decided the Edit branch from the two string fragments
|
|
65
|
+
* alone. That caught erasure OF A RESULT and missed erasure of the PATH to
|
|
66
|
+
* one: `old_string: '"gates"'`, `new_string: '"gates_old"'` contains no
|
|
67
|
+
* forbidden substring, leaves the file valid JSON, and moves every gate
|
|
68
|
+
* record out of reach of `collectGateResults`. Measured ALLOWED.
|
|
69
|
+
*
|
|
70
|
+
* So the Edit branch now reads the file, applies the splice in memory,
|
|
71
|
+
* parses the result, and compares `collectGateResults` before against after
|
|
72
|
+
* — the same reading the Write branch takes. The fragment patterns survive
|
|
73
|
+
* only as a FAST PRE-FILTER that can refuse without I/O; they are never the
|
|
74
|
+
* decision. Two implementations of one rule is why the branches could
|
|
75
|
+
* disagree at all.
|
|
76
|
+
*
|
|
77
|
+
* A gate result that is ADDED, REMOVED or CHANGED all refuse. Re-running a
|
|
78
|
+
* gate legitimately changes `PASS`↔`FAIL`, and that is precisely the write
|
|
79
|
+
* this hook exists to route through the gate runner instead — a re-run is
|
|
80
|
+
* not a reason to let the thing being gated edit the verdict by hand.
|
|
81
|
+
*
|
|
82
|
+
* ## UNMEASURED refuses, including the prior state
|
|
83
|
+
*
|
|
84
|
+
* A workflow state is always a JSON object. Content that does not parse, or
|
|
85
|
+
* parses to an array, a `null` or a scalar, is a reading the guard could not
|
|
86
|
+
* take — and a reading that could not be taken is not one that passed.
|
|
87
|
+
*
|
|
88
|
+
* That now includes the PRIOR state. If the file already on disk will not
|
|
89
|
+
* parse, whether this change erases a result is UNMEASURED, and it refuses.
|
|
90
|
+
* An earlier draft allowed it, reasoning that refusing would make a corrupt
|
|
91
|
+
* state file unrepairable. **Measured 2026-10-22: it would not.** The
|
|
92
|
+
* sanctioned repair is `bun -e "…writeWorkflowState(path, state)"`, which
|
|
93
|
+
* succeeds over unparseable prior content — its own prior read sits inside a
|
|
94
|
+
* `catch` that yields `null` — and which is a Bash call this PreToolUse
|
|
95
|
+
* matcher never sees. The refusal names that path, because a guard nobody
|
|
96
|
+
* can get out from under is a guard that gets switched off.
|
|
97
|
+
*
|
|
98
|
+
* That allowance was also the second half of a two-step bypass: Edit was the
|
|
99
|
+
* only way to reach a corrupt prior state, since the Write branch already
|
|
100
|
+
* refused unparseable incoming content — and once reached, every later Write
|
|
101
|
+
* carrying no results was permitted. Refusing an Edit that leaves the file
|
|
102
|
+
* unparseable closes the entry; refusing an unparseable prior closes the
|
|
103
|
+
* escalation. Either alone breaks the chain; both is right.
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
import { readFileSync } from 'fs';
|
|
107
|
+
|
|
108
|
+
export interface GateWriteDecision {
|
|
109
|
+
blocked: boolean;
|
|
110
|
+
reason?: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const ALLOW: GateWriteDecision = { blocked: false };
|
|
114
|
+
|
|
115
|
+
const GATE_RESULT_REASON =
|
|
116
|
+
'Gate results must be produced by gate-runner.ts, not written directly.';
|
|
117
|
+
const DONE_PHASE_REASON =
|
|
118
|
+
'Phase DONE must be set by gate-runner.ts after ship gate passes.';
|
|
119
|
+
const UNPARSEABLE_REASON =
|
|
120
|
+
'Refused: the content of this write to workflow-state.json could not be parsed as JSON. ' +
|
|
121
|
+
'Gate results are protected, and a guard cannot read a file it cannot parse — so an ' +
|
|
122
|
+
'unparseable write to this path is refused rather than allowed. Write valid JSON.';
|
|
123
|
+
const NON_OBJECT_REASON =
|
|
124
|
+
'Refused: this write to workflow-state.json parses to something that is not a JSON object. ' +
|
|
125
|
+
'A workflow state is always an object, so this reading is UNMEASURED — and UNMEASURED ' +
|
|
126
|
+
'refuses rather than passes.';
|
|
127
|
+
|
|
128
|
+
function erasureReason(dropped: string[]): string {
|
|
129
|
+
return (
|
|
130
|
+
`Refused: this write adds, removes or changes an existing gate result (${dropped.join(', ')}). ` +
|
|
131
|
+
'Erasing a FAIL reaches the same end as forging a PASS. Gate results are produced by ' +
|
|
132
|
+
'gate-runner.ts and are not editable directly.'
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Named in every refusal that turns on a state file the guard could not
|
|
138
|
+
* read. A refusal with no way out is one somebody disables.
|
|
139
|
+
*/
|
|
140
|
+
const REPAIR_HINT =
|
|
141
|
+
' To repair a corrupt workflow-state.json, use the sanctioned writer — it tolerates an ' +
|
|
142
|
+
'unparseable prior state and is a Bash call, not a Write/Edit: ' +
|
|
143
|
+
"bun -e \"import {writeWorkflowState} from '<harness>/gates/orchestrator.ts'; " +
|
|
144
|
+
"writeWorkflowState('<path>', state);\"";
|
|
145
|
+
|
|
146
|
+
const PRIOR_UNPARSEABLE_REASON =
|
|
147
|
+
'Refused: the workflow-state.json already at this path could not be parsed, so whether this ' +
|
|
148
|
+
'change erases a gate result is UNMEASURED — and UNMEASURED refuses rather than passes.' +
|
|
149
|
+
REPAIR_HINT;
|
|
150
|
+
|
|
151
|
+
const UNMODELLABLE_EDIT_REASON =
|
|
152
|
+
'Refused: the guard could not model this edit — `old_string` does not appear in ' +
|
|
153
|
+
'workflow-state.json, so whether the result would erase a gate result is UNMEASURED. ' +
|
|
154
|
+
'Rewrite old_string to match the file byte-for-byte and retry.' +
|
|
155
|
+
REPAIR_HINT;
|
|
156
|
+
|
|
157
|
+
const EDIT_CORRUPTS_REASON =
|
|
158
|
+
'Refused: applying this edit would leave workflow-state.json unparseable. Every reader of ' +
|
|
159
|
+
'this artifact JSON.parses it, and a corrupt state file disables the gate-result comparison ' +
|
|
160
|
+
'for every write after it.' +
|
|
161
|
+
REPAIR_HINT;
|
|
162
|
+
|
|
163
|
+
/** The artifact this module guards. */
|
|
164
|
+
export function isWorkflowState(filePath: string): boolean {
|
|
165
|
+
return filePath.endsWith('workflow-state.json');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Whitespace-tolerant, because JSON is. */
|
|
169
|
+
const RESULT_PATTERN = /"result"\s*:\s*"(?:PASS|FAIL)"/;
|
|
170
|
+
const DONE_PHASE_PATTERN = /"phase"\s*:\s*"DONE"/i;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Decode `\uXXXX` so a pattern sees what `JSON.parse` would see.
|
|
174
|
+
* Naive by design — see the module docstring.
|
|
175
|
+
*/
|
|
176
|
+
export function decodeUnicodeEscapes(text: string): string {
|
|
177
|
+
return text.replace(/\\u([0-9a-fA-F]{4})/g, (_m, hex) =>
|
|
178
|
+
String.fromCharCode(parseInt(hex, 16)),
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A plain JSON object — not an array, not null, not a scalar. */
|
|
183
|
+
function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
184
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Every `gates.<name>.result` present, keyed by gate name.
|
|
189
|
+
*
|
|
190
|
+
* Gate-AGNOSTIC: it iterates the keys the content actually carries rather
|
|
191
|
+
* than a list of names this module knows. That is what makes it impossible
|
|
192
|
+
* for a new gate to arrive unguarded.
|
|
193
|
+
*/
|
|
194
|
+
export function collectGateResults(parsed: unknown): Map<string, string> {
|
|
195
|
+
const found = new Map<string, string>();
|
|
196
|
+
if (!isPlainObject(parsed)) return found;
|
|
197
|
+
const gates = parsed.gates;
|
|
198
|
+
if (!isPlainObject(gates)) return found;
|
|
199
|
+
for (const [name, record] of Object.entries(gates)) {
|
|
200
|
+
if (isPlainObject(record) && record.result !== undefined && record.result !== null) {
|
|
201
|
+
found.set(name, String(record.result));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return found;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
type PriorState =
|
|
208
|
+
| { kind: 'missing' }
|
|
209
|
+
| { kind: 'unreadable' }
|
|
210
|
+
| { kind: 'ok'; raw: string; state: unknown };
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* What is already at `filePath`, as a reading that can say it failed.
|
|
214
|
+
*
|
|
215
|
+
* ONE implementation, called by both branches. The Edit branch used to do
|
|
216
|
+
* its own `existsSync` + `try/catch { return ALLOW }`, which made a
|
|
217
|
+
* present-but-unreadable file ALLOW there and refuse on the Write path —
|
|
218
|
+
* the same divergence, in the same file, that this issue is about.
|
|
219
|
+
*
|
|
220
|
+
* `ENOENT` is separated from every other errno rather than catching
|
|
221
|
+
* everything: "there is no file here" is a fact the guard can act on, and
|
|
222
|
+
* "I could not read the file that is here" is one it cannot. Collapsing
|
|
223
|
+
* them is how an unreadable file comes to look like a new run. No
|
|
224
|
+
* `existsSync` first — one syscall, and no window between the check and
|
|
225
|
+
* the read.
|
|
226
|
+
*/
|
|
227
|
+
function readPriorState(filePath: string): PriorState {
|
|
228
|
+
let raw: string;
|
|
229
|
+
try {
|
|
230
|
+
raw = readFileSync(filePath, 'utf-8');
|
|
231
|
+
} catch (e) {
|
|
232
|
+
return (e as NodeJS.ErrnoException)?.code === 'ENOENT'
|
|
233
|
+
? { kind: 'missing' }
|
|
234
|
+
: { kind: 'unreadable' };
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
return { kind: 'ok', raw, state: JSON.parse(raw) };
|
|
238
|
+
} catch {
|
|
239
|
+
return { kind: 'unreadable' };
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Is this state's phase DONE? */
|
|
244
|
+
function isDone(state: unknown): boolean {
|
|
245
|
+
return (
|
|
246
|
+
isPlainObject(state) &&
|
|
247
|
+
typeof state.phase === 'string' &&
|
|
248
|
+
state.phase.toUpperCase() === 'DONE'
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Does this parsed state forge a gate result? */
|
|
253
|
+
function decideParsed(parsed: unknown): GateWriteDecision {
|
|
254
|
+
if (!isPlainObject(parsed)) return { blocked: true, reason: NON_OBJECT_REASON };
|
|
255
|
+
|
|
256
|
+
if (collectGateResults(parsed).size > 0) {
|
|
257
|
+
return { blocked: true, reason: GATE_RESULT_REASON };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return ALLOW;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Phase DONE is a TRANSITION, not a resting state.
|
|
265
|
+
*
|
|
266
|
+
* The rule is that only the gate runner ADVANCES a run to DONE. An earlier
|
|
267
|
+
* draft refused whenever the outcome was DONE, which refused every edit to
|
|
268
|
+
* an already-finished run — measured as 6 refusals across 157 legitimate
|
|
269
|
+
* operations on the 44 live states in `~/.rungate`, all of them on the two
|
|
270
|
+
* runs whose phase was already DONE. Nothing was being advanced; the guard
|
|
271
|
+
* was reading a resting value as an event.
|
|
272
|
+
*
|
|
273
|
+
* Refusing the transition loses nothing: an attacker who wants DONE has to
|
|
274
|
+
* arrive from not-DONE, and the gate results of a finished run stay covered
|
|
275
|
+
* by the before/after comparison either way.
|
|
276
|
+
*
|
|
277
|
+
* Case-insensitive because the Edit pre-filter is. Readers compare
|
|
278
|
+
* `=== 'DONE'` exactly, so a lowercase spelling is not a live bypass, but
|
|
279
|
+
* only the gate runner sets this field and refusing both costs nothing.
|
|
280
|
+
*/
|
|
281
|
+
function advancesToDone(prior: PriorState, after: unknown): boolean {
|
|
282
|
+
if (!isDone(after)) return false;
|
|
283
|
+
return !(prior.kind === 'ok' && isDone(prior.state));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Gate names whose result differs between two readings, in either direction. */
|
|
287
|
+
function gateResultDelta(
|
|
288
|
+
before: Map<string, string>,
|
|
289
|
+
after: Map<string, string>,
|
|
290
|
+
): string[] {
|
|
291
|
+
const changed = new Set<string>();
|
|
292
|
+
for (const [gate, result] of before) if (after.get(gate) !== result) changed.add(gate);
|
|
293
|
+
for (const gate of after.keys()) if (!before.has(gate)) changed.add(gate);
|
|
294
|
+
return [...changed];
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The Edit tool's splice, in memory. A function replacement rather than a
|
|
299
|
+
* string one: `String.replace` with a string pattern interprets `$&`, `$1`
|
|
300
|
+
* and friends in the replacement, so a `new_string` containing a dollar sign
|
|
301
|
+
* would be spliced as something other than what lands on disk — and a guard
|
|
302
|
+
* that models the write incorrectly is measuring the wrong file.
|
|
303
|
+
*/
|
|
304
|
+
function applySplice(raw: string, oldString: string, newString: string, all: boolean): string {
|
|
305
|
+
if (all) return raw.split(oldString).join(newString);
|
|
306
|
+
const at = raw.indexOf(oldString);
|
|
307
|
+
if (at === -1) return raw;
|
|
308
|
+
return raw.slice(0, at) + newString + raw.slice(at + oldString.length);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Decide an Edit by modelling its OUTCOME, not by reading its fragments.
|
|
313
|
+
*
|
|
314
|
+
* The fragment patterns run first and can refuse with no I/O, but they are a
|
|
315
|
+
* pre-filter: the decision is the before/after comparison below.
|
|
316
|
+
*/
|
|
317
|
+
function decideEdit(
|
|
318
|
+
filePath: string,
|
|
319
|
+
oldString: string,
|
|
320
|
+
newString: string,
|
|
321
|
+
): GateWriteDecision {
|
|
322
|
+
// Fast pre-filter — a replacement that spells a result or DONE outright.
|
|
323
|
+
const text = decodeUnicodeEscapes(newString);
|
|
324
|
+
if (RESULT_PATTERN.test(text)) return { blocked: true, reason: GATE_RESULT_REASON };
|
|
325
|
+
if (DONE_PHASE_PATTERN.test(text)) return { blocked: true, reason: DONE_PHASE_REASON };
|
|
326
|
+
|
|
327
|
+
const prior = readPriorState(filePath);
|
|
328
|
+
// No file yet: the Edit tool refuses this itself, so there is no outcome
|
|
329
|
+
// to model and nothing to protect. This is the ONLY bare allow left on
|
|
330
|
+
// this path, and it is a reading, not a swallowed error.
|
|
331
|
+
if (prior.kind === 'missing') return ALLOW;
|
|
332
|
+
if (prior.kind === 'unreadable') {
|
|
333
|
+
return { blocked: true, reason: PRIOR_UNPARSEABLE_REASON };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// An `old_string` the file does not contain is an edit this guard cannot
|
|
337
|
+
// model. The earlier draft allowed it, reasoning that the Edit tool
|
|
338
|
+
// refuses such an edit anyway — sound about the TOOL, and the wrong party
|
|
339
|
+
// to reason about. The guard models the tool, and where the model and the
|
|
340
|
+
// tool differ it is the model that is wrong; line endings, a BOM and
|
|
341
|
+
// `replace_all` are three places they can. The cost settles it:
|
|
342
|
+
// over-blocking costs an agent one retry, under-blocking costs the control.
|
|
343
|
+
if (oldString === '' || !prior.raw.includes(oldString)) {
|
|
344
|
+
return { blocked: true, reason: UNMODELLABLE_EDIT_REASON };
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const before = collectGateResults(prior.state);
|
|
348
|
+
|
|
349
|
+
// BOTH outcomes, because `replace_all`'s coercion belongs to the tool and
|
|
350
|
+
// not to us. Reading it as `=== true` made every truthy non-boolean —
|
|
351
|
+
// `1`, `"true"` — model a single replacement while the tool may replace
|
|
352
|
+
// all, which was a live bypass: measured ALLOWED where `true` refused.
|
|
353
|
+
// Guessing the coercion is a second implementation of someone else's
|
|
354
|
+
// rule; evaluating both needs no guess. When `old_string` occurs once the
|
|
355
|
+
// two splices are identical, which is the overwhelmingly common case.
|
|
356
|
+
for (const all of [false, true]) {
|
|
357
|
+
let after: unknown;
|
|
358
|
+
try {
|
|
359
|
+
after = JSON.parse(applySplice(prior.raw, oldString, newString, all));
|
|
360
|
+
} catch {
|
|
361
|
+
return { blocked: true, reason: EDIT_CORRUPTS_REASON };
|
|
362
|
+
}
|
|
363
|
+
if (!isPlainObject(after)) return { blocked: true, reason: NON_OBJECT_REASON };
|
|
364
|
+
|
|
365
|
+
const delta = gateResultDelta(before, collectGateResults(after));
|
|
366
|
+
if (delta.length > 0) return { blocked: true, reason: erasureReason(delta) };
|
|
367
|
+
|
|
368
|
+
if (advancesToDone(prior, after)) {
|
|
369
|
+
return { blocked: true, reason: DONE_PHASE_REASON };
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return ALLOW;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* The single decision. `toolInput` is the raw hook payload's `tool_input`.
|
|
378
|
+
*
|
|
379
|
+
* Anything that is not a `Write` or `Edit` targeting `workflow-state.json` is
|
|
380
|
+
* allowed — the guard is scoped to one artifact, and widening the refusal to
|
|
381
|
+
* every unparseable write anywhere would be a brick, not a guard.
|
|
382
|
+
*/
|
|
383
|
+
export function decideGateResultWrite(
|
|
384
|
+
toolName: string,
|
|
385
|
+
toolInput: Record<string, unknown>,
|
|
386
|
+
): GateWriteDecision {
|
|
387
|
+
const filePath = typeof toolInput.file_path === 'string' ? toolInput.file_path : '';
|
|
388
|
+
if (!isWorkflowState(filePath)) return ALLOW;
|
|
389
|
+
|
|
390
|
+
if (toolName === 'Write') {
|
|
391
|
+
const content = typeof toolInput.content === 'string' ? toolInput.content : '';
|
|
392
|
+
let parsed: unknown;
|
|
393
|
+
try {
|
|
394
|
+
parsed = JSON.parse(content);
|
|
395
|
+
} catch {
|
|
396
|
+
return { blocked: true, reason: UNPARSEABLE_REASON };
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const onContent = decideParsed(parsed);
|
|
400
|
+
if (onContent.blocked) return onContent;
|
|
401
|
+
|
|
402
|
+
// The content carries no gate result. If the file already on disk does,
|
|
403
|
+
// this write drops it — and if that file will not parse, whether it does
|
|
404
|
+
// is UNMEASURED, which refuses.
|
|
405
|
+
const prior = readPriorState(filePath);
|
|
406
|
+
if (prior.kind === 'unreadable') {
|
|
407
|
+
return { blocked: true, reason: PRIOR_UNPARSEABLE_REASON };
|
|
408
|
+
}
|
|
409
|
+
const before = prior.kind === 'ok' ? collectGateResults(prior.state) : new Map<string, string>();
|
|
410
|
+
const delta = gateResultDelta(before, collectGateResults(parsed));
|
|
411
|
+
if (delta.length > 0) return { blocked: true, reason: erasureReason(delta) };
|
|
412
|
+
|
|
413
|
+
if (advancesToDone(prior, parsed)) {
|
|
414
|
+
return { blocked: true, reason: DONE_PHASE_REASON };
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return ALLOW;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (toolName === 'Edit') {
|
|
421
|
+
const newString = typeof toolInput.new_string === 'string' ? toolInput.new_string : '';
|
|
422
|
+
const oldString = typeof toolInput.old_string === 'string' ? toolInput.old_string : '';
|
|
423
|
+
return decideEdit(filePath, oldString, newString);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return ALLOW;
|
|
427
|
+
}
|