pi-daddy 0.62.0 → 0.63.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/CHANGELOG.md +10 -0
- package/README.md +28 -3
- package/contracts/task-history/v1/README.md +76 -0
- package/contracts/task-history/v1/dispatch.ts +38 -0
- package/contracts/task-history/v1/observation.schema.json +73 -0
- package/contracts/task-history/v1/producer.ts +48 -0
- package/dist/governance/runtime-settlement.d.ts +8 -1
- package/dist/governance/runtime-settlement.d.ts.map +1 -1
- package/dist/governance/runtime-settlement.js +42 -4
- package/dist/governance/runtime-settlement.js.map +1 -1
- package/dist/governance/task-dispatch.d.ts +85 -0
- package/dist/governance/task-dispatch.d.ts.map +1 -0
- package/dist/governance/task-dispatch.js +342 -0
- package/dist/governance/task-dispatch.js.map +1 -0
- package/dist/kernel/context-handoff.d.ts +14 -0
- package/dist/kernel/context-handoff.d.ts.map +1 -1
- package/dist/kernel/context-handoff.js +17 -0
- package/dist/kernel/context-handoff.js.map +1 -1
- package/dist/kernel/dashboard-choice.d.ts +5 -0
- package/dist/kernel/dashboard-choice.d.ts.map +1 -1
- package/dist/kernel/dashboard-choice.js.map +1 -1
- package/dist/kernel/dashboard-observation.d.ts +31 -0
- package/dist/kernel/dashboard-observation.d.ts.map +1 -1
- package/dist/kernel/dashboard-observation.js +33 -1
- package/dist/kernel/dashboard-observation.js.map +1 -1
- package/dist/kernel/dashboard-request.d.ts +4 -0
- package/dist/kernel/dashboard-request.d.ts.map +1 -0
- package/dist/kernel/dashboard-request.js +8 -0
- package/dist/kernel/dashboard-request.js.map +1 -0
- package/dist/kernel/delegate.d.ts.map +1 -1
- package/dist/kernel/delegate.js +24 -8
- package/dist/kernel/delegate.js.map +1 -1
- package/dist/products/dashboard-choice-picker.d.ts +2 -0
- package/dist/products/dashboard-choice-picker.d.ts.map +1 -1
- package/dist/products/dashboard-choice-picker.js +46 -18
- package/dist/products/dashboard-choice-picker.js.map +1 -1
- package/dist/products/dashboard-cli.d.ts.map +1 -1
- package/dist/products/dashboard-cli.js +31 -4
- package/dist/products/dashboard-cli.js.map +1 -1
- package/dist/products/dashboard-components.d.ts +2 -0
- package/dist/products/dashboard-components.d.ts.map +1 -1
- package/dist/products/dashboard-components.js +81 -0
- package/dist/products/dashboard-components.js.map +1 -1
- package/dist/products/dashboard-panels.d.ts.map +1 -1
- package/dist/products/dashboard-panels.js +7 -2
- package/dist/products/dashboard-panels.js.map +1 -1
- package/dist/products/dashboard-screen.d.ts +2 -0
- package/dist/products/dashboard-screen.d.ts.map +1 -1
- package/dist/products/dashboard-screen.js +12 -3
- package/dist/products/dashboard-screen.js.map +1 -1
- package/dist/products/dashboard-session-client.d.ts +8 -0
- package/dist/products/dashboard-session-client.d.ts.map +1 -1
- package/dist/products/dashboard-session-client.js +54 -26
- package/dist/products/dashboard-session-client.js.map +1 -1
- package/dist/products/dashboard-task-controls.d.ts +6 -0
- package/dist/products/dashboard-task-controls.d.ts.map +1 -0
- package/dist/products/dashboard-task-controls.js +27 -0
- package/dist/products/dashboard-task-controls.js.map +1 -0
- package/extensions/dashboard-choice-controls.ts +33 -1
- package/extensions/dashboard-session-server.ts +67 -4
- package/extensions/dashboard-sources.ts +21 -2
- package/extensions/definition-describe.ts +3 -1
- package/extensions/delegate-chain.ts +6 -0
- package/extensions/delegation-artifacts.ts +34 -0
- package/extensions/delegation-operations.ts +30 -1
- package/extensions/delegation.ts +26 -2
- package/extensions/execute-child.ts +4 -0
- package/extensions/grants.ts +3 -0
- package/extensions/run-delegation.ts +58 -6
- package/extensions/runtime-snapshot.ts +23 -17
- package/extensions/session.ts +4 -0
- package/extensions/task-dispatch-controller.ts +384 -0
- package/extensions/task-inline-control.ts +21 -0
- package/extensions/task-worktree-writer.ts +55 -0
- package/package.json +1 -1
- package/src/governance/runtime-settlement.ts +44 -2
- package/src/governance/task-dispatch.ts +427 -0
- package/src/kernel/context-handoff.ts +19 -0
- package/src/kernel/dashboard-choice.ts +7 -1
- package/src/kernel/dashboard-observation.ts +67 -2
- package/src/kernel/dashboard-request.ts +8 -0
- package/src/kernel/delegate.ts +33 -14
- package/src/products/dashboard-choice-picker.ts +51 -16
- package/src/products/dashboard-cli.ts +33 -4
- package/src/products/dashboard-components.ts +84 -0
- package/src/products/dashboard-panels.ts +6 -1
- package/src/products/dashboard-screen.ts +14 -3
- package/src/products/dashboard-session-client.ts +79 -28
- package/src/products/dashboard-task-controls.ts +34 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
/** Trusted Bookie selection identifies the task. Daddy retains authority over only its own dispatch. */
|
|
2
|
+
import {
|
|
3
|
+
TaskDispatch,
|
|
4
|
+
type DispatchEdit,
|
|
5
|
+
type DispatchView,
|
|
6
|
+
type DispatchOwner,
|
|
7
|
+
isDispatchEdit,
|
|
8
|
+
} from "../src/governance/task-dispatch.ts";
|
|
9
|
+
import { claimTaskWriter } from "./task-worktree-writer.ts";
|
|
10
|
+
import { agentDir } from "../src/kernel/project-paths.ts";
|
|
11
|
+
import {
|
|
12
|
+
readRuntimeExecutionSettlement,
|
|
13
|
+
pinPrelaunchOwner,
|
|
14
|
+
type RuntimeSettlement,
|
|
15
|
+
} from "../src/governance/runtime-settlement.ts";
|
|
16
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
|
|
18
|
+
interface TaskScope {
|
|
19
|
+
projectId: string;
|
|
20
|
+
budgetRootTaskId?: string;
|
|
21
|
+
taskId: string;
|
|
22
|
+
revision: number;
|
|
23
|
+
assertCurrent(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
type Host = { cwd: string; sessionId: string };
|
|
26
|
+
export type AdmittedAttempt = {
|
|
27
|
+
beforeRuntimeBegin?: (runtime: RuntimeSettlement | undefined) => void;
|
|
28
|
+
signal: AbortSignal;
|
|
29
|
+
finish(cleanup: "settled" | "not-started" | "unknown"): void;
|
|
30
|
+
};
|
|
31
|
+
export type TaskDispatchAdmission = {
|
|
32
|
+
(executionId: string, writerFolder?: string): Promise<AdmittedAttempt>;
|
|
33
|
+
assertAvailable(): Promise<void>;
|
|
34
|
+
reconcile(executionId: string, cleanup: "settled" | "not-started"): Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
export function createTaskDispatchController(
|
|
37
|
+
bus: ExtensionAPI["events"],
|
|
38
|
+
current: () => Host | undefined,
|
|
39
|
+
directory = agentDir,
|
|
40
|
+
nativeOwner?: () => RuntimeSettlement | undefined,
|
|
41
|
+
) {
|
|
42
|
+
let lastBoundHost: Host | undefined;
|
|
43
|
+
let previous: TaskDispatch | undefined;
|
|
44
|
+
let lastDisplay:
|
|
45
|
+
| { host: Host; project: string; task: string; root: string; revision: number; checked: number; view: DispatchView }
|
|
46
|
+
| undefined;
|
|
47
|
+
const storeFor = (scope: TaskScope) => {
|
|
48
|
+
const rootId = scope.budgetRootTaskId ?? scope.taskId;
|
|
49
|
+
if (!previous || previous.projectId !== scope.projectId || previous.taskId !== rootId)
|
|
50
|
+
previous = new TaskDispatch(directory(), scope.projectId, rootId);
|
|
51
|
+
return previous;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function select() {
|
|
55
|
+
const host = current();
|
|
56
|
+
if (!host) return;
|
|
57
|
+
const response: { scope?: TaskScope; unavailable?: boolean } = {};
|
|
58
|
+
let accepting = true;
|
|
59
|
+
try {
|
|
60
|
+
bus?.emit?.("pi.task-history.v1.scope", {
|
|
61
|
+
version: 1,
|
|
62
|
+
cwd: host.cwd,
|
|
63
|
+
sessionId: host.sessionId,
|
|
64
|
+
unavailable() {
|
|
65
|
+
if (accepting) response.unavailable = true;
|
|
66
|
+
},
|
|
67
|
+
accept(candidate: TaskScope) {
|
|
68
|
+
if (!accepting) return;
|
|
69
|
+
if (response.scope) {
|
|
70
|
+
response.unavailable = true;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (
|
|
74
|
+
candidate &&
|
|
75
|
+
typeof candidate.projectId === "string" &&
|
|
76
|
+
typeof candidate.taskId === "string" &&
|
|
77
|
+
(candidate.budgetRootTaskId === undefined || typeof candidate.budgetRootTaskId === "string") &&
|
|
78
|
+
Number.isSafeInteger(candidate.revision) &&
|
|
79
|
+
typeof candidate.assertCurrent === "function"
|
|
80
|
+
)
|
|
81
|
+
response.scope = candidate;
|
|
82
|
+
else response.unavailable = true;
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
} catch {
|
|
86
|
+
response.unavailable = true;
|
|
87
|
+
} finally {
|
|
88
|
+
accepting = false;
|
|
89
|
+
}
|
|
90
|
+
if (response.unavailable || (!response.scope && lastBoundHost === host))
|
|
91
|
+
throw Error("Task scope is temporarily unavailable. Reopen its history before dispatch.");
|
|
92
|
+
if (!response.scope) return;
|
|
93
|
+
lastBoundHost = host;
|
|
94
|
+
const chosen = response.scope;
|
|
95
|
+
const assertCurrent = async () => {
|
|
96
|
+
if (current() !== host) throw Error("Task session changed");
|
|
97
|
+
await chosen.assertCurrent();
|
|
98
|
+
if (current() !== host) throw Error("Task session changed");
|
|
99
|
+
};
|
|
100
|
+
return { scope: chosen, assertCurrent, host, store: storeFor(chosen) };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Recover an interrupted predecessor without replacing its occurrence ID. Missing or ambiguous receipts stay pending.
|
|
104
|
+
async function reconcilePredecessor(binding: NonNullable<ReturnType<typeof select>>) {
|
|
105
|
+
const runtime = nativeOwner?.();
|
|
106
|
+
if (!runtime) return;
|
|
107
|
+
for (const attempt of binding.store.outstanding()) {
|
|
108
|
+
if (attempt.owner !== "pi-daddy" || !attempt.beforeLaunch || attempt.beforeLaunch.ownerId === runtime.ownerId)
|
|
109
|
+
continue;
|
|
110
|
+
const state = await readRuntimeExecutionSettlement(
|
|
111
|
+
directory(),
|
|
112
|
+
attempt.sessionId,
|
|
113
|
+
attempt.cwd,
|
|
114
|
+
attempt.executionId,
|
|
115
|
+
attempt.beforeLaunch,
|
|
116
|
+
).catch(() => "unknown" as const);
|
|
117
|
+
if (state !== "unknown") {
|
|
118
|
+
await binding.assertCurrent();
|
|
119
|
+
binding.store.settle(attempt.owner, attempt.executionId, state, attempt.taskId ?? binding.store.taskId);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const controller = {
|
|
125
|
+
/** An inline effect's owner can consult the shared pause state without spending a delegated attempt. */
|
|
126
|
+
captureInline(): (() => Promise<void>) | undefined {
|
|
127
|
+
let binding: ReturnType<typeof select>;
|
|
128
|
+
try {
|
|
129
|
+
binding = select();
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return async () => {
|
|
132
|
+
throw error;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (!binding) return;
|
|
136
|
+
return async () => {
|
|
137
|
+
await binding.assertCurrent();
|
|
138
|
+
binding.store.assertOpen();
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
capture(owner: DispatchOwner = "pi-daddy", expectedHost?: Host): TaskDispatchAdmission | undefined {
|
|
142
|
+
if (expectedHost && (current()?.cwd !== expectedHost.cwd || current()?.sessionId !== expectedHost.sessionId))
|
|
143
|
+
return;
|
|
144
|
+
let binding: ReturnType<typeof select>;
|
|
145
|
+
try {
|
|
146
|
+
binding = select();
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const unavailable = async (): Promise<never> => {
|
|
149
|
+
throw error;
|
|
150
|
+
};
|
|
151
|
+
return Object.assign(unavailable, { assertAvailable: unavailable, reconcile: unavailable });
|
|
152
|
+
}
|
|
153
|
+
if (
|
|
154
|
+
!binding ||
|
|
155
|
+
(expectedHost && (binding.host.cwd !== expectedHost.cwd || binding.host.sessionId !== expectedHost.sessionId))
|
|
156
|
+
)
|
|
157
|
+
return;
|
|
158
|
+
const admit = async (executionId: string, writerFolder?: string): Promise<AdmittedAttempt> => {
|
|
159
|
+
const { host, scope, store } = binding;
|
|
160
|
+
await binding.assertCurrent();
|
|
161
|
+
const native = owner === "pi-daddy" ? nativeOwner?.() : undefined;
|
|
162
|
+
if (owner === "pi-daddy" && nativeOwner && !native)
|
|
163
|
+
throw Error("Native owner is unavailable before task admission");
|
|
164
|
+
const beforeLaunch = native ? pinPrelaunchOwner(native, host.sessionId, host.cwd) : undefined;
|
|
165
|
+
const writer =
|
|
166
|
+
owner === "pi-daddy" && writerFolder
|
|
167
|
+
? await claimTaskWriter(directory(), writerFolder, host, executionId, beforeLaunch)
|
|
168
|
+
: undefined;
|
|
169
|
+
let reserved = false;
|
|
170
|
+
try {
|
|
171
|
+
await binding.assertCurrent();
|
|
172
|
+
store.admit({
|
|
173
|
+
owner,
|
|
174
|
+
executionId,
|
|
175
|
+
taskId: scope.taskId,
|
|
176
|
+
cwd: host.cwd,
|
|
177
|
+
sessionId: host.sessionId,
|
|
178
|
+
...(beforeLaunch ? { beforeLaunch } : {}),
|
|
179
|
+
});
|
|
180
|
+
reserved = true;
|
|
181
|
+
await binding.assertCurrent();
|
|
182
|
+
} catch (error) {
|
|
183
|
+
try {
|
|
184
|
+
if (reserved) store.settle(owner, executionId, "not-started", scope.taskId);
|
|
185
|
+
} finally {
|
|
186
|
+
writer?.("not-started");
|
|
187
|
+
}
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
const controller = new AbortController();
|
|
191
|
+
const checkCancel = () => {
|
|
192
|
+
try {
|
|
193
|
+
if (store.snapshot(scope.revision).state === "cancelling") controller.abort();
|
|
194
|
+
} catch {
|
|
195
|
+
// An unreadable journal does not authorize signalling the running worker.
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
checkCancel();
|
|
199
|
+
const timer = setInterval(checkCancel, 2000);
|
|
200
|
+
timer.unref();
|
|
201
|
+
return {
|
|
202
|
+
signal: controller.signal,
|
|
203
|
+
...(native
|
|
204
|
+
? {
|
|
205
|
+
beforeRuntimeBegin(runtime: RuntimeSettlement | undefined) {
|
|
206
|
+
if (runtime !== native || runtime !== nativeOwner?.() || current() !== host)
|
|
207
|
+
throw Error("Task reservation's native owner changed before executor entry");
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
: {}),
|
|
211
|
+
finish(cleanup) {
|
|
212
|
+
clearInterval(timer);
|
|
213
|
+
if (cleanup === "settled" || cleanup === "not-started") {
|
|
214
|
+
try {
|
|
215
|
+
store.settle(owner, executionId, cleanup, scope.taskId);
|
|
216
|
+
} finally {
|
|
217
|
+
writer?.(cleanup);
|
|
218
|
+
}
|
|
219
|
+
} else if (cleanup !== "unknown") throw Error("Unsupported task cleanup status");
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
return Object.assign(admit, {
|
|
224
|
+
assertAvailable: async () => {
|
|
225
|
+
await binding.assertCurrent();
|
|
226
|
+
await reconcilePredecessor(binding);
|
|
227
|
+
binding.store.assertAvailable(owner);
|
|
228
|
+
},
|
|
229
|
+
reconcile: async (executionId: string, cleanup: "settled" | "not-started") => {
|
|
230
|
+
if (cleanup !== "settled" && cleanup !== "not-started") throw Error("A native terminal receipt is required");
|
|
231
|
+
await binding.assertCurrent();
|
|
232
|
+
const pending = binding.store.outstanding().find((a) => a.owner === owner && a.executionId === executionId);
|
|
233
|
+
if (!pending) return;
|
|
234
|
+
if (pending.cwd !== binding.host.cwd) throw Error("Original execution folder changed");
|
|
235
|
+
binding.store.settle(owner, executionId, cleanup, binding.scope.taskId);
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
async snapshot(reuseDisplayed = false): Promise<DispatchView | undefined> {
|
|
240
|
+
const binding = select();
|
|
241
|
+
if (!binding) return;
|
|
242
|
+
const { scope, host, store } = binding;
|
|
243
|
+
if (
|
|
244
|
+
reuseDisplayed &&
|
|
245
|
+
lastDisplay &&
|
|
246
|
+
lastDisplay.host === host &&
|
|
247
|
+
lastDisplay.root === store.taskId &&
|
|
248
|
+
lastDisplay.project === scope.projectId &&
|
|
249
|
+
lastDisplay.task === scope.taskId &&
|
|
250
|
+
lastDisplay.revision === scope.revision &&
|
|
251
|
+
Date.now() >= lastDisplay.checked &&
|
|
252
|
+
Date.now() - lastDisplay.checked < 1000
|
|
253
|
+
)
|
|
254
|
+
return lastDisplay.view;
|
|
255
|
+
await binding.assertCurrent();
|
|
256
|
+
const checked = Date.now(),
|
|
257
|
+
view = store.snapshot(scope.revision, scope.taskId);
|
|
258
|
+
view.observedAt = new Date(checked).toISOString();
|
|
259
|
+
lastDisplay = {
|
|
260
|
+
host,
|
|
261
|
+
project: scope.projectId,
|
|
262
|
+
task: scope.taskId,
|
|
263
|
+
root: store.taskId,
|
|
264
|
+
revision: scope.revision,
|
|
265
|
+
checked,
|
|
266
|
+
view,
|
|
267
|
+
};
|
|
268
|
+
return view;
|
|
269
|
+
},
|
|
270
|
+
async change(edit: DispatchEdit, checkClient: () => void): Promise<DispatchView> {
|
|
271
|
+
const b = select();
|
|
272
|
+
const expected = edit.expected;
|
|
273
|
+
if (
|
|
274
|
+
!b ||
|
|
275
|
+
!expected ||
|
|
276
|
+
b.scope.projectId !== expected.projectId ||
|
|
277
|
+
b.scope.taskId !== expected.taskId ||
|
|
278
|
+
b.scope.revision !== expected.selectionRevision ||
|
|
279
|
+
!Number.isSafeInteger(expected.revision)
|
|
280
|
+
)
|
|
281
|
+
throw Error("Selected task changed. Inspect the current task before applying this control.");
|
|
282
|
+
if (b.store.taskId !== b.scope.taskId) throw Error("Select the parent task to change shared dispatch controls.");
|
|
283
|
+
if (!isDispatchEdit(edit)) throw Error("Invalid task control");
|
|
284
|
+
await b.assertCurrent();
|
|
285
|
+
checkClient();
|
|
286
|
+
// The operator acts on the state displayed. Reconciliation itself is safe to repeat.
|
|
287
|
+
if (edit.action === "resume") {
|
|
288
|
+
for (const attempt of b.store.outstanding()) {
|
|
289
|
+
if (attempt.owner !== "pi-daddy") continue;
|
|
290
|
+
const state = await readRuntimeExecutionSettlement(
|
|
291
|
+
directory(),
|
|
292
|
+
attempt.sessionId,
|
|
293
|
+
attempt.cwd,
|
|
294
|
+
attempt.executionId,
|
|
295
|
+
attempt.beforeLaunch,
|
|
296
|
+
).catch(() => "unknown" as const);
|
|
297
|
+
if (state !== "unknown")
|
|
298
|
+
b.store.settle("pi-daddy", attempt.executionId, state, attempt.taskId ?? b.store.taskId);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
await b.assertCurrent();
|
|
302
|
+
checkClient();
|
|
303
|
+
lastDisplay = undefined;
|
|
304
|
+
b.store.change(edit.action, edit.limit ?? null, expected.revision, edit.owner);
|
|
305
|
+
return b.store.snapshot(b.scope.revision);
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
const unsubscribe = bus?.on?.("pi.task-dispatch.v1.bind", (raw: unknown) => {
|
|
309
|
+
const req = raw as {
|
|
310
|
+
version?: unknown;
|
|
311
|
+
owner?: unknown;
|
|
312
|
+
cwd?: unknown;
|
|
313
|
+
sessionId?: unknown;
|
|
314
|
+
accept?: (admit: unknown) => void;
|
|
315
|
+
};
|
|
316
|
+
if (
|
|
317
|
+
!req ||
|
|
318
|
+
req.version !== 1 ||
|
|
319
|
+
!["scout", "harness", "jev"].includes(String(req.owner)) ||
|
|
320
|
+
typeof req.cwd !== "string" ||
|
|
321
|
+
typeof req.sessionId !== "string" ||
|
|
322
|
+
typeof req.accept !== "function"
|
|
323
|
+
)
|
|
324
|
+
return;
|
|
325
|
+
const admit = controller.capture(req.owner as DispatchOwner, { cwd: req.cwd, sessionId: req.sessionId });
|
|
326
|
+
if (!admit) return;
|
|
327
|
+
req.accept(
|
|
328
|
+
Object.assign(
|
|
329
|
+
async (input: { operationId: string }) => {
|
|
330
|
+
if (!input || typeof input.operationId !== "string") throw Error("Missing native attempt id");
|
|
331
|
+
return admit(input.operationId);
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
reconcile: async (input: { operationId: string; cleanup: "settled" | "not-started" }) => {
|
|
335
|
+
if (!input || typeof input.operationId !== "string") throw Error("Missing native attempt id");
|
|
336
|
+
await admit.reconcile(input.operationId, input.cleanup);
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
),
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
const stopReplay = bus?.on?.("pi.task-history.v1.replay", (raw: unknown) => {
|
|
343
|
+
const req = raw as {
|
|
344
|
+
version?: unknown;
|
|
345
|
+
owner?: unknown;
|
|
346
|
+
cwd?: unknown;
|
|
347
|
+
sessionId?: unknown;
|
|
348
|
+
projectId?: unknown;
|
|
349
|
+
taskId?: unknown;
|
|
350
|
+
after?: unknown;
|
|
351
|
+
limit?: unknown;
|
|
352
|
+
accept?: (read: () => Promise<ReturnType<TaskDispatch["page"]>>) => void;
|
|
353
|
+
};
|
|
354
|
+
const host = current();
|
|
355
|
+
if (
|
|
356
|
+
!req ||
|
|
357
|
+
!host ||
|
|
358
|
+
req.version !== 1 ||
|
|
359
|
+
req.owner !== "pi-daddy" ||
|
|
360
|
+
req.cwd !== host.cwd ||
|
|
361
|
+
req.sessionId !== host.sessionId ||
|
|
362
|
+
typeof req.accept !== "function" ||
|
|
363
|
+
typeof req.after !== "number" ||
|
|
364
|
+
typeof req.limit !== "number"
|
|
365
|
+
)
|
|
366
|
+
return;
|
|
367
|
+
const binding = select();
|
|
368
|
+
if (!binding || req.projectId !== binding.scope.projectId || req.taskId !== binding.scope.taskId) return;
|
|
369
|
+
const after = req.after,
|
|
370
|
+
limit = req.limit;
|
|
371
|
+
req.accept(async () => {
|
|
372
|
+
await binding.assertCurrent();
|
|
373
|
+
const page = binding.store.page(after, limit, binding.scope.taskId);
|
|
374
|
+
await binding.assertCurrent();
|
|
375
|
+
return page;
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
return Object.assign(controller, {
|
|
379
|
+
close: () => {
|
|
380
|
+
unsubscribe?.();
|
|
381
|
+
stopReplay?.();
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const inlineWorkTools = new Set(["write", "edit", "bash", "apply_patch"]);
|
|
2
|
+
|
|
3
|
+
type InlineSession = {
|
|
4
|
+
depth: number;
|
|
5
|
+
ownExecutionId?: string;
|
|
6
|
+
taskDispatch?: { captureInline(): (() => Promise<void>) | undefined };
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** Check Pi's root tool call, immediately before its built-in effect may start. */
|
|
10
|
+
export async function taskInlineControl(toolName: string, session: InlineSession) {
|
|
11
|
+
if (!inlineWorkTools.has(toolName) || session.depth !== 0 || session.ownExecutionId) return;
|
|
12
|
+
try {
|
|
13
|
+
await session.taskDispatch?.captureInline()?.();
|
|
14
|
+
} catch (error) {
|
|
15
|
+
const reason =
|
|
16
|
+
error instanceof Error && error.message === "Task dispatch is stopped. Inspect task controls before resuming."
|
|
17
|
+
? "This task is paused or cancelled. Open task controls to resume when ready."
|
|
18
|
+
: "The selected task could not be verified. Reopen its history before starting new work.";
|
|
19
|
+
return { block: true, reason };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Serialize opted-in writers by actual working copy. This never creates a grant or changes a worker's cwd. */
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { lstat, stat, realpath } from "node:fs/promises";
|
|
4
|
+
import { TaskDispatch } from "../src/governance/task-dispatch.ts";
|
|
5
|
+
import { readRuntimeExecutionSettlement, type PrelaunchOwner } from "../src/governance/runtime-settlement.ts";
|
|
6
|
+
type Host = { cwd: string; sessionId: string };
|
|
7
|
+
|
|
8
|
+
async function workingCopyId(cwd: string) {
|
|
9
|
+
const requested = await realpath(cwd);
|
|
10
|
+
let root = requested;
|
|
11
|
+
for (let cursor = requested; ; cursor = dirname(cursor)) {
|
|
12
|
+
try {
|
|
13
|
+
const marker = await lstat(join(cursor, ".git"));
|
|
14
|
+
if (!marker.isFile() && !marker.isDirectory()) throw Error("Cannot identify the Git working copy");
|
|
15
|
+
root = cursor;
|
|
16
|
+
break;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if ((error as NodeJS.ErrnoException)?.code !== "ENOENT") throw error;
|
|
19
|
+
}
|
|
20
|
+
if (dirname(cursor) === cursor) break;
|
|
21
|
+
}
|
|
22
|
+
const identity = await stat(root, { bigint: true });
|
|
23
|
+
if (!identity.isDirectory()) throw Error("Working copy is unavailable");
|
|
24
|
+
return `${identity.dev}.${identity.ino}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function claimTaskWriter(
|
|
28
|
+
directory: string,
|
|
29
|
+
target: string,
|
|
30
|
+
host: Host,
|
|
31
|
+
executionId: string,
|
|
32
|
+
beforeLaunch?: PrelaunchOwner,
|
|
33
|
+
) {
|
|
34
|
+
const id = await workingCopyId(target);
|
|
35
|
+
const store = new TaskDispatch(directory, "working-copy", id, true);
|
|
36
|
+
for (const previous of store.outstanding()) {
|
|
37
|
+
const receipt = await readRuntimeExecutionSettlement(
|
|
38
|
+
directory,
|
|
39
|
+
previous.sessionId,
|
|
40
|
+
previous.cwd,
|
|
41
|
+
previous.executionId,
|
|
42
|
+
previous.beforeLaunch,
|
|
43
|
+
).catch(() => "unknown" as const);
|
|
44
|
+
if (receipt !== "unknown") store.settle(previous.owner, previous.executionId, receipt);
|
|
45
|
+
}
|
|
46
|
+
store.admit({ owner: "pi-daddy", executionId, ...host, ...(beforeLaunch ? { beforeLaunch } : {}) });
|
|
47
|
+
const finish = (cleanup: "settled" | "not-started") => store.settle("pi-daddy", executionId, cleanup);
|
|
48
|
+
try {
|
|
49
|
+
if (id !== (await workingCopyId(target))) throw Error("Working copy changed during admission");
|
|
50
|
+
} catch (error) {
|
|
51
|
+
finish("not-started");
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
return finish;
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-daddy",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.63.0",
|
|
4
4
|
"description": "Capability governance for pi sub-agents: spawn Agent Skills (SKILL.md) definitions whose allowed-tools becomes a grant that can only narrow going down a delegation tree, enforced by pi's own --tools allowlist, with an append-only ledger.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
unlinkSync,
|
|
14
14
|
constants,
|
|
15
15
|
readFileSync,
|
|
16
|
+
readlinkSync,
|
|
16
17
|
} from "node:fs";
|
|
17
18
|
import { join } from "node:path";
|
|
18
19
|
import { readBoundedTextSync } from "../kernel/bounded-read-sync.ts";
|
|
@@ -56,6 +57,42 @@ function ownerAlive(id: string): boolean {
|
|
|
56
57
|
throw error;
|
|
57
58
|
}
|
|
58
59
|
}
|
|
60
|
+
/** A local owner receipt for reservations made before native begin() is permitted to run. */
|
|
61
|
+
export interface PrelaunchOwner {
|
|
62
|
+
ownerId: string;
|
|
63
|
+
ownerScope: string;
|
|
64
|
+
pidNamespace: string;
|
|
65
|
+
}
|
|
66
|
+
export function pinPrelaunchOwner(runtime: RuntimeSettlement, sessionId: string, cwd: string): PrelaunchOwner {
|
|
67
|
+
if (runtime.ownerId !== ownerId() || runtime.sessionId !== sessionId || runtime.cwd !== cwd)
|
|
68
|
+
throw Error("Native execution owner changed before task admission");
|
|
69
|
+
return { ownerId: runtime.ownerId, ownerScope: runtime.ownerScope, pidNamespace: readlinkSync("/proc/self/ns/pid") };
|
|
70
|
+
}
|
|
71
|
+
function prelaunchOwnerGone(pin: PrelaunchOwner, journal: Journal): boolean {
|
|
72
|
+
const match = /^([0-9a-f-]{36}):([1-9][0-9]*):([0-9]+)$/.exec(pin.ownerId);
|
|
73
|
+
if (
|
|
74
|
+
!match ||
|
|
75
|
+
pin.ownerScope !== journal.ownerScope ||
|
|
76
|
+
pin.pidNamespace !== readlinkSync("/proc/self/ns/pid") ||
|
|
77
|
+
match[1] !== readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim()
|
|
78
|
+
)
|
|
79
|
+
return false;
|
|
80
|
+
// Identical /proc view, boot and birth ticks distinguish PID reuse. An inaccessible process or changed
|
|
81
|
+
// namespace is not death. If the proc entry vanished, ask the kernel for positive ESRCH evidence too.
|
|
82
|
+
try {
|
|
83
|
+
const text = readFileSync("/proc/" + match[2] + "/stat", "utf8");
|
|
84
|
+
const ticks = text.slice(text.lastIndexOf(")") + 2).split(" ")[19];
|
|
85
|
+
return /^\d+$/.test(ticks ?? "") && ticks !== match[3];
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") return false;
|
|
88
|
+
try {
|
|
89
|
+
process.kill(Number(match[2]), 0);
|
|
90
|
+
return false;
|
|
91
|
+
} catch (probe) {
|
|
92
|
+
return (probe as NodeJS.ErrnoException).code === "ESRCH";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
59
96
|
function validate(value: unknown, sessionId: string, cwd: string): Journal {
|
|
60
97
|
const j = value as Journal;
|
|
61
98
|
if (
|
|
@@ -276,6 +313,7 @@ export async function readRuntimeExecutionSettlement(
|
|
|
276
313
|
sessionId: string,
|
|
277
314
|
cwd: string,
|
|
278
315
|
executionId: string,
|
|
316
|
+
beforeLaunch?: PrelaunchOwner,
|
|
279
317
|
) {
|
|
280
318
|
const root = join(realpathSync(directory), "pi-daddy", "runtime-settlement");
|
|
281
319
|
if (realpathSync(root) !== root) throw Error("runtime settlement root changed");
|
|
@@ -285,10 +323,14 @@ export async function readRuntimeExecutionSettlement(
|
|
|
285
323
|
const path = join(root, settlementHash([sessionId, cwd]) + ".json");
|
|
286
324
|
const text = readBoundedTextSync(path, { maxBytes: MAX_RECORD, confinedRoot: root }).text;
|
|
287
325
|
const journal = validate(JSON.parse(text), sessionId, cwd);
|
|
326
|
+
if (beforeLaunch && beforeLaunch.ownerScope !== journal.ownerScope) return "unknown" as const;
|
|
288
327
|
const e = journal.executions.find((item) => item.id === executionId);
|
|
289
|
-
if (
|
|
328
|
+
if (e?.problem) return "unknown" as const;
|
|
290
329
|
let result: "settled" | "not-started" | "unknown" = "unknown";
|
|
291
|
-
if (
|
|
330
|
+
if (!e) {
|
|
331
|
+
// Only a qualified reservation was fenced to this intact append-preserving runtime journal before launch.
|
|
332
|
+
if (beforeLaunch && prelaunchOwnerGone(beforeLaunch, journal)) result = "not-started";
|
|
333
|
+
} else if (e.state === "not-started" && !e.identity) result = "not-started";
|
|
292
334
|
else if (e.identity) {
|
|
293
335
|
const receipt = await readCapturedWorkerReceipt(e.identity);
|
|
294
336
|
if (receipt && (!e.receiptSha256 || e.receiptSha256 === settlementHash(receipt))) result = "settled";
|