pi-better-subagents 0.34.0 → 0.35.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/assignment-outcomes.ts +44 -6
- package/capacity.mjs +13 -0
- package/continuation-authority.ts +29 -0
- package/continuation.ts +21 -0
- package/failures.ts +7 -1
- package/health.ts +0 -1
- package/index.ts +273 -109
- package/managed-background-capability.ts +9 -0
- package/managed-failure-worker.mjs +27 -0
- package/managed-launch-bootstrap.mjs +35 -0
- package/managed-launch-client.ts +56 -0
- package/managed-launch-host.ts +351 -0
- package/managed-launch-journal.mjs +18 -0
- package/managed-launch-ownership.ts +22 -0
- package/managed-launch-port.ts +21 -0
- package/managed-launch-recovery.ts +45 -0
- package/managed-worker-bridge.ts +48 -0
- package/managed-worker.mjs +11 -0
- package/package.json +1 -1
- package/permission-policy.ts +12 -0
- package/registry.ts +2 -0
- 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/shared-callback-batcher.ts +4 -1
- package/shared-failure-observations.ts +3 -0
- package/shared-navigator.ts +23 -5
- package/shared-operation-coordinator/assignment-observation.mjs +30 -0
- package/shared-operation-coordinator/index.ts +2 -0
- package/shared-operation-coordinator/src/background-parent-callbacks.ts +68 -0
- package/shared-operation-coordinator/src/background-requests.ts +79 -0
- package/shared-operation-coordinator/src/schema.ts +31 -0
- package/shared-operation-coordinator/src/store.ts +18 -0
- package/shared-operation-coordinator/src/subagent-launch.ts +119 -0
- package/shared-operation-coordinator/src/upgrade.ts +22 -0
- package/task-runtime.mjs +1 -0
- package/timing-notifications.ts +37 -0
- package/timing.ts +1 -0
- package/tools.ts +10 -3
- package/work-observation.ts +18 -2
- package/worker-waits.ts +46 -14
package/assignment-outcomes.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { homedir } from "node:os";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import {
|
|
3
|
+
import { isDeepStrictEqual } from "node:util";
|
|
4
|
+
import { join, relative, isAbsolute, dirname, resolve } from "node:path";
|
|
4
5
|
import { openSync, closeSync, fstatSync, readSync, realpathSync, constants } from "node:fs";
|
|
5
|
-
import { openStore, type OwnerIdentity, type AssignmentOutcomeRecord } from "./shared-operation-coordinator/index.ts";
|
|
6
|
+
import { openStore, ownerDiff, StoreError, type OwnerIdentity, type AssignmentOutcomeRecord } from "./shared-operation-coordinator/index.ts";
|
|
6
7
|
import type { RunMeta } from "./registry.ts";
|
|
7
|
-
import { processStartToken } from "./registry.ts";
|
|
8
|
+
import { processStartToken, readMeta } from "./registry.ts";
|
|
8
9
|
import type { PermissionSnapshot } from "./permission-policy.ts";
|
|
9
10
|
import { credentialFilePaths } from "./shared-sandbox-core.ts";
|
|
10
11
|
import { readAssignmentReport } from "./assignment-report.ts";
|
|
@@ -43,9 +44,30 @@ export function coordinatorPath(): string {
|
|
|
43
44
|
return join(process.env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent"), "operation-coordinator.sqlite");
|
|
44
45
|
}
|
|
45
46
|
function withStore<T>(body: (store: ReturnType<typeof openStore>) => T): T {
|
|
46
|
-
const
|
|
47
|
+
const path = coordinatorPath();
|
|
48
|
+
const borrowed = runtimeStores.get(canonicalRoot(dirname(path)));
|
|
49
|
+
if (borrowed) return body(borrowed.store);
|
|
50
|
+
const store = openStore({ path });
|
|
47
51
|
try { return body(store); } finally { store.close(); }
|
|
48
52
|
}
|
|
53
|
+
const runtimeStores = new Map<string, { store: ReturnType<typeof openStore>; leases: number }>();
|
|
54
|
+
function canonicalRoot(root: string): string {
|
|
55
|
+
try { return realpathSync(root); } catch { return resolve(root); }
|
|
56
|
+
}
|
|
57
|
+
/** Fixed runtime adapter borrows its mutation connection; it retains ownership. */
|
|
58
|
+
export function bindRuntimeOutcomeStore(root: string, store: ReturnType<typeof openStore>): () => void {
|
|
59
|
+
const key = canonicalRoot(root);
|
|
60
|
+
let entry = runtimeStores.get(key);
|
|
61
|
+
if (entry && entry.store !== store) throw new Error("Assignment runtime store already bound");
|
|
62
|
+
if (!entry) { entry = { store, leases: 0 }; runtimeStores.set(key, entry); }
|
|
63
|
+
entry.leases++;
|
|
64
|
+
let released = false;
|
|
65
|
+
return () => {
|
|
66
|
+
if (released) return;
|
|
67
|
+
released = true;
|
|
68
|
+
if (--entry.leases === 0) runtimeStores.delete(key);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
49
71
|
export function registerAssignmentAttempt(binding: AssignmentBinding, attemptId: string): void {
|
|
50
72
|
if (binding.version !== ASSIGNMENT_OUTCOMES_VERSION) throw new Error("Unsupported assignment outcome capability.");
|
|
51
73
|
withStore((store) => store.registerAssignment({ ...binding, attemptId }));
|
|
@@ -69,7 +91,7 @@ export function commitAssignmentTerminal(meta: RunMeta, recoveryFailure = false)
|
|
|
69
91
|
catch { if (meta.status === "completed") invalidReport = "Assignment completion is unverified: retained report evidence is unavailable or changed."; }
|
|
70
92
|
const complete = meta.status === "completed" && (report ? report.status === "complete"
|
|
71
93
|
: !invalidReport && waits.some((w) => w.acknowledgement?.disposition === "assignment-complete"));
|
|
72
|
-
return store.recordAssignmentOutcome({ ...binding, revision: 1,
|
|
94
|
+
try { return store.recordAssignmentOutcome({ ...binding, revision: 1,
|
|
73
95
|
outcome: complete ? "succeeded" : meta.status === "killed" ? "cancelled" : "failed",
|
|
74
96
|
reason: meta.failureReason || invalidReport || (meta.status === "completed" && report ? report.reason : complete ? "Assigned work and required interpretation are complete."
|
|
75
97
|
: meta.status === "completed" ? "Assignment completion is unverified: the attempt exited without an evidence-backed completion decision. Inspect retained output and continue unfinished work or report a concrete blocker."
|
|
@@ -81,7 +103,23 @@ export function commitAssignmentTerminal(meta: RunMeta, recoveryFailure = false)
|
|
|
81
103
|
...(report ? [{ kind: "artifact" as const, ref: meta.assignmentReport!.path, summary: report.reason },
|
|
82
104
|
...report.evidence.map((e) => ({ kind: "artifact" as const, ref: e.path, summary: e.explanation }))] : [])],
|
|
83
105
|
waits: waits.map((w) => ({ waitId: w.waitId, generation: w.generation, cancelEpoch: w.cancelEpoch })),
|
|
84
|
-
});
|
|
106
|
+
}); } catch (error) {
|
|
107
|
+
// A competing finalizer may commit after our initial read. The store
|
|
108
|
+
// still refuses rewrites; return only the same captured binding's winner.
|
|
109
|
+
if (error instanceof StoreError && error.code === "validation" && error.message === "A terminal assignment cannot be rewritten or reopened.") {
|
|
110
|
+
const winner = store.getAssignmentOutcome(binding.assignmentId);
|
|
111
|
+
const current = readMeta(meta.id);
|
|
112
|
+
if (winner && winner.assignmentId === binding.assignmentId && ownerDiff(winner.owner, binding.owner) === "none"
|
|
113
|
+
&& ownerDiff(winner.recipient, binding.recipient) === "none"
|
|
114
|
+
&& current?.assignment?.assignmentId === binding.assignmentId
|
|
115
|
+
&& ownerDiff(current.assignment.owner, binding.owner) === "none" && ownerDiff(current.assignment.recipient, binding.recipient) === "none"
|
|
116
|
+
&& current.sessionId === meta.sessionId && current.cwd === meta.cwd && current.pid === meta.pid && current.pidStartTime === meta.pidStartTime
|
|
117
|
+
&& isDeepStrictEqual(current.management, meta.management)
|
|
118
|
+
&& current.operations?.resumeLaunchToken === meta.operations?.resumeLaunchToken
|
|
119
|
+
&& winner.evidence.some(evidence => evidence.kind === "log" && evidence.ref === meta.logPath)) return winner;
|
|
120
|
+
}
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
85
123
|
});
|
|
86
124
|
}
|
|
87
125
|
export function readOutcome(meta: RunMeta): AssignmentOutcomeRecord | null {
|
package/capacity.mjs
CHANGED
|
@@ -106,6 +106,19 @@ export function getSharedCapacityGate(countRunning) {
|
|
|
106
106
|
return sharedGate;
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
/** Server centralization retains each original process/start capacity domain. */
|
|
110
|
+
export function createOwnerCapacityGates(countOwner) {
|
|
111
|
+
const gates = new Map();
|
|
112
|
+
return owner => {
|
|
113
|
+
const key = JSON.stringify([owner.pid, owner.start]);
|
|
114
|
+
if (!gates.has(key)) {
|
|
115
|
+
const captured = { pid: owner.pid, start: owner.start };
|
|
116
|
+
gates.set(key, createCapacityGate(() => countOwner(captured)));
|
|
117
|
+
}
|
|
118
|
+
return gates.get(key);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
109
122
|
/** Test-only: drop the singleton so a suite starts from a clean ledger. */
|
|
110
123
|
export function _resetSharedCapacityGateForTests() {
|
|
111
124
|
sharedGate = undefined;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { parseSubagentTools } from "./shared-task-tools.ts";
|
|
2
|
+
/** Intersect captured permissions with the current mandatory profile. */
|
|
3
|
+
export function intersectContinuationPermissions(captured: any, current: any): any {
|
|
4
|
+
if (!captured || !current) throw new Error("Continuation permission authority unavailable");
|
|
5
|
+
const currentTools = parseSubagentTools(current.subagentTools);
|
|
6
|
+
const capturedTools = captured.subagentTools ? parseSubagentTools(captured.subagentTools) : currentTools;
|
|
7
|
+
const allowed = new Set(currentTools.trusted.map(tool => JSON.stringify([tool.name, tool.package])));
|
|
8
|
+
const tools = { applyPatch: capturedTools.applyPatch && currentTools.applyPatch,
|
|
9
|
+
trusted: capturedTools.trusted.filter(tool => allowed.has(JSON.stringify([tool.name, tool.package])))
|
|
10
|
+
.sort((a, b) => a.name.localeCompare(b.name) || a.package.localeCompare(b.package)) };
|
|
11
|
+
const intersect = (old: any, fresh: any) => {
|
|
12
|
+
if (!old || !fresh) throw new Error("Continuation permission profile unavailable");
|
|
13
|
+
const access = ["off", "read", "write", "read-write"];
|
|
14
|
+
const value = { ...fresh, enabled: old.enabled || fresh.enabled };
|
|
15
|
+
for (const key of ["projectFiles", "outsideProject", "storedCredentials"]) {
|
|
16
|
+
if (!access.includes(old[key]) || !access.includes(fresh[key])) throw new Error("Invalid continuation permission profile");
|
|
17
|
+
value[key] = access[Math.min(access.indexOf(old[key]), access.indexOf(fresh[key]))];
|
|
18
|
+
}
|
|
19
|
+
for (const key of ["commands", "network"]) value[key] = old[key] && fresh[key];
|
|
20
|
+
value.shell = old.shell !== false && fresh.shell !== false;
|
|
21
|
+
value.processAccess = old.processAccess === "read" && fresh.processAccess === "read" ? "read" : "off";
|
|
22
|
+
const caps = [old.commandTimeoutSeconds, fresh.commandTimeoutSeconds].filter((n) => typeof n === "number" && n > 0);
|
|
23
|
+
value.commandTimeoutSeconds = caps.length ? Math.min(...caps) : null;
|
|
24
|
+
return value;
|
|
25
|
+
};
|
|
26
|
+
return { permissions: captured.permissions ? intersect(captured.permissions, current.main) : current.main,
|
|
27
|
+
subagentPermissions: captured.subagentPermissions ? intersect(captured.subagentPermissions, current.subagents) : current.subagents,
|
|
28
|
+
subagentTools: tools };
|
|
29
|
+
}
|
package/continuation.ts
CHANGED
|
@@ -1028,3 +1028,24 @@ export function readConversationLease(sessionId: string): (LeasePayload & { raw:
|
|
|
1028
1028
|
return undefined;
|
|
1029
1029
|
}
|
|
1030
1030
|
}
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
/** Release exact undispatched server ownership under the ordinary conversation lock. */
|
|
1034
|
+
export function releaseUnspawnedConversationLease(input: {
|
|
1035
|
+
sessionId: string; attemptId: string; managerPid: number; managerStart: string;
|
|
1036
|
+
canProveUnspawned(): boolean;
|
|
1037
|
+
}): boolean {
|
|
1038
|
+
return withConversationMutation(input.sessionId, () => {
|
|
1039
|
+
const lease = readConversationLease(input.sessionId);
|
|
1040
|
+
if (!lease) return true;
|
|
1041
|
+
if (lease.attemptId !== input.attemptId || lease.pid !== input.managerPid || !lease.start
|
|
1042
|
+
|| Buffer.from(lease.start).toString("base64url") !== input.managerStart) return false;
|
|
1043
|
+
if (processExists(lease.pid)) {
|
|
1044
|
+
const start = processStartToken(lease.pid);
|
|
1045
|
+
if (!start || start === lease.start) return false;
|
|
1046
|
+
}
|
|
1047
|
+
if (!input.canProveUnspawned()) return false;
|
|
1048
|
+
releaseLeaseUnlocked(leasePathFor(input.sessionId), lease.raw);
|
|
1049
|
+
return readConversationLease(input.sessionId) === undefined;
|
|
1050
|
+
}, { waitMs: RELEASE_LOCK_WAIT_MS });
|
|
1051
|
+
}
|
package/failures.ts
CHANGED
|
@@ -160,7 +160,8 @@ function exactRuleLeftovers(path: string, prior: FailureState): Set<string> {
|
|
|
160
160
|
.filter((x) => x.category === "tool" && x.status === "unresolved" && leftover(x.id)).map((x) => x.id));
|
|
161
161
|
}
|
|
162
162
|
/** Scan complete source records, independent of the finite progress/transcript tail. */
|
|
163
|
-
export function collectRunFailures(id: string, cwd: string, terminal = false): FailureState {
|
|
163
|
+
export function collectRunFailures(id: string, cwd: string, terminal = false, assertCurrent: () => void = () => {}): FailureState {
|
|
164
|
+
assertCurrent();
|
|
164
165
|
const path = failurePath(id);
|
|
165
166
|
let fd: number;
|
|
166
167
|
try { fd = openSync(logPathFor(id), "r"); }
|
|
@@ -229,6 +230,7 @@ export function collectRunFailures(id: string, cwd: string, terminal = false): F
|
|
|
229
230
|
let oversized = false;
|
|
230
231
|
const buffer = Buffer.alloc(CHUNK);
|
|
231
232
|
while (position < size) {
|
|
233
|
+
assertCurrent();
|
|
232
234
|
const count = readSync(fd, buffer, 0, Math.min(CHUNK, size - position), position);
|
|
233
235
|
if (!count) break;
|
|
234
236
|
let from = 0;
|
|
@@ -251,9 +253,11 @@ export function collectRunFailures(id: string, cwd: string, terminal = false): F
|
|
|
251
253
|
(row.type === "tool_execution_end" && typeof row.isError !== "boolean" && typeof row.result?.isError !== "boolean" && typeof row.result?.exitCode !== "number") ||
|
|
252
254
|
(row.type === "message_end" && (!row.message || typeof row.message.role !== "string")) ||
|
|
253
255
|
(row.type === "auto_retry_end" && typeof row.success !== "boolean")) throw new Error("invalid structured event");
|
|
256
|
+
assertCurrent();
|
|
254
257
|
fold(id, scan, row, start, cwd);
|
|
255
258
|
}
|
|
256
259
|
} catch {
|
|
260
|
+
assertCurrent();
|
|
257
261
|
observeFailures(path, [{ id: `malformed:${start}`, operation: "child-log", kind: "incomplete", summary: "Child log contains malformed structured events; observations may be incomplete" }]);
|
|
258
262
|
}
|
|
259
263
|
}
|
|
@@ -270,6 +274,7 @@ export function collectRunFailures(id: string, cwd: string, terminal = false): F
|
|
|
270
274
|
if (!oversized && rest.length) fragments.push(Buffer.from(rest));
|
|
271
275
|
position += count;
|
|
272
276
|
}
|
|
277
|
+
assertCurrent();
|
|
273
278
|
scan.offset = start;
|
|
274
279
|
if (scan.gap) settleMetadataGap(id, scan);
|
|
275
280
|
scans.delete(id);
|
|
@@ -280,6 +285,7 @@ export function collectRunFailures(id: string, cwd: string, terminal = false): F
|
|
|
280
285
|
operation: "child-log-access", kind: "recovered", incidents: [access.id] }]);
|
|
281
286
|
if (terminal && position > start) observeFailures(path, [{ id: `partial:${start}`, operation: "child-log", kind: "incomplete", summary: "Child log ends with a partial event; observations may be incomplete" }]);
|
|
282
287
|
} catch {
|
|
288
|
+
assertCurrent();
|
|
283
289
|
observeFailures(path, [{ id: "log-read-error", operation: "child-log", kind: "incomplete", summary: "Child log could not be fully scanned; observations may be incomplete" }]);
|
|
284
290
|
} finally { closeSync(fd); }
|
|
285
291
|
return readRunFailures(id);
|
package/health.ts
CHANGED
|
@@ -244,7 +244,6 @@ export function needsMonitoring(
|
|
|
244
244
|
(m) => ownedByThisParent(m, parentPid) && (
|
|
245
245
|
m.status === "running"
|
|
246
246
|
|| m.status === "orphaned"
|
|
247
|
-
|| (m.status === "lost" && m.lostCallbackSentAt === undefined && m.lostCallbackSuppressedAt === undefined)
|
|
248
247
|
),
|
|
249
248
|
);
|
|
250
249
|
}
|