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
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Native frontend advertises a server route, never a fake local mechanical service. */
|
|
2
|
+
export function nativeBackgroundCapability(pi: {events?: {emit(channel:string,value:unknown):void}}):boolean {
|
|
3
|
+
let available=false;
|
|
4
|
+
pi.events?.emit('pi-better-background-tasks:managed-capability',{accept(value:unknown){
|
|
5
|
+
const capability=value as {version?:number;assignmentContinuation?:number}|undefined;
|
|
6
|
+
available=capability?.version===1&&capability.assignmentContinuation===1;
|
|
7
|
+
}});
|
|
8
|
+
return available;
|
|
9
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Root-owned mechanical file folding. No SQLite connection or native transcript. */
|
|
2
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
3
|
+
import { collectRunFailures } from "./failures.ts";
|
|
4
|
+
import { readMeta } from "./registry.ts";
|
|
5
|
+
const revoked = new Int32Array(workerData.revocation);
|
|
6
|
+
const assertCurrent = () => {
|
|
7
|
+
if (!Number.isSafeInteger(workerData.epoch) || Atomics.load(revoked, 0)) throw new Error("failure-fold-owner-revoked");
|
|
8
|
+
};
|
|
9
|
+
parentPort.on("message", ({ ids }) => {
|
|
10
|
+
try {
|
|
11
|
+
assertCurrent();
|
|
12
|
+
if (!Array.isArray(ids) || ids.length > 128) throw new Error("failure-fold-batch-overflow");
|
|
13
|
+
for (const id of new Set(ids)) {
|
|
14
|
+
assertCurrent();
|
|
15
|
+
if (typeof id !== "string" || !/^[A-Za-z0-9_-]+$/.test(id)) continue;
|
|
16
|
+
const meta = readMeta(id);
|
|
17
|
+
if (!meta || meta.management?.root !== workerData.root || meta.status === "running") continue;
|
|
18
|
+
const assertRunCurrent = () => {
|
|
19
|
+
assertCurrent();
|
|
20
|
+
const latest = readMeta(id);
|
|
21
|
+
if (!latest || latest.management?.root !== workerData.root || latest.status === "running") throw new Error("failure-fold-run-authority-changed");
|
|
22
|
+
};
|
|
23
|
+
collectRunFailures(id, meta.cwd, meta.status !== "orphaned", assertRunCurrent);
|
|
24
|
+
}
|
|
25
|
+
parentPort.postMessage({ complete: true });
|
|
26
|
+
} catch (error) { parentPort.postMessage({ error: String(error) }); }
|
|
27
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { processStartToken } from "./registry.ts";
|
|
4
|
+
import { launchDigest } from "./managed-launch-journal.mjs";
|
|
5
|
+
|
|
6
|
+
/** Runs before the Pi CLI or any worker extension can execute task code. */
|
|
7
|
+
export function verifyManagedBootstrap(env = process.env) {
|
|
8
|
+
if (!env.PI_MANAGED_SUBAGENT) return;
|
|
9
|
+
const proof = JSON.parse(env.PI_MANAGED_SUBAGENT);
|
|
10
|
+
const { DatabaseSync } = createRequire(import.meta.url)("node:sqlite");
|
|
11
|
+
const db = new DatabaseSync(proof.path);
|
|
12
|
+
db.exec("PRAGMA busy_timeout=5000");
|
|
13
|
+
db.exec("BEGIN IMMEDIATE");
|
|
14
|
+
try {
|
|
15
|
+
const epoch = db.prepare("SELECT epoch FROM subagent_management WHERE singleton=1").get()?.epoch;
|
|
16
|
+
const row = db.prepare("SELECT * FROM subagent_launch_intents WHERE owner=? AND request_key=?").get(proof.owner, proof.key);
|
|
17
|
+
const attempt = row && JSON.parse(row.attempts).find(value => value.id === proof.id);
|
|
18
|
+
if (epoch !== proof.epoch || row?.epoch !== epoch || !attempt || attempt.token !== proof.token
|
|
19
|
+
|| !["prepared", "spawned"].includes(attempt.state)) throw new Error("Managed launch epoch/token is no longer admitted");
|
|
20
|
+
const lease = JSON.parse(readFileSync(attempt.leasePath, "utf8"));
|
|
21
|
+
if (lease.token !== attempt.lease || lease.sessionId !== attempt.sessionId || lease.attemptId !== attempt.id) throw new Error("Managed launch writer lease changed");
|
|
22
|
+
if (launchDigest(JSON.parse(readFileSync(attempt.preflightPath, "utf8"))) !== attempt.preflightDigest) throw new Error("Managed launch preflight changed");
|
|
23
|
+
if (attempt.policyPath && launchDigest(JSON.parse(readFileSync(attempt.policyPath, "utf8"))) !== attempt.policyDigest) throw new Error("Managed launch captured permissions changed");
|
|
24
|
+
if (attempt.bootstrapped || (attempt.pid && attempt.pid !== process.pid)) throw new Error("Managed launch token already has a writer");
|
|
25
|
+
attempt.bootstrapped = true;
|
|
26
|
+
attempt.writerPid = process.pid;
|
|
27
|
+
attempt.writerStartTime = processStartToken(process.pid);
|
|
28
|
+
if (!attempt.writerStartTime) throw new Error("Managed launch cannot prove writer process identity");
|
|
29
|
+
const attempts = JSON.parse(row.attempts).map(value => value.id === attempt.id ? attempt : value);
|
|
30
|
+
db.prepare("UPDATE subagent_launch_intents SET attempts=? WHERE owner=? AND request_key=?").run(JSON.stringify(attempts), proof.owner, proof.key);
|
|
31
|
+
db.exec("COMMIT");
|
|
32
|
+
} catch (error) { db.exec("ROLLBACK"); throw error; }
|
|
33
|
+
finally { db.close(); }
|
|
34
|
+
}
|
|
35
|
+
verifyManagedBootstrap();
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { nativeBackgroundCapability } from './managed-background-capability.ts';
|
|
2
|
+
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
3
|
+
import { lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { getAgentDir, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { currentSandboxPermissions } from "./permission-policy.ts";
|
|
7
|
+
import { ownerBindingFromContext, getWorkerOperationService, resolveProductionSchemas, DEFAULT_WORKER_BRIDGE_TOOLS } from "./worker-waits.ts";
|
|
8
|
+
import { processStartToken } from "./registry.ts";
|
|
9
|
+
import { WorkClient } from "./shared-work-client.mjs";
|
|
10
|
+
import { catalogHostAuthority } from "./catalog-host-admission.ts";
|
|
11
|
+
import { openSharedCatalog } from "./shared-catalog.ts";
|
|
12
|
+
import { SANDBOX_POLICY_CHANNEL } from "./permission-policy.ts";
|
|
13
|
+
import { canonicalJson } from "./operation-bridge-types.ts";
|
|
14
|
+
|
|
15
|
+
/** 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) {
|
|
17
|
+
mkdirSync(getAgentDir(), { recursive: true });
|
|
18
|
+
const root = realpathSync(getAgentDir());
|
|
19
|
+
const catalog = await openSharedCatalog(ctx.cwd, root);
|
|
20
|
+
catalog.close();
|
|
21
|
+
const registration = JSON.parse(readFileSync(join(root, "server-registration-v1.json"), "utf8"));
|
|
22
|
+
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
|
+
const directory = join(root, "subagent-launch-admission");
|
|
25
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
26
|
+
const stat = lstatSync(directory);
|
|
27
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || stat.uid !== process.getuid?.() || (stat.mode & 0o077)) throw new Error("Unsafe launch admission directory");
|
|
28
|
+
const id = randomUUID(), secret = randomBytes(32).toString("hex"), path = join(directory, `${id}.json`);
|
|
29
|
+
let unsubscribe: unknown;
|
|
30
|
+
try {
|
|
31
|
+
const hello = await client.ready;
|
|
32
|
+
const catalogAuthority = await catalogHostAuthority(root, ctx.cwd, join(registration.serverDir, "pi-server.sock"));
|
|
33
|
+
const owner = ownerBindingFromContext(ctx);
|
|
34
|
+
if (!owner.sessionId) throw new Error("Launch requires a registered parent session");
|
|
35
|
+
// Only branch-local settings travel to the mechanical host, never the transcript.
|
|
36
|
+
const settings = ctx.sessionManager.getBranch().filter(entry => entry.type === "custom" && entry.customType.startsWith("pi-better-subagents-"));
|
|
37
|
+
const payload = { version: 1, id, secretHash: createHash("sha256").update(secret).digest("hex"), instanceId: hello.instanceId,
|
|
38
|
+
owner, pid: process.pid, start: processStartToken(process.pid), projectTrusted: ctx.isProjectTrusted(),
|
|
39
|
+
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,
|
|
41
|
+
catalogAuthority, key, tool, params };
|
|
42
|
+
const staging = `${path}.tmp`;
|
|
43
|
+
writeFileSync(staging, JSON.stringify(payload), { flag: "wx", mode: 0o600 });
|
|
44
|
+
renameSync(staging, path);
|
|
45
|
+
let checking = false;
|
|
46
|
+
unsubscribe = pi.events.on(SANDBOX_POLICY_CHANNEL, () => {
|
|
47
|
+
if (checking) return;
|
|
48
|
+
checking = true;
|
|
49
|
+
try {
|
|
50
|
+
if (canonicalJson(currentSandboxPermissions(pi) ?? {}) !== canonicalJson(payload.permissions)) rmSync(path, { force: true });
|
|
51
|
+
} catch { rmSync(path, { force: true }); }
|
|
52
|
+
finally { checking = false; }
|
|
53
|
+
});
|
|
54
|
+
return await client.request("runtime_launch_subagent", { authority: { id, secret } });
|
|
55
|
+
} finally { if (typeof unsubscribe === "function") unsubscribe(); client.close(); rmSync(path, { force: true }); }
|
|
56
|
+
}
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { lstatSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { createAgentSession, DefaultResourceLoader, SessionManager, SettingsManager, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { verifyParkCheckpoint } from "./park-checkpoint.ts";
|
|
7
|
+
import { intersectContinuationPermissions } from "./continuation-authority.ts";
|
|
8
|
+
import extension from "./index.ts";
|
|
9
|
+
import { bindRuntimeOutcomeStore } from "./assignment-outcomes.ts";
|
|
10
|
+
import { catalogSnapshotFromWire } from "./catalog-store.ts";
|
|
11
|
+
import { openLaunchJournal, launchDigest } from "./managed-launch-journal.mjs";
|
|
12
|
+
import { nextRunId, processStartToken, listMetas, writeMeta, listActiveMetasForParent, effectiveStatus, readMeta, readParkRecord, updateParkRecord } from "./registry.ts";
|
|
13
|
+
import { recoverLaunchMetadata } from "./managed-launch-recovery.ts";
|
|
14
|
+
import { assertSubagentHandoff } from "./managed-launch-ownership.ts";
|
|
15
|
+
import { createOwnerCapacityGates } from "./capacity.mjs";
|
|
16
|
+
import { readConversationLease, releaseUnspawnedConversationLease } from "./continuation.ts";
|
|
17
|
+
import { Worker } from "node:worker_threads";
|
|
18
|
+
import { processExists } from "./spawn.ts";
|
|
19
|
+
import type { ManagedLaunchPort } from "./managed-launch-port.ts";
|
|
20
|
+
|
|
21
|
+
// Revocation only: the journal remains the authority. A new same-process
|
|
22
|
+
// manager revokes a previous worker before advancing the journal epoch.
|
|
23
|
+
const failureFoldRevocations = new Map<string, Int32Array>();
|
|
24
|
+
|
|
25
|
+
/** One fixed SDK adapter per runtime, not a client-selected module or transcript writer. */
|
|
26
|
+
export class ManagedSubagentHost {
|
|
27
|
+
private foldRevocation = new Int32Array(new SharedArrayBuffer(4));
|
|
28
|
+
private failureWorker?: Worker;
|
|
29
|
+
private failureWorkerError?: Error;
|
|
30
|
+
private failureResponse?: { resolve(): void; reject(error: Error): void };
|
|
31
|
+
private current: any;
|
|
32
|
+
private api!: ExtensionAPI;
|
|
33
|
+
private session: any;
|
|
34
|
+
private initializing?: Promise<void>;
|
|
35
|
+
private tail: Promise<unknown> = Promise.resolve();
|
|
36
|
+
private journal: ReturnType<typeof openLaunchJournal>["journal"];
|
|
37
|
+
private closeJournal: () => void;
|
|
38
|
+
private epoch: number;
|
|
39
|
+
private pending = 0;
|
|
40
|
+
private continuationWindow = { started: 0, admitted: 0 };
|
|
41
|
+
private continuationFailures = new Map<string, { failures: number; retryAt: number; reason: string }>();
|
|
42
|
+
private workerHost?: { start(ctx?: unknown): void; close(): void; service: { continuationReady: boolean } };
|
|
43
|
+
private path: string;
|
|
44
|
+
private capacityFor = createOwnerCapacityGates(({ pid, start }: { pid: number; start: string }) =>
|
|
45
|
+
listActiveMetasForParent(pid).filter(meta => meta.spawnPidStartTime === start && effectiveStatus(meta) === "running").length
|
|
46
|
+
+ this.journal.unresolvedAttempts().filter((attempt: any) => attempt.capacityOwner?.pid === pid && attempt.capacityOwner?.start === start && !readMeta(attempt.id)).length);
|
|
47
|
+
get busy() { return this.pending > 0 || Boolean(this.failureResponse); }
|
|
48
|
+
status() {
|
|
49
|
+
return { owner: "server", instanceId: this.instance, runtimeEpoch: this.epoch,
|
|
50
|
+
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
|
+
}
|
|
52
|
+
constructor(private root: string, private instance: string, private catalog: any, sharedStore?: unknown, private createWorkerHost?: (pi: ExtensionAPI) => NonNullable<ManagedSubagentHost["workerHost"]>, private readPermissions?: () => unknown) {
|
|
53
|
+
this.current = { pid: process.pid, start: processStartToken(process.pid), owner: { workspace: root, sessionId: "runtime-recovery" } };
|
|
54
|
+
this.path = join(root, "operation-coordinator.sqlite");
|
|
55
|
+
const active = listMetas({ includeArchived: false });
|
|
56
|
+
assertSubagentHandoff(active, root);
|
|
57
|
+
const opened = openLaunchJournal(this.path, sharedStore);
|
|
58
|
+
this.journal = opened.journal;
|
|
59
|
+
const unbind = sharedStore ? bindRuntimeOutcomeStore(root, sharedStore as Parameters<typeof bindRuntimeOutcomeStore>[1]) : () => {};
|
|
60
|
+
this.closeJournal = () => { try { unbind(); } finally { opened.close(); } };
|
|
61
|
+
try {
|
|
62
|
+
for (const meta of active) {
|
|
63
|
+
if (meta.management?.root !== root || !processExists(meta.pid) || processStartToken(meta.pid) !== meta.pidStartTime) continue;
|
|
64
|
+
const attempt = this.journal.attempt(meta.id), lease = readConversationLease(meta.sessionId);
|
|
65
|
+
if (!attempt || attempt.sessionId !== meta.sessionId || !lease || lease.token !== attempt.lease || lease.attemptId !== meta.id) {
|
|
66
|
+
throw new Error(`writer-authority-unknown: ${meta.id}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const priorFold = failureFoldRevocations.get(root);
|
|
70
|
+
if (priorFold) Atomics.store(priorFold, 0, 1);
|
|
71
|
+
this.epoch = this.journal.epoch(instance);
|
|
72
|
+
failureFoldRevocations.set(root, this.foldRevocation);
|
|
73
|
+
// An unpublished reservation cannot have dispatched task code. Release
|
|
74
|
+
// only a proven-dead manager's claim before SDK reconciliation sees it.
|
|
75
|
+
for (const claim of (sharedStore as any)?.listResumeClaims() ?? []) {
|
|
76
|
+
if (!["claimed", "published"].includes(claim.state) || !/^(managed-)?subagents:/.test(claim.lease?.ownerId ?? "")) continue;
|
|
77
|
+
const pid = claim.lease.pid;
|
|
78
|
+
if (!pid) continue;
|
|
79
|
+
if (processExists(pid) && (!claim.lease.processStartToken || Buffer.from(processStartToken(pid) ?? "").toString("base64url") === claim.lease.processStartToken || !processStartToken(pid))) continue;
|
|
80
|
+
if (claim.state === "published") {
|
|
81
|
+
// This fixed adapter journals before spawning. Its exact old
|
|
82
|
+
// manager is dead and its epoch fenced; absence of a journal
|
|
83
|
+
// intent proves this publication never dispatched a child.
|
|
84
|
+
if (!claim.attemptId || this.journal.attempt(claim.attemptId) || readMeta(claim.attemptId)) continue;
|
|
85
|
+
const wait = (sharedStore as any).getWait({ waitId: claim.waitId, generation: claim.generation });
|
|
86
|
+
const prior = wait?.park?.attemptId ? readMeta(wait.park.attemptId) : undefined;
|
|
87
|
+
const park = prior ? readParkRecord(prior.id) : undefined;
|
|
88
|
+
if (!prior || prior.management?.root !== root || claim.lease.ownerId !== `managed-subagents:${prior.management.instance}` || !park?.checkpoint) continue;
|
|
89
|
+
if (readConversationLease(prior.sessionId) && (!claim.lease.processStartToken || !releaseUnspawnedConversationLease({
|
|
90
|
+
sessionId: prior.sessionId, attemptId: claim.attemptId, managerPid: pid, managerStart: claim.lease.processStartToken,
|
|
91
|
+
canProveUnspawned: () => { this.journal.current(this.epoch); return !this.journal.attempt(claim.attemptId) && !readMeta(claim.attemptId); },
|
|
92
|
+
}))) continue;
|
|
93
|
+
verifyParkCheckpoint({ ...park.checkpoint, version: 1, waitId: claim.waitId, generation: claim.generation });
|
|
94
|
+
const result = (sharedStore as any).repairFailedResume({ waitId: claim.waitId, generation: claim.generation,
|
|
95
|
+
claimId: claim.claimId, claimToken: claim.claimToken, attemptId: claim.attemptId, launchToken: claim.launchToken,
|
|
96
|
+
proof: { writerPidGone: true, writerLeaseReleased: true }, checkpointId: wait.park.checkpointId,
|
|
97
|
+
expectedCancelEpoch: wait.cancelEpoch, reason: "Exact managed publication never reached pre-spawn journal" });
|
|
98
|
+
updateParkRecord(prior.id, { phase: result.phase, resumeExitReconciledClaimId: claim.claimId });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
(sharedStore as any).releaseResumeClaim({ waitId: claim.waitId, generation: claim.generation,
|
|
102
|
+
claimId: claim.claimId, claimToken: claim.claimToken, outcome: "ready",
|
|
103
|
+
reason: "Recovered unpublished reservation after exact manager exit" });
|
|
104
|
+
}
|
|
105
|
+
for (const attempt of this.journal.unresolvedAttempts()) {
|
|
106
|
+
if (readMeta(attempt.id)) continue;
|
|
107
|
+
const meta = recoverLaunchMetadata(attempt, { root, instance, epoch: this.epoch }, {
|
|
108
|
+
pidExists: processExists, startToken: processStartToken, lease: readConversationLease,
|
|
109
|
+
});
|
|
110
|
+
writeMeta(meta, { ifAbsent: true });
|
|
111
|
+
active.push(meta);
|
|
112
|
+
}
|
|
113
|
+
for (const intent of this.journal.pendingIntents()) {
|
|
114
|
+
this.journal.finishRecovery(intent.owner, intent.request_key, this.epoch, this.recoveredResult(intent));
|
|
115
|
+
}
|
|
116
|
+
for (const meta of active) {
|
|
117
|
+
if (meta.management?.root !== root || (meta.status !== "running" && meta.status !== "orphaned")) continue;
|
|
118
|
+
writeMeta({ ...meta, management: { root, instance, epoch: this.epoch },
|
|
119
|
+
supervisionInterruptedAt: meta.supervisionInterruptedAt ?? Date.now(), supervisionInterruptedReason: "runtime-recovery" });
|
|
120
|
+
}
|
|
121
|
+
} catch (error) {
|
|
122
|
+
Atomics.store(this.foldRevocation, 0, 1);
|
|
123
|
+
if (failureFoldRevocations.get(root) === this.foldRevocation) failureFoldRevocations.delete(root);
|
|
124
|
+
this.closeJournal(); throw error;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async recover() {
|
|
128
|
+
if (listMetas({ includeArchived: false }).some(meta => meta.management?.root === this.root)) await this.initialize();
|
|
129
|
+
}
|
|
130
|
+
private grant(authority: any) {
|
|
131
|
+
if (!authority || !/^[a-f0-9-]{36}$/.test(authority.id) || !/^[a-f0-9]{64}$/.test(authority.secret)) throw new Error("launch-authority-refused");
|
|
132
|
+
const directory = join(this.root, "subagent-launch-admission"), path = join(directory, `${authority.id}.json`);
|
|
133
|
+
const dir = lstatSync(directory), stat = lstatSync(path);
|
|
134
|
+
if (!dir.isDirectory() || dir.isSymbolicLink() || dir.uid !== process.getuid?.() || (dir.mode & 0o077)
|
|
135
|
+
|| !stat.isFile() || stat.isSymbolicLink() || stat.uid !== dir.uid || (stat.mode & 0o077) || stat.size > 1024 * 1024) throw new Error("launch-authority-refused");
|
|
136
|
+
const record = JSON.parse(readFileSync(path, "utf8"));
|
|
137
|
+
const digest = createHash("sha256").update(authority.secret).digest("hex");
|
|
138
|
+
if (record.version !== 1 || record.instanceId !== this.instance || record.id !== authority.id || typeof record.secretHash !== "string"
|
|
139
|
+
|| record.secretHash.length !== digest.length || !timingSafeEqual(Buffer.from(record.secretHash), Buffer.from(digest))
|
|
140
|
+
|| !record.owner?.sessionId
|
|
141
|
+
|| !Number.isSafeInteger(record.pid)
|
|
142
|
+
|| !["subagent_spawn", "subagent_spawn_batch"].includes(record.tool) || typeof record.key !== "string" || !record.key) throw new Error("launch-authority-refused");
|
|
143
|
+
if (record.owner.workspace !== realpathSync(record.owner.workspace)) throw new Error("launch-authority-refused: noncanonical workspace");
|
|
144
|
+
if (!record.start) throw new Error("launch-authority-refused: host process start identity unavailable");
|
|
145
|
+
const start = processStartToken(record.pid);
|
|
146
|
+
if (!start) throw new Error("launch-authority-refused: server cannot verify host process start identity");
|
|
147
|
+
if (start !== record.start) throw new Error("launch-authority-refused: host process identity changed");
|
|
148
|
+
process.kill(record.pid, 0);
|
|
149
|
+
return record;
|
|
150
|
+
}
|
|
151
|
+
private initialize() {
|
|
152
|
+
return this.initializing ??= (async () => {
|
|
153
|
+
const host = this;
|
|
154
|
+
const port: ManagedLaunchPort = {
|
|
155
|
+
backgroundTools: () => host.current.background?.available === true ? host.current.background.tools : [],
|
|
156
|
+
get owner() { return { pid: host.current.pid, start: host.current.start }; },
|
|
157
|
+
get identity() { return { root: host.root, instance: host.instance, epoch: host.epoch }; },
|
|
158
|
+
continuationContext: () => host.executionContext(host.current),
|
|
159
|
+
dispatchContinuation: (park, run) => host.dispatchContinuation(park, run),
|
|
160
|
+
supervised: () => listMetas({ includeArchived: false }).filter(meta => port.owns(meta)),
|
|
161
|
+
owns: meta => meta.management?.root === host.root && meta.management?.instance === host.instance && meta.management?.epoch === host.epoch,
|
|
162
|
+
assertCurrent: () => host.journal.current(host.epoch),
|
|
163
|
+
capacity: () => host.capacityFor(host.current),
|
|
164
|
+
attempt: () => nextRunId(),
|
|
165
|
+
workerCommand: args => ({ file: process.execPath, fileArgs: [fileURLToPath(new URL("./managed-worker.mjs", import.meta.url)), ...args] }),
|
|
166
|
+
snapshot: async configuration => {
|
|
167
|
+
const command = host.catalog.connection();
|
|
168
|
+
command({ type: "runtime_register_catalog", cwd: host.current.owner.workspace,
|
|
169
|
+
sessionId: host.current.owner.sessionId, authority: host.current.catalogAuthority });
|
|
170
|
+
return catalogSnapshotFromWire(command({ type: "runtime_get_catalog", overlay: configuration.getSessionSettings?.() ?? configuration.sessionSettings }));
|
|
171
|
+
},
|
|
172
|
+
beforeSpawn: input => {
|
|
173
|
+
assertSubagentHandoff(listMetas({ includeArchived: false }), host.root);
|
|
174
|
+
if (host.current.continuation) {
|
|
175
|
+
const permissions = intersectContinuationPermissions(host.current.capturedPermissions, host.readPermissions?.());
|
|
176
|
+
if (launchDigest(permissions) !== launchDigest(host.current.permissions)) throw new Error("continuation-permissions-changed");
|
|
177
|
+
host.current.key = `${host.current.baseKey}:${input.id}`;
|
|
178
|
+
host.current.digest = launchDigest({ key: host.current.key, runId: host.current.priorRunId });
|
|
179
|
+
host.journal.accept(host.current.ownerKey, host.current.key, host.current.digest, host.epoch);
|
|
180
|
+
} else {
|
|
181
|
+
const fresh = host.grant(host.current.authority);
|
|
182
|
+
if (launchDigest(fresh) !== host.current.grantDigest) throw new Error("launch-authority-changed");
|
|
183
|
+
}
|
|
184
|
+
const preflightDigest = launchDigest(JSON.parse(readFileSync(input.preflightPath, "utf8")));
|
|
185
|
+
const policyDigest = input.policyPath ? launchDigest(JSON.parse(readFileSync(input.policyPath, "utf8"))) : undefined;
|
|
186
|
+
const attempt = host.journal.prepare(host.current.ownerKey, host.current.key, host.epoch, { ...input, preflightDigest, policyDigest,
|
|
187
|
+
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 } });
|
|
189
|
+
return { PI_MANAGED_SUBAGENT: JSON.stringify({ path: host.path, owner: host.current.ownerKey,
|
|
190
|
+
key: host.current.key, epoch: host.epoch, id: input.id, token: attempt.token }) };
|
|
191
|
+
},
|
|
192
|
+
spawned: (id, pid) => host.journal.spawned(host.current.ownerKey, host.current.key, host.epoch, id, pid, processStartToken(pid)),
|
|
193
|
+
};
|
|
194
|
+
const cwd = this.root;
|
|
195
|
+
const settingsManager = SettingsManager.inMemory({});
|
|
196
|
+
const resourceLoader = new DefaultResourceLoader({ cwd, agentDir: this.root, settingsManager,
|
|
197
|
+
noExtensions: true, noSkills: true, noContextFiles: true, noPromptTemplates: true, noThemes: true,
|
|
198
|
+
extensionFactories: [(pi: ExtensionAPI) => {
|
|
199
|
+
const registered: any[] = [];
|
|
200
|
+
const adapterPi = { ...pi,
|
|
201
|
+
registerTool: (tool: any) => { registered.push(tool); pi.registerTool(tool); },
|
|
202
|
+
getAllTools: () => [...registered, ...pi.getAllTools()],
|
|
203
|
+
} as ExtensionAPI;
|
|
204
|
+
this.api = adapterPi;
|
|
205
|
+
this.workerHost = this.createWorkerHost?.(adapterPi);
|
|
206
|
+
if (this.workerHost) pi.on("session_start", (_event, ctx) => this.workerHost!.start(ctx));
|
|
207
|
+
extension(adapterPi, port);
|
|
208
|
+
}] });
|
|
209
|
+
await resourceLoader.reload();
|
|
210
|
+
const created = await createAgentSession({ cwd, agentDir: this.root, settingsManager, resourceLoader,
|
|
211
|
+
sessionManager: SessionManager.inMemory(cwd), noTools: "all" });
|
|
212
|
+
this.session = created.session;
|
|
213
|
+
const initializationErrors: string[] = [];
|
|
214
|
+
await this.session.bindExtensions({ mode: "print", onError: (error: any) => {
|
|
215
|
+
if (!initializationErrors.length) initializationErrors.push(String(error.error ?? error.message ?? error));
|
|
216
|
+
} });
|
|
217
|
+
if (initializationErrors.length) throw new Error(`Managed launch initialization failed: ${initializationErrors.join("; ")}`);
|
|
218
|
+
})();
|
|
219
|
+
}
|
|
220
|
+
private executionContext(record: any) {
|
|
221
|
+
const ctx = this.session.extensionRunner.createContext();
|
|
222
|
+
const branch = [...record.settings, ...(record.owner.goalId ? [{ type: "custom", customType: "pi-better-goal", data: { kind: "set", goal: { goalId: record.owner.goalId } } }] : []), { id: record.owner.branchId }];
|
|
223
|
+
const model = record.model?.split("/");
|
|
224
|
+
return { ...ctx, cwd: record.owner.workspace, hasUI: false, isProjectTrusted: () => record.projectTrusted,
|
|
225
|
+
model: model ? ctx.modelRegistry.find(model[0], model.slice(1).join("/")) : undefined,
|
|
226
|
+
sessionManager: { getSessionId: () => record.owner.sessionId, getBranch: () => branch, getLeafId: () => record.owner.branchId,
|
|
227
|
+
getSessionFile: () => undefined, getEntries: () => branch } };
|
|
228
|
+
}
|
|
229
|
+
private dispatchContinuation(park: any, run: () => Promise<"resumed" | "held" | "skipped">) {
|
|
230
|
+
const now = Date.now();
|
|
231
|
+
if (now - this.continuationWindow.started >= 1000) this.continuationWindow = { started: now, admitted: 0 };
|
|
232
|
+
if (this.continuationWindow.admitted >= 32) return Promise.resolve("skipped" as const);
|
|
233
|
+
const retryKey = `${park.waitId}:${park.generation}`;
|
|
234
|
+
const failure = this.continuationFailures.get(retryKey);
|
|
235
|
+
if (failure && Date.now() < failure.retryAt) return Promise.resolve("skipped" as const);
|
|
236
|
+
this.continuationWindow.admitted++;
|
|
237
|
+
this.pending++;
|
|
238
|
+
const result = this.tail.catch(() => undefined).then(async () => {
|
|
239
|
+
const saved = this.journal.attempt(park.runId)?.launchAuthority;
|
|
240
|
+
if (!saved) throw new Error("Captured continuation launch authority unavailable");
|
|
241
|
+
const permissions = intersectContinuationPermissions(saved.permissions, this.readPermissions?.());
|
|
242
|
+
const key = `continuation:${park.waitId}:${park.generation}`;
|
|
243
|
+
this.current = { ...saved, key, baseKey: key, priorRunId: park.runId, continuation: true, launchAuthority: saved,
|
|
244
|
+
capturedPermissions: saved.permissions, permissions,
|
|
245
|
+
ownerKey: JSON.stringify([saved.owner.workspace, saved.owner.sessionId]),
|
|
246
|
+
digest: launchDigest({ key, runId: park.runId }) };
|
|
247
|
+
this.api.events.emit("pi-better-sandbox:policy", { state: "enabled", ...permissions });
|
|
248
|
+
const outcome = await run();
|
|
249
|
+
if (outcome === "resumed") this.journal.finish(this.current.ownerKey, this.current.key, this.epoch, { status: outcome });
|
|
250
|
+
this.continuationFailures.delete(retryKey);
|
|
251
|
+
return outcome;
|
|
252
|
+
}).catch(error => {
|
|
253
|
+
const failures = Math.min(6, (this.continuationFailures.get(retryKey)?.failures ?? 0) + 1);
|
|
254
|
+
if (this.continuationFailures.size >= 128 && !this.continuationFailures.has(retryKey)) this.continuationFailures.delete(this.continuationFailures.keys().next().value!);
|
|
255
|
+
this.continuationFailures.set(retryKey, { failures, retryAt: Date.now() + Math.min(30000, 1000 * 2 ** (failures - 1)), reason: String(error).slice(0, 500) });
|
|
256
|
+
return "skipped" as const;
|
|
257
|
+
}).finally(() => { this.pending--; });
|
|
258
|
+
this.tail = result;
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
private recoveredResult(intent: { attempts: string }) {
|
|
262
|
+
const ids = JSON.parse(intent.attempts).map(({ id }: any) => id);
|
|
263
|
+
const runs = ids.map((id: string) => readMeta(id));
|
|
264
|
+
const status = !ids.length ? "not_launched" : runs.every(Boolean) ? "recovered" : "outcome_unknown";
|
|
265
|
+
return { details: { status, attempts: ids, runs: runs.filter(Boolean).map((meta: any) => ({
|
|
266
|
+
runId: meta.id, sessionId: meta.sessionId, status: meta.status,
|
|
267
|
+
})) }, content: [{ type: "text", text: `Original launch ${status}; no replacement attempt was dispatched.` }] };
|
|
268
|
+
}
|
|
269
|
+
handle(command: Record<string, unknown>) {
|
|
270
|
+
if (Object.keys(command).some(key => !["id", "type", "authority"].includes(key))) return Promise.reject(new Error("unsupported launch field"));
|
|
271
|
+
this.pending++;
|
|
272
|
+
const result = this.tail.catch(() => undefined).then(async () => {
|
|
273
|
+
const record = this.grant(command.authority);
|
|
274
|
+
// A retried tool call keeps its accepted recipient even after Goal/branch edits.
|
|
275
|
+
const ownerKey = JSON.stringify([record.owner.workspace, record.owner.sessionId]);
|
|
276
|
+
const digest = launchDigest({ tool: record.tool, params: record.params });
|
|
277
|
+
const intent = this.journal.accept(ownerKey, record.key, digest, this.epoch);
|
|
278
|
+
if (intent.prior) {
|
|
279
|
+
if (intent.result) return JSON.parse(intent.result);
|
|
280
|
+
return this.recoveredResult(intent);
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
this.current = { ...record, authority: command.authority, ownerKey, grantDigest: launchDigest(record) };
|
|
284
|
+
await this.initialize();
|
|
285
|
+
this.api.events.emit("pi-better-sandbox:policy", { state: "enabled", ...record.permissions });
|
|
286
|
+
const context = this.executionContext(record);
|
|
287
|
+
const tool = this.session.extensionRunner.getToolDefinition(record.tool);
|
|
288
|
+
if (!tool) throw new Error("Fixed subagent adapter unavailable");
|
|
289
|
+
const response = await tool.execute(record.key, record.params, undefined, undefined, context);
|
|
290
|
+
this.journal.finish(ownerKey, record.key, this.epoch, response);
|
|
291
|
+
return response;
|
|
292
|
+
} catch (error) {
|
|
293
|
+
const pending = this.journal.read(ownerKey, record.key);
|
|
294
|
+
if (pending?.state === "accepted" && JSON.parse(pending.attempts).length === 0) {
|
|
295
|
+
this.journal.finish(ownerKey, record.key, this.epoch, this.recoveredResult(pending));
|
|
296
|
+
}
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
}).finally(() => { this.pending--; });
|
|
300
|
+
this.tail = result;
|
|
301
|
+
return result;
|
|
302
|
+
}
|
|
303
|
+
/** One root mechanical worker; no coordinator connection or query mutations. */
|
|
304
|
+
foldTerminalFailures(ids: readonly string[]): Promise<void> {
|
|
305
|
+
const result = this.tail.catch(() => undefined).then(async () => {
|
|
306
|
+
this.journal.current(this.epoch);
|
|
307
|
+
if (Atomics.load(this.foldRevocation, 0)) throw new Error("failure-fold-owner-revoked");
|
|
308
|
+
if (this.failureWorkerError) throw this.failureWorkerError;
|
|
309
|
+
if (ids.length > 128) throw new Error("failure-fold-batch-overflow");
|
|
310
|
+
if (!this.failureWorker) {
|
|
311
|
+
const worker = this.failureWorker = new Worker(new URL("./managed-failure-worker.mjs", import.meta.url), {
|
|
312
|
+
execArgv: [], workerData: { root: this.root, epoch: this.epoch, revocation: this.foldRevocation.buffer },
|
|
313
|
+
});
|
|
314
|
+
const fail = (error: Error) => {
|
|
315
|
+
this.failureWorkerError = error;
|
|
316
|
+
this.failureResponse?.reject(error); this.failureResponse = undefined;
|
|
317
|
+
};
|
|
318
|
+
worker.on("message", message => {
|
|
319
|
+
const response = this.failureResponse; this.failureResponse = undefined;
|
|
320
|
+
if (message.error) response?.reject(new Error(message.error)); else response?.resolve();
|
|
321
|
+
});
|
|
322
|
+
worker.on("error", fail);
|
|
323
|
+
worker.on("exit", code => fail(new Error(`failure-fold-worker-exited: ${code}`)));
|
|
324
|
+
worker.unref();
|
|
325
|
+
}
|
|
326
|
+
await new Promise<void>((resolve, reject) => {
|
|
327
|
+
this.failureResponse = { resolve, reject };
|
|
328
|
+
this.failureWorker!.postMessage({ ids });
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
this.tail = result;
|
|
332
|
+
return result;
|
|
333
|
+
}
|
|
334
|
+
async close() {
|
|
335
|
+
Atomics.store(this.foldRevocation, 0, 1);
|
|
336
|
+
await this.tail.catch(() => undefined);
|
|
337
|
+
await this.failureWorker?.terminate(); this.failureWorker = undefined;
|
|
338
|
+
if (failureFoldRevocations.get(this.root) === this.foldRevocation) failureFoldRevocations.delete(this.root);
|
|
339
|
+
const session = this.session;
|
|
340
|
+
this.session = undefined;
|
|
341
|
+
const workerHost = this.workerHost;
|
|
342
|
+
this.workerHost = undefined;
|
|
343
|
+
try {
|
|
344
|
+
if (session) await session.extensionRunner.emit({ type: "session_shutdown", reason: "quit" });
|
|
345
|
+
} finally {
|
|
346
|
+
try { workerHost?.close(); } finally {
|
|
347
|
+
try { session?.dispose(); } finally { this.closeJournal(); }
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { openStore } from "./shared-operation-coordinator/index.ts";
|
|
3
|
+
const ordered = value => value === null || typeof value !== "object" ? value
|
|
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");
|
|
7
|
+
|
|
8
|
+
/** Standalone callers own a store; server callers pass their shared mutation lease. */
|
|
9
|
+
export function openLaunchJournal(path, sharedStore) {
|
|
10
|
+
const store = sharedStore ?? openStore({ path });
|
|
11
|
+
try {
|
|
12
|
+
const journal = store.subagentLaunchJournal();
|
|
13
|
+
return { journal, close: () => { if (!sharedStore) store.close(); } };
|
|
14
|
+
} catch (error) {
|
|
15
|
+
if (!sharedStore) store.close();
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { realProcessProbe, type ProcessProbe } from "./health.ts";
|
|
2
|
+
import type { RunMeta } from "./registry.ts";
|
|
3
|
+
|
|
4
|
+
/** Management transfer never treats timeout or absent identity as proof of death. */
|
|
5
|
+
export function assertSubagentHandoff(metas: readonly RunMeta[], root: string, probe: Pick<ProcessProbe, "pidExists" | "startToken"> = realProcessProbe): void {
|
|
6
|
+
const evidence = (pid: number | undefined, start: string | undefined) => {
|
|
7
|
+
if (!pid || !probe.pidExists(pid)) return "gone";
|
|
8
|
+
const observed = probe.startToken(pid);
|
|
9
|
+
if (!start || !observed) return "unknown";
|
|
10
|
+
return observed === start ? "live" : "gone";
|
|
11
|
+
};
|
|
12
|
+
for (const meta of metas) {
|
|
13
|
+
if (meta.status !== "running" && meta.status !== "orphaned") continue;
|
|
14
|
+
if (!meta.management) {
|
|
15
|
+
if (evidence(meta.spawnPid, meta.spawnPidStartTime) !== "gone") throw new Error(`legacy-manager-live-or-unknown: ${meta.id}`);
|
|
16
|
+
if (evidence(meta.pid, meta.pidStartTime) !== "gone") throw new Error(`legacy-writer-live-or-unknown: ${meta.id}`);
|
|
17
|
+
} else {
|
|
18
|
+
if (meta.management.root !== root) continue;
|
|
19
|
+
if (evidence(meta.pid, meta.pidStartTime) === "unknown") throw new Error(`writer-identity-unknown: ${meta.id}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CatalogHost } from "./catalog-runtime.ts";
|
|
2
|
+
import type { CatalogSnapshot } from "./catalog-store.ts";
|
|
3
|
+
import type { RunMeta } from "./registry.ts";
|
|
4
|
+
|
|
5
|
+
/** Fixed server adapter port; never deserialized from a socket request. */
|
|
6
|
+
export interface ManagedLaunchPort {
|
|
7
|
+
backgroundTools(): readonly string[];
|
|
8
|
+
readonly owner: { pid: number; start?: string };
|
|
9
|
+
readonly identity: { root: string; instance: string; epoch: number };
|
|
10
|
+
continuationContext?(): import("@earendil-works/pi-coding-agent").ExtensionContext | undefined;
|
|
11
|
+
dispatchContinuation?(park: import("./worker-waits.ts").ParkedWait, run: () => Promise<"resumed" | "held" | "skipped">): Promise<"resumed" | "held" | "skipped">;
|
|
12
|
+
supervised(): RunMeta[];
|
|
13
|
+
owns(meta: Partial<RunMeta>): boolean;
|
|
14
|
+
assertCurrent(): void;
|
|
15
|
+
capacity(countRunning: () => number): { readonly pending: number; tryReserve(count: number, cap: number): boolean; commit(count?: number): void; release(count?: number): void };
|
|
16
|
+
snapshot(host: CatalogHost): Promise<CatalogSnapshot>;
|
|
17
|
+
attempt(): string;
|
|
18
|
+
workerCommand(args: string[]): { file: string; fileArgs: string[] };
|
|
19
|
+
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
|
+
spawned(id: string, pid: number): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { RunMeta } from "./registry.ts";
|
|
2
|
+
|
|
3
|
+
interface RecoverableAttempt {
|
|
4
|
+
id: string;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
lease: string;
|
|
7
|
+
pid?: number;
|
|
8
|
+
pidStartTime?: string;
|
|
9
|
+
writerPid?: number;
|
|
10
|
+
writerStartTime?: string;
|
|
11
|
+
bootstrapped?: boolean;
|
|
12
|
+
recoveryMeta?: unknown;
|
|
13
|
+
}
|
|
14
|
+
interface RecoveryEvidence {
|
|
15
|
+
pidExists(pid: number): boolean;
|
|
16
|
+
startToken(pid: number): string | undefined;
|
|
17
|
+
lease(sessionId: string): { token: string; attemptId: string } | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Called only after replacing the management epoch, fencing every old bootstrap. */
|
|
21
|
+
export function recoverLaunchMetadata(attempt: RecoverableAttempt, management: NonNullable<RunMeta["management"]>, evidence: RecoveryEvidence, now = Date.now()): RunMeta {
|
|
22
|
+
const frozen = attempt.recoveryMeta as RunMeta | undefined;
|
|
23
|
+
if (!frozen || frozen.id !== attempt.id || frozen.sessionId !== attempt.sessionId || frozen.management?.root !== management.root) {
|
|
24
|
+
throw new Error(`launch-recovery-evidence-missing: ${attempt.id}`);
|
|
25
|
+
}
|
|
26
|
+
const pid = attempt.writerPid ?? attempt.pid;
|
|
27
|
+
const start = attempt.writerStartTime ?? attempt.pidStartTime;
|
|
28
|
+
if (attempt.bootstrapped && !pid) throw new Error(`writer-identity-unknown: ${attempt.id}`);
|
|
29
|
+
if (pid && evidence.pidExists(pid)) {
|
|
30
|
+
const observed = evidence.startToken(pid);
|
|
31
|
+
if (!start || !observed) throw new Error(`writer-identity-unknown: ${attempt.id}`);
|
|
32
|
+
if (observed === start) {
|
|
33
|
+
const lease = evidence.lease(attempt.sessionId);
|
|
34
|
+
if (!lease || lease.token !== attempt.lease || lease.attemptId !== attempt.id) {
|
|
35
|
+
throw new Error(`writer-authority-unknown: ${attempt.id}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// No recorded PID and no successful bootstrap means the superseded token can
|
|
40
|
+
// never execute task code. A dispatched writer retains ordinary health/log
|
|
41
|
+
// reconciliation, including process-group evidence and interrupted streams.
|
|
42
|
+
return { ...frozen, management, pid: pid ?? 0,
|
|
43
|
+
...(pid ? { pidStartTime: start, pgid: pid } : { status: "lost", lostAt: now, endedAt: now }),
|
|
44
|
+
supervisionInterruptedAt: now, supervisionInterruptedReason: "runtime-launch-recovery" };
|
|
45
|
+
}
|