pi-better-subagents 0.35.0 → 0.37.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.
Files changed (97) hide show
  1. package/README.md +13 -11
  2. package/assignment-outcomes.ts +9 -6
  3. package/assignment-report.ts +103 -19
  4. package/batch.mjs +14 -5
  5. package/bin/recover-workspace.mjs +19 -0
  6. package/catalog-host-admission.ts +19 -3
  7. package/catalog-preferences.ts +1 -1
  8. package/catalog-resolver.ts +35 -1
  9. package/catalog-store.ts +27 -20
  10. package/child-apply-patch.ts +13 -0
  11. package/child-dependencies.mjs +25 -12
  12. package/child-operations.ts +80 -1
  13. package/child-preflight.mjs +8 -2
  14. package/child-process-list.ts +8 -0
  15. package/cleanup.ts +10 -2
  16. package/config.ts +53 -3
  17. package/continuation-authority.ts +3 -1
  18. package/continuation-runtime.ts +88 -0
  19. package/continuation.ts +4 -2
  20. package/docs/usage.md +92 -81
  21. package/extensions.mjs +11 -13
  22. package/failures.ts +10 -2
  23. package/finalization.ts +21 -1
  24. package/goal-provider.ts +44 -2
  25. package/health-observation.ts +43 -48
  26. package/index.ts +417 -155
  27. package/isolated-workspace.ts +557 -0
  28. package/launch-context.ts +23 -0
  29. package/launch-digest.mjs +6 -0
  30. package/list.mjs +2 -2
  31. package/log-cursor.ts +91 -15
  32. package/managed-launch-bootstrap.mjs +2 -2
  33. package/managed-launch-client.ts +28 -8
  34. package/managed-launch-host.ts +168 -15
  35. package/managed-launch-journal.mjs +1 -5
  36. package/managed-launch-port.ts +4 -0
  37. package/operation-bridge-types.ts +27 -2
  38. package/output-payload.ts +26 -12
  39. package/owned-exit.ts +12 -0
  40. package/package.json +4 -2
  41. package/parse.ts +57 -10
  42. package/permission-policy.ts +41 -8
  43. package/process-identity.d.mts +1 -0
  44. package/process-identity.mjs +29 -0
  45. package/registry.ts +77 -38
  46. package/retained-result.ts +65 -0
  47. package/runtime-client/artifacts/darwin-arm64.gz +0 -0
  48. package/runtime-client/artifacts/darwin-arm64.json +1 -1
  49. package/runtime-client/artifacts/darwin-x64.gz +0 -0
  50. package/runtime-client/artifacts/darwin-x64.json +1 -1
  51. package/runtime-client/artifacts/linux-arm64.gz +0 -0
  52. package/runtime-client/artifacts/linux-arm64.json +1 -1
  53. package/runtime-client/artifacts/linux-x64.gz +0 -0
  54. package/runtime-client/artifacts/linux-x64.json +1 -1
  55. package/runtime-client/src/runtime/client.mjs +30 -3
  56. package/runtime-client/src/runtime/root.mjs +5 -2
  57. package/runtime-client/src/runtime/work-client.mjs +150 -7
  58. package/session-selection.ts +2 -1
  59. package/session-store.ts +9 -0
  60. package/settings-page.ts +147 -108
  61. package/shared-callback-batcher.ts +11 -2
  62. package/shared-catalog.ts +29 -21
  63. package/shared-navigator-panels.ts +26 -4
  64. package/shared-navigator.ts +244 -40
  65. package/shared-operation-coordinator/assignment-inbox.mjs +118 -0
  66. package/shared-operation-coordinator/assignment-observation.mjs +43 -35
  67. package/shared-operation-coordinator/src/assignment-changes.ts +73 -0
  68. package/shared-operation-coordinator/src/assignment-outcomes.ts +2 -0
  69. package/shared-operation-coordinator/src/background-requests.ts +14 -3
  70. package/shared-operation-coordinator/src/discovery.ts +5 -3
  71. package/shared-operation-coordinator/src/operations.ts +50 -2
  72. package/shared-operation-coordinator/src/outbox.ts +9 -0
  73. package/shared-operation-coordinator/src/result-supervision.ts +12 -0
  74. package/shared-operation-coordinator/src/rows.ts +2 -0
  75. package/shared-operation-coordinator/src/schema.ts +40 -24
  76. package/shared-operation-coordinator/src/store.ts +40 -8
  77. package/shared-operation-coordinator/src/subagent-launch.ts +24 -1
  78. package/shared-operation-coordinator/src/types.ts +5 -1
  79. package/shared-sandbox-core.ts +1 -1
  80. package/shared-task-exec.ts +12 -1
  81. package/shared-task-process-identity.ts +126 -0
  82. package/shared-task-process-list.ts +8 -2
  83. package/shared-task-sandbox.ts +5 -3
  84. package/shared-task-tools.ts +8 -5
  85. package/shared-work-client.d.mts +10 -0
  86. package/shared-work-client.mjs +150 -7
  87. package/subagent-tools.ts +28 -34
  88. package/task-guard.ts +13 -1
  89. package/task-policy.ts +8 -2
  90. package/timing.ts +10 -1
  91. package/tools.ts +4 -7
  92. package/work-observation.ts +91 -17
  93. package/worker-operations-host.ts +12 -4
  94. package/worker-waits.ts +93 -34
  95. package/workspace-files.ts +114 -0
  96. package/workspace-scope.ts +6 -0
  97. package/worktree-feedback.ts +77 -0
@@ -11,7 +11,7 @@
11
11
  */
12
12
 
13
13
  import { statSync } from "node:fs";
14
- import { DEFAULT_MAX_READ_BYTES, readAppendedLines, type LogCursor } from "./log-cursor.ts";
14
+ import { DEFAULT_MAX_READ_BYTES, readAppendedLines, parseLogEvent, logReadMetrics, type LogCursor } from "./log-cursor.ts";
15
15
  import { logPathFor } from "./registry.ts";
16
16
  import type { RunStatus } from "./registry.ts";
17
17
  import { loadConfig, type SubagentConfig } from "./config.ts";
@@ -251,16 +251,20 @@ export function mayAffectChildEventFacts(line: string): boolean {
251
251
  * that already parsed NDJSON). Skips non-objects; ignores raw non-JSON noise.
252
252
  */
253
253
  export function extractChildEventFacts(events: ReadonlyArray<unknown>): ChildEventFacts {
254
- const openTools = new Map<string, ActiveToolFact>();
255
- let anonymous = 0;
256
- const meaningful: { last?: number } = {};
257
- let lastToolAt: number | undefined;
258
- let compacting = false;
259
- let compactionStartedAt: number | undefined;
260
- let lastCompaction: CompactionEndFact | undefined;
261
- let sawAgentSettled = false;
262
- let retry: ModelFacts["retry"];
263
- const recovery: RetryRecoveryState = {
254
+ return createChildEventFactFold()(events);
255
+ }
256
+
257
+ function createChildEventFactFold(saved?: any) {
258
+ const openTools = new Map<string, ActiveToolFact>(saved?.openTools);
259
+ let anonymous = saved?.anonymous ?? 0;
260
+ const meaningful: { last?: number } = saved?.meaningful ?? {};
261
+ let lastToolAt: number | undefined = saved?.lastToolAt;
262
+ let compacting = saved?.compacting ?? false;
263
+ let compactionStartedAt: number | undefined = saved?.compactionStartedAt;
264
+ let lastCompaction: CompactionEndFact | undefined = saved?.lastCompaction;
265
+ let sawAgentSettled = saved?.sawAgentSettled ?? false;
266
+ let retry: ModelFacts["retry"] = saved?.retry;
267
+ const recovery: RetryRecoveryState = saved?.recovery ?? {
264
268
  recoveredAfterError: false,
265
269
  retrying: false,
266
270
  willRetry: undefined,
@@ -268,7 +272,9 @@ export function extractChildEventFacts(events: ReadonlyArray<unknown>): ChildEve
268
272
  errorHistory: [],
269
273
  };
270
274
 
275
+ const fold = (events: ReadonlyArray<unknown>): ChildEventFacts => {
271
276
  for (const raw of events) {
277
+ logReadMetrics.healthFolded++;
272
278
  if (!raw || typeof raw !== "object") continue;
273
279
  const e = raw as LooseEvent;
274
280
  const type = typeof e.type === "string" ? e.type : undefined;
@@ -431,6 +437,20 @@ export function extractChildEventFacts(events: ReadonlyArray<unknown>): ChildEve
431
437
  willRetry: recovery.willRetry,
432
438
  longModelCallSupported: false,
433
439
  };
440
+ };
441
+ return Object.assign(fold, { snapshot: () => ({ openTools: [...openTools], anonymous,
442
+ meaningful, lastToolAt, compacting, compactionStartedAt, lastCompaction,
443
+ sawAgentSettled, retry, recovery }) });
444
+ }
445
+
446
+ export function encodeHealthLogState(state: FactLogState): unknown {
447
+ return { cursor: state.cursor, truncated: state.truncated, fold: state.fold.snapshot() };
448
+ }
449
+
450
+ export function decodeHealthLogState(saved: any): FactLogState {
451
+ if (!saved?.fold?.recovery || !Array.isArray(saved.fold.openTools)
452
+ || !Number.isSafeInteger(saved.cursor?.offset) || saved.cursor.offset < 0) throw new Error("Invalid health checkpoint");
453
+ return { cursor: saved.cursor, truncated: saved.truncated, fold: createChildEventFactFold(saved.fold) };
434
454
  }
435
455
 
436
456
  /** Parse the fact-relevant events out of raw NDJSON lines. */
@@ -442,7 +462,7 @@ function parseFactEvents(lines: Iterable<string>): { events: unknown[]; sizes: n
442
462
  if (!s || s[0] !== "{") continue;
443
463
  if (!mayAffectChildEventFacts(s)) continue;
444
464
  try {
445
- events.push(JSON.parse(s));
465
+ events.push(parseLogEvent(s));
446
466
  sizes.push(s.length);
447
467
  } catch {
448
468
  // bad JSON — ignore (noise / partial line)
@@ -453,15 +473,8 @@ function parseFactEvents(lines: Iterable<string>): { events: unknown[]; sizes: n
453
473
 
454
474
  // ---- incremental log reading ----------------------------------------------
455
475
  //
456
- // Logs are append-only and reach gigabytes, so facts are folded from an
457
- // accumulated event list that grows by the bytes appended since the last read
458
- // (see log-cursor.ts). Only the types the fold consumes are retained, which is
459
- // ~2% of a log's lines; `message_update` alone is 98% of the volume and cannot
460
- // move a fact. The retained list is capped, oldest-first, and a drop is
461
- // reported as a diagnostic rather than silently changing an observation.
462
-
463
- /** Memory budget for one run's retained fact-relevant events. */
464
- const MAX_RETAINED_EVENT_BYTES = 8 * 1024 * 1024; // 8 MiB
476
+ // Retain structural facts, not source events. Only newly completed lines enter
477
+ // the fold; a skipped byte window remains explicitly reported in rawLog.
465
478
 
466
479
  function maxHealthReadBytes(): number {
467
480
  const raw = process.env.PI_SUBAGENT_MAX_HEALTH_READ_BYTES;
@@ -478,13 +491,8 @@ const MAX_TRACKED_RUNS = 64;
478
491
 
479
492
  interface FactLogState {
480
493
  cursor: LogCursor;
481
- events: unknown[];
482
- /** Retained source bytes per event, index-aligned with `events`. */
483
- sizes: number[];
484
- /** Sum of `sizes`, for the drop-oldest budget. */
485
- bytes: number;
494
+ fold: ReturnType<typeof createChildEventFactFold>;
486
495
  truncated: boolean;
487
- dropped: boolean;
488
496
  }
489
497
 
490
498
  const factLogStates = new Map<string, FactLogState>();
@@ -509,15 +517,6 @@ function rememberState(id: string, state: FactLogState): void {
509
517
  }
510
518
  }
511
519
 
512
- function trimToBudget(state: FactLogState): void {
513
- while (state.bytes > MAX_RETAINED_EVENT_BYTES && state.events.length > 1) {
514
- state.events.shift();
515
- state.bytes -= state.sizes.shift() ?? 0;
516
- state.dropped = true;
517
- }
518
- if (state.bytes < 0) state.bytes = 0;
519
- }
520
-
521
520
  /**
522
521
  * Read a run log for event facts + raw mtime/size diagnostics.
523
522
  * Raw log write time never promotes activity health by itself.
@@ -529,7 +528,7 @@ function trimToBudget(state: FactLogState): void {
529
528
  */
530
529
  export function extractChildEventFactsFromLog(
531
530
  id: string,
532
- opts: { now?: number; logText?: string; logPath?: string } = {},
531
+ opts: { now?: number; logText?: string; logPath?: string; states?: Map<string, FactLogState> } = {},
533
532
  ): { facts: ChildEventFacts; rawLog: RawLogDiagnostic } {
534
533
  const path = opts.logPath ?? logPathFor(id);
535
534
  const rawLog: RawLogDiagnostic = {};
@@ -553,7 +552,8 @@ export function extractChildEventFactsFromLog(
553
552
  }
554
553
 
555
554
  const key = opts.logPath ?? id;
556
- const previous = factLogStates.get(key);
555
+ const states = opts.states ?? factLogStates;
556
+ const previous = states.get(key);
557
557
  const read = readAppendedLines(path, previous?.cursor, maxHealthReadBytes());
558
558
  if (read.error !== undefined) {
559
559
  rawLog.error = rawLog.error ?? read.error;
@@ -561,23 +561,18 @@ export function extractChildEventFactsFromLog(
561
561
  }
562
562
 
563
563
  const state: FactLogState = read.restarted || !previous
564
- ? { cursor: read.cursor, events: [], sizes: [], bytes: 0, truncated: false, dropped: false }
564
+ ? { cursor: read.cursor, fold: createChildEventFactFold(), truncated: false }
565
565
  : previous;
566
566
  state.cursor = read.cursor;
567
567
  state.truncated = state.truncated || read.truncated;
568
568
 
569
569
  const parsed = parseFactEvents(read.lines);
570
- if (parsed.events.length > 0) {
571
- state.events.push(...parsed.events);
572
- state.sizes.push(...parsed.sizes);
573
- for (const size of parsed.sizes) state.bytes += size;
574
- trimToBudget(state);
575
- }
576
- rememberState(key, state);
570
+ const facts = state.fold(parsed.events);
571
+ if (opts.states) states.set(key, state);
572
+ else rememberState(key, state);
577
573
 
578
574
  if (state.truncated) rawLog.windowTruncated = true;
579
- if (state.dropped) rawLog.eventsDropped = true;
580
- return { facts: extractChildEventFacts(state.events), rawLog };
575
+ return { facts, rawLog };
581
576
  }
582
577
 
583
578
  // ---- observation ----------------------------------------------------------