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/loader.ts CHANGED
@@ -25,9 +25,9 @@
25
25
  * which is how PROJECT > USER precedence is enforced.
26
26
  */
27
27
 
28
+ import type { Logger } from "./logging";
28
29
  import * as fs from "node:fs";
29
30
  import * as path from "node:path";
30
- import type { Logger } from "./logging";
31
31
 
32
32
  // ---------------------------------------------------------------------------
33
33
  // Frontmatter parsing
package/src/logging.ts CHANGED
@@ -14,9 +14,9 @@
14
14
  * session starts and all loggers share <cwd>/.pi/subagent-logs/brl-subagent.log.
15
15
  */
16
16
 
17
+ import type { LogLevel } from "./types";
17
18
  import * as fs from "node:fs";
18
19
  import * as path from "node:path";
19
- import type { LogLevel } from "./types";
20
20
 
21
21
  // ---------------------------------------------------------------------------
22
22
  // Configuration
@@ -129,6 +129,61 @@ function resolveLogDir(): string | undefined {
129
129
  return logCwd ? path.join(logCwd, ".pi", "subagent-logs") : undefined;
130
130
  }
131
131
 
132
+ // ---------------------------------------------------------------------------
133
+ // Serialisation (issue #545)
134
+ // ---------------------------------------------------------------------------
135
+
136
+ /**
137
+ * #545: `data` is caller-supplied and `JSON.stringify` throws on a cycle and on a
138
+ * `BigInt`. A cycle collapses to `[Circular]`, a `BigInt` to its decimal literal
139
+ * (`10n`, and `-0n` to `"0n"`). Ancestors are tracked rather than every value ever
140
+ * seen, so `{a: x, b: x}` — a shared reference, not a cycle — keeps both copies.
141
+ * The replacer only sees values JSON reaches: a falsy root `data` (`0n`, `-0n`,
142
+ * `""`) is dropped by `log()`'s truthiness check before serialisation.
143
+ */
144
+ function circularSafeReplacer(): (this: unknown, key: string, value: unknown) => unknown {
145
+ const ancestors: unknown[] = [];
146
+ return function (this: unknown, _key: string, value: unknown): unknown {
147
+ if (typeof value === "bigint") return `${value}n`;
148
+ if (typeof value !== "object" || value === null) return value;
149
+ while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) ancestors.pop();
150
+ if (ancestors.includes(value)) return "[Circular]";
151
+ ancestors.push(value);
152
+ return value;
153
+ };
154
+ }
155
+
156
+ /** #545: why a payload failed to serialise — guarded, because the caught value can
157
+ * be a Proxy whose `.message` access throws too. */
158
+ function faultReason(err: unknown): string {
159
+ try {
160
+ const message = (err as { message?: unknown }).message;
161
+ return typeof message === "string" && message !== "" ? message : "unknown fault";
162
+ } catch {
163
+ return "unknown fault";
164
+ }
165
+ }
166
+
167
+ /** #545: the entry line, defensively. The replacer covers a cycle and a `BigInt`;
168
+ * the catch covers what it cannot — a throwing `toJSON`, getter or Proxy trap.
169
+ * `data` becomes an object marker, `{"[unserializable]": reason}`: an object keeps
170
+ * every consumer that reads `data.<field>` alive (`missing → undefined`), where a
171
+ * string would crash one. The line is serialised and reaches the conditional
172
+ * file-write attempt below; whether a file exists still depends on a configured
173
+ * cwd and on the sink, which keeps swallowing its own faults. */
174
+ function serializeEntry(entry: Record<string, unknown>): string {
175
+ try {
176
+ return JSON.stringify(entry, circularSafeReplacer());
177
+ } catch (err) {
178
+ return JSON.stringify({
179
+ ...entry,
180
+ prefix: typeof entry.prefix === "string" ? entry.prefix : "[unprintable prefix]",
181
+ message: typeof entry.message === "string" ? entry.message : "[unprintable message]",
182
+ data: { "[unserializable]": faultReason(err) },
183
+ });
184
+ }
185
+ }
186
+
132
187
  /**
133
188
  * Create a logger for a specific module.
134
189
  * Logs are written to `<cwd>/.pi/subagent-logs/brl-subagent.log` relative to the
@@ -150,13 +205,21 @@ export function createLogger(prefix: string, cwd?: string): Logger {
150
205
  ...(data ? { data } : {}),
151
206
  };
152
207
 
153
- const line = JSON.stringify(entry);
208
+ const line = serializeEntry(entry);
154
209
 
155
210
  // Console mirror — opt-in (issue #265). Off by default so a logger call
156
211
  // can never corrupt the pi TUI renderer; BRL_LOG_CONSOLE opts back in.
157
212
  if (consoleOutput) {
158
- const consoleMethod = level === "error" ? console.error : level === "warn" ? console.warn : console.log;
159
- consoleMethod(`[${prefix}] ${level.toUpperCase()}: ${message}`);
213
+ try {
214
+ // Selection is inside the try on purpose — a replaced console can
215
+ // throw on the property access itself, not only on the call.
216
+ const consoleMethod = level === "error" ? console.error : level === "warn" ? console.warn : console.log;
217
+ consoleMethod(`[${prefix}] ${level.toUpperCase()}: ${message}`);
218
+ } catch {
219
+ // Issue #543: the mirror fault does not prevent the file-write attempt
220
+ // when a cwd is configured; persistence remains conditional on the file
221
+ // sink, and `log.*` returns normally.
222
+ }
160
223
  }
161
224
 
162
225
  // File output — resolved on each call because the cwd may be set after
@@ -37,20 +37,14 @@
37
37
  * same point in the sequence.
38
38
  */
39
39
 
40
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
41
- import type { ToolResult } from "./types";
42
- import type { SessionState } from "./state";
43
40
  import type { Logger } from "./logging";
41
+ import type { SessionState } from "./state";
42
+ import type { ToolResult } from "./types";
43
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
44
44
  import { resolveSubagentParams, type ResolveInputParams } from "./params";
45
45
  import { getCurrentDepth } from "./sanitize";
46
46
  import { preflightCheck } from "./preflight";
47
- import {
48
- depthLimitRejection,
49
- gateSessionCost,
50
- rejection,
51
- runPreTaskValidation,
52
- validateDelegationTargets,
53
- } from "./prelude";
47
+ import { depthLimitRejection, gateSessionCost, rejection, runPreTaskValidation, validateDelegationTargets } from "./prelude";
54
48
 
55
49
  /** The params object the shared resolver consumes (`ResolveInputParams`,
56
50
  * params.ts) — the same type the background and single paths pass. */