brl-subagent 2.9.1 → 2.9.3
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/AGENT.md +3 -0
- package/CHANGELOG.md +366 -0
- package/README.md +11 -5
- package/package.json +14 -11
- package/src/agent-tools.ts +178 -19
- package/src/atomic-write.ts +101 -0
- package/src/background-guard.ts +295 -0
- package/src/background-pipeline.ts +21 -5
- package/src/boot-recovery.ts +16 -0
- package/src/brief.ts +228 -0
- package/src/delegate-handler.ts +51 -12
- package/src/dispatch-header.ts +226 -0
- package/src/history.ts +0 -6
- package/src/index.ts +27 -5
- package/src/kill-escalation.ts +32 -9
- package/src/mode-prologue.ts +3 -11
- package/src/mode-runners.ts +0 -3
- package/src/notify-completion.ts +81 -12
- package/src/params.ts +10 -2
- package/src/prelude.ts +1 -9
- package/src/proc.ts +73 -15
- package/src/project-flags.ts +42 -6
- package/src/recovery-engine.ts +16 -2
- package/src/recovery.ts +29 -0
- package/src/retry-armed.ts +383 -0
- package/src/run-registry.ts +121 -1
- package/src/runner.ts +308 -10
- package/src/schema.ts +0 -11
- package/src/session-manager.ts +490 -54
- package/src/state.ts +16 -1
- package/src/stop-lineage.ts +280 -0
- package/src/templates.ts +97 -1
- package/src/terminal-notifier.ts +70 -4
- package/src/types.ts +110 -0
- package/src/unit-run.ts +7 -2
- package/src/validate.ts +47 -86
- package/templates/analyze-data.md +1 -0
- package/templates/code-review.md +1 -0
- package/templates/debug-issue.md +1 -0
- package/templates/implement-feature.md +1 -0
- package/templates/prototype.md +1 -0
- package/templates/refactor.md +1 -0
- package/templates/security-audit.md +1 -0
- package/templates/write-docs.md +1 -0
- package/templates/write-tests.md +1 -0
- package/src/dispatch-line.ts +0 -144
package/src/notify-completion.ts
CHANGED
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
type AgentStatus,
|
|
31
31
|
} from "./types";
|
|
32
32
|
import { resolveTerminalRunEntry } from "./state";
|
|
33
|
+
import { isTransportFailure } from "./transport-retry";
|
|
33
34
|
|
|
34
35
|
/** Terminal statuses the completion message can carry. */
|
|
35
36
|
export type CompletionStatus = "completed" | "failed" | "stopped";
|
|
@@ -246,6 +247,30 @@ export function buildCompletionMessage(
|
|
|
246
247
|
return { customType: "subagent-completion", content, display: true, details };
|
|
247
248
|
}
|
|
248
249
|
|
|
250
|
+
/**
|
|
251
|
+
* Insert a paragraph into a completion message as its own part and BEFORE the
|
|
252
|
+
* closing directive. The ONE insert rule for every paragraph a settle adds
|
|
253
|
+
* (issue #556 made it shared: the worktree-state block and the repeat-transport
|
|
254
|
+
* advisory land the same way).
|
|
255
|
+
*
|
|
256
|
+
* The directive is matched at the END (`endsWith`), never as the first
|
|
257
|
+
* occurrence: an agent's own tail can quote the directive text, and splitting
|
|
258
|
+
* there would drop everything after the quote. A shape that does not end with
|
|
259
|
+
* the directive keeps the paragraph by appending it — a missing report is the
|
|
260
|
+
* one outcome this function must not produce.
|
|
261
|
+
*/
|
|
262
|
+
export function appendCompletionParagraph(
|
|
263
|
+
message: CompletionMessagePayload,
|
|
264
|
+
paragraph: string,
|
|
265
|
+
): CompletionMessagePayload {
|
|
266
|
+
if (!paragraph) return message;
|
|
267
|
+
const suffix = `\n\n${COMPLETION_DIRECTIVE}`;
|
|
268
|
+
const content = message.content.endsWith(suffix)
|
|
269
|
+
? `${message.content.slice(0, -suffix.length)}\n\n${paragraph}${suffix}`
|
|
270
|
+
: `${message.content}\n\n${paragraph}`;
|
|
271
|
+
return { ...message, content };
|
|
272
|
+
}
|
|
273
|
+
|
|
249
274
|
/**
|
|
250
275
|
* Issue #474: insert the worktree-state block into a settled-failure wake, as
|
|
251
276
|
* its own part and BEFORE the closing directive.
|
|
@@ -254,23 +279,12 @@ export function buildCompletionMessage(
|
|
|
254
279
|
* contract is "no side effects", and the probe is a subprocess. The caller
|
|
255
280
|
* renders the block (`describeWorktreeState`) and gates on the CLASSIFIED
|
|
256
281
|
* status.
|
|
257
|
-
*
|
|
258
|
-
* The directive is matched at the END (`endsWith`), never as the first
|
|
259
|
-
* occurrence: an agent's own tail can quote the directive text, and splitting
|
|
260
|
-
* there would drop everything after the quote. A shape that does not end with
|
|
261
|
-
* the directive keeps the block by appending it — a missing report is the one
|
|
262
|
-
* outcome this function must not produce.
|
|
263
282
|
*/
|
|
264
283
|
export function withWorktreeState(
|
|
265
284
|
message: CompletionMessagePayload,
|
|
266
285
|
worktreeState: string,
|
|
267
286
|
): CompletionMessagePayload {
|
|
268
|
-
|
|
269
|
-
const suffix = `\n\n${COMPLETION_DIRECTIVE}`;
|
|
270
|
-
const content = message.content.endsWith(suffix)
|
|
271
|
-
? `${message.content.slice(0, -suffix.length)}\n\n${worktreeState}${suffix}`
|
|
272
|
-
: `${message.content}\n\n${worktreeState}`;
|
|
273
|
-
return { ...message, content };
|
|
287
|
+
return appendCompletionParagraph(message, worktreeState);
|
|
274
288
|
}
|
|
275
289
|
|
|
276
290
|
/**
|
|
@@ -395,6 +409,61 @@ export function buildMonitoringDegradedAdvisory(
|
|
|
395
409
|
);
|
|
396
410
|
}
|
|
397
411
|
|
|
412
|
+
// ---------------------------------------------------------------------------
|
|
413
|
+
// Repeat transport death advisory (issue #556)
|
|
414
|
+
//
|
|
415
|
+
// The rule that should change the conductor's behaviour after a SECOND transport
|
|
416
|
+
// death on the same label — "two transport deaths on one label: change the
|
|
417
|
+
// shape" (`references/autonomous-run.md`) — lived only in prose the conductor
|
|
418
|
+
// had to remember. The completion notice is the moment it matters, so the notice
|
|
419
|
+
// carries it — but ONLY when the lineage is PROVEN from the durable records:
|
|
420
|
+
// this attempt is a transport failure, it is at least the second, and the record
|
|
421
|
+
// it resumed (`resumeOf`) is a transport failure too. A builder that inferred a
|
|
422
|
+
// lineage it cannot read would tell a conductor to abandon a run that died for
|
|
423
|
+
// an unrelated reason, which is worse than saying nothing.
|
|
424
|
+
// ---------------------------------------------------------------------------
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* PURE builder — no pi SDK, no side effects, never throws. Returns `""` for
|
|
428
|
+
* every unproven case (first attempt, non-transport failure, missing or
|
|
429
|
+
* non-transport source record), so a caller can append it unconditionally and a
|
|
430
|
+
* source record it cannot resolve costs nothing.
|
|
431
|
+
*
|
|
432
|
+
* The attempt numbers named in the prose are the SOURCE's recorded `attempt`
|
|
433
|
+
* and this attempt — never a hard-coded "attempt 1". The guard proves exactly
|
|
434
|
+
* two transport deaths: this attempt, and the record it resumed (`resumeOf`).
|
|
435
|
+
* Naming the source attempt is what keeps the sentence true when a manual
|
|
436
|
+
* `retryRunId` mints attempt 3 whose `resumeOf` points at attempt 2 (attempt 1
|
|
437
|
+
* may have failed for an unrelated reason). `source.attempt` is optional on a
|
|
438
|
+
* record, so the fallback is `attempt - 1` — the ordinary auto-retry lineage,
|
|
439
|
+
* which reproduces the same numbers as the source whenever the source carries
|
|
440
|
+
* one.
|
|
441
|
+
*
|
|
442
|
+
* `label` is omitted from the prose when absent, exactly as in the monitoring
|
|
443
|
+
* advisory above.
|
|
444
|
+
*/
|
|
445
|
+
export function buildRepeatTransportAdvisory(
|
|
446
|
+
run: Pick<
|
|
447
|
+
SubagentRun,
|
|
448
|
+
"attempt" | "status" | "errorCategory" | "errorMessage" | "originalParams" | "resumeOf"
|
|
449
|
+
>,
|
|
450
|
+
source:
|
|
451
|
+
| Pick<SubagentRun, "attempt" | "status" | "errorCategory" | "errorMessage" | "originalParams">
|
|
452
|
+
| undefined,
|
|
453
|
+
label?: string,
|
|
454
|
+
): string {
|
|
455
|
+
if (!isTransportFailure(run)) return "";
|
|
456
|
+
const attempt = run.attempt ?? 1;
|
|
457
|
+
if (attempt < 2) return "";
|
|
458
|
+
if (!source || !isTransportFailure(source)) return "";
|
|
459
|
+
const sourceAttempt = source.attempt ?? attempt - 1;
|
|
460
|
+
const name = label ? ` on "${label}"` : "";
|
|
461
|
+
return (
|
|
462
|
+
`Two transport deaths${name}: attempts ${sourceAttempt} and ${attempt} both died on a transport failure and this dispatch is not retried again. ` +
|
|
463
|
+
"Change the shape — run it in the conductor, override the model, or split the brief — per `references/autonomous-run.md`."
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
398
467
|
/**
|
|
399
468
|
* Emit the advisory on both channels, each in its own try: a throwing
|
|
400
469
|
* `sendMessage` must not swallow the footer line, and a missing UI must not
|
package/src/params.ts
CHANGED
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
} from "./types";
|
|
31
31
|
import { getAllPresets, getPreset } from "./presets";
|
|
32
32
|
import { classifyTask } from "./router";
|
|
33
|
+
import { splitDispatchHeader } from "./dispatch-header";
|
|
33
34
|
import { normalizeTimeout } from "./validate";
|
|
34
35
|
|
|
35
36
|
// ---------------------------------------------------------------------------
|
|
@@ -53,7 +54,7 @@ export const KNOWN_DELEGATE_KEYS = new Set([
|
|
|
53
54
|
"outputFile", "label", "model", "timeout", "cwd", "tools",
|
|
54
55
|
"excludeTools", "noBuiltinTools", "preset", "template", "params",
|
|
55
56
|
"retryRunId", "gitMode", "retryOnTimeout", "approvalMode", "background",
|
|
56
|
-
"priority", "chain", "tasks", "graph", "force",
|
|
57
|
+
"priority", "chain", "tasks", "graph", "force",
|
|
57
58
|
] as const);
|
|
58
59
|
|
|
59
60
|
/**
|
|
@@ -495,7 +496,14 @@ export function resolveDispatchPreset(
|
|
|
495
496
|
params.noBuiltinTools !== undefined;
|
|
496
497
|
if (params.template || hasExplicitToolPreference) return { autoRouted: false };
|
|
497
498
|
const matched = classifyTask(
|
|
498
|
-
|
|
499
|
+
// Issue #606 (§3b): classify the BODY, never the header. `classifyTask`
|
|
500
|
+
// matches keywords over the whole string, first rule wins, so a mandatory
|
|
501
|
+
// `dispatch: review` line would re-route a task whose body said "write
|
|
502
|
+
// tests" — a dispatch line must not be a routing decision. The header is
|
|
503
|
+
// stripped here (the caller), not in the classifier: the classifier's
|
|
504
|
+
// contract is "classify the text you are given", and T10/P9 pin both
|
|
505
|
+
// halves — the body-only preset, and that the whole task would differ.
|
|
506
|
+
splitDispatchHeader(params.task ?? "").body,
|
|
499
507
|
getAllPresets(state.builtinPresets, state.customPresets),
|
|
500
508
|
);
|
|
501
509
|
if (!matched) return { autoRouted: false };
|
package/src/prelude.ts
CHANGED
|
@@ -369,13 +369,6 @@ export interface UnitPrePassInput<U, M extends UnitPrePassSource> {
|
|
|
369
369
|
log: Logger;
|
|
370
370
|
/** Per-unit force flag (IN10): mode entry never carries it. */
|
|
371
371
|
force?: boolean;
|
|
372
|
-
/**
|
|
373
|
-
* The call-level `noJev` bypass (issue #412), forwarded to every unit's H1.
|
|
374
|
-
* Mode entry never carries it either: the mode-level pre-task check has no
|
|
375
|
-
* task text of its own to bypass, and a per-unit bypass would let a caller
|
|
376
|
-
* silence the check for a batch while appearing to pass it for the call.
|
|
377
|
-
*/
|
|
378
|
-
noJev?: string;
|
|
379
372
|
/**
|
|
380
373
|
* The unit's DECLARED thinkingLevel, read from the unit itself (issue #443).
|
|
381
374
|
* Required, so a new caller cannot silently skip the check: the merged value
|
|
@@ -407,7 +400,7 @@ export type UnitPrePassOutcome<U, M extends UnitPrePassSource> =
|
|
|
407
400
|
export function validateUnitPrePass<U, M extends UnitPrePassSource>(
|
|
408
401
|
input: UnitPrePassInput<U, M>,
|
|
409
402
|
): UnitPrePassOutcome<U, M> {
|
|
410
|
-
const { units, merge, describe, label, gitMode, ctx, log, force,
|
|
403
|
+
const { units, merge, describe, label, gitMode, ctx, log, force, declaredThinkingLevel } = input;
|
|
411
404
|
const validated: Array<ValidatedUnit<U, M>> = [];
|
|
412
405
|
const warnings: string[] = [];
|
|
413
406
|
|
|
@@ -447,7 +440,6 @@ export function validateUnitPrePass<U, M extends UnitPrePassSource>(
|
|
|
447
440
|
outputFile: merged.outputFile,
|
|
448
441
|
force,
|
|
449
442
|
cwd: targets.cwd,
|
|
450
|
-
noJev,
|
|
451
443
|
},
|
|
452
444
|
});
|
|
453
445
|
if (validation.error) return { ok: false, error: validation.error };
|
package/src/proc.ts
CHANGED
|
@@ -57,18 +57,64 @@ export function isProcAvailable(): boolean {
|
|
|
57
57
|
return procAvailable;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* The outcome of a `/proc/<pid>/stat` read: the fields, or why it failed.
|
|
62
|
+
*
|
|
63
|
+
* `code` is the errno of the FAILED READ, in three shapes:
|
|
64
|
+
*
|
|
65
|
+
* - a VANISHED code — `"ENOENT"` when the pid was gone at open, `"ESRCH"` when
|
|
66
|
+
* the record was opened and the task was released before the read (both in
|
|
67
|
+
* `VANISHED_READ_CODES`) — the task was RELEASED, i.e. absence-of-process
|
|
68
|
+
* evidence;
|
|
69
|
+
* - any OTHER errno (`"EACCES"`, `"EIO"`) — the read failed and we cannot tell
|
|
70
|
+
* whether the process is alive: AMBIGUOUS;
|
|
71
|
+
* - `undefined` when /proc is unavailable, the pid is invalid, or the record
|
|
72
|
+
* cannot be parsed — there is no errno to classify. Readers resolve the
|
|
73
|
+
* no-`/proc` case to their documented PLATFORM answer, never to a partial
|
|
74
|
+
* inspection (see `GroupMembership`).
|
|
75
|
+
*/
|
|
61
76
|
type StatFieldsRead =
|
|
62
77
|
| { ok: true; fields: string[] }
|
|
63
78
|
| { ok: false; code: string | undefined };
|
|
64
79
|
|
|
80
|
+
/**
|
|
81
|
+
* #644 — the errnos that PROVE the task was RELEASED, so a failed read is a
|
|
82
|
+
* COMPLETED observation of absence rather than a partial one:
|
|
83
|
+
*
|
|
84
|
+
* - `ENOENT` — gone at `open` (the dentry no longer exists);
|
|
85
|
+
* - `ESRCH` — opened, then the task was released before the read.
|
|
86
|
+
*
|
|
87
|
+
* Both were MEASURED, not reasoned: a held-fd control answered `ESRCH` 400/400,
|
|
88
|
+
* and a real scan under foreign churn saw 48 `ESRCH` against 25,462 `ENOENT` in
|
|
89
|
+
* 10,291 scans (1,718,042 reads). Treating only `ENOENT` as vanished made a
|
|
90
|
+
* COMPLETE scan look partial (`groupHasMembers` → `undefined`, so the kill path
|
|
91
|
+
* reported an unsignalled survivor) and made a reaped leader look like a live
|
|
92
|
+
* FOREIGN pid (`pidGone` → false, so the surviving members were never signalled).
|
|
93
|
+
*
|
|
94
|
+
* ONE set, honoured by BOTH readers (`statFieldsVanished`). Every other READ
|
|
95
|
+
* outcome stays AMBIGUOUS — another errno (`EACCES`, `EIO`) or a record that
|
|
96
|
+
* cannot be parsed. A host with NO `/proc` at all is not an ambiguous read: it is
|
|
97
|
+
* the documented PLATFORM boundary at each reader (`groupHasMembers` answers
|
|
98
|
+
* `false`, `pidGone` degrades to `!pidAlive`), untouched by this classification.
|
|
99
|
+
*/
|
|
100
|
+
const VANISHED_READ_CODES: ReadonlySet<string> = new Set(["ENOENT", "ESRCH"]);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* True when the failed read PROVES the task was released — the single predicate
|
|
104
|
+
* both readers honour, so the classification cannot drift between them. The
|
|
105
|
+
* `code !== undefined` narrowing is load-bearing: `Set.has` takes a `string`, and
|
|
106
|
+
* a parse anomaly / unavailable /proc carries no errno to test.
|
|
107
|
+
*/
|
|
108
|
+
function statFieldsVanished(read: Extract<StatFieldsRead, { ok: false }>): boolean {
|
|
109
|
+
return read.code !== undefined && VANISHED_READ_CODES.has(read.code);
|
|
110
|
+
}
|
|
111
|
+
|
|
65
112
|
/**
|
|
66
113
|
* The `readStatFields` primitive WITH the failure cause preserved: `code` is the
|
|
67
|
-
* errno of
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* cannot tell" (everything else).
|
|
114
|
+
* errno of the failed read — see `StatFieldsRead` for its three shapes. Callers
|
|
115
|
+
* that only need the coarse split use `statFieldsVanished`: a vanished code means
|
|
116
|
+
* the task was RELEASED (absence-of-process evidence), anything else — another
|
|
117
|
+
* errno, or `undefined` — means "the read failed and we cannot tell".
|
|
72
118
|
*/
|
|
73
119
|
function readStatFieldsDetailed(pid: number): StatFieldsRead {
|
|
74
120
|
if (!isProcAvailable() || !Number.isInteger(pid) || pid <= 0) {
|
|
@@ -133,9 +179,11 @@ export function readProcessGroup(pid: number): number | undefined {
|
|
|
133
179
|
* `undefined`: with no `/proc` at all the caller degrades to leader liveness
|
|
134
180
|
* (this returns `false`). `undefined` is reserved for an inspection that FAILED
|
|
135
181
|
* on a `/proc`-capable host — a `/proc` listing failure, or a member's stat read
|
|
136
|
-
* that failed
|
|
137
|
-
* parse anomaly). A member that genuinely
|
|
138
|
-
*
|
|
182
|
+
* that failed AMBIGUOUSLY (an errno outside `VANISHED_READ_CODES`, such as
|
|
183
|
+
* `EACCES`/`EIO`, or no errno at all — a parse anomaly). A member that genuinely
|
|
184
|
+
* vanished during the scan is skipped — `ENOENT`, or `ESRCH` when it was reaped
|
|
185
|
+
* between the listing and the read (both in `VANISHED_READ_CODES`) — and the
|
|
186
|
+
* scan stays COMPLETE (a gone task cannot be a live member).
|
|
139
187
|
*/
|
|
140
188
|
export type GroupMembership = boolean | undefined;
|
|
141
189
|
|
|
@@ -229,9 +277,10 @@ export function groupHasMembers(pgid: number): GroupMembership {
|
|
|
229
277
|
if (pid === pgid) continue; // the leader (possibly a zombie) is not a member
|
|
230
278
|
const read = readStatFieldsDetailed(pid);
|
|
231
279
|
if (!read.ok) {
|
|
232
|
-
// A member gone mid-scan is skippable (it cannot be a live member)
|
|
280
|
+
// A member gone mid-scan is skippable (it cannot be a live member) — and
|
|
281
|
+
// #644: BOTH vanished codes prove that, so the scan stays complete. Any
|
|
233
282
|
// OTHER failure makes the inspection partial → unknown.
|
|
234
|
-
if (read
|
|
283
|
+
if (statFieldsVanished(read)) continue;
|
|
235
284
|
return undefined;
|
|
236
285
|
}
|
|
237
286
|
// #299 fix A.5: ONE strict prefix validation. A truncated, shifted or
|
|
@@ -315,8 +364,15 @@ export function pidAlive(pid: number): boolean {
|
|
|
315
364
|
* not-yet-reaped zombie included), so ENOENT is absence-of-process evidence;
|
|
316
365
|
* classifying it as "not gone" made the guard treat our own reaped leader as a
|
|
317
366
|
* live FOREIGN pid and refuse BOTH the group SIGTERM and the final group
|
|
318
|
-
* SIGKILL while its group still had live members.
|
|
319
|
-
*
|
|
367
|
+
* SIGKILL while its group still had live members. #644: `ESRCH` is the SAME
|
|
368
|
+
* absence-of-process evidence — the record was opened and the task was released
|
|
369
|
+
* before the read. The held-fd control measures exactly that sub-window: once the
|
|
370
|
+
* record was opened before the reap, the read answers `ESRCH` 400/400. A real
|
|
371
|
+
* `readFileSync` has a far narrower open→read window, so a real scan answers
|
|
372
|
+
* mostly `ENOENT` (measured: 48 `ESRCH` against 25,462 `ENOENT` in 10,291 scans)
|
|
373
|
+
* and `ESRCH` only when the reap lands inside that window. Every OTHER read
|
|
374
|
+
* failure (EACCES, a parse anomaly, /proc unavailable) stays conservatively
|
|
375
|
+
* not-gone.
|
|
320
376
|
*
|
|
321
377
|
* PLATFORM BOUNDARY (hidepid=2): on a `/proc` mounted with `hidepid=2` a foreign
|
|
322
378
|
* LIVE pid we do not own can read `kill -0` EPERM (alive) and then
|
|
@@ -324,12 +380,14 @@ export function pidAlive(pid: number): boolean {
|
|
|
324
380
|
* ATTEMPT a group signal despite the marker mismatch. This is a documented
|
|
325
381
|
* platform boundary, NOT a signal-safety guarantee: the kernel's EPERM on the
|
|
326
382
|
* signal constrains the unprivileged blast radius, and the group signal still
|
|
327
|
-
* targets only the pgid we derived (it does not widen the target set).
|
|
383
|
+
* targets only the pgid we derived (it does not widen the target set). A hidden
|
|
384
|
+
* pid is indistinguishable from a reaped one — already documented, unchanged by
|
|
385
|
+
* the `ESRCH` classification above.
|
|
328
386
|
*/
|
|
329
387
|
export function pidGone(pid: number): boolean {
|
|
330
388
|
if (!pidAlive(pid)) return true;
|
|
331
389
|
const read = readStatFieldsDetailed(pid);
|
|
332
|
-
if (!read.ok) return read
|
|
390
|
+
if (!read.ok) return statFieldsVanished(read);
|
|
333
391
|
const state = read.fields[0];
|
|
334
392
|
return state === "Z" || state === "X";
|
|
335
393
|
}
|
package/src/project-flags.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// Purpose: The single reader for the project-local `brl-subagent` umbrella flags (issue #415).
|
|
2
2
|
/**
|
|
3
|
-
* The umbrella project-local
|
|
3
|
+
* The umbrella project-local flags: `{"brl-subagent": {"processChecks": true}}` and
|
|
4
|
+
* `{"brl-subagent": {"backgroundGuard": "worktree-guard"}}`.
|
|
4
5
|
*
|
|
5
|
-
* `processChecks` gates OUR process mechanisms — the
|
|
6
|
-
* (#
|
|
7
|
-
* engineering-bar injection (#421)
|
|
8
|
-
* `requireJevLine`: one flag, one reader, no per-rule fields.
|
|
6
|
+
* `processChecks` gates OUR process mechanisms — the mandatory dispatch header
|
|
7
|
+
* & signed-brief check (#606), rule 2's durable-shape auto-switch (#412), and the
|
|
8
|
+
* engineering-bar injection (#421): one flag, one reader, no per-rule fields.
|
|
9
9
|
*
|
|
10
10
|
* Why one reader (issue #415): the shape rule shipped GLOBAL, so a user's
|
|
11
11
|
* explicit `background: false` + timeout was overridden by our conductor
|
|
@@ -21,6 +21,12 @@
|
|
|
21
21
|
* The manifest read is the project's OWN `package.json` — tracked, so it exists
|
|
22
22
|
* in every worktree (worktrees share the repo's manifest), unlike a gitignored
|
|
23
23
|
* marker under `.pi/`.
|
|
24
|
+
*
|
|
25
|
+
* `backgroundGuard` (#620, ADR 0029) joined the same section and therefore the
|
|
26
|
+
* same reader: a second manifest reader is exactly what the ratchet below
|
|
27
|
+
* forbids, and the guard opt-in belongs beside the process checks it completes.
|
|
28
|
+
* It is a NAME, not a path — resolution and containment live in
|
|
29
|
+
* `background-guard.ts`, so the manifest can never carry a traversable path.
|
|
24
30
|
*/
|
|
25
31
|
|
|
26
32
|
import { readFileSync } from "node:fs";
|
|
@@ -31,7 +37,7 @@ import { join } from "node:path";
|
|
|
31
37
|
* A third field is a new mechanism, and a new mechanism needs its own ADR and
|
|
32
38
|
* its own line in this whitelist (the ratchet enforces both directions).
|
|
33
39
|
*/
|
|
34
|
-
export const PROCESS_CHECK_KEYS = ["processChecks"] as const;
|
|
40
|
+
export const PROCESS_CHECK_KEYS = ["processChecks", "backgroundGuard"] as const;
|
|
35
41
|
|
|
36
42
|
/**
|
|
37
43
|
* Whether the project at `cwd` opted into our process checks. Fail-open in
|
|
@@ -50,6 +56,36 @@ export function readProcessChecks(cwd: string | undefined): boolean {
|
|
|
50
56
|
}
|
|
51
57
|
}
|
|
52
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The NAME of the guard this project requires a background delegation to carry
|
|
61
|
+
* (#620), or `null` when the project names none. The string is trimmed and is
|
|
62
|
+
* returned verbatim — including the sentinel `"none"`, which the caller folds
|
|
63
|
+
* to "off" — because the sentinel's meaning is the resolver's, not the
|
|
64
|
+
* reader's (one reader, one job).
|
|
65
|
+
*
|
|
66
|
+
* Fail-open like `readProcessChecks`: no cwd, no manifest, unreadable manifest,
|
|
67
|
+
* malformed JSON, a non-object section, or a non-string/blank field all read as
|
|
68
|
+
* `null` — a project's packaging mistake must never reject a dispatch.
|
|
69
|
+
*
|
|
70
|
+
* A name is NOT a path: `background-guard.ts` validates it as one component and
|
|
71
|
+
* resolves it under the project root, so this reader has no traversal surface
|
|
72
|
+
* to get wrong.
|
|
73
|
+
*/
|
|
74
|
+
export function readBackgroundGuard(cwd: string | undefined): string | null {
|
|
75
|
+
if (cwd === undefined || cwd === "") return null;
|
|
76
|
+
try {
|
|
77
|
+
const manifest = JSON.parse(readFileSync(join(cwd, "package.json"), "utf8")) as {
|
|
78
|
+
"brl-subagent"?: { backgroundGuard?: unknown };
|
|
79
|
+
};
|
|
80
|
+
const named = manifest?.["brl-subagent"]?.backgroundGuard;
|
|
81
|
+
if (typeof named !== "string") return null;
|
|
82
|
+
const trimmed = named.trim();
|
|
83
|
+
return trimmed === "" ? null : trimmed;
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
53
89
|
/**
|
|
54
90
|
* The validation seam's name for the same read (`validatePreTask` asks "is the
|
|
55
91
|
* check enabled for this project?"). One implementation: this is an alias, not
|
package/src/recovery-engine.ts
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
import type { Logger } from "./logging";
|
|
27
|
-
import type { ProcessOwner, SubagentRun } from "./types";
|
|
27
|
+
import type { ProcessOwner, RetryCause, SubagentRun } from "./types";
|
|
28
28
|
import { resolveTerminalRunEntry } from "./state";
|
|
29
29
|
import { escalateKill, processTarget, type SignalGuard, type SignalPhase } from "./kill-escalation";
|
|
30
30
|
|
|
@@ -150,16 +150,30 @@ export interface RecoveryOutcome {
|
|
|
150
150
|
* original's dispatchId and records `resumeOf`/`attempt`; a fresh dispatch
|
|
151
151
|
* mints a new dispatchId with attempt 1. These are RECORD fields, not
|
|
152
152
|
* delegate_task params (D4).
|
|
153
|
+
*
|
|
154
|
+
* Issue #616: `retryCause` states WHY the attempt exists, and only the MINT SITE
|
|
155
|
+
* knows it — the automatic transport retry passes `"transport"`, a
|
|
156
|
+
* caller-driven `retryRunId` retry passes `"manual"`. It is deliberately NOT
|
|
157
|
+
* derived here (from the source's failure class, say): a manual retry of a
|
|
158
|
+
* transport-failed attempt would then be indistinguishable from the automatic
|
|
159
|
+
* one, which is exactly the over-stop the narrowing exists to prevent. A fresh
|
|
160
|
+
* dispatch carries no cause at all (absent ⇒ unknown ⇒ never auto-stopped).
|
|
153
161
|
*/
|
|
154
|
-
export function newDispatchIdentity(
|
|
162
|
+
export function newDispatchIdentity(
|
|
163
|
+
source: SubagentRun | undefined,
|
|
164
|
+
retryCause?: RetryCause,
|
|
165
|
+
): {
|
|
155
166
|
dispatchId: string;
|
|
156
167
|
resumeOf?: string;
|
|
157
168
|
attempt: number;
|
|
169
|
+
retryCause?: RetryCause;
|
|
158
170
|
} {
|
|
159
171
|
return {
|
|
160
172
|
dispatchId: source?.dispatchId ?? crypto.randomUUID(),
|
|
161
173
|
resumeOf: source?.id,
|
|
162
174
|
attempt: source ? (source.attempt ?? 1) + 1 : 1,
|
|
175
|
+
// A cause without a source would describe a retry that has no origin.
|
|
176
|
+
...(source && retryCause ? { retryCause } : {}),
|
|
163
177
|
};
|
|
164
178
|
}
|
|
165
179
|
|
package/src/recovery.ts
CHANGED
|
@@ -85,6 +85,15 @@ export interface BootScanSummary {
|
|
|
85
85
|
flipRetired: number;
|
|
86
86
|
/** Issue #546: flips decided but whose durable write did NOT land (#304 rule). */
|
|
87
87
|
flipFailures: number;
|
|
88
|
+
/**
|
|
89
|
+
* Issue #554: the CORRUPT subset of `flipFailures` — the record was present
|
|
90
|
+
* and its bytes were read, but `JSON.parse` rejected them. An ADDITIONAL
|
|
91
|
+
* count, never a replacement: `flipFailures` keeps its meaning (a failed
|
|
92
|
+
* settlement) so #546's H2c and every other marker case stay valid. The
|
|
93
|
+
* operator's distinction is "repair the file" (corrupt) vs "fix permissions"
|
|
94
|
+
* (unreadable, the rest of `flipFailures`).
|
|
95
|
+
*/
|
|
96
|
+
flipCorrupt: number;
|
|
88
97
|
}
|
|
89
98
|
|
|
90
99
|
/** The all-zero summary (a skipped scan / an empty registry). */
|
|
@@ -103,6 +112,7 @@ export function emptyBootScanSummary(): BootScanSummary {
|
|
|
103
112
|
flipRepaired: 0,
|
|
104
113
|
flipRetired: 0,
|
|
105
114
|
flipFailures: 0,
|
|
115
|
+
flipCorrupt: 0,
|
|
106
116
|
};
|
|
107
117
|
}
|
|
108
118
|
|
|
@@ -330,8 +340,18 @@ export type RecordFlipOutcome = "repaired" | "retired" | false;
|
|
|
330
340
|
export interface FreshAgentRecord {
|
|
331
341
|
record: PersistedAgentLike | null;
|
|
332
342
|
exists: boolean;
|
|
343
|
+
/**
|
|
344
|
+
* Issue #554: WHY there is no record when `exists` is true — `"corrupt"`
|
|
345
|
+
* (the file read, but JSON.parse rejected it) vs `"unreadable"` (stat/read
|
|
346
|
+
* failed). Optional so a caller that only knows the tri-state (a test seam)
|
|
347
|
+
* stays valid; the boot ledger counts the corrupt subset by name.
|
|
348
|
+
*/
|
|
349
|
+
reason?: AgentRecordFailureReason;
|
|
333
350
|
}
|
|
334
351
|
|
|
352
|
+
/** Issue #554: the two named record-read failures. */
|
|
353
|
+
export type AgentRecordFailureReason = "corrupt" | "unreadable";
|
|
354
|
+
|
|
335
355
|
export interface RegistryScanOptions {
|
|
336
356
|
deps: RecoveryDeps;
|
|
337
357
|
/**
|
|
@@ -468,6 +488,9 @@ export async function recoverInflightRuns(options: RegistryScanOptions): Promise
|
|
|
468
488
|
let flipRepaired = 0;
|
|
469
489
|
let flipRetired = 0;
|
|
470
490
|
let flipFailures = 0;
|
|
491
|
+
// Issue #554: the corrupt SUBSET of the failures above — counted beside it,
|
|
492
|
+
// never instead of it.
|
|
493
|
+
let flipCorrupt = 0;
|
|
471
494
|
for (const entry of addressable) {
|
|
472
495
|
const isBackground = entry.kind === "background";
|
|
473
496
|
// Only a background entry has an agent record to enrich from (and to drop
|
|
@@ -508,9 +531,14 @@ export async function recoverInflightRuns(options: RegistryScanOptions): Promise
|
|
|
508
531
|
// tell whether it is still `running`. The marker is KEPT and the
|
|
509
532
|
// settlement is counted failed (never silently dropped).
|
|
510
533
|
flipFailures++;
|
|
534
|
+
// Issue #554: name the corrupt subset — the record read fine but
|
|
535
|
+
// its JSON did not parse. The decision and the kept marker are
|
|
536
|
+
// unchanged; only the ledger gains the finer distinction.
|
|
537
|
+
if (fresh.reason === "corrupt") flipCorrupt++;
|
|
511
538
|
options.log?.warn("Recovery: could not read the agent record for a record-flip marker — the marker is kept", {
|
|
512
539
|
id: entry.id,
|
|
513
540
|
kind: entry.kind,
|
|
541
|
+
...(fresh.reason ? { reason: fresh.reason } : {}),
|
|
514
542
|
});
|
|
515
543
|
continue;
|
|
516
544
|
}
|
|
@@ -657,6 +685,7 @@ export async function recoverInflightRuns(options: RegistryScanOptions): Promise
|
|
|
657
685
|
summary.flipRepaired = flipRepaired;
|
|
658
686
|
summary.flipRetired = flipRetired;
|
|
659
687
|
summary.flipFailures = flipFailures;
|
|
688
|
+
summary.flipCorrupt = flipCorrupt;
|
|
660
689
|
return summary;
|
|
661
690
|
}
|
|
662
691
|
|