@ftrdotdev/pi-delegates 0.0.0-stage → 0.2.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 +128 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +130 -0
- package/README.md +456 -2
- package/ROADMAP.md +179 -0
- package/TESTING.md +896 -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 +94 -4
- package/skills/pi-delegates/SKILL.md +176 -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 +184 -0
- package/src/docs-contract.test.ts +106 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +163 -0
- package/src/domain/failure.ts +33 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/model-source.ts +30 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1332 -0
- package/src/domain/orchestration/control.ts +457 -0
- package/src/domain/orchestration/fanout.test.ts +524 -0
- package/src/domain/orchestration/fanout.ts +372 -0
- package/src/domain/orchestration/gates.test.ts +685 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +372 -0
- package/src/domain/orchestration/launch.ts +691 -0
- package/src/domain/orchestration/requests.test.ts +695 -0
- package/src/domain/orchestration/requests.ts +782 -0
- package/src/domain/orchestration/review.test.ts +292 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +265 -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 +1566 -0
- package/src/domain/orchestration/workflow.ts +778 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +689 -0
- package/src/domain/settlement.test.ts +186 -0
- package/src/domain/settlement.ts +137 -0
- package/src/domain/shapes.test.ts +185 -0
- package/src/domain/skills.test.ts +477 -0
- package/src/domain/skills.ts +114 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +215 -0
- package/src/domain/toolkit.ts +374 -0
- package/src/domain/turn-budget.test.ts +37 -0
- package/src/domain/turn-budget.ts +23 -0
- package/src/domain/view.test.ts +803 -0
- package/src/domain/view.ts +491 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +485 -0
- package/src/extension/commands.ts +230 -0
- package/src/extension/entry.test.ts +1276 -0
- package/src/extension/entry.ts +380 -0
- package/src/extension/gate-alerts.test.ts +237 -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 +1343 -0
- package/src/extension/inspector-command.ts +685 -0
- package/src/extension/prompt.test.ts +108 -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/retry-observer.test.ts +292 -0
- package/src/extension/retry-observer.ts +150 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/settle-alerts.test.ts +579 -0
- package/src/extension/settle-alerts.ts +175 -0
- package/src/extension/shortcut.test.ts +62 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +1294 -0
- package/src/extension/tools.ts +553 -0
- package/src/extension/version.test.ts +115 -0
- package/src/extension/version.ts +97 -0
- package/src/goldens.test.ts +294 -0
- package/src/harness-policy.test.ts +76 -0
- package/src/layers.test.ts +309 -0
- package/src/layers.ts +405 -0
- package/src/package-manifest.test.ts +170 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +386 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +359 -0
- package/src/presentation/inspector.test.ts +682 -0
- package/src/presentation/inspector.ts +442 -0
- package/src/presentation/overlay.test.ts +553 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +365 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +535 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/status.test.ts +43 -0
- package/src/presentation/status.ts +30 -0
- package/src/presentation/strip.test.ts +203 -0
- package/src/presentation/strip.ts +67 -0
- package/src/presentation/wording.test.ts +388 -0
- package/src/presentation/wording.ts +294 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +128 -0
- package/src/runtime/clock.test.ts +31 -0
- package/src/runtime/clock.ts +31 -0
- package/src/runtime/config.test.ts +63 -0
- package/src/runtime/config.ts +92 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +33 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +152 -0
- package/src/runtime/handle.ts +54 -0
- package/src/runtime/log.test.ts +140 -0
- package/src/runtime/log.ts +91 -0
- package/src/runtime/mcp.test.ts +1450 -0
- package/src/runtime/mcp.ts +1005 -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/runtime-state.test.ts +25 -0
- package/src/runtime/runtime-state.ts +36 -0
- package/src/runtime/session-tag.ts +11 -0
- package/src/runtime/session.test.ts +352 -0
- package/src/runtime/session.ts +263 -0
- package/src/runtime/storage.ts +29 -0
- package/src/test-hygiene.test.ts +97 -0
|
@@ -0,0 +1,1276 @@
|
|
|
1
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
2
|
+
import { assert, afterEach, it } from "@effect/vitest";
|
|
3
|
+
import { join, makeDir, makeTempDir, readText, writeText } from "../../test-support/platform.ts";
|
|
4
|
+
import { Ref } from "effect";
|
|
5
|
+
import { rejects, throws } from "../../test-support/assertions.ts";
|
|
6
|
+
import { GateDoc, GateIndexDoc, gateKey } from "../domain/orchestration/gates.ts";
|
|
7
|
+
import { RunIndexDoc } from "../domain/run-records.ts";
|
|
8
|
+
import { HostModelAdapter } from "../runtime/models.ts";
|
|
9
|
+
import { waitFor } from "../../test-support/wait.ts";
|
|
10
|
+
import { openReadOnlyRuntime, type SessionRuntime } from "../runtime/session.ts";
|
|
11
|
+
|
|
12
|
+
import type { LogLevel, LogSink } from "../runtime/log.ts";
|
|
13
|
+
import { stubHostSource } from "../../test-support/stub-model-source.ts";
|
|
14
|
+
import { makeStubPi } from "../../test-support/stub-pi.ts";
|
|
15
|
+
import type { StubPi } from "../../test-support/stub-pi.ts";
|
|
16
|
+
import { fakeObservations } from "../../test-support/fake-observations.ts";
|
|
17
|
+
import { inspectorClock } from "../../test-support/inspector-clock.ts";
|
|
18
|
+
import { registerDelegatesCommand } from "./commands.ts";
|
|
19
|
+
import { createDelegatesExtension } from "./entry.ts";
|
|
20
|
+
import { ACTIVATION_ENTRY } from "./inspector-command.ts";
|
|
21
|
+
import type { SessionView } from "../domain/view.ts";
|
|
22
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
23
|
+
import { defineAgent } from "../domain/definitions.ts";
|
|
24
|
+
import { CODING_EXTENSION } from "../domain/names.ts";
|
|
25
|
+
import { openTestSession } from "../../test-support/seams.ts";
|
|
26
|
+
import type { SessionHandle } from "../runtime/handle.ts";
|
|
27
|
+
import { handleFor } from "../../test-support/session-handle.ts";
|
|
28
|
+
|
|
29
|
+
/** A scratch agent directory, so a session start never scans the developer's `~/.pi/agent`. */
|
|
30
|
+
const tempAgentDir = (): Promise<string> => makeTempDir("delegates-agent-");
|
|
31
|
+
|
|
32
|
+
it("a first-start conflict registers only the commands", async () => {
|
|
33
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
34
|
+
const stub = makeStubPi({
|
|
35
|
+
sessionId: "s-1",
|
|
36
|
+
sessionDir,
|
|
37
|
+
activeTools: ["read", "delegate_launch", "delegate_control", "delegate_status", "bash"],
|
|
38
|
+
tools: [{ name: "delegate_launch", source: "npm:pi-subagents" }],
|
|
39
|
+
});
|
|
40
|
+
let opened = 0;
|
|
41
|
+
createDelegatesExtension({
|
|
42
|
+
agentDir: await tempAgentDir(),
|
|
43
|
+
onRuntime: () => {
|
|
44
|
+
opened += 1;
|
|
45
|
+
},
|
|
46
|
+
})(stub.api);
|
|
47
|
+
|
|
48
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
49
|
+
|
|
50
|
+
assert.deepStrictEqual(stub.registrations.tools, []);
|
|
51
|
+
assert.deepStrictEqual(stub.registrations.commands, [
|
|
52
|
+
"delegates",
|
|
53
|
+
"delegates:list",
|
|
54
|
+
"delegates:answer",
|
|
55
|
+
]);
|
|
56
|
+
assert.ok(stub.registrations.handlers.has("before_agent_start"));
|
|
57
|
+
assert.deepStrictEqual(stub.activeTools, ["read", "bash"]);
|
|
58
|
+
assert.strictEqual(stub.calls.setActiveTools, 1);
|
|
59
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
60
|
+
assert.strictEqual(stub.notices[0]?.severity, "error");
|
|
61
|
+
assert.ok(stub.notices[0]?.message.includes("pi remove npm:pi-subagents"));
|
|
62
|
+
assert.ok(stub.notices[0]?.message.includes("resume"));
|
|
63
|
+
assert.strictEqual(opened, 0);
|
|
64
|
+
await stub.fire("before_agent_start", { type: "before_agent_start", prompt: "hello" });
|
|
65
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("a conflict that registers after the start is refused before the first turn", async () => {
|
|
69
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
70
|
+
const stub = makeStubPi({
|
|
71
|
+
sessionId: "s-1",
|
|
72
|
+
sessionDir,
|
|
73
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
74
|
+
});
|
|
75
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
76
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
77
|
+
assert.ok(stub.registrations.tools.includes("delegate_launch"));
|
|
78
|
+
|
|
79
|
+
// A discovered package loads after the explicit extension, so its tools land late.
|
|
80
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
81
|
+
await stub.fire("before_agent_start", { type: "before_agent_start", prompt: "hello" });
|
|
82
|
+
|
|
83
|
+
assert.ok(!stub.activeTools.includes("delegate_launch"));
|
|
84
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
85
|
+
assert.strictEqual(stub.notices[0]?.severity, "error");
|
|
86
|
+
assert.ok(stub.notices[0]?.message.includes("pi remove npm:pi-subagents"));
|
|
87
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("a later start re-filters without a second notice", async () => {
|
|
91
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
92
|
+
const stub = makeStubPi({
|
|
93
|
+
sessionId: "s-1",
|
|
94
|
+
sessionDir,
|
|
95
|
+
activeTools: ["read", "delegate_launch"],
|
|
96
|
+
tools: [{ name: "delegate_launch", source: "npm:pi-subagents" }],
|
|
97
|
+
});
|
|
98
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
99
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
100
|
+
|
|
101
|
+
stub.restoreRegisteredTools();
|
|
102
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
103
|
+
|
|
104
|
+
assert.strictEqual(stub.calls.setActiveTools, 2);
|
|
105
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
106
|
+
assert.deepStrictEqual(stub.registrations.tools, []);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("a clear verdict after a refusal changes nothing", async () => {
|
|
110
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
111
|
+
const stub = makeStubPi({
|
|
112
|
+
sessionId: "s-1",
|
|
113
|
+
sessionDir,
|
|
114
|
+
tools: [{ name: "delegate_launch", source: "npm:pi-subagents" }],
|
|
115
|
+
});
|
|
116
|
+
let opened = 0;
|
|
117
|
+
createDelegatesExtension({
|
|
118
|
+
agentDir: await tempAgentDir(),
|
|
119
|
+
onRuntime: () => {
|
|
120
|
+
opened += 1;
|
|
121
|
+
},
|
|
122
|
+
})(stub.api);
|
|
123
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
124
|
+
|
|
125
|
+
stub.setTools([]);
|
|
126
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
127
|
+
|
|
128
|
+
assert.strictEqual(opened, 0);
|
|
129
|
+
assert.deepStrictEqual(stub.registrations.tools, []);
|
|
130
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("a clear session registers and opens the runtime", async () => {
|
|
134
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
135
|
+
const stub = makeStubPi({
|
|
136
|
+
settings: { packages: [] },
|
|
137
|
+
sessionId: "s-1",
|
|
138
|
+
sessionDir,
|
|
139
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
140
|
+
});
|
|
141
|
+
const runtimes: SessionRuntime[] = [];
|
|
142
|
+
createDelegatesExtension({
|
|
143
|
+
agentDir: await tempAgentDir(),
|
|
144
|
+
onRuntime: (current) => {
|
|
145
|
+
if (current !== undefined) runtimes.push(current);
|
|
146
|
+
},
|
|
147
|
+
})(stub.api);
|
|
148
|
+
try {
|
|
149
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
150
|
+
assert.deepStrictEqual([...stub.registrations.tools].sort(), [
|
|
151
|
+
"delegate_control",
|
|
152
|
+
"delegate_launch",
|
|
153
|
+
"delegate_status",
|
|
154
|
+
]);
|
|
155
|
+
assert.ok(stub.registrations.handlers.has("before_agent_start"));
|
|
156
|
+
assert.strictEqual(runtimes.length, 1);
|
|
157
|
+
assert.deepStrictEqual(stub.activeTools, []);
|
|
158
|
+
} finally {
|
|
159
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("a clear start activates the tools it registers", async () => {
|
|
164
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
165
|
+
const stub = makeStubPi({
|
|
166
|
+
sessionId: "s-1",
|
|
167
|
+
sessionDir,
|
|
168
|
+
activeTools: ["read", "bash"],
|
|
169
|
+
activateOnRegister: true,
|
|
170
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
171
|
+
});
|
|
172
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
173
|
+
try {
|
|
174
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
175
|
+
assert.deepStrictEqual(stub.activeTools, [
|
|
176
|
+
"read",
|
|
177
|
+
"bash",
|
|
178
|
+
"delegate_launch",
|
|
179
|
+
"delegate_control",
|
|
180
|
+
"delegate_status",
|
|
181
|
+
]);
|
|
182
|
+
assert.strictEqual(stub.calls.setActiveTools, 0);
|
|
183
|
+
} finally {
|
|
184
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("print mode gets the message on stderr", async () => {
|
|
189
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
190
|
+
const stub = makeStubPi({
|
|
191
|
+
sessionId: "s-1",
|
|
192
|
+
sessionDir,
|
|
193
|
+
mode: "print",
|
|
194
|
+
tools: [{ name: "delegate_launch", source: "npm:pi-subagents" }],
|
|
195
|
+
});
|
|
196
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
197
|
+
const writes: string[] = [];
|
|
198
|
+
const stderr = process.stderr;
|
|
199
|
+
const original = stderr.write.bind(stderr);
|
|
200
|
+
process.stderr.write = ((chunk: unknown) => {
|
|
201
|
+
writes.push(String(chunk));
|
|
202
|
+
return true;
|
|
203
|
+
}) as typeof process.stderr.write;
|
|
204
|
+
try {
|
|
205
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
206
|
+
} finally {
|
|
207
|
+
stderr.write = original;
|
|
208
|
+
}
|
|
209
|
+
assert.strictEqual(writes.length, 1);
|
|
210
|
+
assert.ok(writes[0]?.includes("pi remove npm:pi-subagents"), writes[0] ?? "");
|
|
211
|
+
// One stderr line, not the two lines the TUI notice uses.
|
|
212
|
+
assert.strictEqual((writes[0] ?? "").replace(/\n$/, "").includes("\n"), false);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("a refusal writes one log line", async () => {
|
|
216
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
217
|
+
const stub = makeStubPi({
|
|
218
|
+
sessionId: "s-1",
|
|
219
|
+
sessionDir,
|
|
220
|
+
tools: [{ name: "delegate_launch", source: "npm:pi-subagents" }],
|
|
221
|
+
});
|
|
222
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
223
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
224
|
+
|
|
225
|
+
const entries = (await readText(join(sessionDir, "delegates.log")))
|
|
226
|
+
.trim()
|
|
227
|
+
.split("\n")
|
|
228
|
+
.map((line) => JSON.parse(line) as { code: string; conflict?: string; removed?: string });
|
|
229
|
+
assert.strictEqual(entries.length, 1);
|
|
230
|
+
assert.strictEqual(entries[0]?.code, "coexistence.revoked");
|
|
231
|
+
assert.strictEqual(entries[0]?.conflict, "pi-subagents");
|
|
232
|
+
assert.ok(!("removed" in (entries[0] ?? {})));
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("a reload reports again", async () => {
|
|
236
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
237
|
+
const factory = createDelegatesExtension({ agentDir: await tempAgentDir() });
|
|
238
|
+
const first = makeStubPi({
|
|
239
|
+
sessionId: "s-1",
|
|
240
|
+
sessionDir,
|
|
241
|
+
tools: [{ name: "delegate_launch", source: "npm:pi-subagents" }],
|
|
242
|
+
});
|
|
243
|
+
factory(first.api);
|
|
244
|
+
await first.fire("session_start", { type: "session_start", reason: "startup" });
|
|
245
|
+
|
|
246
|
+
const second = makeStubPi({
|
|
247
|
+
sessionId: "s-1",
|
|
248
|
+
sessionDir,
|
|
249
|
+
tools: [{ name: "delegate_launch", source: "npm:pi-subagents" }],
|
|
250
|
+
});
|
|
251
|
+
factory(second.api);
|
|
252
|
+
await second.fire("session_start", { type: "session_start", reason: "reload" });
|
|
253
|
+
|
|
254
|
+
assert.strictEqual(first.notices.length, 1);
|
|
255
|
+
assert.strictEqual(second.notices.length, 1);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("a model or thinking-level switch updates the host", async () => {
|
|
259
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
260
|
+
const stub = makeStubPi({
|
|
261
|
+
settings: { packages: [] },
|
|
262
|
+
sessionId: "s-1",
|
|
263
|
+
sessionDir,
|
|
264
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
265
|
+
});
|
|
266
|
+
let current: SessionRuntime | undefined;
|
|
267
|
+
createDelegatesExtension({
|
|
268
|
+
agentDir: await tempAgentDir(),
|
|
269
|
+
onRuntime: (runtime) => {
|
|
270
|
+
current = runtime;
|
|
271
|
+
},
|
|
272
|
+
})(stub.api);
|
|
273
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
274
|
+
try {
|
|
275
|
+
const base = stub.context as Record<string, unknown>;
|
|
276
|
+
const model = { provider: "stub", id: "stub-2" };
|
|
277
|
+
const switched = { ...base, model, thinkingLevel: "high" };
|
|
278
|
+
await stub.fire(
|
|
279
|
+
"model_select",
|
|
280
|
+
{ type: "model_select", model, previousModel: undefined, source: "user" },
|
|
281
|
+
switched,
|
|
282
|
+
);
|
|
283
|
+
assert.deepStrictEqual(current?.host(), {
|
|
284
|
+
model: { provider: "stub", modelId: "stub-2" },
|
|
285
|
+
thinkingLevel: "high",
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
await stub.fire(
|
|
289
|
+
"thinking_level_select",
|
|
290
|
+
{ type: "thinking_level_select", level: "low", previousLevel: "high" },
|
|
291
|
+
{ ...switched, thinkingLevel: "low" },
|
|
292
|
+
);
|
|
293
|
+
assert.strictEqual(current?.host().thinkingLevel, "low");
|
|
294
|
+
} finally {
|
|
295
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("an installed but disabled pi-subagents does not block", async () => {
|
|
300
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
301
|
+
const stub = makeStubPi({
|
|
302
|
+
settings: { packages: ["npm:pi-subagents"] },
|
|
303
|
+
commands: [{ name: "skill:pi-subagents", source: "npm:pi-subagents", kind: "skill" }],
|
|
304
|
+
sessionId: "s-1",
|
|
305
|
+
sessionDir,
|
|
306
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
307
|
+
});
|
|
308
|
+
createDelegatesExtension()(stub.api);
|
|
309
|
+
|
|
310
|
+
// The package stays in the settings, but -ne loads no extension, so it
|
|
311
|
+
// contributes no tool or command. The guard must not treat it as enabled.
|
|
312
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
313
|
+
assert.deepStrictEqual([...stub.registrations.tools].sort(), [
|
|
314
|
+
"delegate_control",
|
|
315
|
+
"delegate_launch",
|
|
316
|
+
"delegate_status",
|
|
317
|
+
]);
|
|
318
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("the factory reads no host action while the extension loads", () => {
|
|
322
|
+
const stub = makeStubPi({ settings: { packages: [] } });
|
|
323
|
+
assert.doesNotThrow(() => createDelegatesExtension()(stub.api));
|
|
324
|
+
throws(
|
|
325
|
+
() => stub.api.getSettings(),
|
|
326
|
+
(error: unknown) =>
|
|
327
|
+
error instanceof Error && error.message.includes("during extension loading"),
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("the first session start registers the command when clear", async () => {
|
|
332
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
333
|
+
const stub = makeStubPi({
|
|
334
|
+
settings: { packages: [] },
|
|
335
|
+
sessionId: "s-1",
|
|
336
|
+
sessionDir,
|
|
337
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
338
|
+
});
|
|
339
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
340
|
+
assert.deepStrictEqual(stub.registrations.commands, []);
|
|
341
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
342
|
+
assert.deepStrictEqual(stub.registrations.commands, [
|
|
343
|
+
"delegates",
|
|
344
|
+
"delegates:list",
|
|
345
|
+
"delegates:answer",
|
|
346
|
+
"delegates:overlay",
|
|
347
|
+
]);
|
|
348
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("the delegates command lists each agent's tools and gates", async () => {
|
|
352
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
353
|
+
const stub = makeStubPi({
|
|
354
|
+
settings: { packages: [] },
|
|
355
|
+
sessionId: "s-1",
|
|
356
|
+
sessionDir,
|
|
357
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
358
|
+
mode: "rpc",
|
|
359
|
+
});
|
|
360
|
+
createDelegatesExtension({
|
|
361
|
+
agentDir: await tempAgentDir(),
|
|
362
|
+
definitions: [
|
|
363
|
+
defineAgent({
|
|
364
|
+
name: "deployer",
|
|
365
|
+
role: "Deploy agent",
|
|
366
|
+
prompt: { role: "You deploy things." },
|
|
367
|
+
tools: ["bash"],
|
|
368
|
+
extensions: [],
|
|
369
|
+
approval: { gates: [{ name: "deploy", tools: ["bash"] }] },
|
|
370
|
+
}),
|
|
371
|
+
],
|
|
372
|
+
})(stub.api);
|
|
373
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
374
|
+
const handler = stub.registrations.commandHandlers.get("delegates");
|
|
375
|
+
assert.ok(handler !== undefined);
|
|
376
|
+
await handler("", stub.context);
|
|
377
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
378
|
+
assert.ok(notice.includes("deployer — Deploy agent — tools: bash"), notice);
|
|
379
|
+
assert.ok(notice.includes("gates: deploy"), notice);
|
|
380
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("a reload closes the previous runtime before opening the next", async () => {
|
|
384
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
385
|
+
const stub = makeStubPi({
|
|
386
|
+
settings: { packages: [] },
|
|
387
|
+
sessionId: "s-1",
|
|
388
|
+
sessionDir,
|
|
389
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
390
|
+
});
|
|
391
|
+
const runtimes: SessionRuntime[] = [];
|
|
392
|
+
createDelegatesExtension({
|
|
393
|
+
agentDir: await tempAgentDir(),
|
|
394
|
+
onRuntime: (current) => {
|
|
395
|
+
if (current !== undefined) runtimes.push(current);
|
|
396
|
+
},
|
|
397
|
+
})(stub.api);
|
|
398
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
399
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
400
|
+
assert.strictEqual(runtimes.length, 2);
|
|
401
|
+
assert.strictEqual(runtimes[0]?.state.kind, "closed");
|
|
402
|
+
assert.strictEqual(runtimes[1]?.state.kind, "ready");
|
|
403
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("a session registers exactly the three delegation tools", async () => {
|
|
407
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
408
|
+
const stub = makeStubPi({
|
|
409
|
+
settings: { packages: [] },
|
|
410
|
+
sessionId: "s-1",
|
|
411
|
+
sessionDir,
|
|
412
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
413
|
+
});
|
|
414
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
415
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
416
|
+
assert.deepStrictEqual([...stub.registrations.tools].sort(), [
|
|
417
|
+
"delegate_control",
|
|
418
|
+
"delegate_launch",
|
|
419
|
+
"delegate_status",
|
|
420
|
+
]);
|
|
421
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("an unverified version note is logged at session start", async () => {
|
|
425
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
426
|
+
const stub = makeStubPi({
|
|
427
|
+
settings: { packages: [] },
|
|
428
|
+
sessionId: "s-1",
|
|
429
|
+
sessionDir,
|
|
430
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
431
|
+
});
|
|
432
|
+
createDelegatesExtension({
|
|
433
|
+
agentDir: await tempAgentDir(),
|
|
434
|
+
version: { kind: "clear", note: "unverified" },
|
|
435
|
+
})(stub.api);
|
|
436
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
437
|
+
|
|
438
|
+
const lines = (await readText(join(sessionDir, "delegates.log"))).trim().split("\n");
|
|
439
|
+
const entries = lines.map((line) => JSON.parse(line) as { code: string; level: string });
|
|
440
|
+
const notes = entries.filter((entry) => entry.code === "version.unverified");
|
|
441
|
+
assert.strictEqual(notes.length, 1);
|
|
442
|
+
assert.strictEqual(notes[0]?.level, "warn");
|
|
443
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it("the delegates command logs a viewer failure and still prints the registry", async () => {
|
|
447
|
+
const stub = makeStubPi({ settings: { packages: [] }, mode: "rpc" });
|
|
448
|
+
const entries: { level: LogLevel; code: string }[] = [];
|
|
449
|
+
const sink: LogSink = {
|
|
450
|
+
write: (level, code) => {
|
|
451
|
+
entries.push({ level, code });
|
|
452
|
+
},
|
|
453
|
+
};
|
|
454
|
+
const failing = {
|
|
455
|
+
snapshot: () => Promise.reject(new Error("viewer gone")),
|
|
456
|
+
} as unknown as SessionRuntime;
|
|
457
|
+
registerDelegatesCommand(stub.api, {
|
|
458
|
+
definitions: [
|
|
459
|
+
defineAgent({
|
|
460
|
+
name: "deployer",
|
|
461
|
+
role: "Deploy agent",
|
|
462
|
+
prompt: { role: "You deploy things." },
|
|
463
|
+
tools: ["bash"],
|
|
464
|
+
extensions: [],
|
|
465
|
+
approval: { gates: [{ name: "deploy", tools: ["bash"] }] },
|
|
466
|
+
}),
|
|
467
|
+
],
|
|
468
|
+
session: Ref.makeUnsafe<SessionHandle | undefined>(handleFor(failing)),
|
|
469
|
+
log: () => sink,
|
|
470
|
+
});
|
|
471
|
+
const handler = stub.registrations.commandHandlers.get("delegates");
|
|
472
|
+
assert.ok(handler !== undefined);
|
|
473
|
+
await handler("", stub.context);
|
|
474
|
+
|
|
475
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
476
|
+
assert.ok(notice.includes("deployer — Deploy agent"), notice);
|
|
477
|
+
assert.deepStrictEqual(entries, [{ level: "warn", code: "command.viewer_failed" }]);
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
let cleanStub: StubPi | undefined;
|
|
481
|
+
|
|
482
|
+
const stubForSession = async (mode: "tui" | "rpc" = "tui"): Promise<StubPi> => {
|
|
483
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
484
|
+
const stub = makeStubPi({
|
|
485
|
+
settings: { packages: [] },
|
|
486
|
+
sessionId: "s-1",
|
|
487
|
+
sessionDir,
|
|
488
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
489
|
+
mode,
|
|
490
|
+
});
|
|
491
|
+
cleanStub = stub;
|
|
492
|
+
return stub;
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
const sessionDirOf = (stub: StubPi): string =>
|
|
496
|
+
(
|
|
497
|
+
stub.context as { sessionManager: { getSessionDir: () => string } }
|
|
498
|
+
).sessionManager.getSessionDir();
|
|
499
|
+
|
|
500
|
+
// A failed assertion must not leave an activation or an open runtime for the next test.
|
|
501
|
+
afterEach(async () => {
|
|
502
|
+
const stub = cleanStub;
|
|
503
|
+
cleanStub = undefined;
|
|
504
|
+
if (stub !== undefined) await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
const liveView = (): SessionView => ({
|
|
508
|
+
runs: [
|
|
509
|
+
{
|
|
510
|
+
runId: "run:a",
|
|
511
|
+
definition: "fanout",
|
|
512
|
+
mode: "fanout",
|
|
513
|
+
lane: "default",
|
|
514
|
+
conversationId: 10,
|
|
515
|
+
taskId: null,
|
|
516
|
+
status: "running",
|
|
517
|
+
answer: null,
|
|
518
|
+
durationMs: null,
|
|
519
|
+
children: [
|
|
520
|
+
{
|
|
521
|
+
child: 0,
|
|
522
|
+
runId: "run:a",
|
|
523
|
+
conversationId: 10,
|
|
524
|
+
taskId: null,
|
|
525
|
+
agent: "deployer",
|
|
526
|
+
status: "running",
|
|
527
|
+
answer: null,
|
|
528
|
+
durationMs: null,
|
|
529
|
+
maxTurns: null,
|
|
530
|
+
failure: null,
|
|
531
|
+
},
|
|
532
|
+
],
|
|
533
|
+
missing: [],
|
|
534
|
+
gates: [],
|
|
535
|
+
requests: [],
|
|
536
|
+
diagnostic: null,
|
|
537
|
+
execution: "wait",
|
|
538
|
+
maxTurns: null,
|
|
539
|
+
failure: null,
|
|
540
|
+
},
|
|
541
|
+
],
|
|
542
|
+
lanes: [],
|
|
543
|
+
gates: [],
|
|
544
|
+
reviews: [],
|
|
545
|
+
requests: [],
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
const marker = (active: boolean): SessionEntry =>
|
|
549
|
+
({ type: "custom", customType: ACTIVATION_ENTRY, data: { active } }) as SessionEntry;
|
|
550
|
+
|
|
551
|
+
const markerEntries = (stub: StubPi): readonly SessionEntry[] =>
|
|
552
|
+
stub.entries.filter((entry) => entry.type === "custom" && entry.customType === ACTIVATION_ENTRY);
|
|
553
|
+
|
|
554
|
+
const inspectorOptions = (fake: ReturnType<typeof fakeObservations>) => ({
|
|
555
|
+
observe: fake,
|
|
556
|
+
view: async () => liveView(),
|
|
557
|
+
now: () => 4_000,
|
|
558
|
+
timers: inspectorClock(),
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
it("an rpc session start restores no surfaces", async () => {
|
|
562
|
+
const stub = await stubForSession("rpc");
|
|
563
|
+
stub.entries.push(marker(true));
|
|
564
|
+
const fake = fakeObservations();
|
|
565
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
566
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
567
|
+
|
|
568
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
569
|
+
assert.strictEqual(stub.customCalls.length, 0);
|
|
570
|
+
assert.strictEqual(stub.statusCalls.length, 0);
|
|
571
|
+
assert.strictEqual(fake.attaches.length, 0);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it("a session that never opted in writes no footer line", async () => {
|
|
575
|
+
const stub = await stubForSession();
|
|
576
|
+
const fake = fakeObservations();
|
|
577
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
578
|
+
await stub.fire("session_start", { type: "session_start", reason: "new" });
|
|
579
|
+
|
|
580
|
+
assert.strictEqual(stub.statusCalls.length, 0);
|
|
581
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
it("a resume with the inspector off names the command in the footer", async () => {
|
|
585
|
+
const stub = await stubForSession();
|
|
586
|
+
stub.entries.push(marker(false));
|
|
587
|
+
const fake = fakeObservations();
|
|
588
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
589
|
+
await stub.fire("session_start", { type: "session_start", reason: "resume" });
|
|
590
|
+
|
|
591
|
+
assert.strictEqual(stub.statusCalls.at(-1)?.text, "delegates off · /delegates");
|
|
592
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
593
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
it("an rpc resume with the inspector off writes no footer line", async () => {
|
|
597
|
+
const stub = await stubForSession("rpc");
|
|
598
|
+
stub.entries.push(marker(false));
|
|
599
|
+
const fake = fakeObservations();
|
|
600
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
601
|
+
await stub.fire("session_start", { type: "session_start", reason: "resume" });
|
|
602
|
+
|
|
603
|
+
assert.strictEqual(stub.statusCalls.length, 0);
|
|
604
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
it("a reload restores the strip through the session_start handler", async () => {
|
|
608
|
+
const stub = await stubForSession();
|
|
609
|
+
stub.entries.push(marker(true));
|
|
610
|
+
const fake = fakeObservations();
|
|
611
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
612
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
613
|
+
|
|
614
|
+
assert.strictEqual(stub.widgetCalls.length, 1);
|
|
615
|
+
assert.strictEqual(typeof stub.widgetCalls[0]?.content, "function");
|
|
616
|
+
assert.ok(fake.attaches.length > 0);
|
|
617
|
+
assert.strictEqual(stub.notices.length, 0);
|
|
618
|
+
|
|
619
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
620
|
+
assert.strictEqual(stub.widgetCalls.at(-1)?.content, undefined);
|
|
621
|
+
assert.strictEqual(stub.statusCalls.at(-1)?.text, undefined);
|
|
622
|
+
assert.deepStrictEqual(fake.attached(), []);
|
|
623
|
+
assert.strictEqual(new Set(fake.detaches).size, fake.detaches.length);
|
|
624
|
+
assert.strictEqual(stub.notices.length, 0);
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it("a resume restores the strip through the session_start handler", async () => {
|
|
628
|
+
const stub = await stubForSession();
|
|
629
|
+
stub.entries.push(marker(true));
|
|
630
|
+
const fake = fakeObservations();
|
|
631
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
632
|
+
await stub.fire("session_start", { type: "session_start", reason: "resume" });
|
|
633
|
+
|
|
634
|
+
assert.strictEqual(typeof stub.widgetCalls.at(-1)?.content, "function");
|
|
635
|
+
assert.ok(fake.attaches.length > 0);
|
|
636
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it("the first delegate launch activates the inspector in a tui session", async () => {
|
|
640
|
+
const stub = await stubForSession();
|
|
641
|
+
const fake = fakeObservations();
|
|
642
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
643
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
644
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
645
|
+
assert.deepStrictEqual(markerEntries(stub), []);
|
|
646
|
+
|
|
647
|
+
const tool = stub.registrations.toolDefinitions.get("delegate_launch") as {
|
|
648
|
+
execute: (
|
|
649
|
+
id: string,
|
|
650
|
+
params: unknown,
|
|
651
|
+
signal: undefined,
|
|
652
|
+
onUpdate: undefined,
|
|
653
|
+
ctx: unknown,
|
|
654
|
+
) => Promise<{ content: readonly { readonly text: string }[] }>;
|
|
655
|
+
};
|
|
656
|
+
const result = await tool.execute(
|
|
657
|
+
"",
|
|
658
|
+
{
|
|
659
|
+
request: {
|
|
660
|
+
kind: "single",
|
|
661
|
+
agent: "scout",
|
|
662
|
+
task: "look",
|
|
663
|
+
definition: {
|
|
664
|
+
name: "scout",
|
|
665
|
+
role: "Scout",
|
|
666
|
+
prompt: { role: "look" },
|
|
667
|
+
tools: ["read"],
|
|
668
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
669
|
+
},
|
|
670
|
+
},
|
|
671
|
+
},
|
|
672
|
+
undefined,
|
|
673
|
+
undefined,
|
|
674
|
+
stub.context,
|
|
675
|
+
);
|
|
676
|
+
|
|
677
|
+
assert.deepStrictEqual(
|
|
678
|
+
markerEntries(stub).map((entry) => (entry as { data?: { active?: boolean } }).data),
|
|
679
|
+
[{ active: true }],
|
|
680
|
+
result.content[0]?.text ?? "",
|
|
681
|
+
);
|
|
682
|
+
assert.strictEqual(stub.widgetCalls.length, 1);
|
|
683
|
+
assert.strictEqual(typeof stub.widgetCalls[0]?.content, "function");
|
|
684
|
+
assert.ok(fake.attaches.length > 0);
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
it("a gate alert starts with the session, not the inspector", async () => {
|
|
688
|
+
const stub = await stubForSession();
|
|
689
|
+
let runtime: SessionRuntime | undefined;
|
|
690
|
+
createDelegatesExtension({
|
|
691
|
+
onRuntime: (current) => {
|
|
692
|
+
runtime = current;
|
|
693
|
+
},
|
|
694
|
+
})(stub.api);
|
|
695
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
696
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
697
|
+
assert.deepStrictEqual(markerEntries(stub), []);
|
|
698
|
+
assert.ok(runtime !== undefined);
|
|
699
|
+
|
|
700
|
+
await runtime.harness.commit(async (tx) => {
|
|
701
|
+
const index = await tx.doc(GateIndexDoc);
|
|
702
|
+
const key = gateKey("run:fan", "deploy");
|
|
703
|
+
const gate = await tx.doc(GateDoc, key, { runId: "run:fan", gate: "deploy" });
|
|
704
|
+
gate.status = "pending";
|
|
705
|
+
gate.requestedAt = 1_000;
|
|
706
|
+
index.keys.push(key);
|
|
707
|
+
}, BACKGROUND_CONTEXT);
|
|
708
|
+
await waitFor(() => stub.notices.length === 1, "the session watcher alerted");
|
|
709
|
+
assert.strictEqual(stub.messages.length, 1);
|
|
710
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
it("a new session starts with no surfaces through the handler", async () => {
|
|
714
|
+
const stub = await stubForSession();
|
|
715
|
+
stub.entries.push(marker(true));
|
|
716
|
+
const fake = fakeObservations();
|
|
717
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
718
|
+
await stub.fire("session_start", { type: "session_start", reason: "new" });
|
|
719
|
+
|
|
720
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
721
|
+
assert.strictEqual(stub.customCalls.length, 0);
|
|
722
|
+
assert.strictEqual(fake.attaches.length, 0);
|
|
723
|
+
assert.strictEqual(markerEntries(stub).length, 1);
|
|
724
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
it("a fork starts with no surfaces and its reload stays empty", async () => {
|
|
728
|
+
const stub = await stubForSession();
|
|
729
|
+
stub.entries.push(marker(true));
|
|
730
|
+
const fake = fakeObservations();
|
|
731
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
732
|
+
await stub.fire("session_start", { type: "session_start", reason: "fork" });
|
|
733
|
+
|
|
734
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
735
|
+
assert.strictEqual(fake.attaches.length, 0);
|
|
736
|
+
assert.deepStrictEqual(
|
|
737
|
+
markerEntries(stub).map((entry) => (entry as { data?: { active?: boolean } }).data),
|
|
738
|
+
[{ active: true }, { active: false }],
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
742
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
743
|
+
assert.strictEqual(fake.attaches.length, 0);
|
|
744
|
+
assert.strictEqual(markerEntries(stub).length, 2);
|
|
745
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it("a fork outside tui still writes the off marker the resume reads", async () => {
|
|
749
|
+
const stub = await stubForSession("rpc");
|
|
750
|
+
stub.entries.push(marker(true));
|
|
751
|
+
const fake = fakeObservations();
|
|
752
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
753
|
+
await stub.fire("session_start", { type: "session_start", reason: "fork" });
|
|
754
|
+
|
|
755
|
+
assert.deepStrictEqual(
|
|
756
|
+
markerEntries(stub).map((entry) => (entry as { data?: { active?: boolean } }).data),
|
|
757
|
+
[{ active: true }, { active: false }],
|
|
758
|
+
);
|
|
759
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
760
|
+
assert.strictEqual(stub.statusCalls.length, 0);
|
|
761
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
it("a branch switch follows the marker through the session_tree handler", async () => {
|
|
765
|
+
const stub = await stubForSession();
|
|
766
|
+
const fake = fakeObservations();
|
|
767
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
768
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
769
|
+
assert.strictEqual(stub.widgetCalls.length, 0);
|
|
770
|
+
|
|
771
|
+
stub.entries.push(marker(true));
|
|
772
|
+
await stub.fire("session_tree", { type: "session_tree" });
|
|
773
|
+
assert.strictEqual(typeof stub.widgetCalls.at(-1)?.content, "function");
|
|
774
|
+
const attaches = fake.attaches.length;
|
|
775
|
+
assert.ok(attaches > 0);
|
|
776
|
+
|
|
777
|
+
// An active marker while the owner is live changes nothing.
|
|
778
|
+
await stub.fire("session_tree", { type: "session_tree" });
|
|
779
|
+
assert.strictEqual(stub.widgetCalls.length, 1);
|
|
780
|
+
assert.strictEqual(fake.attaches.length, attaches);
|
|
781
|
+
|
|
782
|
+
stub.entries.push(marker(false));
|
|
783
|
+
await stub.fire("session_tree", { type: "session_tree" });
|
|
784
|
+
assert.strictEqual(stub.widgetCalls.at(-1)?.content, undefined);
|
|
785
|
+
assert.strictEqual(fake.detaches.length, attaches);
|
|
786
|
+
|
|
787
|
+
stub.entries.push(marker(true));
|
|
788
|
+
await stub.fire("session_tree", { type: "session_tree" });
|
|
789
|
+
assert.strictEqual(stub.widgetCalls.filter((call) => call.content !== undefined).length, 2);
|
|
790
|
+
assert.strictEqual(fake.attaches.length, attaches * 2);
|
|
791
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
it("a second session_start without a shutdown replaces the activation", async () => {
|
|
795
|
+
const stub = await stubForSession();
|
|
796
|
+
stub.entries.push(marker(true));
|
|
797
|
+
const fake = fakeObservations();
|
|
798
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
799
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
800
|
+
const attaches = fake.attaches.length;
|
|
801
|
+
assert.ok(attaches > 0);
|
|
802
|
+
|
|
803
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
804
|
+
assert.strictEqual(fake.detaches.length, attaches);
|
|
805
|
+
assert.strictEqual(fake.attaches.length, attaches * 2);
|
|
806
|
+
assert.strictEqual(stub.widgetCalls.filter((call) => call.content !== undefined).length, 2);
|
|
807
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
it("session shutdown removes the strip, writes no notice and detaches once", async () => {
|
|
811
|
+
const stub = await stubForSession();
|
|
812
|
+
stub.entries.push(marker(true));
|
|
813
|
+
const fake = fakeObservations();
|
|
814
|
+
createDelegatesExtension({ inspector: inspectorOptions(fake) })(stub.api);
|
|
815
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
816
|
+
const attaches = fake.attaches.length;
|
|
817
|
+
|
|
818
|
+
await stub.fire("session_shutdown", { type: "session_shutdown", reason: "quit" });
|
|
819
|
+
assert.strictEqual(stub.widgetCalls.at(-1)?.content, undefined);
|
|
820
|
+
assert.strictEqual(stub.notices.length, 0);
|
|
821
|
+
assert.strictEqual(fake.detaches.length, attaches);
|
|
822
|
+
assert.strictEqual(new Set(fake.detaches).size, fake.detaches.length);
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
it("a late conflict withdraws the tools and closes the runtime", async () => {
|
|
826
|
+
const stub = await stubForSession();
|
|
827
|
+
const delivered: Array<SessionRuntime | undefined> = [];
|
|
828
|
+
createDelegatesExtension({
|
|
829
|
+
agentDir: await tempAgentDir(),
|
|
830
|
+
onRuntime: (current) => {
|
|
831
|
+
delivered.push(current);
|
|
832
|
+
},
|
|
833
|
+
})(stub.api);
|
|
834
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
835
|
+
const first = delivered.at(-1);
|
|
836
|
+
assert.ok(first !== undefined);
|
|
837
|
+
stub.activeTools.push("delegate_launch", "delegate_control", "delegate_status");
|
|
838
|
+
|
|
839
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
840
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
841
|
+
|
|
842
|
+
assert.strictEqual(delivered.at(-1), undefined);
|
|
843
|
+
assert.strictEqual(first.state.kind, "closed");
|
|
844
|
+
assert.deepStrictEqual(stub.activeTools, []);
|
|
845
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
846
|
+
assert.strictEqual(stub.registrations.commands.filter((name) => name === "delegates").length, 1);
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
it("the notice precedes the close", async () => {
|
|
850
|
+
const stub = await stubForSession();
|
|
851
|
+
let runtime: SessionRuntime | undefined;
|
|
852
|
+
createDelegatesExtension({
|
|
853
|
+
agentDir: await tempAgentDir(),
|
|
854
|
+
onRuntime: (current) => {
|
|
855
|
+
if (current !== undefined) runtime = current;
|
|
856
|
+
},
|
|
857
|
+
})(stub.api);
|
|
858
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
859
|
+
assert.ok(runtime !== undefined);
|
|
860
|
+
|
|
861
|
+
// The harness close is the graph's finalizer, preceded by the notice count it observes.
|
|
862
|
+
const harness = runtime.harness as {
|
|
863
|
+
close: (context: typeof BACKGROUND_CONTEXT) => Promise<void>;
|
|
864
|
+
};
|
|
865
|
+
const original = harness.close.bind(runtime.harness);
|
|
866
|
+
let noticesAtClose = -1;
|
|
867
|
+
harness.close = async (context) => {
|
|
868
|
+
noticesAtClose = stub.notices.length;
|
|
869
|
+
await original(context);
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
873
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
874
|
+
assert.strictEqual(noticesAtClose, 1);
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
it("the inspector and the gate alerts stop", async () => {
|
|
878
|
+
const stub = await stubForSession();
|
|
879
|
+
const fake = fakeObservations();
|
|
880
|
+
createDelegatesExtension({
|
|
881
|
+
agentDir: await tempAgentDir(),
|
|
882
|
+
inspector: inspectorOptions(fake),
|
|
883
|
+
})(stub.api);
|
|
884
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
885
|
+
|
|
886
|
+
const toggle = stub.registrations.commandHandlers.get("delegates");
|
|
887
|
+
assert.ok(toggle !== undefined);
|
|
888
|
+
await toggle("", stub.context);
|
|
889
|
+
assert.strictEqual(stub.widgetCalls.length, 1);
|
|
890
|
+
assert.ok(fake.attaches.length > 0);
|
|
891
|
+
|
|
892
|
+
stub.addTool({ name: "delegate_control", source: "npm:pi-subagents" });
|
|
893
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
894
|
+
assert.strictEqual(stub.widgetCalls.at(-1)?.content, undefined);
|
|
895
|
+
assert.deepStrictEqual(fake.attached(), []);
|
|
896
|
+
|
|
897
|
+
// A gate appended after the refusal must not alert: the watcher stopped with the runtime.
|
|
898
|
+
const sessionDir = sessionDirOf(stub);
|
|
899
|
+
const second = await openTestSession({
|
|
900
|
+
identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
901
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
902
|
+
});
|
|
903
|
+
try {
|
|
904
|
+
await second.harness.commit(async (tx) => {
|
|
905
|
+
const index = await tx.doc(GateIndexDoc);
|
|
906
|
+
const key = gateKey("run:fan", "deploy");
|
|
907
|
+
const gate = await tx.doc(GateDoc, key, { runId: "run:fan", gate: "deploy" });
|
|
908
|
+
gate.status = "pending";
|
|
909
|
+
gate.requestedAt = 1_000;
|
|
910
|
+
index.keys.push(key);
|
|
911
|
+
}, BACKGROUND_CONTEXT);
|
|
912
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
913
|
+
assert.strictEqual(stub.messages.length, 0);
|
|
914
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
915
|
+
} finally {
|
|
916
|
+
await second.close();
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
it("a close that never settles does not block the start", async () => {
|
|
921
|
+
const stub = await stubForSession();
|
|
922
|
+
const delivered: Array<SessionRuntime | undefined> = [];
|
|
923
|
+
let runtime: SessionRuntime | undefined;
|
|
924
|
+
createDelegatesExtension({
|
|
925
|
+
agentDir: await tempAgentDir(),
|
|
926
|
+
revocationCloseMs: 5,
|
|
927
|
+
onRuntime: (current) => {
|
|
928
|
+
delivered.push(current);
|
|
929
|
+
if (current !== undefined) runtime = current;
|
|
930
|
+
},
|
|
931
|
+
})(stub.api);
|
|
932
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
933
|
+
assert.ok(runtime !== undefined);
|
|
934
|
+
|
|
935
|
+
// A close that outlives the bound: the harness close settles only when the test releases it.
|
|
936
|
+
const harness = runtime.harness as {
|
|
937
|
+
close: (context: typeof BACKGROUND_CONTEXT) => Promise<void>;
|
|
938
|
+
};
|
|
939
|
+
let release: () => void = () => {};
|
|
940
|
+
const hanging = new Promise<void>((resolve) => {
|
|
941
|
+
release = resolve;
|
|
942
|
+
});
|
|
943
|
+
harness.close = () => hanging;
|
|
944
|
+
|
|
945
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
946
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
947
|
+
|
|
948
|
+
// The start was not blocked; release the close so the shutdown drain terminates.
|
|
949
|
+
release();
|
|
950
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
951
|
+
|
|
952
|
+
const entries = (await readText(join(sessionDirOf(stub), "delegates.log")))
|
|
953
|
+
.trim()
|
|
954
|
+
.split("\n")
|
|
955
|
+
.map((line) => JSON.parse(line) as { code: string });
|
|
956
|
+
assert.ok(
|
|
957
|
+
entries.some((entry) => entry.code === "revocation.close_timeout"),
|
|
958
|
+
JSON.stringify(entries),
|
|
959
|
+
);
|
|
960
|
+
|
|
961
|
+
const filtered = stub.calls.setActiveTools;
|
|
962
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
963
|
+
assert.strictEqual(stub.calls.setActiveTools, filtered + 1);
|
|
964
|
+
// Startup opened one runtime; the refusal and the shutdown each reported none.
|
|
965
|
+
assert.strictEqual(delivered.length, 3);
|
|
966
|
+
assert.strictEqual(delivered[1], undefined);
|
|
967
|
+
assert.strictEqual(delivered[2], undefined);
|
|
968
|
+
});
|
|
969
|
+
|
|
970
|
+
it("the prompt section is present before the refusal and absent after", async () => {
|
|
971
|
+
const stub = await stubForSession();
|
|
972
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
973
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
974
|
+
|
|
975
|
+
const before = {
|
|
976
|
+
systemPromptOptions: {
|
|
977
|
+
selectedTools: ["delegate_launch"],
|
|
978
|
+
sections: {} as Record<string, string>,
|
|
979
|
+
},
|
|
980
|
+
};
|
|
981
|
+
await stub.fire("before_agent_start", before);
|
|
982
|
+
assert.ok(
|
|
983
|
+
before.systemPromptOptions.sections.delegates?.startsWith("Delegates (call delegate_launch):"),
|
|
984
|
+
before.systemPromptOptions.sections.delegates ?? "",
|
|
985
|
+
);
|
|
986
|
+
|
|
987
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
988
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
989
|
+
|
|
990
|
+
const after = {
|
|
991
|
+
systemPromptOptions: {
|
|
992
|
+
selectedTools: ["delegate_launch"],
|
|
993
|
+
sections: {} as Record<string, string>,
|
|
994
|
+
},
|
|
995
|
+
};
|
|
996
|
+
await stub.fire("before_agent_start", after);
|
|
997
|
+
assert.ok(!("delegates" in after.systemPromptOptions.sections));
|
|
998
|
+
});
|
|
999
|
+
|
|
1000
|
+
it("a later turn still omits the section", async () => {
|
|
1001
|
+
const stub = await stubForSession();
|
|
1002
|
+
createDelegatesExtension({ agentDir: await tempAgentDir() })(stub.api);
|
|
1003
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
1004
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
1005
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1006
|
+
|
|
1007
|
+
for (let turn = 0; turn < 2; turn += 1) {
|
|
1008
|
+
const event = {
|
|
1009
|
+
systemPromptOptions: {
|
|
1010
|
+
selectedTools: ["delegate_launch"],
|
|
1011
|
+
sections: {} as Record<string, string>,
|
|
1012
|
+
},
|
|
1013
|
+
};
|
|
1014
|
+
await stub.fire("before_agent_start", event);
|
|
1015
|
+
assert.ok(!("delegates" in event.systemPromptOptions.sections));
|
|
1016
|
+
}
|
|
1017
|
+
});
|
|
1018
|
+
|
|
1019
|
+
it("every later start re-filters", async () => {
|
|
1020
|
+
const stub = await stubForSession();
|
|
1021
|
+
const delivered: Array<SessionRuntime | undefined> = [];
|
|
1022
|
+
createDelegatesExtension({
|
|
1023
|
+
agentDir: await tempAgentDir(),
|
|
1024
|
+
onRuntime: (current) => {
|
|
1025
|
+
delivered.push(current);
|
|
1026
|
+
},
|
|
1027
|
+
})(stub.api);
|
|
1028
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
1029
|
+
stub.activeTools.push("delegate_launch", "delegate_control", "delegate_status");
|
|
1030
|
+
|
|
1031
|
+
stub.addTool({ name: "delegate_status", source: "npm:pi-subagents" });
|
|
1032
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1033
|
+
const afterRefusal = stub.calls.setActiveTools;
|
|
1034
|
+
assert.ok(afterRefusal > 0);
|
|
1035
|
+
|
|
1036
|
+
stub.restoreRegisteredTools();
|
|
1037
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1038
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1039
|
+
|
|
1040
|
+
assert.strictEqual(stub.calls.setActiveTools, afterRefusal + 2);
|
|
1041
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
1042
|
+
assert.strictEqual(delivered.length, 2);
|
|
1043
|
+
assert.strictEqual(delivered[1], undefined);
|
|
1044
|
+
});
|
|
1045
|
+
|
|
1046
|
+
it("committed runs stay readable", async () => {
|
|
1047
|
+
const stub = await stubForSession();
|
|
1048
|
+
let runtime: SessionRuntime | undefined;
|
|
1049
|
+
createDelegatesExtension({
|
|
1050
|
+
agentDir: await tempAgentDir(),
|
|
1051
|
+
onRuntime: (current) => {
|
|
1052
|
+
if (current !== undefined) runtime = current;
|
|
1053
|
+
},
|
|
1054
|
+
})(stub.api);
|
|
1055
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
1056
|
+
assert.ok(runtime !== undefined);
|
|
1057
|
+
const storePath = runtime.storePath;
|
|
1058
|
+
|
|
1059
|
+
// Launch one run through the registered tool, as a live session does.
|
|
1060
|
+
const tool = stub.registrations.toolDefinitions.get("delegate_launch") as {
|
|
1061
|
+
execute: (
|
|
1062
|
+
id: string,
|
|
1063
|
+
params: unknown,
|
|
1064
|
+
signal: undefined,
|
|
1065
|
+
onUpdate: undefined,
|
|
1066
|
+
ctx: unknown,
|
|
1067
|
+
) => Promise<{ content: readonly { readonly text: string }[] }>;
|
|
1068
|
+
};
|
|
1069
|
+
await tool.execute(
|
|
1070
|
+
"",
|
|
1071
|
+
{
|
|
1072
|
+
request: {
|
|
1073
|
+
kind: "single",
|
|
1074
|
+
agent: "scout",
|
|
1075
|
+
task: "look",
|
|
1076
|
+
definition: {
|
|
1077
|
+
name: "scout",
|
|
1078
|
+
role: "Scout",
|
|
1079
|
+
prompt: { role: "look" },
|
|
1080
|
+
tools: ["read"],
|
|
1081
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
1082
|
+
},
|
|
1083
|
+
},
|
|
1084
|
+
},
|
|
1085
|
+
undefined,
|
|
1086
|
+
undefined,
|
|
1087
|
+
stub.context,
|
|
1088
|
+
);
|
|
1089
|
+
const written = await runtime.snapshot(RunIndexDoc);
|
|
1090
|
+
const runId = written?.runIds.at(-1);
|
|
1091
|
+
assert.ok(runId !== undefined);
|
|
1092
|
+
|
|
1093
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
1094
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1095
|
+
|
|
1096
|
+
const viewer = await openReadOnlyRuntime({
|
|
1097
|
+
storePath,
|
|
1098
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
1099
|
+
});
|
|
1100
|
+
try {
|
|
1101
|
+
const index = await viewer.snapshot(RunIndexDoc);
|
|
1102
|
+
assert.ok(index?.runIds.includes(runId), JSON.stringify(index?.runIds));
|
|
1103
|
+
await rejects(
|
|
1104
|
+
viewer.harness.commit(async (tx) => {
|
|
1105
|
+
const index = await tx.doc(RunIndexDoc);
|
|
1106
|
+
index.runIds.push("run:forbidden");
|
|
1107
|
+
}, BACKGROUND_CONTEXT),
|
|
1108
|
+
(error: unknown) => error instanceof Error && error.name === "ReadOnlySession",
|
|
1109
|
+
);
|
|
1110
|
+
} finally {
|
|
1111
|
+
await viewer.close();
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
|
|
1115
|
+
it("a close that settles after the bound is retried at shutdown", async () => {
|
|
1116
|
+
const stub = await stubForSession();
|
|
1117
|
+
let runtime: SessionRuntime | undefined;
|
|
1118
|
+
createDelegatesExtension({
|
|
1119
|
+
agentDir: await tempAgentDir(),
|
|
1120
|
+
revocationCloseMs: 5,
|
|
1121
|
+
onRuntime: (current) => {
|
|
1122
|
+
if (current !== undefined) runtime = current;
|
|
1123
|
+
},
|
|
1124
|
+
})(stub.api);
|
|
1125
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
1126
|
+
assert.ok(runtime !== undefined);
|
|
1127
|
+
|
|
1128
|
+
// The harness close resolves only after the bound; one close is awaited, never started twice.
|
|
1129
|
+
const harness = runtime.harness as {
|
|
1130
|
+
close: (context: typeof BACKGROUND_CONTEXT) => Promise<void>;
|
|
1131
|
+
};
|
|
1132
|
+
const original = harness.close.bind(runtime.harness);
|
|
1133
|
+
let closeCalls = 0;
|
|
1134
|
+
let settled = false;
|
|
1135
|
+
harness.close = async (context) => {
|
|
1136
|
+
closeCalls += 1;
|
|
1137
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
1138
|
+
settled = true;
|
|
1139
|
+
await original(context);
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
1143
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1144
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
1145
|
+
|
|
1146
|
+
// The retry re-awaits the same dispose: the drain terminates and the close ran once.
|
|
1147
|
+
assert.strictEqual(closeCalls, 1);
|
|
1148
|
+
assert.strictEqual(settled, true);
|
|
1149
|
+
});
|
|
1150
|
+
|
|
1151
|
+
it("the catalogue loads Pi's configured skill paths", async () => {
|
|
1152
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
1153
|
+
const agentDir = await tempAgentDir();
|
|
1154
|
+
const extraDir = await makeTempDir("delegates-skills-");
|
|
1155
|
+
await makeDir(join(extraDir, "gamma"));
|
|
1156
|
+
await writeText(
|
|
1157
|
+
join(extraDir, "gamma", "SKILL.md"),
|
|
1158
|
+
"---\nname: gamma\ndescription: The gamma skill\n---\n\nGAMMA_BODY\n",
|
|
1159
|
+
);
|
|
1160
|
+
const stub = makeStubPi({
|
|
1161
|
+
settings: { packages: [], skills: [extraDir] },
|
|
1162
|
+
sessionId: "s-1",
|
|
1163
|
+
sessionDir,
|
|
1164
|
+
modelRegistry: new HostModelAdapter(stubHostSource()),
|
|
1165
|
+
});
|
|
1166
|
+
const runtimes: SessionRuntime[] = [];
|
|
1167
|
+
createDelegatesExtension({
|
|
1168
|
+
agentDir,
|
|
1169
|
+
onRuntime: (current) => {
|
|
1170
|
+
if (current !== undefined) runtimes.push(current);
|
|
1171
|
+
},
|
|
1172
|
+
})(stub.api);
|
|
1173
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
1174
|
+
try {
|
|
1175
|
+
const names = runtimes[0]?.skills.all().map((skill) => skill.name) ?? [];
|
|
1176
|
+
assert.ok(names.includes("gamma"), names.join(", "));
|
|
1177
|
+
assert.ok(names.length === 1, names.join(", "));
|
|
1178
|
+
} finally {
|
|
1179
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
|
|
1183
|
+
it("a close that outlives its bound still closes once when the shutdown drains it", async () => {
|
|
1184
|
+
const stub = await stubForSession();
|
|
1185
|
+
let runtime: SessionRuntime | undefined;
|
|
1186
|
+
createDelegatesExtension({
|
|
1187
|
+
agentDir: await tempAgentDir(),
|
|
1188
|
+
revocationCloseMs: 5,
|
|
1189
|
+
onRuntime: (current) => {
|
|
1190
|
+
if (current !== undefined) runtime = current;
|
|
1191
|
+
},
|
|
1192
|
+
})(stub.api);
|
|
1193
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
1194
|
+
assert.ok(runtime !== undefined);
|
|
1195
|
+
|
|
1196
|
+
// The harness close outlives the bound; the shutdown drain awaits it exactly once.
|
|
1197
|
+
const harness = runtime.harness as {
|
|
1198
|
+
close: (context: typeof BACKGROUND_CONTEXT) => Promise<void>;
|
|
1199
|
+
};
|
|
1200
|
+
const original = harness.close.bind(runtime.harness);
|
|
1201
|
+
let closes = 0;
|
|
1202
|
+
harness.close = async (context) => {
|
|
1203
|
+
closes += 1;
|
|
1204
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
1205
|
+
await original(context);
|
|
1206
|
+
};
|
|
1207
|
+
|
|
1208
|
+
stub.addTool({ name: "delegate_launch", source: "npm:pi-subagents" });
|
|
1209
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1210
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
1211
|
+
|
|
1212
|
+
assert.strictEqual(closes, 1);
|
|
1213
|
+
const entries = (await readText(join(sessionDirOf(stub), "delegates.log")))
|
|
1214
|
+
.trim()
|
|
1215
|
+
.split("\n")
|
|
1216
|
+
.map((line) => JSON.parse(line) as { code: string });
|
|
1217
|
+
assert.strictEqual(
|
|
1218
|
+
entries.filter((entry) => entry.code === "revocation.close_timeout").length,
|
|
1219
|
+
1,
|
|
1220
|
+
JSON.stringify(entries),
|
|
1221
|
+
);
|
|
1222
|
+
});
|
|
1223
|
+
|
|
1224
|
+
it("a reload swap serves the new runtime when the next tool call runs", async () => {
|
|
1225
|
+
const sessionDir = await makeTempDir("delegates-entry-");
|
|
1226
|
+
const stub = makeStubPi({
|
|
1227
|
+
settings: { packages: [] },
|
|
1228
|
+
sessionId: "s-1",
|
|
1229
|
+
sessionDir,
|
|
1230
|
+
modelRegistry: stubHostSource({ text: "served" }),
|
|
1231
|
+
});
|
|
1232
|
+
const runtimes: SessionRuntime[] = [];
|
|
1233
|
+
createDelegatesExtension({
|
|
1234
|
+
agentDir: await tempAgentDir(),
|
|
1235
|
+
definitions: [
|
|
1236
|
+
defineAgent({
|
|
1237
|
+
name: "scout",
|
|
1238
|
+
role: "Scout",
|
|
1239
|
+
prompt: { role: "You look around." },
|
|
1240
|
+
tools: ["bash"],
|
|
1241
|
+
extensions: [CODING_EXTENSION],
|
|
1242
|
+
approval: { gates: [] },
|
|
1243
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
1244
|
+
}),
|
|
1245
|
+
],
|
|
1246
|
+
onRuntime: (current) => {
|
|
1247
|
+
if (current !== undefined) runtimes.push(current);
|
|
1248
|
+
},
|
|
1249
|
+
})(stub.api);
|
|
1250
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
1251
|
+
await stub.fire("session_start", { type: "session_start", reason: "reload" });
|
|
1252
|
+
assert.strictEqual(runtimes.length, 2);
|
|
1253
|
+
|
|
1254
|
+
const tool = stub.registrations.toolDefinitions.get("delegate_launch") as {
|
|
1255
|
+
execute: (
|
|
1256
|
+
id: string,
|
|
1257
|
+
params: unknown,
|
|
1258
|
+
signal: undefined,
|
|
1259
|
+
onUpdate: undefined,
|
|
1260
|
+
ctx: unknown,
|
|
1261
|
+
) => Promise<{ readonly content: readonly { readonly text: string }[] }>;
|
|
1262
|
+
};
|
|
1263
|
+
const result = await tool.execute(
|
|
1264
|
+
"",
|
|
1265
|
+
{ request: { kind: "single", agent: "scout", task: "look" } },
|
|
1266
|
+
undefined,
|
|
1267
|
+
undefined,
|
|
1268
|
+
stub.context,
|
|
1269
|
+
);
|
|
1270
|
+
|
|
1271
|
+
assert.strictEqual(result.content[0]?.text, "served");
|
|
1272
|
+
assert.strictEqual(runtimes[0]?.state.kind, "closed");
|
|
1273
|
+
const index = await runtimes[1]?.snapshot(RunIndexDoc);
|
|
1274
|
+
assert.strictEqual(index?.runIds.length, 1, JSON.stringify(index));
|
|
1275
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
1276
|
+
});
|