@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,481 @@
|
|
|
1
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { mkdtemp } from "node:fs/promises";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { assert, it } from "@effect/vitest";
|
|
8
|
+
import { stubHostSource } from "../../test-support/stub-model-source.ts";
|
|
9
|
+
import { makeStubPi } from "../../test-support/stub-pi.ts";
|
|
10
|
+
import { defineAgent } from "../domain/definitions.ts";
|
|
11
|
+
import { RunDoc, RunIndexDoc } from "../domain/run-records.ts";
|
|
12
|
+
import { RequestDoc, RequestIndexDoc } from "../domain/orchestration/requests.ts";
|
|
13
|
+
import type { SessionView } from "../domain/view.ts";
|
|
14
|
+
import { nullLogSink, type LogSink } from "../runtime/log.ts";
|
|
15
|
+
import { HostModelAdapter } from "../runtime/models.ts";
|
|
16
|
+
import { openSessionRuntime } from "../runtime/session.ts";
|
|
17
|
+
import { noticeFor, type RefusedVerdict } from "./revocation.ts";
|
|
18
|
+
import { printDelegatesListing, parseAnswerArgs, registerDelegatesCommand } from "./commands.ts";
|
|
19
|
+
|
|
20
|
+
const refused: RefusedVerdict = {
|
|
21
|
+
kind: "refused",
|
|
22
|
+
conflict: "pi-subagents",
|
|
23
|
+
message: "pi-delegates cannot load while pi-subagents is enabled.",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const viewWithRun = (runId: string, diagnostic: string | null = null): SessionView => ({
|
|
27
|
+
runs: [
|
|
28
|
+
{
|
|
29
|
+
runId,
|
|
30
|
+
definition: "scout",
|
|
31
|
+
mode: "single",
|
|
32
|
+
lane: "default",
|
|
33
|
+
conversationId: null,
|
|
34
|
+
taskId: null,
|
|
35
|
+
status: "queued",
|
|
36
|
+
answer: null,
|
|
37
|
+
durationMs: null,
|
|
38
|
+
children: [],
|
|
39
|
+
missing: [],
|
|
40
|
+
gates: [],
|
|
41
|
+
requests: [],
|
|
42
|
+
diagnostic,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
lanes: [],
|
|
46
|
+
gates: [],
|
|
47
|
+
reviews: [],
|
|
48
|
+
requests: [],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("the answer command parses run, request and the rest as the answer", () => {
|
|
52
|
+
assert.deepStrictEqual(parseAnswerArgs("run:1 12:c1 staging west"), {
|
|
53
|
+
run: "run:1",
|
|
54
|
+
request: "12:c1",
|
|
55
|
+
answer: "staging west",
|
|
56
|
+
});
|
|
57
|
+
assert.strictEqual(parseAnswerArgs("run:1"), undefined);
|
|
58
|
+
assert.strictEqual(parseAnswerArgs("run:1 12:c1"), undefined);
|
|
59
|
+
assert.strictEqual(parseAnswerArgs(" "), undefined);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("the delegates command lists each definition's declared skills", async () => {
|
|
63
|
+
const stub = makeStubPi();
|
|
64
|
+
registerDelegatesCommand(stub.api, {
|
|
65
|
+
definitions: [
|
|
66
|
+
defineAgent({
|
|
67
|
+
name: "tester",
|
|
68
|
+
role: "Test agent",
|
|
69
|
+
prompt: { role: "You test." },
|
|
70
|
+
tools: ["read"],
|
|
71
|
+
extensions: [],
|
|
72
|
+
approval: { gates: [] },
|
|
73
|
+
skills: ["alpha", "beta"],
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
76
|
+
log: () => nullLogSink,
|
|
77
|
+
});
|
|
78
|
+
const handler = stub.registrations.commandHandlers.get("delegates");
|
|
79
|
+
assert.ok(handler !== undefined);
|
|
80
|
+
await handler("", stub.context);
|
|
81
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
82
|
+
assert.ok(notice.includes("skills: alpha, beta"), notice);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("the listing prints each run's mode except for single", async () => {
|
|
86
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-command-"));
|
|
87
|
+
const runtime = await openSessionRuntime({
|
|
88
|
+
identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
89
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
90
|
+
});
|
|
91
|
+
try {
|
|
92
|
+
await runtime.harness.commit(async (tx) => {
|
|
93
|
+
const workflow = await tx.doc(RunDoc, "run:wf", {
|
|
94
|
+
definition: "composer",
|
|
95
|
+
lane: "workflow",
|
|
96
|
+
mode: "workflow",
|
|
97
|
+
});
|
|
98
|
+
workflow.requestedAgent = "composer";
|
|
99
|
+
const single = await tx.doc(RunDoc, "run:one", { definition: "scout", lane: "default" });
|
|
100
|
+
single.requestedAgent = "scout";
|
|
101
|
+
const index = await tx.doc(RunIndexDoc);
|
|
102
|
+
index.runIds.push("run:wf", "run:one");
|
|
103
|
+
}, BACKGROUND_CONTEXT);
|
|
104
|
+
|
|
105
|
+
const stub = makeStubPi();
|
|
106
|
+
await printDelegatesListing(stub.context as ExtensionContext, {
|
|
107
|
+
definitions: [],
|
|
108
|
+
runtime: () => runtime,
|
|
109
|
+
log: () => nullLogSink,
|
|
110
|
+
});
|
|
111
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
112
|
+
assert.ok(notice.includes("run:wf composer · workflow · queued"), notice);
|
|
113
|
+
assert.ok(notice.includes("run:one scout · queued"), notice);
|
|
114
|
+
assert.ok(!notice.includes("single"), notice);
|
|
115
|
+
} finally {
|
|
116
|
+
await runtime.close();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("the refused command prints the message and the runs", async () => {
|
|
121
|
+
const stub = makeStubPi({ mode: "rpc" });
|
|
122
|
+
registerDelegatesCommand(stub.api, {
|
|
123
|
+
definitions: [],
|
|
124
|
+
log: () => nullLogSink,
|
|
125
|
+
revoked: () => refused,
|
|
126
|
+
reader: () => Promise.resolve(viewWithRun("run:kept")),
|
|
127
|
+
});
|
|
128
|
+
const handler = stub.registrations.commandHandlers.get("delegates");
|
|
129
|
+
assert.ok(handler !== undefined);
|
|
130
|
+
await handler("", stub.context);
|
|
131
|
+
|
|
132
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
133
|
+
assert.ok(notice.startsWith(refused.message), notice);
|
|
134
|
+
assert.strictEqual(notice.split("\n").at(-1), "run:kept scout · queued");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("the refused listing prints the same run line as the live listing", async () => {
|
|
138
|
+
const stub = makeStubPi({ mode: "rpc" });
|
|
139
|
+
registerDelegatesCommand(stub.api, {
|
|
140
|
+
definitions: [],
|
|
141
|
+
log: () => nullLogSink,
|
|
142
|
+
revoked: () => refused,
|
|
143
|
+
reader: () => Promise.resolve(viewWithRun("run:kept", "why it stopped")),
|
|
144
|
+
});
|
|
145
|
+
const handler = stub.registrations.commandHandlers.get("delegates:list");
|
|
146
|
+
assert.ok(handler !== undefined);
|
|
147
|
+
await handler("", stub.context);
|
|
148
|
+
|
|
149
|
+
assert.strictEqual(
|
|
150
|
+
stub.notices.at(-1)?.message.split("\n").at(-1),
|
|
151
|
+
"run:kept scout · queued (why it stopped)",
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("a refused listing on a missing store prints the message alone", async () => {
|
|
156
|
+
const stub = makeStubPi({ mode: "rpc" });
|
|
157
|
+
registerDelegatesCommand(stub.api, {
|
|
158
|
+
definitions: [],
|
|
159
|
+
log: () => nullLogSink,
|
|
160
|
+
revoked: () => refused,
|
|
161
|
+
reader: () => Promise.resolve(undefined),
|
|
162
|
+
});
|
|
163
|
+
const handler = stub.registrations.commandHandlers.get("delegates:list");
|
|
164
|
+
assert.ok(handler !== undefined);
|
|
165
|
+
await handler("", stub.context);
|
|
166
|
+
|
|
167
|
+
assert.deepStrictEqual(
|
|
168
|
+
stub.notices.map((entry) => entry.message),
|
|
169
|
+
[noticeFor(refused)],
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("a refused listing with a failing reader prints the message and one line", async () => {
|
|
174
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-command-"));
|
|
175
|
+
const stub = makeStubPi({ sessionDir, mode: "rpc" });
|
|
176
|
+
registerDelegatesCommand(stub.api, {
|
|
177
|
+
definitions: [],
|
|
178
|
+
log: () => nullLogSink,
|
|
179
|
+
revoked: () => refused,
|
|
180
|
+
reader: () => Promise.reject(new Error("the store is gone")),
|
|
181
|
+
});
|
|
182
|
+
const handler = stub.registrations.commandHandlers.get("delegates:list");
|
|
183
|
+
assert.ok(handler !== undefined);
|
|
184
|
+
await handler("", stub.context);
|
|
185
|
+
|
|
186
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
187
|
+
assert.ok(notice.startsWith(refused.message), notice);
|
|
188
|
+
assert.strictEqual(notice.split("\n").at(-1), "The stored runs could not be read.");
|
|
189
|
+
assert.ok(notice.startsWith(noticeFor(refused)), notice);
|
|
190
|
+
const entries = readFileSync(join(sessionDir, "delegates.log"), "utf8")
|
|
191
|
+
.trim()
|
|
192
|
+
.split("\n")
|
|
193
|
+
.map((line) => JSON.parse(line) as { code: string });
|
|
194
|
+
assert.deepStrictEqual(
|
|
195
|
+
entries.map((entry) => entry.code),
|
|
196
|
+
["command.viewer_failed"],
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("the refused command never toggles the inspector", async () => {
|
|
201
|
+
const stub = makeStubPi({ mode: "tui" });
|
|
202
|
+
let toggled = 0;
|
|
203
|
+
registerDelegatesCommand(stub.api, {
|
|
204
|
+
definitions: [],
|
|
205
|
+
log: () => nullLogSink,
|
|
206
|
+
revoked: () => refused,
|
|
207
|
+
reader: () => Promise.resolve(viewWithRun("run:kept")),
|
|
208
|
+
inspector: () => {
|
|
209
|
+
toggled += 1;
|
|
210
|
+
return Promise.resolve();
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
const handler = stub.registrations.commandHandlers.get("delegates");
|
|
214
|
+
assert.ok(handler !== undefined);
|
|
215
|
+
await handler("", stub.context);
|
|
216
|
+
|
|
217
|
+
assert.strictEqual(toggled, 0);
|
|
218
|
+
assert.ok((stub.notices.at(-1)?.message ?? "").startsWith(refused.message));
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("the refused listing prints a pending request", async () => {
|
|
222
|
+
const stub = makeStubPi();
|
|
223
|
+
const view: SessionView = {
|
|
224
|
+
...viewWithRun("run:kept"),
|
|
225
|
+
requests: [
|
|
226
|
+
{
|
|
227
|
+
runId: "run:kept",
|
|
228
|
+
childRunId: "run:kept",
|
|
229
|
+
requestId: "12:c1",
|
|
230
|
+
conversationId: 12,
|
|
231
|
+
prompt: "Which environment?",
|
|
232
|
+
options: [],
|
|
233
|
+
allowOther: true,
|
|
234
|
+
status: "pending",
|
|
235
|
+
taskIds: [],
|
|
236
|
+
requestedAt: 1_000,
|
|
237
|
+
deadline: Number.MAX_SAFE_INTEGER,
|
|
238
|
+
answer: null,
|
|
239
|
+
answeredBy: null,
|
|
240
|
+
reason: null,
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
};
|
|
244
|
+
registerDelegatesCommand(stub.api, {
|
|
245
|
+
definitions: [],
|
|
246
|
+
log: () => nullLogSink,
|
|
247
|
+
revoked: () => refused,
|
|
248
|
+
reader: () => Promise.resolve(view),
|
|
249
|
+
});
|
|
250
|
+
const handler = stub.registrations.commandHandlers.get("delegates:list");
|
|
251
|
+
assert.ok(handler !== undefined);
|
|
252
|
+
await handler("", stub.context);
|
|
253
|
+
|
|
254
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
255
|
+
assert.ok(notice.startsWith(refused.message), notice);
|
|
256
|
+
assert.ok(notice.includes("request 12:c1 pending for run:kept"), notice);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("the listing adds a request line", async () => {
|
|
260
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-command-"));
|
|
261
|
+
const runtime = await openSessionRuntime({
|
|
262
|
+
identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
263
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
264
|
+
now: () => 10_000,
|
|
265
|
+
});
|
|
266
|
+
try {
|
|
267
|
+
await runtime.harness.commit(async (tx) => {
|
|
268
|
+
await tx.doc(RunDoc, "run:one", { definition: "scout", lane: "default" });
|
|
269
|
+
const record = await tx.doc(RequestDoc, "12:c1", { requestId: "12:c1" });
|
|
270
|
+
record.runId = "run:one";
|
|
271
|
+
record.childRunId = "run:one";
|
|
272
|
+
record.conversationId = 12;
|
|
273
|
+
record.prompt = "Which environment?";
|
|
274
|
+
record.status = "pending";
|
|
275
|
+
record.requestedAt = 1_000;
|
|
276
|
+
record.deadline = 20_000;
|
|
277
|
+
const index = await tx.doc(RequestIndexDoc);
|
|
278
|
+
index.rows.push({
|
|
279
|
+
key: "12:c1",
|
|
280
|
+
runId: "run:one",
|
|
281
|
+
requestId: "12:c1",
|
|
282
|
+
requestedAt: 1_000,
|
|
283
|
+
deadline: 20_000,
|
|
284
|
+
status: "pending",
|
|
285
|
+
});
|
|
286
|
+
}, BACKGROUND_CONTEXT);
|
|
287
|
+
|
|
288
|
+
const stub = makeStubPi();
|
|
289
|
+
await printDelegatesListing(stub.context as ExtensionContext, {
|
|
290
|
+
definitions: [],
|
|
291
|
+
runtime: () => runtime,
|
|
292
|
+
log: () => nullLogSink,
|
|
293
|
+
});
|
|
294
|
+
const notice = stub.notices.at(-1)?.message ?? "";
|
|
295
|
+
assert.ok(notice.includes("request 12:c1 pending for run:one"), notice);
|
|
296
|
+
|
|
297
|
+
await runtime.harness.commit(async (tx) => {
|
|
298
|
+
const record = await tx.doc(RequestDoc, "12:c1", { requestId: "12:c1" });
|
|
299
|
+
record.deadline = 5_000;
|
|
300
|
+
}, BACKGROUND_CONTEXT);
|
|
301
|
+
await printDelegatesListing(stub.context as ExtensionContext, {
|
|
302
|
+
definitions: [],
|
|
303
|
+
runtime: () => runtime,
|
|
304
|
+
log: () => nullLogSink,
|
|
305
|
+
});
|
|
306
|
+
const expired = stub.notices.at(-1)?.message ?? "";
|
|
307
|
+
assert.ok(!expired.includes("request 12:c1 pending"), expired);
|
|
308
|
+
} finally {
|
|
309
|
+
await runtime.close();
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("the answer picker offers free text only when the request allows it", async () => {
|
|
314
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-answer-"));
|
|
315
|
+
const runtime = await openSessionRuntime({
|
|
316
|
+
identity: { sessionId: "s-offer", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
317
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
318
|
+
});
|
|
319
|
+
const option = (label: string) => ({ label, description: null, recommended: false });
|
|
320
|
+
try {
|
|
321
|
+
await runtime.harness.commit(async (tx) => {
|
|
322
|
+
const index = await tx.doc(RequestIndexDoc);
|
|
323
|
+
for (const key of ["12:c1", "12:c2"]) {
|
|
324
|
+
const record = await tx.doc(RequestDoc, key, { requestId: key });
|
|
325
|
+
record.runId = "run:1";
|
|
326
|
+
record.childRunId = "run:1";
|
|
327
|
+
record.conversationId = 12;
|
|
328
|
+
record.prompt = "Which environment?";
|
|
329
|
+
record.options = [option("staging"), option("production")];
|
|
330
|
+
record.allowOther = key === "12:c1";
|
|
331
|
+
record.requestedAt = 1_000;
|
|
332
|
+
record.deadline = Number.MAX_SAFE_INTEGER;
|
|
333
|
+
index.rows.push({
|
|
334
|
+
key,
|
|
335
|
+
runId: "run:1",
|
|
336
|
+
requestId: key,
|
|
337
|
+
requestedAt: 1_000,
|
|
338
|
+
deadline: Number.MAX_SAFE_INTEGER,
|
|
339
|
+
status: "pending",
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
}, BACKGROUND_CONTEXT);
|
|
343
|
+
|
|
344
|
+
const stub = makeStubPi();
|
|
345
|
+
registerDelegatesCommand(stub.api, {
|
|
346
|
+
definitions: [],
|
|
347
|
+
log: () => nullLogSink,
|
|
348
|
+
runtime: () => runtime,
|
|
349
|
+
});
|
|
350
|
+
const handler = stub.registrations.commandHandlers.get("delegates:answer");
|
|
351
|
+
assert.ok(handler !== undefined);
|
|
352
|
+
const offers = async (request: string): Promise<string[][]> => {
|
|
353
|
+
const calls: string[][] = [];
|
|
354
|
+
await handler("", {
|
|
355
|
+
ui: {
|
|
356
|
+
notify: () => {},
|
|
357
|
+
select: async (_title: string, options: string[]) => {
|
|
358
|
+
calls.push([...options]);
|
|
359
|
+
return calls.length === 1 ? request : undefined;
|
|
360
|
+
},
|
|
361
|
+
input: async () => undefined,
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
return calls;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const open = await offers("run:1 · 12:c1 · Which environment?");
|
|
368
|
+
assert.deepStrictEqual(open[1], ["staging", "production", "Type something."]);
|
|
369
|
+
const fixed = await offers("run:1 · 12:c2 · Which environment?");
|
|
370
|
+
assert.deepStrictEqual(fixed[1], ["staging", "production"]);
|
|
371
|
+
} finally {
|
|
372
|
+
await runtime.close();
|
|
373
|
+
}
|
|
374
|
+
}, 30_000);
|
|
375
|
+
|
|
376
|
+
it("a failed direct answer writes one command.answer_failed line", async () => {
|
|
377
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-answer-"));
|
|
378
|
+
const runtime = await openSessionRuntime({
|
|
379
|
+
identity: { sessionId: "s-direct", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
380
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
381
|
+
});
|
|
382
|
+
try {
|
|
383
|
+
const codes: string[] = [];
|
|
384
|
+
const log: LogSink = { write: (_level, code) => void codes.push(code) };
|
|
385
|
+
const stub = makeStubPi();
|
|
386
|
+
registerDelegatesCommand(stub.api, {
|
|
387
|
+
definitions: [],
|
|
388
|
+
log: () => log,
|
|
389
|
+
runtime: () => runtime,
|
|
390
|
+
});
|
|
391
|
+
const handler = stub.registrations.commandHandlers.get("delegates:answer");
|
|
392
|
+
assert.ok(handler !== undefined);
|
|
393
|
+
const notices: string[] = [];
|
|
394
|
+
await handler("run:1 12:missing staging", {
|
|
395
|
+
ui: {
|
|
396
|
+
notify: (message: string) => void notices.push(message),
|
|
397
|
+
select: async () => undefined,
|
|
398
|
+
input: async () => undefined,
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
assert.strictEqual(notices.length, 1);
|
|
403
|
+
assert.deepStrictEqual(codes, ["command.answer_failed"]);
|
|
404
|
+
} finally {
|
|
405
|
+
await runtime.close();
|
|
406
|
+
}
|
|
407
|
+
}, 30_000);
|
|
408
|
+
|
|
409
|
+
it("the answer command declines only when the answer dialog is cancelled", async () => {
|
|
410
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-answer-"));
|
|
411
|
+
const runtime = await openSessionRuntime({
|
|
412
|
+
identity: { sessionId: "s-answer", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
413
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
414
|
+
});
|
|
415
|
+
const option = (label: string) => ({ label, description: null, recommended: false });
|
|
416
|
+
try {
|
|
417
|
+
await runtime.harness.commit(async (tx) => {
|
|
418
|
+
const index = await tx.doc(RequestIndexDoc);
|
|
419
|
+
for (const key of ["12:c1", "12:c2"]) {
|
|
420
|
+
const record = await tx.doc(RequestDoc, key, { requestId: key });
|
|
421
|
+
record.runId = "run:1";
|
|
422
|
+
record.childRunId = "run:1";
|
|
423
|
+
record.conversationId = 12;
|
|
424
|
+
record.prompt = "Which environment?";
|
|
425
|
+
record.options = [option("staging"), option("production")];
|
|
426
|
+
record.requestedAt = 1_000;
|
|
427
|
+
record.deadline = Number.MAX_SAFE_INTEGER;
|
|
428
|
+
index.rows.push({
|
|
429
|
+
key,
|
|
430
|
+
runId: "run:1",
|
|
431
|
+
requestId: key,
|
|
432
|
+
requestedAt: 1_000,
|
|
433
|
+
deadline: Number.MAX_SAFE_INTEGER,
|
|
434
|
+
status: "pending",
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
}, BACKGROUND_CONTEXT);
|
|
438
|
+
|
|
439
|
+
const stub = makeStubPi();
|
|
440
|
+
registerDelegatesCommand(stub.api, {
|
|
441
|
+
definitions: [],
|
|
442
|
+
log: () => nullLogSink,
|
|
443
|
+
runtime: () => runtime,
|
|
444
|
+
});
|
|
445
|
+
const handler = stub.registrations.commandHandlers.get("delegates:answer");
|
|
446
|
+
assert.ok(handler !== undefined);
|
|
447
|
+
const run = async (args: string, choices: (string | undefined)[]) => {
|
|
448
|
+
const notices: { message: string; severity?: string }[] = [];
|
|
449
|
+
await handler(args, {
|
|
450
|
+
ui: {
|
|
451
|
+
notify: (message: string, severity?: string) =>
|
|
452
|
+
notices.push({ message, ...(severity === undefined ? {} : { severity }) }),
|
|
453
|
+
select: async () => choices.shift(),
|
|
454
|
+
input: async () => undefined,
|
|
455
|
+
},
|
|
456
|
+
});
|
|
457
|
+
return notices;
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
// Cancelling the request picker leaves the request pending.
|
|
461
|
+
assert.deepStrictEqual(await run("", []), []);
|
|
462
|
+
assert.strictEqual((await runtime.snapshot(RequestDoc, "12:c1"))?.status, "pending");
|
|
463
|
+
|
|
464
|
+
// The direct form answers, and the picker's answer dialog can then decline.
|
|
465
|
+
const answered = await run("run:1 12:c1 staging", []);
|
|
466
|
+
assert.deepStrictEqual(
|
|
467
|
+
answered.map((notice) => notice.message),
|
|
468
|
+
["answer run:1: answered"],
|
|
469
|
+
);
|
|
470
|
+
const declined = await run("", ["run:1 · 12:c2 · Which environment?", undefined]);
|
|
471
|
+
assert.deepStrictEqual(
|
|
472
|
+
declined.map((notice) => notice.message),
|
|
473
|
+
["decline run:1: cancelled"],
|
|
474
|
+
);
|
|
475
|
+
const record = await runtime.snapshot(RequestDoc, "12:c2");
|
|
476
|
+
assert.strictEqual(record?.status, "cancelled");
|
|
477
|
+
assert.strictEqual(record?.reason, "declined");
|
|
478
|
+
} finally {
|
|
479
|
+
await runtime.close();
|
|
480
|
+
}
|
|
481
|
+
}, 30_000);
|