@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
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
2
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
3
|
import { Effect } from "effect";
|
|
8
4
|
import type { CodemodeSandboxBase } from "@earendil-works/pi-codemode/portable";
|
|
9
5
|
import { assert, it } from "@effect/vitest";
|
|
@@ -18,15 +14,12 @@ import { launch } from "../domain/orchestration/launch.ts";
|
|
|
18
14
|
import { HarnessClosed } from "./bridge.ts";
|
|
19
15
|
import { HostModelAdapter } from "./models.ts";
|
|
20
16
|
import type { LogFields, LogLevel, LogSink } from "./log.ts";
|
|
21
|
-
import {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
storePathFor,
|
|
25
|
-
type SessionIdentity,
|
|
26
|
-
} from "./session.ts";
|
|
17
|
+
import { openReadOnlyRuntime, storePathFor, type SessionIdentity } from "./session.ts";
|
|
18
|
+
import { openTestSession } from "../../test-support/seams.ts";
|
|
19
|
+
import { existsPath, join, makeTempDir, readDir, writeText } from "../../test-support/platform.ts";
|
|
27
20
|
|
|
28
21
|
const identityFor = async (sessionId: string): Promise<SessionIdentity> => {
|
|
29
|
-
const sessionDir = await
|
|
22
|
+
const sessionDir = await makeTempDir("delegates-session-");
|
|
30
23
|
return {
|
|
31
24
|
sessionId,
|
|
32
25
|
sessionDir,
|
|
@@ -78,13 +71,13 @@ const recordingSink = () => {
|
|
|
78
71
|
|
|
79
72
|
it("opening a session creates one store under the session directory", async () => {
|
|
80
73
|
const identity = await identityFor("s-1");
|
|
81
|
-
const runtime = await
|
|
74
|
+
const runtime = await openTestSession({ identity, models: models() });
|
|
82
75
|
try {
|
|
83
76
|
assert.strictEqual(runtime.storePath, join(identity.sessionDir, "delegates-s-1.sqlite"));
|
|
84
77
|
assert.strictEqual(runtime.storePath, storePathFor(identity));
|
|
85
|
-
assert.ok(
|
|
78
|
+
assert.ok(await existsPath(runtime.storePath));
|
|
86
79
|
assert.strictEqual(runtime.state.kind, "ready");
|
|
87
|
-
const stores = (await
|
|
80
|
+
const stores = (await readDir(identity.sessionDir)).filter((name) => name.endsWith(".sqlite"));
|
|
88
81
|
assert.deepStrictEqual(stores, ["delegates-s-1.sqlite"]);
|
|
89
82
|
} finally {
|
|
90
83
|
await runtime.close();
|
|
@@ -93,11 +86,11 @@ it("opening a session creates one store under the session directory", async () =
|
|
|
93
86
|
|
|
94
87
|
it("reopening the same identity resumes committed state", async () => {
|
|
95
88
|
const identity = await identityFor("s-1");
|
|
96
|
-
const first = await
|
|
89
|
+
const first = await openTestSession({ identity, models: models() });
|
|
97
90
|
await commitProbe(first.harness, 1);
|
|
98
91
|
await first.close();
|
|
99
92
|
|
|
100
|
-
const second = await
|
|
93
|
+
const second = await openTestSession({ identity, models: models() });
|
|
101
94
|
try {
|
|
102
95
|
assert.deepStrictEqual(await second.snapshot(ProbeDoc), { counter: 1 });
|
|
103
96
|
assert.strictEqual(second.state.kind, "ready");
|
|
@@ -108,12 +101,12 @@ it("reopening the same identity resumes committed state", async () => {
|
|
|
108
101
|
|
|
109
102
|
it("a forked session gets a fresh store", async () => {
|
|
110
103
|
const identity = await identityFor("s-1");
|
|
111
|
-
const first = await
|
|
104
|
+
const first = await openTestSession({ identity, models: models() });
|
|
112
105
|
await commitProbe(first.harness, 1);
|
|
113
106
|
await first.close();
|
|
114
107
|
|
|
115
108
|
const forkedIdentity = { ...identity, sessionId: "s-2" };
|
|
116
|
-
const forked = await
|
|
109
|
+
const forked = await openTestSession({ identity: forkedIdentity, models: models() });
|
|
117
110
|
try {
|
|
118
111
|
assert.notStrictEqual(forked.storePath, storePathFor(identity));
|
|
119
112
|
assert.strictEqual(await forked.snapshot(ProbeDoc), undefined);
|
|
@@ -124,7 +117,7 @@ it("a forked session gets a fresh store", async () => {
|
|
|
124
117
|
|
|
125
118
|
it("a read-only open refuses writes but reads", async () => {
|
|
126
119
|
const identity = await identityFor("s-1");
|
|
127
|
-
const writer = await
|
|
120
|
+
const writer = await openTestSession({ identity, models: models() });
|
|
128
121
|
await commitProbe(writer.harness, 5);
|
|
129
122
|
await writer.close();
|
|
130
123
|
|
|
@@ -147,7 +140,7 @@ it("a failing storage ends the runtime with its reason", async () => {
|
|
|
147
140
|
const identity = await identityFor("s-1");
|
|
148
141
|
const { storage, failNext } = failingStorage(new MemoryStorage());
|
|
149
142
|
const states: string[] = [];
|
|
150
|
-
const runtime = await
|
|
143
|
+
const runtime = await openTestSession({ identity, models: models(), storage });
|
|
151
144
|
const unsubscribe = runtime.onState((state) => states.push(state.kind));
|
|
152
145
|
|
|
153
146
|
failNext();
|
|
@@ -171,7 +164,7 @@ it("a failed open closes the sandbox", async () => {
|
|
|
171
164
|
const { storage, failAnyNext } = failingStorage(new MemoryStorage());
|
|
172
165
|
failAnyNext();
|
|
173
166
|
await rejects(
|
|
174
|
-
|
|
167
|
+
openTestSession({ identity, models: models(), storage, sandbox: events.sandbox }),
|
|
175
168
|
/storage failed/,
|
|
176
169
|
);
|
|
177
170
|
assert.deepStrictEqual(events.events, ["close"]);
|
|
@@ -181,7 +174,7 @@ it("a storage failure logs one entry and closes the runtime", async () => {
|
|
|
181
174
|
const identity = await identityFor("s-1");
|
|
182
175
|
const { storage, failNext } = failingStorage(new MemoryStorage());
|
|
183
176
|
const { sink, entries } = recordingSink();
|
|
184
|
-
const runtime = await
|
|
177
|
+
const runtime = await openTestSession({ identity, models: models(), storage, log: sink });
|
|
185
178
|
try {
|
|
186
179
|
failNext();
|
|
187
180
|
await rejects(commitProbe(runtime.harness, 1));
|
|
@@ -201,9 +194,9 @@ it("a storage failure logs one entry and closes the runtime", async () => {
|
|
|
201
194
|
it("config diagnostics are logged when delegates.json is invalid", async () => {
|
|
202
195
|
const identity = await identityFor("s-1");
|
|
203
196
|
const path = join(identity.agentDir, "delegates.json");
|
|
204
|
-
await
|
|
197
|
+
await writeText(path, "{\n");
|
|
205
198
|
const { sink, entries } = recordingSink();
|
|
206
|
-
const runtime = await
|
|
199
|
+
const runtime = await openTestSession({ identity, models: models(), log: sink });
|
|
207
200
|
try {
|
|
208
201
|
assert.strictEqual(entries.length, 1);
|
|
209
202
|
assert.strictEqual(entries[0]?.level, "warn");
|
|
@@ -231,14 +224,14 @@ it("a failed open closes the storage", async () => {
|
|
|
231
224
|
return typeof value === "function" ? value.bind(target) : value;
|
|
232
225
|
},
|
|
233
226
|
}) as unknown as Storage;
|
|
234
|
-
await rejects(
|
|
227
|
+
await rejects(openTestSession({ identity, models: models(), storage }));
|
|
235
228
|
assert.strictEqual(closed, true);
|
|
236
229
|
});
|
|
237
230
|
|
|
238
231
|
it("the injected clock is the runtime clock", async () => {
|
|
239
232
|
const identity = await identityFor("s-1");
|
|
240
233
|
let now = 1_000;
|
|
241
|
-
const runtime = await
|
|
234
|
+
const runtime = await openTestSession({ identity, models: models(), now: () => now });
|
|
242
235
|
try {
|
|
243
236
|
assert.strictEqual(runtime.now(), 1_000);
|
|
244
237
|
now = 2_000;
|
|
@@ -250,7 +243,7 @@ it("the injected clock is the runtime clock", async () => {
|
|
|
250
243
|
|
|
251
244
|
it("the root conversation carries no tools and no extensions", async () => {
|
|
252
245
|
const identity = await identityFor("s-1");
|
|
253
|
-
const runtime = await
|
|
246
|
+
const runtime = await openTestSession({ identity, models: models() });
|
|
254
247
|
try {
|
|
255
248
|
const agent = await runtime.root.agent(BACKGROUND_CONTEXT);
|
|
256
249
|
assert.deepStrictEqual(agent.tools, []);
|
|
@@ -262,7 +255,7 @@ it("the root conversation carries no tools and no extensions", async () => {
|
|
|
262
255
|
|
|
263
256
|
it("the caller signal reaches the harness call", async () => {
|
|
264
257
|
const identity = await identityFor("s-1");
|
|
265
|
-
const runtime = await
|
|
258
|
+
const runtime = await openTestSession({ identity, models: models() });
|
|
266
259
|
try {
|
|
267
260
|
const controller = new AbortController();
|
|
268
261
|
let abortedDuring: boolean | undefined;
|
|
@@ -292,7 +285,7 @@ it("the caller signal reaches the harness call", async () => {
|
|
|
292
285
|
it("a skill scan problem reaches the log at open", async () => {
|
|
293
286
|
const identity = await identityFor("s-1");
|
|
294
287
|
const { sink, entries } = recordingSink();
|
|
295
|
-
const runtime = await
|
|
288
|
+
const runtime = await openTestSession({
|
|
296
289
|
identity,
|
|
297
290
|
models: models(),
|
|
298
291
|
log: sink,
|
|
@@ -317,7 +310,7 @@ it("a skill scan problem reaches the log at open", async () => {
|
|
|
317
310
|
|
|
318
311
|
it("a viewer reads a store written by a runtime", async () => {
|
|
319
312
|
const identity = await identityFor("s-1");
|
|
320
|
-
const writer = await
|
|
313
|
+
const writer = await openTestSession({ identity, models: models() });
|
|
321
314
|
await commitProbe(writer.harness, 1);
|
|
322
315
|
await writer.close();
|
|
323
316
|
|
|
@@ -331,7 +324,7 @@ it("a viewer reads a store written by a runtime", async () => {
|
|
|
331
324
|
|
|
332
325
|
it("the read-only viewer satisfies the view port", async () => {
|
|
333
326
|
const identity = await identityFor("s-1");
|
|
334
|
-
const writer = await
|
|
327
|
+
const writer = await openTestSession({ identity, models: models() });
|
|
335
328
|
const launched = await launch(
|
|
336
329
|
{ kind: "single", agent: "scout", task: "look" },
|
|
337
330
|
{
|
package/src/runtime/session.ts
CHANGED
|
@@ -3,48 +3,32 @@ import type { Models } from "@earendil-works/pi-ai/models";
|
|
|
3
3
|
import type { JsonValue } from "@earendil-works/chord/json";
|
|
4
4
|
import {
|
|
5
5
|
Harness,
|
|
6
|
-
SessionFailed,
|
|
7
6
|
type Conversation,
|
|
8
7
|
type ConversationId,
|
|
9
8
|
type JsonObject,
|
|
10
|
-
type Report,
|
|
11
9
|
type Registry,
|
|
12
10
|
type SessionDocFamilyToken,
|
|
13
11
|
type SessionDocToken,
|
|
14
12
|
type SetTimer,
|
|
15
|
-
type Storage,
|
|
16
13
|
} from "@earendil-works/pi-durable";
|
|
17
14
|
import { openNodeSqliteStorage } from "@earendil-works/pi-durable/storage/sqlite/node";
|
|
18
15
|
import { Effect } from "effect";
|
|
19
16
|
import { join } from "node:path";
|
|
20
17
|
import { DurableBridge, type HarnessCalls } from "./bridge.ts";
|
|
21
|
-
import {
|
|
22
|
-
import { conversationEnv } from "./execution-env.ts";
|
|
23
|
-
import { readDelegatesConfig, type DelegatesConfig } from "./config.ts";
|
|
18
|
+
import { type DelegatesConfig } from "./config.ts";
|
|
24
19
|
import { createDelegatesRegistry, type DelegatesTasks } from "./extensions.ts";
|
|
25
|
-
import {
|
|
20
|
+
import type { DefinitionCatalogue } from "../domain/definitions.ts";
|
|
21
|
+
import { sweepRequests } from "../domain/orchestration/requests.ts";
|
|
22
|
+
import type { RuntimeState, RuntimeStateMachine } from "./runtime-state.ts";
|
|
26
23
|
import type { HostDefaults } from "../domain/orchestration/launch.ts";
|
|
27
|
-
import { refusingSandbox } from "../domain/orchestration/workflow.ts";
|
|
24
|
+
import { refusingSandbox } from "../domain/orchestration/workflow-section.ts";
|
|
28
25
|
import type { ViewPort } from "../domain/view.ts";
|
|
29
26
|
import type { CodemodeSandboxBase } from "@earendil-works/pi-codemode/portable";
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
type McpClientFactory,
|
|
36
|
-
type McpHost,
|
|
37
|
-
type McpPresence,
|
|
38
|
-
} from "./mcp.ts";
|
|
39
|
-
import {
|
|
40
|
-
createSkills,
|
|
41
|
-
emptySkills,
|
|
42
|
-
type SkillRoots,
|
|
43
|
-
type SkillsService,
|
|
44
|
-
} from "../domain/skills.ts";
|
|
45
|
-
import { systemTimers, type Timers } from "./timers.ts";
|
|
46
|
-
import type { AdmissionHost } from "../domain/orchestration/admission.ts";
|
|
47
|
-
import { pruneRequests } from "../domain/orchestration/requests.ts";
|
|
27
|
+
import { reasonOf, type LogSink } from "./log.ts";
|
|
28
|
+
import type { McpHost, McpPresence } from "./mcp.ts";
|
|
29
|
+
import { SkillsCatalogue } from "../domain/skills.ts";
|
|
30
|
+
import type { Timers } from "./clock.ts";
|
|
31
|
+
import type { SettleSignal } from "../domain/settlement.ts";
|
|
48
32
|
|
|
49
33
|
export interface SessionIdentity {
|
|
50
34
|
readonly sessionId: string;
|
|
@@ -53,42 +37,7 @@ export interface SessionIdentity {
|
|
|
53
37
|
readonly agentDir: string;
|
|
54
38
|
}
|
|
55
39
|
|
|
56
|
-
export type RuntimeState
|
|
57
|
-
| { readonly kind: "opening" }
|
|
58
|
-
| { readonly kind: "ready" }
|
|
59
|
-
| { readonly kind: "failing"; readonly reason: string }
|
|
60
|
-
| { readonly kind: "closed"; readonly reason?: string };
|
|
61
|
-
|
|
62
|
-
export interface SessionRuntimeDeps {
|
|
63
|
-
readonly identity: SessionIdentity;
|
|
64
|
-
readonly models: Models;
|
|
65
|
-
/** Overrides file-backed storage in tests. */
|
|
66
|
-
readonly storage?: Storage;
|
|
67
|
-
readonly config?: DelegatesConfig;
|
|
68
|
-
/** The one sink; default `nullLogSink`. */
|
|
69
|
-
readonly log?: LogSink;
|
|
70
|
-
readonly onReport?: (report: Report) => void;
|
|
71
|
-
/** The one clock; default `Date.now`. */
|
|
72
|
-
readonly now?: () => number;
|
|
73
|
-
/** The one timer source; default `systemTimers`. */
|
|
74
|
-
readonly timers?: Timers;
|
|
75
|
-
/** Overrides the Pi skill roots in tests. */
|
|
76
|
-
readonly skills?: SkillRoots;
|
|
77
|
-
/** The registered definitions a scripted child resolves; default none. */
|
|
78
|
-
readonly catalogue?: DefinitionCatalogue;
|
|
79
|
-
/** The host model a scripted child fills in, read at each launch; default empty. */
|
|
80
|
-
readonly host?: () => HostDefaults;
|
|
81
|
-
/** Overrides the code mode sandbox in tests. */
|
|
82
|
-
readonly sandbox?: CodemodeSandboxBase;
|
|
83
|
-
/** The project's trust answer; the runtime reads `.pi/mcp.json` only when true. */
|
|
84
|
-
readonly projectTrusted?: boolean;
|
|
85
|
-
/** Overrides the file-backed MCP host in tests. */
|
|
86
|
-
readonly mcp?: McpHost;
|
|
87
|
-
/** Overrides the real MCP transport factory in tests. */
|
|
88
|
-
readonly mcpFactory?: McpClientFactory;
|
|
89
|
-
/** Shows one host notice, such as a dropped MCP connection; default none. */
|
|
90
|
-
readonly notify?: (message: string) => void;
|
|
91
|
-
}
|
|
40
|
+
export type { RuntimeState } from "./runtime-state.ts";
|
|
92
41
|
|
|
93
42
|
export interface SessionRuntime {
|
|
94
43
|
readonly storePath: string;
|
|
@@ -113,7 +62,7 @@ export interface SessionRuntime {
|
|
|
113
62
|
/** The one code mode sandbox; it closes after the harness. */
|
|
114
63
|
readonly sandbox: CodemodeSandboxBase;
|
|
115
64
|
/** The session's skill catalogue; one scan when the runtime opens. */
|
|
116
|
-
readonly skills:
|
|
65
|
+
readonly skills: SkillsCatalogue["Service"];
|
|
117
66
|
/** The MCP servers this session connects to; none in a read-only runtime. */
|
|
118
67
|
readonly mcp: McpHost;
|
|
119
68
|
/** Configured servers with no live connection. */
|
|
@@ -122,6 +71,8 @@ export interface SessionRuntime {
|
|
|
122
71
|
readonly now: () => number;
|
|
123
72
|
/** The timer source the domain reads, shared with the harness. */
|
|
124
73
|
readonly timers: Timers;
|
|
74
|
+
/** The one in-process settle signal: the recorder emits, the batcher listens. */
|
|
75
|
+
readonly settle: SettleSignal;
|
|
125
76
|
readonly state: RuntimeState;
|
|
126
77
|
readonly onState: (listener: (state: RuntimeState) => void) => () => void;
|
|
127
78
|
snapshot<T extends JsonObject>(token: SessionDocToken<T>): Promise<Readonly<T> | undefined>;
|
|
@@ -129,13 +80,12 @@ export interface SessionRuntime {
|
|
|
129
80
|
token: SessionDocFamilyToken<T, I>,
|
|
130
81
|
key: string,
|
|
131
82
|
): Promise<Readonly<T> | undefined>;
|
|
132
|
-
close(): Promise<void>;
|
|
133
83
|
}
|
|
134
84
|
|
|
135
85
|
export type ReadOnlyRuntime = ViewPort & { readonly close: () => Promise<void> };
|
|
136
86
|
|
|
137
87
|
/** A family token's definition carries `family: true`; a singleton token's does not. */
|
|
138
|
-
const isFamilyToken = <T extends JsonObject, I extends JsonValue>(
|
|
88
|
+
export const isFamilyToken = <T extends JsonObject, I extends JsonValue>(
|
|
139
89
|
token: SessionDocToken<T> | SessionDocFamilyToken<T, I>,
|
|
140
90
|
): token is SessionDocFamilyToken<T, I> => "family" in token.definition;
|
|
141
91
|
|
|
@@ -143,226 +93,114 @@ export const storePathFor = (identity: SessionIdentity): string =>
|
|
|
143
93
|
join(identity.sessionDir, `delegates-${identity.sessionId}.sqlite`);
|
|
144
94
|
|
|
145
95
|
/** The harness's timer seam, armed by the runtime's one timer source. */
|
|
146
|
-
const setTimerFrom =
|
|
96
|
+
export const setTimerFrom =
|
|
147
97
|
(timers: Timers): SetTimer =>
|
|
148
98
|
(callback, delayMs) => {
|
|
149
99
|
const handle = timers.setTimeout(callback, delayMs);
|
|
150
100
|
return () => timers.clearTimeout(handle);
|
|
151
101
|
};
|
|
152
102
|
|
|
103
|
+
// Re-exported from a leaf module: `workflow.ts` requires the tag, and this file
|
|
104
|
+
// reaches `workflow.ts` through `extensions.ts`.
|
|
105
|
+
export { SessionRuntimeTag } from "./session-tag.ts";
|
|
106
|
+
|
|
153
107
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
108
|
+
* Filled once, after the runtime object exists, for getters created before it.
|
|
109
|
+
*
|
|
110
|
+
* Two registries reach the same runtime on purpose. This one is filled inside
|
|
111
|
+
* `buildSessionRuntime`, before the handle exists, because `harness.resume()`
|
|
112
|
+
* runs in the same turn and the registry's thunks (`admission.inspect`,
|
|
113
|
+
* `mcpPresence`) read it. The handle's `Ref` is written only once the handle is
|
|
114
|
+
* built, so it cannot serve that window.
|
|
157
115
|
*/
|
|
158
|
-
export
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const log = deps.log ?? nullLogSink;
|
|
162
|
-
for (const diagnostic of config.diagnostics) {
|
|
163
|
-
log.write("warn", diagnostic.code, { detail: diagnostic.detail });
|
|
164
|
-
}
|
|
165
|
-
const storage = deps.storage ?? (await openNodeSqliteStorage(storePath));
|
|
166
|
-
const now = deps.now ?? (() => Date.now());
|
|
167
|
-
const timers = deps.timers ?? systemTimers;
|
|
168
|
-
const roots = deps.skills ?? {
|
|
169
|
-
cwd: deps.identity.cwd,
|
|
170
|
-
agentDir: deps.identity.agentDir,
|
|
171
|
-
skillPaths: [],
|
|
172
|
-
};
|
|
173
|
-
const skills = createSkills(roots);
|
|
174
|
-
for (const diagnostic of skills.diagnostics()) {
|
|
175
|
-
log.write("warn", diagnostic.code, { detail: diagnostic.detail });
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
let state: RuntimeState = { kind: "opening" };
|
|
179
|
-
const listeners = new Set<(state: RuntimeState) => void>();
|
|
180
|
-
const setState = (next: RuntimeState): void => {
|
|
181
|
-
state = next;
|
|
182
|
-
for (const listener of listeners) listener(next);
|
|
183
|
-
};
|
|
116
|
+
export interface RuntimeHolder {
|
|
117
|
+
runtime?: SessionRuntime;
|
|
118
|
+
}
|
|
184
119
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
agentDir: deps.identity.agentDir,
|
|
193
|
-
cwd: deps.identity.cwd,
|
|
194
|
-
projectTrusted: deps.projectTrusted ?? false,
|
|
195
|
-
});
|
|
196
|
-
const notify = deps.notify;
|
|
197
|
-
const mcp =
|
|
198
|
-
deps.mcp ??
|
|
199
|
-
(await connectServers(mcpConfig.servers, log, {
|
|
200
|
-
referenced: referencedServers(catalogue.all, mcpConfig.servers),
|
|
201
|
-
timers,
|
|
202
|
-
sessionDir: deps.identity.sessionDir,
|
|
203
|
-
...(deps.mcpFactory === undefined ? {} : { factory: deps.mcpFactory }),
|
|
204
|
-
...(notify === undefined
|
|
205
|
-
? {}
|
|
206
|
-
: {
|
|
207
|
-
onLost: (server: string) =>
|
|
208
|
-
notify(
|
|
209
|
-
`MCP server "${server}" disconnected; its tools fail until the session restarts.`,
|
|
210
|
-
),
|
|
211
|
-
}),
|
|
212
|
-
}));
|
|
213
|
-
for (const diagnostic of [...mcpConfig.diagnostics, ...mcp.diagnostics]) {
|
|
214
|
-
log.write("warn", diagnostic.code, { detail: diagnostic.detail });
|
|
215
|
-
}
|
|
216
|
-
const host =
|
|
217
|
-
deps.host ?? (() => ({ model: { provider: "", modelId: "" }, thinkingLevel: "low" }));
|
|
218
|
-
const admission: AdmissionHost = {
|
|
219
|
-
inspect: () => {
|
|
220
|
-
if (harnessRef.current === undefined) {
|
|
221
|
-
return Promise.reject(new Error("the harness is not open"));
|
|
222
|
-
}
|
|
223
|
-
return harnessRef.current.inspect(BACKGROUND_CONTEXT);
|
|
224
|
-
},
|
|
225
|
-
caps: () => ({ laneCap: config.laneCap, sessionCeiling: config.sessionCeiling }),
|
|
226
|
-
pollMs: 1000,
|
|
227
|
-
};
|
|
228
|
-
const installed = createDelegatesRegistry({
|
|
229
|
-
admission,
|
|
230
|
-
skills,
|
|
231
|
-
...(deps.sandbox === undefined ? {} : { sandbox: deps.sandbox }),
|
|
232
|
-
mcp,
|
|
233
|
-
log,
|
|
234
|
-
timers,
|
|
235
|
-
mcpPresence: () => runtimeRef.current?.mcpPresence,
|
|
236
|
-
runtime: () => {
|
|
237
|
-
if (runtimeRef.current === undefined) {
|
|
238
|
-
throw new Error("The delegation runtime is not open.");
|
|
239
|
-
}
|
|
240
|
-
return runtimeRef.current;
|
|
241
|
-
},
|
|
242
|
-
});
|
|
243
|
-
registryRef.current = installed;
|
|
244
|
-
const openHarness = async () => {
|
|
245
|
-
let harness: Harness | undefined;
|
|
246
|
-
try {
|
|
247
|
-
harness = await Harness.open(
|
|
248
|
-
storage,
|
|
249
|
-
{
|
|
250
|
-
models: deps.models,
|
|
251
|
-
registry: installed.registry,
|
|
252
|
-
env: conversationEnv(deps.identity.cwd),
|
|
253
|
-
now,
|
|
254
|
-
setTimer: setTimerFrom(timers),
|
|
255
|
-
onReport: (report) => {
|
|
256
|
-
deps.onReport?.(report);
|
|
257
|
-
logReport(log, report);
|
|
258
|
-
if (report.kind === "storage_failed") {
|
|
259
|
-
setState({ kind: "failing", reason: reasonOf(report.error) });
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
|
-
},
|
|
263
|
-
BACKGROUND_CONTEXT,
|
|
264
|
-
);
|
|
265
|
-
const root = await harness.root(BACKGROUND_CONTEXT, {
|
|
266
|
-
agent: { extensions: [], tools: [] },
|
|
267
|
-
});
|
|
268
|
-
return { harness, root };
|
|
269
|
-
} catch (error) {
|
|
270
|
-
await (
|
|
271
|
-
harness === undefined
|
|
272
|
-
? storage.close(BACKGROUND_CONTEXT)
|
|
273
|
-
: harness.close(BACKGROUND_CONTEXT)
|
|
274
|
-
).catch(() => undefined);
|
|
275
|
-
// The registry ran before the harness opened, so its sandbox exists and must stop too.
|
|
276
|
-
await installed.sandbox.close().catch(() => undefined);
|
|
277
|
-
await mcp.close().catch(() => undefined);
|
|
278
|
-
throw error;
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
const { harness, root } = await openHarness();
|
|
282
|
-
harnessRef.current = harness;
|
|
120
|
+
export interface SessionSnapshot {
|
|
121
|
+
<T extends JsonObject>(token: SessionDocToken<T>): Promise<Readonly<T> | undefined>;
|
|
122
|
+
<T extends JsonObject, I extends JsonValue>(
|
|
123
|
+
token: SessionDocFamilyToken<T, I>,
|
|
124
|
+
key: string,
|
|
125
|
+
): Promise<Readonly<T> | undefined>;
|
|
126
|
+
}
|
|
283
127
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Everything the opened harness owns, before the bridge exists. The graph
|
|
130
|
+
* builds it once and hands it to `buildSessionRuntime`; `close` runs the three
|
|
131
|
+
* closes once and is shared with the scope's finalizer.
|
|
132
|
+
*/
|
|
133
|
+
export interface HarnessParts extends RuntimeStateMachine {
|
|
134
|
+
readonly storePath: string;
|
|
135
|
+
readonly sessionId: string;
|
|
136
|
+
readonly harness: Harness;
|
|
137
|
+
readonly root: Conversation;
|
|
138
|
+
readonly calls: HarnessCalls;
|
|
139
|
+
readonly config: DelegatesConfig;
|
|
140
|
+
readonly log: LogSink;
|
|
141
|
+
readonly registry: Registry;
|
|
142
|
+
readonly tasks: DelegatesTasks;
|
|
143
|
+
readonly catalogue: DefinitionCatalogue;
|
|
144
|
+
readonly host: () => HostDefaults;
|
|
145
|
+
readonly sandbox: CodemodeSandboxBase;
|
|
146
|
+
readonly skills: SkillsCatalogue["Service"];
|
|
147
|
+
readonly mcp: McpHost;
|
|
148
|
+
readonly mcpPresence: McpPresence;
|
|
149
|
+
readonly now: () => number;
|
|
150
|
+
readonly timers: Timers;
|
|
151
|
+
readonly settle: SettleSignal;
|
|
152
|
+
readonly snapshot: SessionSnapshot;
|
|
153
|
+
readonly close: () => Promise<void>;
|
|
154
|
+
readonly holder: RuntimeHolder;
|
|
155
|
+
}
|
|
293
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Assembles the public runtime from the opened session and the bridge. It
|
|
159
|
+
* receives both, so it cannot create a scope and owns no close of its own:
|
|
160
|
+
* `close` arrives from the graph's finalizer.
|
|
161
|
+
*/
|
|
162
|
+
export const buildSessionRuntime = async (
|
|
163
|
+
core: HarnessParts,
|
|
164
|
+
bridge: DurableBridge["Service"],
|
|
165
|
+
): Promise<SessionRuntime> => {
|
|
294
166
|
const run = <A>(operation: (context: Context) => Promise<A>, signal?: AbortSignal): Promise<A> =>
|
|
295
167
|
Effect.runPromise(bridge.call(operation), signal === undefined ? undefined : { signal });
|
|
296
168
|
|
|
297
|
-
setState({ kind: "ready" });
|
|
298
|
-
harness.resume();
|
|
169
|
+
core.setState({ kind: "ready" });
|
|
170
|
+
core.harness.resume();
|
|
299
171
|
|
|
300
|
-
void harness.closed.then((end) => {
|
|
172
|
+
void core.harness.closed.then((end) => {
|
|
301
173
|
const reason = end.kind === "failed" ? reasonOf(end.error) : undefined;
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
174
|
+
const failing = core.state();
|
|
175
|
+
if (failing.kind !== "failing")
|
|
176
|
+
core.setState({ kind: "closed", ...(reason === undefined ? {} : { reason }) });
|
|
177
|
+
else core.setState({ kind: "closed", reason: failing.reason });
|
|
305
178
|
});
|
|
306
179
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
};
|
|
319
|
-
|
|
180
|
+
// The runtime is the core minus its owner-only members, plus the bridge and the
|
|
181
|
+
// getter. The spread keeps the two shapes in step; a field added to the core that the
|
|
182
|
+
// runtime exposes needs no second line here.
|
|
183
|
+
const {
|
|
184
|
+
calls: _calls,
|
|
185
|
+
setState: _setState,
|
|
186
|
+
close: _close,
|
|
187
|
+
holder: _holder,
|
|
188
|
+
state,
|
|
189
|
+
...shared
|
|
190
|
+
} = core;
|
|
320
191
|
const runtime: SessionRuntime = {
|
|
321
|
-
|
|
322
|
-
sessionId: deps.identity.sessionId,
|
|
323
|
-
harness,
|
|
324
|
-
root,
|
|
192
|
+
...shared,
|
|
325
193
|
bridge,
|
|
326
194
|
run,
|
|
327
|
-
config,
|
|
328
|
-
log,
|
|
329
|
-
registry: installed.registry,
|
|
330
|
-
tasks: installed.tasks,
|
|
331
|
-
catalogue,
|
|
332
|
-
host,
|
|
333
|
-
sandbox: installed.sandbox,
|
|
334
|
-
skills,
|
|
335
|
-
mcp,
|
|
336
|
-
mcpPresence: mcp.presence,
|
|
337
|
-
now,
|
|
338
|
-
timers,
|
|
339
195
|
get state() {
|
|
340
|
-
return state;
|
|
341
|
-
},
|
|
342
|
-
onState: (listener) => {
|
|
343
|
-
listeners.add(listener);
|
|
344
|
-
return () => {
|
|
345
|
-
listeners.delete(listener);
|
|
346
|
-
};
|
|
347
|
-
},
|
|
348
|
-
snapshot,
|
|
349
|
-
close: async () => {
|
|
350
|
-
await harness.close(BACKGROUND_CONTEXT);
|
|
351
|
-
await mcp.close();
|
|
352
|
-
await installed.sandbox.close();
|
|
196
|
+
return state();
|
|
353
197
|
},
|
|
354
198
|
};
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
if (pruned > 0) log.write("info", "requests.pruned", { retired: pruned });
|
|
359
|
-
} catch (error) {
|
|
360
|
-
// A failed sweep must not stop the session; the next start retries it.
|
|
361
|
-
log.write("warn", "requests.prune_failed", errorFields(error));
|
|
362
|
-
}
|
|
199
|
+
core.holder.runtime = runtime;
|
|
200
|
+
// The start-up sweep never fails the session; see `sweepRequests`.
|
|
201
|
+
await sweepRequests(runtime);
|
|
363
202
|
return runtime;
|
|
364
|
-
}
|
|
365
|
-
|
|
203
|
+
};
|
|
366
204
|
/**
|
|
367
205
|
* The read-only viewer path: it opens an existing store, exposes only reads and
|
|
368
206
|
* runs no task. The registry gets no session runtime, so it installs no run
|
|
@@ -377,7 +215,7 @@ export async function openReadOnlyRuntime(options: {
|
|
|
377
215
|
readonly sandbox?: CodemodeSandboxBase;
|
|
378
216
|
}): Promise<ReadOnlyRuntime> {
|
|
379
217
|
const storage = await openNodeSqliteStorage(options.storePath);
|
|
380
|
-
const skills =
|
|
218
|
+
const skills = SkillsCatalogue.make();
|
|
381
219
|
const installed = createDelegatesRegistry({
|
|
382
220
|
admission: {
|
|
383
221
|
inspect: () => Promise.reject(new Error("a read-only viewer runs no task")),
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Storage } from "@earendil-works/pi-durable";
|
|
2
|
+
import { Context, Effect, Layer, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
/** The session's durable store: one per session, opened and closed by the seam layer. */
|
|
5
|
+
export class StorageService extends Context.Service<StorageService, Storage>()(
|
|
6
|
+
"@ftrdotdev/pi-delegates/runtime/storage/StorageService",
|
|
7
|
+
) {
|
|
8
|
+
/**
|
|
9
|
+
* The store, opened once per build; a failed open fails the build here.
|
|
10
|
+
* `Harness.close` owns the backend once the harness holds it, so this layer
|
|
11
|
+
* adds no finalizer: the only other close is the graph's own failure path,
|
|
12
|
+
* which releases a store the harness never received.
|
|
13
|
+
*/
|
|
14
|
+
static layer(open: () => Promise<Storage>): Layer.Layer<StorageService, StorageError> {
|
|
15
|
+
return Layer.effect(
|
|
16
|
+
StorageService,
|
|
17
|
+
Effect.tryPromise({
|
|
18
|
+
try: open,
|
|
19
|
+
catch: (cause) => StorageError.make({ cause }),
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The store could not be opened; the layer refuses the build rather than expose no store. */
|
|
26
|
+
export class StorageError extends Schema.TaggedError<
|
|
27
|
+
StorageError,
|
|
28
|
+
{ readonly brand: unique symbol }
|
|
29
|
+
>()("StorageError", { cause: Schema.Defect() }) {}
|