@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.
Files changed (94) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/OPERATIONS.md +79 -6
  3. package/README.md +25 -17
  4. package/ROADMAP.md +21 -0
  5. package/TESTING.md +153 -318
  6. package/package.json +4 -3
  7. package/skills/pi-delegates/SKILL.md +29 -0
  8. package/skills/pi-delegates/references/launch.md +28 -0
  9. package/src/docs-contract.test.ts +106 -0
  10. package/src/domain/definitions.ts +3 -1
  11. package/src/domain/failure.ts +33 -0
  12. package/src/domain/model-source.ts +30 -0
  13. package/src/domain/orchestration/control.test.ts +69 -35
  14. package/src/domain/orchestration/control.ts +110 -89
  15. package/src/domain/orchestration/fanout.test.ts +125 -20
  16. package/src/domain/orchestration/fanout.ts +60 -22
  17. package/src/domain/orchestration/gates.test.ts +14 -15
  18. package/src/domain/orchestration/launch.test.ts +210 -11
  19. package/src/domain/orchestration/launch.ts +142 -32
  20. package/src/domain/orchestration/requests.test.ts +11 -16
  21. package/src/domain/orchestration/requests.ts +14 -1
  22. package/src/domain/orchestration/review.test.ts +89 -11
  23. package/src/domain/orchestration/run-wait.test.ts +4 -1
  24. package/src/domain/orchestration/run-wait.ts +1 -1
  25. package/src/domain/orchestration/workflow.test.ts +157 -16
  26. package/src/domain/orchestration/workflow.ts +269 -196
  27. package/src/domain/run-records.ts +214 -52
  28. package/src/domain/settlement.test.ts +186 -0
  29. package/src/domain/settlement.ts +137 -0
  30. package/src/domain/shapes.test.ts +20 -0
  31. package/src/domain/skills.test.ts +31 -20
  32. package/src/domain/skills.ts +36 -19
  33. package/src/domain/toolkit.test.ts +15 -1
  34. package/src/domain/toolkit.ts +8 -13
  35. package/src/domain/turn-budget.test.ts +37 -0
  36. package/src/domain/turn-budget.ts +23 -0
  37. package/src/domain/view.test.ts +78 -14
  38. package/src/domain/view.ts +67 -35
  39. package/src/extension/commands.test.ts +27 -23
  40. package/src/extension/commands.ts +10 -7
  41. package/src/extension/entry.test.ts +214 -56
  42. package/src/extension/entry.ts +78 -40
  43. package/src/extension/gate-alerts.test.ts +10 -8
  44. package/src/extension/inspector-command.test.ts +173 -21
  45. package/src/extension/inspector-command.ts +100 -53
  46. package/src/extension/prompt.test.ts +2 -4
  47. package/src/extension/retry-observer.test.ts +292 -0
  48. package/src/extension/retry-observer.ts +150 -0
  49. package/src/extension/settle-alerts.test.ts +579 -0
  50. package/src/extension/settle-alerts.ts +175 -0
  51. package/src/extension/shortcut.test.ts +6 -8
  52. package/src/extension/tools.test.ts +325 -24
  53. package/src/extension/tools.ts +61 -13
  54. package/src/extension/version.test.ts +21 -29
  55. package/src/goldens.test.ts +294 -0
  56. package/src/harness-policy.test.ts +13 -11
  57. package/src/layers.test.ts +309 -0
  58. package/src/layers.ts +405 -0
  59. package/src/package-manifest.test.ts +32 -19
  60. package/src/presentation/actions.test.ts +8 -8
  61. package/src/presentation/activity.test.ts +7 -8
  62. package/src/presentation/close.test.ts +14 -16
  63. package/src/presentation/inspector.test.ts +14 -11
  64. package/src/presentation/inspector.ts +27 -25
  65. package/src/presentation/overlay.test.ts +1 -3
  66. package/src/presentation/repaint.test.ts +226 -190
  67. package/src/presentation/snapshot.test.ts +5 -0
  68. package/src/presentation/status.test.ts +43 -0
  69. package/src/presentation/status.ts +30 -0
  70. package/src/presentation/strip.test.ts +13 -6
  71. package/src/presentation/strip.ts +7 -14
  72. package/src/presentation/wording.test.ts +153 -3
  73. package/src/presentation/wording.ts +78 -6
  74. package/src/runtime/bridge.ts +38 -44
  75. package/src/runtime/{timers.test.ts → clock.test.ts} +1 -1
  76. package/src/runtime/clock.ts +31 -0
  77. package/src/runtime/config.test.ts +26 -7
  78. package/src/runtime/config.ts +6 -1
  79. package/src/runtime/execution-env.test.ts +6 -8
  80. package/src/runtime/extensions.test.ts +4 -4
  81. package/src/runtime/extensions.ts +23 -11
  82. package/src/runtime/handle.ts +54 -0
  83. package/src/runtime/log.test.ts +19 -7
  84. package/src/runtime/log.ts +23 -0
  85. package/src/runtime/mcp.test.ts +29 -32
  86. package/src/runtime/mcp.ts +70 -3
  87. package/src/runtime/runtime-state.test.ts +25 -0
  88. package/src/runtime/runtime-state.ts +36 -0
  89. package/src/runtime/session-tag.ts +11 -0
  90. package/src/runtime/session.test.ts +24 -31
  91. package/src/runtime/session.ts +99 -261
  92. package/src/runtime/storage.ts +29 -0
  93. package/src/test-hygiene.test.ts +97 -0
  94. package/src/runtime/timers.ts +0 -10
@@ -0,0 +1,175 @@
1
+ import { listedRuns, RunDoc } from "../domain/run-records.ts";
2
+ import { errorFields, truncate, type LogSink, DETAIL_LIMIT } from "../runtime/log.ts";
3
+ import type { SessionRuntime } from "../runtime/session.ts";
4
+ import {
5
+ SETTLE_MAX_ROWS,
6
+ settleAgentMessage,
7
+ type SettleNotice,
8
+ type SettleRow,
9
+ } from "../presentation/wording.ts";
10
+
11
+ /** One coalescing window: every settlement inside it arrives as one message. */
12
+ export const SETTLE_COALESCE_MS = 1_000;
13
+
14
+ /** The start scan reads the newest this many run ids; the rest are not enumerated. */
15
+ export const SETTLE_SCAN_LIMIT = 200;
16
+
17
+ /** The surface a settle notice needs, independent of whether the inspector is active. */
18
+ export interface SettleAlertHost {
19
+ readonly notifySettled: (notice: SettleNotice, omitted?: number) => void;
20
+ }
21
+
22
+ export interface SettleAlertDeps {
23
+ readonly runtime: SessionRuntime;
24
+ readonly host: SettleAlertHost;
25
+ /** The only committer of the announced marker: the recorder's announcement path. */
26
+ readonly announce: (runId: string, taskId: number) => Promise<void>;
27
+ readonly log: LogSink;
28
+ /** Overrides `SETTLE_COALESCE_MS` in tests. */
29
+ readonly intervalMs?: number;
30
+ }
31
+
32
+ export interface SettleAlertWatch {
33
+ readonly close: () => Promise<void>;
34
+ }
35
+
36
+ /** One admitted row and the task whose settlement it reports. */
37
+ interface PendingRow {
38
+ readonly runId: string;
39
+ readonly taskId: number;
40
+ readonly row: SettleRow;
41
+ }
42
+
43
+ /**
44
+ * One session-level settle watcher. It never observes a run document: the
45
+ * recorder emits in-process after its commit landed, and the start scan covers
46
+ * a run that settled while Pi was closed. It holds no announcement state — the
47
+ * marker is the run document's — so one run is announced once.
48
+ */
49
+ export const startSettleAlerts = (deps: SettleAlertDeps): SettleAlertWatch => {
50
+ let closed = false;
51
+ let armed: unknown;
52
+ let chain: Promise<void> = Promise.resolve();
53
+ const pending: PendingRow[] = [];
54
+
55
+ const log = (error: unknown): void => {
56
+ deps.log.write("warn", "settle.notify_failed", errorFields(error));
57
+ };
58
+
59
+ const enqueue = (task: () => Promise<void>): Promise<void> => {
60
+ chain = chain.then(task).catch(log);
61
+ return chain;
62
+ };
63
+
64
+ const roleOf = (value: unknown): string =>
65
+ typeof value === "string" && value.length > 0 ? value : "unknown";
66
+
67
+ /** Admit one run when it is detached, settled and not yet announced for this task. */
68
+ const admit = async (runId: string): Promise<void> => {
69
+ if (closed) return;
70
+ const doc = await deps.runtime.snapshot(RunDoc, runId);
71
+ if (doc === undefined) return;
72
+ const taskId = doc.taskId;
73
+ if (doc.execution !== "detached") return;
74
+ if (doc.resolution === null) return;
75
+ if (taskId === null || doc.settledTaskId === taskId) return;
76
+ const row: SettleRow = {
77
+ runId,
78
+ definition: roleOf(doc.definition),
79
+ outcome: doc.resolution,
80
+ failure: doc.failure,
81
+ parentRunId: doc.parentRunId,
82
+ };
83
+ const existing = pending.findIndex((entry) => entry.runId === runId);
84
+ if (existing >= 0) pending.splice(existing, 1, { runId, taskId, row });
85
+ else pending.push({ runId, taskId, row });
86
+ arm();
87
+ };
88
+
89
+ const arm = (): void => {
90
+ if (closed || armed !== undefined) return;
91
+ armed = deps.runtime.timers.setTimeout(() => {
92
+ armed = undefined;
93
+ void enqueue(deliver);
94
+ }, deps.intervalMs ?? SETTLE_COALESCE_MS);
95
+ };
96
+
97
+ /** One window: hand at most `SETTLE_MAX_ROWS` rows to the host, then commit each marker. */
98
+ const deliver = async (): Promise<void> => {
99
+ if (closed || pending.length === 0) return;
100
+ const batch = pending.slice(0, SETTLE_MAX_ROWS);
101
+ const omitted = pending.length - batch.length;
102
+ const runs = batch.map((entry) => entry.runId).join(",");
103
+ try {
104
+ deps.host.notifySettled({ rows: batch.map((entry) => entry.row) }, omitted);
105
+ } catch (error) {
106
+ deps.log.write("warn", "settle.notify_failed", { runs, ...errorFields(error) });
107
+ // Nothing was delivered: the rows stay pending for the next window.
108
+ arm();
109
+ return;
110
+ }
111
+ pending.splice(0, batch.length);
112
+ for (const entry of batch) {
113
+ try {
114
+ await deps.announce(entry.runId, entry.taskId);
115
+ } catch (error) {
116
+ log(error);
117
+ }
118
+ }
119
+ if (pending.length > 0) arm();
120
+ };
121
+
122
+ /**
123
+ * Once at start: the newest `SETTLE_SCAN_LIMIT` run ids with their group children,
124
+ * and one line when the window truncates.
125
+ */
126
+ const scan = async (): Promise<void> => {
127
+ const { runIds, omitted } = await listedRuns(deps.runtime, { limit: SETTLE_SCAN_LIMIT });
128
+ if (omitted > 0) {
129
+ deps.log.write("info", "settle.scan_truncated", { omitted });
130
+ }
131
+ for (const runId of runIds) {
132
+ if (closed) return;
133
+ await admit(runId);
134
+ }
135
+ };
136
+
137
+ const unsubscribe = deps.runtime.settle.on((event) => {
138
+ void enqueue(() => admit(event.runId));
139
+ });
140
+
141
+ const onState = deps.runtime.onState((state) => {
142
+ if (state.kind === "closed" || state.kind === "failing") void close();
143
+ });
144
+
145
+ const close = async (): Promise<void> => {
146
+ if (closed) return;
147
+ closed = true;
148
+ unsubscribe();
149
+ onState();
150
+ if (armed !== undefined) {
151
+ deps.runtime.timers.clearTimeout(armed);
152
+ armed = undefined;
153
+ }
154
+ await chain;
155
+ };
156
+
157
+ void enqueue(scan);
158
+ return { close };
159
+ };
160
+
161
+ /** A host with no surface: one id-only log line per batch. */
162
+ export const logOnlySettleHost = (log: LogSink): SettleAlertHost => ({
163
+ notifySettled: (notice, omitted = 0) =>
164
+ log.write("info", "run.settled", {
165
+ runs: notice.rows.map((row) => row.runId).join(","),
166
+ detail: truncate(settleAgentMessage(notice, omitted), DETAIL_LIMIT),
167
+ }),
168
+ });
169
+
170
+ /** The host a mode gets: a person-facing surface in `tui`/`rpc`, the log line otherwise. */
171
+ export const settleHostFor = (
172
+ mode: "tui" | "rpc" | "json" | "print",
173
+ surface: SettleAlertHost,
174
+ log: LogSink,
175
+ ): SettleAlertHost => (mode === "tui" || mode === "rpc" ? surface : logOnlySettleHost(log));
@@ -1,7 +1,5 @@
1
1
  import { assert, it, vi } from "@effect/vitest";
2
- import { mkdtemp, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
2
+ import { join, makeTempDir, writeText } from "../../test-support/platform.ts";
5
3
  import { DEFAULT_INSPECT_SHORTCUT } from "../presentation/shortcut.ts";
6
4
  import {
7
5
  INSPECT_SHORTCUT_ENV,
@@ -10,7 +8,7 @@ import {
10
8
  resolveInspectShortcut,
11
9
  } from "./shortcut.ts";
12
10
 
13
- const agentDir = (): Promise<string> => mkdtemp(join(tmpdir(), "delegates-shortcut-"));
11
+ const agentDir = (): Promise<string> => makeTempDir("delegates-shortcut-");
14
12
 
15
13
  it("accepts the documented key shapes", () => {
16
14
  for (const key of ["ctrl+space", "ctrl+alt+i", "ctrl+shift+i", "f2", "escape"]) {
@@ -27,7 +25,7 @@ it("defaults to ctrl+space", async () => {
27
25
 
28
26
  it("reads inspectorShortcut from delegates.json", async () => {
29
27
  const dir = await agentDir();
30
- await writeFile(
28
+ await writeText(
31
29
  join(dir, INSPECT_SHORTCUT_FILE),
32
30
  JSON.stringify({ inspectorShortcut: "ctrl+alt+i" }),
33
31
  );
@@ -36,7 +34,7 @@ it("reads inspectorShortcut from delegates.json", async () => {
36
34
 
37
35
  it("an explicit option beats the environment and the file", async () => {
38
36
  const dir = await agentDir();
39
- await writeFile(
37
+ await writeText(
40
38
  join(dir, INSPECT_SHORTCUT_FILE),
41
39
  JSON.stringify({ inspectorShortcut: "ctrl+alt+i" }),
42
40
  );
@@ -46,7 +44,7 @@ it("an explicit option beats the environment and the file", async () => {
46
44
 
47
45
  it("the environment beats the file", async () => {
48
46
  const dir = await agentDir();
49
- await writeFile(
47
+ await writeText(
50
48
  join(dir, INSPECT_SHORTCUT_FILE),
51
49
  JSON.stringify({ inspectorShortcut: "ctrl+alt+i" }),
52
50
  );
@@ -56,7 +54,7 @@ it("the environment beats the file", async () => {
56
54
 
57
55
  it("an invalid file value falls back to the default", async () => {
58
56
  const dir = await agentDir();
59
- await writeFile(
57
+ await writeText(
60
58
  join(dir, INSPECT_SHORTCUT_FILE),
61
59
  JSON.stringify({ inspectorShortcut: "ctrl+nope" }),
62
60
  );