brl-subagent 2.7.0 → 2.9.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/AGENT.md +3 -3
- package/README.md +18 -10
- package/package.json +6 -5
- package/src/agent-tools.ts +332 -0
- package/src/background-pipeline.ts +1025 -0
- package/src/boot-recovery.ts +313 -0
- package/src/commands.ts +137 -0
- package/src/concurrency.ts +26 -5
- package/src/delegate-handler.ts +2261 -0
- package/src/engineering-bar.ts +69 -0
- package/src/event-bus.ts +10 -0
- package/src/git.ts +274 -5
- package/src/headstate.ts +471 -0
- package/src/history.ts +21 -3
- package/src/index.ts +127 -3943
- package/src/kill-escalation.ts +87 -7
- package/src/loader.ts +47 -1
- package/src/logging.ts +1 -1
- package/src/mode-prologue.ts +4 -10
- package/src/mode-runners.ts +1575 -0
- package/src/notify-completion.ts +180 -12
- package/src/params.ts +211 -37
- package/src/preflight.ts +123 -16
- package/src/prelude.ts +92 -7
- package/src/presets.ts +85 -18
- package/src/proc.ts +2 -3
- package/src/project-flags.ts +3 -3
- package/src/recovery-engine.ts +11 -1
- package/src/recovery-surface.ts +1 -2
- package/src/recovery.ts +155 -20
- package/src/reports.ts +1 -7
- package/src/run-registry.ts +22 -3
- package/src/runner.ts +80 -21
- package/src/runtime.ts +50 -0
- package/src/sanitize.ts +20 -2
- package/src/scheduler.ts +1 -2
- package/src/schema.ts +37 -7
- package/src/scratch-root.ts +909 -0
- package/src/scratch-spawn.ts +256 -0
- package/src/session-manager.ts +854 -200
- package/src/state.ts +130 -30
- package/src/templates.ts +42 -17
- package/src/terminal-notifier.ts +405 -0
- package/src/transcript.ts +1 -1
- package/src/tui-format.ts +177 -7
- package/src/tui.ts +115 -57
- package/src/types.ts +206 -5
- package/src/unit-params.ts +113 -0
- package/src/unit-run.ts +45 -16
- package/src/validate.ts +89 -11
- package/src/worktree-report.ts +142 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// Purpose: The spawn seam for the shipped scratch root — resolves, creates, prunes and settles one run's directory.
|
|
2
|
+
/**
|
|
3
|
+
* brl-subagent — the scratch root at the spawn seam (issue #511, U2)
|
|
4
|
+
*
|
|
5
|
+
* WHY THIS MODULE EXISTS. `scratch-root.ts` (U1) decides WHERE a scratch path is
|
|
6
|
+
* allowed to live and how one is created safely; it creates nothing on its own.
|
|
7
|
+
* This module is the one caller that turns that verdict into a run's directory
|
|
8
|
+
* on the way to a spawn, so the two execution surfaces cannot disagree about
|
|
9
|
+
* the shape of a run's scratch:
|
|
10
|
+
*
|
|
11
|
+
* - FOREGROUND — `runner.ts` (`runSubagent`), the chokepoint every blocking
|
|
12
|
+
* dispatch passes through: single, chain, parallel, graph, and the timeout
|
|
13
|
+
* retry. The child is a real `pi` subprocess, so the directory rides its
|
|
14
|
+
* environment as `BRL_SCRATCH` (an `envOverrides` entry, which `getSafeEnv`
|
|
15
|
+
* applies LAST — so a value inherited from the parent process is replaced,
|
|
16
|
+
* never trusted).
|
|
17
|
+
* - BACKGROUND — `session-manager.ts` (`spawnBackgroundSession`), the single
|
|
18
|
+
* place a background agent record and run entry are created: single,
|
|
19
|
+
* fan-out and the transport retry all funnel through it. A background agent
|
|
20
|
+
* is an IN-PROCESS SDK session, not a subprocess — there is no per-run child
|
|
21
|
+
* environment to set (the SDK builds the shell env from `process.env` at
|
|
22
|
+
* command time, so a per-run value there would be process-global and racy).
|
|
23
|
+
* The directory and the run record's `scratchDir` are therefore the whole
|
|
24
|
+
* carrier on that surface.
|
|
25
|
+
*
|
|
26
|
+
* WHY THE SEAM IS NOT IN THE PREFLIGHT OR A BACKGROUND-ONLY TAIL. The
|
|
27
|
+
* foreground path deliberately skips the preflight, so a decision made there
|
|
28
|
+
* would miss every blocking dispatch; and resolving inside the background tail
|
|
29
|
+
* would miss the foreground one. Both surfaces call THIS module at their own
|
|
30
|
+
* spawn point instead.
|
|
31
|
+
*
|
|
32
|
+
* THE INVARIANT, stated once: by the time a child starts, `<root>/runs/<runId>`
|
|
33
|
+
* exists at 0700 and the child's environment carries that absolute path as
|
|
34
|
+
* `BRL_SCRATCH`. A refusal is named and PRE-SPAWN: the caller gets the code and
|
|
35
|
+
* message and no child starts (`runner.ts` returns the refusal as a failed
|
|
36
|
+
* result; `session-manager.ts` throws before the agent record exists, and its
|
|
37
|
+
* callers translate that into their ordinary spawn-failure shape).
|
|
38
|
+
*
|
|
39
|
+
* THE RESOLVER NEVER READS `BRL_SCRATCH`. `resolveRoot` reads `HOME` /
|
|
40
|
+
* `LOCALAPPDATA` only, so a child cannot steer its own root — and because
|
|
41
|
+
* `BRL_SCRATCH` is deliberately NOT in `getSafeEnv`'s allowlist, a nested child
|
|
42
|
+
* resolves its OWN root instead of inheriting its parent's (the parent's value
|
|
43
|
+
* is a path the parent chose; the grandchild's run is a different directory).
|
|
44
|
+
*
|
|
45
|
+
* BEST-EFFORT, NAMED AS SUCH. Two operations here can fail without failing the
|
|
46
|
+
* spawn: the retention prune (`pruneRuns` reports, never throws) and the settle
|
|
47
|
+
* marker (`markRunDone`); both log and continue. Resolution and creation are the
|
|
48
|
+
* opposite — they are named refusals that stop the spawn.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
import { createLogger, type Logger } from "./logging";
|
|
52
|
+
import {
|
|
53
|
+
ensureRunDir,
|
|
54
|
+
markRunDone,
|
|
55
|
+
pruneRuns,
|
|
56
|
+
resolveRoot,
|
|
57
|
+
type RootOptions,
|
|
58
|
+
type RootWarningKind,
|
|
59
|
+
type ScratchRootCode,
|
|
60
|
+
} from "./scratch-root";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The child environment key carrying the run's scratch directory. Set through
|
|
64
|
+
* `envOverrides` (applied last by `getSafeEnv`), never allowlisted: an
|
|
65
|
+
* inherited value must be REPLACED, and a grandchild must resolve its own.
|
|
66
|
+
*/
|
|
67
|
+
export const SCRATCH_ENV_KEY = "BRL_SCRATCH";
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The logger for a seam whose caller has none to pass (`createUnitRun` writes a
|
|
71
|
+
* run record before any mode's logger is threaded through). Mirrors
|
|
72
|
+
* `runner.ts`'s `fallbackLog`: a diagnostic the caller could not instrument
|
|
73
|
+
* still has somewhere to land, and it is never dropped silently.
|
|
74
|
+
*/
|
|
75
|
+
const fallbackLog = createLogger("scratch");
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* `prepareRunScratch`'s verdict: the run's directory, or the resolver's named
|
|
79
|
+
* refusal. The `ok: false` arm carries the U1 code verbatim — one taxonomy, so
|
|
80
|
+
* the caller, the message and the tests cannot drift.
|
|
81
|
+
*/
|
|
82
|
+
export type RunScratch =
|
|
83
|
+
| { ok: true; root: string; runDir: string }
|
|
84
|
+
| { ok: false; code: ScratchRootCode; message: string };
|
|
85
|
+
|
|
86
|
+
/** What a caller may inject. Production passes nothing but the logger. */
|
|
87
|
+
export interface PrepareScratchOptions {
|
|
88
|
+
/** The seam's own sink. Absent falls back to the module logger. */
|
|
89
|
+
log?: Logger;
|
|
90
|
+
/**
|
|
91
|
+
* Root-resolution inputs, injected by tests only (`home` is the one that
|
|
92
|
+
* moves the layout). Production lets `resolveRoot` read the real environment.
|
|
93
|
+
*/
|
|
94
|
+
root?: RootOptions;
|
|
95
|
+
/**
|
|
96
|
+
* The retention sweep, injected by tests only. Production uses `pruneRuns`;
|
|
97
|
+
* the injection exists so the containment below (a throwing sweep must not
|
|
98
|
+
* fail a spawn) is asserted against a sweep that really throws, rather than
|
|
99
|
+
* claimed from `pruneRuns`'s own best-effort contract.
|
|
100
|
+
*/
|
|
101
|
+
prune?: typeof pruneRuns;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
// Test-only redirects (mirroring __setStorageDir / __setRegistryDir)
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A test-only root redirect. The suites that exercise a spawn cannot thread
|
|
110
|
+
* `RootOptions` through `runSubagent`/`spawnBackgroundSession`, and they must
|
|
111
|
+
* not write into the developer's real cache root. When set, the root is used
|
|
112
|
+
* AS GIVEN — `ensureRunDir` still validates the chain and creates at 0700, so
|
|
113
|
+
* the seam's guarantees are unchanged; only the platform map is bypassed.
|
|
114
|
+
*
|
|
115
|
+
* `createTempEnv` points this at a throwaway directory per test, so no suite
|
|
116
|
+
* creates anything under the real `$HOME`.
|
|
117
|
+
*/
|
|
118
|
+
let rootRedirect: string | undefined;
|
|
119
|
+
|
|
120
|
+
/** TEST-ONLY: resolve every run's scratch under `root` instead of the platform cache. */
|
|
121
|
+
export function __setScratchRoot(root: string | undefined): void {
|
|
122
|
+
rootRedirect = root;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** TEST-ONLY: forget the once-per-process root advisory. */
|
|
126
|
+
export function __resetScratchWarning(): void {
|
|
127
|
+
warnedRoot.clear();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
// The root advisory — once per process, never once per run
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The advisory KINDS already emitted, not one boolean. The resolver can carry two different warnings
|
|
136
|
+
* (a `tmpfs` mount and a group/other-writable ancestor, or both joined into one string), and a single
|
|
137
|
+
* flag would let the first kind swallow the second: the process would learn about the writable parent
|
|
138
|
+
* only if it happened to be resolved first. Keyed by kind rather than by text, because the mount
|
|
139
|
+
* sentence legitimately changes wording once the root exists (see `ROOT_WARNING_KINDS`).
|
|
140
|
+
*/
|
|
141
|
+
const warnedRoot = new Set<RootWarningKind>();
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* One line per advisory KIND per process: a fan-out of twenty units prints one line, never twenty. A
|
|
145
|
+
* report can carry both kinds at once, and then that ONE line carries both sentences; a kind already
|
|
146
|
+
* reported never re-logs on its own, which is the whole point of keying on kinds rather than on one
|
|
147
|
+
* boolean — a single flag would let the first kind swallow the second.
|
|
148
|
+
*/
|
|
149
|
+
function warnRootOnce(warning: string, kinds: readonly RootWarningKind[], log: Logger): void {
|
|
150
|
+
const fresh = kinds.filter((kind) => !warnedRoot.has(kind));
|
|
151
|
+
if (fresh.length === 0) return;
|
|
152
|
+
for (const kind of fresh) warnedRoot.add(kind);
|
|
153
|
+
log.warn(`scratch root: ${warning}`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
// The seam
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Resolve the root, create `<root>/runs/<runId>` at 0700, and prune settled
|
|
162
|
+
* runs — the one call both spawn surfaces make.
|
|
163
|
+
*
|
|
164
|
+
* Order matters. Resolution comes first (a refusal there means nothing is
|
|
165
|
+
* created), then creation (`ensureRunDir` validates the chain before and after),
|
|
166
|
+
* then the sweep (which must never see the current run as prunable, hence
|
|
167
|
+
* `exclude`). Pruning AFTER creation is what makes `exclude` meaningful: the
|
|
168
|
+
* directory it protects is the one that was just made.
|
|
169
|
+
*
|
|
170
|
+
* A prune failure is contained here, not by the caller: a retention sweep that
|
|
171
|
+
* cannot delete a directory is a cache that grows, never a spawn that fails.
|
|
172
|
+
*/
|
|
173
|
+
export function prepareRunScratch(runId: string, options: PrepareScratchOptions = {}): RunScratch {
|
|
174
|
+
const log = options.log ?? fallbackLog;
|
|
175
|
+
// Precedence, stated once: an EXPLICIT injection wins (the module's own
|
|
176
|
+
// tests), then the suite-level redirect (`createTempEnv`, the global test
|
|
177
|
+
// setup), then the resolver's real defaults — the production path.
|
|
178
|
+
const resolved =
|
|
179
|
+
options.root !== undefined
|
|
180
|
+
? resolveRoot(options.root)
|
|
181
|
+
: rootRedirect !== undefined
|
|
182
|
+
? { ok: true as const, root: rootRedirect }
|
|
183
|
+
: resolveRoot();
|
|
184
|
+
if (!resolved.ok) {
|
|
185
|
+
log.warn(`scratch root: refusing the spawn (${resolved.code}) — ${resolved.message}`);
|
|
186
|
+
return { ok: false, code: resolved.code, message: resolved.message };
|
|
187
|
+
}
|
|
188
|
+
if (resolved.warning !== undefined) warnRootOnce(resolved.warning, resolved.warningKinds ?? [], log);
|
|
189
|
+
|
|
190
|
+
const run = ensureRunDir(resolved.root, runId, options.root?.platform ? { platform: options.root.platform } : {});
|
|
191
|
+
if (!run.ok) {
|
|
192
|
+
log.warn(`scratch run dir: refusing the spawn (${run.code}) — ${run.message}`);
|
|
193
|
+
return { ok: false, code: run.code, message: run.message };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
pruneSettled(run.root, run.runDir, log, options.prune ?? pruneRuns);
|
|
197
|
+
return { ok: true, root: run.root, runDir: run.runDir };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The retention sweep: report what it removed, never throw, never fail the spawn. */
|
|
201
|
+
function pruneSettled(root: string, runDir: string, log: Logger, sweep: typeof pruneRuns): void {
|
|
202
|
+
try {
|
|
203
|
+
const pruned = sweep(root, { exclude: runDir });
|
|
204
|
+
if (pruned.removed.length > 0) {
|
|
205
|
+
log.debug("scratch root: pruned settled run directories", {
|
|
206
|
+
root,
|
|
207
|
+
removed: pruned.removed.length,
|
|
208
|
+
kept: pruned.kept.length,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
} catch (error) {
|
|
212
|
+
// `pruneRuns` reports rather than throws by construction; this belt covers
|
|
213
|
+
// a future edit of that contract, because a sweep is never worth a run.
|
|
214
|
+
log.warn("scratch root: prune failed — the spawn continues", {
|
|
215
|
+
root,
|
|
216
|
+
error: error instanceof Error ? error.message : String(error),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ---------------------------------------------------------------------------
|
|
222
|
+
// The settle marker
|
|
223
|
+
// ---------------------------------------------------------------------------
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Write the run's settle marker (`.done`) on a terminal path: done, failed or
|
|
227
|
+
* killed. Best-effort by contract — a marker that cannot be written is logged
|
|
228
|
+
* and the run's outcome is untouched, because retention is an optimization and
|
|
229
|
+
* boundedness is not this function's business.
|
|
230
|
+
*
|
|
231
|
+
* A run without a `scratchDir` (a pre-U2 record, or one whose preparation was
|
|
232
|
+
* refused before anything was created) is a no-op: there is no directory to
|
|
233
|
+
* mark, and a `.done` written beside a directory that does not exist would be a
|
|
234
|
+
* marker for nothing.
|
|
235
|
+
*/
|
|
236
|
+
export function markSettled(run: { id?: string; scratchDir?: string }, log?: Logger): void {
|
|
237
|
+
if (run.scratchDir === undefined) return;
|
|
238
|
+
const sink = log ?? fallbackLog;
|
|
239
|
+
try {
|
|
240
|
+
const marked = markRunDone(run.scratchDir);
|
|
241
|
+
if (!marked.ok) {
|
|
242
|
+
sink.warn("scratch root: settle marker not written — the run's outcome is unchanged", {
|
|
243
|
+
runId: run.id,
|
|
244
|
+
runDir: run.scratchDir,
|
|
245
|
+
code: marked.code,
|
|
246
|
+
error: marked.message,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
} catch (error) {
|
|
250
|
+
sink.warn("scratch root: settle marker threw — the run's outcome is unchanged", {
|
|
251
|
+
runId: run.id,
|
|
252
|
+
runDir: run.scratchDir,
|
|
253
|
+
error: error instanceof Error ? error.message : String(error),
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|