@ftrdotdev/pi-delegates 0.1.0 → 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 +51 -0
- package/OPERATIONS.md +79 -6
- package/README.md +25 -17
- package/ROADMAP.md +21 -0
- package/TESTING.md +153 -318
- package/package.json +4 -3
- package/skills/pi-delegates/SKILL.md +29 -0
- package/skills/pi-delegates/references/launch.md +28 -0
- package/src/docs-contract.test.ts +106 -0
- package/src/domain/definitions.ts +3 -1
- package/src/domain/failure.ts +33 -0
- package/src/domain/model-source.ts +30 -0
- package/src/domain/orchestration/control.test.ts +69 -35
- package/src/domain/orchestration/control.ts +110 -89
- package/src/domain/orchestration/fanout.test.ts +125 -20
- package/src/domain/orchestration/fanout.ts +60 -22
- package/src/domain/orchestration/gates.test.ts +14 -15
- package/src/domain/orchestration/launch.test.ts +210 -11
- package/src/domain/orchestration/launch.ts +142 -32
- package/src/domain/orchestration/requests.test.ts +11 -16
- package/src/domain/orchestration/requests.ts +14 -1
- package/src/domain/orchestration/review.test.ts +89 -11
- package/src/domain/orchestration/run-wait.test.ts +4 -1
- package/src/domain/orchestration/run-wait.ts +1 -1
- package/src/domain/orchestration/workflow.test.ts +157 -16
- package/src/domain/orchestration/workflow.ts +269 -196
- package/src/domain/run-records.ts +214 -52
- package/src/domain/settlement.test.ts +186 -0
- package/src/domain/settlement.ts +137 -0
- package/src/domain/shapes.test.ts +20 -0
- package/src/domain/skills.test.ts +31 -20
- package/src/domain/skills.ts +36 -19
- package/src/domain/toolkit.test.ts +15 -1
- package/src/domain/toolkit.ts +8 -13
- package/src/domain/turn-budget.test.ts +37 -0
- package/src/domain/turn-budget.ts +23 -0
- package/src/domain/view.test.ts +78 -14
- package/src/domain/view.ts +67 -35
- package/src/extension/commands.test.ts +27 -23
- package/src/extension/commands.ts +10 -7
- package/src/extension/entry.test.ts +214 -56
- package/src/extension/entry.ts +78 -40
- package/src/extension/gate-alerts.test.ts +10 -8
- package/src/extension/inspector-command.test.ts +173 -21
- package/src/extension/inspector-command.ts +100 -53
- package/src/extension/prompt.test.ts +2 -4
- package/src/extension/retry-observer.test.ts +292 -0
- package/src/extension/retry-observer.ts +150 -0
- package/src/extension/settle-alerts.test.ts +579 -0
- package/src/extension/settle-alerts.ts +175 -0
- package/src/extension/shortcut.test.ts +6 -8
- package/src/extension/tools.test.ts +325 -24
- package/src/extension/tools.ts +61 -13
- package/src/extension/version.test.ts +21 -29
- package/src/goldens.test.ts +294 -0
- package/src/harness-policy.test.ts +13 -11
- package/src/layers.test.ts +309 -0
- package/src/layers.ts +405 -0
- package/src/package-manifest.test.ts +32 -19
- package/src/presentation/actions.test.ts +8 -8
- package/src/presentation/activity.test.ts +7 -8
- package/src/presentation/close.test.ts +14 -16
- package/src/presentation/inspector.test.ts +14 -11
- package/src/presentation/inspector.ts +27 -25
- package/src/presentation/overlay.test.ts +1 -3
- package/src/presentation/repaint.test.ts +226 -190
- package/src/presentation/snapshot.test.ts +5 -0
- package/src/presentation/status.test.ts +43 -0
- package/src/presentation/status.ts +30 -0
- package/src/presentation/strip.test.ts +13 -6
- package/src/presentation/strip.ts +7 -14
- package/src/presentation/wording.test.ts +153 -3
- package/src/presentation/wording.ts +78 -6
- package/src/runtime/bridge.ts +38 -44
- package/src/runtime/{timers.test.ts → clock.test.ts} +1 -1
- package/src/runtime/clock.ts +31 -0
- package/src/runtime/config.test.ts +26 -7
- package/src/runtime/config.ts +6 -1
- package/src/runtime/execution-env.test.ts +6 -8
- package/src/runtime/extensions.test.ts +4 -4
- package/src/runtime/extensions.ts +23 -11
- package/src/runtime/handle.ts +54 -0
- package/src/runtime/log.test.ts +19 -7
- package/src/runtime/log.ts +23 -0
- package/src/runtime/mcp.test.ts +29 -32
- package/src/runtime/mcp.ts +70 -3
- 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 +24 -31
- package/src/runtime/session.ts +99 -261
- package/src/runtime/storage.ts +29 -0
- package/src/test-hygiene.test.ts +97 -0
- package/src/runtime/timers.ts +0 -10
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { makeTempDir } from "../test-support/platform.ts";
|
|
3
|
+
import { Context, Effect, Layer, Ref } from "effect";
|
|
4
|
+
import { stubHostSource } from "../test-support/stub-model-source.ts";
|
|
5
|
+
import { ClockService, systemTimers, TimersService } from "./runtime/clock.ts";
|
|
6
|
+
import { HostModelAdapter } from "./runtime/models.ts";
|
|
7
|
+
import { LogSinkService, nullLogSink } from "./runtime/log.ts";
|
|
8
|
+
import { McpHostService } from "./runtime/mcp.ts";
|
|
9
|
+
import type { SessionIdentity } from "./runtime/session.ts";
|
|
10
|
+
import { SkillsCatalogue } from "./domain/skills.ts";
|
|
11
|
+
import { ModelSourceService } from "./domain/model-source.ts";
|
|
12
|
+
import { StorageError, StorageService } from "./runtime/storage.ts";
|
|
13
|
+
import { makeSessionSeams, openSessionRuntime } from "./layers.ts";
|
|
14
|
+
import type { SessionHandle } from "./runtime/handle.ts";
|
|
15
|
+
import { SessionRuntimeTag } from "./runtime/session.ts";
|
|
16
|
+
import { MemoryStorage } from "@earendil-works/pi-durable";
|
|
17
|
+
import { failingStorage } from "../test-support/failing-storage.ts";
|
|
18
|
+
import { storePathFor } from "./runtime/session.ts";
|
|
19
|
+
import { countingLayer } from "../test-support/counting-runtime.ts";
|
|
20
|
+
import { runTools, WorkflowService } from "./domain/orchestration/workflow.ts";
|
|
21
|
+
|
|
22
|
+
const counted = countingLayer;
|
|
23
|
+
|
|
24
|
+
const identityFor = async (): Promise<SessionIdentity> => {
|
|
25
|
+
const sessionDir = await makeTempDir("delegates-layers-");
|
|
26
|
+
return { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
it.effect("the clock layer reads the host's now function when it is built with one", () =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
const context = yield* Layer.build(ClockService.layer(() => 42));
|
|
32
|
+
const clock = Context.get(context, ClockService);
|
|
33
|
+
assert.strictEqual(clock(), 42);
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
it.effect("the model source reads the host thunk at each call, not once at build", () =>
|
|
38
|
+
Effect.gen(function* () {
|
|
39
|
+
const first = { model: { provider: "a", modelId: "a" }, thinkingLevel: "low" } as const;
|
|
40
|
+
const second = { model: { provider: "b", modelId: "b" }, thinkingLevel: "high" } as const;
|
|
41
|
+
let host: typeof first | typeof second = first;
|
|
42
|
+
const context = yield* Layer.build(ModelSourceService.layer(models(), () => host));
|
|
43
|
+
const source = Context.get(context, ModelSourceService);
|
|
44
|
+
|
|
45
|
+
host = second;
|
|
46
|
+
assert.deepStrictEqual(source.host(), second);
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
it.effect("the model source refuses to invent a host model when none is given", () =>
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
const context = yield* Layer.build(ModelSourceService.layer(models(), undefined));
|
|
53
|
+
assert.throws(
|
|
54
|
+
() => Context.get(context, ModelSourceService).host(),
|
|
55
|
+
/No host model for this session/,
|
|
56
|
+
);
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
it.effect("one build constructs each seam once when three tags resolve from it", () =>
|
|
61
|
+
Effect.gen(function* () {
|
|
62
|
+
const storage: { builds: number } = { builds: 0 };
|
|
63
|
+
const log: { builds: number } = { builds: 0 };
|
|
64
|
+
const timers: { builds: number } = { builds: 0 };
|
|
65
|
+
const layer = Layer.mergeAll(
|
|
66
|
+
counted(
|
|
67
|
+
StorageService.layer(() => Promise.resolve(new MemoryStorage())),
|
|
68
|
+
storage,
|
|
69
|
+
),
|
|
70
|
+
counted(LogSinkService.layer(nullLogSink), log),
|
|
71
|
+
counted(TimersService.layer(systemTimers), timers),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const context = yield* Layer.build(layer);
|
|
75
|
+
Context.get(context, StorageService);
|
|
76
|
+
Context.get(context, LogSinkService);
|
|
77
|
+
Context.get(context, TimersService);
|
|
78
|
+
|
|
79
|
+
assert.deepStrictEqual(
|
|
80
|
+
{ storage: storage.builds, log: log.builds, timers: timers.builds },
|
|
81
|
+
{ storage: 1, log: 1, timers: 1 },
|
|
82
|
+
);
|
|
83
|
+
}),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
it.effect("the storage layer fails the build with a StorageError when its open rejects", () =>
|
|
87
|
+
Effect.gen(function* () {
|
|
88
|
+
const failing = Layer.build(StorageService.layer(() => Promise.reject(new Error("no store"))));
|
|
89
|
+
const error = yield* Effect.flip(failing);
|
|
90
|
+
assert.ok(error instanceof StorageError, String(error));
|
|
91
|
+
|
|
92
|
+
const context = yield* Layer.build(
|
|
93
|
+
makeSessionSeams({
|
|
94
|
+
identity: yield* Effect.promise(identityFor),
|
|
95
|
+
models: new HostModelAdapter(stubHostSource()),
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
assert.deepStrictEqual(Context.get(context, SkillsCatalogue).all(), []);
|
|
99
|
+
assert.deepStrictEqual(Context.get(context, McpHostService).connections, []);
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const models = () => new HostModelAdapter(stubHostSource());
|
|
104
|
+
|
|
105
|
+
/** A temp identity whose session id differs, so a swap moves the store. */
|
|
106
|
+
const nextIdentity = (sessionId: string): Effect.Effect<SessionIdentity> =>
|
|
107
|
+
Effect.map(Effect.promise(identityFor), (identity) => ({ ...identity, sessionId }));
|
|
108
|
+
|
|
109
|
+
it.effect(
|
|
110
|
+
"two awaiters of the same close share one settled result when close is called twice",
|
|
111
|
+
() =>
|
|
112
|
+
Effect.gen(function* () {
|
|
113
|
+
const current = Ref.makeUnsafe<SessionHandle | undefined>(undefined);
|
|
114
|
+
const identity = yield* nextIdentity("a");
|
|
115
|
+
const storage = new MemoryStorage();
|
|
116
|
+
let storeCloses = 0;
|
|
117
|
+
const counted = new Proxy(storage, {
|
|
118
|
+
get(target, property, receiver) {
|
|
119
|
+
if (property === "close") {
|
|
120
|
+
return async (context: never) => {
|
|
121
|
+
storeCloses += 1;
|
|
122
|
+
await target.close(context);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
const value = Reflect.get(target, property, receiver) as unknown;
|
|
126
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
const handle = yield* Effect.promise(() =>
|
|
130
|
+
openSessionRuntime(
|
|
131
|
+
{ identity, models: models() },
|
|
132
|
+
current,
|
|
133
|
+
makeSessionSeams({ identity, models: models() }, { storage: counted }),
|
|
134
|
+
),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
// Both awaiters resolve and the scope's finalizers run once.
|
|
138
|
+
yield* Effect.promise(() => Promise.all([handle.close(), handle.close()]));
|
|
139
|
+
assert.strictEqual(storeCloses, 1);
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
it.effect(
|
|
144
|
+
"a tool call after a swap resolves the new runtime when the previous handle is closed",
|
|
145
|
+
() =>
|
|
146
|
+
Effect.gen(function* () {
|
|
147
|
+
const current = Ref.makeUnsafe<SessionHandle | undefined>(undefined);
|
|
148
|
+
const first = yield* nextIdentity("a");
|
|
149
|
+
const second = yield* nextIdentity("b");
|
|
150
|
+
yield* Effect.promise(() =>
|
|
151
|
+
openSessionRuntime(
|
|
152
|
+
{ identity: first, models: models() },
|
|
153
|
+
current,
|
|
154
|
+
makeSessionSeams({ identity: first, models: models() }),
|
|
155
|
+
),
|
|
156
|
+
);
|
|
157
|
+
const previous = Ref.getUnsafe(current);
|
|
158
|
+
assert.ok(previous !== undefined);
|
|
159
|
+
|
|
160
|
+
yield* Effect.promise(() =>
|
|
161
|
+
openSessionRuntime(
|
|
162
|
+
{ identity: second, models: models() },
|
|
163
|
+
current,
|
|
164
|
+
makeSessionSeams({ identity: second, models: models() }),
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
yield* Effect.promise(() => previous.close());
|
|
168
|
+
|
|
169
|
+
const live = Ref.getUnsafe(current);
|
|
170
|
+
assert.ok(live !== undefined);
|
|
171
|
+
const sessionId = yield* live.run(
|
|
172
|
+
Effect.map(SessionRuntimeTag, (runtime) => runtime.sessionId),
|
|
173
|
+
);
|
|
174
|
+
assert.strictEqual(sessionId, "b");
|
|
175
|
+
}),
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
it.effect("a session opens from the graph when no overrides are passed", () =>
|
|
179
|
+
Effect.gen(function* () {
|
|
180
|
+
const identity = yield* nextIdentity("s-1");
|
|
181
|
+
const current = Ref.makeUnsafe<SessionHandle | undefined>(undefined);
|
|
182
|
+
const handle = yield* Effect.promise(() =>
|
|
183
|
+
openSessionRuntime({ identity, models: models() }, current),
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
assert.strictEqual(handle.runtime.storePath, storePathFor(identity));
|
|
187
|
+
assert.strictEqual(handle.runtime.sessionId, "s-1");
|
|
188
|
+
assert.strictEqual(handle.runtime.state.kind, "ready");
|
|
189
|
+
// The close lives on the handle only: the runtime no longer owns one.
|
|
190
|
+
assert.strictEqual("close" in handle.runtime, false);
|
|
191
|
+
yield* Effect.promise(() => handle.close());
|
|
192
|
+
}),
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
it.effect("a test that needs a failing storage seam provides it through the layer", () =>
|
|
196
|
+
Effect.gen(function* () {
|
|
197
|
+
const identity = yield* nextIdentity("s-1");
|
|
198
|
+
const { storage, failAnyNext } = failingStorage(new MemoryStorage());
|
|
199
|
+
failAnyNext();
|
|
200
|
+
const failed = yield* Effect.promise(async () => {
|
|
201
|
+
try {
|
|
202
|
+
await openSessionRuntime(
|
|
203
|
+
{ identity, models: models() },
|
|
204
|
+
undefined,
|
|
205
|
+
makeSessionSeams({ identity, models: models() }, { storage }),
|
|
206
|
+
);
|
|
207
|
+
return false;
|
|
208
|
+
} catch {
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
assert.strictEqual(failed, true);
|
|
213
|
+
}),
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
it.effect("a failed open releases what the build acquired before failing", () =>
|
|
217
|
+
Effect.gen(function* () {
|
|
218
|
+
const identity = yield* nextIdentity("s-1");
|
|
219
|
+
const events: string[] = [];
|
|
220
|
+
const { storage, failAnyNext } = failingStorage(new MemoryStorage());
|
|
221
|
+
failAnyNext();
|
|
222
|
+
const acquired = Layer.effectDiscard(
|
|
223
|
+
Effect.acquireRelease(
|
|
224
|
+
Effect.sync(() => events.push("acquired")),
|
|
225
|
+
() => Effect.sync(() => events.push("released")),
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
const failed = yield* Effect.promise(async () => {
|
|
229
|
+
try {
|
|
230
|
+
await openSessionRuntime(
|
|
231
|
+
{ identity, models: models() },
|
|
232
|
+
undefined,
|
|
233
|
+
Layer.mergeAll(makeSessionSeams({ identity, models: models() }, { storage }), acquired),
|
|
234
|
+
);
|
|
235
|
+
return false;
|
|
236
|
+
} catch {
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// No `dispose` runs on this path: the release is the only thing that proves it.
|
|
242
|
+
assert.strictEqual(failed, true);
|
|
243
|
+
assert.deepStrictEqual(events, ["acquired", "released"]);
|
|
244
|
+
}),
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
/** One handle over the seams a fixture needs, its workflow layer counted. */
|
|
248
|
+
const openCounted = (
|
|
249
|
+
identity: SessionIdentity,
|
|
250
|
+
current: Ref.Ref<SessionHandle | undefined>,
|
|
251
|
+
counter: { builds: number },
|
|
252
|
+
): Effect.Effect<SessionHandle> =>
|
|
253
|
+
Effect.promise(() =>
|
|
254
|
+
openSessionRuntime(
|
|
255
|
+
{ identity, models: models() },
|
|
256
|
+
current,
|
|
257
|
+
makeSessionSeams({ identity, models: models() }),
|
|
258
|
+
countingLayer(WorkflowService.layer, counter),
|
|
259
|
+
),
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
/** One script tool call: a refusal still runs the whole call path inside the handle. */
|
|
263
|
+
const callStatus = (current: Ref.Ref<SessionHandle | undefined>): Promise<unknown> => {
|
|
264
|
+
const tool = runTools(current).find((candidate) => candidate.name === "status");
|
|
265
|
+
assert.ok(tool !== undefined);
|
|
266
|
+
return tool.execute({ run: "run:missing" }, undefined as never, undefined as never);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
it.effect("the workflow layer builds once per handle when two tool calls run on one session", () =>
|
|
270
|
+
Effect.gen(function* () {
|
|
271
|
+
const current = Ref.makeUnsafe<SessionHandle | undefined>(undefined);
|
|
272
|
+
const counter: { builds: number } = { builds: 0 };
|
|
273
|
+
const identity = yield* nextIdentity("s-1");
|
|
274
|
+
const handle = yield* openCounted(identity, current, counter);
|
|
275
|
+
try {
|
|
276
|
+
// One service instance serves every call; the graph built it at open.
|
|
277
|
+
const service = yield* handle.run(WorkflowService);
|
|
278
|
+
yield* Effect.promise(() => Promise.all([callStatus(current), callStatus(current)]));
|
|
279
|
+
const afterCalls = yield* handle.run(WorkflowService);
|
|
280
|
+
assert.strictEqual(afterCalls, service);
|
|
281
|
+
assert.strictEqual(counter.builds, 1);
|
|
282
|
+
} finally {
|
|
283
|
+
yield* Effect.promise(() => handle.close());
|
|
284
|
+
}
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
it.effect("one more layer is built when a tool call runs after a session swap", () =>
|
|
289
|
+
Effect.gen(function* () {
|
|
290
|
+
const current = Ref.makeUnsafe<SessionHandle | undefined>(undefined);
|
|
291
|
+
const first: { builds: number } = { builds: 0 };
|
|
292
|
+
const second: { builds: number } = { builds: 0 };
|
|
293
|
+
const firstIdentity = yield* nextIdentity("s-1");
|
|
294
|
+
const secondIdentity = yield* nextIdentity("s-2");
|
|
295
|
+
const before = yield* openCounted(firstIdentity, current, first);
|
|
296
|
+
const after = yield* openCounted(secondIdentity, current, second);
|
|
297
|
+
try {
|
|
298
|
+
// The seam reads the Ref, so the call lands in the replacement's graph.
|
|
299
|
+
yield* Effect.promise(() => callStatus(current));
|
|
300
|
+
assert.deepStrictEqual(
|
|
301
|
+
{ second: second.builds, first: first.builds },
|
|
302
|
+
{ second: 1, first: 1 },
|
|
303
|
+
);
|
|
304
|
+
} finally {
|
|
305
|
+
yield* Effect.promise(() => after.close());
|
|
306
|
+
yield* Effect.promise(() => before.close());
|
|
307
|
+
}
|
|
308
|
+
}),
|
|
309
|
+
);
|