@ftrdotdev/pi-delegates 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +57 -0
- package/README.md +448 -2
- package/ROADMAP.md +158 -0
- package/TESTING.md +1061 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +93 -4
- package/skills/pi-delegates/SKILL.md +147 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +156 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +161 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1298 -0
- package/src/domain/orchestration/control.ts +436 -0
- package/src/domain/orchestration/fanout.test.ts +419 -0
- package/src/domain/orchestration/fanout.ts +334 -0
- package/src/domain/orchestration/gates.test.ts +686 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +173 -0
- package/src/domain/orchestration/launch.ts +581 -0
- package/src/domain/orchestration/requests.test.ts +700 -0
- package/src/domain/orchestration/requests.ts +769 -0
- package/src/domain/orchestration/review.test.ts +214 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +262 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1425 -0
- package/src/domain/orchestration/workflow.ts +705 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +527 -0
- package/src/domain/shapes.test.ts +165 -0
- package/src/domain/skills.test.ts +466 -0
- package/src/domain/skills.ts +97 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +201 -0
- package/src/domain/toolkit.ts +379 -0
- package/src/domain/view.test.ts +739 -0
- package/src/domain/view.ts +459 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +481 -0
- package/src/extension/commands.ts +227 -0
- package/src/extension/entry.test.ts +1118 -0
- package/src/extension/entry.ts +342 -0
- package/src/extension/gate-alerts.test.ts +235 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1191 -0
- package/src/extension/inspector-command.ts +638 -0
- package/src/extension/prompt.test.ts +110 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/shortcut.test.ts +64 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +993 -0
- package/src/extension/tools.ts +505 -0
- package/src/extension/version.test.ts +123 -0
- package/src/extension/version.ts +97 -0
- package/src/harness-policy.test.ts +74 -0
- package/src/package-manifest.test.ts +157 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +387 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +361 -0
- package/src/presentation/inspector.test.ts +679 -0
- package/src/presentation/inspector.ts +440 -0
- package/src/presentation/overlay.test.ts +555 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +329 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +530 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/strip.test.ts +196 -0
- package/src/presentation/strip.ts +74 -0
- package/src/presentation/wording.test.ts +238 -0
- package/src/presentation/wording.ts +222 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +134 -0
- package/src/runtime/config.test.ts +44 -0
- package/src/runtime/config.ts +87 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +35 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +140 -0
- package/src/runtime/log.test.ts +128 -0
- package/src/runtime/log.ts +68 -0
- package/src/runtime/mcp.test.ts +1453 -0
- package/src/runtime/mcp.ts +938 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/session.test.ts +359 -0
- package/src/runtime/session.ts +425 -0
- package/src/runtime/timers.test.ts +31 -0
- package/src/runtime/timers.ts +10 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import { BACKGROUND_CONTEXT, type Context } from "@earendil-works/chord/context";
|
|
2
|
+
import type { Models } from "@earendil-works/pi-ai/models";
|
|
3
|
+
import type { JsonValue } from "@earendil-works/chord/json";
|
|
4
|
+
import {
|
|
5
|
+
Harness,
|
|
6
|
+
SessionFailed,
|
|
7
|
+
type Conversation,
|
|
8
|
+
type ConversationId,
|
|
9
|
+
type JsonObject,
|
|
10
|
+
type Report,
|
|
11
|
+
type Registry,
|
|
12
|
+
type SessionDocFamilyToken,
|
|
13
|
+
type SessionDocToken,
|
|
14
|
+
type SetTimer,
|
|
15
|
+
type Storage,
|
|
16
|
+
} from "@earendil-works/pi-durable";
|
|
17
|
+
import { openNodeSqliteStorage } from "@earendil-works/pi-durable/storage/sqlite/node";
|
|
18
|
+
import { Effect } from "effect";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
import { DurableBridge, type HarnessCalls } from "./bridge.ts";
|
|
21
|
+
import { operationContext } from "./context.ts";
|
|
22
|
+
import { conversationEnv } from "./execution-env.ts";
|
|
23
|
+
import { readDelegatesConfig, type DelegatesConfig } from "./config.ts";
|
|
24
|
+
import { createDelegatesRegistry, type DelegatesTasks } from "./extensions.ts";
|
|
25
|
+
import { createCatalogue, type DefinitionCatalogue } from "../domain/definitions.ts";
|
|
26
|
+
import type { HostDefaults } from "../domain/orchestration/launch.ts";
|
|
27
|
+
import { refusingSandbox } from "../domain/orchestration/workflow.ts";
|
|
28
|
+
import type { ViewPort } from "../domain/view.ts";
|
|
29
|
+
import type { CodemodeSandboxBase } from "@earendil-works/pi-codemode/portable";
|
|
30
|
+
import { logReport, nullLogSink, errorFields, reasonOf, type LogSink } from "./log.ts";
|
|
31
|
+
import {
|
|
32
|
+
connectServers,
|
|
33
|
+
readMcpConfig,
|
|
34
|
+
referencedServers,
|
|
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";
|
|
48
|
+
|
|
49
|
+
export interface SessionIdentity {
|
|
50
|
+
readonly sessionId: string;
|
|
51
|
+
readonly sessionDir: string;
|
|
52
|
+
readonly cwd: string;
|
|
53
|
+
readonly agentDir: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
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
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface SessionRuntime {
|
|
94
|
+
readonly storePath: string;
|
|
95
|
+
readonly sessionId: string;
|
|
96
|
+
readonly harness: Harness;
|
|
97
|
+
readonly root: Conversation;
|
|
98
|
+
readonly bridge: DurableBridge["Service"];
|
|
99
|
+
/** Every harness call crosses the bridge; domain errors stay outside it. */
|
|
100
|
+
readonly run: <A>(
|
|
101
|
+
operation: (context: Context) => Promise<A>,
|
|
102
|
+
signal?: AbortSignal,
|
|
103
|
+
) => Promise<A>;
|
|
104
|
+
readonly config: DelegatesConfig;
|
|
105
|
+
/** The one log sink a surface writes diagnostics and failures to. */
|
|
106
|
+
readonly log: LogSink;
|
|
107
|
+
readonly registry: Registry;
|
|
108
|
+
readonly tasks: DelegatesTasks;
|
|
109
|
+
/** The registered definitions a workflow resolves at launch. */
|
|
110
|
+
readonly catalogue: DefinitionCatalogue;
|
|
111
|
+
/** The host model a scripted child fills in, read at each launch. */
|
|
112
|
+
readonly host: () => HostDefaults;
|
|
113
|
+
/** The one code mode sandbox; it closes after the harness. */
|
|
114
|
+
readonly sandbox: CodemodeSandboxBase;
|
|
115
|
+
/** The session's skill catalogue; one scan when the runtime opens. */
|
|
116
|
+
readonly skills: SkillsService;
|
|
117
|
+
/** The MCP servers this session connects to; none in a read-only runtime. */
|
|
118
|
+
readonly mcp: McpHost;
|
|
119
|
+
/** Configured servers with no live connection. */
|
|
120
|
+
readonly mcpPresence: McpPresence;
|
|
121
|
+
/** The clock the domain reads, shared with the harness. */
|
|
122
|
+
readonly now: () => number;
|
|
123
|
+
/** The timer source the domain reads, shared with the harness. */
|
|
124
|
+
readonly timers: Timers;
|
|
125
|
+
readonly state: RuntimeState;
|
|
126
|
+
readonly onState: (listener: (state: RuntimeState) => void) => () => void;
|
|
127
|
+
snapshot<T extends JsonObject>(token: SessionDocToken<T>): Promise<Readonly<T> | undefined>;
|
|
128
|
+
snapshot<T extends JsonObject, I extends JsonValue>(
|
|
129
|
+
token: SessionDocFamilyToken<T, I>,
|
|
130
|
+
key: string,
|
|
131
|
+
): Promise<Readonly<T> | undefined>;
|
|
132
|
+
close(): Promise<void>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export type ReadOnlyRuntime = ViewPort & { readonly close: () => Promise<void> };
|
|
136
|
+
|
|
137
|
+
/** A family token's definition carries `family: true`; a singleton token's does not. */
|
|
138
|
+
const isFamilyToken = <T extends JsonObject, I extends JsonValue>(
|
|
139
|
+
token: SessionDocToken<T> | SessionDocFamilyToken<T, I>,
|
|
140
|
+
): token is SessionDocFamilyToken<T, I> => "family" in token.definition;
|
|
141
|
+
|
|
142
|
+
export const storePathFor = (identity: SessionIdentity): string =>
|
|
143
|
+
join(identity.sessionDir, `delegates-${identity.sessionId}.sqlite`);
|
|
144
|
+
|
|
145
|
+
/** The harness's timer seam, armed by the runtime's one timer source. */
|
|
146
|
+
const setTimerFrom =
|
|
147
|
+
(timers: Timers): SetTimer =>
|
|
148
|
+
(callback, delayMs) => {
|
|
149
|
+
const handle = timers.setTimeout(callback, delayMs);
|
|
150
|
+
return () => timers.clearTimeout(handle);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Derives the store path, reads `delegates.json`, installs the registry, opens
|
|
155
|
+
* the harness, creates the empty root conversation, resumes interrupted work
|
|
156
|
+
* and closes on failure.
|
|
157
|
+
*/
|
|
158
|
+
export async function openSessionRuntime(deps: SessionRuntimeDeps): Promise<SessionRuntime> {
|
|
159
|
+
const storePath = storePathFor(deps.identity);
|
|
160
|
+
const config = deps.config ?? readDelegatesConfig(deps.identity.agentDir);
|
|
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
|
+
};
|
|
184
|
+
|
|
185
|
+
const registryRef: {
|
|
186
|
+
current?: { readonly registry: Registry; readonly tasks: DelegatesTasks };
|
|
187
|
+
} = {};
|
|
188
|
+
const harnessRef: { current?: Harness } = {};
|
|
189
|
+
const runtimeRef: { current?: SessionRuntime } = {};
|
|
190
|
+
const catalogue = deps.catalogue ?? createCatalogue([]);
|
|
191
|
+
const mcpConfig = readMcpConfig({
|
|
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;
|
|
283
|
+
|
|
284
|
+
const calls: HarnessCalls = {
|
|
285
|
+
call: (operation, signal) => {
|
|
286
|
+
if (state.kind === "failing" || state.kind === "closed") {
|
|
287
|
+
return Promise.reject(new SessionFailed(new Error(state.reason ?? "the harness closed")));
|
|
288
|
+
}
|
|
289
|
+
return operation(operationContext(signal));
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
const bridge = Effect.runSync(DurableBridge.pipe(Effect.provide(DurableBridge.layer(calls))));
|
|
293
|
+
|
|
294
|
+
const run = <A>(operation: (context: Context) => Promise<A>, signal?: AbortSignal): Promise<A> =>
|
|
295
|
+
Effect.runPromise(bridge.call(operation), signal === undefined ? undefined : { signal });
|
|
296
|
+
|
|
297
|
+
setState({ kind: "ready" });
|
|
298
|
+
harness.resume();
|
|
299
|
+
|
|
300
|
+
void harness.closed.then((end) => {
|
|
301
|
+
const reason = end.kind === "failed" ? reasonOf(end.error) : undefined;
|
|
302
|
+
if (state.kind !== "failing")
|
|
303
|
+
setState({ kind: "closed", ...(reason === undefined ? {} : { reason }) });
|
|
304
|
+
else setState({ kind: "closed", reason: state.reason });
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const snapshot = <T extends JsonObject, I extends JsonValue>(
|
|
308
|
+
token: SessionDocToken<T> | SessionDocFamilyToken<T, I>,
|
|
309
|
+
key?: string,
|
|
310
|
+
): Promise<Readonly<T> | undefined> => {
|
|
311
|
+
if (isFamilyToken(token)) {
|
|
312
|
+
if (key === undefined) {
|
|
313
|
+
return Promise.reject(new Error("A family document token needs a key."));
|
|
314
|
+
}
|
|
315
|
+
return run((context) => harness.snapshot(token, key, context));
|
|
316
|
+
}
|
|
317
|
+
return run((context) => harness.snapshot(token, context));
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const runtime: SessionRuntime = {
|
|
321
|
+
storePath,
|
|
322
|
+
sessionId: deps.identity.sessionId,
|
|
323
|
+
harness,
|
|
324
|
+
root,
|
|
325
|
+
bridge,
|
|
326
|
+
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
|
+
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();
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
runtimeRef.current = runtime;
|
|
356
|
+
try {
|
|
357
|
+
const pruned = await pruneRequests(runtime);
|
|
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
|
+
}
|
|
363
|
+
return runtime;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The read-only viewer path: it opens an existing store, exposes only reads and
|
|
368
|
+
* runs no task. The registry gets no session runtime, so it installs no run
|
|
369
|
+
* tools: a viewer can read runs and cannot start or control one. The refused
|
|
370
|
+
* `/delegates` reader opens it; `session.test.ts` proves reads succeed and
|
|
371
|
+
* writes are refused.
|
|
372
|
+
*/
|
|
373
|
+
export async function openReadOnlyRuntime(options: {
|
|
374
|
+
readonly storePath: string;
|
|
375
|
+
readonly models: Models;
|
|
376
|
+
/** Overrides the refusing sandbox in tests. */
|
|
377
|
+
readonly sandbox?: CodemodeSandboxBase;
|
|
378
|
+
}): Promise<ReadOnlyRuntime> {
|
|
379
|
+
const storage = await openNodeSqliteStorage(options.storePath);
|
|
380
|
+
const skills = emptySkills();
|
|
381
|
+
const installed = createDelegatesRegistry({
|
|
382
|
+
admission: {
|
|
383
|
+
inspect: () => Promise.reject(new Error("a read-only viewer runs no task")),
|
|
384
|
+
caps: () => ({ laneCap: 0, sessionCeiling: 0 }),
|
|
385
|
+
pollMs: 1000,
|
|
386
|
+
},
|
|
387
|
+
skills,
|
|
388
|
+
sandbox: options.sandbox ?? refusingSandbox(),
|
|
389
|
+
});
|
|
390
|
+
const harness = await Harness.open(
|
|
391
|
+
storage,
|
|
392
|
+
{
|
|
393
|
+
models: options.models,
|
|
394
|
+
registry: installed.registry,
|
|
395
|
+
readOnly: true,
|
|
396
|
+
},
|
|
397
|
+
BACKGROUND_CONTEXT,
|
|
398
|
+
);
|
|
399
|
+
const snapshot = <T extends JsonObject, I extends JsonValue>(
|
|
400
|
+
token: SessionDocToken<T> | SessionDocFamilyToken<T, I>,
|
|
401
|
+
key?: string,
|
|
402
|
+
): Promise<Readonly<T> | undefined> => {
|
|
403
|
+
if (isFamilyToken(token)) {
|
|
404
|
+
if (key === undefined) {
|
|
405
|
+
return Promise.reject(new Error("A family document token needs a key."));
|
|
406
|
+
}
|
|
407
|
+
return harness.snapshot(token, key, BACKGROUND_CONTEXT);
|
|
408
|
+
}
|
|
409
|
+
return harness.snapshot(token, BACKGROUND_CONTEXT);
|
|
410
|
+
};
|
|
411
|
+
return {
|
|
412
|
+
harness,
|
|
413
|
+
registry: installed.registry,
|
|
414
|
+
skills,
|
|
415
|
+
snapshot,
|
|
416
|
+
run: (operation) => operation(BACKGROUND_CONTEXT),
|
|
417
|
+
close: async () => {
|
|
418
|
+
await harness.close(BACKGROUND_CONTEXT);
|
|
419
|
+
await installed.sandbox.close();
|
|
420
|
+
},
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** A conversation id as the harness records it. */
|
|
425
|
+
export type { ConversationId };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { afterEach, assert, beforeEach, it, vi } from "@effect/vitest";
|
|
2
|
+
import { systemTimers } from "./timers.ts";
|
|
3
|
+
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
vi.useFakeTimers();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
vi.useRealTimers();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("systemTimers runs the callback at the delay, not before", () => {
|
|
13
|
+
let fired = 0;
|
|
14
|
+
systemTimers.setTimeout(() => {
|
|
15
|
+
fired += 1;
|
|
16
|
+
}, 100);
|
|
17
|
+
vi.advanceTimersByTime(99);
|
|
18
|
+
assert.strictEqual(fired, 0);
|
|
19
|
+
vi.advanceTimersByTime(1);
|
|
20
|
+
assert.strictEqual(fired, 1);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("clearTimeout cancels the armed callback", () => {
|
|
24
|
+
let fired = 0;
|
|
25
|
+
const handle = systemTimers.setTimeout(() => {
|
|
26
|
+
fired += 1;
|
|
27
|
+
}, 50);
|
|
28
|
+
systemTimers.clearTimeout(handle);
|
|
29
|
+
vi.advanceTimersByTime(1_000);
|
|
30
|
+
assert.strictEqual(fired, 0);
|
|
31
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Injectable timers, so orchestration tests never sleep on wall-clock time. */
|
|
2
|
+
export interface Timers {
|
|
3
|
+
setTimeout(callback: () => void, ms: number): unknown;
|
|
4
|
+
clearTimeout(handle: unknown): void;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const systemTimers: Timers = {
|
|
8
|
+
setTimeout: (callback, ms) => setTimeout(callback, ms),
|
|
9
|
+
clearTimeout: (handle) => clearTimeout(handle as ReturnType<typeof setTimeout>),
|
|
10
|
+
};
|