@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,221 @@
|
|
|
1
|
+
import { MemoryStorage } from "@earendil-works/pi-durable";
|
|
2
|
+
import { assert, it } from "@effect/vitest";
|
|
3
|
+
import { makeTempDir } from "../../test-support/platform.ts";
|
|
4
|
+
import { stubHostSource } from "../../test-support/stub-model-source.ts";
|
|
5
|
+
import { createCatalogue, defineAgent } from "../domain/definitions.ts";
|
|
6
|
+
import { CODING_EXTENSION } from "../domain/names.ts";
|
|
7
|
+
import { control } from "../domain/orchestration/control.ts";
|
|
8
|
+
import { launch, type HostDefaults } from "../domain/orchestration/launch.ts";
|
|
9
|
+
import { RunDoc } from "../domain/run-records.ts";
|
|
10
|
+
import { HostModelAdapter } from "../runtime/models.ts";
|
|
11
|
+
import type { LogFields, LogLevel, LogSink } from "../runtime/log.ts";
|
|
12
|
+
import type { Timers } from "../runtime/clock.ts";
|
|
13
|
+
import { type SessionRuntime } from "../runtime/session.ts";
|
|
14
|
+
|
|
15
|
+
import { runInspectorAction } from "./actions.ts";
|
|
16
|
+
import { openTestSession, type TestSession } from "../../test-support/seams.ts";
|
|
17
|
+
|
|
18
|
+
const host: HostDefaults = {
|
|
19
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
20
|
+
thinkingLevel: "low",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const deployer = defineAgent({
|
|
24
|
+
name: "deployer",
|
|
25
|
+
role: "Deploy agent",
|
|
26
|
+
prompt: { role: "You deploy things." },
|
|
27
|
+
tools: ["bash"],
|
|
28
|
+
extensions: [CODING_EXTENSION],
|
|
29
|
+
approval: { gates: [{ name: "deploy", tools: ["bash"], match: "deploy" }] },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const catalogue = createCatalogue([deployer]);
|
|
33
|
+
|
|
34
|
+
interface Recorder {
|
|
35
|
+
readonly timers: Timers;
|
|
36
|
+
readonly delays: number[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Real timers with a delay log, so the harness still schedules. */
|
|
40
|
+
const recordingTimers = (): Recorder => {
|
|
41
|
+
const delays: number[] = [];
|
|
42
|
+
return {
|
|
43
|
+
delays,
|
|
44
|
+
timers: {
|
|
45
|
+
setTimeout: (callback, ms) => {
|
|
46
|
+
delays.push(ms);
|
|
47
|
+
return setTimeout(callback, ms);
|
|
48
|
+
},
|
|
49
|
+
clearTimeout: (handle) => clearTimeout(handle as ReturnType<typeof setTimeout>),
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const recordingLog = (): {
|
|
55
|
+
readonly sink: LogSink;
|
|
56
|
+
readonly entries: { level: LogLevel; code: string; fields: LogFields }[];
|
|
57
|
+
} => {
|
|
58
|
+
const entries: { level: LogLevel; code: string; fields: LogFields }[] = [];
|
|
59
|
+
return {
|
|
60
|
+
entries,
|
|
61
|
+
sink: {
|
|
62
|
+
write: (level, code, fields = {}) => {
|
|
63
|
+
entries.push({ level, code, fields });
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const openRuntime = async (clock: Recorder, log?: LogSink): Promise<TestSession> => {
|
|
70
|
+
const sessionDir = await makeTempDir("delegates-actions-");
|
|
71
|
+
return openTestSession({
|
|
72
|
+
identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
73
|
+
models: new HostModelAdapter(
|
|
74
|
+
stubHostSource({
|
|
75
|
+
text: "deployed",
|
|
76
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
77
|
+
}),
|
|
78
|
+
),
|
|
79
|
+
storage: new MemoryStorage(),
|
|
80
|
+
timers: clock.timers,
|
|
81
|
+
...(log === undefined ? {} : { log }),
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const parkedRun = async (runtime: SessionRuntime): Promise<string> => {
|
|
86
|
+
const result = await launch(
|
|
87
|
+
{ kind: "single", agent: "deployer", task: "deploy it" },
|
|
88
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
89
|
+
);
|
|
90
|
+
return result.view.runId;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
it("the commit policy arms no settlement wait and returns no view", async () => {
|
|
94
|
+
const clock = recordingTimers();
|
|
95
|
+
const runtime = await openRuntime(clock);
|
|
96
|
+
try {
|
|
97
|
+
const runId = await parkedRun(runtime);
|
|
98
|
+
const result = await control(
|
|
99
|
+
{ action: "stop", run: runId },
|
|
100
|
+
{ runtime, wait: { policy: "commit" } },
|
|
101
|
+
);
|
|
102
|
+
assert.ok(!("view" in result));
|
|
103
|
+
assert.ok(!clock.delays.includes(4_321), clock.delays.join(","));
|
|
104
|
+
} finally {
|
|
105
|
+
await runtime.close();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("the settle policy still arms one wait and returns a view", async () => {
|
|
110
|
+
const clock = recordingTimers();
|
|
111
|
+
const runtime = await openRuntime(clock);
|
|
112
|
+
try {
|
|
113
|
+
const runId = await parkedRun(runtime);
|
|
114
|
+
const result = await control(
|
|
115
|
+
{ action: "stop", run: runId },
|
|
116
|
+
{ runtime, wait: { policy: "settle", waitMs: 4_321 } },
|
|
117
|
+
);
|
|
118
|
+
assert.ok(clock.delays.includes(4_321), clock.delays.join(","));
|
|
119
|
+
assert.ok(result.view !== undefined);
|
|
120
|
+
} finally {
|
|
121
|
+
await runtime.close();
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("a verb that does not apply renders the pinned line and writes nothing", async () => {
|
|
126
|
+
const clock = recordingTimers();
|
|
127
|
+
const runtime = await openRuntime(clock);
|
|
128
|
+
let refreshed = 0;
|
|
129
|
+
try {
|
|
130
|
+
const result = await runInspectorAction({
|
|
131
|
+
runtime,
|
|
132
|
+
target: { rowId: "child:r1", runId: "r1" },
|
|
133
|
+
verb: "approve",
|
|
134
|
+
refresh: async () => {
|
|
135
|
+
refreshed += 1;
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
assert.deepStrictEqual(result, {
|
|
139
|
+
ok: false,
|
|
140
|
+
message: "approve does not apply to run r1",
|
|
141
|
+
});
|
|
142
|
+
assert.strictEqual(refreshed, 0);
|
|
143
|
+
} finally {
|
|
144
|
+
await runtime.close();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("an already-decided gate renders the pinned refusal naming gate and run", async () => {
|
|
149
|
+
const clock = recordingTimers();
|
|
150
|
+
const log = recordingLog();
|
|
151
|
+
const runtime = await openRuntime(clock, log.sink);
|
|
152
|
+
let refreshed = 0;
|
|
153
|
+
try {
|
|
154
|
+
const runId = await parkedRun(runtime);
|
|
155
|
+
await control(
|
|
156
|
+
{ action: "approve", run: runId, gate: "deploy" },
|
|
157
|
+
{ runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
158
|
+
);
|
|
159
|
+
const result = await runInspectorAction({
|
|
160
|
+
runtime,
|
|
161
|
+
target: { rowId: `gate:${runId}:deploy`, runId, gate: "deploy" },
|
|
162
|
+
verb: "approve",
|
|
163
|
+
refresh: async () => {
|
|
164
|
+
refreshed += 1;
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
assert.strictEqual(result.ok, false);
|
|
168
|
+
assert.ok(result.message.startsWith(`gate deploy on run ${runId}: `), result.message);
|
|
169
|
+
assert.strictEqual(refreshed, 0);
|
|
170
|
+
const failures = log.entries.filter((entry) => entry.code === "inspector.action_failed");
|
|
171
|
+
assert.strictEqual(failures.length, 1);
|
|
172
|
+
assert.strictEqual(failures[0]?.fields.target, `gate deploy on run ${runId}`);
|
|
173
|
+
} finally {
|
|
174
|
+
await runtime.close();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("a committed action asks for an immediate refresh", async () => {
|
|
179
|
+
const clock = recordingTimers();
|
|
180
|
+
const runtime = await openRuntime(clock);
|
|
181
|
+
const events: string[] = [];
|
|
182
|
+
try {
|
|
183
|
+
const runId = await parkedRun(runtime);
|
|
184
|
+
const result = await runInspectorAction({
|
|
185
|
+
runtime,
|
|
186
|
+
target: { rowId: `child:${runId}`, runId },
|
|
187
|
+
verb: "stop",
|
|
188
|
+
refresh: async () => {
|
|
189
|
+
events.push("refresh");
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
events.push("resolved");
|
|
193
|
+
assert.deepStrictEqual(result, { ok: true, message: `stopped run ${runId}` });
|
|
194
|
+
assert.deepStrictEqual(events, ["refresh", "resolved"]);
|
|
195
|
+
const doc = await runtime.snapshot(RunDoc, runId);
|
|
196
|
+
assert.ok(doc !== undefined);
|
|
197
|
+
} finally {
|
|
198
|
+
await runtime.close();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("a refused write names the target and writes no notification", async () => {
|
|
203
|
+
const clock = recordingTimers();
|
|
204
|
+
const runtime = await openRuntime(clock);
|
|
205
|
+
const notices: string[] = [];
|
|
206
|
+
try {
|
|
207
|
+
const result = await runInspectorAction({
|
|
208
|
+
runtime,
|
|
209
|
+
target: { rowId: "child:run-absent", runId: "run-absent" },
|
|
210
|
+
verb: "stop",
|
|
211
|
+
refresh: async () => {
|
|
212
|
+
notices.push("refresh");
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
assert.strictEqual(result.ok, false);
|
|
216
|
+
assert.ok(result.message.startsWith("run run-absent: "), result.message);
|
|
217
|
+
assert.deepStrictEqual(notices, []);
|
|
218
|
+
} finally {
|
|
219
|
+
await runtime.close();
|
|
220
|
+
}
|
|
221
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { control, type ControlRequest } from "../domain/orchestration/control.ts";
|
|
2
|
+
import { errorFields, reasonOf } from "../runtime/log.ts";
|
|
3
|
+
import type { SessionRuntime } from "../runtime/session.ts";
|
|
4
|
+
import type { OverlayVerb } from "./overlay.ts";
|
|
5
|
+
|
|
6
|
+
/** The pinned status lines; every one names its target. */
|
|
7
|
+
export const actionLine = {
|
|
8
|
+
stopped: (runId: string) => `stopped run ${runId}`,
|
|
9
|
+
approved: (gate: string, runId: string) => `approved gate ${gate} on run ${runId}`,
|
|
10
|
+
denied: (gate: string, runId: string) => `denied gate ${gate} on run ${runId}`,
|
|
11
|
+
notApplicable: (verb: OverlayVerb, target: string) => `${verb} does not apply to ${target}`,
|
|
12
|
+
refused: (target: string, message: string) => `${target}: ${message}`,
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
export interface ActionTarget {
|
|
16
|
+
readonly rowId: string;
|
|
17
|
+
readonly runId: string;
|
|
18
|
+
readonly gate?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** `run r1`, or `gate deploy on run r1` — the target every line names. */
|
|
22
|
+
export const targetLabel = (target: ActionTarget): string =>
|
|
23
|
+
target.gate === undefined ? `run ${target.runId}` : `gate ${target.gate} on run ${target.runId}`;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The one map from an activation to one existing write: `stop` on a run row,
|
|
27
|
+
* `approve` or `deny` on a gate row.
|
|
28
|
+
*/
|
|
29
|
+
export const requestFor = (target: ActionTarget, verb: OverlayVerb): ControlRequest | undefined => {
|
|
30
|
+
if (verb === "stop") {
|
|
31
|
+
return target.gate === undefined ? { action: "stop", run: target.runId } : undefined;
|
|
32
|
+
}
|
|
33
|
+
return target.gate === undefined
|
|
34
|
+
? undefined
|
|
35
|
+
: { action: verb, run: target.runId, gate: target.gate };
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export interface ActionResult {
|
|
39
|
+
readonly ok: boolean;
|
|
40
|
+
/** The status line: names the target on success, the target and the refusal on failure. */
|
|
41
|
+
readonly message: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const successLine = (target: ActionTarget, verb: OverlayVerb): string => {
|
|
45
|
+
if (target.gate === undefined) return actionLine.stopped(target.runId);
|
|
46
|
+
return verb === "deny"
|
|
47
|
+
? actionLine.denied(target.gate, target.runId)
|
|
48
|
+
: actionLine.approved(target.gate, target.runId);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* One activation, one existing write. The control call commits and returns;
|
|
53
|
+
* the owner's refresh runs immediately after, so the row settles on the next
|
|
54
|
+
* push without the keypress waiting for settlement. A refusal is one line and
|
|
55
|
+
* never a chat notification.
|
|
56
|
+
*/
|
|
57
|
+
export const runInspectorAction = async (input: {
|
|
58
|
+
readonly runtime: SessionRuntime;
|
|
59
|
+
readonly target: ActionTarget;
|
|
60
|
+
readonly verb: OverlayVerb;
|
|
61
|
+
/** The owner's refresh effect, run by the host after the write: not a settlement wait. */
|
|
62
|
+
readonly refresh: () => Promise<void>;
|
|
63
|
+
}): Promise<ActionResult> => {
|
|
64
|
+
const request = requestFor(input.target, input.verb);
|
|
65
|
+
if (request === undefined) {
|
|
66
|
+
return {
|
|
67
|
+
ok: false,
|
|
68
|
+
message: actionLine.notApplicable(input.verb, targetLabel(input.target)),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
await control(request, { runtime: input.runtime, wait: { policy: "commit" } });
|
|
73
|
+
} catch (error) {
|
|
74
|
+
input.runtime.log.write("warn", "inspector.action_failed", {
|
|
75
|
+
target: targetLabel(input.target),
|
|
76
|
+
...errorFields(error),
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
message: actionLine.refused(targetLabel(input.target), reasonOf(error)),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
await input.refresh();
|
|
84
|
+
return { ok: true, message: successLine(input.target, input.verb) };
|
|
85
|
+
};
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { MemoryStorage, type TaskGraph } from "@earendil-works/pi-durable";
|
|
2
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
3
|
+
import { assert, it } from "@effect/vitest";
|
|
4
|
+
import { makeTempDir } from "../../test-support/platform.ts";
|
|
5
|
+
import { stubHostSource } from "../../test-support/stub-model-source.ts";
|
|
6
|
+
import { waitFor } from "../../test-support/wait.ts";
|
|
7
|
+
import { createCatalogue, defineAgent } from "../domain/definitions.ts";
|
|
8
|
+
import { CODING_EXTENSION } from "../domain/names.ts";
|
|
9
|
+
import { control } from "../domain/orchestration/control.ts";
|
|
10
|
+
import { GateDoc } from "../domain/orchestration/gates.ts";
|
|
11
|
+
import { launch, type HostDefaults } from "../domain/orchestration/launch.ts";
|
|
12
|
+
import { RunDoc, RunIndexDoc } from "../domain/run-records.ts";
|
|
13
|
+
import { viewRun } from "../domain/view.ts";
|
|
14
|
+
import { HostModelAdapter } from "../runtime/models.ts";
|
|
15
|
+
import { observeFamilyDoc, observeSessionDoc, observeTaskGraph } from "../runtime/observe.ts";
|
|
16
|
+
|
|
17
|
+
import { watchActivity, type RunActivity } from "./activity.ts";
|
|
18
|
+
import { openTestSession, type TestSession } from "../../test-support/seams.ts";
|
|
19
|
+
|
|
20
|
+
const host: HostDefaults = {
|
|
21
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
22
|
+
thinkingLevel: "low",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const deployer = defineAgent({
|
|
26
|
+
name: "deployer",
|
|
27
|
+
role: "Deploy agent",
|
|
28
|
+
prompt: { role: "You deploy things." },
|
|
29
|
+
tools: ["bash"],
|
|
30
|
+
extensions: [CODING_EXTENSION],
|
|
31
|
+
approval: { gates: [{ name: "deploy", tools: ["bash"], match: "deploy" }] },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const catalogue = createCatalogue([deployer]);
|
|
35
|
+
|
|
36
|
+
const openRuntime = async (): Promise<TestSession> => {
|
|
37
|
+
const sessionDir = await makeTempDir("delegates-activity-");
|
|
38
|
+
return openTestSession({
|
|
39
|
+
identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
|
|
40
|
+
models: new HostModelAdapter(
|
|
41
|
+
stubHostSource({
|
|
42
|
+
text: "deployed",
|
|
43
|
+
toolCall: { name: "bash", arguments: { command: "echo deploy" } },
|
|
44
|
+
}),
|
|
45
|
+
),
|
|
46
|
+
storage: new MemoryStorage(),
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
interface ParkedRun {
|
|
51
|
+
readonly runtime: TestSession;
|
|
52
|
+
readonly runId: string;
|
|
53
|
+
readonly childId: number;
|
|
54
|
+
readonly taskId: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The shared Arrange: a real runtime, a gated run, and the ids a watch needs. */
|
|
58
|
+
const parkedRun = async (): Promise<ParkedRun> => {
|
|
59
|
+
const runtime = await openRuntime();
|
|
60
|
+
const result = await launch(
|
|
61
|
+
{ kind: "single", agent: "deployer", task: "deploy it" },
|
|
62
|
+
{ runtime, catalogue, host, waitMs: 10_000 },
|
|
63
|
+
);
|
|
64
|
+
const doc = await runtime.snapshot(RunDoc, result.view.runId);
|
|
65
|
+
assert.ok(doc?.childConversationId !== null && doc?.childConversationId !== undefined);
|
|
66
|
+
assert.ok(doc?.taskId !== null && doc?.taskId !== undefined);
|
|
67
|
+
return {
|
|
68
|
+
runtime,
|
|
69
|
+
runId: result.view.runId,
|
|
70
|
+
childId: doc.childConversationId,
|
|
71
|
+
taskId: doc.taskId,
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const approve = async (run: ParkedRun): Promise<void> => {
|
|
76
|
+
await control(
|
|
77
|
+
{ action: "approve", run: run.runId, gate: "deploy" },
|
|
78
|
+
{ runtime: run.runtime, wait: { policy: "settle", waitMs: 10_000 } },
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
it("a parked tool call reports its name and slot status as activity", async () => {
|
|
83
|
+
const run = await parkedRun();
|
|
84
|
+
try {
|
|
85
|
+
const activities: RunActivity[] = [];
|
|
86
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
87
|
+
const first = activities[0];
|
|
88
|
+
assert.ok(first !== undefined, "the watch must report the value at attach");
|
|
89
|
+
assert.strictEqual(first.tool, "bash");
|
|
90
|
+
assert.strictEqual(first.slot, "sleeping");
|
|
91
|
+
assert.strictEqual(first.turns, 1);
|
|
92
|
+
assert.strictEqual(typeof first.at, "number");
|
|
93
|
+
} finally {
|
|
94
|
+
await run.runtime.close();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("activity arrives when the call resumes", async () => {
|
|
99
|
+
const run = await parkedRun();
|
|
100
|
+
const activities: RunActivity[] = [];
|
|
101
|
+
try {
|
|
102
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
103
|
+
await approve(run);
|
|
104
|
+
await waitFor(
|
|
105
|
+
() => activities.some((activity) => activity.slot === "done"),
|
|
106
|
+
"the resumed call reported done",
|
|
107
|
+
);
|
|
108
|
+
const slots = activities
|
|
109
|
+
.filter((activity) => activity.tool === "bash")
|
|
110
|
+
.map((activity) => activity.slot)
|
|
111
|
+
// The live document advances more than once per transition (output, then
|
|
112
|
+
// status); consecutive repeats are one visible change.
|
|
113
|
+
.filter((slot, index, all) => index === 0 || slot !== all[index - 1]);
|
|
114
|
+
assert.deepStrictEqual(slots, ["sleeping", "running", "done"]);
|
|
115
|
+
assert.strictEqual(slots.at(-1), "done");
|
|
116
|
+
} finally {
|
|
117
|
+
await run.runtime.close();
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("activity never carries transcript text", async () => {
|
|
122
|
+
const run = await parkedRun();
|
|
123
|
+
const activities: RunActivity[] = [];
|
|
124
|
+
try {
|
|
125
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
126
|
+
await approve(run);
|
|
127
|
+
for (const activity of activities) {
|
|
128
|
+
for (const key of ["output", "details", "message", "answer"]) {
|
|
129
|
+
assert.ok(!(key in activity), `activity owns ${key}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const serialized = JSON.stringify(activities);
|
|
133
|
+
assert.ok(!serialized.includes("echo deploy"), serialized);
|
|
134
|
+
assert.ok(!serialized.includes("deployed"), serialized);
|
|
135
|
+
} finally {
|
|
136
|
+
await run.runtime.close();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("detaching stops the watch", async () => {
|
|
141
|
+
const run = await parkedRun();
|
|
142
|
+
const activities: RunActivity[] = [];
|
|
143
|
+
const ends: string[] = [];
|
|
144
|
+
try {
|
|
145
|
+
const detach = await watchActivity(
|
|
146
|
+
run.runtime,
|
|
147
|
+
run.childId,
|
|
148
|
+
(activity) => activities.push(activity),
|
|
149
|
+
(kind) => ends.push(kind),
|
|
150
|
+
);
|
|
151
|
+
detach();
|
|
152
|
+
const before = activities.length;
|
|
153
|
+
assert.doesNotThrow(() => detach());
|
|
154
|
+
await approve(run);
|
|
155
|
+
await waitFor(
|
|
156
|
+
async () => (await viewRun(run.runId, run.runtime)).status === "answered",
|
|
157
|
+
"the run settled after approval",
|
|
158
|
+
);
|
|
159
|
+
assert.strictEqual(activities.length, before);
|
|
160
|
+
assert.deepStrictEqual(ends, []);
|
|
161
|
+
} finally {
|
|
162
|
+
await run.runtime.close();
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("the watch ends once when the runtime closes", async () => {
|
|
167
|
+
const run = await parkedRun();
|
|
168
|
+
const activities: RunActivity[] = [];
|
|
169
|
+
const ends: { readonly kind: string; readonly reason?: string }[] = [];
|
|
170
|
+
let atEnd = 0;
|
|
171
|
+
try {
|
|
172
|
+
await watchActivity(
|
|
173
|
+
run.runtime,
|
|
174
|
+
run.childId,
|
|
175
|
+
(activity) => activities.push(activity),
|
|
176
|
+
(kind, reason) => {
|
|
177
|
+
atEnd = activities.length;
|
|
178
|
+
ends.push({ kind, ...(reason === undefined ? {} : { reason }) });
|
|
179
|
+
},
|
|
180
|
+
);
|
|
181
|
+
await run.runtime.close();
|
|
182
|
+
await waitFor(() => ends.length === 1, "the watch ended once");
|
|
183
|
+
assert.strictEqual(ends[0]?.kind, "failed");
|
|
184
|
+
assert.strictEqual(activities.length, atEnd);
|
|
185
|
+
} finally {
|
|
186
|
+
await run.runtime.close().catch(() => undefined);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("a conversation with no live document can still be watched", async () => {
|
|
191
|
+
const run = await parkedRun();
|
|
192
|
+
try {
|
|
193
|
+
const activities: RunActivity[] = [];
|
|
194
|
+
await watchActivity(run.runtime, run.runtime.root.id as unknown as number, (activity) =>
|
|
195
|
+
activities.push(activity),
|
|
196
|
+
);
|
|
197
|
+
assert.deepStrictEqual(
|
|
198
|
+
{ tool: activities[0]?.tool, slot: activities[0]?.slot, turns: activities[0]?.turns },
|
|
199
|
+
{ tool: null, slot: null, turns: 0 },
|
|
200
|
+
);
|
|
201
|
+
} finally {
|
|
202
|
+
await run.runtime.close();
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
interface Recorded<T> {
|
|
207
|
+
readonly values: Readonly<T>[];
|
|
208
|
+
readonly ends: { readonly kind: "ended" | "failed"; readonly reason?: string }[];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const recorder = <T>(): Recorded<T> & {
|
|
212
|
+
sink: {
|
|
213
|
+
advance: (value: Readonly<T>) => void;
|
|
214
|
+
end: (kind: "ended" | "failed", reason?: string) => void;
|
|
215
|
+
};
|
|
216
|
+
} => {
|
|
217
|
+
const values: Readonly<T>[] = [];
|
|
218
|
+
const ends: { kind: "ended" | "failed"; reason?: string }[] = [];
|
|
219
|
+
return {
|
|
220
|
+
values,
|
|
221
|
+
ends,
|
|
222
|
+
sink: {
|
|
223
|
+
advance: (value) => values.push(value),
|
|
224
|
+
end: (kind, reason) => ends.push({ kind, ...(reason === undefined ? {} : { reason }) }),
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
it("a session-document observation delivers the value at attach", async () => {
|
|
230
|
+
const run = await parkedRun();
|
|
231
|
+
try {
|
|
232
|
+
const recorded = recorder<{ runIds: string[] }>();
|
|
233
|
+
const detach = await observeSessionDoc(run.runtime, RunIndexDoc, recorded.sink);
|
|
234
|
+
assert.ok(recorded.values[0]?.runIds.includes(run.runId));
|
|
235
|
+
assert.strictEqual(typeof detach, "function");
|
|
236
|
+
} finally {
|
|
237
|
+
await run.runtime.close();
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("a family observation addresses one run by key", async () => {
|
|
242
|
+
const run = await parkedRun();
|
|
243
|
+
try {
|
|
244
|
+
const recorded = recorder<{ definition: string; childConversationId: number | null }>();
|
|
245
|
+
await observeFamilyDoc(run.runtime, RunDoc, run.runId, recorded.sink);
|
|
246
|
+
assert.strictEqual(recorded.values[0]?.definition, "deployer");
|
|
247
|
+
assert.strictEqual(recorded.values[0]?.childConversationId, run.childId);
|
|
248
|
+
|
|
249
|
+
const absent = recorder<{ definition: string }>();
|
|
250
|
+
const refused = await observeFamilyDoc(run.runtime, RunDoc, "run-absent#1", absent.sink);
|
|
251
|
+
assert.strictEqual(refused, undefined);
|
|
252
|
+
} finally {
|
|
253
|
+
await run.runtime.close();
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("an index observation pushes once per committed change", async () => {
|
|
258
|
+
const run = await parkedRun();
|
|
259
|
+
try {
|
|
260
|
+
const recorded = recorder<{ runIds: string[] }>();
|
|
261
|
+
await observeSessionDoc(run.runtime, RunIndexDoc, recorded.sink);
|
|
262
|
+
const before = recorded.values.length;
|
|
263
|
+
|
|
264
|
+
const second = await launch(
|
|
265
|
+
{ kind: "single", agent: "deployer", task: "deploy again" },
|
|
266
|
+
{ runtime: run.runtime, catalogue, host, waitMs: 100 },
|
|
267
|
+
);
|
|
268
|
+
await waitFor(() => recorded.values.length > before, "the index observation saw the new run");
|
|
269
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
270
|
+
assert.strictEqual(recorded.values.length, before + 1);
|
|
271
|
+
assert.ok(recorded.values.at(-1)?.runIds.includes(second.view.runId));
|
|
272
|
+
|
|
273
|
+
// An unrelated family write at another address must not push the index.
|
|
274
|
+
const afterIndex = recorded.values.length;
|
|
275
|
+
await run.runtime.harness.commit(async (tx) => {
|
|
276
|
+
const gate = await tx.doc(GateDoc, "run:unrelated:deploy", {
|
|
277
|
+
runId: "run:unrelated",
|
|
278
|
+
gate: "deploy",
|
|
279
|
+
});
|
|
280
|
+
gate.status = "pending";
|
|
281
|
+
gate.requestedAt = 1_000;
|
|
282
|
+
}, BACKGROUND_CONTEXT);
|
|
283
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
284
|
+
assert.strictEqual(recorded.values.length, afterIndex);
|
|
285
|
+
} finally {
|
|
286
|
+
await run.runtime.close();
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("an absent address is refused, not invented", async () => {
|
|
291
|
+
const run = await parkedRun();
|
|
292
|
+
try {
|
|
293
|
+
const recorded = recorder<{ gate: string }>();
|
|
294
|
+
const detach = await observeFamilyDoc(run.runtime, GateDoc, "absent:none", recorded.sink);
|
|
295
|
+
assert.strictEqual(detach, undefined);
|
|
296
|
+
assert.deepStrictEqual(recorded.values, []);
|
|
297
|
+
} finally {
|
|
298
|
+
await run.runtime.close();
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const observeGraph = async (
|
|
303
|
+
run: ParkedRun,
|
|
304
|
+
sink: (graph: Readonly<TaskGraph>) => void,
|
|
305
|
+
): Promise<() => void> => observeTaskGraph(run.runtime, { advance: sink, end: () => {} });
|
|
306
|
+
|
|
307
|
+
it("a task-graph observation reports the parked task at attach", async () => {
|
|
308
|
+
const run = await parkedRun();
|
|
309
|
+
try {
|
|
310
|
+
let attached: Readonly<TaskGraph> | undefined;
|
|
311
|
+
await observeGraph(run, (graph) => {
|
|
312
|
+
attached = graph;
|
|
313
|
+
});
|
|
314
|
+
assert.ok(attached !== undefined);
|
|
315
|
+
const node = attached.tasks[String(run.taskId)];
|
|
316
|
+
assert.ok(node !== undefined, "the parked task is in the graph");
|
|
317
|
+
assert.strictEqual(node.state.status, "sleeping");
|
|
318
|
+
// The run task lives in the root conversation and owns the child one.
|
|
319
|
+
assert.strictEqual(node.conversationId, run.runtime.root.id);
|
|
320
|
+
assert.ok(node.conversations.includes(run.childId as never));
|
|
321
|
+
} finally {
|
|
322
|
+
await run.runtime.close();
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("a task-graph observation pushes on the park and on the resume", async () => {
|
|
327
|
+
const run = await parkedRun();
|
|
328
|
+
const graphs: Readonly<TaskGraph>[] = [];
|
|
329
|
+
try {
|
|
330
|
+
await observeGraph(run, (graph) => graphs.push(graph));
|
|
331
|
+
const first = graphs.length;
|
|
332
|
+
await approve(run);
|
|
333
|
+
await waitFor(
|
|
334
|
+
() => graphs.length >= first + 2 && graphs.at(-1)?.tasks[String(run.taskId)] === undefined,
|
|
335
|
+
"the graph dropped the settled task",
|
|
336
|
+
);
|
|
337
|
+
assert.ok(graphs.length >= 3);
|
|
338
|
+
for (let index = 1; index < graphs.length; index += 1) {
|
|
339
|
+
assert.notDeepEqual(graphs[index], graphs[index - 1]);
|
|
340
|
+
}
|
|
341
|
+
assert.strictEqual(graphs.at(-1)?.tasks[String(run.taskId)], undefined);
|
|
342
|
+
} finally {
|
|
343
|
+
await run.runtime.close();
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("a task-graph observation is never absent and ends once when the session closes", async () => {
|
|
348
|
+
const run = await parkedRun();
|
|
349
|
+
const ends: string[] = [];
|
|
350
|
+
let advances = 0;
|
|
351
|
+
try {
|
|
352
|
+
const detach = await observeTaskGraph(run.runtime, {
|
|
353
|
+
advance: () => {
|
|
354
|
+
advances += 1;
|
|
355
|
+
},
|
|
356
|
+
end: (kind) => ends.push(kind),
|
|
357
|
+
});
|
|
358
|
+
assert.strictEqual(typeof detach, "function");
|
|
359
|
+
const before = advances;
|
|
360
|
+
await run.runtime.close();
|
|
361
|
+
await waitFor(() => ends.length === 1, "the graph observation ended once");
|
|
362
|
+
assert.strictEqual(ends[0], "failed");
|
|
363
|
+
assert.strictEqual(advances, before);
|
|
364
|
+
} finally {
|
|
365
|
+
await run.runtime.close().catch(() => undefined);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("an observation carries no transcript text", async () => {
|
|
370
|
+
const run = await parkedRun();
|
|
371
|
+
const activities: RunActivity[] = [];
|
|
372
|
+
const values: unknown[] = [];
|
|
373
|
+
try {
|
|
374
|
+
await watchActivity(run.runtime, run.childId, (activity) => activities.push(activity));
|
|
375
|
+
await observeFamilyDoc(run.runtime, RunDoc, run.runId, {
|
|
376
|
+
advance: (value) => values.push(value),
|
|
377
|
+
end: () => {},
|
|
378
|
+
});
|
|
379
|
+
await approve(run);
|
|
380
|
+
const serialized = JSON.stringify({ activities, values });
|
|
381
|
+
assert.ok(!serialized.includes("echo deploy"), serialized);
|
|
382
|
+
assert.ok(!serialized.includes("deployed"), serialized);
|
|
383
|
+
} finally {
|
|
384
|
+
await run.runtime.close();
|
|
385
|
+
}
|
|
386
|
+
});
|