pi-better-subagents 0.35.0 → 0.36.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/README.md +1 -1
- package/assignment-outcomes.ts +11 -5
- package/assignment-report.ts +83 -19
- package/catalog-host-admission.ts +19 -3
- package/catalog-preferences.ts +1 -1
- package/catalog-resolver.ts +35 -1
- package/catalog-store.ts +27 -20
- package/child-apply-patch.ts +13 -0
- package/child-dependencies.mjs +25 -12
- package/child-operations.ts +80 -1
- package/child-preflight.mjs +8 -2
- package/child-process-list.ts +8 -0
- package/config.ts +46 -2
- package/continuation-authority.ts +3 -1
- package/continuation-runtime.ts +88 -0
- package/docs/usage.md +53 -28
- package/extensions.mjs +11 -13
- package/finalization.ts +15 -0
- package/goal-provider.ts +44 -2
- package/health-observation.ts +43 -48
- package/index.ts +257 -98
- package/launch-context.ts +23 -0
- package/launch-digest.mjs +6 -0
- package/log-cursor.ts +91 -15
- package/managed-launch-bootstrap.mjs +2 -2
- package/managed-launch-client.ts +6 -3
- package/managed-launch-host.ts +168 -15
- package/managed-launch-journal.mjs +1 -5
- package/managed-launch-port.ts +4 -0
- package/operation-bridge-types.ts +27 -2
- package/owned-exit.ts +12 -0
- package/package.json +1 -1
- package/parse.ts +48 -6
- package/permission-policy.ts +41 -8
- package/process-identity.d.mts +1 -0
- package/process-identity.mjs +29 -0
- package/registry.ts +54 -33
- package/runtime-client/artifacts/darwin-arm64.gz +0 -0
- package/runtime-client/artifacts/darwin-arm64.json +1 -1
- package/runtime-client/artifacts/darwin-x64.gz +0 -0
- package/runtime-client/artifacts/darwin-x64.json +1 -1
- package/runtime-client/artifacts/linux-arm64.gz +0 -0
- package/runtime-client/artifacts/linux-arm64.json +1 -1
- package/runtime-client/artifacts/linux-x64.gz +0 -0
- package/runtime-client/artifacts/linux-x64.json +1 -1
- package/runtime-client/src/runtime/client.mjs +30 -3
- package/runtime-client/src/runtime/work-client.mjs +150 -7
- package/shared-callback-batcher.ts +11 -2
- package/shared-catalog.ts +6 -14
- package/shared-navigator-panels.ts +26 -4
- package/shared-navigator.ts +197 -14
- package/shared-operation-coordinator/assignment-inbox.mjs +118 -0
- package/shared-operation-coordinator/assignment-observation.mjs +43 -35
- package/shared-operation-coordinator/src/assignment-changes.ts +73 -0
- package/shared-operation-coordinator/src/assignment-outcomes.ts +2 -0
- package/shared-operation-coordinator/src/background-requests.ts +14 -3
- package/shared-operation-coordinator/src/discovery.ts +5 -3
- package/shared-operation-coordinator/src/operations.ts +50 -2
- package/shared-operation-coordinator/src/result-supervision.ts +12 -0
- package/shared-operation-coordinator/src/rows.ts +2 -0
- package/shared-operation-coordinator/src/schema.ts +40 -24
- package/shared-operation-coordinator/src/store.ts +34 -8
- package/shared-operation-coordinator/src/subagent-launch.ts +24 -1
- package/shared-operation-coordinator/src/types.ts +5 -1
- package/shared-sandbox-core.ts +1 -1
- package/shared-task-exec.ts +12 -1
- package/shared-task-process-identity.ts +126 -0
- package/shared-task-process-list.ts +8 -2
- package/shared-task-sandbox.ts +5 -3
- package/shared-task-tools.ts +8 -5
- package/shared-work-client.d.mts +10 -0
- package/shared-work-client.mjs +150 -7
- package/subagent-tools.ts +28 -34
- package/task-policy.ts +3 -1
- package/timing.ts +10 -1
- package/work-observation.ts +71 -16
- package/worker-operations-host.ts +12 -4
- package/worker-waits.ts +93 -34
package/goal-provider.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { activateWorkObservation, hasWorkObservationContext, workObservation, onWorkObservation } from "./work-observation.ts";
|
|
2
3
|
import {
|
|
3
4
|
belongsToOrigin,
|
|
4
5
|
effectiveStatus,
|
|
@@ -38,13 +39,42 @@ function parkActivity(meta: Parameters<typeof effectiveStatus>[0]): Record<strin
|
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
function observedActivity(observed: any, ctx?: ExtensionContext) {
|
|
43
|
+
if (observed?.status !== "ready") throw new Error(observed?.reason ?? "Subagent activity is unverified");
|
|
44
|
+
return {
|
|
45
|
+
providerId: "subagents", label: "Subagents", nextDeadline: observed.nextDeadline ?? null,
|
|
46
|
+
items: observed.rows.filter((row: any) => !ctx || (
|
|
47
|
+
belongsToOrigin(row.meta, { cwd: ctx.cwd, sessionId: ctx.sessionManager.getSessionId() }) &&
|
|
48
|
+
(!row.meta.callbackOrigin?.branchId || ctx.sessionManager.getBranch().some(entry => entry.id === row.meta.callbackOrigin.branchId))
|
|
49
|
+
)).map((row: any) => {
|
|
50
|
+
const status = row.assignment.state;
|
|
51
|
+
const active = status === "running" || status === "waiting";
|
|
52
|
+
return { id: row.meta.id, label: row.meta.name, status, active, terminal: !active,
|
|
53
|
+
attention: !active && status !== "completed", unhealthy: row.meta.status === "orphaned",
|
|
54
|
+
startedAt: row.meta.startedAt, endedAt: row.meta.endedAt,
|
|
55
|
+
details: { assignment: row.assignment, sourceRevision: row.sourceRevision } };
|
|
56
|
+
}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
41
60
|
export function registerSubagentsGoalProvider(pi: ExtensionAPI): void {
|
|
42
61
|
const emitProvider = (): void => {
|
|
43
62
|
pi.events?.emit(GOAL_REGISTER_PROVIDER_EVENT, {
|
|
44
63
|
id: "subagents",
|
|
45
64
|
label: "Subagents",
|
|
46
65
|
scope: "session",
|
|
47
|
-
|
|
66
|
+
activityVersion: 1,
|
|
67
|
+
getActivity: (ctx?: ExtensionContext, options?: { cachedOnly?: boolean }) => {
|
|
68
|
+
const observed = workObservation();
|
|
69
|
+
if (!observed && options?.cachedOnly) {
|
|
70
|
+
return { providerId: "subagents", coverage: "unverified", items: [], nextDeadline: null };
|
|
71
|
+
}
|
|
72
|
+
if (!observed && hasWorkObservationContext()) {
|
|
73
|
+
return activateWorkObservation().then(() => observedActivity(workObservation(), ctx));
|
|
74
|
+
}
|
|
75
|
+
if (observed) {
|
|
76
|
+
return observedActivity(observed, ctx);
|
|
77
|
+
}
|
|
48
78
|
let sessionId: string | undefined;
|
|
49
79
|
try {
|
|
50
80
|
sessionId = ctx?.sessionManager?.getSessionId();
|
|
@@ -54,9 +84,11 @@ export function registerSubagentsGoalProvider(pi: ExtensionAPI): void {
|
|
|
54
84
|
const origin = ctx ? { cwd: ctx.cwd, sessionId } : undefined;
|
|
55
85
|
return {
|
|
56
86
|
providerId: "subagents",
|
|
87
|
+
nextDeadline: null,
|
|
57
88
|
label: "Subagents",
|
|
58
89
|
items: listMetasForParent(process.pid)
|
|
59
90
|
.filter((meta) => !origin || belongsToOrigin(meta, origin))
|
|
91
|
+
.filter((meta) => !ctx || !meta.callbackOrigin?.branchId || ctx.sessionManager.getBranch().some(entry => entry.id === meta.callbackOrigin?.branchId))
|
|
60
92
|
.map((meta) => {
|
|
61
93
|
const status = effectiveStatus(meta);
|
|
62
94
|
const active = status === "running" || status === "orphaned";
|
|
@@ -79,7 +111,17 @@ export function registerSubagentsGoalProvider(pi: ExtensionAPI): void {
|
|
|
79
111
|
}),
|
|
80
112
|
};
|
|
81
113
|
},
|
|
82
|
-
onActivityChanged:
|
|
114
|
+
onActivityChanged: (notify: () => void, ctx?: ExtensionContext) => {
|
|
115
|
+
const offObservation = onWorkObservation(notify);
|
|
116
|
+
const offMeta = onMetaChanged((meta) => {
|
|
117
|
+
if (ctx && meta) {
|
|
118
|
+
if (!belongsToOrigin(meta, { cwd: ctx.cwd, sessionId: ctx.sessionManager.getSessionId() })) return;
|
|
119
|
+
if (meta.callbackOrigin?.branchId && !ctx.sessionManager.getBranch().some(entry => entry.id === meta.callbackOrigin?.branchId)) return;
|
|
120
|
+
}
|
|
121
|
+
notify();
|
|
122
|
+
});
|
|
123
|
+
return () => { offObservation(); offMeta(); };
|
|
124
|
+
},
|
|
83
125
|
});
|
|
84
126
|
};
|
|
85
127
|
if (!goalReadySubscriptions.has(pi)) {
|
package/health-observation.ts
CHANGED
|
@@ -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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
let
|
|
260
|
-
|
|
261
|
-
let
|
|
262
|
-
let
|
|
263
|
-
|
|
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(
|
|
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
|
-
//
|
|
457
|
-
//
|
|
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
|
-
|
|
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
|
|
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,
|
|
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
|
-
|
|
571
|
-
|
|
572
|
-
|
|
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
|
-
|
|
580
|
-
return { facts: extractChildEventFacts(state.events), rawLog };
|
|
575
|
+
return { facts, rawLog };
|
|
581
576
|
}
|
|
582
577
|
|
|
583
578
|
// ---- observation ----------------------------------------------------------
|