@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,459 @@
1
+ import type { Context } from "@earendil-works/chord/context";
2
+ import type { JsonValue } from "@earendil-works/chord/json";
3
+ import type {
4
+ Harness,
5
+ JsonObject,
6
+ Registry,
7
+ SessionDocFamilyToken,
8
+ SessionDocToken,
9
+ } from "@earendil-works/pi-durable";
10
+ import { AgentDoc, type TaskRecord } from "@earendil-works/pi-durable";
11
+ import { Schema } from "effect";
12
+ import {
13
+ answerText,
14
+ resolverFor,
15
+ RunMode,
16
+ RunDoc,
17
+ RunIndexDoc,
18
+ runResultOf,
19
+ type RunResult,
20
+ } from "./run-records.ts";
21
+ import { resolveStoredToolkit, ToolkitDoc, type ToolkitResolver } from "./toolkit.ts";
22
+ import { GateDecision, GateRecord, pendingGates } from "./orchestration/gates.ts";
23
+ import {
24
+ pendingRequests,
25
+ requestRecords,
26
+ RequestDecided,
27
+ RequestRecord,
28
+ } from "./orchestration/requests.ts";
29
+ import { ReviewDoc, ReviewRecord } from "./orchestration/review.ts";
30
+ import type { SkillsService } from "./skills.ts";
31
+ import type { McpPresence } from "../runtime/mcp.ts";
32
+ import { conversationId as toConversationId, taskId as toTaskId } from "./ids.ts";
33
+
34
+ /**
35
+ * Everything a listing reads. A live session and the read-only viewer both
36
+ * satisfy it, so one listing path serves both.
37
+ */
38
+ export interface ViewPort {
39
+ readonly harness: Harness;
40
+ readonly registry: Registry;
41
+ readonly skills: SkillsService;
42
+ /** The live runtime's MCP presence; absent in a viewer, which connects to nothing. */
43
+ readonly mcpPresence?: McpPresence;
44
+ /** The clock a derived deadline reads; a viewer without one falls back to the wall clock. */
45
+ readonly now?: () => number;
46
+ snapshot<T extends JsonObject>(token: SessionDocToken<T>): Promise<Readonly<T> | undefined>;
47
+ snapshot<T extends JsonObject, I extends JsonValue>(
48
+ token: SessionDocFamilyToken<T, I>,
49
+ key: string,
50
+ ): Promise<Readonly<T> | undefined>;
51
+ readonly run: <A>(
52
+ operation: (context: Context) => Promise<A>,
53
+ signal?: AbortSignal,
54
+ ) => Promise<A>;
55
+ }
56
+
57
+ export const RunStatus = Schema.Literals([
58
+ "queued",
59
+ "running",
60
+ "waiting",
61
+ "answered",
62
+ "failed",
63
+ "stopped",
64
+ "denied",
65
+ ]);
66
+ export type RunStatus = typeof RunStatus.Type;
67
+
68
+ export const ChildView = Schema.Struct({
69
+ child: Schema.Finite,
70
+ /** The child run id, so control can address one child of a fanout. */
71
+ runId: Schema.String,
72
+ /** The child's own conversation, for a per-child activity watch. */
73
+ conversationId: Schema.NullOr(Schema.Finite),
74
+ /** The child's task, for a task-document observation that pushes status. */
75
+ taskId: Schema.NullOr(Schema.Finite),
76
+ agent: Schema.String,
77
+ status: RunStatus,
78
+ answer: Schema.NullOr(Schema.String),
79
+ /** `endedAt - startedAt` once terminal; null while live or without timestamps. */
80
+ durationMs: Schema.NullOr(Schema.Finite),
81
+ });
82
+ export type ChildView = typeof ChildView.Type;
83
+
84
+ export const LaneView = Schema.Struct({
85
+ name: Schema.String,
86
+ running: Schema.Finite,
87
+ queued: Schema.Finite,
88
+ answered: Schema.Finite,
89
+ });
90
+ export type LaneView = typeof LaneView.Type;
91
+
92
+ export const RunView = Schema.Struct({
93
+ runId: Schema.String,
94
+ definition: Schema.String,
95
+ mode: RunMode,
96
+ lane: Schema.String,
97
+ /** The parent's conversation; `null` for a fanout parent or a run that has not started one. */
98
+ conversationId: Schema.NullOr(Schema.Finite),
99
+ /** The run's task, when it has one: the address whose document pushes status transitions. */
100
+ taskId: Schema.NullOr(Schema.Finite),
101
+ status: RunStatus,
102
+ answer: Schema.NullOr(Schema.String),
103
+ /** `endedAt - startedAt` once terminal; null while live or without timestamps. */
104
+ durationMs: Schema.NullOr(Schema.Finite),
105
+ children: Schema.Array(ChildView),
106
+ missing: Schema.Array(Schema.String),
107
+ /** Gate names the run waits on; empty unless `status` is `waiting`. */
108
+ gates: Schema.Array(Schema.String),
109
+ /** Request ids the run waits on, oldest first; empty unless `status` is `waiting`. */
110
+ requests: Schema.Array(Schema.String),
111
+ diagnostic: Schema.NullOr(Schema.String),
112
+ });
113
+ export type RunView = typeof RunView.Type;
114
+
115
+ export const SessionView = Schema.Struct({
116
+ runs: Schema.Array(RunView),
117
+ lanes: Schema.Array(LaneView),
118
+ gates: Schema.Array(GateRecord),
119
+ /** Every request record in the store; a reader derives the state from the deadline. */
120
+ requests: Schema.Array(RequestRecord),
121
+ reviews: Schema.Array(ReviewRecord),
122
+ /** Runs a session view left out; absent means none. */
123
+ omitted: Schema.optional(Schema.Finite),
124
+ });
125
+ export type SessionView = typeof SessionView.Type;
126
+
127
+ const resultOf = (
128
+ task: TaskRecord<unknown, unknown, unknown> | undefined,
129
+ ): RunResult | undefined => {
130
+ if (task?.state.status !== "terminal") return undefined;
131
+ return task.state.outcome.status === "completed" || task.state.outcome.status === "aborted"
132
+ ? runResultOf(task.state.outcome.result)
133
+ : undefined;
134
+ };
135
+
136
+ /** Durable task state → the one status a person reads. */
137
+ export const statusOf = (task: TaskRecord<unknown, unknown, unknown> | undefined): RunStatus => {
138
+ if (task === undefined) return "queued";
139
+ const state = task.state;
140
+ switch (state.status) {
141
+ case "pending":
142
+ return "queued";
143
+ case "running":
144
+ case "completing":
145
+ return "running";
146
+ case "waiting":
147
+ return "waiting";
148
+ case "sleeping": {
149
+ const signals = state.signals ?? [];
150
+ if (signals.length === 0) return "queued";
151
+ return signals.some(
152
+ (signal) => signal.signal === GateDecision.name || signal.signal === RequestDecided.name,
153
+ )
154
+ ? "waiting"
155
+ : "running";
156
+ }
157
+ case "terminal": {
158
+ const outcome = state.outcome;
159
+ if (outcome.status === "aborted") return "stopped";
160
+ if (
161
+ outcome.status === "failed" ||
162
+ outcome.status === "faulted" ||
163
+ outcome.status === "orphaned"
164
+ ) {
165
+ return "failed";
166
+ }
167
+ const result = runResultOf(state.outcome.result);
168
+ return result?.status ?? "failed";
169
+ }
170
+ }
171
+ };
172
+
173
+ export const isTerminalStatus = (status: RunStatus): boolean =>
174
+ status === "answered" || status === "failed" || status === "stopped" || status === "denied";
175
+
176
+ /** `endedAt - startedAt` once the task is terminal; absent timestamps stay null. */
177
+ const durationOf = (task: TaskRecord<unknown, unknown, unknown> | undefined): number | null => {
178
+ if (task?.state.status !== "terminal") return null;
179
+ const { startedAt, endedAt } = task;
180
+ if (typeof startedAt !== "number" || typeof endedAt !== "number") return null;
181
+ return Math.max(0, endedAt - startedAt);
182
+ };
183
+
184
+ const diagnosticOf = (task: TaskRecord<unknown, unknown, unknown> | undefined): string | null => {
185
+ if (task === undefined) return null;
186
+ const state = task.state;
187
+ if (state.status !== "terminal") return null;
188
+ if (state.outcome.status === "failed" || state.outcome.status === "faulted") {
189
+ return state.outcome.error.message;
190
+ }
191
+ if (state.outcome.status === "orphaned") return state.outcome.reason;
192
+ const result = runResultOf(state.outcome.result);
193
+ return result?.status === "failed" || result?.status === "denied"
194
+ ? (result.diagnostic ?? null)
195
+ : null;
196
+ };
197
+
198
+ export const viewOf = (input: {
199
+ readonly runId: string;
200
+ readonly definition: string;
201
+ readonly mode: RunMode;
202
+ readonly lane: string;
203
+ readonly requestedAgent: string;
204
+ readonly conversationId: number | null;
205
+ readonly taskId: number | null;
206
+ readonly task: TaskRecord<unknown, unknown, unknown> | undefined;
207
+ readonly missing: readonly string[];
208
+ readonly children?: readonly ChildView[];
209
+ readonly gates?: readonly string[];
210
+ readonly requests?: readonly string[];
211
+ }): RunView => {
212
+ const recordedStatus = statusOf(input.task);
213
+ const waiting = (input.gates?.length ?? 0) > 0 || (input.requests?.length ?? 0) > 0;
214
+ const status: RunStatus = waiting && recordedStatus === "running" ? "waiting" : recordedStatus;
215
+ const result = resultOf(input.task);
216
+ const answer = result?.status === "answered" ? result.answer : null;
217
+ return {
218
+ runId: input.runId,
219
+ definition: input.definition,
220
+ mode: input.mode,
221
+ lane: input.lane,
222
+ conversationId: input.conversationId,
223
+ taskId: input.taskId,
224
+ status,
225
+ answer,
226
+ durationMs: durationOf(input.task),
227
+ children: input.children ?? [
228
+ {
229
+ child: 0,
230
+ runId: input.runId,
231
+ conversationId: input.conversationId,
232
+ taskId: input.taskId,
233
+ agent: input.requestedAgent || input.definition,
234
+ status,
235
+ answer,
236
+ durationMs: durationOf(input.task),
237
+ },
238
+ ],
239
+ missing: input.missing,
240
+ gates: input.gates ?? [],
241
+ requests: input.requests ?? [],
242
+ diagnostic: diagnosticOf(input.task),
243
+ };
244
+ };
245
+
246
+ /** The declared skill names of a child's toolkit document. */
247
+ export const declaredSkills = async (
248
+ runtime: ViewPort,
249
+ childId: number | null | undefined,
250
+ ): Promise<readonly string[]> => {
251
+ if (childId === null || childId === undefined) return [];
252
+ const toolkit = await runtime.run((context) =>
253
+ runtime.harness.snapshot(ToolkitDoc, toConversationId(childId), context),
254
+ );
255
+ return toolkit?.skills ?? [];
256
+ };
257
+
258
+ const childView = async (
259
+ runtime: ViewPort,
260
+ runId: string,
261
+ index: number,
262
+ gateNames: readonly string[],
263
+ resolver: ToolkitResolver,
264
+ ): Promise<{ readonly child: ChildView; readonly missing: readonly string[] }> => {
265
+ const doc = await runtime.snapshot(RunDoc, runId);
266
+ const taskNumber = doc?.taskId;
267
+ const task =
268
+ taskNumber === null || taskNumber === undefined
269
+ ? undefined
270
+ : await runtime.run((context) => runtime.harness.getTask(toTaskId(taskNumber), context));
271
+ const childNumber = doc?.childConversationId;
272
+ const agent =
273
+ childNumber === null || childNumber === undefined
274
+ ? undefined
275
+ : await runtime.run((context) =>
276
+ runtime.harness.snapshot(AgentDoc, toConversationId(childNumber), context),
277
+ );
278
+ const missing = resolveStoredToolkit(
279
+ agent,
280
+ resolver,
281
+ await declaredSkills(runtime, doc?.childConversationId),
282
+ ).missing;
283
+ const recorded = statusOf(task);
284
+ const status: RunStatus = gateNames.length > 0 && recorded === "running" ? "waiting" : recorded;
285
+ const result = resultOf(task);
286
+ return {
287
+ child: {
288
+ child: index,
289
+ runId,
290
+ conversationId: doc?.childConversationId ?? null,
291
+ taskId: doc?.taskId ?? null,
292
+ agent: doc?.requestedAgent || doc?.definition || "unknown",
293
+ status,
294
+ answer: result?.status === "answered" ? result.answer : null,
295
+ durationMs: durationOf(task),
296
+ },
297
+ missing,
298
+ };
299
+ };
300
+
301
+ /** Gate names by owner run and by each child run, read once per view. */
302
+ export const gateNamesFor = (
303
+ gates: readonly GateRecord[],
304
+ ): ReadonlyMap<string, readonly string[]> => {
305
+ const byRun = new Map<string, string[]>();
306
+ const add = (runId: string, gate: string): void => {
307
+ const names = byRun.get(runId) ?? [];
308
+ if (!names.includes(gate)) names.push(gate);
309
+ byRun.set(runId, names);
310
+ };
311
+ for (const gate of gates) {
312
+ add(gate.runId, gate.gate);
313
+ for (const childRunId of gate.childRunIds) add(childRunId, gate.gate);
314
+ }
315
+ return byRun;
316
+ };
317
+
318
+ export interface ViewShared {
319
+ readonly gateNamesByRun: ReadonlyMap<string, readonly string[]>;
320
+ readonly resolver: ToolkitResolver;
321
+ }
322
+
323
+ export const viewRun = async (
324
+ runId: string,
325
+ runtime: ViewPort,
326
+ shared?: ViewShared,
327
+ ): Promise<RunView> => {
328
+ const doc = await runtime.snapshot(RunDoc, runId);
329
+ if (doc === undefined) throw new Error(`Run ${runId} not found.`);
330
+ const taskNumber = doc.taskId;
331
+ const task =
332
+ taskNumber === null
333
+ ? undefined
334
+ : await runtime.run((context) => runtime.harness.getTask(toTaskId(taskNumber), context));
335
+ const gateRunId = doc.gateRunId === undefined || doc.gateRunId === "" ? runId : doc.gateRunId;
336
+ const sharedData: ViewShared = shared ?? {
337
+ gateNamesByRun: gateNamesFor(await pendingGates(runtime, gateRunId)),
338
+ resolver: resolverFor({
339
+ registry: runtime.registry.snapshot(),
340
+ skills: runtime.skills,
341
+ ...(runtime.mcpPresence === undefined ? {} : { presence: runtime.mcpPresence }),
342
+ }),
343
+ };
344
+ const gates = sharedData.gateNamesByRun.get(gateRunId) ?? [];
345
+ const requests = (await pendingRequests(runtime, gateRunId)).map((record) => record.requestId);
346
+
347
+ if (doc.childRunIds.length > 0) {
348
+ const children: ChildView[] = [];
349
+ const missing: string[] = [];
350
+ for (const [index, childRunId] of doc.childRunIds.entries()) {
351
+ const child = await childView(
352
+ runtime,
353
+ childRunId,
354
+ index,
355
+ sharedData.gateNamesByRun.get(childRunId) ?? [],
356
+ sharedData.resolver,
357
+ );
358
+ children.push(child.child);
359
+ missing.push(...child.missing);
360
+ }
361
+ return viewOf({
362
+ runId,
363
+ definition: doc.definition,
364
+ mode: doc.mode,
365
+ lane: doc.lane,
366
+ requestedAgent: doc.requestedAgent,
367
+ conversationId: doc.childConversationId,
368
+ taskId: doc.taskId,
369
+ task,
370
+ missing,
371
+ children,
372
+ gates,
373
+ requests,
374
+ });
375
+ }
376
+
377
+ const childNumber = doc.childConversationId;
378
+ const agent =
379
+ childNumber === null
380
+ ? undefined
381
+ : await runtime.run((context) =>
382
+ runtime.harness.snapshot(AgentDoc, toConversationId(childNumber), context),
383
+ );
384
+ const missing = resolveStoredToolkit(
385
+ agent,
386
+ sharedData.resolver,
387
+ await declaredSkills(runtime, doc.childConversationId),
388
+ ).missing;
389
+ return viewOf({
390
+ runId,
391
+ definition: doc.definition,
392
+ mode: doc.mode,
393
+ lane: doc.lane,
394
+ requestedAgent: doc.requestedAgent,
395
+ conversationId: doc.childConversationId,
396
+ taskId: doc.taskId,
397
+ task,
398
+ missing,
399
+ gates,
400
+ requests,
401
+ });
402
+ };
403
+
404
+ export const laneViews = (runs: readonly RunView[]): readonly LaneView[] => {
405
+ const lanes = new Map<string, { running: number; queued: number; answered: number }>();
406
+ for (const run of runs) {
407
+ const lane = lanes.get(run.lane) ?? { running: 0, queued: 0, answered: 0 };
408
+ // A fanout reports one lane row per child; a single run has one synthetic row.
409
+ const rows = run.children.length > 0 ? run.children : [{ status: run.status }];
410
+ for (const row of rows) {
411
+ if (row.status === "running" || row.status === "waiting") lane.running += 1;
412
+ else if (row.status === "queued") lane.queued += 1;
413
+ else if (row.status === "answered") lane.answered += 1;
414
+ }
415
+ lanes.set(run.lane, lane);
416
+ }
417
+ return [...lanes].map(([name, counts]) => ({ name, ...counts }));
418
+ };
419
+
420
+ /** A session view lists the newest runs; the count of the rest is `omitted`. */
421
+ export const STATUS_RUN_LIMIT = 50;
422
+
423
+ export const viewSession = async (runtime: ViewPort): Promise<SessionView> => {
424
+ const index = await runtime.snapshot(RunIndexDoc);
425
+ const runIds = index?.runIds ?? [];
426
+ // The index is in creation order, so the tail is the newest.
427
+ const selected = runIds.slice(-STATUS_RUN_LIMIT);
428
+ const omitted = runIds.length - selected.length;
429
+ // The view resolver is one `registry.snapshot()`; `run-records.ts` keeps the live
430
+ // registry for launch-time binding only, where a task needs the phase's own view.
431
+ const resolver = resolverFor({
432
+ registry: runtime.registry.snapshot(),
433
+ skills: runtime.skills,
434
+ ...(runtime.mcpPresence === undefined ? {} : { presence: runtime.mcpPresence }),
435
+ });
436
+ const pending = await pendingGates(runtime);
437
+ const shared: ViewShared = { gateNamesByRun: gateNamesFor(pending), resolver };
438
+ const runs: RunView[] = [];
439
+ const reviews: ReviewRecord[] = [];
440
+ for (const runId of selected) {
441
+ // A scripted child or a scripted group is nested under its parent run, not listed beside it.
442
+ const doc = await runtime.snapshot(RunDoc, runId);
443
+ if (doc === undefined || doc.parentRunId !== null) continue;
444
+ runs.push(await viewRun(runId, runtime, shared));
445
+ const review = await runtime.snapshot(ReviewDoc, runId);
446
+ if (review !== undefined) reviews.push(review);
447
+ }
448
+ return {
449
+ runs,
450
+ lanes: laneViews(runs),
451
+ gates: pending,
452
+ requests: await requestRecords(runtime),
453
+ reviews,
454
+ omitted,
455
+ };
456
+ };
457
+
458
+ /** The text of a child's answer, for callers that read the transcript directly. */
459
+ export { answerText };
@@ -0,0 +1,15 @@
1
+ import { assert, it } from "@effect/vitest";
2
+
3
+ it("the gated example exports a Pi extension factory and its rule", async () => {
4
+ const example = (await import("../examples/gated.ts")) as {
5
+ readonly default?: unknown;
6
+ readonly vaultScout?: {
7
+ readonly approval?: { readonly gates?: readonly unknown[] };
8
+ };
9
+ };
10
+
11
+ assert.strictEqual(typeof example.default, "function");
12
+ assert.deepStrictEqual(example.vaultScout?.approval?.gates, [
13
+ { name: "vault-access", tools: ["bash"], match: "vault" },
14
+ ]);
15
+ }, 30_000);