gentle-pi 4.8.0 → 4.9.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 (370) hide show
  1. package/README.md +60 -0
  2. package/assets/agents/gentle-ai-explore.md +1 -1
  3. package/assets/agents/gentle-ai-verify.md +1 -1
  4. package/assets/agents/gentle-ai-worker.md +10 -9
  5. package/assets/design-canvas/canvas-boards.js +22 -2
  6. package/assets/design-canvas/canvas-boot.js +3 -1
  7. package/assets/design-canvas/canvas-i18n-es.js +5 -0
  8. package/assets/design-canvas/canvas-menu.js +13 -7
  9. package/assets/design-canvas/canvas-messages.js +1 -1
  10. package/assets/design-canvas/canvas-rail.js +5 -2
  11. package/assets/design-canvas/canvas-store.js +3 -0
  12. package/assets/design-canvas/canvas-theme.js +69 -0
  13. package/assets/design-canvas/canvas.css +27 -2
  14. package/assets/design-canvas/index.html +3 -0
  15. package/assets/design-canvas/inspector.js +28 -5
  16. package/assets/design-canvas/theme-early.js +10 -0
  17. package/assets/migrations/agent-body-history.json +6 -0
  18. package/assets/orchestrator-delegation.md +1 -5
  19. package/assets/orchestrator-full-delegation-card.md +9 -13
  20. package/assets/orchestrator-full-delegation.md +2 -2
  21. package/assets/orchestrator-memory.md +2 -2
  22. package/assets/orchestrator-prompts.md +3 -31
  23. package/assets/orchestrator-tracking.md +6 -6
  24. package/assets/orchestrator-verification.md +4 -4
  25. package/assets/orchestrator-writer.md +12 -12
  26. package/assets/orchestrator.md +2 -2
  27. package/bin/gentle-shell.mjs +77 -1
  28. package/contracts/review-provider-contract-mirror/provider-contract.lock.json +7 -7
  29. package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/manifest.json +3 -3
  30. package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/orchestration/pi.md +1 -1
  31. package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/routing/pi.md +10 -10
  32. package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/generated/provider-capabilities.baseline.json +3 -3
  33. package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/generated/provider-roles.baseline.json +1 -1
  34. package/docs/assets/features/gentle-tower.png +0 -0
  35. package/docs/auto-permission-answers.md +1 -1
  36. package/docs/bundled-install.md +1 -1
  37. package/docs/delegated-verification.md +6 -4
  38. package/docs/desktop-feed.md +303 -0
  39. package/docs/desktop-rpc.md +703 -25
  40. package/docs/fast-mode.md +2 -2
  41. package/docs/gentle-shell-desktop.md +465 -0
  42. package/docs/gentle-shell.md +65 -3
  43. package/docs/images/desktop/chat.png +0 -0
  44. package/docs/images/desktop/palette.png +0 -0
  45. package/docs/images/desktop/todo.png +0 -0
  46. package/docs/install-wizard.md +1 -1
  47. package/docs/odd-preparation-continuity.md +1 -1
  48. package/docs/readme-reference.md +44 -15
  49. package/docs/work-continuity.md +10 -7
  50. package/extensions/ask-user-choice.ts +36 -18
  51. package/extensions/ask-user-question.ts +47 -8
  52. package/extensions/gentle-agents.ts +206 -37
  53. package/extensions/gentle-ai.ts +234 -24
  54. package/extensions/gentle-desktop-feed.ts +778 -0
  55. package/extensions/gentle-desktop-rpc.ts +145 -12
  56. package/extensions/gentle-herdr-desktop-status.ts +275 -0
  57. package/extensions/gentle-shell.ts +69 -7
  58. package/extensions/gentle-stats.ts +30 -1
  59. package/extensions/gentle-tower.ts +305 -0
  60. package/extensions/skill-registry.ts +13 -3
  61. package/herdr/RECIPE.md +157 -0
  62. package/herdr/keys.json +14 -0
  63. package/herdr/layouts/gentle-shell.json +202 -0
  64. package/herdr/mods/gentle-agents/icons/kind-herdr.svg +1 -0
  65. package/herdr/mods/gentle-agents/icons/kind-remote.svg +1 -0
  66. package/herdr/mods/gentle-agents/icons/kind-tui.svg +1 -0
  67. package/herdr/mods/gentle-agents/main.js +190 -0
  68. package/herdr/mods/gentle-agents/mod.json +1 -0
  69. package/herdr/mods/gentle-agents/snapshot.js +469 -0
  70. package/herdr/mods/gentle-agents/tokens.js +273 -0
  71. package/herdr/mods/gentle-chat/MINIMAL.md +69 -0
  72. package/herdr/mods/gentle-chat/actions.js +180 -0
  73. package/herdr/mods/gentle-chat/feed.js +321 -0
  74. package/herdr/mods/gentle-chat/icons/branch.svg +1 -0
  75. package/herdr/mods/gentle-chat/icons/chat.svg +1 -0
  76. package/herdr/mods/gentle-chat/icons/check.svg +1 -0
  77. package/herdr/mods/gentle-chat/icons/chev.svg +1 -0
  78. package/herdr/mods/gentle-chat/icons/clip.svg +1 -0
  79. package/herdr/mods/gentle-chat/icons/down.svg +1 -0
  80. package/herdr/mods/gentle-chat/icons/folder.svg +1 -0
  81. package/herdr/mods/gentle-chat/icons/kind-herdr.svg +1 -0
  82. package/herdr/mods/gentle-chat/icons/kind-remote.svg +1 -0
  83. package/herdr/mods/gentle-chat/icons/kind-tui.svg +1 -0
  84. package/herdr/mods/gentle-chat/icons/mark.svg +1 -0
  85. package/herdr/mods/gentle-chat/icons/send.svg +1 -0
  86. package/herdr/mods/gentle-chat/icons/shield.svg +1 -0
  87. package/herdr/mods/gentle-chat/icons/stop.svg +1 -0
  88. package/herdr/mods/gentle-chat/icons/term.svg +1 -0
  89. package/herdr/mods/gentle-chat/icons/user.svg +1 -0
  90. package/herdr/mods/gentle-chat/icons/x.svg +1 -0
  91. package/herdr/mods/gentle-chat/main.js +968 -0
  92. package/herdr/mods/gentle-chat/mod.json +1 -0
  93. package/herdr/mods/gentle-chat/session.js +212 -0
  94. package/herdr/mods/gentle-chat/snapshot.js +469 -0
  95. package/herdr/mods/gentle-chat/tokens.js +137 -0
  96. package/herdr/mods/gentle-chat/ui.js +883 -0
  97. package/herdr/mods/gentle-confirm/main.js +60 -0
  98. package/herdr/mods/gentle-confirm/mod.json +1 -0
  99. package/herdr/mods/gentle-inspector/actions.js +180 -0
  100. package/herdr/mods/gentle-inspector/icons/bolt.svg +1 -0
  101. package/herdr/mods/gentle-inspector/icons/bot.svg +1 -0
  102. package/herdr/mods/gentle-inspector/icons/check.svg +1 -0
  103. package/herdr/mods/gentle-inspector/icons/db.svg +1 -0
  104. package/herdr/mods/gentle-inspector/icons/diff.svg +1 -0
  105. package/herdr/mods/gentle-inspector/icons/jobs.svg +1 -0
  106. package/herdr/mods/gentle-inspector/icons/plug.svg +1 -0
  107. package/herdr/mods/gentle-inspector/icons/review.svg +1 -0
  108. package/herdr/mods/gentle-inspector/icons/stats.svg +1 -0
  109. package/herdr/mods/gentle-inspector/icons/stop.svg +1 -0
  110. package/herdr/mods/gentle-inspector/icons/todo.svg +1 -0
  111. package/herdr/mods/gentle-inspector/main.js +500 -0
  112. package/herdr/mods/gentle-inspector/mod.json +1 -0
  113. package/herdr/mods/gentle-inspector/snapshot.js +469 -0
  114. package/herdr/mods/gentle-inspector/tokens.js +273 -0
  115. package/herdr/mods/gentle-palette/actions.js +180 -0
  116. package/herdr/mods/gentle-palette/icons/bolt.svg +1 -0
  117. package/herdr/mods/gentle-palette/icons/bot.svg +1 -0
  118. package/herdr/mods/gentle-palette/icons/chat.svg +1 -0
  119. package/herdr/mods/gentle-palette/icons/check.svg +1 -0
  120. package/herdr/mods/gentle-palette/icons/cmd.svg +1 -0
  121. package/herdr/mods/gentle-palette/icons/diff.svg +1 -0
  122. package/herdr/mods/gentle-palette/icons/jobs.svg +1 -0
  123. package/herdr/mods/gentle-palette/icons/plus.svg +1 -0
  124. package/herdr/mods/gentle-palette/icons/review.svg +1 -0
  125. package/herdr/mods/gentle-palette/icons/search.svg +1 -0
  126. package/herdr/mods/gentle-palette/icons/shield.svg +1 -0
  127. package/herdr/mods/gentle-palette/icons/stats.svg +1 -0
  128. package/herdr/mods/gentle-palette/icons/stop.svg +1 -0
  129. package/herdr/mods/gentle-palette/icons/term.svg +1 -0
  130. package/herdr/mods/gentle-palette/icons/todo.svg +1 -0
  131. package/herdr/mods/gentle-palette/main.js +333 -0
  132. package/herdr/mods/gentle-palette/mod.json +1 -0
  133. package/herdr/mods/gentle-palette/snapshot.js +469 -0
  134. package/herdr/mods/gentle-palette/tokens.js +273 -0
  135. package/herdr/mods/gentle-pane/icons/chat.svg +1 -0
  136. package/herdr/mods/gentle-pane/icons/close.svg +1 -0
  137. package/herdr/mods/gentle-pane/icons/kind-herdr.svg +1 -0
  138. package/herdr/mods/gentle-pane/icons/kind-remote.svg +1 -0
  139. package/herdr/mods/gentle-pane/icons/kind-tui.svg +1 -0
  140. package/herdr/mods/gentle-pane/icons/split-down.svg +1 -0
  141. package/herdr/mods/gentle-pane/icons/split-right.svg +1 -0
  142. package/herdr/mods/gentle-pane/icons/term.svg +1 -0
  143. package/herdr/mods/gentle-pane/icons/unzoom.svg +1 -0
  144. package/herdr/mods/gentle-pane/icons/zoom.svg +1 -0
  145. package/herdr/mods/gentle-pane/main.js +91 -0
  146. package/herdr/mods/gentle-pane/mod.json +1 -0
  147. package/herdr/mods/gentle-pane/snapshot.js +469 -0
  148. package/herdr/mods/gentle-pane/tokens.js +273 -0
  149. package/herdr/mods/gentle-sidebar/actions.js +180 -0
  150. package/herdr/mods/gentle-sidebar/icons/gear.svg +1 -0
  151. package/herdr/mods/gentle-sidebar/icons/mark.svg +1 -0
  152. package/herdr/mods/gentle-sidebar/icons/refresh.svg +1 -0
  153. package/herdr/mods/gentle-sidebar/icons/server-plus.svg +1 -0
  154. package/herdr/mods/gentle-sidebar/main.js +171 -0
  155. package/herdr/mods/gentle-sidebar/mod.json +1 -0
  156. package/herdr/mods/gentle-sidebar/snapshot.js +469 -0
  157. package/herdr/mods/gentle-sidebar/tokens.js +273 -0
  158. package/herdr/mods/gentle-spaces/icons/kind-herdr.svg +1 -0
  159. package/herdr/mods/gentle-spaces/icons/kind-remote.svg +1 -0
  160. package/herdr/mods/gentle-spaces/icons/kind-tui.svg +1 -0
  161. package/herdr/mods/gentle-spaces/icons/monitor.svg +1 -0
  162. package/herdr/mods/gentle-spaces/icons/plus.svg +1 -0
  163. package/herdr/mods/gentle-spaces/icons/server.svg +1 -0
  164. package/herdr/mods/gentle-spaces/machines.js +31 -0
  165. package/herdr/mods/gentle-spaces/main.js +299 -0
  166. package/herdr/mods/gentle-spaces/mod.json +1 -0
  167. package/herdr/mods/gentle-spaces/snapshot.js +469 -0
  168. package/herdr/mods/gentle-spaces/tokens.js +273 -0
  169. package/herdr/mods/gentle-status/actions.js +180 -0
  170. package/herdr/mods/gentle-status/main.js +192 -0
  171. package/herdr/mods/gentle-status/mod.json +1 -0
  172. package/herdr/mods/gentle-status/snapshot.js +469 -0
  173. package/herdr/mods/gentle-status/tokens.js +273 -0
  174. package/herdr/mods/gentle-tabs/icons/close.svg +1 -0
  175. package/herdr/mods/gentle-tabs/icons/kind-herdr.svg +1 -0
  176. package/herdr/mods/gentle-tabs/icons/kind-remote.svg +1 -0
  177. package/herdr/mods/gentle-tabs/icons/kind-tui.svg +1 -0
  178. package/herdr/mods/gentle-tabs/icons/plus.svg +1 -0
  179. package/herdr/mods/gentle-tabs/icons/win-close.svg +1 -0
  180. package/herdr/mods/gentle-tabs/icons/win-max.svg +1 -0
  181. package/herdr/mods/gentle-tabs/icons/win-min.svg +1 -0
  182. package/herdr/mods/gentle-tabs/icons/win-restore.svg +1 -0
  183. package/herdr/mods/gentle-tabs/main.js +171 -0
  184. package/herdr/mods/gentle-tabs/mod.json +1 -0
  185. package/herdr/mods/gentle-tabs/snapshot.js +469 -0
  186. package/herdr/mods/gentle-tabs/tokens.js +273 -0
  187. package/herdr/mods/gentle-tokens/main.js +20 -0
  188. package/herdr/mods/gentle-tokens/mod.json +1 -0
  189. package/herdr/mods/gentle-tokens/snapshot.js +469 -0
  190. package/herdr/mods/gentle-tokens/tokens.js +273 -0
  191. package/herdr/preview.png +0 -0
  192. package/herdr/themes/gentle-shell-dark.json +33 -0
  193. package/herdr/themes/gentle-shell-light.json +34 -0
  194. package/lib/agents-protocol.ts +2 -0
  195. package/lib/agents-rpc-publisher.ts +39 -12
  196. package/lib/agents-runner.ts +1 -0
  197. package/lib/auto-permission-answers.ts +2 -1
  198. package/lib/background-jobs.ts +22 -2
  199. package/lib/bounded-writer-admission.ts +1 -1
  200. package/lib/change-diff.ts +104 -0
  201. package/lib/commit-slice-signal.ts +1 -1
  202. package/lib/design-canvas-assets.ts +2 -0
  203. package/lib/desktop-agents-service.ts +31 -10
  204. package/lib/desktop-changes-service.ts +171 -0
  205. package/lib/desktop-feed-actions.ts +804 -0
  206. package/lib/desktop-feed-answers.ts +129 -0
  207. package/lib/desktop-feed-sources.ts +184 -0
  208. package/lib/desktop-feed-stats.ts +98 -0
  209. package/lib/desktop-feed-store.ts +124 -0
  210. package/lib/desktop-feed.ts +788 -0
  211. package/lib/desktop-jobs-service.ts +34 -7
  212. package/lib/desktop-review-service.ts +187 -0
  213. package/lib/desktop-rpc-agents.ts +112 -3
  214. package/lib/desktop-rpc-changes.ts +223 -0
  215. package/lib/desktop-rpc-config-watch.ts +285 -0
  216. package/lib/desktop-rpc-config.ts +162 -8
  217. package/lib/desktop-rpc-jobs.ts +118 -9
  218. package/lib/desktop-rpc-permissions.ts +154 -3
  219. package/lib/desktop-rpc-review.ts +180 -0
  220. package/lib/desktop-rpc-session.ts +203 -0
  221. package/lib/desktop-rpc-sessions.ts +250 -0
  222. package/lib/desktop-rpc-stats.ts +187 -0
  223. package/lib/desktop-rpc-updates.ts +143 -0
  224. package/lib/desktop-rpc.ts +116 -5
  225. package/lib/desktop-session-service.ts +223 -0
  226. package/lib/desktop-sessions-service.ts +249 -0
  227. package/lib/desktop-stats-service.ts +142 -0
  228. package/lib/desktop-updates-service.ts +268 -0
  229. package/lib/gentle-shell-desktop.ts +1370 -0
  230. package/lib/gentle-shell-launcher.ts +45 -3
  231. package/lib/herdr-desktop-events.ts +40 -0
  232. package/lib/herdr-desktop-status.ts +229 -0
  233. package/lib/jobs-sidebar-state.ts +5 -0
  234. package/lib/native-review-cli.ts +7 -0
  235. package/lib/openai-fast-tier.ts +1 -0
  236. package/lib/session-profile-freeze.ts +13 -5
  237. package/lib/stats-collector.ts +56 -27
  238. package/lib/tower-actions.ts +100 -0
  239. package/lib/tower-hub.ts +73 -0
  240. package/lib/tower-launch.ts +111 -0
  241. package/lib/tower-open.ts +99 -0
  242. package/lib/tower-presence.ts +70 -0
  243. package/lib/tower-server.ts +234 -0
  244. package/lib/tower-snapshot.ts +298 -0
  245. package/lib/update-status.ts +238 -0
  246. package/lib/verifier-settle-reminder.ts +1 -1
  247. package/package.json +13 -4
  248. package/runtime/design-canvas-assets.mjs +2 -0
  249. package/runtime/gentle-shell-desktop.mjs +1371 -0
  250. package/runtime/gentle-shell-launcher.mjs +44 -2
  251. package/runtime/native-review-cli.mjs +7 -0
  252. package/runtime/tower-actions.mjs +101 -0
  253. package/runtime/tower-hub.mjs +74 -0
  254. package/runtime/tower-launch.mjs +112 -0
  255. package/runtime/tower-open.mjs +100 -0
  256. package/runtime/tower-presence.mjs +71 -0
  257. package/runtime/tower-server.mjs +234 -0
  258. package/runtime/tower-snapshot.mjs +299 -0
  259. package/scripts/build-full-delegation-card.mjs +1 -21
  260. package/scripts/build-runtime-modules.mjs +9 -0
  261. package/scripts/gentle-ai-installer.mjs +10 -10
  262. package/scripts/verify-package-files.mjs +35 -15
  263. package/skills/provider-defect-report/SKILL.md +61 -0
  264. package/skills/rdd-defect-workflow/SKILL.md +3 -3
  265. package/tests/agent-prompt-diet-contract.test.ts +2 -2
  266. package/tests/append-system-prompt-route.test.ts +16 -5
  267. package/tests/ask-user-choice.test.ts +78 -21
  268. package/tests/ask-user-question.test.ts +59 -5
  269. package/tests/auto-permission-answers.test.ts +12 -1
  270. package/tests/background-jobs.test.ts +12 -0
  271. package/tests/bounded-stop-contract.test.ts +139 -26
  272. package/tests/change-diff.test.ts +75 -0
  273. package/tests/commit-slice-signal.test.ts +3 -2
  274. package/tests/decision-routing-contract.test.ts +390 -0
  275. package/tests/delegated-key-learnings-contract.test.ts +21 -28
  276. package/tests/delivery-b-rendered-surfaces.test.ts +32 -0
  277. package/tests/design-canvas-i18n.test.ts +24 -22
  278. package/tests/design-canvas-inspector.test.ts +67 -0
  279. package/tests/design-canvas-modules.test.ts +3 -2
  280. package/tests/design-canvas-rail.test.ts +10 -10
  281. package/tests/design-canvas-store.test.ts +9 -0
  282. package/tests/design-canvas-theme.test.ts +198 -0
  283. package/tests/design-canvas-tokens.test.ts +1 -1
  284. package/tests/desktop-feed-actions-guards.test.ts +595 -0
  285. package/tests/desktop-feed-actions.test.ts +761 -0
  286. package/tests/desktop-feed-live-work.test.ts +209 -0
  287. package/tests/desktop-feed-performance.test.ts +369 -0
  288. package/tests/desktop-feed-plan-profiles.test.ts +509 -0
  289. package/tests/desktop-feed-send-prompt.test.ts +303 -0
  290. package/tests/desktop-feed-tool-summaries.test.ts +69 -0
  291. package/tests/desktop-feed.test.ts +615 -0
  292. package/tests/desktop-rpc-agents.test.ts +428 -2
  293. package/tests/desktop-rpc-changes.test.ts +608 -0
  294. package/tests/desktop-rpc-config-watch.test.ts +1159 -0
  295. package/tests/desktop-rpc-config.test.ts +11 -1
  296. package/tests/desktop-rpc-design.test.ts +11 -1
  297. package/tests/desktop-rpc-jobs.test.ts +402 -5
  298. package/tests/desktop-rpc-permissions.test.ts +11 -1
  299. package/tests/desktop-rpc-review.test.ts +678 -0
  300. package/tests/desktop-rpc-session.test.ts +642 -0
  301. package/tests/desktop-rpc-sessions.test.ts +647 -0
  302. package/tests/desktop-rpc-stats.test.ts +431 -0
  303. package/tests/desktop-rpc-updates.test.ts +525 -0
  304. package/tests/desktop-rpc-usage.test.ts +11 -1
  305. package/tests/desktop-rpc.test.ts +43 -2
  306. package/tests/fixtures/primary-session-prompt/gentleman.golden.md +11 -13
  307. package/tests/fixtures/session-profile-v1/replay.ts +4 -1
  308. package/tests/full-delegation-closure.test.ts +20 -1
  309. package/tests/gentle-agents.test.ts +519 -4
  310. package/tests/gentle-ai-binary.test.ts +1 -1
  311. package/tests/gentle-ai-installer.test.ts +52 -52
  312. package/tests/gentle-ai.test.ts +75 -16
  313. package/tests/gentle-fast.test.ts +15 -0
  314. package/tests/gentle-herdr-desktop-status.test.ts +595 -0
  315. package/tests/gentle-shell-desktop-bin.test.ts +125 -0
  316. package/tests/gentle-shell-desktop.test.ts +927 -0
  317. package/tests/gentle-shell-launcher-tower.test.ts +104 -0
  318. package/tests/gentle-shell.test.ts +3 -0
  319. package/tests/gentle-tower.test.ts +578 -0
  320. package/tests/herdr-desktop-status.test.ts +285 -0
  321. package/tests/independent-recovery-delivery.test.ts +16 -4
  322. package/tests/installer-server.test.ts +2 -2
  323. package/tests/lazy-module-contradictions-contract.test.ts +27 -4
  324. package/tests/maintainer/desktop-rpc.maintest.ts +279 -4
  325. package/tests/native-review-capability-contract.test.ts +9 -1
  326. package/tests/no-self-pause-contract.test.ts +23 -13
  327. package/tests/odd-routing-canonical-ratchet.test.ts +10 -0
  328. package/tests/odd-routing-contract.test.ts +19 -3
  329. package/tests/orchestrator-budget.test.ts +3 -1
  330. package/tests/orchestrator-prompt-budget.test.ts +2 -1
  331. package/tests/orchestrator-prompt-diet-contract.test.ts +31 -3
  332. package/tests/package-manifest.test.ts +10 -10
  333. package/tests/pi-routing-measured-values.test.ts +24 -0
  334. package/tests/primary-session-prompt.test.ts +25 -0
  335. package/tests/profile-fast-tier.test.ts +66 -1
  336. package/tests/provider-defect-handoff.test.ts +118 -100
  337. package/tests/review-contract-prompt.test.ts +2 -1
  338. package/tests/review-due-public-boundary.test.ts +2 -1
  339. package/tests/sensitive-path-tools.test.ts +138 -4
  340. package/tests/session-profile-extension.test.ts +310 -16
  341. package/tests/session-profile-freeze.test.ts +26 -0
  342. package/tests/session-profile-v1-compat.test.ts +20 -0
  343. package/tests/skill-collision-prefixes.test.ts +1 -0
  344. package/tests/skill-contradictions-contract.test.ts +5 -3
  345. package/tests/skill-registry.test.ts +54 -0
  346. package/tests/stats-collector.test.ts +109 -2
  347. package/tests/support/design-canvas-dom.ts +17 -0
  348. package/tests/task-size-routing-contract.test.ts +7 -3
  349. package/tests/tower-actions.test.ts +247 -0
  350. package/tests/tower-artifacts.test.ts +114 -0
  351. package/tests/tower-open.test.ts +97 -0
  352. package/tests/tower-presence.test.ts +175 -0
  353. package/tests/tower-server.test.ts +122 -0
  354. package/tests/tower-snapshot.test.ts +236 -0
  355. package/tests/tower-web-echo.test.ts +125 -0
  356. package/tests/tower-web-hover.test.ts +251 -0
  357. package/tests/verification-scoped-stop-contract.test.ts +32 -0
  358. package/tests/verification-tiers-contract.test.ts +5 -5
  359. package/tests/verifier-settle-reminder.test.ts +2 -2
  360. package/tests/verify-first-launch-contract.test.ts +2 -2
  361. package/tests/work-continuity-contract.test.ts +83 -17
  362. package/tests/writer-rejection-next-step.test.ts +55 -0
  363. package/tests/writer-surface-authority-contract.test.ts +158 -0
  364. /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/README.md +0 -0
  365. /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/schemas/lens.schema.json +0 -0
  366. /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/schemas/refuter.schema.json +0 -0
  367. /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/schemas/targeted-validator.schema.json +0 -0
  368. /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/vectors/lens.json +0 -0
  369. /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/vectors/refuter.json +0 -0
  370. /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/vectors/targeted-validator.json +0 -0
@@ -0,0 +1,469 @@
1
+ // Reads the Gentle Shell desktop snapshots (T4, docs/desktop-feed.md) and follows the focused
2
+ // Gentle Shell terminal, the same way the centre chat (herdr/mods/gentle-chat) does.
3
+ //
4
+ // CANONICAL COPY, carried byte-identical as `./snapshot.js` by every gentle-* mod that shows
5
+ // snapshot data, gentle-chat included (gpui-shell refuses imports outside a mod's folder).
6
+ // `targetTerminal` mirrors GentleChat.target() so every region shows one session.
7
+ //
8
+ // Gentle Shell runs bundled (agent dir ~/.gentle-shell/agent), as the npm / Pi extension
9
+ // (~/.pi/agent) or with its own home (GENTLE_PI_AGENT_HOME / PI_CODING_AGENT_DIR). Snapshots are
10
+ // read from every agent dir in `agentRoots()`; each session's own `identity.agentDir` is where its
11
+ // actions go (actions.js).
12
+ //
13
+ // EVERY SESSION (T11, S10): the window shows every live Gentle Shell session on the machine,
14
+ // wherever it runs (Herdr app, the Herdr TUI, Ghostty, any terminal). A session is a live snapshot
15
+ // (its publisher's pid still runs) with the Herdr terminal that runs it when one matches
16
+ // (`allSessions`), or a Gentle Shell terminal with no snapshot (an older Gentle Shell). The session
17
+ // the window is about (`targetSession`) follows Herdr's focus, unless a session that runs outside
18
+ // Herdr app was picked in a list (`selectSession`): every mod then shows that one, until Herdr's
19
+ // focus moves to another terminal.
20
+ //
21
+ // ONE READER (S13, herdr/PERF.md): only the leader's FeedSource (gentle-status, always mounted in
22
+ // the gentle-shell layout: `start(cx, null, every, { lead: true })`) watches the snapshot folders,
23
+ // reads them and runs `ps`; it publishes what it read in shared state only when the digest
24
+ // changes (`FEEDS_KEY`, then `FEEDS_DIGEST_KEY`). Every other FeedSource follows: no timer, no
25
+ // watch, no `ps`; its `feeds` are the published ones, and setting shared state re-renders every
26
+ // mod. A copy in a layout without the leader reads for itself, as before, and publishes nothing.
27
+
28
+ import { readFile, readdir } from "fs/promises";
29
+ import { agents, terminals, panes, focused, home, layout, watchFolder, exec, run, shared, setShared } from "herdr";
30
+
31
+ export const FEED_SCHEMA = "gentle-shell.desktop-feed/v1";
32
+ // Extra agent dirs, one per custom Gentle Shell home: `{ "roots": ["/abs/dir", "~/dir"] }` (or a
33
+ // bare array). Optional; the two default homes are always read.
34
+ export const rootsFile = () => `${home()}/.config/herdr-app/gentle-shell-roots.json`;
35
+ const MAX_ROOTS = 8;
36
+ const MAX_FEEDS = 64; // a bounded read per root and refresh, even with many crashed leftovers
37
+
38
+ const trimSlash = (p) => (p.length > 1 ? p.replace(/[/\\]+$/, "") : p);
39
+ export const isAbsolute = (p) => typeof p === "string" && (p.startsWith("/") || /^[A-Za-z]:[/\\]/.test(p));
40
+ const expand = (p) => (p === "~" ? home() : p.startsWith("~/") ? `${home()}${p.slice(1)}` : p);
41
+
42
+ // The agent dirs to read, bundled first: ~/.gentle-shell/agent, ~/.pi/agent, then the entries of
43
+ // the roots file (absolute or ~/), without duplicates.
44
+ export async function agentRoots() {
45
+ const roots = [`${home()}/.gentle-shell/agent`, `${home()}/.pi/agent`];
46
+ try {
47
+ const parsed = JSON.parse(await readFile(rootsFile(), "utf8"));
48
+ const extra = Array.isArray(parsed) ? parsed : parsed?.roots;
49
+ if (Array.isArray(extra)) for (const r of extra) if (typeof r === "string" && r.trim()) roots.push(expand(r.trim()));
50
+ } catch {
51
+ // no roots file (the usual case) or not JSON: the default homes only
52
+ }
53
+ const seen = new Set();
54
+ return roots.map(trimSlash).filter((r) => isAbsolute(r) && !seen.has(r) && seen.add(r)).slice(0, MAX_ROOTS);
55
+ }
56
+
57
+ // The snapshot folder of an agent dir (lib/desktop-feed-store.ts desktopFeedDirectory).
58
+ export const feedDirOf = (root) => `${root}/desktop/sessions`;
59
+
60
+ // Every live snapshot of every root (default: agentRoots()), newest first. Unreadable, foreign or
61
+ // partial files are skipped. A snapshot from an older Gentle Shell without `identity.agentDir`
62
+ // gets the root it was found in.
63
+ export async function readFeeds(roots) {
64
+ const feeds = [];
65
+ const seen = new Set();
66
+ for (const root of roots ?? await agentRoots()) {
67
+ const dir = feedDirOf(root);
68
+ let names;
69
+ try {
70
+ names = await readdir(dir);
71
+ } catch {
72
+ continue;
73
+ }
74
+ for (const name of names.filter((n) => n.endsWith(".json") && !n.startsWith(".")).slice(0, MAX_FEEDS)) {
75
+ try {
76
+ const feed = JSON.parse(await readFile(`${dir}/${name}`, "utf8"));
77
+ if (feed?.schema !== FEED_SCHEMA || !feed.identity) continue;
78
+ if (!isAbsolute(feed.identity.agentDir)) feed.identity.agentDir = root;
79
+ const id = `${feed.identity.sessionId}@${feed.identity.pid}`;
80
+ if (seen.has(id)) continue; // one home reached twice (a symlink, a repeated root)
81
+ seen.add(id);
82
+ feeds.push(feed);
83
+ } catch {
84
+ // being replaced or not ours: the next refresh reads it again
85
+ }
86
+ }
87
+ }
88
+ return feeds.sort((a, b) => String(b.updatedAt).localeCompare(String(a.updatedAt)));
89
+ }
90
+
91
+ const positive = (n) => Number.isInteger(n) && n > 0;
92
+
93
+ // Whether each publisher still runs: one `ps` for the pids not checked in the last ALIVE_MS (a
94
+ // crashed session leaves its snapshot behind until the next session start sweeps it). Without
95
+ // `ps` (Windows) every snapshot counts as live.
96
+ const ALIVE_MS = 5000;
97
+ const alive = new Map(); // pid -> { ok, at }
98
+ export async function liveFeeds(feeds) {
99
+ const now = Date.now();
100
+ const want = [...new Set(feeds.map((f) => f.identity?.pid).filter(positive))].filter((pid) => !(now - (alive.get(pid)?.at ?? 0) < ALIVE_MS));
101
+ if (want.length) {
102
+ try {
103
+ // `ps -p` lists the pids that run; it exits 1 when one of them does not.
104
+ const r = await exec("ps", ["-o", "pid=", "-p", want.join(",")], { timeout: 3000 });
105
+ if (r.code === 0 || r.code === 1) {
106
+ const running = new Set(String(r.stdout).split(/\s+/).map(Number).filter(positive));
107
+ for (const pid of want) alive.set(pid, { ok: running.has(pid), at: now });
108
+ }
109
+ } catch {
110
+ // no ps: the publisher's own sweep is the only cleanup
111
+ }
112
+ if (alive.size > 256) for (const [pid, v] of alive) if (now - v.at > 60000) alive.delete(pid);
113
+ }
114
+ return feeds.filter((f) => alive.get(f.identity?.pid)?.ok !== false);
115
+ }
116
+
117
+ // One session, across every mod and refresh: its agent dir and session id.
118
+ export const feedKey = (feed) => (feed?.identity ? `${feed.identity.agentDir}|${feed.identity.sessionId}` : null);
119
+
120
+ // The snapshot of the session running in `terminal` (an entry of `terminals()`), or null.
121
+ // A terminal's own pid, or its foreground process, is the publisher or one of its ancestors
122
+ // (the launcher, the shell); a Herdr pane id the publisher saw in HERDR_PANE_ID also matches.
123
+ // The nearest process match wins; ties go to the newest snapshot.
124
+ // A terminal on another machine (S14: its address is "<machine>/<id>") never matches: its pids are
125
+ // that machine's, and the snapshots read here are this computer's.
126
+ export function matchFeed(terminal, feeds) {
127
+ if (machineOf(terminal?.address)) return null;
128
+ const pids = [terminal?.pid, terminal?.foreground?.pid].filter(positive);
129
+ const ids = [terminal?.address, terminal?.id, terminal?.pane, terminal?.pane_id].filter((v) => typeof v === "string" && v);
130
+ let best = null;
131
+ let bestRank = Infinity;
132
+ for (const feed of feeds) {
133
+ const id = feed.identity;
134
+ const chain = [id.pid, id.ppid, ...(Array.isArray(id.ancestors) ? id.ancestors : [])].filter(positive);
135
+ let rank = chain.findIndex((pid) => pids.includes(pid));
136
+ if (rank < 0 && id.herdr?.paneId && ids.includes(id.herdr.paneId)) rank = chain.length;
137
+ if (rank >= 0 && rank < bestRank) { best = feed; bestRank = rank; }
138
+ }
139
+ return best;
140
+ }
141
+
142
+ // The machine a Herdr address lives on: "vps/t7" → "vps"; a bare id ("t7", "wMJ:p1") is this
143
+ // computer's (null). Herdr prefixes every remote space, tab, pane and terminal id with its machine.
144
+ export function machineOf(address) {
145
+ const m = /^([^/]+)\/./.exec(typeof address === "string" ? address : "");
146
+ return m ? m[1] : null;
147
+ }
148
+
149
+ const APPS = new Set(["gentle-shell", "pi"]);
150
+ export const isGentle = (v) => typeof v === "string" && APPS.has(v);
151
+
152
+ // Gentle Shell terminals: the agents that report as gentle-shell / pi, and terminals that say so.
153
+ export function gentleTerminals() {
154
+ const set = new Set(agents().filter((a) => isGentle(a.app)).map((a) => a.address));
155
+ return terminals().filter((t) => set.has(t.address) || isGentle(t.agent) || isGentle(t.report?.app) || isGentle(t.app));
156
+ }
157
+
158
+ // Every session the window knows (see the header): { key, feed, terminal } for each live snapshot,
159
+ // with the Gentle Shell terminal of this window that runs it or null, newest first; then each
160
+ // Gentle Shell terminal of this window no snapshot matches ({ feed: null }). A herdr-core terminal
161
+ // this window does not show is another terminal: its session counts as running elsewhere.
162
+ export function allSessions(feeds) {
163
+ const out = (feeds ?? []).map((feed) => ({ key: feedKey(feed), feed, terminal: null }));
164
+ const rest = [];
165
+ let shown;
166
+ try { shown = new Set([...panes().map((p) => p.terminal), ...agents().map((a) => a.address)].filter(Boolean)); } catch { shown = null; }
167
+ for (const t of gentleTerminals()) {
168
+ if (shown && !shown.has(t.address)) continue;
169
+ const feed = matchFeed(t, feeds ?? []);
170
+ const s = feed ? out.find((x) => x.feed === feed) : null;
171
+ if (s) { if (!s.terminal) s.terminal = t; } else rest.push({ key: `terminal|${t.address}`, feed: null, terminal: t });
172
+ }
173
+ return [...out, ...rest];
174
+ }
175
+
176
+ // The two kinds of session (S10), shown the same way in every region (a labelled badge with its own
177
+ // icon, tokens.js kindBadge, and the sidebar's two Agents sections):
178
+ // herdr runs in a herdr-core terminal of this window: the Terminal switch shows its real TUI and
179
+ // what you type goes to that terminal (hc send / keys);
180
+ // tui a Gentle Shell TUI in another terminal (the Herdr TUI, Ghostty, …): no Terminal here;
181
+ // prompts, answers and actions go through its desktop inbox.
182
+ // remote a Herdr terminal of this window on another machine (S14, `hc machine ls`): herdr-core
183
+ // reports its state and the Terminal shows it, but its desktop snapshot and conversation
184
+ // are on that machine, so nothing of them is shown here (no feed, never a local match).
185
+ export const SESSION_KINDS = {
186
+ herdr: { kind: "herdr", label: "Herdr", section: "In this window", icon: "kind-herdr", about: "Runs in a Herdr terminal of this window: Chat | Terminal, typed input" },
187
+ tui: { kind: "tui", label: "TUI", section: "Other terminals", icon: "kind-tui", about: "A Gentle Shell TUI in another terminal: no Terminal here, input through its inbox" },
188
+ remote: { kind: "remote", label: "Remote", section: "Other machines", icon: "kind-remote", about: "Runs on another machine: Herdr reports its state and shows its Terminal; its chat and inspector feed stay on that machine" },
189
+ };
190
+ // A session's kind: a Herdr terminal of this window (on this computer or another machine), or a
191
+ // session that runs elsewhere on this computer.
192
+ export const sessionKind = (s) => (s?.terminal ? (machineOf(s.terminal.address) ? SESSION_KINDS.remote : SESSION_KINDS.herdr) : SESSION_KINDS.tui);
193
+
194
+ // S15: a session's live state, the same words in every region (sidebar, tabs, status bar, chat):
195
+ // blocked a question, a permission prompt, a consent or a blocker waits for the person;
196
+ // working its turn runs: "Working", "Working · 2 agents", "Working · 2 agents · 1 job";
197
+ // waiting its turn ended while its background subagents or jobs still run: "Waiting on 2 agents"
198
+ // (never "Idle" while that work runs);
199
+ // idle nothing runs.
200
+ // The publisher's own words (`activity.label`, docs/desktop-feed.md) when it says them; a snapshot
201
+ // from an older Gentle Shell gets them from its agents and jobs sections.
202
+ const counted = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
203
+ export function liveState(feed) {
204
+ const a = feed?.activity ?? {};
205
+ const agents = Number.isInteger(a.agents) ? a.agents : Number(feed?.agents?.active) || 0;
206
+ const jobs = Number.isInteger(a.jobs) ? a.jobs : Array.isArray(feed?.jobs?.running) ? feed.jobs.running.length : 0;
207
+ const work = [agents > 0 ? counted(agents, "agent") : null, jobs > 0 ? counted(jobs, "job") : null].filter(Boolean).join(" · ");
208
+ if (feed?.pendingQuestion || a.state === "blocked") return { status: "blocked", label: "Needs you", agents, jobs, work };
209
+ if (a.state === "working" || a.streaming) return { status: "working", label: work ? `Working · ${work}` : "Working", agents, jobs, work };
210
+ if (a.state === "waiting" || work) return { status: "waiting", label: `Waiting on ${work || "background work"}`, agents, jobs, work };
211
+ return { status: "idle", label: "Idle", agents, jobs, work };
212
+ }
213
+
214
+ // S15: what a session waits for, or null: { kind, word, reason, id }. kind: question (an ask tool),
215
+ // permission (a pi dialog: select, confirm, input), consent (a content-free blocker), blocked (the
216
+ // session says it waits, nothing more). `id` names this wait, so one notification per question.
217
+ const NEED_WORD = { question: "Question", permission: "Permission", consent: "Consent", blocked: "Blocked" };
218
+ export function needsYou(feed) {
219
+ const q = feed?.pendingQuestion;
220
+ const a = feed?.activity;
221
+ if (!q && a?.state !== "blocked") return null;
222
+ const kind = !q ? "blocked" : q.kind === "choice" || q.kind === "questionnaire" ? "question" : q.kind === "prompt" ? "permission" : q.kind === "consent" ? "consent" : "blocked";
223
+ const reason = String(q?.question || a?.blockedLabel || "Waiting for your answer").replace(/\s+/g, " ").trim();
224
+ return { kind, word: NEED_WORD[kind], reason, id: String(q?.toolCallId || q?.openedAt || "blocked") };
225
+ }
226
+
227
+ // Every Gentle Shell session that waits for the person, wherever it runs (S15, the sidebar's pinned
228
+ // "Needs you"): { key, session, need } for each live snapshot that waits (needsYou), then each
229
+ // Gentle Shell terminal of this window Herdr reports blocked whose snapshot says nothing (a remote
230
+ // one, an older Gentle Shell). Oldest wait first: the one that waited longest leads.
231
+ export function needsYouList(sessions) {
232
+ const out = [];
233
+ let blocked;
234
+ try { blocked = new Map(agents().filter((a) => isGentle(a.app) && a.status === "blocked").map((a) => [a.address, a])); } catch { blocked = new Map(); }
235
+ for (const s of sessions ?? []) {
236
+ const need = s.feed ? needsYou(s.feed) : null;
237
+ if (need) out.push({ key: s.key, session: s, need, since: String(s.feed.pendingQuestion?.openedAt ?? s.feed.updatedAt ?? "") });
238
+ else if (s.terminal && blocked.has(s.terminal.address)) {
239
+ out.push({ key: s.key, session: s, need: { kind: "blocked", word: NEED_WORD.blocked, reason: "Waiting for you in its terminal", id: "herdr" }, since: "" });
240
+ }
241
+ }
242
+ return out.sort((x, y) => x.since.localeCompare(y.since));
243
+ }
244
+
245
+ // Where a session runs, for a row's second line: its Herdr terminal, its Herdr TUI pane, else its pid.
246
+ export function whereLabel(s) {
247
+ if (s?.terminal) return s.terminal.address;
248
+ const pane = s?.feed?.identity?.herdr?.paneId;
249
+ if (typeof pane === "string" && pane) return `herdr ${pane}`;
250
+ return s?.feed?.identity?.pid ? `pid ${s.feed.identity.pid}` : "";
251
+ }
252
+
253
+ // The project a session belongs to: the Git root of its folder when the snapshot names one (its
254
+ // Changes worktrees), else its real folder.
255
+ export function projectOf(feed) {
256
+ const cwd = feed?.identity?.realCwd || feed?.project?.cwd || feed?.identity?.cwd || null;
257
+ if (!cwd) return null;
258
+ const roots = (feed?.changes?.worktrees ?? []).map((w) => w?.root).filter((r) => typeof r === "string" && r && (cwd === r || cwd.startsWith(`${r}/`)));
259
+ return trimSlash(roots.sort((a, b) => b.length - a.length)[0] ?? cwd);
260
+ }
261
+ // The Herdr space (an entry of `spaces()`) whose folder holds `path`, the deepest one; a space on
262
+ // another machine or at the home folder holds nothing here.
263
+ export function spaceFor(path, list) {
264
+ if (!path) return null;
265
+ let best = null;
266
+ for (const s of list ?? []) {
267
+ const f = trimSlash(String(s?.folder ?? ""));
268
+ if (!f || s.machine || f === trimSlash(home()) || !(path === f || path.startsWith(`${f}/`))) continue;
269
+ if (!best || f.length > trimSlash(best.folder).length) best = s;
270
+ }
271
+ return best;
272
+ }
273
+
274
+ export const projectName = (path) => String(path ?? "").replace(/[/\\]+$/, "").split(/[/\\]/).pop() || String(path ?? "");
275
+
276
+ // Shared state naming the picked session that runs outside Herdr app: { key, focus, at }.
277
+ export const SESSION_KEY = "gentle-shell.session";
278
+ const focusedTerminal = () => {
279
+ try { const f = focused(); return f?.terminal ?? f?.agent ?? null; } catch { return null; }
280
+ };
281
+
282
+ // Shows `s` in every mod. A session Herdr runs gets the keyboard on its terminal (every mod follows
283
+ // Herdr's focus); any other one is named in shared state. Call it from a host call (a click).
284
+ export function selectSession(s) {
285
+ if (!s) return;
286
+ if (s.terminal) {
287
+ setShared(SESSION_KEY, null);
288
+ try { run("pane.focus", { terminal: s.terminal.address }); } catch { /* a terminal outside the layout */ }
289
+ return;
290
+ }
291
+ setShared(SESSION_KEY, { key: s.key, focus: focusedTerminal(), at: Date.now() });
292
+ }
293
+
294
+ // Forgets a picked session: Herdr's focus decides again (a Herdr tab or agent was chosen).
295
+ export function followHerdr() {
296
+ if (shared(SESSION_KEY)) setShared(SESSION_KEY, null);
297
+ }
298
+
299
+ // The picked session's key while it still holds: Herdr's focus has not moved to another terminal.
300
+ export function pickedKey() {
301
+ const pick = shared(SESSION_KEY);
302
+ if (!pick || typeof pick.key !== "string") return null;
303
+ const now = focusedTerminal();
304
+ return now && now !== (pick.focus ?? null) ? null : pick.key;
305
+ }
306
+
307
+ // The session the window is about: the picked one (see selectSession), else the focused Gentle
308
+ // Shell terminal's, else one in the shown tab, else the last one shown (`last`, a session key),
309
+ // else a Herdr one, else the newest snapshot. Mirrors targetTerminal for Herdr terminals.
310
+ export function targetSession(feeds, last) {
311
+ const all = allSessions(feeds);
312
+ const picked = pickedKey();
313
+ const chosen = picked ? all.find((s) => s.key === picked) : null;
314
+ if (chosen) return chosen;
315
+ const f = focused();
316
+ const byAddr = (a) => (a ? all.find((s) => s.terminal?.address === a) : undefined);
317
+ const inTab = agents().filter((a) => isGentle(a.app) && a.tab === f?.tab);
318
+ return byAddr(f?.agent) ?? byAddr(f?.terminal) ?? byAddr(inTab[0]?.address)
319
+ ?? (last ? all.find((s) => s.key === last) : undefined) ?? all.find((s) => s.terminal) ?? all[0] ?? null;
320
+ }
321
+
322
+ // The Gentle Shell terminal the window is about: the focused one, else one in the shown tab,
323
+ // else the last one shown (`last`), else the first. Mirrors GentleChat.target().
324
+ export function targetTerminal(last) {
325
+ const all = gentleTerminals();
326
+ const f = focused();
327
+ const byAddr = (a) => (a ? all.find((t) => t.address === a) : undefined);
328
+ const inTab = agents().filter((a) => isGentle(a.app) && a.tab === f?.tab);
329
+ return byAddr(f?.agent) ?? byAddr(f?.terminal) ?? byAddr(inTab[0]?.address) ?? byAddr(last) ?? all[0] ?? null;
330
+ }
331
+
332
+ // The live snapshots as the leader published them ({ digest, at, feeds }), and their digest alone:
333
+ // a follower compares the small digest and reads the big value only when it changed.
334
+ export const FEEDS_KEY = "gentle-shell.feeds";
335
+ export const FEEDS_DIGEST_KEY = "gentle-shell.feeds.digest";
336
+ export const FEED_LEADER = "gentle-status";
337
+
338
+ // Whether the shown layout places the leader mod in a block (gentle-shell.json: the status bar).
339
+ export function leaderShown() {
340
+ const has = (n) => n?.content?.mod === FEED_LEADER || (Array.isArray(n?.children) && n.children.some(has));
341
+ try { return has(layout()); } catch { return false; }
342
+ }
343
+
344
+ // Keeps `feeds` fresh. The leader (and a copy without one): a watch on every root's snapshot folder
345
+ // plus a gentle fallback timer (a folder may not exist yet, and a missed event must not freeze the
346
+ // view). `onChange(cx)` runs only when a snapshot really changed (the publisher rewrites a file
347
+ // only when its content changed). A follower reads the leader's shared value (see the header).
348
+ export class FeedSource {
349
+ constructor() {
350
+ this.own = []; // what this copy read itself (the leader, or a copy without one)
351
+ this.key = "";
352
+ this.loading = false;
353
+ this.again = false;
354
+ this.watching = new Set(); // the snapshot folders watched now
355
+ this.mode = "alone"; // lead | follow | alone
356
+ this.digest = undefined; // the published digest `cache` belongs to
357
+ this.cache = [];
358
+ }
359
+
360
+ start(cx, onChange, every = 10000, { lead = false } = {}) {
361
+ this.onChange = onChange;
362
+ if (lead) { this.mode = "lead"; this.read(cx, every); return; }
363
+ this.mode = "follow";
364
+ if (leaderShown()) return;
365
+ // A layout switch starts the mods while layout() may still be the layout before (measured):
366
+ // look again before reading alone, and on every tick after.
367
+ cx.timer.after(500, (cx) => { if (!leaderShown()) { this.mode = "alone"; this.read(cx, every); } });
368
+ }
369
+
370
+ // Reads now and every `every` ms; a copy reading alone stops once its layout places the leader.
371
+ read(cx, every) {
372
+ this.task = cx.timer.every(every, (cx) => {
373
+ if (this.mode === "alone" && leaderShown()) {
374
+ this.mode = "follow";
375
+ this.task?.cancel();
376
+ cx.notify();
377
+ return;
378
+ }
379
+ this.refresh(cx);
380
+ });
381
+ this.refresh(cx);
382
+ }
383
+
384
+ // Every live snapshot, newest first. A follower's read is ambient (a render, a timer, a handler);
385
+ // outside one it keeps the last value it read.
386
+ get feeds() {
387
+ if (this.mode !== "follow") return this.own;
388
+ try {
389
+ const digest = shared(FEEDS_DIGEST_KEY);
390
+ if (digest !== this.digest) {
391
+ const v = shared(FEEDS_KEY);
392
+ this.cache = Array.isArray(v?.feeds) ? v.feeds : [];
393
+ this.digest = digest;
394
+ }
395
+ } catch {
396
+ // not in a host call: the last value read
397
+ }
398
+ return this.cache;
399
+ }
400
+
401
+ // The leader's publish: from a host call (a timer callback), and only the newest read. Setting
402
+ // shared state re-renders every mod, the leader included.
403
+ publish(cx, key, feeds) {
404
+ cx.timer.after(1, (cx) => {
405
+ if (key !== this.key) return;
406
+ try {
407
+ setShared(FEEDS_KEY, { digest: key, at: Date.now(), feeds });
408
+ setShared(FEEDS_DIGEST_KEY, key);
409
+ } catch {
410
+ cx.notify(); // a copy being closed: at least this one shows it
411
+ }
412
+ });
413
+ }
414
+
415
+ refresh(cx) {
416
+ if (this.loading) { this.again = true; return; }
417
+ this.loading = true;
418
+ cx.spawn(async (cx) => {
419
+ try {
420
+ do {
421
+ this.again = false;
422
+ const roots = await agentRoots();
423
+ for (const root of roots) this.watch(feedDirOf(root), cx);
424
+ const feeds = await liveFeeds(await readFeeds(roots));
425
+ const key = feeds.map((f) => `${f.identity.agentDir}/${f.identity.sessionId}@${f.updatedAt}`).join("|");
426
+ if (key !== this.key) {
427
+ this.key = key;
428
+ this.own = feeds;
429
+ this.onChange?.(cx);
430
+ if (this.mode === "lead") this.publish(cx, key, feeds);
431
+ else cx.notify();
432
+ }
433
+ } while (this.again);
434
+ } finally {
435
+ this.loading = false;
436
+ }
437
+ });
438
+ }
439
+
440
+ // One watch per snapshot folder; a missing folder ends it, and the next refresh tries again.
441
+ watch(dir, cx) {
442
+ if (this.watching.has(dir)) return;
443
+ this.watching.add(dir);
444
+ cx.spawn(async (cx) => {
445
+ try {
446
+ for (;;) {
447
+ await watchFolder(dir);
448
+ this.refresh(cx);
449
+ }
450
+ } catch {
451
+ this.watching.delete(dir); // no folder yet: the timer reads and tries the watch again
452
+ }
453
+ });
454
+ }
455
+
456
+ // The snapshot of a terminal (an entry of terminals()), or null.
457
+ of(terminal) {
458
+ return terminal ? matchFeed(terminal, this.feeds) : null;
459
+ }
460
+
461
+ // Every live session (allSessions) and the one the window is about (targetSession).
462
+ sessions() {
463
+ return allSessions(this.feeds);
464
+ }
465
+
466
+ target(last) {
467
+ return targetSession(this.feeds, last);
468
+ }
469
+ }