@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,229 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { Key, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
3
+ import {
4
+ makeBorderedBox,
5
+ makeRowList,
6
+ type RowList,
7
+ type SelectableRow,
8
+ type VimRouter,
9
+ } from "@ftrdotdev/pi-tui";
10
+ import { isTerminalStatus } from "../domain/view.ts";
11
+ import type { InspectorRow, InspectorSnapshot } from "./snapshot.ts";
12
+ import {
13
+ GATES_HEADER,
14
+ OVERLAY_FOOTER,
15
+ REQUESTS_HEADER,
16
+ RUNS_HEADER,
17
+ formatElapsed,
18
+ gateDetailLine,
19
+ gateRowLine,
20
+ laneLine,
21
+ omittedLine,
22
+ requestDetailLines,
23
+ requestRowLine,
24
+ rowLine,
25
+ } from "./wording.ts";
26
+
27
+ /** Pinned geometry: the same shape the tui settings overlay ships, so the house has one floating box. */
28
+ export const OVERLAY_OPTIONS = {
29
+ width: "60%",
30
+ minWidth: 44,
31
+ maxHeight: "80%",
32
+ anchor: "center",
33
+ margin: 1,
34
+ } as const;
35
+
36
+ /** Row lines the overlay may paint; the list scrolls to keep the cursor inside. */
37
+ export const OVERLAY_MAX_LINES = 12;
38
+
39
+ export type OverlayVerb = "stop" | "approve" | "deny";
40
+
41
+ export interface DelegatesOverlayOptions {
42
+ readonly getSnapshot: () => InspectorSnapshot;
43
+ readonly theme: Theme;
44
+ /** Optional; the router self-gates on the shared vim toggle. */
45
+ readonly router?: VimRouter["Service"];
46
+ readonly requestRender: () => void;
47
+ /** Called by the host when a row verb is dispatched; the write lives in `actions.ts`. */
48
+ readonly onAction?: (rowId: string, verb: OverlayVerb) => void;
49
+ readonly onClose: () => void;
50
+ /** Registered with the activation so teardown can close the overlay without the host's help. */
51
+ readonly registerClose?: (close: () => void) => () => void;
52
+ readonly statusLine?: () => string | null;
53
+ }
54
+
55
+ /**
56
+ * Rows are `child:<runId>` / `run:<runId>` for runs and `gate:<runId>:<gate>`
57
+ * for gates, oldest gate first. A detail block is emitted under the expanded
58
+ * row so the list's cursor and cache need no new concept.
59
+ */
60
+ export const detailLines = (row: InspectorRow): string[] => [
61
+ `definition ${row.label} · lane ${row.lane} · status ${row.status}`,
62
+ `conversation ${row.conversationId ?? "none"} · gates ${row.gates.join(", ") || "none"}`,
63
+ `diagnostic ${row.diagnostic ?? "none"} · ${
64
+ row.durationMs !== null
65
+ ? `took ${formatElapsed(row.durationMs)}`
66
+ : isTerminalStatus(row.status)
67
+ ? "took unknown"
68
+ : `age ${row.idleMs === null ? "none" : formatElapsed(row.idleMs)}`
69
+ }`,
70
+ ];
71
+
72
+ export const rowsOf = (
73
+ snapshot: InspectorSnapshot,
74
+ expandedId?: string,
75
+ openParents: ReadonlySet<string> = new Set(),
76
+ ): readonly SelectableRow[] => [
77
+ ...snapshot.rows
78
+ .filter((row) => row.parentId === null || openParents.has(row.parentId))
79
+ .map((row) => ({
80
+ id: row.id,
81
+ header: RUNS_HEADER,
82
+ lines: [rowLine(row), ...(row.id === expandedId ? detailLines(row) : [])],
83
+ ...(row.parentId === null ? {} : { indent: 1, follows: row.parentId }),
84
+ })),
85
+ ...[...snapshot.gates]
86
+ .sort((left, right) => right.idleMs - left.idleMs)
87
+ .map((gate) => ({
88
+ id: gate.id,
89
+ header: GATES_HEADER,
90
+ lines: [gateRowLine(gate), ...(gate.id === expandedId ? [gateDetailLine(gate)] : [])],
91
+ })),
92
+ ...snapshot.requests.map((request) => ({
93
+ id: request.id,
94
+ header: REQUESTS_HEADER,
95
+ lines: [
96
+ requestRowLine(request),
97
+ ...(request.id === expandedId ? requestDetailLines(request) : []),
98
+ ],
99
+ })),
100
+ ];
101
+
102
+ /**
103
+ * The overlay body: the row list, one lane line per lane, the omitted line,
104
+ * the last action's status line, then the pinned footer. Framed by the house
105
+ * box with the `Delegates` label, so the floating surface is one box.
106
+ */
107
+ export const makeDelegatesOverlay = (
108
+ options: DelegatesOverlayOptions,
109
+ ): {
110
+ readonly render: (width: number) => readonly string[];
111
+ readonly invalidate: () => void;
112
+ readonly handleInput: (data: string) => void;
113
+ readonly selectedId: () => string | undefined;
114
+ } => {
115
+ let unregisterClose: (() => void) | undefined;
116
+ let closed = false;
117
+ let expandedId: string | undefined;
118
+ const openParents = new Set<string>();
119
+ const snapshot = (): InspectorSnapshot => options.getSnapshot();
120
+
121
+ const list: RowList = makeRowList({
122
+ rows: () => rowsOf(snapshot(), expandedId, openParents),
123
+ theme: options.theme,
124
+ ...(options.router === undefined ? {} : { router: options.router }),
125
+ maxLines: OVERLAY_MAX_LINES,
126
+ requestRender: options.requestRender,
127
+ onActivate: (id) => toggleExpanded(id),
128
+ });
129
+
130
+ const close = (): void => {
131
+ if (closed) return;
132
+ closed = true;
133
+ unregisterClose?.();
134
+ unregisterClose = undefined;
135
+ options.onClose();
136
+ };
137
+ unregisterClose = options.registerClose?.(close);
138
+
139
+ const content = {
140
+ render: (width: number): string[] => {
141
+ const current = snapshot();
142
+ if (
143
+ expandedId !== undefined &&
144
+ !current.rows.some((row) => row.id === expandedId) &&
145
+ !current.gates.some((gate) => gate.id === expandedId) &&
146
+ !current.requests.some((request) => request.id === expandedId)
147
+ ) {
148
+ expandedId = undefined;
149
+ list.invalidate();
150
+ }
151
+ for (const id of openParents) {
152
+ if (!current.rows.some((row) => row.id === id && row.childCount > 0)) {
153
+ openParents.delete(id);
154
+ list.invalidate();
155
+ }
156
+ }
157
+ const lines = [...list.render(width)];
158
+ for (const lane of current.lanes) lines.push(truncateToWidth(laneLine(lane), width));
159
+ if (current.omitted > 0) lines.push(truncateToWidth(omittedLine(current.omitted), width));
160
+ const status = options.statusLine?.();
161
+ if (status !== null && status !== undefined) lines.push(truncateToWidth(status, width));
162
+ lines.push(truncateToWidth(OVERLAY_FOOTER, width));
163
+ return lines;
164
+ },
165
+ invalidate: (): void => list.invalidate(),
166
+ };
167
+
168
+ const box = makeBorderedBox(content, options.theme, {
169
+ label: "Delegates",
170
+ color: "border",
171
+ cache: false,
172
+ });
173
+
174
+ const activate = (verb: OverlayVerb): void => {
175
+ const id = list.selectedId();
176
+ if (id !== undefined) options.onAction?.(id, verb);
177
+ };
178
+
179
+ const toggleExpanded = (id: string): void => {
180
+ const row = snapshot().rows.find((candidate) => candidate.id === id);
181
+ if (row !== undefined && row.childCount > 0) {
182
+ if (openParents.has(id)) openParents.delete(id);
183
+ else openParents.add(id);
184
+ list.invalidate();
185
+ options.requestRender();
186
+ return;
187
+ }
188
+ expandedId = expandedId === id ? undefined : id;
189
+ list.invalidate();
190
+ options.requestRender();
191
+ };
192
+
193
+ const handleInput = (data: string): void => {
194
+ if (matchesKey(data, Key.escape)) {
195
+ close();
196
+ return;
197
+ }
198
+ if (matchesKey(data, Key.up) || matchesKey(data, Key.down) || matchesKey(data, Key.enter)) {
199
+ list.handleInput(data);
200
+ return;
201
+ }
202
+ const intent = options.router?.decodeNavigation(data);
203
+ if (intent?.kind === "move" && (intent.dir === "up" || intent.dir === "down")) {
204
+ list.handleInput(data);
205
+ return;
206
+ }
207
+ if (matchesKey(data, "i")) {
208
+ const id = list.selectedId();
209
+ if (id !== undefined) toggleExpanded(id);
210
+ return;
211
+ }
212
+ if (matchesKey(data, "s")) {
213
+ activate("stop");
214
+ return;
215
+ }
216
+ if (matchesKey(data, "a")) {
217
+ activate("approve");
218
+ return;
219
+ }
220
+ if (matchesKey(data, "d")) activate("deny");
221
+ };
222
+
223
+ return {
224
+ render: (width) => box.render(width),
225
+ invalidate: () => box.invalidate(),
226
+ handleInput,
227
+ selectedId: () => list.selectedId(),
228
+ };
229
+ };
@@ -0,0 +1,329 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { MemoryStorage } from "@earendil-works/pi-durable";
3
+ import { Context, Deferred, Effect, Exit, Fiber, Layer, Scope, Stream } from "effect";
4
+ import { mkdtemp } from "node:fs/promises";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import { fakeObservations } from "../../test-support/fake-observations.ts";
8
+ import { inspectorClock } from "../../test-support/inspector-clock.ts";
9
+ import { stubHostSource } from "../../test-support/stub-model-source.ts";
10
+ import type { ChildView, RunStatus, RunView, SessionView } from "../domain/view.ts";
11
+ import type { LogFields, LogLevel, LogSink } from "../runtime/log.ts";
12
+ import { HostModelAdapter } from "../runtime/models.ts";
13
+ import { openSessionRuntime, type SessionRuntime } from "../runtime/session.ts";
14
+ import {
15
+ Inspector,
16
+ liveAddress,
17
+ REPAINT_INTERVAL_MS,
18
+ type InspectorDeps,
19
+ type InspectorReadFailed,
20
+ type InspectorService,
21
+ } from "./inspector.ts";
22
+
23
+ const recordingLog = (): {
24
+ readonly sink: LogSink;
25
+ readonly entries: { level: LogLevel; code: string; fields: LogFields }[];
26
+ } => {
27
+ const entries: { level: LogLevel; code: string; fields: LogFields }[] = [];
28
+ return {
29
+ entries,
30
+ sink: {
31
+ write: (level, code, fields = {}) => {
32
+ entries.push({ level, code, fields });
33
+ },
34
+ },
35
+ };
36
+ };
37
+
38
+ const openRuntime = async (log?: LogSink): Promise<SessionRuntime> => {
39
+ const sessionDir = await mkdtemp(join(tmpdir(), "delegates-repaint-"));
40
+ return openSessionRuntime({
41
+ identity: { sessionId: "s-1", sessionDir, cwd: sessionDir, agentDir: sessionDir },
42
+ models: new HostModelAdapter(stubHostSource()),
43
+ storage: new MemoryStorage(),
44
+ ...(log === undefined ? {} : { log }),
45
+ });
46
+ };
47
+
48
+ const withRuntime = <A, E, R>(
49
+ use: (runtime: SessionRuntime) => Effect.Effect<A, E, R>,
50
+ log?: LogSink,
51
+ ): Effect.Effect<A, E, R> =>
52
+ Effect.acquireUseRelease(
53
+ Effect.promise(() => openRuntime(log)),
54
+ use,
55
+ (runtime) => Effect.promise(() => runtime.close()),
56
+ );
57
+
58
+ const runView = (
59
+ input: Partial<RunView> & { readonly runId: string; readonly status: RunStatus },
60
+ ): RunView => ({
61
+ definition: "scout",
62
+ mode: "single",
63
+ lane: "default",
64
+ conversationId: null,
65
+ taskId: null,
66
+ answer: null,
67
+ durationMs: null,
68
+ children: [],
69
+ missing: [],
70
+ gates: [],
71
+ requests: [],
72
+ diagnostic: null,
73
+ ...input,
74
+ });
75
+
76
+ const childView = (
77
+ input: Partial<ChildView> & { readonly child: number; readonly runId: string },
78
+ ): ChildView => ({
79
+ conversationId: null,
80
+ taskId: null,
81
+ agent: "scout",
82
+ status: "running",
83
+ answer: null,
84
+ durationMs: null,
85
+ ...input,
86
+ });
87
+
88
+ const view = (runs: readonly RunView[]): SessionView => ({
89
+ runs,
90
+ lanes: [],
91
+ gates: [],
92
+ reviews: [],
93
+ requests: [],
94
+ });
95
+
96
+ const oneRunningView = (): SessionView =>
97
+ view([
98
+ runView({
99
+ runId: "run:a",
100
+ status: "running",
101
+ conversationId: 10,
102
+ children: [childView({ child: 0, runId: "run:a", conversationId: 10 })],
103
+ }),
104
+ ]);
105
+
106
+ interface Built {
107
+ readonly inspector: InspectorService;
108
+ readonly scope: Scope.Closeable;
109
+ }
110
+
111
+ const build = Effect.fn("buildInspector")(function* (
112
+ deps: InspectorDeps,
113
+ ): Effect.fn.Return<Built, InspectorReadFailed> {
114
+ const scope = yield* Scope.make("sequential");
115
+ return yield* Effect.provideService(Layer.build(Inspector.layer(deps)), Scope.Scope, scope).pipe(
116
+ Effect.map((context) => ({ inspector: Context.get(context, Inspector), scope })),
117
+ Effect.onError((cause) => Scope.close(scope, Exit.failCause(cause))),
118
+ );
119
+ });
120
+
121
+ const close = (built: Built): Effect.Effect<void> => Scope.close(built.scope, Exit.void);
122
+
123
+ it.effect("building the owner reads once and arms the repaint interval", () => {
124
+ const clock = inspectorClock();
125
+ const views = { reads: 0 };
126
+ const fake = fakeObservations();
127
+ return withRuntime((runtime) =>
128
+ Effect.gen(function* () {
129
+ const built = yield* build({
130
+ runtime,
131
+ observe: fake,
132
+ view: () => {
133
+ views.reads += 1;
134
+ return Promise.resolve(oneRunningView());
135
+ },
136
+ now: () => 4_000,
137
+ timers: clock,
138
+ });
139
+ const snapshot = yield* built.inspector.snapshot;
140
+ assert.strictEqual(snapshot.revision, 1);
141
+ assert.deepStrictEqual(clock.delays, [REPAINT_INTERVAL_MS]);
142
+ assert.strictEqual(views.reads, 1);
143
+ yield* close(built);
144
+ }),
145
+ );
146
+ });
147
+
148
+ it.effect("a fired repaint reads nothing", () => {
149
+ const clock = inspectorClock();
150
+ let reads = 0;
151
+ const fake = fakeObservations();
152
+ return withRuntime((runtime) =>
153
+ Effect.gen(function* () {
154
+ const built = yield* build({
155
+ runtime,
156
+ observe: fake,
157
+ view: () => {
158
+ reads += 1;
159
+ return Promise.resolve(oneRunningView());
160
+ },
161
+ now: () => 4_000,
162
+ timers: clock,
163
+ });
164
+ const before = reads;
165
+ yield* clock.fire;
166
+ assert.strictEqual(reads, before);
167
+ assert.deepStrictEqual(clock.delays, [REPAINT_INTERVAL_MS, REPAINT_INTERVAL_MS]);
168
+ yield* close(built);
169
+ }),
170
+ );
171
+ });
172
+
173
+ it.effect("a fired repaint publishes when an age crosses a second", () => {
174
+ const clock = inspectorClock();
175
+ let now = 1_999;
176
+ const fake = fakeObservations();
177
+ return withRuntime((runtime) =>
178
+ Effect.gen(function* () {
179
+ const built = yield* build({
180
+ runtime,
181
+ observe: fake,
182
+ view: async () => oneRunningView(),
183
+ now: () => now,
184
+ timers: clock,
185
+ });
186
+ const subscribed = Deferred.makeUnsafe<void>();
187
+ const changes = yield* Stream.map(built.inspector.changes, (snapshot) => {
188
+ Deferred.doneUnsafe(subscribed, Exit.void);
189
+ return snapshot;
190
+ }).pipe(Stream.take(2), Stream.runCollect, Effect.forkChild);
191
+ yield* Deferred.await(subscribed);
192
+ fake.pushActivity(liveAddress(10), { tool: "bash", slot: "running", turns: 1, at: 1_000 });
193
+ const collected = yield* Fiber.join(changes);
194
+ const afterActivity = [...collected].at(-1);
195
+ assert.ok(afterActivity !== undefined);
196
+ assert.strictEqual(afterActivity.revision, 2);
197
+ assert.strictEqual(afterActivity.rows[0]?.idleMs, 999);
198
+
199
+ now = 2_001;
200
+ yield* clock.fire;
201
+ const afterRepaint = yield* built.inspector.snapshot;
202
+ assert.strictEqual(afterRepaint.revision, 3);
203
+ assert.strictEqual(afterRepaint.rows[0]?.idleMs, 1_001);
204
+ yield* close(built);
205
+ }),
206
+ );
207
+ });
208
+
209
+ it.effect("a fired repaint publishes nothing when no age is rendered", () => {
210
+ const clock = inspectorClock();
211
+ let reads = 0;
212
+ const fake = fakeObservations();
213
+ return withRuntime((runtime) =>
214
+ Effect.gen(function* () {
215
+ const built = yield* build({
216
+ runtime,
217
+ observe: fake,
218
+ view: () => {
219
+ reads += 1;
220
+ return Promise.resolve(oneRunningView());
221
+ },
222
+ now: () => 4_000,
223
+ timers: clock,
224
+ });
225
+ const before = reads;
226
+ yield* clock.fire;
227
+ yield* clock.fire;
228
+ const snapshot = yield* built.inspector.snapshot;
229
+ assert.strictEqual(snapshot.revision, 1);
230
+ assert.strictEqual(reads, before);
231
+ yield* close(built);
232
+ }),
233
+ );
234
+ });
235
+
236
+ it.effect("closing the scope clears the armed timer", () => {
237
+ const clock = inspectorClock();
238
+ const fake = fakeObservations();
239
+ return withRuntime((runtime) =>
240
+ Effect.gen(function* () {
241
+ const built = yield* build({
242
+ runtime,
243
+ observe: fake,
244
+ view: async () => oneRunningView(),
245
+ now: () => 4_000,
246
+ timers: clock,
247
+ });
248
+ const callback = clock.current();
249
+ assert.strictEqual(clock.armed, 1);
250
+ yield* close(built);
251
+ assert.strictEqual(clock.armed, 0);
252
+ callback?.();
253
+ const snapshot = yield* built.inspector.snapshot;
254
+ assert.strictEqual(snapshot.revision, 1);
255
+ assert.strictEqual(clock.armed, 0);
256
+ }),
257
+ );
258
+ });
259
+
260
+ it.effect("a repaint after the runtime closed publishes nothing and does not throw", () => {
261
+ const log = recordingLog();
262
+ const clock = inspectorClock();
263
+ const fake = fakeObservations();
264
+ return withRuntime(
265
+ (runtime) =>
266
+ Effect.gen(function* () {
267
+ const built = yield* build({
268
+ runtime,
269
+ observe: fake,
270
+ view: async () => oneRunningView(),
271
+ now: () => 4_000,
272
+ timers: clock,
273
+ });
274
+ const ends = yield* Stream.runCollect(built.inspector.end).pipe(Effect.forkChild);
275
+ yield* Effect.promise(() => runtime.close());
276
+ const collected = yield* Fiber.join(ends);
277
+ assert.deepStrictEqual([...collected], [{ kind: "runtime-ended" }]);
278
+ // Slice 10: the runtime end interrupts the age clock, so no timer
279
+ // survives the runtime it would read.
280
+ assert.strictEqual(clock.armed, 0);
281
+ yield* clock.fire;
282
+ const snapshot = yield* built.inspector.snapshot;
283
+ assert.strictEqual(snapshot.revision, 1);
284
+ const failures = log.entries.filter(
285
+ (entry) => entry.code.startsWith("inspector.") && entry.code.endsWith("_failed"),
286
+ );
287
+ assert.deepStrictEqual(failures, []);
288
+ yield* close(built);
289
+ }),
290
+ log.sink,
291
+ );
292
+ });
293
+
294
+ it.effect("a failed refresh logs its cause and keeps the published snapshot", () => {
295
+ const log = recordingLog();
296
+ const clock = inspectorClock();
297
+ const fake = fakeObservations();
298
+ let failing = false;
299
+ return withRuntime(
300
+ (runtime) =>
301
+ Effect.gen(function* () {
302
+ const built = yield* build({
303
+ runtime,
304
+ observe: fake,
305
+ view: async () => {
306
+ if (failing) throw new Error("boom");
307
+ return oneRunningView();
308
+ },
309
+ now: () => 4_000,
310
+ timers: clock,
311
+ });
312
+ const before = yield* built.inspector.snapshot;
313
+ failing = true;
314
+ yield* built.inspector.refresh;
315
+ const after = yield* built.inspector.snapshot;
316
+ assert.strictEqual(after.revision, before.revision);
317
+ // The name is free to change: unwrapping at the log site or giving the
318
+ // error a message both keep the detail, and the detail is the subject.
319
+ const failures = log.entries.filter((entry) => entry.code === "inspector.refresh_failed");
320
+ assert.strictEqual(failures.length, 1);
321
+ assert.strictEqual(failures[0]?.level, "warn");
322
+ assert.strictEqual(failures[0]?.fields.detail, "boom");
323
+ assert.strictEqual(typeof failures[0]?.fields.error, "string");
324
+ assert.notStrictEqual(failures[0]?.fields.error, "");
325
+ yield* close(built);
326
+ }),
327
+ log.sink,
328
+ );
329
+ });
@@ -0,0 +1,2 @@
1
+ /** The default overlay shortcut. `ctrl+i` is unusable: legacy terminals send it as Tab. */
2
+ export const DEFAULT_INSPECT_SHORTCUT = "ctrl+space";