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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! Verify-before-finish gate (#2279): a recoverable retry, not a hard block,
//! that keeps a coding agent from calling `finish_task` before it has run a
//! test suite it was explicitly told about.
//!
//! Why: (bake-off L2 diagnosis) tcode's delegated engineer authored 81 tests
//! validating the wrong contract and never ran the visible, prompt-named
//! test suite the L2 challenge instructed it to run before submitting. The
//! task prompt and `challenges/README.md` (folded into project context)
//! named a runnable test command (`pytest challenges/level-2-git-analyzer/
//! test_suite/ -v`); the transcript never shows it being invoked. This
//! module is the structural fix: at the `finish_task` turn boundary
//! (`agent_loop::AgentLoop::dispatch_all`), if the task/project-context text
//! names a runnable test command AND no matching `bash` invocation appears
//! in the relevant transcript, `finish_task` is rejected with a recoverable
//! error — the SAME `ToolResult::err` retry path every other tool failure
//! uses — instead of terminating the loop. If no test command is named at
//! all, the gate is INERT: #2279 explicitly defers general polyglot
//! test-suite discovery (the "(b2)" follow-up) rather than inventing one
//! here.
//! What: [`names_test_command`] and [`is_test_command`] are pure,
//! independently unit-testable regex predicates — Improvement 2's own
//! testability principle applied to its sibling Improvement 1. Two
//! `agent_loop::FinishGate` constructors build on them: [`default_finish_gate`]
//! scans a single loop's OWN `Transcript` — what a delegated engineer's
//! sub-agent loop attaches (`runner::in_process::InProcessAgentRunner`),
//! since its own `bash` calls land directly in that transcript.
//! [`pm_finish_gate`] is for the delegating PM's loop, which never calls
//! `bash` itself — it scans the externally shared `run_task::SharedTranscript`
//! of `TurnRecord`s the delegated engineer's turns are recorded into
//! instead, via each record's `ran_test_command` flag (set by
//! `run_task::recorder::RecordingLlmClient` using the SAME [`is_test_command`]
//! predicate this module owns, so the two paths can never independently
//! drift on what counts as a matching invocation).
//! What (#8206): naming a test command is necessary but no longer
//! sufficient. Both gates additionally require that a test command be
//! DETECTABLE for the bound project ([`detect::detect_test_command`]) and —
//! for the engineer gate — that the agent's own registry carry `bash` to run
//! it with. When either fails, `finish_task` is ACCEPTED and the completion
//! report records that no test command ran and why; only a detectable,
//! runnable, unrun suite still produces a refusal, and that refusal names the
//! manifest or command it detected.
//! Test: `verify_gate::tests::*`.
//!
//! [`names_test_command`]: crate::verify_gate::names_test_command
//! [`is_test_command`]: crate::verify_gate::is_test_command
//! [`default_finish_gate`]: crate::verify_gate::default_finish_gate
//! [`pm_finish_gate`]: crate::verify_gate::pm_finish_gate
//! [`detect::detect_test_command`]: crate::verify_gate::detect::detect_test_command
use PathBuf;
use Arc;
use Regex;
use Value;
use crate;
use crate;
use crateSharedTranscript;
use crateBASH_TOOL_NAME;
use ;
/// Recoverable-retry message fed back to a delegated engineer's own loop
/// when its `finish_task` call is rejected.
///
/// Why (#8206): built per call rather than held as a `const`, so the refusal
/// names the detected manifest or command — an agent told only that "a
/// pytest/cargo/npm/pnpm/go test invocation" was named cannot tell WHICH
/// suite to run.
/// What: `target.evidence()` is the detection clause; the rest is the
/// unchanged #2279 nudge, including the "Run the named test suite" phrase
/// `agent_loop::tests::gate_intercept` asserts on.
/// Test: `tests::default_gate_refusal_names_the_manifest`.
/// Recoverable-retry message fed back to the delegating PM's own loop when
/// its `finish_task` call is rejected because the delegated engineer never
/// ran the named tests.
///
/// Why: The PM-side twin of [`gate_reason_engineer`], carrying the same
/// #8206 evidence clause so the PM's follow-up delegation can name the suite.
/// Test: `tests::pm_gate_trips_when_engineer_never_ran_tests`.
/// The note appended to an ACCEPTED completion report when the gate was
/// triggered but could not be satisfied here (#8206).
///
/// Why: Closure condition 3 — the accepted finish must record that no test
/// command ran, and why, rather than silently looking like a verified one.
/// What: One sentence naming [`UndetectableReason::explain`]'s clause.
/// Test: `tests::default_gate_accepts_on_empty_project_root`,
/// `tests::default_gate_accepts_without_bash_tool`.
/// What a [`FinishGate`] decided about a successful `finish_task` call.
///
/// Why (#8206): before this enum the gate could only accept or reject. An
/// accepted finish that the gate WOULD have policed, but could not (no
/// detectable test suite, no `bash` tool to run one with), must not read as a
/// verified one — so a third outcome carries the reason into the recorded
/// report instead of silently accepting.
/// What: `Accept` leaves the report untouched; `AcceptWithNote` appends the
/// note to the model's own `summary` before it becomes the run's output;
/// `Reject` downgrades the call to a recoverable tool error.
/// Test: `agent_loop::tests::finish_gate_*`,
/// `verify_gate::tests::default_gate_accepts_on_empty_project_root`.
/// The project/tooling facts the engineer gate needs beyond the prompt text.
///
/// Why (#8206): the pre-#8206 gate was a pure text match, so it refused a
/// finish on an empty project root, and refused an agent that had no `bash`
/// tool to comply with — the run wedged, because nothing the agent could do
/// would satisfy the refusal.
/// What: `project_root` is the directory the agent's fs/bash tools are scoped
/// to (`None` when nothing is bound); `has_bash` is whether THIS agent's
/// gated registry carries the `bash` tool.
/// Test: `tests::default_gate_accepts_without_bash_tool`,
/// `tests::default_gate_accepts_on_empty_project_root`.
/// Build the regex alternation matching the six test-invocation shapes named
/// in #2279's finalized trigger heuristic.
///
/// Why: A single compiled pattern, rather than six separate `Regex`es, keeps
/// [`names_test_command`] and [`is_test_command`] identical in shape — "does
/// this text contain a recognizable test-command invocation" — the only
/// difference between the two call sites is WHICH text they scan. Compiled
/// fresh per call rather than cached in a `once_cell`/`lazy_static` global
/// (project convention reserves those for the tracing subscriber only); this
/// is not a hot path — at most once per `finish_task` dispatch attempt, or
/// once per recorded turn — so the recompilation cost is immaterial next to
/// the LLM round-trip it sits beside.
/// What: `pytest\s+\S+` (a pytest invocation naming a path/module),
/// `cargo test`, `npm test`, `pnpm test`, `go test`, `python -m pytest` —
/// exactly the six patterns #2279's finalized decision comment named.
/// Test: covered indirectly by every `tests::*` case below.
/// Whether `text` textually names a runnable test command (the gate's
/// TRIGGER half).
///
/// Why: The gate must stay inert when nothing in the task/project context
/// ever asked for a specific test command.
/// What: `true` iff [`test_command_pattern`] finds a match anywhere in
/// `text`.
/// Test: `tests::names_test_command_detects_each_pattern`,
/// `tests::names_test_command_false_on_unrelated_text`.
/// The test command `text` names, as the matched substring (#8206).
///
/// Why: [`names_test_command`]'s boolean answer cannot name the command in a
/// refusal message, and `detect::TestCommandTarget::PromptNamed` needs the
/// literal text the prompt used when no manifest was found.
/// What: The FIRST [`test_command_pattern`] match in `text`, e.g.
/// `"cargo test"` out of "then run cargo test before finishing".
/// Test: `tests::named_test_command_returns_the_matched_text`.
/// Whether `command` (a `bash` tool call's shell command) IS a matching test
/// invocation (the gate's VERIFY half).
///
/// Why: Reused by both [`default_finish_gate`]'s own-transcript scan and
/// `run_task::recorder`'s per-turn `ran_test_command` signal, so the two
/// gate sites can never independently drift on what counts as "the named
/// tests actually ran".
/// What: Same predicate as [`names_test_command`], applied to a single
/// command string rather than free-form prose.
/// Test: `tests::is_test_command_matches_each_pattern`,
/// `tests::is_test_command_false_on_unrelated_command`.
/// Extract the shell command from a `bash` tool call's arguments, if `call`
/// names the `bash` tool at all.
///
/// Why: Shared by [`default_finish_gate`]'s own-transcript scan and
/// `run_task::recorder::RecordingLlmClient`'s per-turn signal, so the two
/// gate sites parse the SAME wire shape (`{"command": "..."}`) identically.
/// What: `None` when `call.function.name != BASH_TOOL_NAME` or the
/// arguments do not parse as a JSON object with a string `command` field.
/// Test: `tests::bash_command_from_call_extracts_command`,
/// `tests::bash_command_from_call_ignores_other_tools`.
/// Whether any `bash` call in `messages` invoked a matching test command.
///
/// Why: The core predicate [`default_finish_gate`] applies to a delegated
/// engineer's own `Transcript` — factored out as a free function so it is
/// independently unit-testable against a plain `&[ChatMessage]` without
/// constructing a full `Transcript`.
/// What: Flattens every assistant turn's `tool_calls`, extracts each `bash`
/// call's command via [`bash_command_from_call`], and checks
/// [`is_test_command`].
/// Test: `tests::transcript_ran_test_command_detects_match`,
/// `tests::transcript_ran_test_command_false_without_bash`.
/// Join the seeded `system` + `user` message text into one haystack for
/// [`names_test_command`].
///
/// Why: `Transcript::seed` is the ONLY place raw history ever gets a
/// `system`- or `user`-role entry, and it seeds exactly one of each — the
/// assembled system prompt (BASE + agent prompt + project `CLAUDE.md`
/// context) and the task text. Scanning by role (not position) stays
/// correct even if `Transcript`'s internal layout ever changes.
/// What: Concatenates the `content` of every `system`/`user` entry with
/// newlines.
/// Test: `tests::seed_text_joins_system_and_user`.
/// Build the default verify-before-finish gate: scans the SAME loop's own
/// transcript for both the trigger and the verification.
///
/// Why: This is the shape a delegated engineer's sub-agent loop needs — its
/// own `bash` calls land directly in its own `Transcript`, so no external
/// state is required.
/// What: Returns a [`FinishGate`] closure that is inert unless
/// [`seed_text`] of the transcript [`names_test_command`] and
/// [`transcript_ran_test_command`] finds no match. Past that (#8206) it
/// rejects ONLY when the agent's registry carries `bash` AND
/// [`detect_test_command`] finds a runnable suite for `ctx`'s project root;
/// otherwise it accepts the finish with an [`unverified_finish_note`],
/// because no refusal the agent could act on exists.
/// Test: `tests::default_gate_trips_when_named_and_unrun`,
/// `tests::default_gate_inert_when_not_named`,
/// `tests::default_gate_satisfied_when_run`,
/// `tests::default_gate_accepts_without_bash_tool`,
/// `tests::default_gate_accepts_on_empty_project_root`,
/// `agent_loop::tests::finish_gate_trips_and_recovers`.
/// Build the delegating PM's verify-before-finish gate: the trigger comes
/// from the PM's own transcript, but the verification scans the externally
/// shared engineer transcript instead.
///
/// Why: The PM's own tool registry never includes `bash` (`run_task::
/// execute_run_task` registers only `delegate_to_agent` and `finish_task`),
/// so scanning the PM's own `Transcript` for a `bash` call would ALWAYS
/// report "not run" — wrongly tripping the gate even when the delegated
/// engineer ran the named tests correctly. `shared` is the SAME
/// `run_task::SharedTranscript` the engineer's `RecordingLlmClient` records
/// into, so this closure sees the engineer's turns too.
/// What: Returns a [`FinishGate`] closure inert unless the PM transcript's
/// [`seed_text`] [`names_test_command`]; when it does, trips (returning
/// [`gate_reason_pm`]) unless any `TurnRecord` in `shared` has
/// `ran_test_command == true`. A poisoned lock is treated as "not run" —
/// fail toward asking the model to verify, not toward silently finishing —
/// and (#2857) logs `tracing::warn!` when this happens, since a poisoned
/// lock means some other turn's code already panicked; silently folding
/// that into an ordinary gate trip would hide the real failure.
/// (#8206) `project_root` gets the same detectability precondition the
/// engineer gate has: an empty or unbound root can satisfy no test command,
/// so the PM's finish is accepted with an [`unverified_finish_note`] rather
/// than refused. The bash half of that check is NOT mirrored here — the
/// delegated engineer's registry is built per delegation and is not knowable
/// at PM-loop construction; its own [`default_finish_gate`] applies it.
/// Test: `tests::pm_gate_trips_when_engineer_never_ran_tests`,
/// `tests::pm_gate_satisfied_when_engineer_ran_tests`,
/// `tests::pm_gate_inert_when_not_named`,
/// `tests::pm_gate_accepts_on_empty_project_root`,
/// `tests::pm_gate_poisoned_lock_warns_and_trips`.