brl-subagent 2.8.0 → 2.9.1

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.
Files changed (48) hide show
  1. package/AGENT.md +17 -2
  2. package/README.md +3 -3
  3. package/package.json +6 -5
  4. package/src/agent-tools.ts +332 -0
  5. package/src/background-pipeline.ts +1025 -0
  6. package/src/boot-recovery.ts +360 -0
  7. package/src/commands.ts +137 -0
  8. package/src/concurrency.ts +3 -5
  9. package/src/delegate-handler.ts +2261 -0
  10. package/src/engineering-bar.ts +1 -1
  11. package/src/git.ts +220 -8
  12. package/src/headstate.ts +3 -3
  13. package/src/history.ts +21 -3
  14. package/src/index.ts +109 -4473
  15. package/src/loader.ts +1 -1
  16. package/src/logging.ts +67 -4
  17. package/src/mode-prologue.ts +4 -10
  18. package/src/mode-runners.ts +1575 -0
  19. package/src/notify-completion.ts +35 -8
  20. package/src/params.ts +14 -13
  21. package/src/preflight.ts +123 -16
  22. package/src/prelude.ts +14 -5
  23. package/src/presets.ts +3 -5
  24. package/src/proc.ts +2 -3
  25. package/src/recovery-engine.ts +1 -1
  26. package/src/recovery-surface.ts +1 -2
  27. package/src/recovery.ts +238 -10
  28. package/src/reports.ts +1 -7
  29. package/src/run-registry.ts +133 -10
  30. package/src/runner.ts +47 -18
  31. package/src/runtime.ts +50 -0
  32. package/src/sanitize.ts +20 -2
  33. package/src/scheduler.ts +1 -2
  34. package/src/schema.ts +4 -2
  35. package/src/scratch-root.ts +909 -0
  36. package/src/scratch-spawn.ts +256 -0
  37. package/src/session-manager.ts +587 -81
  38. package/src/state.ts +12 -14
  39. package/src/templates.ts +3 -5
  40. package/src/terminal-notifier.ts +405 -0
  41. package/src/transcript.ts +1 -1
  42. package/src/tui-format.ts +3 -1
  43. package/src/tui.ts +46 -36
  44. package/src/types.ts +29 -3
  45. package/src/unit-params.ts +113 -0
  46. package/src/unit-run.ts +35 -15
  47. package/src/validate.ts +28 -9
  48. package/src/worktree-report.ts +142 -0
package/src/runtime.ts ADDED
@@ -0,0 +1,50 @@
1
+ // Purpose: The per-invocation runtime context (pi, log, state) the composition root builds once and hands to the mode runners.
2
+ /**
3
+ * brl-subagent — the runtime context (cycle S2, ADR 0027 D1/D3)
4
+ *
5
+ * ADR 0027 D1 splits the composition root from the code it composes. The mode
6
+ * runners (S2) are the first consumers that need the three long-lived bindings
7
+ * `src/index.ts`'s `default` export used to own as closures: `pi` (the extension
8
+ * API), `log` (the session logger) and `state` (the session-bound store).
9
+ *
10
+ * `RuntimeContext` names those three, and `createRuntime` builds them with the
11
+ * exact order and arguments `index.ts` used inline — `createLogger` first
12
+ * because `createSessionState` takes the logger. D1 is why this is a value, not
13
+ * a module-level singleton: one context per `initExtension` invocation, so two
14
+ * loaded sessions in one process share no state.
15
+ *
16
+ * Deliberately NOT part of the context (D3): `sessionCtx`,
17
+ * `recoveryNoticeDelivered` and the other `default`-export closures. The runners
18
+ * never read them (recon at 46b427e: zero references across the moved range);
19
+ * adding one here without a consumer would be a binding the split does not
20
+ * need. ADR 0027 F1: if a runner ever needs a fourth field, the split is wrong —
21
+ * report it rather than widening this interface.
22
+ */
23
+
24
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
25
+ import { createLogger, type Logger } from "./logging";
26
+ import { createSessionState, type SessionState } from "./state";
27
+
28
+ /**
29
+ * The three bindings the mode runners read, captured once per extension
30
+ * invocation. `readonly` because the split's contract is "handed in, never
31
+ * rebound": `createRuntime` produces it, `createModeRunners` destructures it,
32
+ * and nothing downstream reassigns a field (a rebind would be a second owner).
33
+ */
34
+ export interface RuntimeContext {
35
+ readonly pi: ExtensionAPI;
36
+ readonly log: Logger;
37
+ readonly state: SessionState;
38
+ }
39
+
40
+ /**
41
+ * Build the runtime context for one extension invocation — the same two calls
42
+ * `index.ts` made inline (log → state, in that order). The only reason this is a
43
+ * function rather than three lines at the call site: the S2 boundary test drives
44
+ * two contexts and asserts they are independent (ADR 0027 D1).
45
+ */
46
+ export function createRuntime(pi: ExtensionAPI): RuntimeContext {
47
+ const log = createLogger("brl-subagent");
48
+ const state = createSessionState(log);
49
+ return { pi, log, state };
50
+ }
package/src/sanitize.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  import * as path from "node:path";
10
10
  import * as fs from "node:fs";
11
11
  import { EMPTY_USAGE, DEFAULT_OUTPUT_CAP_BYTES } from "./types";
12
+ import { appendWorktreeState } from "./worktree-report";
12
13
 
13
14
  // ---------------------------------------------------------------------------
14
15
  // F1: Input sanitization
@@ -152,6 +153,14 @@ export function assertSafeAgentId(id: string): void {
152
153
  const SAFE_ENV_KEYS = new Set([
153
154
  "PATH",
154
155
  "HOME",
156
+ // Issue #511 (U2): the Windows halves of the same question. The shipped
157
+ // scratch root is `%LOCALAPPDATA%\brl-subagent` there, and `USERPROFILE` is
158
+ // what a nested child needs to find its own home at all — `HOME` alone leaves
159
+ // a Windows grandchild with no root to resolve. Symmetric with `HOME`, and
160
+ // deliberately NOT a place for `BRL_SCRATCH`: that value is per-run, so it is
161
+ // overridden at the spawn seam and stripped from anything inherited.
162
+ "USERPROFILE",
163
+ "LOCALAPPDATA",
155
164
  "USER",
156
165
  "LANG",
157
166
  "LC_ALL",
@@ -316,7 +325,7 @@ function neutralizePathPrefix(text: string, prefix: string, replacement: string)
316
325
  * 4. Caps length at 2000 chars with an ellipsis marker.
317
326
  *
318
327
  * Remaining-absolute-path policy: paths OUTSIDE the project root and its
319
- * parent are left as-is (e.g. `/etc/passwd`, `/usr/bin/node`, `/tmp/x.log`).
328
+ * parent are left as-is (e.g. `/etc/passwd`, `/usr/bin/node`, `/var/log/x.log`).
320
329
  * Issue #30 is specifically about CWD/home disclosure; system paths carry no
321
330
  * personal structure and keeping them preserves debugging value. Messages
322
331
  * that are short and clean (no ANSI, no cwd/parent prefixes) pass through
@@ -357,6 +366,13 @@ export function sanitizeErrorMessage(msg: string, cwd?: string): string {
357
366
  * serializes `content` into the LLM context, while the raw text persists to
358
367
  * the session file for post-mortems (issue #68 framing: extraction is DRY,
359
368
  * not a stderr-content change).
369
+ *
370
+ * Issue #474: this is the ONE crash seam (single, chain, graph and the per-unit
371
+ * finalizer all build their envelope here), so the worktree-state block is
372
+ * appended here rather than at seven call sites. `err` may carry the crashing
373
+ * UNIT's own block (`finalizeUnitRunCrash` attaches it) and that wins over
374
+ * `cwd`, which a mode-level caller only knows as the dispatch cwd. The block is
375
+ * appended, never spliced: the cause reads first, the residue after.
360
376
  */
361
377
  export function buildCrashResult(mode: string, err: unknown, cwd: string): {
362
378
  content: Array<{ type: "text"; text: string }>;
@@ -374,7 +390,9 @@ export function buildCrashResult(mode: string, err: unknown, cwd: string): {
374
390
  cwd,
375
391
  );
376
392
  return {
377
- content: [{ type: "text", text: `${mode} crashed: ${errorMessage}` }],
393
+ content: [
394
+ { type: "text", text: appendWorktreeState(`${mode} crashed: ${errorMessage}`, cwd, err) },
395
+ ],
378
396
  details: {
379
397
  messages: [],
380
398
  usage: { ...EMPTY_USAGE },
package/src/scheduler.ts CHANGED
@@ -6,8 +6,7 @@
6
6
  * and graph validation for P10 dependency graph mode.
7
7
  */
8
8
 
9
- import type { GraphTask } from "./types";
10
- import { MAX_GRAPH_TASKS } from "./types";
9
+ import { MAX_GRAPH_TASKS, type GraphTask } from "./types";
11
10
 
12
11
  // ---------------------------------------------------------------------------
13
12
  // Cycle detection — three-color DFS
package/src/schema.ts CHANGED
@@ -128,8 +128,10 @@ export const delegateTaskParamsSchema = Type.Object({
128
128
  Type.Number({
129
129
  description:
130
130
  "Maximum time in milliseconds the subagent is allowed to run. " +
131
- "If exceeded, the subagent is killed and an error is returned. " +
132
- "Background runs default to a 30-minute deadline when no timeout is given; an explicit timeout is honored as given (30 minutes is a default, not a ceiling). A raw value ≥ 2^31-1 normalizes to the default.",
131
+ "A **background** run with no valid timeout (none passed, or normalized away) gets the 30-minute default deadline; a **foreground** run has no default. " +
132
+ "An explicit **positive** timeout is honored as given, except values that normalize to no timeout (a raw value `≥ 2^31-1` or a nonpositive value; a background run then gets the 30-minute default, a foreground run has none) and the opt-in `processChecks` high/xhigh foreground switch, which may drop an explicit timeout. " +
133
+ "**Foreground expiry** kills the process (SIGTERM, 5s grace, then SIGKILL); the expired attempt's result is a timeout error, and with `retryOnTimeout: true` the tool may retry automatically and return the later result. " +
134
+ "**Background expiry** requests a cooperative abort — a prompt that ignores it keeps running; the run's **timeout snapshot** (`failed`/`timeout`) is written at expiry, and a later settle **amends** that record (never re-classifies it).",
133
135
  }),
134
136
  ),
135
137
  cwd: Type.Optional(