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.
- package/README.md +13 -11
- package/assignment-outcomes.ts +9 -6
- package/assignment-report.ts +103 -19
- package/batch.mjs +14 -5
- package/bin/recover-workspace.mjs +19 -0
- 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/cleanup.ts +10 -2
- package/config.ts +53 -3
- package/continuation-authority.ts +3 -1
- package/continuation-runtime.ts +88 -0
- package/continuation.ts +4 -2
- package/docs/usage.md +92 -81
- package/extensions.mjs +11 -13
- package/failures.ts +10 -2
- package/finalization.ts +21 -1
- package/goal-provider.ts +44 -2
- package/health-observation.ts +43 -48
- package/index.ts +417 -155
- package/isolated-workspace.ts +557 -0
- package/launch-context.ts +23 -0
- package/launch-digest.mjs +6 -0
- package/list.mjs +2 -2
- package/log-cursor.ts +91 -15
- package/managed-launch-bootstrap.mjs +2 -2
- package/managed-launch-client.ts +28 -8
- 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/output-payload.ts +26 -12
- package/owned-exit.ts +12 -0
- package/package.json +4 -2
- package/parse.ts +57 -10
- package/permission-policy.ts +41 -8
- package/process-identity.d.mts +1 -0
- package/process-identity.mjs +29 -0
- package/registry.ts +77 -38
- package/retained-result.ts +65 -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/runtime-client/src/runtime/client.mjs +30 -3
- package/runtime-client/src/runtime/root.mjs +5 -2
- package/runtime-client/src/runtime/work-client.mjs +150 -7
- package/session-selection.ts +2 -1
- package/session-store.ts +9 -0
- package/settings-page.ts +147 -108
- package/shared-callback-batcher.ts +11 -2
- package/shared-catalog.ts +29 -21
- package/shared-navigator-panels.ts +26 -4
- package/shared-navigator.ts +244 -40
- 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/outbox.ts +9 -0
- 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 +40 -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-guard.ts +13 -1
- package/task-policy.ts +8 -2
- package/timing.ts +10 -1
- package/tools.ts +4 -7
- package/work-observation.ts +91 -17
- package/worker-operations-host.ts +12 -4
- package/worker-waits.ts +93 -34
- package/workspace-files.ts +114 -0
- package/workspace-scope.ts +6 -0
- package/worktree-feedback.ts +77 -0
package/log-cursor.ts
CHANGED
|
@@ -18,11 +18,29 @@
|
|
|
18
18
|
* `truncated` reports that bytes were never seen.
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
import { closeSync, openSync, readSync, statSync } from "node:fs";
|
|
21
|
+
import { closeSync, openSync, readSync, statSync, fstatSync } from "node:fs";
|
|
22
22
|
|
|
23
23
|
/** Bytes of the log head kept to detect a rewrite behind our back. */
|
|
24
24
|
const HEAD_SAMPLE_BYTES = 256;
|
|
25
25
|
|
|
26
|
+
export const logReadMetrics = { bytes: 0, reads: 0, parsedEvents: 0, displayFolded: 0, healthFolded: 0 };
|
|
27
|
+
let batch: { reads: Map<string, LogRead>; events: Map<string, unknown>; ranges: Map<string, Array<{ start: number; data: Buffer }>> } | undefined;
|
|
28
|
+
|
|
29
|
+
/** Share identical ranges and decoding only during a synchronous observation. */
|
|
30
|
+
export function withLogReadBatch<T>(observe: () => T): T {
|
|
31
|
+
const previous = batch;
|
|
32
|
+
batch = { reads: new Map(), events: new Map(), ranges: new Map() };
|
|
33
|
+
try { return observe(); } finally { batch = previous; }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function parseLogEvent(line: string): any {
|
|
37
|
+
if (batch?.events.has(line)) return batch.events.get(line);
|
|
38
|
+
const event = JSON.parse(line);
|
|
39
|
+
logReadMetrics.parsedEvents++;
|
|
40
|
+
batch?.events.set(line, event);
|
|
41
|
+
return event;
|
|
42
|
+
}
|
|
43
|
+
|
|
26
44
|
/** Default ceiling on a single read: also the cold-start tail window. */
|
|
27
45
|
export const DEFAULT_MAX_READ_BYTES = 32 * 1024 * 1024; // 32 MiB
|
|
28
46
|
|
|
@@ -31,6 +49,15 @@ export const DEFAULT_MAX_READ_BYTES = 32 * 1024 * 1024; // 32 MiB
|
|
|
31
49
|
* `readAppendedLines` and store the one it returns.
|
|
32
50
|
*/
|
|
33
51
|
export interface LogCursor {
|
|
52
|
+
/** First observed byte; a tail consumer never claims coverage before it. */
|
|
53
|
+
coverageStart?: number;
|
|
54
|
+
/** Bounded envelope of skipped byte ranges, exclusive end. */
|
|
55
|
+
missing?: [number, number];
|
|
56
|
+
/** An oversized record is being discarded up to its next newline. */
|
|
57
|
+
skipLeading?: boolean;
|
|
58
|
+
generation?: string;
|
|
59
|
+
size?: number;
|
|
60
|
+
modified?: number;
|
|
34
61
|
/** Byte offset of the first unread byte; always a line boundary. */
|
|
35
62
|
offset: number;
|
|
36
63
|
/** Head bytes (latin1, byte-exact) used to detect a rewritten log. */
|
|
@@ -38,6 +65,7 @@ export interface LogCursor {
|
|
|
38
65
|
}
|
|
39
66
|
|
|
40
67
|
export interface LogRead {
|
|
68
|
+
more?: boolean;
|
|
41
69
|
/** Complete lines appended since the previous cursor, in order. */
|
|
42
70
|
lines: string[];
|
|
43
71
|
/**
|
|
@@ -63,22 +91,38 @@ function errText(err: unknown): string {
|
|
|
63
91
|
return err instanceof Error ? err.message : String(err);
|
|
64
92
|
}
|
|
65
93
|
|
|
66
|
-
function readAt(fd: number, position: number, length: number): Buffer {
|
|
94
|
+
function readAt(fd: number, position: number, length: number, path: string): Buffer {
|
|
67
95
|
const buf = Buffer.allocUnsafe(length);
|
|
96
|
+
const ranges = batch?.ranges.get(path) ?? [];
|
|
68
97
|
let filled = 0;
|
|
69
98
|
while (filled < length) {
|
|
70
|
-
const
|
|
99
|
+
const at = position + filled;
|
|
100
|
+
const cached = ranges.find(range => range.start <= at && range.start + range.data.length > at);
|
|
101
|
+
if (cached) {
|
|
102
|
+
const offset = at - cached.start;
|
|
103
|
+
const n = Math.min(length - filled, cached.data.length - offset);
|
|
104
|
+
cached.data.copy(buf, filled, offset, offset + n);
|
|
105
|
+
filled += n;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const next = Math.min(position + length, ...ranges.filter(range => range.start > at).map(range => range.start));
|
|
109
|
+
const n = readSync(fd, buf, filled, next - at, at);
|
|
71
110
|
if (n <= 0) break;
|
|
111
|
+
if (batch) {
|
|
112
|
+
ranges.push({ start: at, data: buf.subarray(filled, filled + n) });
|
|
113
|
+
batch.ranges.set(path, ranges);
|
|
114
|
+
}
|
|
72
115
|
filled += n;
|
|
116
|
+
logReadMetrics.bytes += n;
|
|
73
117
|
}
|
|
74
118
|
return filled === length ? buf : buf.subarray(0, filled);
|
|
75
119
|
}
|
|
76
120
|
|
|
77
121
|
/** Head sample as a byte-exact string, for rewrite detection. */
|
|
78
|
-
function readHead(fd: number, size: number): string {
|
|
122
|
+
function readHead(fd: number, size: number, path: string): string {
|
|
79
123
|
const length = Math.min(HEAD_SAMPLE_BYTES, size);
|
|
80
124
|
if (length <= 0) return "";
|
|
81
|
-
return readAt(fd, 0, length).toString("latin1");
|
|
125
|
+
return readAt(fd, 0, length, path).toString("latin1");
|
|
82
126
|
}
|
|
83
127
|
|
|
84
128
|
/**
|
|
@@ -103,7 +147,18 @@ export function readAppendedLines(
|
|
|
103
147
|
path: string,
|
|
104
148
|
previous?: LogCursor,
|
|
105
149
|
maxBytes: number = DEFAULT_MAX_READ_BYTES,
|
|
150
|
+
cumulative = false,
|
|
106
151
|
): LogRead {
|
|
152
|
+
const key = JSON.stringify([path, previous, maxBytes, cumulative]);
|
|
153
|
+
const cached = batch?.reads.get(key);
|
|
154
|
+
if (cached) return cached;
|
|
155
|
+
const result = readLines(path, previous, maxBytes, cumulative);
|
|
156
|
+
batch?.reads.set(key, result);
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function readLines(path: string, previous: LogCursor | undefined, maxBytes: number, cumulative: boolean): LogRead {
|
|
161
|
+
logReadMetrics.reads++;
|
|
107
162
|
const empty = (error?: string): LogRead => ({
|
|
108
163
|
lines: [],
|
|
109
164
|
cursor: previous ?? { offset: 0, head: "" },
|
|
@@ -128,59 +183,80 @@ export function readAppendedLines(
|
|
|
128
183
|
}
|
|
129
184
|
|
|
130
185
|
try {
|
|
131
|
-
const
|
|
186
|
+
const info = fstatSync(fd);
|
|
187
|
+
size = info.size;
|
|
188
|
+
const generation = `${info.dev}:${info.ino}:${info.birthtimeMs}`;
|
|
189
|
+
const head = readHead(fd, size, path);
|
|
132
190
|
// A log that shrank or whose head changed is a different log: the old
|
|
133
191
|
// offset would point into unrelated bytes. Compare only the prefix the
|
|
134
192
|
// two samples share, so a log still shorter than the sample window is
|
|
135
193
|
// not "rewritten" every time it grows.
|
|
136
194
|
const resumable = previous !== undefined
|
|
195
|
+
&& (previous.generation === undefined || previous.generation === generation)
|
|
196
|
+
&& !(previous.size === size && previous.modified !== undefined && previous.modified !== info.mtimeMs)
|
|
137
197
|
&& previous.offset <= size
|
|
138
198
|
&& headsAgree(previous.head, head);
|
|
139
199
|
const restarted = previous !== undefined && !resumable;
|
|
140
200
|
|
|
201
|
+
let coverageStart = resumable ? previous!.coverageStart ?? 0 : 0;
|
|
202
|
+
let missing = resumable ? previous!.missing : undefined;
|
|
203
|
+
let skipLeading = resumable ? previous!.skipLeading : false;
|
|
204
|
+
const cursor = (offset: number): LogCursor => ({ offset, head, generation, size, modified: info.mtimeMs, coverageStart, missing, skipLeading });
|
|
141
205
|
let start = resumable ? previous!.offset : 0;
|
|
142
206
|
let truncated = false;
|
|
143
|
-
if (size - start > maxBytes) {
|
|
207
|
+
if (!cumulative && size - start > maxBytes) {
|
|
144
208
|
// Skip ahead instead of lagging: a chatty log must not push the
|
|
145
209
|
// cursor permanently into the past.
|
|
146
210
|
start = size - maxBytes;
|
|
211
|
+
coverageStart = start;
|
|
212
|
+
missing = [missing?.[0] ?? (resumable ? previous!.offset : 0), start];
|
|
147
213
|
truncated = true;
|
|
148
214
|
}
|
|
149
215
|
// We begin mid-line whenever bytes were skipped.
|
|
150
|
-
const startedMidLine = truncated;
|
|
216
|
+
const startedMidLine = truncated || skipLeading;
|
|
151
217
|
|
|
152
218
|
if (size === start) {
|
|
153
|
-
return { lines: [], cursor:
|
|
219
|
+
return { lines: [], cursor: cursor(start), totalBytes: size, truncated, restarted };
|
|
154
220
|
}
|
|
155
221
|
|
|
156
|
-
const
|
|
222
|
+
const end = cumulative ? Math.min(size, start + maxBytes) : size;
|
|
223
|
+
const buf = readAt(fd, start, end - start, path);
|
|
157
224
|
const lastNewline = buf.lastIndexOf(0x0a);
|
|
158
225
|
if (lastNewline === -1) {
|
|
159
226
|
// No complete line yet. Hold position so the line can finish —
|
|
160
227
|
// unless it is an oversized partial we already skipped into, which
|
|
161
228
|
// would otherwise be re-read forever.
|
|
162
|
-
const
|
|
229
|
+
const oversized = cumulative && end < size;
|
|
230
|
+
if (oversized) missing = [missing?.[0] ?? start, end];
|
|
231
|
+
if (oversized) skipLeading = true;
|
|
232
|
+
const offset = startedMidLine || oversized ? start + buf.length : start;
|
|
163
233
|
const partial = startedMidLine ? undefined : buf.toString("utf-8");
|
|
164
|
-
return { lines: [], partial, cursor:
|
|
234
|
+
return { lines: [], partial: oversized ? undefined : partial, cursor: cursor(offset), totalBytes: size, truncated: truncated || oversized, restarted, more: end < size };
|
|
165
235
|
}
|
|
166
236
|
|
|
167
237
|
const text = buf.subarray(0, lastNewline + 1).toString("utf-8");
|
|
168
238
|
const lines = text.split("\n");
|
|
169
239
|
lines.pop(); // trailing "" after the final newline
|
|
170
|
-
if (startedMidLine)
|
|
240
|
+
if (startedMidLine) {
|
|
241
|
+
lines.shift(); // partial first line
|
|
242
|
+
missing = [missing?.[0] ?? 0, start + buf.indexOf(0x0a) + 1];
|
|
243
|
+
coverageStart = Math.max(coverageStart, missing[1]);
|
|
244
|
+
skipLeading = false;
|
|
245
|
+
}
|
|
171
246
|
|
|
172
247
|
// Anything after the final newline is unterminated: hand it over
|
|
173
248
|
// separately rather than pretending it is a line.
|
|
174
249
|
const trailing = buf.subarray(lastNewline + 1);
|
|
175
|
-
const partial = trailing.length > 0 ? trailing.toString("utf-8") : undefined;
|
|
250
|
+
const partial = end === size && trailing.length > 0 ? trailing.toString("utf-8") : undefined;
|
|
176
251
|
|
|
177
252
|
return {
|
|
178
253
|
lines,
|
|
179
254
|
partial,
|
|
180
|
-
cursor:
|
|
255
|
+
cursor: cursor(start + lastNewline + 1),
|
|
181
256
|
totalBytes: size,
|
|
182
257
|
truncated,
|
|
183
258
|
restarted,
|
|
259
|
+
more: end < size,
|
|
184
260
|
};
|
|
185
261
|
} catch (err) {
|
|
186
262
|
return empty(errText(err));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
|
-
import { processStartToken } from "./
|
|
4
|
-
import { launchDigest } from "./
|
|
3
|
+
import { processStartToken } from "./process-identity.mjs";
|
|
4
|
+
import { launchDigest } from "./launch-digest.mjs";
|
|
5
5
|
|
|
6
6
|
/** Runs before the Pi CLI or any worker extension can execute task code. */
|
|
7
7
|
export function verifyManagedBootstrap(env = process.env) {
|
package/managed-launch-client.ts
CHANGED
|
@@ -6,30 +6,42 @@ import { getAgentDir, type ExtensionAPI, type ExtensionContext } from "@earendil
|
|
|
6
6
|
import { currentSandboxPermissions } from "./permission-policy.ts";
|
|
7
7
|
import { ownerBindingFromContext, getWorkerOperationService, resolveProductionSchemas, DEFAULT_WORKER_BRIDGE_TOOLS } from "./worker-waits.ts";
|
|
8
8
|
import { processStartToken } from "./registry.ts";
|
|
9
|
-
import {
|
|
9
|
+
import { connectWorkClient } from "./shared-work-client.mjs";
|
|
10
10
|
import { catalogHostAuthority } from "./catalog-host-admission.ts";
|
|
11
|
-
import { openSharedCatalog } from "./shared-catalog.ts";
|
|
11
|
+
import { openSharedCatalog, waitForCatalogSetup } from "./shared-catalog.ts";
|
|
12
12
|
import { SANDBOX_POLICY_CHANNEL } from "./permission-policy.ts";
|
|
13
13
|
import { canonicalJson } from "./operation-bridge-types.ts";
|
|
14
|
+
import { activeChildProviders } from "./config.ts";
|
|
15
|
+
import { captureLaunchContext } from "./launch-context.ts";
|
|
14
16
|
|
|
15
17
|
/** The fixed Pi host issues execution grants. Catalog registration grants no execution. */
|
|
16
|
-
export async function submitManagedLaunch(pi: ExtensionAPI, ctx: ExtensionContext, key: string, tool: string, params: unknown) {
|
|
18
|
+
export async function submitManagedLaunch(pi: ExtensionAPI, ctx: ExtensionContext, key: string, tool: string, params: unknown, signal?: AbortSignal) {
|
|
19
|
+
signal?.throwIfAborted();
|
|
20
|
+
ctx = captureLaunchContext(ctx);
|
|
17
21
|
mkdirSync(getAgentDir(), { recursive: true });
|
|
18
22
|
const root = realpathSync(getAgentDir());
|
|
19
|
-
const catalog = await openSharedCatalog(ctx.cwd, root);
|
|
23
|
+
const catalog = await openSharedCatalog(ctx.cwd, root, undefined, undefined, signal);
|
|
20
24
|
catalog.close();
|
|
25
|
+
signal?.throwIfAborted();
|
|
21
26
|
const registration = JSON.parse(readFileSync(join(root, "server-registration-v1.json"), "utf8"));
|
|
22
27
|
if (registration.agentDir !== root || registration.version !== 1) throw new Error("Invalid runtime registration");
|
|
23
|
-
const client = new WorkClient(join(registration.serverDir, "pi-server.sock"), root, ["runtime-subagent-launch-v1"]);
|
|
24
28
|
const directory = join(root, "subagent-launch-admission");
|
|
25
29
|
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
26
30
|
const stat = lstatSync(directory);
|
|
27
31
|
if (!stat.isDirectory() || stat.isSymbolicLink() || stat.uid !== process.getuid?.() || (stat.mode & 0o077)) throw new Error("Unsafe launch admission directory");
|
|
32
|
+
const client = await connectWorkClient(join(registration.serverDir, "pi-server.sock"), root, ["runtime-subagent-launch-v1"], { signal });
|
|
28
33
|
const id = randomUUID(), secret = randomBytes(32).toString("hex"), path = join(directory, `${id}.json`);
|
|
29
34
|
let unsubscribe: unknown;
|
|
35
|
+
const cancel = () => {
|
|
36
|
+
try { rmSync(path, { force: true }); } catch { /* Finally retries cleanup. */ }
|
|
37
|
+
client.close();
|
|
38
|
+
};
|
|
39
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
30
40
|
try {
|
|
41
|
+
signal?.throwIfAborted();
|
|
31
42
|
const hello = await client.ready;
|
|
32
|
-
const catalogAuthority = await catalogHostAuthority(root, ctx.cwd, join(registration.serverDir, "pi-server.sock"));
|
|
43
|
+
const catalogAuthority = await waitForCatalogSetup(catalogHostAuthority(root, ctx.cwd, join(registration.serverDir, "pi-server.sock")), signal);
|
|
44
|
+
signal?.throwIfAborted();
|
|
33
45
|
const owner = ownerBindingFromContext(ctx);
|
|
34
46
|
if (!owner.sessionId) throw new Error("Launch requires a registered parent session");
|
|
35
47
|
// Only branch-local settings travel to the mechanical host, never the transcript.
|
|
@@ -37,7 +49,7 @@ export async function submitManagedLaunch(pi: ExtensionAPI, ctx: ExtensionContex
|
|
|
37
49
|
const payload = { version: 1, id, secretHash: createHash("sha256").update(secret).digest("hex"), instanceId: hello.instanceId,
|
|
38
50
|
owner, pid: process.pid, start: processStartToken(process.pid), projectTrusted: ctx.isProjectTrusted(),
|
|
39
51
|
background: { available: getWorkerOperationService(pi)?.assignmentContinuation === 1 || nativeBackgroundCapability(pi), tools: [...resolveProductionSchemas(pi, DEFAULT_WORKER_BRIDGE_TOOLS).schemas.keys()] },
|
|
40
|
-
permissions: currentSandboxPermissions(pi) ?? {}, settings, model: ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : undefined,
|
|
52
|
+
activeProviders: activeChildProviders(pi), permissions: currentSandboxPermissions(pi) ?? {}, settings, model: ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : undefined,
|
|
41
53
|
catalogAuthority, key, tool, params };
|
|
42
54
|
const staging = `${path}.tmp`;
|
|
43
55
|
writeFileSync(staging, JSON.stringify(payload), { flag: "wx", mode: 0o600 });
|
|
@@ -52,5 +64,13 @@ export async function submitManagedLaunch(pi: ExtensionAPI, ctx: ExtensionContex
|
|
|
52
64
|
finally { checking = false; }
|
|
53
65
|
});
|
|
54
66
|
return await client.request("runtime_launch_subagent", { authority: { id, secret } });
|
|
55
|
-
}
|
|
67
|
+
} catch (error) {
|
|
68
|
+
signal?.throwIfAborted();
|
|
69
|
+
throw error;
|
|
70
|
+
} finally {
|
|
71
|
+
signal?.removeEventListener("abort", cancel);
|
|
72
|
+
if (typeof unsubscribe === "function") unsubscribe();
|
|
73
|
+
client.close();
|
|
74
|
+
rmSync(path, { force: true });
|
|
75
|
+
}
|
|
56
76
|
}
|
package/managed-launch-host.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
-
import { lstatSync, readFileSync, realpathSync } from "node:fs";
|
|
2
|
+
import { lstatSync, mkdirSync, readFileSync, realpathSync, statSync, watch, type FSWatcher } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import { createAgentSession, DefaultResourceLoader, SessionManager, SettingsManager, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
@@ -9,7 +9,7 @@ import extension from "./index.ts";
|
|
|
9
9
|
import { bindRuntimeOutcomeStore } from "./assignment-outcomes.ts";
|
|
10
10
|
import { catalogSnapshotFromWire } from "./catalog-store.ts";
|
|
11
11
|
import { openLaunchJournal, launchDigest } from "./managed-launch-journal.mjs";
|
|
12
|
-
import { nextRunId, processStartToken, listMetas, writeMeta, listActiveMetasForParent, effectiveStatus, readMeta, readParkRecord, updateParkRecord } from "./registry.ts";
|
|
12
|
+
import { baseDir, nextRunId, processStartToken, listMetas, writeMeta, listActiveMetasForParent, effectiveStatus, readMeta, readParkRecord, updateParkRecord } from "./registry.ts";
|
|
13
13
|
import { recoverLaunchMetadata } from "./managed-launch-recovery.ts";
|
|
14
14
|
import { assertSubagentHandoff } from "./managed-launch-ownership.ts";
|
|
15
15
|
import { createOwnerCapacityGates } from "./capacity.mjs";
|
|
@@ -37,19 +37,127 @@ export class ManagedSubagentHost {
|
|
|
37
37
|
private closeJournal: () => void;
|
|
38
38
|
private epoch: number;
|
|
39
39
|
private pending = 0;
|
|
40
|
+
private requestSequence = 0;
|
|
41
|
+
private requestRecords = new Map<number, any>();
|
|
42
|
+
private foldRecord?: { kind: string; ids: readonly string[]; lastProgressAt: number; nextDeadline: number; recovery: string };
|
|
43
|
+
private acceptedRequestLive = false;
|
|
40
44
|
private continuationWindow = { started: 0, admitted: 0 };
|
|
41
45
|
private continuationFailures = new Map<string, { failures: number; retryAt: number; reason: string }>();
|
|
42
|
-
private workerHost?: { start(ctx?: unknown): void; close(): void; service: { continuationReady: boolean } };
|
|
46
|
+
private workerHost?: { start(ctx?: unknown): void; close(): void; advancingWaits?(): any[]; service: { continuationReady: boolean } };
|
|
43
47
|
private path: string;
|
|
48
|
+
private known = new Map<string, any>();
|
|
49
|
+
private healthClose?: () => void;
|
|
50
|
+
private healthChanged?: (id: string) => void;
|
|
51
|
+
private lifecycleMetrics = { passes: 0, entities: 0, deadlineLatenessMs: 0 };
|
|
52
|
+
private healthDue = new Map<string, number>();
|
|
53
|
+
private writerProofs = new Map<string, { verified: boolean; checkedAt: number; unknownSince: number }>();
|
|
54
|
+
lifecycleReasons() {
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
const writers = [...this.known.values()].filter(meta => ["running", "orphaned"].includes(meta.status)).flatMap(meta => {
|
|
57
|
+
const proof = this.writerProofs.get(meta.id);
|
|
58
|
+
if (!proof || !proof.verified && now >= proof.unknownSince + this.verificationMs) return [];
|
|
59
|
+
return [{
|
|
60
|
+
kind: proof.verified ? "worker" : "verification", id: meta.id, owner: meta.callbackOrigin, writer: { pid: meta.pid, start: meta.pidStartTime },
|
|
61
|
+
management: meta.management, lastProgressAt: meta.timing?.lastProgressAt ?? meta.startedAt,
|
|
62
|
+
nextDeadline: proof.verified ? this.healthDue.get(meta.id) ?? null : proof.unknownSince + this.verificationMs,
|
|
63
|
+
recovery: proof.verified ? "current-managed-writer-supervision" : "writer-unverified-no-takeover",
|
|
64
|
+
}]; });
|
|
65
|
+
return [...this.requestRecords.values(), ...(this.foldRecord ? [{ ...this.foldRecord }] : []),
|
|
66
|
+
...(this.acceptedRequestLive ? this.journal.lifecycleReasons(this.current.ownerKey, this.current.key) : []).map(reason => ({ ...reason,
|
|
67
|
+
management: { ...reason.management, instance: this.instance }, writerFence: 'current-managed-launch-epoch' })),
|
|
68
|
+
...writers, ...(this.workerHost?.advancingWaits?.() ?? [])];
|
|
69
|
+
}
|
|
70
|
+
private observeHealth(reconcile: (ids: readonly string[]) => void) {
|
|
71
|
+
this.healthClose?.();
|
|
72
|
+
let closed = false, scheduled = false;
|
|
73
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
74
|
+
const watchers: FSWatcher[] = [], dirty = new Set(this.known.keys()), signatures = new Map<string, string>();
|
|
75
|
+
const signature = (meta: any) => {
|
|
76
|
+
let log = "missing";
|
|
77
|
+
try { const info = statSync(meta.logPath, { bigint: true }); log = `${info.ino}:${info.size}:${info.mtimeNs}`; } catch { /* No log yet. */ }
|
|
78
|
+
return JSON.stringify(meta) + log;
|
|
79
|
+
};
|
|
80
|
+
const schedule = () => {
|
|
81
|
+
if (closed || scheduled) return;
|
|
82
|
+
scheduled = true;
|
|
83
|
+
setImmediate(() => { scheduled = false; if (!closed) pass(); });
|
|
84
|
+
};
|
|
85
|
+
const hint = (id: string) => {
|
|
86
|
+
const meta = readMeta(id);
|
|
87
|
+
if (meta?.management?.root === this.root && meta.management.instance === this.instance && meta.management.epoch === this.epoch) {
|
|
88
|
+
if (signatures.get(id) === signature(meta)) return;
|
|
89
|
+
if (!this.writerProofs.has(id)) this.writerProofs.set(id, { verified: false, checkedAt: Date.now(), unknownSince: Date.now() });
|
|
90
|
+
this.known.set(id, meta); dirty.add(id); schedule();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
this.healthChanged = hint;
|
|
94
|
+
const pass = () => {
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
for (const [id, due] of this.healthDue) if (due <= now) dirty.add(id);
|
|
97
|
+
const ids = [...dirty].slice(0, 32);
|
|
98
|
+
ids.forEach(id => dirty.delete(id));
|
|
99
|
+
if (ids.length) {
|
|
100
|
+
const release = this.enterControl?.();
|
|
101
|
+
if (this.enterControl && !release) {
|
|
102
|
+
ids.forEach(id => dirty.add(id));
|
|
103
|
+
timer = setTimeout(schedule, 1000); timer.unref(); return;
|
|
104
|
+
}
|
|
105
|
+
this.lifecycleMetrics.passes++; this.lifecycleMetrics.entities += ids.length;
|
|
106
|
+
for (const id of ids) this.lifecycleMetrics.deadlineLatenessMs = Math.max(this.lifecycleMetrics.deadlineLatenessMs, now - (this.healthDue.get(id) ?? now));
|
|
107
|
+
try { reconcile(ids); } finally { release?.(); }
|
|
108
|
+
for (const id of ids) {
|
|
109
|
+
const meta = readMeta(id);
|
|
110
|
+
if (meta) this.known.set(id, meta); else this.known.delete(id);
|
|
111
|
+
if (meta) signatures.set(id, signature(meta));
|
|
112
|
+
this.healthDue.delete(id);
|
|
113
|
+
if (!meta || !["running", "orphaned"].includes(meta.status)) {
|
|
114
|
+
this.writerProofs.delete(id);
|
|
115
|
+
if (meta?.completionCallbackPendingAt && !meta.completionCallbackSentAt && !meta.completionCallbackSuppressedAt) this.healthDue.set(id, now + 15000);
|
|
116
|
+
else { this.known.delete(id); signatures.delete(id); }
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const verified = Boolean(meta.pidStartTime && processStartToken(meta.pid) === meta.pidStartTime && processExists(meta.pid));
|
|
120
|
+
const prior = this.writerProofs.get(id);
|
|
121
|
+
this.writerProofs.set(id, { verified, checkedAt: now, unknownSince: verified ? now : prior?.unknownSince ?? now });
|
|
122
|
+
const timing = meta.timing;
|
|
123
|
+
const deadlines = [now + 15000, timing?.ceilingAt,
|
|
124
|
+
!timing?.deadlineWakeSentAt ? timing?.deadlineAt : undefined,
|
|
125
|
+
timing?.steerDeliveredAt ? timing.steerDeliveredAt + timing.graceMs : undefined]
|
|
126
|
+
.filter((value): value is number => typeof value === "number" && value > now);
|
|
127
|
+
this.healthDue.set(id, Math.min(...deadlines));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (dirty.size) schedule();
|
|
131
|
+
if (timer) clearTimeout(timer);
|
|
132
|
+
if (this.healthDue.size) {
|
|
133
|
+
const next = [...this.healthDue.values()].reduce((a, b) => Math.min(a, b), Infinity);
|
|
134
|
+
timer = setTimeout(schedule, Math.max(1, next - Date.now())); timer.unref();
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
try {
|
|
138
|
+
mkdirSync(join(baseDir(), "runs"), { recursive: true });
|
|
139
|
+
const watcher = watch(join(baseDir(), "runs"), { recursive: true }, (_event, file) => {
|
|
140
|
+
const id = file && String(file).split(/[/\\]/)[0];
|
|
141
|
+
if (id && /^[A-Za-z0-9_-]+$/.test(id)) hint(id);
|
|
142
|
+
});
|
|
143
|
+
watcher.on("error", () => watcher.close()); watchers.push(watcher);
|
|
144
|
+
} catch { /* Known active writers still have independent liveness deadlines. */ }
|
|
145
|
+
schedule();
|
|
146
|
+
return this.healthClose = () => {
|
|
147
|
+
closed = true; if (timer) clearTimeout(timer); watchers.forEach(watcher => watcher.close());
|
|
148
|
+
this.healthChanged = undefined; this.healthDue.clear();
|
|
149
|
+
};
|
|
150
|
+
}
|
|
44
151
|
private capacityFor = createOwnerCapacityGates(({ pid, start }: { pid: number; start: string }) =>
|
|
45
152
|
listActiveMetasForParent(pid).filter(meta => meta.spawnPidStartTime === start && effectiveStatus(meta) === "running").length
|
|
46
153
|
+ this.journal.unresolvedAttempts().filter((attempt: any) => attempt.capacityOwner?.pid === pid && attempt.capacityOwner?.start === start && !readMeta(attempt.id)).length);
|
|
47
154
|
get busy() { return this.pending > 0 || Boolean(this.failureResponse); }
|
|
48
155
|
status() {
|
|
49
156
|
return { owner: "server", instanceId: this.instance, runtimeEpoch: this.epoch,
|
|
157
|
+
lifecycle: { ...this.lifecycleMetrics, activeDeadlines: this.healthDue.size },
|
|
50
158
|
inFlight: this.pending, failureFold: { worker: Boolean(this.failureWorker), inFlight: Boolean(this.failureResponse) }, continuationReady: this.workerHost?.service.continuationReady ?? false, continuationUnavailable: [...this.continuationFailures].map(([key, value]) => ({ key, ...value })), unresolvedAttempts: this.journal.unresolvedAttempts().filter((attempt: any) => !readMeta(attempt.id)).length };
|
|
51
159
|
}
|
|
52
|
-
constructor(private root: string, private instance: string, private catalog: any, sharedStore?: unknown, private createWorkerHost?: (pi: ExtensionAPI) => NonNullable<ManagedSubagentHost["workerHost"]>, private readPermissions?: () => unknown) {
|
|
160
|
+
constructor(private root: string, private instance: string, private catalog: any, sharedStore?: unknown, private createWorkerHost?: (pi: ExtensionAPI) => NonNullable<ManagedSubagentHost["workerHost"]>, private readPermissions?: () => unknown, private verificationMs = 30000, private enterControl?: () => (() => void) | undefined) {
|
|
53
161
|
this.current = { pid: process.pid, start: processStartToken(process.pid), owner: { workspace: root, sessionId: "runtime-recovery" } };
|
|
54
162
|
this.path = join(root, "operation-coordinator.sqlite");
|
|
55
163
|
const active = listMetas({ includeArchived: false });
|
|
@@ -125,6 +233,10 @@ export class ManagedSubagentHost {
|
|
|
125
233
|
}
|
|
126
234
|
}
|
|
127
235
|
async recover() {
|
|
236
|
+
for (const meta of listMetas({ includeArchived: false })) if (meta.management?.root === this.root) {
|
|
237
|
+
this.known.set(meta.id, meta);
|
|
238
|
+
this.writerProofs.set(meta.id, { verified: false, checkedAt: Date.now(), unknownSince: Date.now() });
|
|
239
|
+
}
|
|
128
240
|
if (listMetas({ includeArchived: false }).some(meta => meta.management?.root === this.root)) await this.initialize();
|
|
129
241
|
}
|
|
130
242
|
private grant(authority: any) {
|
|
@@ -152,12 +264,14 @@ export class ManagedSubagentHost {
|
|
|
152
264
|
return this.initializing ??= (async () => {
|
|
153
265
|
const host = this;
|
|
154
266
|
const port: ManagedLaunchPort = {
|
|
267
|
+
observeHealth: reconcile => host.observeHealth(reconcile),
|
|
268
|
+
activeProviders: () => host.current.activeProviders ?? {},
|
|
155
269
|
backgroundTools: () => host.current.background?.available === true ? host.current.background.tools : [],
|
|
156
270
|
get owner() { return { pid: host.current.pid, start: host.current.start }; },
|
|
157
271
|
get identity() { return { root: host.root, instance: host.instance, epoch: host.epoch }; },
|
|
158
272
|
continuationContext: () => host.executionContext(host.current),
|
|
159
273
|
dispatchContinuation: (park, run) => host.dispatchContinuation(park, run),
|
|
160
|
-
supervised: () =>
|
|
274
|
+
supervised: () => [...host.known.values()].filter(meta => port.owns(meta)),
|
|
161
275
|
owns: meta => meta.management?.root === host.root && meta.management?.instance === host.instance && meta.management?.epoch === host.epoch,
|
|
162
276
|
assertCurrent: () => host.journal.current(host.epoch),
|
|
163
277
|
capacity: () => host.capacityFor(host.current),
|
|
@@ -167,7 +281,7 @@ export class ManagedSubagentHost {
|
|
|
167
281
|
const command = host.catalog.connection();
|
|
168
282
|
command({ type: "runtime_register_catalog", cwd: host.current.owner.workspace,
|
|
169
283
|
sessionId: host.current.owner.sessionId, authority: host.current.catalogAuthority });
|
|
170
|
-
return catalogSnapshotFromWire(command({ type: "runtime_get_catalog", overlay: configuration.getSessionSettings?.() ?? configuration.sessionSettings }));
|
|
284
|
+
return catalogSnapshotFromWire(command({ type: "runtime_get_catalog", fresh: true, overlay: configuration.getSessionSettings?.() ?? configuration.sessionSettings }));
|
|
171
285
|
},
|
|
172
286
|
beforeSpawn: input => {
|
|
173
287
|
assertSubagentHandoff(listMetas({ includeArchived: false }), host.root);
|
|
@@ -185,11 +299,11 @@ export class ManagedSubagentHost {
|
|
|
185
299
|
const policyDigest = input.policyPath ? launchDigest(JSON.parse(readFileSync(input.policyPath, "utf8"))) : undefined;
|
|
186
300
|
const attempt = host.journal.prepare(host.current.ownerKey, host.current.key, host.epoch, { ...input, preflightDigest, policyDigest,
|
|
187
301
|
capacityOwner: { pid: host.current.pid, start: host.current.start },
|
|
188
|
-
launchAuthority: host.current.launchAuthority ?? { owner: host.current.owner, pid: host.current.pid, start: host.current.start, settings: host.current.settings, model: host.current.model, projectTrusted: host.current.projectTrusted, background: host.current.background, permissions: host.current.permissions } });
|
|
302
|
+
launchAuthority: host.current.launchAuthority ?? { owner: host.current.owner, pid: host.current.pid, start: host.current.start, settings: host.current.settings, model: host.current.model, projectTrusted: host.current.projectTrusted, background: host.current.background, activeProviders: host.current.activeProviders, permissions: host.current.permissions } });
|
|
189
303
|
return { PI_MANAGED_SUBAGENT: JSON.stringify({ path: host.path, owner: host.current.ownerKey,
|
|
190
304
|
key: host.current.key, epoch: host.epoch, id: input.id, token: attempt.token }) };
|
|
191
305
|
},
|
|
192
|
-
spawned: (id, pid) => host.journal.spawned(host.current.ownerKey, host.current.key, host.epoch, id, pid, processStartToken(pid)),
|
|
306
|
+
spawned: (id, pid) => { host.journal.spawned(host.current.ownerKey, host.current.key, host.epoch, id, pid, processStartToken(pid)); setImmediate(() => host.healthChanged?.(id)); },
|
|
193
307
|
};
|
|
194
308
|
const cwd = this.root;
|
|
195
309
|
const settingsManager = SettingsManager.inMemory({});
|
|
@@ -235,6 +349,11 @@ export class ManagedSubagentHost {
|
|
|
235
349
|
if (failure && Date.now() < failure.retryAt) return Promise.resolve("skipped" as const);
|
|
236
350
|
this.continuationWindow.admitted++;
|
|
237
351
|
this.pending++;
|
|
352
|
+
const requestId = ++this.requestSequence;
|
|
353
|
+
this.requestRecords.set(requestId, { kind: 'continuation-request', id: requestId, waitId: park.waitId, runId: park.runId,
|
|
354
|
+
generation: park.generation, lastProgressAt: now, executionDeadline: null, nextDeadline: null,
|
|
355
|
+
recovery: 'captured-continuation-serialized-by-current-launch-journal-owner' });
|
|
356
|
+
let dispatched = false;
|
|
238
357
|
const result = this.tail.catch(() => undefined).then(async () => {
|
|
239
358
|
const saved = this.journal.attempt(park.runId)?.launchAuthority;
|
|
240
359
|
if (!saved) throw new Error("Captured continuation launch authority unavailable");
|
|
@@ -244,7 +363,9 @@ export class ManagedSubagentHost {
|
|
|
244
363
|
capturedPermissions: saved.permissions, permissions,
|
|
245
364
|
ownerKey: JSON.stringify([saved.owner.workspace, saved.owner.sessionId]),
|
|
246
365
|
digest: launchDigest({ key, runId: park.runId }) };
|
|
366
|
+
this.acceptedRequestLive = true;
|
|
247
367
|
this.api.events.emit("pi-better-sandbox:policy", { state: "enabled", ...permissions });
|
|
368
|
+
dispatched = true;
|
|
248
369
|
const outcome = await run();
|
|
249
370
|
if (outcome === "resumed") this.journal.finish(this.current.ownerKey, this.current.key, this.epoch, { status: outcome });
|
|
250
371
|
this.continuationFailures.delete(retryKey);
|
|
@@ -253,8 +374,11 @@ export class ManagedSubagentHost {
|
|
|
253
374
|
const failures = Math.min(6, (this.continuationFailures.get(retryKey)?.failures ?? 0) + 1);
|
|
254
375
|
if (this.continuationFailures.size >= 128 && !this.continuationFailures.has(retryKey)) this.continuationFailures.delete(this.continuationFailures.keys().next().value!);
|
|
255
376
|
this.continuationFailures.set(retryKey, { failures, retryAt: Date.now() + Math.min(30000, 1000 * 2 ** (failures - 1)), reason: String(error).slice(0, 500) });
|
|
377
|
+
// Let orchestration persist its existing pre-spawn hold. A skipped
|
|
378
|
+
// result would leave this exact generation ready despite refusal.
|
|
379
|
+
if (!dispatched) throw error;
|
|
256
380
|
return "skipped" as const;
|
|
257
|
-
}).finally(() => { this.pending--; });
|
|
381
|
+
}).finally(() => { this.requestRecords.delete(requestId); this.acceptedRequestLive = false; this.pending--; });
|
|
258
382
|
this.tail = result;
|
|
259
383
|
return result;
|
|
260
384
|
}
|
|
@@ -266,21 +390,29 @@ export class ManagedSubagentHost {
|
|
|
266
390
|
runId: meta.id, sessionId: meta.sessionId, status: meta.status,
|
|
267
391
|
})) }, content: [{ type: "text", text: `Original launch ${status}; no replacement attempt was dispatched.` }] };
|
|
268
392
|
}
|
|
269
|
-
handle(command: Record<string, unknown
|
|
393
|
+
handle(command: Record<string, unknown>, onAccepted?: (id: string) => void) {
|
|
270
394
|
if (Object.keys(command).some(key => !["id", "type", "authority"].includes(key))) return Promise.reject(new Error("unsupported launch field"));
|
|
271
395
|
this.pending++;
|
|
396
|
+
const requestId = ++this.requestSequence;
|
|
397
|
+
this.requestRecords.set(requestId, { kind: 'launch-request', id: requestId, phase: 'queued-unaccepted',
|
|
398
|
+
lastProgressAt: Date.now(), executionDeadline: null, nextDeadline: null,
|
|
399
|
+
recovery: 'validate-captured-native-authority-before-admission-no-detached-execution-lease' });
|
|
272
400
|
const result = this.tail.catch(() => undefined).then(async () => {
|
|
273
401
|
const record = this.grant(command.authority);
|
|
274
402
|
// A retried tool call keeps its accepted recipient even after Goal/branch edits.
|
|
275
403
|
const ownerKey = JSON.stringify([record.owner.workspace, record.owner.sessionId]);
|
|
276
404
|
const digest = launchDigest({ tool: record.tool, params: record.params });
|
|
277
405
|
const intent = this.journal.accept(ownerKey, record.key, digest, this.epoch);
|
|
406
|
+
onAccepted?.(`launch-progress:${launchDigest([ownerKey, record.key])}`);
|
|
407
|
+
this.requestRecords.set(requestId, { ...this.requestRecords.get(requestId), phase: 'accepted',
|
|
408
|
+
operation: `launch-progress:${launchDigest([ownerKey, record.key])}`, lastProgressAt: Date.now() });
|
|
278
409
|
if (intent.prior) {
|
|
279
410
|
if (intent.result) return JSON.parse(intent.result);
|
|
280
411
|
return this.recoveredResult(intent);
|
|
281
412
|
}
|
|
282
413
|
try {
|
|
283
414
|
this.current = { ...record, authority: command.authority, ownerKey, grantDigest: launchDigest(record) };
|
|
415
|
+
this.acceptedRequestLive = true;
|
|
284
416
|
await this.initialize();
|
|
285
417
|
this.api.events.emit("pi-better-sandbox:policy", { state: "enabled", ...record.permissions });
|
|
286
418
|
const context = this.executionContext(record);
|
|
@@ -296,13 +428,17 @@ export class ManagedSubagentHost {
|
|
|
296
428
|
}
|
|
297
429
|
throw error;
|
|
298
430
|
}
|
|
299
|
-
}).finally(() => { this.pending--; });
|
|
431
|
+
}).finally(() => { this.requestRecords.delete(requestId); this.acceptedRequestLive = false; this.pending--; });
|
|
300
432
|
this.tail = result;
|
|
301
433
|
return result;
|
|
302
434
|
}
|
|
303
435
|
/** One root mechanical worker; no coordinator connection or query mutations. */
|
|
304
436
|
foldTerminalFailures(ids: readonly string[]): Promise<void> {
|
|
437
|
+
let release: (() => void) | undefined;
|
|
438
|
+
let physicalExit: Promise<number> | undefined;
|
|
305
439
|
const result = this.tail.catch(() => undefined).then(async () => {
|
|
440
|
+
release = this.enterControl?.();
|
|
441
|
+
if (this.enterControl && !release) throw new Error("runtime-overloaded: failure fold source capacity");
|
|
306
442
|
this.journal.current(this.epoch);
|
|
307
443
|
if (Atomics.load(this.foldRevocation, 0)) throw new Error("failure-fold-owner-revoked");
|
|
308
444
|
if (this.failureWorkerError) throw this.failureWorkerError;
|
|
@@ -323,15 +459,32 @@ export class ManagedSubagentHost {
|
|
|
323
459
|
worker.on("exit", code => fail(new Error(`failure-fold-worker-exited: ${code}`)));
|
|
324
460
|
worker.unref();
|
|
325
461
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
462
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
463
|
+
try {
|
|
464
|
+
await new Promise<void>((resolve, reject) => {
|
|
465
|
+
this.failureResponse = { resolve, reject };
|
|
466
|
+
this.foldRecord = { kind: 'failure-fold', ids: [...ids], lastProgressAt: Date.now(), nextDeadline: Date.now() + this.verificationMs,
|
|
467
|
+
recovery: 'revoke-unacknowledged-fold-generation-at-existing-verification-deadline' };
|
|
468
|
+
timer = setTimeout(() => {
|
|
469
|
+
const error = new Error("failure-fold-verification-deadline: source worker did not acknowledge");
|
|
470
|
+
Atomics.store(this.foldRevocation, 0, 1);
|
|
471
|
+
this.failureWorkerError = error;
|
|
472
|
+
this.failureResponse = undefined;
|
|
473
|
+
physicalExit = this.failureWorker?.terminate();
|
|
474
|
+
reject(error);
|
|
475
|
+
}, this.verificationMs);
|
|
476
|
+
this.failureWorker!.postMessage({ ids });
|
|
477
|
+
});
|
|
478
|
+
} finally { this.foldRecord = undefined; if (timer) clearTimeout(timer); }
|
|
479
|
+
}).finally(() => {
|
|
480
|
+
if (physicalExit) void physicalExit.then(() => release?.(), () => undefined);
|
|
481
|
+
else release?.();
|
|
330
482
|
});
|
|
331
483
|
this.tail = result;
|
|
332
484
|
return result;
|
|
333
485
|
}
|
|
334
486
|
async close() {
|
|
487
|
+
this.healthClose?.();
|
|
335
488
|
Atomics.store(this.foldRevocation, 0, 1);
|
|
336
489
|
await this.tail.catch(() => undefined);
|
|
337
490
|
await this.failureWorker?.terminate(); this.failureWorker = undefined;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
1
|
import { openStore } from "./shared-operation-coordinator/index.ts";
|
|
3
|
-
|
|
4
|
-
: Array.isArray(value) ? value.map(ordered)
|
|
5
|
-
: Object.fromEntries(Object.keys(value).sort().map(key => [key, ordered(value[key])]));
|
|
6
|
-
export const launchDigest = value => createHash("sha256").update(JSON.stringify(ordered(value))).digest("hex");
|
|
2
|
+
export { launchDigest } from "./launch-digest.mjs";
|
|
7
3
|
|
|
8
4
|
/** Standalone callers own a store; server callers pass their shared mutation lease. */
|
|
9
5
|
export function openLaunchJournal(path, sharedStore) {
|
package/managed-launch-port.ts
CHANGED
|
@@ -4,6 +4,8 @@ import type { RunMeta } from "./registry.ts";
|
|
|
4
4
|
|
|
5
5
|
/** Fixed server adapter port; never deserialized from a socket request. */
|
|
6
6
|
export interface ManagedLaunchPort {
|
|
7
|
+
observeHealth?(reconcile: (ids: readonly string[]) => void): () => void;
|
|
8
|
+
activeProviders?(): Record<string, string>;
|
|
7
9
|
backgroundTools(): readonly string[];
|
|
8
10
|
readonly owner: { pid: number; start?: string };
|
|
9
11
|
readonly identity: { root: string; instance: string; epoch: number };
|
|
@@ -18,4 +20,6 @@ export interface ManagedLaunchPort {
|
|
|
18
20
|
workerCommand(args: string[]): { file: string; fileArgs: string[] };
|
|
19
21
|
beforeSpawn(input: { id: string; sessionId: string; lease: string; leasePath: string; preflightPath: string; policyPath?: string; permissions: unknown; recoveryMeta: RunMeta; recipe: NonNullable<RunMeta["recipe"]> }): Record<string, string>;
|
|
20
22
|
spawned(id: string, pid: number): void;
|
|
23
|
+
/** Private fixture/owner observation, never a serialized execution capability. */
|
|
24
|
+
observeExit?(id: string, completion: Promise<void>): void;
|
|
21
25
|
}
|