@crouton-kit/crouter 0.3.18 → 0.3.19

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 (257) hide show
  1. package/dist/build-root.js +6 -0
  2. package/dist/builtin-memory/crouter-development/marketplaces.md +164 -0
  3. package/dist/builtin-memory/crouter-development/personas/base-prompt.md +58 -0
  4. package/dist/builtin-memory/crouter-development/personas/orchestrator-prompt.md +60 -0
  5. package/dist/builtin-memory/crouter-development/personas.md +117 -0
  6. package/dist/builtin-memory/crouter-development/plugins.md +163 -0
  7. package/dist/builtin-memory/design.md +60 -0
  8. package/dist/builtin-memory/development.md +116 -0
  9. package/dist/builtin-memory/planning.md +66 -0
  10. package/dist/builtin-memory/spec.md +99 -0
  11. package/dist/builtin-personas/design/orchestrator.md +1 -1
  12. package/dist/builtin-personas/developer/orchestrator.md +1 -1
  13. package/dist/builtin-personas/lifecycle/resident.md +1 -1
  14. package/dist/builtin-personas/lifecycle/terminal.md +5 -2
  15. package/dist/builtin-personas/orchestration-kernel.md +15 -24
  16. package/dist/builtin-personas/plan/orchestrator.md +1 -1
  17. package/dist/builtin-personas/runtime-base.md +6 -3
  18. package/dist/builtin-personas/spec/orchestrator.md +1 -1
  19. package/dist/builtin-personas/waiting.md +8 -0
  20. package/dist/builtin-skills/skills/crouter-development/personas/orchestrator-prompt/SKILL.md +1 -1
  21. package/dist/builtin-skills/skills/crouter-development/plugins/SKILL.md +2 -2
  22. package/dist/builtin-views/_lib/states.mjs +161 -0
  23. package/dist/builtin-views/canvas/client.mjs +303 -0
  24. package/dist/builtin-views/canvas/view.mjs +576 -0
  25. package/dist/builtin-views/git-pr/client.mjs +440 -0
  26. package/dist/builtin-views/git-pr/view.mjs +675 -0
  27. package/dist/builtin-views/inbox/_lib/render.mjs +177 -0
  28. package/dist/builtin-views/inbox/sources/gmail.mjs +965 -0
  29. package/dist/builtin-views/inbox/sources/linkedin.mjs +427 -0
  30. package/dist/builtin-views/inbox/view.mjs +889 -0
  31. package/dist/builtin-views/linkedin/client.mjs +610 -0
  32. package/dist/builtin-views/linkedin/view.mjs +1171 -0
  33. package/dist/clients/attach/attach-cmd.d.ts +2 -0
  34. package/dist/clients/attach/attach-cmd.js +354 -0
  35. package/dist/clients/attach/chat-view.d.ts +77 -0
  36. package/dist/clients/attach/chat-view.js +450 -0
  37. package/dist/clients/attach/clipboard-image.d.ts +16 -0
  38. package/dist/clients/attach/clipboard-image.js +113 -0
  39. package/dist/clients/attach/config-load.d.ts +31 -0
  40. package/dist/clients/attach/config-load.js +113 -0
  41. package/dist/clients/attach/extension-dialogs.d.ts +29 -0
  42. package/dist/clients/attach/extension-dialogs.js +101 -0
  43. package/dist/clients/attach/input-controller.d.ts +54 -0
  44. package/dist/clients/attach/input-controller.js +204 -0
  45. package/dist/clients/attach/slash-commands.d.ts +36 -0
  46. package/dist/clients/attach/slash-commands.js +200 -0
  47. package/dist/clients/attach/view-socket.d.ts +48 -0
  48. package/dist/clients/attach/view-socket.js +126 -0
  49. package/dist/commands/attention.js +3 -3
  50. package/dist/commands/canvas-prune.js +1 -1
  51. package/dist/commands/daemon.js +3 -2
  52. package/dist/commands/human/prompts.js +1 -1
  53. package/dist/commands/human/queue.js +43 -8
  54. package/dist/commands/human/shared.d.ts +2 -0
  55. package/dist/commands/memory/find.d.ts +1 -0
  56. package/dist/commands/memory/find.js +180 -0
  57. package/dist/commands/memory/lint.d.ts +1 -0
  58. package/dist/commands/memory/lint.js +140 -0
  59. package/dist/commands/memory/list.d.ts +1 -0
  60. package/dist/commands/memory/list.js +79 -0
  61. package/dist/commands/memory/read.js +103 -0
  62. package/dist/commands/memory/shared.d.ts +30 -0
  63. package/dist/commands/memory/shared.js +122 -0
  64. package/dist/commands/memory/write.d.ts +1 -0
  65. package/dist/commands/memory/write.js +85 -0
  66. package/dist/commands/memory.d.ts +2 -0
  67. package/dist/commands/memory.js +27 -0
  68. package/dist/commands/node.d.ts +3 -2
  69. package/dist/commands/node.js +607 -61
  70. package/dist/commands/pkg/market-manage.js +1 -1
  71. package/dist/commands/push.js +6 -6
  72. package/dist/commands/revive.js +1 -1
  73. package/dist/commands/skill/author.js +1 -1
  74. package/dist/commands/skill/shared.d.ts +1 -8
  75. package/dist/commands/skill/shared.js +2 -55
  76. package/dist/commands/skill.js +9 -14
  77. package/dist/commands/sys/doctor.js +1 -1
  78. package/dist/commands/sys/update.js +1 -1
  79. package/dist/commands/view-cycle.d.ts +2 -0
  80. package/dist/commands/view-cycle.js +125 -0
  81. package/dist/commands/view-list.d.ts +2 -0
  82. package/dist/commands/view-list.js +66 -0
  83. package/dist/commands/view-new.d.ts +2 -0
  84. package/dist/commands/view-new.js +70 -0
  85. package/dist/commands/view-pick.d.ts +2 -0
  86. package/dist/commands/view-pick.js +119 -0
  87. package/dist/commands/view-run.d.ts +2 -0
  88. package/dist/commands/view-run.js +191 -0
  89. package/dist/commands/view.d.ts +2 -0
  90. package/dist/commands/view.js +29 -0
  91. package/dist/core/__tests__/broker-lifecycle.test.d.ts +1 -0
  92. package/dist/core/__tests__/broker-lifecycle.test.js +677 -0
  93. package/dist/core/__tests__/broker-sdk-wiring.test.d.ts +1 -0
  94. package/dist/core/__tests__/broker-sdk-wiring.test.js +166 -0
  95. package/dist/core/__tests__/cascade-close.test.js +12 -2
  96. package/dist/core/__tests__/child-death-wake.test.d.ts +1 -0
  97. package/dist/core/__tests__/child-death-wake.test.js +245 -0
  98. package/dist/core/__tests__/context-intro.test.js +76 -62
  99. package/dist/core/__tests__/daemon-boot.test.js +14 -5
  100. package/dist/core/__tests__/daemon-liveness.test.js +34 -9
  101. package/dist/core/__tests__/detach-focus.test.d.ts +1 -0
  102. package/dist/core/__tests__/detach-focus.test.js +206 -0
  103. package/dist/core/__tests__/draw-style.test.d.ts +1 -0
  104. package/dist/core/__tests__/draw-style.test.js +258 -0
  105. package/dist/core/__tests__/fixtures/c3-custom-provider-ext.d.ts +2 -0
  106. package/dist/core/__tests__/fixtures/c3-custom-provider-ext.js +18 -0
  107. package/dist/core/__tests__/fixtures/fake-engine.d.ts +138 -0
  108. package/dist/core/__tests__/fixtures/fake-engine.js +614 -0
  109. package/dist/core/__tests__/fixtures/fake-pi-host.js +1 -0
  110. package/dist/core/__tests__/flagship-lifecycle.test.js +7 -1
  111. package/dist/core/__tests__/frame-decoder-perf.test.d.ts +1 -0
  112. package/dist/core/__tests__/frame-decoder-perf.test.js +198 -0
  113. package/dist/core/__tests__/helpers/harness.d.ts +9 -0
  114. package/dist/core/__tests__/helpers/harness.js +111 -1
  115. package/dist/core/__tests__/home-session.test.js +41 -8
  116. package/dist/core/__tests__/human-new-window-regression.test.d.ts +1 -0
  117. package/dist/core/__tests__/human-new-window-regression.test.js +101 -0
  118. package/dist/core/__tests__/kickoff.test.js +37 -3
  119. package/dist/core/__tests__/live-mutation.test.js +50 -33
  120. package/dist/core/__tests__/memory.test.js +23 -115
  121. package/dist/core/__tests__/placement-focus.test.js +5 -0
  122. package/dist/core/__tests__/placement-teardown.test.js +54 -11
  123. package/dist/core/__tests__/relaunch.test.js +4 -3
  124. package/dist/core/__tests__/review-render-pane-regression.test.d.ts +1 -0
  125. package/dist/core/__tests__/review-render-pane-regression.test.js +133 -0
  126. package/dist/core/__tests__/spawn-root.test.js +10 -0
  127. package/dist/core/__tests__/spike-harness.test.js +1 -0
  128. package/dist/core/__tests__/wake-bearings.test.d.ts +1 -0
  129. package/dist/core/__tests__/wake-bearings.test.js +156 -0
  130. package/dist/core/__tests__/wake-origin.test.d.ts +1 -0
  131. package/dist/core/__tests__/wake-origin.test.js +110 -0
  132. package/dist/core/bootstrap.js +1 -1
  133. package/dist/core/canvas/browse/__tests__/render.test.js +1 -0
  134. package/dist/core/canvas/browse/app.js +24 -2
  135. package/dist/core/canvas/browse/model.d.ts +38 -2
  136. package/dist/core/canvas/browse/model.js +134 -10
  137. package/dist/core/canvas/browse/render.d.ts +6 -12
  138. package/dist/core/canvas/browse/render.js +72 -104
  139. package/dist/core/canvas/canvas.js +15 -22
  140. package/dist/core/canvas/db.js +46 -0
  141. package/dist/core/canvas/index.d.ts +1 -0
  142. package/dist/core/canvas/index.js +1 -0
  143. package/dist/core/canvas/pid.d.ts +4 -0
  144. package/dist/core/canvas/pid.js +23 -0
  145. package/dist/core/canvas/render.d.ts +11 -2
  146. package/dist/core/canvas/render.js +69 -0
  147. package/dist/core/canvas/types.d.ts +85 -3
  148. package/dist/core/canvas/wakeups.d.ts +76 -0
  149. package/dist/core/canvas/wakeups.js +185 -0
  150. package/dist/core/config.js +4 -1
  151. package/dist/core/frontmatter.js +37 -124
  152. package/dist/core/memory-resolver.d.ts +49 -0
  153. package/dist/core/memory-resolver.js +141 -0
  154. package/dist/core/personas/loader.d.ts +7 -0
  155. package/dist/core/personas/loader.js +36 -5
  156. package/dist/core/personas/resolve.js +13 -3
  157. package/dist/core/predicate.d.ts +63 -0
  158. package/dist/core/predicate.js +189 -0
  159. package/dist/core/resolver.js +26 -5
  160. package/dist/core/runtime/bearings.d.ts +53 -12
  161. package/dist/core/runtime/bearings.js +132 -59
  162. package/dist/core/runtime/broker-cli.d.ts +1 -0
  163. package/dist/core/runtime/broker-cli.js +46 -0
  164. package/dist/core/runtime/broker-protocol.d.ts +332 -0
  165. package/dist/core/runtime/broker-protocol.js +153 -0
  166. package/dist/core/runtime/broker-sdk.d.ts +48 -0
  167. package/dist/core/runtime/broker-sdk.js +72 -0
  168. package/dist/core/runtime/broker.d.ts +55 -0
  169. package/dist/core/runtime/broker.js +1128 -0
  170. package/dist/core/runtime/close.js +35 -6
  171. package/dist/core/runtime/host.d.ts +53 -0
  172. package/dist/core/runtime/host.js +186 -0
  173. package/dist/core/runtime/kickoff.d.ts +2 -1
  174. package/dist/core/runtime/kickoff.js +91 -5
  175. package/dist/core/runtime/launch.d.ts +45 -2
  176. package/dist/core/runtime/launch.js +65 -2
  177. package/dist/core/runtime/lifecycle.js +23 -6
  178. package/dist/core/runtime/memory.d.ts +2 -42
  179. package/dist/core/runtime/memory.js +11 -162
  180. package/dist/core/runtime/nodes.d.ts +33 -0
  181. package/dist/core/runtime/nodes.js +59 -1
  182. package/dist/core/runtime/persona.js +21 -11
  183. package/dist/core/runtime/pi-vendored.d.ts +18 -0
  184. package/dist/core/runtime/pi-vendored.js +49 -0
  185. package/dist/core/runtime/placement.d.ts +32 -14
  186. package/dist/core/runtime/placement.js +206 -52
  187. package/dist/core/runtime/promote.d.ts +0 -6
  188. package/dist/core/runtime/promote.js +1 -12
  189. package/dist/core/runtime/{demote.d.ts → recycle.d.ts} +5 -5
  190. package/dist/core/runtime/{demote.js → recycle.js} +27 -11
  191. package/dist/core/runtime/reset.js +8 -6
  192. package/dist/core/runtime/revive.d.ts +2 -0
  193. package/dist/core/runtime/revive.js +34 -34
  194. package/dist/core/runtime/spawn.d.ts +19 -0
  195. package/dist/core/runtime/spawn.js +75 -22
  196. package/dist/core/runtime/stop-guard.d.ts +1 -1
  197. package/dist/core/runtime/stop-guard.js +6 -1
  198. package/dist/core/runtime/tmux-chrome.d.ts +1 -1
  199. package/dist/core/runtime/tmux-chrome.js +1 -1
  200. package/dist/core/runtime/tmux.d.ts +28 -0
  201. package/dist/core/runtime/tmux.js +80 -6
  202. package/dist/core/scope.d.ts +11 -0
  203. package/dist/core/scope.js +39 -0
  204. package/dist/core/spawn.d.ts +5 -0
  205. package/dist/core/spawn.js +20 -2
  206. package/dist/core/substrate/gate.d.ts +13 -0
  207. package/dist/core/substrate/gate.js +21 -0
  208. package/dist/core/substrate/index.d.ts +7 -0
  209. package/dist/core/substrate/index.js +18 -0
  210. package/dist/core/substrate/on-read.d.ts +14 -0
  211. package/dist/core/substrate/on-read.js +292 -0
  212. package/dist/core/substrate/render.d.ts +25 -0
  213. package/dist/core/substrate/render.js +256 -0
  214. package/dist/core/substrate/schema.d.ts +76 -0
  215. package/dist/core/substrate/schema.js +124 -0
  216. package/dist/core/substrate/session-cache.d.ts +30 -0
  217. package/dist/core/substrate/session-cache.js +77 -0
  218. package/dist/core/substrate/subject.d.ts +41 -0
  219. package/dist/core/substrate/subject.js +54 -0
  220. package/dist/core/tui/contract.d.ts +83 -0
  221. package/dist/core/tui/contract.js +8 -0
  222. package/dist/core/tui/draw.d.ts +96 -0
  223. package/dist/core/tui/draw.js +339 -0
  224. package/dist/core/tui/host.d.ts +29 -0
  225. package/dist/core/tui/host.js +379 -0
  226. package/dist/core/tui/loader.d.ts +16 -0
  227. package/dist/core/tui/loader.js +94 -0
  228. package/dist/core/{canvas/browse → tui}/terminal.js +7 -6
  229. package/dist/core/wake.d.ts +86 -0
  230. package/dist/core/wake.js +308 -0
  231. package/dist/daemon/crtrd.d.ts +29 -4
  232. package/dist/daemon/crtrd.js +662 -46
  233. package/dist/pi-extensions/__tests__/canvas-context-intro.test.d.ts +1 -0
  234. package/dist/pi-extensions/__tests__/canvas-context-intro.test.js +171 -0
  235. package/dist/pi-extensions/__tests__/canvas-stophook-agentend.test.js +19 -12
  236. package/dist/pi-extensions/canvas-commands.d.ts +3 -0
  237. package/dist/pi-extensions/canvas-commands.js +10 -0
  238. package/dist/pi-extensions/canvas-context-intro.d.ts +17 -0
  239. package/dist/pi-extensions/canvas-context-intro.js +55 -15
  240. package/dist/pi-extensions/canvas-doc-substrate.d.ts +44 -0
  241. package/dist/pi-extensions/canvas-doc-substrate.js +112 -0
  242. package/dist/pi-extensions/canvas-nav.d.ts +3 -0
  243. package/dist/pi-extensions/canvas-nav.js +87 -23
  244. package/dist/pi-extensions/canvas-stophook.js +17 -8
  245. package/dist/pi-extensions/canvas-view.d.ts +21 -0
  246. package/dist/pi-extensions/canvas-view.js +75 -0
  247. package/dist/prompts/skill.js +19 -26
  248. package/dist/prompts/view.d.ts +7 -0
  249. package/dist/prompts/view.js +101 -0
  250. package/dist/types.d.ts +4 -0
  251. package/dist/types.js +1 -0
  252. package/package.json +9 -4
  253. package/dist/commands/skill/find.d.ts +0 -4
  254. package/dist/commands/skill/find.js +0 -257
  255. package/dist/commands/skill/read.js +0 -91
  256. /package/dist/commands/{skill → memory}/read.d.ts +0 -0
  257. /package/dist/core/{canvas/browse → tui}/terminal.d.ts +0 -0
@@ -0,0 +1,23 @@
1
+ // src/core/canvas/pid.ts
2
+ //
3
+ // The ONE shared signal-0 liveness probe. It lives at the canvas/ layer — the
4
+ // LOWEST shared layer — so canvas/, runtime/, AND daemon/ can all import it
5
+ // "down" without a reverse-layer violation (runtime/ sits above canvas/, daemon/
6
+ // above runtime/). It is a pure process-existence utility, NOT data-model access,
7
+ // so it is exempt from the "only canvas.ts touches the db" rule. Collapses the
8
+ // four near-identical copies that used to live in canvas.ts, revive.ts,
9
+ // placement.ts, and crtrd.ts into one (Phase 3 review reuse MINOR-1).
10
+ /** True if a process with `pid` is currently alive (signal-0 probe). `kill(pid,
11
+ * 0)` throws ESRCH when the process is gone; EPERM means it exists but isn't
12
+ * ours — still alive. A null/undefined pid (legacy / never-booted) reads dead. */
13
+ export function isPidAlive(pid) {
14
+ if (pid == null)
15
+ return false;
16
+ try {
17
+ process.kill(pid, 0);
18
+ return true;
19
+ }
20
+ catch (e) {
21
+ return e.code === 'EPERM';
22
+ }
23
+ }
@@ -1,4 +1,4 @@
1
- import type { NodeStatus } from './types.js';
1
+ import type { NodeStatus, Lifecycle } from './types.js';
2
2
  /**
3
3
  * Render the subscription sub-DAG rooted at `rootId` as an ASCII tree.
4
4
  * The root is the first line (no connector prefix); children are indented.
@@ -28,11 +28,20 @@ export interface DashboardRow {
28
28
  cwd: string;
29
29
  /** ISO 8601 birth timestamp — drives the recency sort + the relative-age cue. */
30
30
  created: string;
31
+ /** terminal = one-shot worker (finalizes on push --final); resident = persistent
32
+ * agent you come back to. Drives the browser's resident-only lifecycle filter.
33
+ * Only populated by dashboardRowsAll (the browser snapshot). */
34
+ lifecycle?: Lifecycle;
31
35
  /** The node's spawn prompt (context/initial-prompt.md), trimmed + capped. Only
32
36
  * populated by dashboardRowsAll (the browser snapshot) — the dashboard leaf
33
37
  * leaves it undefined to avoid a file read per node. Indexed by super-search
34
- * and shown in the preview panel. */
38
+ * and shown in the preview panel when there is no live query. */
35
39
  goal?: string;
40
+ /** EVERY user prompt across the node's pi session — the whole conversation, not
41
+ * just the spawn prompt — joined + capped. Populated by dashboardRowsAll, undefined
42
+ * for never-revived nodes (no session file). Powers whole-conversation super-search
43
+ * + the windowed match snippet in the preview. */
44
+ prompts?: string;
36
45
  }
37
46
  /** One row per node visible in the sub-DAG of `rootId` (including root). */
38
47
  export declare function dashboardRows(rootId: string): DashboardRow[];
@@ -181,6 +181,73 @@ function readGoalText(nodeId) {
181
181
  return undefined;
182
182
  }
183
183
  }
184
+ /** EVERY user prompt across a node's pi session — the whole conversation, not just
185
+ * the spawn prompt. Streams the session jsonl off disk, prefiltering to user-role
186
+ * lines so the big assistant/toolResult lines are never JSON-parsed, extracts each
187
+ * user message's text, joins newline-separated, and caps total + per-message so a
188
+ * long session can't bloat the snapshot. Never throws; returns undefined when there
189
+ * is no session file yet (a node that was never revived). */
190
+ const CONVO_CAP = 8192;
191
+ const CONVO_MSG_CAP = 2048;
192
+ function readConversationPrompts(sessionFile) {
193
+ if (sessionFile === undefined || sessionFile === null || sessionFile === '')
194
+ return undefined;
195
+ try {
196
+ if (!existsSync(sessionFile))
197
+ return undefined;
198
+ const raw = readFileSync(sessionFile, 'utf8');
199
+ const parts = [];
200
+ let total = 0;
201
+ for (const line of raw.split('\n')) {
202
+ // Cheap prefilter: skip every line that isn't a user-role message before the
203
+ // (relatively costly) JSON.parse. Pi writes compact JSON (no spaces), but
204
+ // tolerate the spaced form too.
205
+ if (line === '' || (line.indexOf('"role":"user"') === -1 && line.indexOf('"role": "user"') === -1))
206
+ continue;
207
+ let rec;
208
+ try {
209
+ rec = JSON.parse(line);
210
+ }
211
+ catch {
212
+ continue;
213
+ }
214
+ if (rec.type !== 'message' || rec.message?.role !== 'user')
215
+ continue;
216
+ const text = extractUserText(rec.message.content);
217
+ if (text === '')
218
+ continue;
219
+ const capped = text.length > CONVO_MSG_CAP ? text.slice(0, CONVO_MSG_CAP) : text;
220
+ parts.push(capped);
221
+ total += capped.length + 1;
222
+ if (total >= CONVO_CAP)
223
+ break;
224
+ }
225
+ if (parts.length === 0)
226
+ return undefined;
227
+ const joined = parts.join('\n');
228
+ return joined.length > CONVO_CAP ? joined.slice(0, CONVO_CAP) : joined;
229
+ }
230
+ catch {
231
+ return undefined;
232
+ }
233
+ }
234
+ /** Concatenate the `text` blocks of one pi user message's content. Content is
235
+ * usually an array of `{type,text}` blocks but may be a bare string. */
236
+ function extractUserText(content) {
237
+ if (typeof content === 'string')
238
+ return content.trim();
239
+ if (!Array.isArray(content))
240
+ return '';
241
+ const out = [];
242
+ for (const block of content) {
243
+ if (block !== null && typeof block === 'object') {
244
+ const b = block;
245
+ if (b.type === 'text' && typeof b.text === 'string')
246
+ out.push(b.text);
247
+ }
248
+ }
249
+ return out.join(' ').trim();
250
+ }
184
251
  /** One row per node visible in the sub-DAG of `rootId` (including root). */
185
252
  export function dashboardRows(rootId) {
186
253
  const ids = [rootId, ...view(rootId)];
@@ -219,7 +286,9 @@ export function dashboardRowsAll() {
219
286
  asks: countAsks(row.node_id),
220
287
  cwd: row.cwd,
221
288
  created: row.created,
289
+ lifecycle: row.lifecycle,
222
290
  goal: readGoalText(row.node_id),
291
+ prompts: readConversationPrompts(meta?.pi_session_file),
223
292
  }];
224
293
  });
225
294
  }
@@ -1,6 +1,10 @@
1
- /** What a node is doing right now. UI shows active+idle; `done` is hidden but
2
- * revivable; `canceled` is a user-closed node (also hidden, also revivable
3
- * not a fault); only `dead` is a fault. */
1
+ import type { SpawnChildOpts } from '../runtime/spawn.js';
2
+ /** What a node is doing right now. `active` means the engine process is live on
3
+ * its host — a tmux pane OR a headless broker (`host_kind`) NOT that it holds
4
+ * a window or that anyone is watching it (attachment is a separate axis: the
5
+ * `focuses` table for tmux, the broker's viewer set for headless). UI shows
6
+ * active+idle; `done` is hidden but revivable; `canceled` is a user-closed node
7
+ * (also hidden, also revivable — not a fault); only `dead` is a fault. */
4
8
  export type NodeStatus = 'active' | 'idle' | 'done' | 'dead' | 'canceled';
5
9
  /** Does stopping finalize the node? terminal = worker (finalizes on push --final);
6
10
  * resident = manager/orchestrator (stays dormant, woken by inbox). */
@@ -44,6 +48,11 @@ export interface NodeIdentity {
44
48
  created: string;
45
49
  /** The dir this node is pinned to — its cwd (where pi runs, bash executes). */
46
50
  cwd: string;
51
+ /** Which HOST launches + supervises this node: 'tmux' (a pane) or 'broker' (the
52
+ * headless in-process engine). Durable IDENTITY (mirrored to the row like
53
+ * `cwd`, re-derived on index rebuild), NOT live runtime like `pane`.
54
+ * NULL ⇒ 'tmux' (every pre-broker node). */
55
+ host_kind?: 'tmux' | 'broker' | null;
47
56
  /** Role the node was born as: explore | developer | plan | review | general… */
48
57
  kind: string;
49
58
  mode: Mode;
@@ -65,6 +74,14 @@ export interface NodeIdentity {
65
74
  * `parent` so an INDEPENDENT root (parent=null) still records its lineage.
66
75
  * Audit only; null for a user-opened root. Defaults to `parent` for a child. */
67
76
  spawned_by?: string | null;
77
+ /** Fork provenance — the `--fork-from` source this node was forked from at
78
+ * spawn (the node id / session path / pi session uuid the caller passed). pi
79
+ * COPIED that conversation into this node's first session, so the node boots
80
+ * carrying the SOURCE's first-person history. Persisted here so the boot
81
+ * intro can assert this node's OWN identity over that inherited narrative
82
+ * (without it a fork impersonates its source). Undefined for a fresh node;
83
+ * identity/audit only. */
84
+ fork_from?: string | null;
68
85
  /** New subscriptions this node opens default to passive when true. */
69
86
  passive_default?: boolean;
70
87
  /** REVIVE-HOME — the tmux session a node is (re)opened into when it must
@@ -143,6 +160,8 @@ export interface NodeRow {
143
160
  lifecycle: Lifecycle;
144
161
  status: NodeStatus;
145
162
  cwd: string;
163
+ /** Which HOST launches + supervises this node; see NodeIdentity.host_kind. */
164
+ host_kind: 'tmux' | 'broker' | null;
146
165
  parent: string | null;
147
166
  created: string;
148
167
  /** Authoritative runtime columns (see NodeRuntime). */
@@ -188,3 +207,66 @@ export interface SubscriptionRef {
188
207
  active: boolean;
189
208
  created: string;
190
209
  }
210
+ /** The four firing actions a wakeup can carry (design §4):
211
+ * - bare → revive the target node directly (resume:false), no inbox entry.
212
+ * - noted → deliver a note to the target's inbox (normal tier).
213
+ * - deadline → deliver a timeout note to the target's inbox (urgent tier); ≤1 per node.
214
+ * - spawn → spawnChild a fresh node from a stored recipe (detached, node_id NULL). */
215
+ export type WakeKind = 'bare' | 'noted' | 'deadline' | 'spawn';
216
+ /** payload for a `bare` wake — `null`, or a label carried only for list rendering. */
217
+ export interface BareWakePayload {
218
+ label?: string;
219
+ }
220
+ /** payload for a `noted` wake — a non-empty note body + a ≤120-char first-line label. */
221
+ export interface NotedWakePayload {
222
+ body: string;
223
+ label: string;
224
+ }
225
+ /** payload for a `deadline` wake — a non-empty timeout note + the machine-readable
226
+ * `timeout` mirror. The VISIBLE timeout signal rides the RENDERED label/body, not
227
+ * this flag (inbox digests never surface arbitrary data keys). ≤1 per node. */
228
+ export interface DeadlineWakePayload {
229
+ body: string;
230
+ timeout: true;
231
+ label: string;
232
+ }
233
+ /** The parsed per-kind payload union (Maj-1): the data-access layer JSON.stringifies
234
+ * this on write and JSON.parses it back on read, so callers consume `payload.body`
235
+ * / `payload as SpawnChildOpts` directly with no re-parse. A `spawn` payload is a
236
+ * SpawnChildOpts recipe with `parent` resolved to a NON-NULL armer id at arm time. */
237
+ export type WakePayload = null | BareWakePayload | NotedWakePayload | DeadlineWakePayload | SpawnChildOpts;
238
+ /** A wakeup row as stored in the `wakeups` table (canvas.db, migration v7). One row =
239
+ * "do <kind> at fire_at [every recur]". `node_id` is the revive TARGET + node-anchored
240
+ * cancel anchor (NULL = canvas-detached); `owner_id` is the ARMER (set on every row).
241
+ * `payload` is the PARSED per-kind union, never the raw JSON string (Maj-1). */
242
+ export interface Wakeup {
243
+ /** Stable id (caller-supplied at arm time, minted as `wk-${newNodeId()}`). */
244
+ wakeup_id: string;
245
+ /** Revive target + node-anchored cancel anchor; NULL = canvas-detached (spawn). */
246
+ node_id: string | null;
247
+ /** The node that armed this wake; set on EVERY row. == node_id for a self-alarm. */
248
+ owner_id: string;
249
+ /** Next absolute fire time, ISO 8601 UTC; lexicographic <= compare. */
250
+ fire_at: string;
251
+ kind: WakeKind;
252
+ /** NULL = one-shot/adaptive; else the recur JSON the daemon advances. */
253
+ recur: string | null;
254
+ /** Parsed per-kind payload (NULL for a bare one-shot with no label). */
255
+ payload: WakePayload;
256
+ /** ISO 8601, audit/order. */
257
+ created: string;
258
+ }
259
+ /** The arm-a-wake spec (the pinned shared contract). `wakeup_id` is CALLER-SUPPLIED —
260
+ * the canvas layer never mints ids (importing newNodeId from runtime/nodes.ts would
261
+ * force a canvas → runtime → canvas cycle, Min-1); the command surface mints
262
+ * `wk-${newNodeId()}` and passes it in. `owner_id` is required on every row;
263
+ * `node_id` is NULL for a detached wake (deferred spawn / spawn-cron). */
264
+ export interface ArmWakeSpec {
265
+ wakeup_id: string;
266
+ node_id?: string | null;
267
+ owner_id: string;
268
+ fire_at: string;
269
+ kind: WakeKind;
270
+ recur?: string | null;
271
+ payload?: WakePayload;
272
+ }
@@ -0,0 +1,76 @@
1
+ import type { ArmWakeSpec, Wakeup } from './types.js';
2
+ /** A thrown integrity-backstop error from armWake. `code` is the AC-N3 code the
3
+ * command surface (the ONLY sanctioned caller of armWake) maps to a rendered
4
+ * error block — armWake carries integrity backstops only (empty body / recur-on-
5
+ * deadline / unknown kind); target-resolvability + recoverable-state + the
6
+ * per-owner cap live in the surface (Min-6), so a non-surface armer could insert
7
+ * a dangling wake — every armer MUST route through the surface. */
8
+ export declare class WakeArmError extends Error {
9
+ readonly code: string;
10
+ constructor(code: string, message: string);
11
+ }
12
+ /** Which wakes a listWakes call returns. Distinguished by which key is present
13
+ * (mirrors listNodes' filter shape):
14
+ * - { node } — wakes anchored to this node (node_id = ?).
15
+ * - { owner } — wakes this node ARMED (owner_id = ?); the per-owner cap count.
16
+ * - { subtree } — wakes anchored to any node in this id set (node_id IN (…)).
17
+ * - { due } — wakes whose fire_at <= this ISO instant.
18
+ * - { canvas } — every wake on the canvas. */
19
+ export type WakeScope = {
20
+ node: string;
21
+ } | {
22
+ owner: string;
23
+ } | {
24
+ subtree: string[];
25
+ } | {
26
+ due: string;
27
+ } | {
28
+ canvas: true;
29
+ };
30
+ /** INSERT one wakeup row from the pinned spec, returning its (caller-supplied)
31
+ * `wakeup_id`. JSON.stringifies the typed `payload` union into the TEXT column
32
+ * and stamps `created`.
33
+ *
34
+ * Deadline upsert (design Q3, "≤1 deadline per node"): for `kind === 'deadline'`
35
+ * the node's existing deadline is canceled first, then the new one inserted — the
36
+ * partial unique index `idx_wakeups_deadline` is the schema-level backstop.
37
+ *
38
+ * Integrity backstops (throw WakeArmError; the surface maps the code to AC-N3):
39
+ * - empty/whitespace note `body` for `noted`/`deadline` → `empty_note`.
40
+ * - non-NULL `recur` on a `deadline` → `deadline_cannot_recur`.
41
+ * - unknown `kind` → `bad_kind`.
42
+ * NO target-resolvability / recoverable-state / cap backstop here (Min-6). */
43
+ export declare function armWake(spec: ArmWakeSpec): string;
44
+ /** UPDATE the next fire time — recurrence advancement (the daemon settles a
45
+ * recurring row to its next slot BEFORE enacting, crash-safe; design D4). */
46
+ export declare function advanceWake(wakeup_id: string, nextFireIso: string): void;
47
+ /** DELETE one wakeup by id — one-shot consumption (the daemon settles a one-shot
48
+ * row BEFORE enacting, crash-safe; design D4). */
49
+ export declare function consumeWake(wakeup_id: string): void;
50
+ /** DELETE one wakeup by id — the explicit user/surface cancel. Idempotent:
51
+ * 0 rows ⇒ no error (AC-C2). */
52
+ export declare function cancelWake(wakeup_id: string): void;
53
+ /** DELETE every wake anchored to this node AND every detached wake it ARMED —
54
+ * deliberate close/reap (ruling A). Hooked at transition('cancel'). */
55
+ export declare function cancelWakesFor(node_id: string): void;
56
+ /** DELETE this node's one-shot self-alarms (recur IS NULL); node-anchored crons
57
+ * SURVIVE — finalize is the instance finishing, not a teardown of standing
58
+ * schedules (design Q1). Hooked at transition('finalize'). */
59
+ export declare function cancelSelfAlarms(node_id: string): void;
60
+ /** DELETE this node's pending deadline(s) — cancel-on-wake (design §6.4).
61
+ * Hooked at reviveNode, so the deadline always belongs to the dormancy left. */
62
+ export declare function cancelDeadlinesFor(node_id: string): void;
63
+ /** The daemon's per-tick due query: every row whose fire_at has arrived, in
64
+ * fire_at order, with `payload` parsed into the typed union. Returns exactly ONE
65
+ * row per recurring wake regardless of missed slots — coalescing is structural
66
+ * (AC-E2). */
67
+ export declare function dueWakes(nowIso: string): Wakeup[];
68
+ /** List wakes for a scope (node / owner / subtree / due / canvas), `payload`
69
+ * parsed as in dueWakes. The subtree variant binds the id set with `?`
70
+ * placeholders (mirroring listNodes — the codebase's one safe dynamic-IN spot);
71
+ * ids are NEVER string-concatenated (Min-10). */
72
+ export declare function listWakes(scope: WakeScope): Wakeup[];
73
+ /** True if the node has ANY pending self-anchored wake (of any kind). Consumed by
74
+ * the stop-guard self-wake seam so a no-child poll node releases dormant after
75
+ * arming instead of being nagged to finish (AC-X3/AC-R1). */
76
+ export declare function hasPendingSelfWake(node_id: string): boolean;
@@ -0,0 +1,185 @@
1
+ // wakeups.ts — the WAKEUPS table data-access layer (canvas.db, migration v7).
2
+ //
3
+ // One-module-per-table (the focuses.ts precedent): ALL SQL that touches the
4
+ // `wakeups` table lives HERE and nowhere else — never appended to canvas.ts,
5
+ // which keeps the node/edge model. A WAKEUP is one durable row = "do <kind> at
6
+ // fire_at [every recur]", carrying a fire time, a kind, an optional payload, an
7
+ // optional recurrence cadence, a nullable TARGET node_id, and the owner_id of
8
+ // the node that ARMED it.
9
+ //
10
+ // Two anchors, two reap rules (design D1/D2):
11
+ // - node_id → the revive target AND the node-anchored cancel anchor; it
12
+ // carries the `ON DELETE CASCADE` FK, so a pruned node's node-anchored wakes
13
+ // are reaped FOR FREE in pruneNodes' same `DELETE FROM nodes` (canvas.ts is
14
+ // UNTOUCHED — the cascade does it; there is no pruneNodes wakeups code).
15
+ // NULL = canvas-detached (a deferred spawn / spawn-cron).
16
+ // - owner_id → the armer; a PLAIN indexed column with NO FK, so a crashed-
17
+ // then-pruned armer's detached cron is NOT cascade-dropped (Invariant E).
18
+ // Reaped only by the explicit cancelWakesFor DELETE on deliberate close.
19
+ //
20
+ // Each function is a single-statement query mirroring the canvas.ts atomic-setter
21
+ // pattern (setStatus/setIntent/setPresence) — no read-modify-write, serialized by
22
+ // WAL. The data-access layer OWNS payload serialization: armWake JSON.stringifies
23
+ // the typed per-kind union on write; dueWakes/listWakes JSON.parse it back on read
24
+ // (Maj-1), so callers consume `payload.body` / `payload as SpawnChildOpts` directly.
25
+ //
26
+ // `wakeup_id` is CALLER-SUPPLIED (the command surface mints `wk-${newNodeId()}`):
27
+ // the canvas layer never imports newNodeId from runtime/nodes.ts — that would
28
+ // force a canvas → runtime → canvas import cycle (Min-1).
29
+ import { openDb } from './db.js';
30
+ /** A thrown integrity-backstop error from armWake. `code` is the AC-N3 code the
31
+ * command surface (the ONLY sanctioned caller of armWake) maps to a rendered
32
+ * error block — armWake carries integrity backstops only (empty body / recur-on-
33
+ * deadline / unknown kind); target-resolvability + recoverable-state + the
34
+ * per-owner cap live in the surface (Min-6), so a non-surface armer could insert
35
+ * a dangling wake — every armer MUST route through the surface. */
36
+ export class WakeArmError extends Error {
37
+ code;
38
+ constructor(code, message) {
39
+ super(message);
40
+ this.code = code;
41
+ this.name = 'WakeArmError';
42
+ }
43
+ }
44
+ /** Hydrate a raw row into a Wakeup, JSON.parsing `payload` back into the typed
45
+ * per-kind union (NULL column → null). */
46
+ function wakeupFrom(r) {
47
+ const raw = r['payload'];
48
+ return {
49
+ wakeup_id: r['wakeup_id'],
50
+ node_id: r['node_id'] ?? null,
51
+ owner_id: r['owner_id'],
52
+ fire_at: r['fire_at'],
53
+ kind: r['kind'],
54
+ recur: r['recur'] ?? null,
55
+ payload: raw == null ? null : JSON.parse(raw),
56
+ created: r['created'],
57
+ };
58
+ }
59
+ // ---------------------------------------------------------------------------
60
+ // Writes — each a single-statement INSERT/UPDATE/DELETE.
61
+ // ---------------------------------------------------------------------------
62
+ /** INSERT one wakeup row from the pinned spec, returning its (caller-supplied)
63
+ * `wakeup_id`. JSON.stringifies the typed `payload` union into the TEXT column
64
+ * and stamps `created`.
65
+ *
66
+ * Deadline upsert (design Q3, "≤1 deadline per node"): for `kind === 'deadline'`
67
+ * the node's existing deadline is canceled first, then the new one inserted — the
68
+ * partial unique index `idx_wakeups_deadline` is the schema-level backstop.
69
+ *
70
+ * Integrity backstops (throw WakeArmError; the surface maps the code to AC-N3):
71
+ * - empty/whitespace note `body` for `noted`/`deadline` → `empty_note`.
72
+ * - non-NULL `recur` on a `deadline` → `deadline_cannot_recur`.
73
+ * - unknown `kind` → `bad_kind`.
74
+ * NO target-resolvability / recoverable-state / cap backstop here (Min-6). */
75
+ export function armWake(spec) {
76
+ const { kind } = spec;
77
+ if (kind !== 'bare' && kind !== 'noted' && kind !== 'deadline' && kind !== 'spawn') {
78
+ throw new WakeArmError('bad_kind', `unknown wake kind: ${String(kind)}`);
79
+ }
80
+ if (kind === 'noted' || kind === 'deadline') {
81
+ const body = spec.payload?.body;
82
+ if (typeof body !== 'string' || body.trim() === '') {
83
+ throw new WakeArmError('empty_note', `a ${kind} wake requires a non-empty note body.`);
84
+ }
85
+ }
86
+ if (kind === 'deadline' && spec.recur != null) {
87
+ throw new WakeArmError('deadline_cannot_recur', 'a deadline wake cannot recur.');
88
+ }
89
+ // Deadline upsert: clear the node's existing deadline before inserting the new one.
90
+ if (kind === 'deadline' && spec.node_id != null) {
91
+ cancelDeadlinesFor(spec.node_id);
92
+ }
93
+ const payloadText = spec.payload == null ? null : JSON.stringify(spec.payload);
94
+ openDb()
95
+ .prepare(`INSERT INTO wakeups (wakeup_id, node_id, owner_id, fire_at, kind, recur, payload, created)
96
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
97
+ .run(spec.wakeup_id, spec.node_id ?? null, spec.owner_id, spec.fire_at, kind, spec.recur ?? null, payloadText, new Date().toISOString());
98
+ return spec.wakeup_id;
99
+ }
100
+ /** UPDATE the next fire time — recurrence advancement (the daemon settles a
101
+ * recurring row to its next slot BEFORE enacting, crash-safe; design D4). */
102
+ export function advanceWake(wakeup_id, nextFireIso) {
103
+ openDb().prepare('UPDATE wakeups SET fire_at = ? WHERE wakeup_id = ?').run(nextFireIso, wakeup_id);
104
+ }
105
+ /** DELETE one wakeup by id — one-shot consumption (the daemon settles a one-shot
106
+ * row BEFORE enacting, crash-safe; design D4). */
107
+ export function consumeWake(wakeup_id) {
108
+ openDb().prepare('DELETE FROM wakeups WHERE wakeup_id = ?').run(wakeup_id);
109
+ }
110
+ /** DELETE one wakeup by id — the explicit user/surface cancel. Idempotent:
111
+ * 0 rows ⇒ no error (AC-C2). */
112
+ export function cancelWake(wakeup_id) {
113
+ openDb().prepare('DELETE FROM wakeups WHERE wakeup_id = ?').run(wakeup_id);
114
+ }
115
+ /** DELETE every wake anchored to this node AND every detached wake it ARMED —
116
+ * deliberate close/reap (ruling A). Hooked at transition('cancel'). */
117
+ export function cancelWakesFor(node_id) {
118
+ openDb()
119
+ .prepare('DELETE FROM wakeups WHERE node_id = ? OR owner_id = ?')
120
+ .run(node_id, node_id);
121
+ }
122
+ /** DELETE this node's one-shot self-alarms (recur IS NULL); node-anchored crons
123
+ * SURVIVE — finalize is the instance finishing, not a teardown of standing
124
+ * schedules (design Q1). Hooked at transition('finalize'). */
125
+ export function cancelSelfAlarms(node_id) {
126
+ openDb().prepare('DELETE FROM wakeups WHERE node_id = ? AND recur IS NULL').run(node_id);
127
+ }
128
+ /** DELETE this node's pending deadline(s) — cancel-on-wake (design §6.4).
129
+ * Hooked at reviveNode, so the deadline always belongs to the dormancy left. */
130
+ export function cancelDeadlinesFor(node_id) {
131
+ openDb().prepare("DELETE FROM wakeups WHERE node_id = ? AND kind = 'deadline'").run(node_id);
132
+ }
133
+ // ---------------------------------------------------------------------------
134
+ // Reads.
135
+ // ---------------------------------------------------------------------------
136
+ /** The daemon's per-tick due query: every row whose fire_at has arrived, in
137
+ * fire_at order, with `payload` parsed into the typed union. Returns exactly ONE
138
+ * row per recurring wake regardless of missed slots — coalescing is structural
139
+ * (AC-E2). */
140
+ export function dueWakes(nowIso) {
141
+ return openDb()
142
+ .prepare('SELECT * FROM wakeups WHERE fire_at <= ? ORDER BY fire_at')
143
+ .all(nowIso).map(wakeupFrom);
144
+ }
145
+ /** List wakes for a scope (node / owner / subtree / due / canvas), `payload`
146
+ * parsed as in dueWakes. The subtree variant binds the id set with `?`
147
+ * placeholders (mirroring listNodes — the codebase's one safe dynamic-IN spot);
148
+ * ids are NEVER string-concatenated (Min-10). */
149
+ export function listWakes(scope) {
150
+ const db = openDb();
151
+ let rows;
152
+ if ('node' in scope) {
153
+ rows = db
154
+ .prepare('SELECT * FROM wakeups WHERE node_id = ? ORDER BY fire_at')
155
+ .all(scope.node);
156
+ }
157
+ else if ('owner' in scope) {
158
+ rows = db
159
+ .prepare('SELECT * FROM wakeups WHERE owner_id = ? ORDER BY fire_at')
160
+ .all(scope.owner);
161
+ }
162
+ else if ('subtree' in scope) {
163
+ if (scope.subtree.length === 0)
164
+ return [];
165
+ const placeholders = scope.subtree.map(() => '?').join(',');
166
+ rows = db
167
+ .prepare(`SELECT * FROM wakeups WHERE node_id IN (${placeholders}) ORDER BY fire_at`)
168
+ .all(...scope.subtree);
169
+ }
170
+ else if ('due' in scope) {
171
+ rows = db
172
+ .prepare('SELECT * FROM wakeups WHERE fire_at <= ? ORDER BY fire_at')
173
+ .all(scope.due);
174
+ }
175
+ else {
176
+ rows = db.prepare('SELECT * FROM wakeups ORDER BY fire_at').all();
177
+ }
178
+ return rows.map(wakeupFrom);
179
+ }
180
+ /** True if the node has ANY pending self-anchored wake (of any kind). Consumed by
181
+ * the stop-guard self-wake seam so a no-child poll node releases dormant after
182
+ * arming instead of being nagged to finish (AC-X3/AC-R1). */
183
+ export function hasPendingSelfWake(node_id) {
184
+ return (openDb().prepare('SELECT 1 FROM wakeups WHERE node_id = ? LIMIT 1').get(node_id) !== undefined);
185
+ }
@@ -137,7 +137,10 @@ function mergeConfig(partial) {
137
137
  ? Math.floor(rawMaxPanes)
138
138
  : defaults.max_panes_per_window;
139
139
  const canvasNav = mergeCanvasNav(partial.canvasNav);
140
- return { schema_version, marketplaces, plugins, skills, auto_update, max_panes_per_window, canvasNav };
140
+ // The merge drops unknown keys, so `headless` must be carried explicitly or it
141
+ // is lost on every read-modify-write of config.json.
142
+ const headless = typeof partial.headless === 'boolean' ? partial.headless : defaults.headless;
143
+ return { schema_version, marketplaces, plugins, skills, auto_update, max_panes_per_window, canvasNav, headless };
141
144
  }
142
145
  export function updateConfig(scope, mutate) {
143
146
  const cfg = readConfig(scope);