@ftrdotdev/pi-delegates 0.0.0-stage → 0.1.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 +77 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +57 -0
- package/README.md +448 -2
- package/ROADMAP.md +158 -0
- package/TESTING.md +1061 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +93 -4
- package/skills/pi-delegates/SKILL.md +147 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +156 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +161 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1298 -0
- package/src/domain/orchestration/control.ts +436 -0
- package/src/domain/orchestration/fanout.test.ts +419 -0
- package/src/domain/orchestration/fanout.ts +334 -0
- package/src/domain/orchestration/gates.test.ts +686 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +173 -0
- package/src/domain/orchestration/launch.ts +581 -0
- package/src/domain/orchestration/requests.test.ts +700 -0
- package/src/domain/orchestration/requests.ts +769 -0
- package/src/domain/orchestration/review.test.ts +214 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +262 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1425 -0
- package/src/domain/orchestration/workflow.ts +705 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +527 -0
- package/src/domain/shapes.test.ts +165 -0
- package/src/domain/skills.test.ts +466 -0
- package/src/domain/skills.ts +97 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +201 -0
- package/src/domain/toolkit.ts +379 -0
- package/src/domain/view.test.ts +739 -0
- package/src/domain/view.ts +459 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +481 -0
- package/src/extension/commands.ts +227 -0
- package/src/extension/entry.test.ts +1118 -0
- package/src/extension/entry.ts +342 -0
- package/src/extension/gate-alerts.test.ts +235 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1191 -0
- package/src/extension/inspector-command.ts +638 -0
- package/src/extension/prompt.test.ts +110 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/shortcut.test.ts +64 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +993 -0
- package/src/extension/tools.ts +505 -0
- package/src/extension/version.test.ts +123 -0
- package/src/extension/version.ts +97 -0
- package/src/harness-policy.test.ts +74 -0
- package/src/package-manifest.test.ts +157 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +387 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +361 -0
- package/src/presentation/inspector.test.ts +679 -0
- package/src/presentation/inspector.ts +440 -0
- package/src/presentation/overlay.test.ts +555 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +329 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +530 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/strip.test.ts +196 -0
- package/src/presentation/strip.ts +74 -0
- package/src/presentation/wording.test.ts +238 -0
- package/src/presentation/wording.ts +222 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +134 -0
- package/src/runtime/config.test.ts +44 -0
- package/src/runtime/config.ts +87 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +35 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +140 -0
- package/src/runtime/log.test.ts +128 -0
- package/src/runtime/log.ts +68 -0
- package/src/runtime/mcp.test.ts +1453 -0
- package/src/runtime/mcp.ts +938 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/session.test.ts +359 -0
- package/src/runtime/session.ts +425 -0
- package/src/runtime/timers.test.ts +31 -0
- package/src/runtime/timers.ts +10 -0
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import { MemoryStorage, type TaskGraph } from "@earendil-works/pi-durable";
|
|
2
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
3
|
+
import { mkdtemp } from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { assert, it } from "@effect/vitest";
|
|
7
|
+
import { stubHostSource } from "../../test-support/stub-model-source.ts";
|
|
8
|
+
import { waitFor } from "../../test-support/wait.ts";
|
|
9
|
+
import { createCatalogue, defineAgent } from "../domain/definitions.ts";
|
|
10
|
+
import { CODING_EXTENSION } from "../domain/names.ts";
|
|
11
|
+
import { control } from "../domain/orchestration/control.ts";
|
|
12
|
+
import { GateDoc } from "../domain/orchestration/gates.ts";
|
|
13
|
+
import { launch, type HostDefaults } from "../domain/orchestration/launch.ts";
|
|
14
|
+
import { RunDoc, RunIndexDoc } from "../domain/run-records.ts";
|
|
15
|
+
import { viewRun } from "../domain/view.ts";
|
|
16
|
+
import { HostModelAdapter } from "../runtime/models.ts";
|
|
17
|
+
import { observeFamilyDoc, observeSessionDoc, observeTaskGraph } from "../runtime/observe.ts";
|
|
18
|
+
import { openSessionRuntime, type SessionRuntime } from "../runtime/session.ts";
|
|
19
|
+
import { watchActivity, type RunActivity } from "./activity.ts";
|
|
20
|
+
|
|
21
|
+
const host: HostDefaults = {
|
|
22
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
23
|
+
thinkingLevel: "low",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const deployer = defineAgent({
|
|
27
|
+
name: "deployer",
|
|
28
|
+
role: "Deploy agent",
|
|
29
|
+
prompt: { role: "You deploy things." },
|
|
30
|
+
tools: ["bash"],
|
|
31
|
+
extensions: [CODING_EXTENSION],
|
|
32
|
+
approval: { gates: [{ name: "deploy", tools: ["bash"], match: "deploy" }] },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const catalogue = createCatalogue([deployer]);
|
|
36
|
+
|
|
37
|
+
const openRuntime = async (): Promise<SessionRuntime> => {
|
|
38
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-activity-"));
|
|
39
|
+
return openSessionRuntime({
|
|
40
|
+
identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
41
|
+
models: new HostModelAdapter(
|
|
42
|
+
stubHostSource({
|
|
43
|
+
text: "deployed",
|
|
44
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
45
|
+
}),
|
|
46
|
+
),
|
|
47
|
+
storage: new MemoryStorage(),
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
interface ParkedRun {
|
|
52
|
+
readonly runtime: SessionRuntime;
|
|
53
|
+
readonly runId: string;
|
|
54
|
+
readonly childId: number;
|
|
55
|
+
readonly taskId: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The shared Arrange: a real runtime, a gated run, and the ids a watch needs. */
|
|
59
|
+
const parkedRun = async (): Promise<ParkedRun> => {
|
|
60
|
+
const runtime = await openRuntime();
|
|
61
|
+
const result = await launch(
|
|
62
|
+
{ kind: "single", agent: "deployer", task: "deploy it" },
|
|
63
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
64
|
+
);
|
|
65
|
+
const doc = await runtime.snapshot(RunDoc, result.view.runId);
|
|
66
|
+
assert.ok(doc?.childConversationId !== null && doc?.childConversationId !== undefined);
|
|
67
|
+
assert.ok(doc?.taskId !== null && doc?.taskId !== undefined);
|
|
68
|
+
return {
|
|
69
|
+
runtime,
|
|
70
|
+
runId: result.view.runId,
|
|
71
|
+
childId: doc.childConversationId,
|
|
72
|
+
taskId: doc.taskId,
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const approve = async (run: ParkedRun): Promise<void> => {
|
|
77
|
+
await control(
|
|
78
|
+
{ action: "approve", run: run.runId, gate: "deploy" },
|
|
79
|
+
{ runtime: run.runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
it("a parked tool call reports its name and slot status as activity", async () => {
|
|
84
|
+
const run = await parkedRun();
|
|
85
|
+
try {
|
|
86
|
+
const activities: RunActivity[] = [];
|
|
87
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
88
|
+
const first = activities[0];
|
|
89
|
+
assert.ok(first !== undefined, "the watch must report the value at attach");
|
|
90
|
+
assert.strictEqual(first.tool, "bash");
|
|
91
|
+
assert.strictEqual(first.slot, "sleeping");
|
|
92
|
+
assert.strictEqual(first.turns, 1);
|
|
93
|
+
assert.strictEqual(typeof first.at, "number");
|
|
94
|
+
} finally {
|
|
95
|
+
await run.runtime.close();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("activity arrives when the call resumes", async () => {
|
|
100
|
+
const run = await parkedRun();
|
|
101
|
+
const activities: RunActivity[] = [];
|
|
102
|
+
try {
|
|
103
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
104
|
+
await approve(run);
|
|
105
|
+
await waitFor(
|
|
106
|
+
() => activities.some((activity) => activity.slot === "done"),
|
|
107
|
+
"the resumed call reported done",
|
|
108
|
+
);
|
|
109
|
+
const slots = activities
|
|
110
|
+
.filter((activity) => activity.tool === "bash")
|
|
111
|
+
.map((activity) => activity.slot)
|
|
112
|
+
// The live document advances more than once per transition (output, then
|
|
113
|
+
// status); consecutive repeats are one visible change.
|
|
114
|
+
.filter((slot, index, all) => index === 0 || slot !== all[index - 1]);
|
|
115
|
+
assert.deepStrictEqual(slots, ["sleeping", "running", "done"]);
|
|
116
|
+
assert.strictEqual(slots.at(-1), "done");
|
|
117
|
+
} finally {
|
|
118
|
+
await run.runtime.close();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("activity never carries transcript text", async () => {
|
|
123
|
+
const run = await parkedRun();
|
|
124
|
+
const activities: RunActivity[] = [];
|
|
125
|
+
try {
|
|
126
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
127
|
+
await approve(run);
|
|
128
|
+
for (const activity of activities) {
|
|
129
|
+
for (const key of ["output", "details", "message", "answer"]) {
|
|
130
|
+
assert.ok(!(key in activity), `activity owns ${key}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const serialized = JSON.stringify(activities);
|
|
134
|
+
assert.ok(!serialized.includes("echo deploy"), serialized);
|
|
135
|
+
assert.ok(!serialized.includes("deployed"), serialized);
|
|
136
|
+
} finally {
|
|
137
|
+
await run.runtime.close();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("detaching stops the watch", async () => {
|
|
142
|
+
const run = await parkedRun();
|
|
143
|
+
const activities: RunActivity[] = [];
|
|
144
|
+
const ends: string[] = [];
|
|
145
|
+
try {
|
|
146
|
+
const detach = await watchActivity(
|
|
147
|
+
run.runtime,
|
|
148
|
+
run.childId,
|
|
149
|
+
(activity) => activities.push(activity),
|
|
150
|
+
(kind) => ends.push(kind),
|
|
151
|
+
);
|
|
152
|
+
detach();
|
|
153
|
+
const before = activities.length;
|
|
154
|
+
assert.doesNotThrow(() => detach());
|
|
155
|
+
await approve(run);
|
|
156
|
+
await waitFor(
|
|
157
|
+
async () => (await viewRun(run.runId, run.runtime)).status === "answered",
|
|
158
|
+
"the run settled after approval",
|
|
159
|
+
);
|
|
160
|
+
assert.strictEqual(activities.length, before);
|
|
161
|
+
assert.deepStrictEqual(ends, []);
|
|
162
|
+
} finally {
|
|
163
|
+
await run.runtime.close();
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("the watch ends once when the runtime closes", async () => {
|
|
168
|
+
const run = await parkedRun();
|
|
169
|
+
const activities: RunActivity[] = [];
|
|
170
|
+
const ends: { readonly kind: string; readonly reason?: string }[] = [];
|
|
171
|
+
let atEnd = 0;
|
|
172
|
+
try {
|
|
173
|
+
await watchActivity(
|
|
174
|
+
run.runtime,
|
|
175
|
+
run.childId,
|
|
176
|
+
(activity) => activities.push(activity),
|
|
177
|
+
(kind, reason) => {
|
|
178
|
+
atEnd = activities.length;
|
|
179
|
+
ends.push({ kind, ...(reason === undefined ? {} : { reason }) });
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
await run.runtime.close();
|
|
183
|
+
await waitFor(() => ends.length === 1, "the watch ended once");
|
|
184
|
+
assert.strictEqual(ends[0]?.kind, "failed");
|
|
185
|
+
assert.strictEqual(activities.length, atEnd);
|
|
186
|
+
} finally {
|
|
187
|
+
await run.runtime.close().catch(() => undefined);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("a conversation with no live document can still be watched", async () => {
|
|
192
|
+
const run = await parkedRun();
|
|
193
|
+
try {
|
|
194
|
+
const activities: RunActivity[] = [];
|
|
195
|
+
await watchActivity(run.runtime, run.runtime.root.id as unknown as number, (activity) =>
|
|
196
|
+
activities.push(activity),
|
|
197
|
+
);
|
|
198
|
+
assert.deepStrictEqual(
|
|
199
|
+
{ tool: activities[0]?.tool, slot: activities[0]?.slot, turns: activities[0]?.turns },
|
|
200
|
+
{ tool: null, slot: null, turns: 0 },
|
|
201
|
+
);
|
|
202
|
+
} finally {
|
|
203
|
+
await run.runtime.close();
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
interface Recorded<T> {
|
|
208
|
+
readonly values: Readonly<T>[];
|
|
209
|
+
readonly ends: { readonly kind: "ended" | "failed"; readonly reason?: string }[];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const recorder = <T>(): Recorded<T> & {
|
|
213
|
+
sink: {
|
|
214
|
+
advance: (value: Readonly<T>) => void;
|
|
215
|
+
end: (kind: "ended" | "failed", reason?: string) => void;
|
|
216
|
+
};
|
|
217
|
+
} => {
|
|
218
|
+
const values: Readonly<T>[] = [];
|
|
219
|
+
const ends: { kind: "ended" | "failed"; reason?: string }[] = [];
|
|
220
|
+
return {
|
|
221
|
+
values,
|
|
222
|
+
ends,
|
|
223
|
+
sink: {
|
|
224
|
+
advance: (value) => values.push(value),
|
|
225
|
+
end: (kind, reason) => ends.push({ kind, ...(reason === undefined ? {} : { reason }) }),
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
it("a session-document observation delivers the value at attach", async () => {
|
|
231
|
+
const run = await parkedRun();
|
|
232
|
+
try {
|
|
233
|
+
const recorded = recorder<{ runIds: string[] }>();
|
|
234
|
+
const detach = await observeSessionDoc(run.runtime, RunIndexDoc, recorded.sink);
|
|
235
|
+
assert.ok(recorded.values[0]?.runIds.includes(run.runId));
|
|
236
|
+
assert.strictEqual(typeof detach, "function");
|
|
237
|
+
} finally {
|
|
238
|
+
await run.runtime.close();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("a family observation addresses one run by key", async () => {
|
|
243
|
+
const run = await parkedRun();
|
|
244
|
+
try {
|
|
245
|
+
const recorded = recorder<{ definition: string; childConversationId: number | null }>();
|
|
246
|
+
await observeFamilyDoc(run.runtime, RunDoc, run.runId, recorded.sink);
|
|
247
|
+
assert.strictEqual(recorded.values[0]?.definition, "deployer");
|
|
248
|
+
assert.strictEqual(recorded.values[0]?.childConversationId, run.childId);
|
|
249
|
+
|
|
250
|
+
const absent = recorder<{ definition: string }>();
|
|
251
|
+
const refused = await observeFamilyDoc(run.runtime, RunDoc, "run-absent#1", absent.sink);
|
|
252
|
+
assert.strictEqual(refused, undefined);
|
|
253
|
+
} finally {
|
|
254
|
+
await run.runtime.close();
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("an index observation pushes once per committed change", async () => {
|
|
259
|
+
const run = await parkedRun();
|
|
260
|
+
try {
|
|
261
|
+
const recorded = recorder<{ runIds: string[] }>();
|
|
262
|
+
await observeSessionDoc(run.runtime, RunIndexDoc, recorded.sink);
|
|
263
|
+
const before = recorded.values.length;
|
|
264
|
+
|
|
265
|
+
const second = await launch(
|
|
266
|
+
{ kind: "single", agent: "deployer", task: "deploy again" },
|
|
267
|
+
{ runtime: run.runtime, catalogue, host, waitMs: 100 },
|
|
268
|
+
);
|
|
269
|
+
await waitFor(() => recorded.values.length > before, "the index observation saw the new run");
|
|
270
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
271
|
+
assert.strictEqual(recorded.values.length, before + 1);
|
|
272
|
+
assert.ok(recorded.values.at(-1)?.runIds.includes(second.view.runId));
|
|
273
|
+
|
|
274
|
+
// An unrelated family write at another address must not push the index.
|
|
275
|
+
const afterIndex = recorded.values.length;
|
|
276
|
+
await run.runtime.harness.commit(async (tx) => {
|
|
277
|
+
const gate = await tx.doc(GateDoc, "run:unrelated:deploy", {
|
|
278
|
+
runId: "run:unrelated",
|
|
279
|
+
gate: "deploy",
|
|
280
|
+
});
|
|
281
|
+
gate.status = "pending";
|
|
282
|
+
gate.requestedAt = 1_000;
|
|
283
|
+
}, BACKGROUND_CONTEXT);
|
|
284
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
285
|
+
assert.strictEqual(recorded.values.length, afterIndex);
|
|
286
|
+
} finally {
|
|
287
|
+
await run.runtime.close();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("an absent address is refused, not invented", async () => {
|
|
292
|
+
const run = await parkedRun();
|
|
293
|
+
try {
|
|
294
|
+
const recorded = recorder<{ gate: string }>();
|
|
295
|
+
const detach = await observeFamilyDoc(run.runtime, GateDoc, "absent:none", recorded.sink);
|
|
296
|
+
assert.strictEqual(detach, undefined);
|
|
297
|
+
assert.deepStrictEqual(recorded.values, []);
|
|
298
|
+
} finally {
|
|
299
|
+
await run.runtime.close();
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
const observeGraph = async (
|
|
304
|
+
run: ParkedRun,
|
|
305
|
+
sink: (graph: Readonly<TaskGraph>) => void,
|
|
306
|
+
): Promise<() => void> => observeTaskGraph(run.runtime, { advance: sink, end: () => {} });
|
|
307
|
+
|
|
308
|
+
it("a task-graph observation reports the parked task at attach", async () => {
|
|
309
|
+
const run = await parkedRun();
|
|
310
|
+
try {
|
|
311
|
+
let attached: Readonly<TaskGraph> | undefined;
|
|
312
|
+
await observeGraph(run, (graph) => {
|
|
313
|
+
attached = graph;
|
|
314
|
+
});
|
|
315
|
+
assert.ok(attached !== undefined);
|
|
316
|
+
const node = attached.tasks[String(run.taskId)];
|
|
317
|
+
assert.ok(node !== undefined, "the parked task is in the graph");
|
|
318
|
+
assert.strictEqual(node.state.status, "sleeping");
|
|
319
|
+
// The run task lives in the root conversation and owns the child one.
|
|
320
|
+
assert.strictEqual(node.conversationId, run.runtime.root.id);
|
|
321
|
+
assert.ok(node.conversations.includes(run.childId as never));
|
|
322
|
+
} finally {
|
|
323
|
+
await run.runtime.close();
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("a task-graph observation pushes on the park and on the resume", async () => {
|
|
328
|
+
const run = await parkedRun();
|
|
329
|
+
const graphs: Readonly<TaskGraph>[] = [];
|
|
330
|
+
try {
|
|
331
|
+
await observeGraph(run, (graph) => graphs.push(graph));
|
|
332
|
+
const first = graphs.length;
|
|
333
|
+
await approve(run);
|
|
334
|
+
await waitFor(
|
|
335
|
+
() => graphs.length >= first + 2 && graphs.at(-1)?.tasks[String(run.taskId)] === undefined,
|
|
336
|
+
"the graph dropped the settled task",
|
|
337
|
+
);
|
|
338
|
+
assert.ok(graphs.length >= 3);
|
|
339
|
+
for (let index = 1; index < graphs.length; index += 1) {
|
|
340
|
+
assert.notDeepEqual(graphs[index], graphs[index - 1]);
|
|
341
|
+
}
|
|
342
|
+
assert.strictEqual(graphs.at(-1)?.tasks[String(run.taskId)], undefined);
|
|
343
|
+
} finally {
|
|
344
|
+
await run.runtime.close();
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("a task-graph observation is never absent and ends once when the session closes", async () => {
|
|
349
|
+
const run = await parkedRun();
|
|
350
|
+
const ends: string[] = [];
|
|
351
|
+
let advances = 0;
|
|
352
|
+
try {
|
|
353
|
+
const detach = await observeTaskGraph(run.runtime, {
|
|
354
|
+
advance: () => {
|
|
355
|
+
advances += 1;
|
|
356
|
+
},
|
|
357
|
+
end: (kind) => ends.push(kind),
|
|
358
|
+
});
|
|
359
|
+
assert.strictEqual(typeof detach, "function");
|
|
360
|
+
const before = advances;
|
|
361
|
+
await run.runtime.close();
|
|
362
|
+
await waitFor(() => ends.length === 1, "the graph observation ended once");
|
|
363
|
+
assert.strictEqual(ends[0], "failed");
|
|
364
|
+
assert.strictEqual(advances, before);
|
|
365
|
+
} finally {
|
|
366
|
+
await run.runtime.close().catch(() => undefined);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("an observation carries no transcript text", async () => {
|
|
371
|
+
const run = await parkedRun();
|
|
372
|
+
const activities: RunActivity[] = [];
|
|
373
|
+
const values: unknown[] = [];
|
|
374
|
+
try {
|
|
375
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
376
|
+
await observeFamilyDoc(run.runtime, RunDoc, run.runId, {
|
|
377
|
+
advance: (value) => values.push(value),
|
|
378
|
+
end: () => {},
|
|
379
|
+
});
|
|
380
|
+
await approve(run);
|
|
381
|
+
const serialized = JSON.stringify({ activities, values });
|
|
382
|
+
assert.ok(!serialized.includes("echo deploy"), serialized);
|
|
383
|
+
assert.ok(!serialized.includes("deployed"), serialized);
|
|
384
|
+
} finally {
|
|
385
|
+
await run.runtime.close();
|
|
386
|
+
}
|
|
387
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { LiveDoc, type LiveState, type ToolSlot, type WatchEnd } from "@earendil-works/pi-durable";
|
|
2
|
+
import type { DocEndKind } from "../runtime/observe.ts";
|
|
3
|
+
import { endOf } from "../runtime/observe.ts";
|
|
4
|
+
import type { SessionRuntime } from "../runtime/session.ts";
|
|
5
|
+
import { conversationId as toConversationId } from "../domain/ids.ts";
|
|
6
|
+
|
|
7
|
+
export type SlotStatus = ToolSlot["status"];
|
|
8
|
+
|
|
9
|
+
export interface RunActivity {
|
|
10
|
+
readonly tool: string | null;
|
|
11
|
+
readonly slot: SlotStatus | null;
|
|
12
|
+
readonly turns: number;
|
|
13
|
+
/** The runtime clock at the arrival; presentations derive an age from it. */
|
|
14
|
+
readonly at: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Activity is name, slot and turn count only. The live document also carries
|
|
19
|
+
* `tools[].output`, `tools[].details` and `generation.message`; none of them
|
|
20
|
+
* may reach this projection, so a surface can never show a transcript.
|
|
21
|
+
*/
|
|
22
|
+
export const activityOf = (live: LiveState | undefined, at: number): RunActivity => {
|
|
23
|
+
const slot = live?.tools?.at(-1);
|
|
24
|
+
return {
|
|
25
|
+
tool: slot?.name ?? null,
|
|
26
|
+
slot: slot?.status ?? null,
|
|
27
|
+
turns: live?.run?.turns ?? 0,
|
|
28
|
+
at,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Attach one per-child activity watch. The sink sees the current activity at
|
|
34
|
+
* attach, then one activity per live-document change. Returns an idempotent
|
|
35
|
+
* detach; the watch ends once when the runtime closes.
|
|
36
|
+
*/
|
|
37
|
+
export const watchActivity = async (
|
|
38
|
+
runtime: SessionRuntime,
|
|
39
|
+
conversationId: number,
|
|
40
|
+
onActivity: (activity: RunActivity) => void,
|
|
41
|
+
onEnd?: (kind: DocEndKind, reason?: string) => void,
|
|
42
|
+
): Promise<() => void> => {
|
|
43
|
+
let advanced = false;
|
|
44
|
+
let ended = false;
|
|
45
|
+
const observer = {
|
|
46
|
+
advance(
|
|
47
|
+
call: { readonly kind: "value"; readonly value: LiveState } | { readonly kind: "failed" },
|
|
48
|
+
) {
|
|
49
|
+
if (call.kind !== "value" || ended) return;
|
|
50
|
+
advanced = true;
|
|
51
|
+
onActivity(activityOf(call.value, runtime.now()));
|
|
52
|
+
},
|
|
53
|
+
end(end: WatchEnd) {
|
|
54
|
+
if (ended) return;
|
|
55
|
+
ended = true;
|
|
56
|
+
const folded = endOf(end);
|
|
57
|
+
onEnd?.(folded.kind, folded.reason);
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
const observation = await runtime.run((context) =>
|
|
61
|
+
runtime.harness.observeDoc(LiveDoc, toConversationId(conversationId), observer, context),
|
|
62
|
+
);
|
|
63
|
+
if (observation === undefined) return () => {};
|
|
64
|
+
if (!advanced && !ended) onActivity(activityOf(observation.value, runtime.now()));
|
|
65
|
+
return () => observation.detach();
|
|
66
|
+
};
|