@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.
Files changed (116) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/LICENSE +21 -0
  3. package/OPERATIONS.md +57 -0
  4. package/README.md +448 -2
  5. package/ROADMAP.md +158 -0
  6. package/TESTING.md +1061 -0
  7. package/agents.ts +8 -0
  8. package/examples/README.md +114 -0
  9. package/examples/gated.ts +28 -0
  10. package/index.ts +9 -0
  11. package/package.json +93 -4
  12. package/skills/pi-delegates/SKILL.md +147 -0
  13. package/skills/pi-delegates/references/control.md +159 -0
  14. package/skills/pi-delegates/references/definitions.md +163 -0
  15. package/skills/pi-delegates/references/gates.md +175 -0
  16. package/skills/pi-delegates/references/launch.md +156 -0
  17. package/src/domain/definitions.test.ts +41 -0
  18. package/src/domain/definitions.ts +161 -0
  19. package/src/domain/ids.ts +13 -0
  20. package/src/domain/names.ts +35 -0
  21. package/src/domain/orchestration/admission.ts +78 -0
  22. package/src/domain/orchestration/control.test.ts +1298 -0
  23. package/src/domain/orchestration/control.ts +436 -0
  24. package/src/domain/orchestration/fanout.test.ts +419 -0
  25. package/src/domain/orchestration/fanout.ts +334 -0
  26. package/src/domain/orchestration/gates.test.ts +686 -0
  27. package/src/domain/orchestration/gates.ts +360 -0
  28. package/src/domain/orchestration/launch.test.ts +173 -0
  29. package/src/domain/orchestration/launch.ts +581 -0
  30. package/src/domain/orchestration/requests.test.ts +700 -0
  31. package/src/domain/orchestration/requests.ts +769 -0
  32. package/src/domain/orchestration/review.test.ts +214 -0
  33. package/src/domain/orchestration/review.ts +168 -0
  34. package/src/domain/orchestration/run-wait.test.ts +262 -0
  35. package/src/domain/orchestration/run-wait.ts +256 -0
  36. package/src/domain/orchestration/workflow-section.test.ts +62 -0
  37. package/src/domain/orchestration/workflow-section.ts +99 -0
  38. package/src/domain/orchestration/workflow.test.ts +1425 -0
  39. package/src/domain/orchestration/workflow.ts +705 -0
  40. package/src/domain/run-records.test.ts +27 -0
  41. package/src/domain/run-records.ts +527 -0
  42. package/src/domain/shapes.test.ts +165 -0
  43. package/src/domain/skills.test.ts +466 -0
  44. package/src/domain/skills.ts +97 -0
  45. package/src/domain/tool-schema.test.ts +46 -0
  46. package/src/domain/tool-schema.ts +20 -0
  47. package/src/domain/toolkit.test.ts +201 -0
  48. package/src/domain/toolkit.ts +379 -0
  49. package/src/domain/view.test.ts +739 -0
  50. package/src/domain/view.ts +459 -0
  51. package/src/example.test.ts +15 -0
  52. package/src/extension/commands.test.ts +481 -0
  53. package/src/extension/commands.ts +227 -0
  54. package/src/extension/entry.test.ts +1118 -0
  55. package/src/extension/entry.ts +342 -0
  56. package/src/extension/gate-alerts.test.ts +235 -0
  57. package/src/extension/gate-alerts.ts +143 -0
  58. package/src/extension/guard.test.ts +48 -0
  59. package/src/extension/guard.ts +31 -0
  60. package/src/extension/host-signals.test.ts +20 -0
  61. package/src/extension/host-signals.ts +13 -0
  62. package/src/extension/inspector-command.test.ts +1191 -0
  63. package/src/extension/inspector-command.ts +638 -0
  64. package/src/extension/prompt.test.ts +110 -0
  65. package/src/extension/prompt.ts +50 -0
  66. package/src/extension/request-alerts.test.ts +183 -0
  67. package/src/extension/request-alerts.ts +181 -0
  68. package/src/extension/revocation.test.ts +81 -0
  69. package/src/extension/revocation.ts +84 -0
  70. package/src/extension/shortcut.test.ts +64 -0
  71. package/src/extension/shortcut.ts +105 -0
  72. package/src/extension/tools.test.ts +993 -0
  73. package/src/extension/tools.ts +505 -0
  74. package/src/extension/version.test.ts +123 -0
  75. package/src/extension/version.ts +97 -0
  76. package/src/harness-policy.test.ts +74 -0
  77. package/src/package-manifest.test.ts +157 -0
  78. package/src/presentation/actions.test.ts +221 -0
  79. package/src/presentation/actions.ts +85 -0
  80. package/src/presentation/activity.test.ts +387 -0
  81. package/src/presentation/activity.ts +66 -0
  82. package/src/presentation/close.test.ts +361 -0
  83. package/src/presentation/inspector.test.ts +679 -0
  84. package/src/presentation/inspector.ts +440 -0
  85. package/src/presentation/overlay.test.ts +555 -0
  86. package/src/presentation/overlay.ts +229 -0
  87. package/src/presentation/repaint.test.ts +329 -0
  88. package/src/presentation/shortcut.ts +2 -0
  89. package/src/presentation/snapshot.test.ts +530 -0
  90. package/src/presentation/snapshot.ts +306 -0
  91. package/src/presentation/strip.test.ts +196 -0
  92. package/src/presentation/strip.ts +74 -0
  93. package/src/presentation/wording.test.ts +238 -0
  94. package/src/presentation/wording.ts +222 -0
  95. package/src/runtime/bridge.test.ts +131 -0
  96. package/src/runtime/bridge.ts +134 -0
  97. package/src/runtime/config.test.ts +44 -0
  98. package/src/runtime/config.ts +87 -0
  99. package/src/runtime/context.test.ts +19 -0
  100. package/src/runtime/context.ts +9 -0
  101. package/src/runtime/execution-env.test.ts +35 -0
  102. package/src/runtime/execution-env.ts +15 -0
  103. package/src/runtime/extensions.test.ts +86 -0
  104. package/src/runtime/extensions.ts +140 -0
  105. package/src/runtime/log.test.ts +128 -0
  106. package/src/runtime/log.ts +68 -0
  107. package/src/runtime/mcp.test.ts +1453 -0
  108. package/src/runtime/mcp.ts +938 -0
  109. package/src/runtime/models.test.ts +145 -0
  110. package/src/runtime/models.ts +259 -0
  111. package/src/runtime/observe.test.ts +50 -0
  112. package/src/runtime/observe.ts +150 -0
  113. package/src/runtime/session.test.ts +359 -0
  114. package/src/runtime/session.ts +425 -0
  115. package/src/runtime/timers.test.ts +31 -0
  116. package/src/runtime/timers.ts +10 -0
@@ -0,0 +1,359 @@
1
+ import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
2
+ import { MemoryStorage, type Harness, type Storage } from "@earendil-works/pi-durable";
3
+ import { existsSync } from "node:fs";
4
+ import { mkdtemp, readdir, writeFile } from "node:fs/promises";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import { Effect } from "effect";
8
+ import type { CodemodeSandboxBase } from "@earendil-works/pi-codemode/portable";
9
+ import { assert, it } from "@effect/vitest";
10
+ import { rejects } from "../../test-support/assertions.ts";
11
+ import { failingStorage } from "../../test-support/failing-storage.ts";
12
+ import { ProbeDoc } from "../../test-support/probe-doc.ts";
13
+ import { stubHostSource } from "../../test-support/stub-model-source.ts";
14
+ import { RunDoc } from "../domain/run-records.ts";
15
+ import { viewSession } from "../domain/view.ts";
16
+ import { createCatalogue } from "../domain/definitions.ts";
17
+ import { launch } from "../domain/orchestration/launch.ts";
18
+ import { HarnessClosed } from "./bridge.ts";
19
+ import { HostModelAdapter } from "./models.ts";
20
+ import type { LogFields, LogLevel, LogSink } from "./log.ts";
21
+ import {
22
+ openReadOnlyRuntime,
23
+ openSessionRuntime,
24
+ storePathFor,
25
+ type SessionIdentity,
26
+ } from "./session.ts";
27
+
28
+ const identityFor = async (sessionId: string): Promise<SessionIdentity> => {
29
+ const sessionDir = await mkdtemp(join(tmpdir(), "delegates-session-"));
30
+ return {
31
+ sessionId,
32
+ sessionDir,
33
+ cwd: sessionDir,
34
+ agentDir: sessionDir,
35
+ };
36
+ };
37
+
38
+ const models = () => new HostModelAdapter(stubHostSource());
39
+
40
+ const commitProbe = async (harness: Harness, counter: number): Promise<void> => {
41
+ await harness.commit(async (tx) => {
42
+ const doc = await tx.doc(ProbeDoc);
43
+ doc.counter = counter;
44
+ }, BACKGROUND_CONTEXT);
45
+ };
46
+
47
+ /** A sandbox double that records a real `start()` or `close()`; a start is a bug in this test. */
48
+ const countingSandbox = (): {
49
+ readonly sandbox: CodemodeSandboxBase;
50
+ readonly events: string[];
51
+ } => {
52
+ const events: string[] = [];
53
+ const sandbox = {
54
+ start: async () => {
55
+ events.push("start");
56
+ throw new Error("this test runs no script");
57
+ },
58
+ execute: async () => ({
59
+ ok: false as const,
60
+ error: { kind: "sandbox" as const, message: "this test runs no script" },
61
+ }),
62
+ close: async () => {
63
+ events.push("close");
64
+ },
65
+ } as unknown as CodemodeSandboxBase;
66
+ return { sandbox, events };
67
+ };
68
+
69
+ const recordingSink = () => {
70
+ const entries: { level: LogLevel; code: string; fields: LogFields }[] = [];
71
+ const sink: LogSink = {
72
+ write: (level, code, fields = {}) => {
73
+ entries.push({ level, code, fields });
74
+ },
75
+ };
76
+ return { sink, entries };
77
+ };
78
+
79
+ it("opening a session creates one store under the session directory", async () => {
80
+ const identity = await identityFor("s-1");
81
+ const runtime = await openSessionRuntime({ identity, models: models() });
82
+ try {
83
+ assert.strictEqual(runtime.storePath, join(identity.sessionDir, "delegates-s-1.sqlite"));
84
+ assert.strictEqual(runtime.storePath, storePathFor(identity));
85
+ assert.ok(existsSync(runtime.storePath));
86
+ assert.strictEqual(runtime.state.kind, "ready");
87
+ const stores = (await readdir(identity.sessionDir)).filter((name) => name.endsWith(".sqlite"));
88
+ assert.deepStrictEqual(stores, ["delegates-s-1.sqlite"]);
89
+ } finally {
90
+ await runtime.close();
91
+ }
92
+ });
93
+
94
+ it("reopening the same identity resumes committed state", async () => {
95
+ const identity = await identityFor("s-1");
96
+ const first = await openSessionRuntime({ identity, models: models() });
97
+ await commitProbe(first.harness, 1);
98
+ await first.close();
99
+
100
+ const second = await openSessionRuntime({ identity, models: models() });
101
+ try {
102
+ assert.deepStrictEqual(await second.snapshot(ProbeDoc), { counter: 1 });
103
+ assert.strictEqual(second.state.kind, "ready");
104
+ } finally {
105
+ await second.close();
106
+ }
107
+ });
108
+
109
+ it("a forked session gets a fresh store", async () => {
110
+ const identity = await identityFor("s-1");
111
+ const first = await openSessionRuntime({ identity, models: models() });
112
+ await commitProbe(first.harness, 1);
113
+ await first.close();
114
+
115
+ const forkedIdentity = { ...identity, sessionId: "s-2" };
116
+ const forked = await openSessionRuntime({ identity: forkedIdentity, models: models() });
117
+ try {
118
+ assert.notStrictEqual(forked.storePath, storePathFor(identity));
119
+ assert.strictEqual(await forked.snapshot(ProbeDoc), undefined);
120
+ } finally {
121
+ await forked.close();
122
+ }
123
+ });
124
+
125
+ it("a read-only open refuses writes but reads", async () => {
126
+ const identity = await identityFor("s-1");
127
+ const writer = await openSessionRuntime({ identity, models: models() });
128
+ await commitProbe(writer.harness, 5);
129
+ await writer.close();
130
+
131
+ const viewer = await openReadOnlyRuntime({ storePath: storePathFor(identity), models: models() });
132
+ try {
133
+ await rejects(
134
+ viewer.harness.commit(async (tx) => {
135
+ const doc = await tx.doc(ProbeDoc);
136
+ doc.counter = 6;
137
+ }, BACKGROUND_CONTEXT),
138
+ (error: unknown) => error instanceof Error && error.name === "ReadOnlySession",
139
+ );
140
+ assert.deepStrictEqual(await viewer.snapshot(ProbeDoc), { counter: 5 });
141
+ } finally {
142
+ await viewer.close();
143
+ }
144
+ });
145
+
146
+ it("a failing storage ends the runtime with its reason", async () => {
147
+ const identity = await identityFor("s-1");
148
+ const { storage, failNext } = failingStorage(new MemoryStorage());
149
+ const states: string[] = [];
150
+ const runtime = await openSessionRuntime({ identity, models: models(), storage });
151
+ const unsubscribe = runtime.onState((state) => states.push(state.kind));
152
+
153
+ failNext();
154
+ await rejects(commitProbe(runtime.harness, 1));
155
+ await runtime.harness.closed;
156
+
157
+ assert.deepStrictEqual(states, ["failing", "closed"]);
158
+ assert.strictEqual(runtime.state.kind, "closed");
159
+ assert.ok(runtime.state.kind === "closed" && runtime.state.reason?.includes("storage failed"));
160
+
161
+ const failure = await runtime.bridge.call(async () => 1).pipe(Effect.flip, Effect.runPromise);
162
+ assert.ok(failure instanceof HarnessClosed);
163
+ assert.ok(failure.reason.includes("storage failed"));
164
+ unsubscribe();
165
+ await runtime.close();
166
+ });
167
+
168
+ it("a failed open closes the sandbox", async () => {
169
+ const identity = await identityFor("s-1");
170
+ const events = countingSandbox();
171
+ const { storage, failAnyNext } = failingStorage(new MemoryStorage());
172
+ failAnyNext();
173
+ await rejects(
174
+ openSessionRuntime({ identity, models: models(), storage, sandbox: events.sandbox }),
175
+ /storage failed/,
176
+ );
177
+ assert.deepStrictEqual(events.events, ["close"]);
178
+ });
179
+
180
+ it("a storage failure logs one entry and closes the runtime", async () => {
181
+ const identity = await identityFor("s-1");
182
+ const { storage, failNext } = failingStorage(new MemoryStorage());
183
+ const { sink, entries } = recordingSink();
184
+ const runtime = await openSessionRuntime({ identity, models: models(), storage, log: sink });
185
+ try {
186
+ failNext();
187
+ await rejects(commitProbe(runtime.harness, 1));
188
+ await runtime.harness.closed;
189
+
190
+ assert.deepStrictEqual(
191
+ entries.map((entry) => entry.code),
192
+ ["report.storage_failed"],
193
+ );
194
+ assert.strictEqual(entries[0]?.level, "error");
195
+ assert.strictEqual(runtime.state.kind, "closed");
196
+ } finally {
197
+ await runtime.close();
198
+ }
199
+ });
200
+
201
+ it("config diagnostics are logged when delegates.json is invalid", async () => {
202
+ const identity = await identityFor("s-1");
203
+ const path = join(identity.agentDir, "delegates.json");
204
+ await writeFile(path, "{\n");
205
+ const { sink, entries } = recordingSink();
206
+ const runtime = await openSessionRuntime({ identity, models: models(), log: sink });
207
+ try {
208
+ assert.strictEqual(entries.length, 1);
209
+ assert.strictEqual(entries[0]?.level, "warn");
210
+ assert.strictEqual(entries[0]?.code, "config.invalid_json");
211
+ assert.ok(String(entries[0]?.fields.detail).includes(path), String(entries[0]?.fields.detail));
212
+ } finally {
213
+ await runtime.close();
214
+ }
215
+ });
216
+
217
+ it("a failed open closes the storage", async () => {
218
+ const identity = await identityFor("s-1");
219
+ let closed = false;
220
+ const base = new MemoryStorage();
221
+ const storage = new Proxy(base, {
222
+ get(target, property, receiver) {
223
+ if (property === "mintId") return () => Promise.reject(new Error("no id"));
224
+ if (property === "close") {
225
+ return () => {
226
+ closed = true;
227
+ return Promise.resolve();
228
+ };
229
+ }
230
+ const value = Reflect.get(target, property, receiver) as unknown;
231
+ return typeof value === "function" ? value.bind(target) : value;
232
+ },
233
+ }) as unknown as Storage;
234
+ await rejects(openSessionRuntime({ identity, models: models(), storage }));
235
+ assert.strictEqual(closed, true);
236
+ });
237
+
238
+ it("the injected clock is the runtime clock", async () => {
239
+ const identity = await identityFor("s-1");
240
+ let now = 1_000;
241
+ const runtime = await openSessionRuntime({ identity, models: models(), now: () => now });
242
+ try {
243
+ assert.strictEqual(runtime.now(), 1_000);
244
+ now = 2_000;
245
+ assert.strictEqual(runtime.now(), 2_000);
246
+ } finally {
247
+ await runtime.close();
248
+ }
249
+ });
250
+
251
+ it("the root conversation carries no tools and no extensions", async () => {
252
+ const identity = await identityFor("s-1");
253
+ const runtime = await openSessionRuntime({ identity, models: models() });
254
+ try {
255
+ const agent = await runtime.root.agent(BACKGROUND_CONTEXT);
256
+ assert.deepStrictEqual(agent.tools, []);
257
+ assert.deepStrictEqual(agent.extensions, []);
258
+ } finally {
259
+ await runtime.close();
260
+ }
261
+ });
262
+
263
+ it("the caller signal reaches the harness call", async () => {
264
+ const identity = await identityFor("s-1");
265
+ const runtime = await openSessionRuntime({ identity, models: models() });
266
+ try {
267
+ const controller = new AbortController();
268
+ let abortedDuring: boolean | undefined;
269
+ let entered: () => void = () => {};
270
+ const started = new Promise<void>((resolve) => {
271
+ entered = resolve;
272
+ });
273
+ const pending = runtime.run(async (context) => {
274
+ entered();
275
+ await new Promise<void>((resolve) => {
276
+ if (context.abortSignal?.aborted ?? false) resolve();
277
+ else context.abortSignal?.addEventListener("abort", () => resolve(), { once: true });
278
+ });
279
+ abortedDuring = context.abortSignal?.aborted ?? false;
280
+ return abortedDuring;
281
+ }, controller.signal);
282
+ await started;
283
+ controller.abort();
284
+ await rejects(pending);
285
+ await new Promise((resolve) => setTimeout(resolve, 10));
286
+ assert.strictEqual(abortedDuring, true);
287
+ } finally {
288
+ await runtime.close();
289
+ }
290
+ });
291
+
292
+ it("a skill scan problem reaches the log at open", async () => {
293
+ const identity = await identityFor("s-1");
294
+ const { sink, entries } = recordingSink();
295
+ const runtime = await openSessionRuntime({
296
+ identity,
297
+ models: models(),
298
+ log: sink,
299
+ skills: {
300
+ cwd: identity.cwd,
301
+ agentDir: identity.agentDir,
302
+ skillPaths: [join(identity.sessionDir, "absent-skills")],
303
+ },
304
+ });
305
+ try {
306
+ assert.ok(
307
+ entries.some(
308
+ (entry) =>
309
+ entry.code === "skill.warning" && JSON.stringify(entry.fields).includes("absent-skills"),
310
+ ),
311
+ JSON.stringify(entries),
312
+ );
313
+ } finally {
314
+ await runtime.close();
315
+ }
316
+ });
317
+
318
+ it("a viewer reads a store written by a runtime", async () => {
319
+ const identity = await identityFor("s-1");
320
+ const writer = await openSessionRuntime({ identity, models: models() });
321
+ await commitProbe(writer.harness, 1);
322
+ await writer.close();
323
+
324
+ const viewer = await openReadOnlyRuntime({ storePath: storePathFor(identity), models: models() });
325
+ try {
326
+ assert.deepStrictEqual(await viewer.snapshot(ProbeDoc), { counter: 1 });
327
+ } finally {
328
+ await viewer.close();
329
+ }
330
+ });
331
+
332
+ it("the read-only viewer satisfies the view port", async () => {
333
+ const identity = await identityFor("s-1");
334
+ const writer = await openSessionRuntime({ identity, models: models() });
335
+ const launched = await launch(
336
+ { kind: "single", agent: "scout", task: "look" },
337
+ {
338
+ runtime: writer,
339
+ catalogue: createCatalogue(),
340
+ host: { model: { provider: "stub", modelId: "stub-1" }, thinkingLevel: "low" },
341
+ waitMs: 10_000,
342
+ },
343
+ );
344
+ const runId = launched.view.runId;
345
+ await writer.close();
346
+
347
+ const viewer = await openReadOnlyRuntime({ storePath: storePathFor(identity), models: models() });
348
+ try {
349
+ const session = await viewSession(viewer);
350
+ assert.deepStrictEqual(
351
+ session.runs.map((run) => run.runId),
352
+ [runId],
353
+ );
354
+ const doc = await viewer.snapshot(RunDoc, runId);
355
+ assert.strictEqual(doc?.definition, "scout");
356
+ } finally {
357
+ await viewer.close();
358
+ }
359
+ });