@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,686 @@
|
|
|
1
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
2
|
+
import { assert, it } from "@effect/vitest";
|
|
3
|
+
import { rejects } from "../../../test-support/assertions.ts";
|
|
4
|
+
import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
|
|
5
|
+
import { ruleCallArbitrary, ruleListArbitrary } from "../../../test-support/arbitraries.ts";
|
|
6
|
+
import { mkdtemp } from "node:fs/promises";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { stubHostSource } from "../../../test-support/stub-model-source.ts";
|
|
10
|
+
import { createCatalogue, defineAgent } from "../definitions.ts";
|
|
11
|
+
import { CODING_EXTENSION } from "../names.ts";
|
|
12
|
+
import { launch, type HostDefaults } from "./launch.ts";
|
|
13
|
+
import { control } from "./control.ts";
|
|
14
|
+
import {
|
|
15
|
+
DAY_MS,
|
|
16
|
+
GateAlreadyDecided,
|
|
17
|
+
GateDoc,
|
|
18
|
+
GateIndexDoc,
|
|
19
|
+
gateKey,
|
|
20
|
+
gateRecords,
|
|
21
|
+
gatedTool,
|
|
22
|
+
matchRule,
|
|
23
|
+
pendingGates,
|
|
24
|
+
type GateRecord,
|
|
25
|
+
} from "./gates.ts";
|
|
26
|
+
import { RunDoc } from "../run-records.ts";
|
|
27
|
+
import { viewRun } from "../view.ts";
|
|
28
|
+
import { HostModelAdapter } from "../../runtime/models.ts";
|
|
29
|
+
import { countingRuntime } from "../../../test-support/counting-runtime.ts";
|
|
30
|
+
import { openSessionRuntime, type SessionIdentity } from "../../runtime/session.ts";
|
|
31
|
+
|
|
32
|
+
const host: HostDefaults = {
|
|
33
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
34
|
+
thinkingLevel: "low",
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const deployer = defineAgent({
|
|
38
|
+
name: "deployer",
|
|
39
|
+
role: "Deploy agent",
|
|
40
|
+
prompt: { role: "You deploy things." },
|
|
41
|
+
tools: ["bash"],
|
|
42
|
+
extensions: [CODING_EXTENSION],
|
|
43
|
+
approval: { gates: [{ name: "deploy", tools: ["bash"], match: "deploy" }] },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const releaser = defineAgent({
|
|
47
|
+
name: "releaser",
|
|
48
|
+
role: "Release agent",
|
|
49
|
+
prompt: { role: "You release things." },
|
|
50
|
+
tools: ["bash"],
|
|
51
|
+
extensions: [CODING_EXTENSION],
|
|
52
|
+
approval: { gates: [{ name: "release", tools: ["bash"] }] },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const catalogue = createCatalogue([deployer, releaser]);
|
|
56
|
+
|
|
57
|
+
const identityFor = async (): Promise<SessionIdentity> => {
|
|
58
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-gates-"));
|
|
59
|
+
return { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir };
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const deferred = (): { readonly promise: Promise<void>; readonly release: () => void } => {
|
|
63
|
+
let release: () => void = () => {};
|
|
64
|
+
const promise = new Promise<void>((resolve) => {
|
|
65
|
+
release = resolve;
|
|
66
|
+
});
|
|
67
|
+
return { promise, release };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const waitForGateTasks = async (
|
|
71
|
+
runtime: Awaited<ReturnType<typeof openSessionRuntime>>,
|
|
72
|
+
key: string,
|
|
73
|
+
count: number,
|
|
74
|
+
): Promise<GateRecord> => {
|
|
75
|
+
const started = Date.now();
|
|
76
|
+
while (Date.now() - started < 10_000) {
|
|
77
|
+
const record = await runtime.snapshot(GateDoc, key);
|
|
78
|
+
if (record !== undefined && record.taskIds.length >= count) return record;
|
|
79
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
80
|
+
}
|
|
81
|
+
const record = await runtime.snapshot(GateDoc, key);
|
|
82
|
+
throw new Error(`gate ${key} did not park ${count} tasks: ${JSON.stringify(record)}`);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const openRuntime = async (
|
|
86
|
+
toolCall: boolean,
|
|
87
|
+
now?: () => number,
|
|
88
|
+
): Promise<Awaited<ReturnType<typeof openSessionRuntime>>> =>
|
|
89
|
+
openSessionRuntime({
|
|
90
|
+
identity: await identityFor(),
|
|
91
|
+
models: new HostModelAdapter(
|
|
92
|
+
stubHostSource({
|
|
93
|
+
text: "deployed",
|
|
94
|
+
...(toolCall ? { toolCall: { name: "bash", arguments: { command: "echo deploy" } } } : {}),
|
|
95
|
+
}),
|
|
96
|
+
),
|
|
97
|
+
...(now === undefined ? {} : { now }),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const parkedRun = async (runtime: Awaited<ReturnType<typeof openSessionRuntime>>) => {
|
|
101
|
+
const result = await launch(
|
|
102
|
+
{ kind: "single", agent: "deployer", task: "deploy it" },
|
|
103
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
104
|
+
);
|
|
105
|
+
return result.view;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
it("a gated call parks and commits a pending gate", async () => {
|
|
109
|
+
const runtime = await openRuntime(true);
|
|
110
|
+
try {
|
|
111
|
+
const view = await parkedRun(runtime);
|
|
112
|
+
assert.strictEqual(view.status, "waiting");
|
|
113
|
+
assert.deepStrictEqual(view.gates, ["deploy"]);
|
|
114
|
+
|
|
115
|
+
const key = gateKey(view.runId, "deploy");
|
|
116
|
+
const record = await runtime.snapshot(GateDoc, key);
|
|
117
|
+
assert.strictEqual(record?.status, "pending");
|
|
118
|
+
assert.strictEqual(record?.gate, "deploy");
|
|
119
|
+
assert.strictEqual(record?.tool, "bash");
|
|
120
|
+
assert.notStrictEqual(record?.taskId, null);
|
|
121
|
+
|
|
122
|
+
const doc = await runtime.snapshot(RunDoc, view.runId);
|
|
123
|
+
const conversation = await runtime.harness.conversation(
|
|
124
|
+
doc!.childConversationId as never,
|
|
125
|
+
BACKGROUND_CONTEXT,
|
|
126
|
+
);
|
|
127
|
+
const context = await conversation!.context(BACKGROUND_CONTEXT);
|
|
128
|
+
const parkedResult = context.messages.find((message) => message.role === "toolResult");
|
|
129
|
+
const parkedText = (parkedResult?.content ?? [])
|
|
130
|
+
.map((part) => (part.type === "text" ? part.text : ""))
|
|
131
|
+
.join("");
|
|
132
|
+
assert.ok(!parkedText.includes("deploy\n"), parkedText);
|
|
133
|
+
assert.ok(parkedText.includes("unavailable") || parkedText.length === 0, parkedText);
|
|
134
|
+
} finally {
|
|
135
|
+
await runtime.close();
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("an approval commits once and the call runs", async () => {
|
|
140
|
+
const runtime = await openRuntime(true);
|
|
141
|
+
try {
|
|
142
|
+
const view = await parkedRun(runtime);
|
|
143
|
+
const approved = await control(
|
|
144
|
+
{ action: "approve", run: view.runId, gate: "deploy" },
|
|
145
|
+
{
|
|
146
|
+
runtime,
|
|
147
|
+
wait: { policy: "settle", waitMs: 10_000 },
|
|
148
|
+
},
|
|
149
|
+
);
|
|
150
|
+
assert.strictEqual(approved.view!.status, "answered");
|
|
151
|
+
assert.strictEqual(approved.view!.children[0]?.answer, "deployed");
|
|
152
|
+
|
|
153
|
+
const record = await runtime.snapshot(GateDoc, gateKey(view.runId, "deploy"));
|
|
154
|
+
assert.strictEqual(record?.status, "approved");
|
|
155
|
+
assert.strictEqual(record?.decidedBy?.sessionId, "s-1");
|
|
156
|
+
|
|
157
|
+
await rejects(
|
|
158
|
+
control(
|
|
159
|
+
{ action: "approve", run: view.runId, gate: "deploy" },
|
|
160
|
+
{ runtime, wait: { policy: "settle", waitMs: 1_000 } },
|
|
161
|
+
),
|
|
162
|
+
(error: unknown) => error instanceof Error && error.message.includes("approved"),
|
|
163
|
+
);
|
|
164
|
+
} finally {
|
|
165
|
+
await runtime.close();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("two concurrent approvals commit one decision", async () => {
|
|
170
|
+
const runtime = await openRuntime(true);
|
|
171
|
+
try {
|
|
172
|
+
const view = await parkedRun(runtime);
|
|
173
|
+
const outcomes = await Promise.allSettled([
|
|
174
|
+
control(
|
|
175
|
+
{ action: "approve", run: view.runId, gate: "deploy" },
|
|
176
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
177
|
+
),
|
|
178
|
+
control(
|
|
179
|
+
{ action: "approve", run: view.runId, gate: "deploy" },
|
|
180
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
181
|
+
),
|
|
182
|
+
]);
|
|
183
|
+
const fulfilled = outcomes.filter((outcome) => outcome.status === "fulfilled");
|
|
184
|
+
const rejected = outcomes.filter((outcome) => outcome.status === "rejected");
|
|
185
|
+
assert.strictEqual(fulfilled.length, 1);
|
|
186
|
+
assert.strictEqual(rejected.length, 1);
|
|
187
|
+
const loser = rejected[0];
|
|
188
|
+
assert.ok(loser?.status === "rejected" && loser.reason instanceof GateAlreadyDecided);
|
|
189
|
+
|
|
190
|
+
const record = await runtime.snapshot(GateDoc, gateKey(view.runId, "deploy"));
|
|
191
|
+
assert.strictEqual(record?.status, "approved");
|
|
192
|
+
} finally {
|
|
193
|
+
await runtime.close();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("a concurrent approval and denial keep exactly one decision", async () => {
|
|
198
|
+
const runtime = await openRuntime(true);
|
|
199
|
+
try {
|
|
200
|
+
const view = await parkedRun(runtime);
|
|
201
|
+
const outcomes = await Promise.allSettled([
|
|
202
|
+
control(
|
|
203
|
+
{ action: "approve", run: view.runId, gate: "deploy" },
|
|
204
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
205
|
+
),
|
|
206
|
+
control(
|
|
207
|
+
{ action: "deny", run: view.runId, gate: "deploy" },
|
|
208
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
209
|
+
),
|
|
210
|
+
]);
|
|
211
|
+
const fulfilled = outcomes.filter((outcome) => outcome.status === "fulfilled");
|
|
212
|
+
const rejected = outcomes.filter((outcome) => outcome.status === "rejected");
|
|
213
|
+
assert.strictEqual(fulfilled.length, 1);
|
|
214
|
+
assert.strictEqual(rejected.length, 1);
|
|
215
|
+
const loser = rejected[0];
|
|
216
|
+
assert.ok(loser?.status === "rejected" && loser.reason instanceof GateAlreadyDecided);
|
|
217
|
+
|
|
218
|
+
const record = await runtime.snapshot(GateDoc, gateKey(view.runId, "deploy"));
|
|
219
|
+
assert.ok(
|
|
220
|
+
record?.status === "approved" || record?.status === "denied",
|
|
221
|
+
record?.status ?? "missing",
|
|
222
|
+
);
|
|
223
|
+
assert.strictEqual(record?.decision, record?.status === "approved" ? "approve" : "deny");
|
|
224
|
+
} finally {
|
|
225
|
+
await runtime.close();
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("a denial ends the call blocked and does not run the tool", async () => {
|
|
230
|
+
const runtime = await openRuntime(true);
|
|
231
|
+
try {
|
|
232
|
+
const view = await parkedRun(runtime);
|
|
233
|
+
const denied = await control(
|
|
234
|
+
{ action: "deny", run: view.runId, gate: "deploy" },
|
|
235
|
+
{
|
|
236
|
+
runtime,
|
|
237
|
+
wait: { policy: "settle", waitMs: 10_000 },
|
|
238
|
+
},
|
|
239
|
+
);
|
|
240
|
+
assert.strictEqual(denied.view!.status, "answered");
|
|
241
|
+
const record = await runtime.snapshot(GateDoc, gateKey(view.runId, "deploy"));
|
|
242
|
+
assert.strictEqual(record?.status, "denied");
|
|
243
|
+
|
|
244
|
+
const doc = await runtime.snapshot(RunDoc, view.runId);
|
|
245
|
+
const conversation = await runtime.harness.conversation(
|
|
246
|
+
doc!.childConversationId as never,
|
|
247
|
+
BACKGROUND_CONTEXT,
|
|
248
|
+
);
|
|
249
|
+
const context = await conversation!.context(BACKGROUND_CONTEXT);
|
|
250
|
+
const toolResult = context.messages.find((message) => message.role === "toolResult");
|
|
251
|
+
const text = (toolResult?.content ?? [])
|
|
252
|
+
.map((part) => (part.type === "text" ? part.text : ""))
|
|
253
|
+
.join("");
|
|
254
|
+
assert.ok(text.includes("denied"), text);
|
|
255
|
+
} finally {
|
|
256
|
+
await runtime.close();
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("a partial wake reports the ended task and wakes the rest", async () => {
|
|
261
|
+
const runtime = await openSessionRuntime({
|
|
262
|
+
identity: await identityFor(),
|
|
263
|
+
models: new HostModelAdapter(
|
|
264
|
+
stubHostSource({
|
|
265
|
+
text: "deployed",
|
|
266
|
+
toolCallAt: [1, 2],
|
|
267
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
268
|
+
}),
|
|
269
|
+
),
|
|
270
|
+
});
|
|
271
|
+
try {
|
|
272
|
+
const result = await launch(
|
|
273
|
+
{
|
|
274
|
+
kind: "fanout",
|
|
275
|
+
lane: "deploy",
|
|
276
|
+
agents: [
|
|
277
|
+
{ agent: "deployer", task: "first deploy" },
|
|
278
|
+
{ agent: "deployer", task: "second deploy" },
|
|
279
|
+
],
|
|
280
|
+
},
|
|
281
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
282
|
+
);
|
|
283
|
+
const key = gateKey(result.view.runId, "deploy");
|
|
284
|
+
const pending = await waitForGateTasks(runtime, key, 2);
|
|
285
|
+
|
|
286
|
+
const stoppedRunId = result.view.children[0]?.runId;
|
|
287
|
+
assert.ok(stoppedRunId !== undefined);
|
|
288
|
+
await control(
|
|
289
|
+
{ action: "stop", run: stoppedRunId },
|
|
290
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
const approved = await control(
|
|
294
|
+
{ action: "approve", run: result.view.runId, gate: "deploy" },
|
|
295
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
296
|
+
);
|
|
297
|
+
assert.strictEqual(approved.gate?.gate, "deploy");
|
|
298
|
+
assert.strictEqual(approved.gate?.decision, "approve");
|
|
299
|
+
assert.strictEqual(approved.gate?.endedTaskIds.length, 1);
|
|
300
|
+
assert.strictEqual(approved.gate?.deliveredTaskIds.length, 1);
|
|
301
|
+
assert.deepStrictEqual(
|
|
302
|
+
[...approved.gate!.deliveredTaskIds, ...approved.gate!.endedTaskIds].sort(
|
|
303
|
+
(left, right) => left - right,
|
|
304
|
+
),
|
|
305
|
+
[...pending.taskIds].sort((left, right) => left - right),
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
const record = await runtime.snapshot(GateDoc, key);
|
|
309
|
+
assert.strictEqual(record?.status, "approved");
|
|
310
|
+
} finally {
|
|
311
|
+
await runtime.close();
|
|
312
|
+
}
|
|
313
|
+
}, 20_000);
|
|
314
|
+
|
|
315
|
+
it("the rule only matches its own commands", () => {
|
|
316
|
+
const rules = deployer.approval.gates;
|
|
317
|
+
assert.ok(matchRule(rules, "bash", { command: "echo deploy" }) !== undefined);
|
|
318
|
+
assert.strictEqual(matchRule(rules, "bash", { command: "ls" }), undefined);
|
|
319
|
+
assert.strictEqual(matchRule(rules, "bash", { command: "Deploy prod" }), undefined);
|
|
320
|
+
assert.strictEqual(matchRule(rules, "read", { command: "echo deploy" }), undefined);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it.prop(
|
|
324
|
+
"a rule matches exactly its tool and its substring",
|
|
325
|
+
{ rules: ruleListArbitrary, call: ruleCallArbitrary },
|
|
326
|
+
({ rules, call }) => {
|
|
327
|
+
const matched = matchRule(rules, call.tool, { command: call.command });
|
|
328
|
+
const expected = rules.find(
|
|
329
|
+
(rule) =>
|
|
330
|
+
rule.tools.includes(call.tool) &&
|
|
331
|
+
(rule.match === undefined || (call.tool === "bash" && call.command.includes(rule.match))),
|
|
332
|
+
);
|
|
333
|
+
assert.deepStrictEqual(matched, expected);
|
|
334
|
+
},
|
|
335
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
it("expiry ends the call blocked", async () => {
|
|
339
|
+
const committed: GateRecord[] = [];
|
|
340
|
+
const fakeTool = {
|
|
341
|
+
name: "bash",
|
|
342
|
+
parameters: {},
|
|
343
|
+
execute: async () => ({ output: [{ type: "text", text: "ran" }] }),
|
|
344
|
+
} as unknown as Parameters<typeof gatedTool>[0];
|
|
345
|
+
const pending: GateRecord = {
|
|
346
|
+
runId: "run-1",
|
|
347
|
+
gate: "deploy",
|
|
348
|
+
status: "pending",
|
|
349
|
+
tool: "bash",
|
|
350
|
+
taskId: 7,
|
|
351
|
+
taskIds: [7],
|
|
352
|
+
childRunIds: [],
|
|
353
|
+
requestedAt: 0,
|
|
354
|
+
deadline: 10,
|
|
355
|
+
decision: null,
|
|
356
|
+
decidedBy: null,
|
|
357
|
+
reason: null,
|
|
358
|
+
};
|
|
359
|
+
const api = {
|
|
360
|
+
conversationId: 1,
|
|
361
|
+
taskId: 7,
|
|
362
|
+
now: () => 100,
|
|
363
|
+
memo: async () => 10,
|
|
364
|
+
snapshot: async (token: { readonly definition: { readonly kind: string } }) =>
|
|
365
|
+
token.definition.kind === "delegates.toolkit"
|
|
366
|
+
? { runId: "run-1", approval: deployer.approval }
|
|
367
|
+
: pending,
|
|
368
|
+
commit: async (change: (tx: unknown) => unknown) => {
|
|
369
|
+
const doc = { ...pending };
|
|
370
|
+
const tx = { doc: async () => doc };
|
|
371
|
+
await change(tx);
|
|
372
|
+
committed.push(doc as GateRecord);
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
const result = await (
|
|
376
|
+
gatedTool(fakeTool).execute as unknown as (
|
|
377
|
+
args: unknown,
|
|
378
|
+
api: unknown,
|
|
379
|
+
context: unknown,
|
|
380
|
+
) => Promise<{ readonly output: readonly { readonly text?: string }[] }>
|
|
381
|
+
)({ command: "echo deploy" }, api, BACKGROUND_CONTEXT);
|
|
382
|
+
assert.ok(result.output[0]?.text?.includes("expired"), result.output[0]?.text ?? "");
|
|
383
|
+
assert.strictEqual(committed[0]?.status, "expired");
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it("pending gates are visible from the view", async () => {
|
|
387
|
+
const runtime = await openRuntime(true);
|
|
388
|
+
try {
|
|
389
|
+
const view = await parkedRun(runtime);
|
|
390
|
+
const gates = await pendingGates(runtime);
|
|
391
|
+
assert.strictEqual(gates.length, 1);
|
|
392
|
+
assert.strictEqual(gates[0]?.runId, view.runId);
|
|
393
|
+
assert.strictEqual(gates[0]?.gate, "deploy");
|
|
394
|
+
const viewed = await viewRun(view.runId, runtime);
|
|
395
|
+
assert.deepStrictEqual(viewed.gates, ["deploy"]);
|
|
396
|
+
} finally {
|
|
397
|
+
await runtime.close();
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it("only the child at a gate is reported waiting", async () => {
|
|
402
|
+
const hold = deferred();
|
|
403
|
+
const runtime = await openSessionRuntime({
|
|
404
|
+
identity: await identityFor(),
|
|
405
|
+
models: new HostModelAdapter(
|
|
406
|
+
stubHostSource({
|
|
407
|
+
text: "deployed",
|
|
408
|
+
toolCallAt: [1],
|
|
409
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
410
|
+
hold: () => hold.promise,
|
|
411
|
+
holdAt: [2],
|
|
412
|
+
}),
|
|
413
|
+
),
|
|
414
|
+
});
|
|
415
|
+
try {
|
|
416
|
+
const result = await launch(
|
|
417
|
+
{
|
|
418
|
+
kind: "fanout",
|
|
419
|
+
lane: "deploy",
|
|
420
|
+
agents: [
|
|
421
|
+
{ agent: "deployer", task: "deploy it" },
|
|
422
|
+
{ agent: "scout", task: "look around" },
|
|
423
|
+
],
|
|
424
|
+
},
|
|
425
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
426
|
+
);
|
|
427
|
+
const pending = await waitForGateTasks(runtime, gateKey(result.view.runId, "deploy"), 1);
|
|
428
|
+
assert.deepStrictEqual(pending.childRunIds, [`${result.view.runId}:child:0`]);
|
|
429
|
+
|
|
430
|
+
const viewed = await viewRun(result.view.runId, runtime);
|
|
431
|
+
assert.deepStrictEqual(
|
|
432
|
+
viewed.children.map((child) => child.status),
|
|
433
|
+
["waiting", "running"],
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
hold.release();
|
|
437
|
+
const approved = await control(
|
|
438
|
+
{ action: "approve", run: result.view.runId, gate: "deploy" },
|
|
439
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
440
|
+
);
|
|
441
|
+
assert.strictEqual(approved.view!.status, "answered");
|
|
442
|
+
} finally {
|
|
443
|
+
hold.release();
|
|
444
|
+
await runtime.close();
|
|
445
|
+
}
|
|
446
|
+
}, 20_000);
|
|
447
|
+
|
|
448
|
+
it("every task parked on one gate is signaled", async () => {
|
|
449
|
+
const runtime = await openSessionRuntime({
|
|
450
|
+
identity: await identityFor(),
|
|
451
|
+
models: new HostModelAdapter(
|
|
452
|
+
stubHostSource({
|
|
453
|
+
text: "deployed",
|
|
454
|
+
toolCallAt: [1, 2],
|
|
455
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
456
|
+
}),
|
|
457
|
+
),
|
|
458
|
+
});
|
|
459
|
+
try {
|
|
460
|
+
const result = await launch(
|
|
461
|
+
{
|
|
462
|
+
kind: "fanout",
|
|
463
|
+
lane: "deploy",
|
|
464
|
+
agents: [
|
|
465
|
+
{ agent: "deployer", task: "first deploy" },
|
|
466
|
+
{ agent: "deployer", task: "second deploy" },
|
|
467
|
+
],
|
|
468
|
+
},
|
|
469
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
470
|
+
);
|
|
471
|
+
const pending = await waitForGateTasks(runtime, gateKey(result.view.runId, "deploy"), 2);
|
|
472
|
+
assert.strictEqual(pending.taskIds.length, 2);
|
|
473
|
+
assert.deepStrictEqual(pending.childRunIds, [
|
|
474
|
+
`${result.view.runId}:child:0`,
|
|
475
|
+
`${result.view.runId}:child:1`,
|
|
476
|
+
]);
|
|
477
|
+
|
|
478
|
+
const approved = await control(
|
|
479
|
+
{ action: "approve", run: result.view.runId, gate: "deploy" },
|
|
480
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
481
|
+
);
|
|
482
|
+
assert.strictEqual(approved.view!.status, "answered");
|
|
483
|
+
assert.deepStrictEqual(
|
|
484
|
+
approved.view!.children.map((child) => child.status),
|
|
485
|
+
["answered", "answered"],
|
|
486
|
+
);
|
|
487
|
+
} finally {
|
|
488
|
+
await runtime.close();
|
|
489
|
+
}
|
|
490
|
+
}, 20_000);
|
|
491
|
+
|
|
492
|
+
it("the gate deadline reads the injected clock", async () => {
|
|
493
|
+
const start = Date.now() + 60 * 60 * 1000;
|
|
494
|
+
let clock = start;
|
|
495
|
+
const runtime = await openRuntime(true, () => clock);
|
|
496
|
+
try {
|
|
497
|
+
const view = await parkedRun(runtime);
|
|
498
|
+
const key = gateKey(view.runId, "deploy");
|
|
499
|
+
const record = await runtime.snapshot(GateDoc, key);
|
|
500
|
+
assert.strictEqual(record?.requestedAt, start);
|
|
501
|
+
assert.strictEqual(record?.deadline, start + DAY_MS);
|
|
502
|
+
|
|
503
|
+
clock = start + DAY_MS;
|
|
504
|
+
await rejects(
|
|
505
|
+
control(
|
|
506
|
+
{ action: "approve", run: view.runId, gate: "deploy" },
|
|
507
|
+
{ runtime, wait: { policy: "settle", waitMs: 1_000 } },
|
|
508
|
+
),
|
|
509
|
+
(error: unknown) => error instanceof Error && error.message.includes("expired"),
|
|
510
|
+
);
|
|
511
|
+
const expired = await runtime.snapshot(GateDoc, key);
|
|
512
|
+
assert.strictEqual(expired?.status, "expired");
|
|
513
|
+
assert.strictEqual(expired?.reason, "deadline");
|
|
514
|
+
} finally {
|
|
515
|
+
await runtime.close();
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it("a decision after the gate deadline is refused", async () => {
|
|
520
|
+
const runtime = await openRuntime(true);
|
|
521
|
+
try {
|
|
522
|
+
const view = await parkedRun(runtime);
|
|
523
|
+
const key = gateKey(view.runId, "deploy");
|
|
524
|
+
await runtime.harness.commit(async (tx) => {
|
|
525
|
+
const doc = await tx.doc(GateDoc, key, { runId: view.runId, gate: "deploy" });
|
|
526
|
+
doc.deadline = Date.now() - 1;
|
|
527
|
+
}, BACKGROUND_CONTEXT);
|
|
528
|
+
|
|
529
|
+
await rejects(
|
|
530
|
+
control(
|
|
531
|
+
{ action: "approve", run: view.runId, gate: "deploy" },
|
|
532
|
+
{ runtime, wait: { policy: "settle", waitMs: 1_000 } },
|
|
533
|
+
),
|
|
534
|
+
(error: unknown) => error instanceof Error && error.message.includes("expired"),
|
|
535
|
+
);
|
|
536
|
+
const record = await runtime.snapshot(GateDoc, key);
|
|
537
|
+
assert.strictEqual(record?.status, "expired");
|
|
538
|
+
} finally {
|
|
539
|
+
await runtime.close();
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it("a fanout child's gate belongs to the parent run and can be approved", async () => {
|
|
544
|
+
const runtime = await openRuntime(true);
|
|
545
|
+
try {
|
|
546
|
+
const result = await launch(
|
|
547
|
+
{ kind: "fanout", lane: "deploy", agents: [{ agent: "deployer", task: "deploy it" }] },
|
|
548
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
549
|
+
);
|
|
550
|
+
assert.strictEqual(result.view.status, "waiting");
|
|
551
|
+
assert.deepStrictEqual(result.view.gates, ["deploy"]);
|
|
552
|
+
assert.strictEqual(result.view.children[0]?.status, "waiting");
|
|
553
|
+
|
|
554
|
+
const key = gateKey(result.view.runId, "deploy");
|
|
555
|
+
const record = await runtime.snapshot(GateDoc, key);
|
|
556
|
+
assert.strictEqual(record?.runId, result.view.runId);
|
|
557
|
+
|
|
558
|
+
const approved = await control(
|
|
559
|
+
{ action: "approve", run: result.view.runId, gate: "deploy" },
|
|
560
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
561
|
+
);
|
|
562
|
+
assert.strictEqual(approved.view!.status, "answered");
|
|
563
|
+
assert.strictEqual(approved.view!.children[0]?.answer, "deployed");
|
|
564
|
+
} finally {
|
|
565
|
+
await runtime.close();
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
it("each fanout child settles on its own gate", async () => {
|
|
570
|
+
const hold = deferred();
|
|
571
|
+
const runtime = await openSessionRuntime({
|
|
572
|
+
identity: await identityFor(),
|
|
573
|
+
models: new HostModelAdapter(
|
|
574
|
+
stubHostSource({
|
|
575
|
+
text: "deployed",
|
|
576
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
577
|
+
toolCallAt: [1, 2],
|
|
578
|
+
failAt: [3, 4],
|
|
579
|
+
hold: () => hold.promise,
|
|
580
|
+
holdAt: [2],
|
|
581
|
+
}),
|
|
582
|
+
),
|
|
583
|
+
});
|
|
584
|
+
try {
|
|
585
|
+
const result = await launch(
|
|
586
|
+
{
|
|
587
|
+
kind: "fanout",
|
|
588
|
+
lane: "deploy",
|
|
589
|
+
agents: [
|
|
590
|
+
{ agent: "deployer", task: "deploy it" },
|
|
591
|
+
{ agent: "releaser", task: "release it" },
|
|
592
|
+
],
|
|
593
|
+
},
|
|
594
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
595
|
+
);
|
|
596
|
+
const parent = result.view.runId;
|
|
597
|
+
const deployGate = gateKey(parent, "deploy");
|
|
598
|
+
const releaseGate = gateKey(parent, "release");
|
|
599
|
+
|
|
600
|
+
let earlier: string | undefined;
|
|
601
|
+
for (let i = 0; i < 500 && earlier === undefined; i += 1) {
|
|
602
|
+
if ((await runtime.snapshot(GateDoc, deployGate)) !== undefined) earlier = deployGate;
|
|
603
|
+
else if ((await runtime.snapshot(GateDoc, releaseGate)) !== undefined) {
|
|
604
|
+
earlier = releaseGate;
|
|
605
|
+
} else await new Promise((resolve) => setTimeout(resolve, 20));
|
|
606
|
+
}
|
|
607
|
+
assert.ok(earlier !== undefined, "no gate parked");
|
|
608
|
+
hold.release();
|
|
609
|
+
const later = earlier === deployGate ? releaseGate : deployGate;
|
|
610
|
+
await waitForGateTasks(runtime, later, 1);
|
|
611
|
+
|
|
612
|
+
const laterName = later === deployGate ? "deploy" : "release";
|
|
613
|
+
const earlierName = earlier === deployGate ? "deploy" : "release";
|
|
614
|
+
await control(
|
|
615
|
+
{ action: "deny", run: parent, gate: laterName },
|
|
616
|
+
{ runtime, wait: { policy: "settle", waitMs: 100 } },
|
|
617
|
+
);
|
|
618
|
+
const approved = await control(
|
|
619
|
+
{ action: "approve", run: parent, gate: earlierName },
|
|
620
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
621
|
+
);
|
|
622
|
+
|
|
623
|
+
const earlierChild = earlierName === "deploy" ? 0 : 1;
|
|
624
|
+
const laterChild = laterName === "deploy" ? 0 : 1;
|
|
625
|
+
const statuses = approved.view!.children.map((child) => child.status);
|
|
626
|
+
assert.strictEqual(statuses[earlierChild], "failed", JSON.stringify(statuses));
|
|
627
|
+
assert.strictEqual(statuses[laterChild], "denied", JSON.stringify(statuses));
|
|
628
|
+
} finally {
|
|
629
|
+
hold.release();
|
|
630
|
+
await runtime.close();
|
|
631
|
+
}
|
|
632
|
+
}, 20_000);
|
|
633
|
+
|
|
634
|
+
it("a denial without an answer ends the run denied", async () => {
|
|
635
|
+
const runtime = await openSessionRuntime({
|
|
636
|
+
identity: await identityFor(),
|
|
637
|
+
models: new HostModelAdapter(
|
|
638
|
+
stubHostSource({
|
|
639
|
+
failAt: [2],
|
|
640
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
641
|
+
}),
|
|
642
|
+
),
|
|
643
|
+
});
|
|
644
|
+
try {
|
|
645
|
+
const view = await parkedRun(runtime);
|
|
646
|
+
const denied = await control(
|
|
647
|
+
{ action: "deny", run: view.runId, gate: "deploy" },
|
|
648
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
649
|
+
);
|
|
650
|
+
assert.strictEqual(denied.view!.status, "denied");
|
|
651
|
+
assert.strictEqual(denied.view!.answer, null);
|
|
652
|
+
} finally {
|
|
653
|
+
await runtime.close();
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
it("a gate query ignores other runs' keys", async () => {
|
|
658
|
+
const runtime = await openSessionRuntime({
|
|
659
|
+
identity: await identityFor(),
|
|
660
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
661
|
+
});
|
|
662
|
+
try {
|
|
663
|
+
const seeds = [
|
|
664
|
+
{ key: gateKey("run:a", "approve-deploy"), runId: "run:a", gate: "approve-deploy" },
|
|
665
|
+
{ key: gateKey("run:b", "approve-release"), runId: "run:b", gate: "approve-release" },
|
|
666
|
+
];
|
|
667
|
+
await runtime.harness.commit(async (tx) => {
|
|
668
|
+
const index = await tx.doc(GateIndexDoc);
|
|
669
|
+
for (const seed of seeds) {
|
|
670
|
+
const doc = await tx.doc(GateDoc, seed.key, { runId: seed.runId, gate: seed.gate });
|
|
671
|
+
doc.status = "pending";
|
|
672
|
+
index.keys.push(seed.key);
|
|
673
|
+
}
|
|
674
|
+
}, BACKGROUND_CONTEXT);
|
|
675
|
+
|
|
676
|
+
const counted = countingRuntime(runtime);
|
|
677
|
+
const records = await gateRecords(counted.runtime, "run:a");
|
|
678
|
+
assert.deepStrictEqual(
|
|
679
|
+
records.map((record) => record.gate),
|
|
680
|
+
["approve-deploy"],
|
|
681
|
+
);
|
|
682
|
+
assert.strictEqual(counted.count("delegates.gate", seeds[1]!.key), 0);
|
|
683
|
+
} finally {
|
|
684
|
+
await runtime.close();
|
|
685
|
+
}
|
|
686
|
+
});
|