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
@@ -4,16 +4,17 @@
4
4
  //
5
5
  // Nothing here starts, stops, or polls a job. It only names the wire shapes,
6
6
  // projects the owner's actual `JobRecord` state into the safe Desktop
7
- // projection, and defines the bounded running + recent-finished selection the
8
- // Desktop list uses. The owner (the single parent-session `JobRegistry` plus
7
+ // projection, defines the bounded running + recent-finished selection the
8
+ // Desktop list uses, and turns the owner's bounded output tail into the plain
9
+ // text `get_job_output` answers. The owner (the single parent-session `JobRegistry` plus
9
10
  // the parent `MonitorController`, reached through `job_stop`'s exact stop
10
11
  // path) stays the single source of truth; this module is a projection, not a
11
12
  // second registry, process manager, or store.
12
13
 
13
- import type { JobRecord, JobStatus } from "./background-jobs.ts";
14
+ import { MAX_TAIL_LINE_CHARS, TAIL_LINES, type JobRecord, type JobStatus } from "./background-jobs.ts";
14
15
  import { JOBS_FINISHED_TTL_MS } from "./jobs-widget.ts";
15
16
 
16
- export const DESKTOP_JOBS_METHODS = ["list_jobs", "cancel_job"] as const;
17
+ export const DESKTOP_JOBS_METHODS = ["list_jobs", "cancel_job", "get_job_output"] as const;
17
18
  export const DESKTOP_JOBS_EVENT = "jobs_update";
18
19
  /** Advertised only because the owner seam records the registry changes this event carries. */
19
20
  export const DESKTOP_JOBS_EVENTS = [DESKTOP_JOBS_EVENT] as const;
@@ -27,13 +28,22 @@ export const DESKTOP_JOBS_SERVICE_CHANNEL = "gentle:desktop:jobs-service";
27
28
  /** Upper bound on the number of jobs one list answer carries (running + recent finished). */
28
29
  export const DESKTOP_JOBS_LIST_LIMIT = 50;
29
30
 
31
+ /**
32
+ * Upper bound, in UTF-16 code units, on one `get_job_output` text: the owner
33
+ * tail's own bound (`TAIL_LINES` lines of at most `MAX_TAIL_LINE_CHARS`
34
+ * characters plus the owner's one-character cut marker) joined by newlines.
35
+ */
36
+ export const DESKTOP_JOB_OUTPUT_MAX_CHARS = TAIL_LINES * (MAX_TAIL_LINE_CHARS + 1) + (TAIL_LINES - 1);
37
+
30
38
  export type DesktopJobStatus = JobStatus;
31
39
 
32
40
  /**
33
41
  * The safe wire window for one job. Only these fields exist on the wire:
34
42
  * identity, the human label, the status word, start/end timestamps, and the
35
- * exit code when the owner recorded one. Never a command, cwd, output path or
36
- * tail, environment, owner session id, monitor counters, or an error string.
43
+ * exit code when the owner recorded one. Never a command, cwd, output path,
44
+ * environment, owner session id, monitor counters, or an error string. The
45
+ * output tail is not part of this entry: it crosses only through the separate,
46
+ * owner-verified `get_job_output` read (`DesktopJobOutput`).
37
47
  */
38
48
  export interface DesktopJobEntry {
39
49
  id: string;
@@ -50,6 +60,29 @@ export interface DesktopJobsOwnerRead {
50
60
  jobs: DesktopJobEntry[];
51
61
  }
52
62
 
63
+ /**
64
+ * The `get_job_output` answer for one owned job: the job's own whitelisted
65
+ * projection, its bounded output tail as one plain-text field, and whether that
66
+ * tail is incomplete. `output` is the owner's in-memory tail (the last
67
+ * `TAIL_LINES` lines, each at most `MAX_TAIL_LINE_CHARS` characters) with
68
+ * terminal control sequences removed (see `plainJobOutput`). Otherwise it is the
69
+ * job's own unsanitized process output: it can contain anything the job printed,
70
+ * including echoed commands, paths, or secrets. No command, cwd, output path,
71
+ * environment, owner session id, monitor counter, or error string is ever a
72
+ * separate field.
73
+ */
74
+ export interface DesktopJobOutput {
75
+ job: DesktopJobEntry;
76
+ output: string;
77
+ /** True when earlier output was dropped or a line was shortened. */
78
+ truncated: boolean;
79
+ }
80
+
81
+ /** A `get_job_output` owner result; a foreign or unknown id is `not_found`. */
82
+ export type DesktopJobsOutputOutcome =
83
+ | { kind: "ok"; view: DesktopJobOutput }
84
+ | { kind: "not_found" };
85
+
53
86
  /**
54
87
  * A `cancel_job` owner result. `changed` is `true` only when the job really
55
88
  * transitioned from running to stopped; an already-terminal job answers
@@ -68,6 +101,12 @@ export interface DesktopJobsService {
68
101
  schema: typeof DESKTOP_JOBS_SERVICE_SCHEMA;
69
102
  read(): DesktopJobsOwnerRead;
70
103
  cancel(id: string): DesktopJobsCancelOutcome;
104
+ /**
105
+ * Read one owned job's bounded output tail. Optional so an owner adapter
106
+ * without it stays a valid list/cancel owner; the bridge then answers
107
+ * `get_job_output` with `jobs_unavailable`.
108
+ */
109
+ output?(id: string): DesktopJobsOutputOutcome;
71
110
  /** Subscribe to actual registry changes; the unsubscribe stops delivery. */
72
111
  observe(onChange: (sessionId: string) => void): () => void;
73
112
  dispose(): void;
@@ -127,6 +166,72 @@ export function selectDesktopJobs(jobs: readonly JobRecord[], now: number): Desk
127
166
  return [...running, ...finished].slice(0, DESKTOP_JOBS_LIST_LIMIT).map(projectDesktopJob);
128
167
  }
129
168
 
169
+ // Terminal control sequences, matched per line: OSC (BEL- or ST-terminated, or
170
+ // unterminated to the end of the line), DCS/SOS/PM/APC strings, CSI (7- and
171
+ // 8-bit), and the remaining two-character escapes.
172
+ const TERMINAL_SEQUENCE = /\u001b\][\s\S]*?(?:\u0007|\u001b\\|$)|\u001b[PX^_][\s\S]*?(?:\u001b\\|$)|\u001b\[[0-?]*[ -/]*[@-~]|\u009b[0-?]*[ -/]*[@-~]|\u001b[ -/]*[0-~]/g;
173
+ // Every other C0/C1 control character except tab (a line never holds a newline).
174
+ const CONTROL_CHARACTER = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g;
175
+ // Half of a UTF-16 pair, left behind when a line was cut at a fixed length.
176
+ const LONE_SURROGATE = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g;
177
+
178
+ /**
179
+ * One tail line as plain text: terminal control sequences (colors, cursor
180
+ * moves, titles, hyperlinks) are removed, a carriage-return progress line keeps
181
+ * only its last non-empty segment (what a terminal leaves visible), other
182
+ * control characters except tab are dropped, and a half character left by a
183
+ * length cut is removed. Idempotent, so the bridge can re-apply it to an owner
184
+ * answer.
185
+ */
186
+ function plainJobOutputLine(line: string): string {
187
+ const segments = line.replace(TERMINAL_SEQUENCE, "").split("\r");
188
+ let visible = "";
189
+ for (let index = segments.length - 1; index >= 0; index -= 1) {
190
+ if (segments[index] !== "") {
191
+ visible = segments[index]!;
192
+ break;
193
+ }
194
+ }
195
+ return visible.replace(CONTROL_CHARACTER, "").replace(LONE_SURROGATE, "");
196
+ }
197
+
198
+ /** Owner tail lines as one plain-text field, newline-joined (see `plainJobOutputLine`). */
199
+ export function plainJobOutput(lines: readonly string[]): string {
200
+ return lines.map(plainJobOutputLine).join("\n");
201
+ }
202
+
203
+ /**
204
+ * The whitelisted `get_job_output` projection of one owner `JobRecord`: the job
205
+ * projection, its bounded tail as plain text, and the owner's truncation flag.
206
+ * Nothing else on the record is read.
207
+ */
208
+ export function projectDesktopJobOutput(job: JobRecord): DesktopJobOutput {
209
+ return {
210
+ job: projectDesktopJob(job),
211
+ output: plainJobOutput(job.tail.slice(-TAIL_LINES)),
212
+ truncated: job.tailTruncated === true,
213
+ };
214
+ }
215
+
216
+ /**
217
+ * Defensive validation of one owner `get_job_output` answer. It crosses the
218
+ * event bus, so it is re-validated, re-bounded to `TAIL_LINES` lines and
219
+ * `DESKTOP_JOB_OUTPUT_MAX_CHARS` code units (newest output kept, `truncated`
220
+ * set when anything is cut), and re-normalized to plain text.
221
+ */
222
+ export function parseDesktopJobOutput(value: unknown): DesktopJobOutput | undefined {
223
+ if (!value || typeof value !== "object") return undefined;
224
+ const raw = value as Record<string, unknown>;
225
+ const job = parseDesktopJobEntry(raw.job);
226
+ if (job === undefined) return undefined;
227
+ if (typeof raw.output !== "string" || typeof raw.truncated !== "boolean") return undefined;
228
+ const oversized = raw.output.length > DESKTOP_JOB_OUTPUT_MAX_CHARS;
229
+ const text = oversized ? raw.output.slice(-DESKTOP_JOB_OUTPUT_MAX_CHARS) : raw.output;
230
+ const lines = text.split("\n");
231
+ const kept = lines.slice(-TAIL_LINES);
232
+ return { job, output: plainJobOutput(kept), truncated: raw.truncated || oversized || kept.length < lines.length };
233
+ }
234
+
130
235
  /**
131
236
  * Defensive validation of one owner-projected entry. The owner's answer crosses
132
237
  * the event bus, so its shape is never trusted beyond the fields the bridge
@@ -161,10 +266,11 @@ export function parseListJobsParams(params: Record<string, unknown>): { ok: true
161
266
  }
162
267
 
163
268
  /**
164
- * `cancel_job` params are exactly `{ id: non-empty string }`. Anything else is
165
- * a typed `invalid_request` before any owner work happens.
269
+ * `cancel_job` and `get_job_output` params are exactly `{ id: non-empty
270
+ * string }`. Anything else is a typed `invalid_request` before any owner work
271
+ * happens.
166
272
  */
167
- export function parseCancelJobParams(params: Record<string, unknown>): { id: string } | { error: { code: string; message: string } } {
273
+ function parseJobIdParams(params: Record<string, unknown>): { id: string } | { error: { code: string; message: string } } {
168
274
  const keys = Object.keys(params);
169
275
  if (keys.length !== 1 || keys[0] !== "id" || typeof params.id !== "string" || params.id.length === 0) {
170
276
  return { error: INVALID_JOBS_PARAMS };
@@ -172,6 +278,9 @@ export function parseCancelJobParams(params: Record<string, unknown>): { id: str
172
278
  return { id: params.id };
173
279
  }
174
280
 
281
+ export const parseCancelJobParams = parseJobIdParams;
282
+ export const parseJobOutputParams = parseJobIdParams;
283
+
175
284
  /** True only for the adapter-shaped object the discovery responder must return. */
176
285
  export function isDesktopJobsServiceShape(value: unknown): value is DesktopJobsService {
177
286
  if (!value || typeof value !== "object") return false;
@@ -2,6 +2,13 @@ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-a
2
2
  import { discoverYoloUiAdapter, YOLO_DISPLAY, type YoloUiAdapter } from "./yolo-session-policy.ts";
3
3
  import { isPermissionMode, type PermissionMode } from "./permission-mode-policy.ts";
4
4
  import type { DesktopRpcOutcome } from "./desktop-rpc.ts";
5
+ import {
6
+ createDesktopDebouncer,
7
+ DESKTOP_CHANGE_DEBOUNCE_MS,
8
+ realDesktopChangeTimers,
9
+ type DesktopChangeTimers,
10
+ type DesktopDebouncer,
11
+ } from "./desktop-rpc-config-watch.ts";
5
12
 
6
13
  export const DESKTOP_PERMISSION_METHODS = ["get_permission_mode", "set_permission_mode"] as const;
7
14
  export const DESKTOP_PERMISSION_EVENT = "permission_mode_changed";
@@ -14,6 +21,17 @@ const DEFAULT_UNCHANGED = { code: "default_unchanged", message: "Default permiss
14
21
 
15
22
  type PermissionState = { mode: PermissionMode; defaultMode: PermissionMode };
16
23
 
24
+ export type DesktopPermissionEmit = (event: string, data: Record<string, unknown>) => void;
25
+
26
+ export interface DesktopPermissionWatchOptions {
27
+ timers?: DesktopChangeTimers;
28
+ debounceMs?: number;
29
+ }
30
+
31
+ function sameState(left: PermissionState, right: PermissionState): boolean {
32
+ return left.mode === right.mode && left.defaultMode === right.defaultMode;
33
+ }
34
+
17
35
  function parseSetParams(params: Record<string, unknown>): { mode: unknown; persist: boolean } | undefined {
18
36
  for (const key of Object.keys(params)) {
19
37
  if (key !== "mode" && key !== "persist") return undefined;
@@ -31,6 +49,15 @@ function parseSetParams(params: Record<string, unknown>): { mode: unknown; persi
31
49
  * store, no second file reader, and no filesystem watcher. Every mutation is
32
50
  * validated by the owner (interactive host, live session identity, atomic save);
33
51
  * this bridge only shapes owner results into the Desktop RPC contract.
52
+ *
53
+ * Once wired for an interactive RPC host, the bridge subscribes to the owner's
54
+ * existing observer (`observe`, the same one the desktop feed uses). Each owner
55
+ * notification restarts a short debounce window; when it closes, the bridge
56
+ * re-reads the owner and emits `permission_mode_changed` with
57
+ * `source: "external"` only when the state differs from the last state this
58
+ * channel reported. Desktop requests record what they reported, and an observer
59
+ * window that closes while a Desktop change is in flight is deferred until it
60
+ * finishes, so a Desktop change is never echoed as an external one.
34
61
  */
35
62
  export class DesktopPermissionBridge {
36
63
  private readonly pi: ExtensionAPI;
@@ -39,10 +66,27 @@ export class DesktopPermissionBridge {
39
66
  private discovering: Promise<YoloUiAdapter | undefined> | undefined;
40
67
  private host: ExtensionContext | undefined;
41
68
  private generation = 0;
69
+ private readonly debouncer: DesktopDebouncer;
70
+ private emit: DesktopPermissionEmit | undefined;
71
+ private unobserve: (() => void) | undefined;
72
+ /** The last state this channel reported, and a counter bumped on every report. */
73
+ private reported: PermissionState | undefined;
74
+ private reportEpoch = 0;
75
+ private setsInFlight = 0;
76
+ private refreshDeferred = false;
42
77
 
43
- constructor(pi: ExtensionAPI, discover: typeof discoverYoloUiAdapter = discoverYoloUiAdapter) {
78
+ constructor(
79
+ pi: ExtensionAPI,
80
+ discover: typeof discoverYoloUiAdapter = discoverYoloUiAdapter,
81
+ watch: DesktopPermissionWatchOptions = {},
82
+ ) {
44
83
  this.pi = pi;
45
84
  this.discover = discover;
85
+ this.debouncer = createDesktopDebouncer(
86
+ watch.timers ?? realDesktopChangeTimers,
87
+ watch.debounceMs ?? DESKTOP_CHANGE_DEBOUNCE_MS,
88
+ () => void this.refresh(),
89
+ );
46
90
  }
47
91
 
48
92
  /** The live interactive context of this Desktop session; bound per command. */
@@ -54,9 +98,22 @@ export class DesktopPermissionBridge {
54
98
  reset(): void {
55
99
  this.generation += 1;
56
100
  const adapter = this.adapter;
101
+ const unobserve = this.unobserve;
57
102
  this.adapter = undefined;
58
103
  this.discovering = undefined;
59
104
  this.host = undefined;
105
+ this.emit = undefined;
106
+ this.unobserve = undefined;
107
+ this.reported = undefined;
108
+ this.reportEpoch += 1;
109
+ this.setsInFlight = 0;
110
+ this.refreshDeferred = false;
111
+ this.debouncer.cancel();
112
+ try {
113
+ unobserve?.();
114
+ } catch {
115
+ /* display-only owner teardown */
116
+ }
60
117
  try {
61
118
  adapter?.dispose();
62
119
  } catch {
@@ -64,13 +121,88 @@ export class DesktopPermissionBridge {
64
121
  }
65
122
  }
66
123
 
124
+ /**
125
+ * Interactive RPC hosts only, after `attach`: subscribe to the owner observer
126
+ * and record the current state as the baseline, so wiring never emits by
127
+ * itself. A missing owner simply leaves the session without external events.
128
+ */
129
+ async wire(emit: DesktopPermissionEmit): Promise<void> {
130
+ const context = this.host;
131
+ if (!context) return;
132
+ this.emit = emit;
133
+ const generation = this.generation;
134
+ try {
135
+ const adapter = await this.owner(context);
136
+ if (generation !== this.generation || !adapter || this.unobserve) return;
137
+ this.unobserve = adapter.observe(() => this.debouncer.schedule());
138
+ const epoch = this.reportEpoch;
139
+ const state = await this.read(context);
140
+ if (generation !== this.generation || epoch !== this.reportEpoch || !state) return;
141
+ this.report(state);
142
+ } catch {
143
+ // A failing owner leaves this session without external permission
144
+ // events; nothing reaches the caller or the process as a rejection.
145
+ if (generation !== this.generation) return;
146
+ this.emit = undefined;
147
+ this.debouncer.cancel();
148
+ const unobserve = this.unobserve;
149
+ this.unobserve = undefined;
150
+ try {
151
+ unobserve?.();
152
+ } catch {
153
+ /* display-only owner teardown */
154
+ }
155
+ }
156
+ }
157
+
158
+ private report(state: PermissionState): void {
159
+ this.reported = state;
160
+ this.reportEpoch += 1;
161
+ }
162
+
163
+ /** Debounced owner notification: emit only a state this channel has not reported. */
164
+ private async refresh(): Promise<void> {
165
+ const context = this.host;
166
+ const emit = this.emit;
167
+ if (!context || !emit) return;
168
+ if (this.setsInFlight > 0) {
169
+ this.refreshDeferred = true;
170
+ return;
171
+ }
172
+ const generation = this.generation;
173
+ const epoch = this.reportEpoch;
174
+ const state = await this.read(context);
175
+ // A newer report (a Desktop request, or a reset) owns the state now.
176
+ if (generation !== this.generation || epoch !== this.reportEpoch || !state) return;
177
+ if (this.setsInFlight > 0) {
178
+ this.refreshDeferred = true;
179
+ return;
180
+ }
181
+ const previous = this.reported;
182
+ this.report(state);
183
+ if (!previous || sameState(previous, state)) return;
184
+ try {
185
+ emit(DESKTOP_PERMISSION_EVENT, {
186
+ mode: state.mode,
187
+ previous_mode: previous.mode,
188
+ default_mode: state.defaultMode,
189
+ persisted: state.defaultMode !== previous.defaultMode,
190
+ source: "external",
191
+ });
192
+ } catch {
193
+ /* a closed transport never throws back into the owner observer */
194
+ }
195
+ }
196
+
67
197
  async handle(method: string, params: Record<string, unknown>): Promise<DesktopRpcOutcome | undefined> {
68
198
  const context = this.host;
69
199
  if (!context) return { ok: false, error: UNAVAILABLE };
70
200
  if (method === "get_permission_mode") {
71
201
  if (Object.keys(params).length > 0) return { ok: false, error: INVALID_PARAMS };
72
202
  const state = await this.read(context);
73
- return state ? { ok: true, data: { mode: state.mode, default_mode: state.defaultMode } } : { ok: false, error: UNAVAILABLE };
203
+ if (!state) return { ok: false, error: UNAVAILABLE };
204
+ this.report(state);
205
+ return { ok: true, data: { mode: state.mode, default_mode: state.defaultMode } };
74
206
  }
75
207
  if (method === "set_permission_mode") return this.set(context, params);
76
208
  return undefined;
@@ -107,8 +239,8 @@ export class DesktopPermissionBridge {
107
239
 
108
240
  private async read(context: ExtensionContext): Promise<PermissionState | undefined> {
109
241
  const adapter = await this.owner(context);
110
- if (!adapter || typeof adapter.readSnapshot !== "function") return undefined;
111
242
  try {
243
+ if (!adapter || typeof adapter.readSnapshot !== "function") return undefined;
112
244
  const snapshot = await adapter.readSnapshot();
113
245
  if (!snapshot || snapshot.yolo === YOLO_DISPLAY.unavailable) return undefined;
114
246
  if (!isPermissionMode(snapshot.mode) || !isPermissionMode(snapshot.defaultMode)) return undefined;
@@ -122,6 +254,22 @@ export class DesktopPermissionBridge {
122
254
  const parsed = parseSetParams(params);
123
255
  if (!parsed) return { ok: false, error: INVALID_PARAMS };
124
256
  if (!isPermissionMode(parsed.mode)) return { ok: false, error: INVALID_MODE };
257
+ const generation = this.generation;
258
+ this.setsInFlight += 1;
259
+ try {
260
+ return await this.apply(context, { mode: parsed.mode, persist: parsed.persist });
261
+ } finally {
262
+ if (generation === this.generation) {
263
+ this.setsInFlight -= 1;
264
+ if (this.setsInFlight === 0 && this.refreshDeferred) {
265
+ this.refreshDeferred = false;
266
+ this.debouncer.schedule();
267
+ }
268
+ }
269
+ }
270
+ }
271
+
272
+ private async apply(context: ExtensionContext, parsed: { mode: PermissionMode; persist: boolean }): Promise<DesktopRpcOutcome> {
125
273
  const adapter = await this.owner(context);
126
274
  if (!adapter || typeof adapter.setMode !== "function") return { ok: false, error: UNAVAILABLE };
127
275
  const before = await this.read(context);
@@ -153,6 +301,9 @@ export class DesktopPermissionBridge {
153
301
  // owner is not enforcing.
154
302
  const after = await this.read(context);
155
303
  if (!after) return { ok: false, error: UNAVAILABLE };
304
+ // Recorded before the response leaves: the owner notified its observers
305
+ // while applying this request, and that window must find nothing new.
306
+ this.report(after);
156
307
  const mode = after.mode;
157
308
  const defaultMode = after.defaultMode;
158
309
  const changed = mode !== before.mode || defaultMode !== before.defaultMode;
@@ -0,0 +1,180 @@
1
+ // Gentle Desktop review-mode contract (#3324): the pure wire shapes, the
2
+ // owner-service interface, strict param parsers, the whitelist projection of
3
+ // the owner's receipt-driven development (RDD) status, and the confirmation
4
+ // text and token. The owner is `extensions/gentle-ai.ts`, which reads and
5
+ // writes the switch through the same native review CLI adapter the
6
+ // `/gentle:review-mode` command uses; nothing here runs `gentle-ai`.
7
+
8
+ import { createHash } from "node:crypto";
9
+
10
+ export const DESKTOP_REVIEW_METHODS = ["get_review_mode", "set_review_mode"] as const;
11
+ export const DESKTOP_REVIEW_MODE_CHANGED_EVENT = "review_mode_changed";
12
+ export const DESKTOP_REVIEW_EVENTS = [DESKTOP_REVIEW_MODE_CHANGED_EVENT] as const;
13
+ export const DESKTOP_REVIEW_SERVICE_SCHEMA = "gentle-pi.desktop-review-service/v1";
14
+ export const DESKTOP_REVIEW_SERVICE_CHANNEL = "gentle:desktop:review-service";
15
+
16
+ const MAX_TOKEN_LENGTH = 128;
17
+
18
+ export const REVIEW_UNAVAILABLE = { code: "review_unavailable", message: "Review mode is not available in this session" } as const;
19
+ export const REVIEW_STATE_UNREADABLE = { code: "review_unavailable", message: "Review mode could not be read; nothing was changed" } as const;
20
+ export const REVIEW_WRITE_UNCONFIRMED = { code: "review_unavailable", message: "The review mode change did not complete; re-read the mode" } as const;
21
+ export const REVIEW_MODE_BUSY = { code: "review_mode_busy", message: "Another review mode change is in progress" } as const;
22
+ export const INVALID_GET_REVIEW_MODE = { code: "invalid_request", message: "Invalid get_review_mode parameters" } as const;
23
+ export const INVALID_SET_REVIEW_MODE = { code: "invalid_request", message: "Invalid set_review_mode parameters" } as const;
24
+
25
+ /** One scope's stored value as the owner reports it: `""` means unset. */
26
+ export type DesktopReviewOwnerScopeValue = "on" | "off" | "";
27
+
28
+ /** The owner's status, already decoded by the native adapter (`gentle-ai.rdd-mode-status/v1`). */
29
+ export interface DesktopReviewOwnerStatus {
30
+ global: DesktopReviewOwnerScopeValue;
31
+ cloneLocal: DesktopReviewOwnerScopeValue;
32
+ effective: "on" | "off";
33
+ source: "default" | "global" | "clone_local";
34
+ revision?: string;
35
+ }
36
+
37
+ /** `unavailable`: no adapter, or `gentle-ai` is missing or does not support the command. `unknown`: the read failed or timed out. */
38
+ export type DesktopReviewOwnerRead =
39
+ | { kind: "status"; status: DesktopReviewOwnerStatus }
40
+ | { kind: "unknown" }
41
+ | { kind: "unavailable" };
42
+
43
+ /** A write answers the owner's re-read after it, or `failed` when the write's outcome is unknown. */
44
+ export type DesktopReviewOwnerWrite = DesktopReviewOwnerRead | { kind: "failed" };
45
+
46
+ export type DesktopReviewScope = "clone" | "global";
47
+
48
+ export interface DesktopReviewSetRequest {
49
+ enabled: boolean;
50
+ scope: DesktopReviewScope;
51
+ }
52
+
53
+ export interface DesktopReviewService {
54
+ readonly schema: typeof DESKTOP_REVIEW_SERVICE_SCHEMA;
55
+ read(): Promise<DesktopReviewOwnerRead>;
56
+ write(request: DesktopReviewSetRequest): Promise<DesktopReviewOwnerWrite>;
57
+ dispose(): void;
58
+ }
59
+
60
+ export type DesktopReviewWireScope = "enabled" | "disabled" | "unset" | "unknown";
61
+
62
+ /** The wire projection: never a raw owner value, never CLI output. */
63
+ export interface DesktopReviewMode {
64
+ effective: "enabled" | "disabled" | "unknown";
65
+ source: "default" | "global" | "clone" | "unknown";
66
+ scopes: { global: DesktopReviewWireScope; clone: DesktopReviewWireScope };
67
+ }
68
+
69
+ export const UNKNOWN_REVIEW_MODE: DesktopReviewMode = Object.freeze({
70
+ effective: "unknown",
71
+ source: "unknown",
72
+ scopes: Object.freeze({ global: "unknown", clone: "unknown" }),
73
+ }) as DesktopReviewMode;
74
+
75
+ export function isDesktopReviewServiceShape(value: unknown): value is DesktopReviewService {
76
+ if (!value || typeof value !== "object") return false;
77
+ const service = value as Partial<DesktopReviewService>;
78
+ return service.schema === DESKTOP_REVIEW_SERVICE_SCHEMA && typeof service.read === "function" && typeof service.write === "function" && typeof service.dispose === "function";
79
+ }
80
+
81
+ function isScopeValue(value: unknown): value is DesktopReviewOwnerScopeValue {
82
+ return value === "on" || value === "off" || value === "";
83
+ }
84
+
85
+ /** Re-validates an owner status that crossed the event bus; anything unrecognized is undefined. */
86
+ export function parseDesktopReviewOwnerStatus(value: unknown): DesktopReviewOwnerStatus | undefined {
87
+ if (!value || typeof value !== "object") return undefined;
88
+ const status = value as Record<string, unknown>;
89
+ if (!isScopeValue(status.global) || !isScopeValue(status.cloneLocal)) return undefined;
90
+ if (status.effective !== "on" && status.effective !== "off") return undefined;
91
+ if (status.source !== "default" && status.source !== "global" && status.source !== "clone_local") return undefined;
92
+ if (status.revision !== undefined && (typeof status.revision !== "string" || status.revision.length > MAX_TOKEN_LENGTH)) return undefined;
93
+ return {
94
+ global: status.global,
95
+ cloneLocal: status.cloneLocal,
96
+ effective: status.effective,
97
+ source: status.source,
98
+ ...(status.revision === undefined ? {} : { revision: status.revision as string }),
99
+ };
100
+ }
101
+
102
+ function wireScope(value: DesktopReviewOwnerScopeValue): DesktopReviewWireScope {
103
+ return value === "on" ? "enabled" : value === "off" ? "disabled" : "unset";
104
+ }
105
+
106
+ export function projectDesktopReviewMode(status: DesktopReviewOwnerStatus | undefined): DesktopReviewMode {
107
+ if (!status) return { effective: "unknown", source: "unknown", scopes: { global: "unknown", clone: "unknown" } };
108
+ return {
109
+ effective: status.effective === "on" ? "enabled" : "disabled",
110
+ source: status.source === "clone_local" ? "clone" : status.source,
111
+ scopes: { global: wireScope(status.global), clone: wireScope(status.cloneLocal) },
112
+ };
113
+ }
114
+
115
+ export function parseGetReviewModeParams(params: Record<string, unknown>): boolean {
116
+ return Object.keys(params).length === 0;
117
+ }
118
+
119
+ export function parseSetReviewModeParams(params: Record<string, unknown>): (DesktopReviewSetRequest & { confirmationToken?: string }) | undefined {
120
+ for (const key of Object.keys(params)) {
121
+ if (key !== "enabled" && key !== "scope" && key !== "confirmationToken") return undefined;
122
+ }
123
+ if (typeof params.enabled !== "boolean") return undefined;
124
+ if (params.scope !== "clone" && params.scope !== "global") return undefined;
125
+ const token = params.confirmationToken;
126
+ if (token === undefined) return { enabled: params.enabled, scope: params.scope };
127
+ if (typeof token !== "string" || token.length === 0 || token.length > MAX_TOKEN_LENGTH) return undefined;
128
+ return { enabled: params.enabled, scope: params.scope, confirmationToken: token };
129
+ }
130
+
131
+ function sourceLabel(source: DesktopReviewOwnerStatus["source"]): string {
132
+ return source === "global" ? "the machine-wide setting" : source === "clone_local" ? "this repository's setting" : "the default";
133
+ }
134
+
135
+ /**
136
+ * The dialog Desktop must show before a change. It names the current state and
137
+ * the owner's exact semantics; it never carries a path, a command line, or CLI
138
+ * output.
139
+ */
140
+ export function desktopReviewModeConfirmation(request: DesktopReviewSetRequest, status: DesktopReviewOwnerStatus): { title: string; message: string; token: string } {
141
+ const where = request.scope === "global" ? "machine-wide" : "for this repository";
142
+ const title = `Turn review mode ${request.enabled ? "on" : "off"} ${where}?`;
143
+ const current = `Receipt-driven development is currently ${status.effective} (decided by ${sourceLabel(status.source)}).`;
144
+ let effect: string;
145
+ if (request.scope === "global") {
146
+ effect = request.enabled
147
+ ? "Reviews will be on for every repository on this machine, except a repository with its own off setting."
148
+ : "Reviews will be off for every repository on this machine.";
149
+ } else if (!request.enabled) {
150
+ effect = "Reviews will be off for this repository only.";
151
+ } else if (status.global === "off") {
152
+ effect = "This only clears this repository's off setting. Review mode is off machine-wide, and a repository setting cannot override that, so reviews stay off.";
153
+ } else {
154
+ effect = "This clears this repository's off setting; it cannot override a machine-wide off.";
155
+ }
156
+ const message = `${current} ${effect}`;
157
+ return { title, message, token: desktopReviewModeToken(request, title, message, status) };
158
+ }
159
+
160
+ /** sha256 over the request, the exact text shown, and the owner state it was computed from. */
161
+ export function desktopReviewModeToken(request: DesktopReviewSetRequest, title: string, message: string, status: DesktopReviewOwnerStatus): string {
162
+ const state = [status.global, status.cloneLocal, status.effective, status.source, status.revision ?? null];
163
+ return createHash("sha256").update(JSON.stringify([request.enabled, request.scope, title, message, state])).digest("hex");
164
+ }
165
+
166
+ // Process-wide: the review-mode owner and this bridge may load this module
167
+ // separately, so the single-flight gate lives on a registered symbol.
168
+ const SET_GATE = Symbol.for("gentle-shell.desktop-review-mode-set");
169
+ type GateHost = { [SET_GATE]?: boolean };
170
+
171
+ export function beginDesktopReviewModeSet(): boolean {
172
+ const host = globalThis as GateHost;
173
+ if (host[SET_GATE]) return false;
174
+ host[SET_GATE] = true;
175
+ return true;
176
+ }
177
+
178
+ export function endDesktopReviewModeSet(): void {
179
+ (globalThis as GateHost)[SET_GATE] = false;
180
+ }