@evandu/super-pi-server 0.0.0-stage → 0.1.260

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 +250 -3
  2. package/agents/code-auditor.md +64 -0
  3. package/agents/code-designer.md +64 -0
  4. package/agents/coder.md +24 -0
  5. package/agents/critic.md +24 -0
  6. package/agents/document-organizer.md +48 -0
  7. package/agents/patcher.md +27 -0
  8. package/agents/planner.md +30 -0
  9. package/agents/project-leader.md +28 -0
  10. package/agents/reviewer.md +27 -0
  11. package/agents/scout.md +27 -0
  12. package/agents/solution-designer.md +62 -0
  13. package/agents/super-pi-documents/index.ts +92 -0
  14. package/agents/super-pi-feishu/index.ts +88 -0
  15. package/agents/super-pi-knowledge/index.ts +70 -0
  16. package/agents/super-pi-managed-model/index.ts +9 -0
  17. package/agents/super-pi-memory/index.ts +75 -0
  18. package/agents/super-pi-native-tools/index.ts +1 -0
  19. package/agents/super-pi-task/core.ts +2262 -0
  20. package/agents/super-pi-task/index.ts +814 -0
  21. package/agents/super-pi-task/workflow-schema.ts +34 -0
  22. package/agents/super-pi-web/index.ts +32 -0
  23. package/agents/verifier.md +25 -0
  24. package/assets/fonts/LICENSE +92 -0
  25. package/assets/fonts/NotoSansSC-Bold.otf +0 -0
  26. package/assets/fonts/NotoSansSC-Regular.otf +0 -0
  27. package/assets/knowledge-model/README.md +37 -0
  28. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/config.json +31 -0
  29. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/onnx/model_quantized.onnx +0 -0
  30. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/special_tokens_map.json +7 -0
  31. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer.json +21278 -0
  32. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer_config.json +15 -0
  33. package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/vocab.txt +21128 -0
  34. package/bin/npm-prefix.d.ts +1 -0
  35. package/bin/npm-prefix.js +10 -0
  36. package/bin/super-pi.js +349 -0
  37. package/dist/client/.super-pi-client-sources.json +82 -0
  38. package/dist/client/assets/CodeMirrorEditor-mi9rGUce.js +2 -0
  39. package/dist/client/assets/GitDiffEditor-CpoDDQrA.css +1 -0
  40. package/dist/client/assets/GitDiffEditor-FQBfGxOY.js +29 -0
  41. package/dist/client/assets/diff-ChtP43wD.js +1 -0
  42. package/dist/client/assets/dist-AW5lyI3I.js +1 -0
  43. package/dist/client/assets/dist-B3-a6uF0.js +7 -0
  44. package/dist/client/assets/dist-BGxkyh6e.js +1 -0
  45. package/dist/client/assets/dist-BJxfd7yb.js +11 -0
  46. package/dist/client/assets/dist-BRVQKlcp.js +1 -0
  47. package/dist/client/assets/dist-BV64A9Ji.js +1 -0
  48. package/dist/client/assets/dist-BXAOrsjq.js +1 -0
  49. package/dist/client/assets/dist-CKF2Jksy.js +1 -0
  50. package/dist/client/assets/dist-CX_Hm4GS.js +1 -0
  51. package/dist/client/assets/dist-Cus6_oaF.js +2 -0
  52. package/dist/client/assets/dist-Cx00EZKR.js +23 -0
  53. package/dist/client/assets/dist-D71y2EI1.js +1 -0
  54. package/dist/client/assets/dist-D978o95T.js +5 -0
  55. package/dist/client/assets/dist-Dc1MBGAG.js +1 -0
  56. package/dist/client/assets/dist-Dds-qLxY.js +9 -0
  57. package/dist/client/assets/dist-DeD9copn.js +1 -0
  58. package/dist/client/assets/dist-DuvGReu-.js +6 -0
  59. package/dist/client/assets/dist-Dvb0SqcK.js +2 -0
  60. package/dist/client/assets/dist-J6XWqRpB.js +3 -0
  61. package/dist/client/assets/dist-QzbOCAU6.js +1 -0
  62. package/dist/client/assets/dist-moiMRpw_.js +1 -0
  63. package/dist/client/assets/dist-scBkrwv-.js +2 -0
  64. package/dist/client/assets/dockerfile-BDePf5PZ.js +1 -0
  65. package/dist/client/assets/index-Cn7hdrDw.js +350 -0
  66. package/dist/client/assets/index-UfzqdECL.css +1 -0
  67. package/dist/client/assets/powershell-D8WpQPaX.js +1 -0
  68. package/dist/client/assets/properties-C357ku5U.js +1 -0
  69. package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  70. package/dist/client/assets/ruby-ChfNwlCY.js +1 -0
  71. package/dist/client/assets/shell-DwuoZtxw.js +1 -0
  72. package/dist/client/assets/swift-DKB6_1j6.js +1 -0
  73. package/dist/client/assets/toml-BPTmHmyx.js +1 -0
  74. package/dist/client/favicon.ico +0 -0
  75. package/dist/client/favicon.png +0 -0
  76. package/dist/client/favicon.svg +10 -0
  77. package/dist/client/index.html +18 -0
  78. package/dist/extension.d.ts +15 -0
  79. package/dist/extension.js +126 -0
  80. package/dist/server/active-memory.d.ts +247 -0
  81. package/dist/server/active-memory.js +669 -0
  82. package/dist/server/agents.d.ts +245 -0
  83. package/dist/server/agents.js +701 -0
  84. package/dist/server/application/catalog-service.d.ts +98 -0
  85. package/dist/server/application/catalog-service.js +245 -0
  86. package/dist/server/application/conversation-service.d.ts +245 -0
  87. package/dist/server/application/conversation-service.js +654 -0
  88. package/dist/server/application/document-service.d.ts +68 -0
  89. package/dist/server/application/document-service.js +120 -0
  90. package/dist/server/application/extension-ui-service.d.ts +44 -0
  91. package/dist/server/application/extension-ui-service.js +135 -0
  92. package/dist/server/application/feishu-service.d.ts +179 -0
  93. package/dist/server/application/feishu-service.js +358 -0
  94. package/dist/server/application/group-chat-service.d.ts +75 -0
  95. package/dist/server/application/group-chat-service.js +953 -0
  96. package/dist/server/application/group-execution.d.ts +15 -0
  97. package/dist/server/application/group-execution.js +77 -0
  98. package/dist/server/application/project-service.d.ts +464 -0
  99. package/dist/server/application/project-service.js +2523 -0
  100. package/dist/server/application/provider-service.d.ts +174 -0
  101. package/dist/server/application/provider-service.js +510 -0
  102. package/dist/server/application/run-service.d.ts +547 -0
  103. package/dist/server/application/run-service.js +2683 -0
  104. package/dist/server/application/scheduled-task-service.d.ts +184 -0
  105. package/dist/server/application/scheduled-task-service.js +391 -0
  106. package/dist/server/application/session-orchestrator-service.d.ts +388 -0
  107. package/dist/server/application/session-orchestrator-service.js +1146 -0
  108. package/dist/server/application/session-reminder-service.d.ts +59 -0
  109. package/dist/server/application/session-reminder-service.js +141 -0
  110. package/dist/server/application/session-watchdog.d.ts +171 -0
  111. package/dist/server/application/session-watchdog.js +284 -0
  112. package/dist/server/application/workspace-service.d.ts +52 -0
  113. package/dist/server/application/workspace-service.js +186 -0
  114. package/dist/server/application/worktree-service.d.ts +13 -0
  115. package/dist/server/application/worktree-service.js +76 -0
  116. package/dist/server/browser-access.d.ts +34 -0
  117. package/dist/server/browser-access.js +89 -0
  118. package/dist/server/capability.d.ts +105 -0
  119. package/dist/server/capability.js +170 -0
  120. package/dist/server/command-handler.d.ts +93 -0
  121. package/dist/server/command-handler.js +540 -0
  122. package/dist/server/content-search.d.ts +16 -0
  123. package/dist/server/content-search.js +235 -0
  124. package/dist/server/conversation-usage.d.ts +22 -0
  125. package/dist/server/conversation-usage.js +43 -0
  126. package/dist/server/documents.d.ts +152 -0
  127. package/dist/server/documents.js +998 -0
  128. package/dist/server/engineering-learning.d.ts +30 -0
  129. package/dist/server/engineering-learning.js +106 -0
  130. package/dist/server/exec-service.d.ts +45 -0
  131. package/dist/server/exec-service.js +167 -0
  132. package/dist/server/explicit-file-open.d.ts +34 -0
  133. package/dist/server/explicit-file-open.js +105 -0
  134. package/dist/server/failure-text.d.ts +18 -0
  135. package/dist/server/failure-text.js +44 -0
  136. package/dist/server/feishu-cli.d.ts +223 -0
  137. package/dist/server/feishu-cli.js +464 -0
  138. package/dist/server/file-changes.d.ts +119 -0
  139. package/dist/server/file-changes.js +280 -0
  140. package/dist/server/file-diff.d.ts +41 -0
  141. package/dist/server/file-diff.js +97 -0
  142. package/dist/server/file-index.d.ts +100 -0
  143. package/dist/server/file-index.js +296 -0
  144. package/dist/server/file-name-index.d.ts +160 -0
  145. package/dist/server/file-name-index.js +411 -0
  146. package/dist/server/files.d.ts +269 -0
  147. package/dist/server/files.js +748 -0
  148. package/dist/server/git.d.ts +245 -0
  149. package/dist/server/git.js +903 -0
  150. package/dist/server/http-server.d.ts +45 -0
  151. package/dist/server/http-server.js +3830 -0
  152. package/dist/server/index.d.ts +1 -0
  153. package/dist/server/index.js +485 -0
  154. package/dist/server/inline-thinking.d.ts +90 -0
  155. package/dist/server/inline-thinking.js +181 -0
  156. package/dist/server/json-comments.d.ts +14 -0
  157. package/dist/server/json-comments.js +71 -0
  158. package/dist/server/knowledge-embedding-worker.d.ts +1 -0
  159. package/dist/server/knowledge-embedding-worker.js +27 -0
  160. package/dist/server/knowledge-vectors.d.ts +150 -0
  161. package/dist/server/knowledge-vectors.js +385 -0
  162. package/dist/server/knowledge.d.ts +273 -0
  163. package/dist/server/knowledge.js +848 -0
  164. package/dist/server/length-continuation.d.ts +83 -0
  165. package/dist/server/length-continuation.js +118 -0
  166. package/dist/server/logging.d.ts +32 -0
  167. package/dist/server/logging.js +185 -0
  168. package/dist/server/managed-http-tunnel.d.ts +6 -0
  169. package/dist/server/managed-http-tunnel.js +85 -0
  170. package/dist/server/managed-http.d.ts +4 -0
  171. package/dist/server/managed-http.js +85 -0
  172. package/dist/server/managed-model-adapter.d.ts +17 -0
  173. package/dist/server/managed-model-adapter.js +75 -0
  174. package/dist/server/managed-model-gateway.d.ts +49 -0
  175. package/dist/server/managed-model-gateway.js +73 -0
  176. package/dist/server/managed-servers.d.ts +55 -0
  177. package/dist/server/managed-servers.js +396 -0
  178. package/dist/server/managed-ssh.d.ts +26 -0
  179. package/dist/server/managed-ssh.js +73 -0
  180. package/dist/server/manager-vault.d.ts +11 -0
  181. package/dist/server/manager-vault.js +67 -0
  182. package/dist/server/memory-recall.d.ts +25 -0
  183. package/dist/server/memory-recall.js +45 -0
  184. package/dist/server/memory.d.ts +67 -0
  185. package/dist/server/memory.js +216 -0
  186. package/dist/server/message-media.d.ts +7 -0
  187. package/dist/server/message-media.js +54 -0
  188. package/dist/server/model-context-window.d.ts +186 -0
  189. package/dist/server/model-context-window.js +444 -0
  190. package/dist/server/model-filter.d.ts +23 -0
  191. package/dist/server/model-filter.js +48 -0
  192. package/dist/server/models-store.d.ts +36 -0
  193. package/dist/server/models-store.js +132 -0
  194. package/dist/server/native-tool-settings.d.ts +25 -0
  195. package/dist/server/native-tool-settings.js +41 -0
  196. package/dist/server/native-tools-extension.d.ts +2 -0
  197. package/dist/server/native-tools-extension.js +39 -0
  198. package/dist/server/native-tools-service.d.ts +39 -0
  199. package/dist/server/native-tools-service.js +240 -0
  200. package/dist/server/persistence/app-state-repository.d.ts +65 -0
  201. package/dist/server/persistence/app-state-repository.js +1 -0
  202. package/dist/server/persistence/json-state-repository.d.ts +14 -0
  203. package/dist/server/persistence/json-state-repository.js +43 -0
  204. package/dist/server/persistence/open-state-store.d.ts +3 -0
  205. package/dist/server/persistence/open-state-store.js +35 -0
  206. package/dist/server/persistence/rename.d.ts +22 -0
  207. package/dist/server/persistence/rename.js +44 -0
  208. package/dist/server/persistence/sqlite-state-repository.d.ts +12 -0
  209. package/dist/server/persistence/sqlite-state-repository.js +264 -0
  210. package/dist/server/pi-command.d.ts +94 -0
  211. package/dist/server/pi-command.js +265 -0
  212. package/dist/server/pi-native.d.ts +12 -0
  213. package/dist/server/pi-native.js +16 -0
  214. package/dist/server/pi-session-events.d.ts +158 -0
  215. package/dist/server/pi-session-events.js +373 -0
  216. package/dist/server/pi-session-pool.d.ts +233 -0
  217. package/dist/server/pi-session-pool.js +382 -0
  218. package/dist/server/pi-session.d.ts +642 -0
  219. package/dist/server/pi-session.js +655 -0
  220. package/dist/server/presentations/schema.d.ts +120 -0
  221. package/dist/server/presentations/schema.js +128 -0
  222. package/dist/server/provider-network.d.ts +16 -0
  223. package/dist/server/provider-network.js +106 -0
  224. package/dist/server/provider-proxy.d.ts +18 -0
  225. package/dist/server/provider-proxy.js +200 -0
  226. package/dist/server/providers.d.ts +333 -0
  227. package/dist/server/providers.js +989 -0
  228. package/dist/server/remote-bootstrap.d.ts +17 -0
  229. package/dist/server/remote-bootstrap.js +119 -0
  230. package/dist/server/rpc-channel.d.ts +131 -0
  231. package/dist/server/rpc-channel.js +135 -0
  232. package/dist/server/runtime.d.ts +1393 -0
  233. package/dist/server/runtime.js +3286 -0
  234. package/dist/server/scheduled-tasks.d.ts +101 -0
  235. package/dist/server/scheduled-tasks.js +138 -0
  236. package/dist/server/server-context.d.ts +119 -0
  237. package/dist/server/server-context.js +1 -0
  238. package/dist/server/session-cwd.d.ts +19 -0
  239. package/dist/server/session-cwd.js +58 -0
  240. package/dist/server/session-ledger.d.ts +67 -0
  241. package/dist/server/session-ledger.js +76 -0
  242. package/dist/server/session-memory-scope.d.ts +4 -0
  243. package/dist/server/session-memory-scope.js +30 -0
  244. package/dist/server/session-repository.d.ts +100 -0
  245. package/dist/server/session-repository.js +246 -0
  246. package/dist/server/session-tree-scope.d.ts +125 -0
  247. package/dist/server/session-tree-scope.js +337 -0
  248. package/dist/server/skills.d.ts +23 -0
  249. package/dist/server/skills.js +190 -0
  250. package/dist/server/store.d.ts +1062 -0
  251. package/dist/server/store.js +3305 -0
  252. package/dist/server/subagent-runs.d.ts +185 -0
  253. package/dist/server/subagent-runs.js +383 -0
  254. package/dist/server/subagents.d.ts +200 -0
  255. package/dist/server/subagents.js +619 -0
  256. package/dist/server/task-tool.d.ts +282 -0
  257. package/dist/server/task-tool.js +507 -0
  258. package/dist/server/tavily.d.ts +32 -0
  259. package/dist/server/tavily.js +136 -0
  260. package/dist/server/team-context.d.ts +12 -0
  261. package/dist/server/team-context.js +180 -0
  262. package/dist/server/terminal.d.ts +163 -0
  263. package/dist/server/terminal.js +557 -0
  264. package/dist/server/thinking-level-discovery.d.ts +38 -0
  265. package/dist/server/thinking-level-discovery.js +183 -0
  266. package/dist/server/time-zone.d.ts +20 -0
  267. package/dist/server/time-zone.js +32 -0
  268. package/dist/server/tool-calls.d.ts +130 -0
  269. package/dist/server/tool-calls.js +341 -0
  270. package/dist/server/tool-llm.d.ts +72 -0
  271. package/dist/server/tool-llm.js +146 -0
  272. package/dist/server/tool-registry.d.ts +129 -0
  273. package/dist/server/tool-registry.js +249 -0
  274. package/dist/server/tool-runner.d.ts +112 -0
  275. package/dist/server/tool-runner.js +306 -0
  276. package/dist/server/turn-failure.d.ts +48 -0
  277. package/dist/server/turn-failure.js +148 -0
  278. package/dist/server/types.d.ts +2315 -0
  279. package/dist/server/types.js +11 -0
  280. package/dist/server/update-status.d.ts +14 -0
  281. package/dist/server/update-status.js +69 -0
  282. package/dist/server/update.d.ts +46 -0
  283. package/dist/server/update.js +104 -0
  284. package/dist/server/upload.d.ts +35 -0
  285. package/dist/server/upload.js +177 -0
  286. package/dist/server/version.d.ts +33 -0
  287. package/dist/server/version.js +111 -0
  288. package/dist/server/web.d.ts +27 -0
  289. package/dist/server/web.js +183 -0
  290. package/dist/server/workflow-engine.d.ts +47 -0
  291. package/dist/server/workflow-engine.js +255 -0
  292. package/dist/server/workflow.d.ts +329 -0
  293. package/dist/server/workflow.js +469 -0
  294. package/dist/server/workspace-file-indexes.d.ts +2 -0
  295. package/dist/server/workspace-file-indexes.js +61 -0
  296. package/dist/server/workspace-file-operations.d.ts +36 -0
  297. package/dist/server/workspace-file-operations.js +281 -0
  298. package/dist/server/workspace-file-response.d.ts +7 -0
  299. package/dist/server/workspace-file-response.js +79 -0
  300. package/dist/server/ws-server.d.ts +149 -0
  301. package/dist/server/ws-server.js +640 -0
  302. package/dist/shared/agent-order.d.ts +44 -0
  303. package/dist/shared/agent-order.js +50 -0
  304. package/dist/shared/archive-decisions.d.ts +44 -0
  305. package/dist/shared/archive-decisions.js +98 -0
  306. package/dist/shared/background-task.d.ts +25 -0
  307. package/dist/shared/background-task.js +183 -0
  308. package/dist/shared/child-session-lock.d.ts +37 -0
  309. package/dist/shared/child-session-lock.js +59 -0
  310. package/dist/shared/content-search.d.ts +22 -0
  311. package/dist/shared/content-search.js +1 -0
  312. package/dist/shared/conversation-title.d.ts +4 -0
  313. package/dist/shared/conversation-title.js +4 -0
  314. package/dist/shared/file-mentions.d.ts +17 -0
  315. package/dist/shared/file-mentions.js +91 -0
  316. package/dist/shared/git-commit.d.ts +11 -0
  317. package/dist/shared/git-commit.js +1 -0
  318. package/dist/shared/git-graph.d.ts +42 -0
  319. package/dist/shared/git-graph.js +30 -0
  320. package/dist/shared/group-chat.d.ts +132 -0
  321. package/dist/shared/group-chat.js +6 -0
  322. package/dist/shared/managed-servers.d.ts +36 -0
  323. package/dist/shared/managed-servers.js +1 -0
  324. package/dist/shared/message-media.d.ts +12 -0
  325. package/dist/shared/message-media.js +27 -0
  326. package/dist/shared/model-window-label.d.ts +39 -0
  327. package/dist/shared/model-window-label.js +55 -0
  328. package/dist/shared/native-tools.d.ts +43 -0
  329. package/dist/shared/native-tools.js +1 -0
  330. package/dist/shared/pi-cache-warming.d.ts +9 -0
  331. package/dist/shared/pi-cache-warming.js +4 -0
  332. package/dist/shared/project-overview.d.ts +107 -0
  333. package/dist/shared/project-overview.js +92 -0
  334. package/dist/shared/project-presentation.d.ts +55 -0
  335. package/dist/shared/project-presentation.js +111 -0
  336. package/dist/shared/provider-network.d.ts +30 -0
  337. package/dist/shared/provider-network.js +1 -0
  338. package/dist/shared/session-reporting.d.ts +40 -0
  339. package/dist/shared/session-reporting.js +97 -0
  340. package/dist/shared/session-search.d.ts +14 -0
  341. package/dist/shared/session-search.js +1 -0
  342. package/dist/shared/team-context-view.d.ts +33 -0
  343. package/dist/shared/team-context-view.js +49 -0
  344. package/dist/shared/team-context.d.ts +59 -0
  345. package/dist/shared/team-context.js +3 -0
  346. package/dist/shared/terminal-protocol.d.ts +71 -0
  347. package/dist/shared/terminal-protocol.js +1 -0
  348. package/dist/shared/thinking-levels.d.ts +53 -0
  349. package/dist/shared/thinking-levels.js +85 -0
  350. package/dist/shared/tool-glyph.d.ts +7 -0
  351. package/dist/shared/tool-glyph.js +19 -0
  352. package/dist/shared/transcript-order.d.ts +7 -0
  353. package/dist/shared/transcript-order.js +12 -0
  354. package/dist/shared/update-status.d.ts +28 -0
  355. package/dist/shared/update-status.js +1 -0
  356. package/dist/shared/workspace-file-preview.d.ts +4 -0
  357. package/dist/shared/workspace-file-preview.js +28 -0
  358. package/package.json +450 -4
  359. package/scripts/diagnose.mjs +603 -0
  360. package/scripts/patch-brace-expansion.mjs +260 -0
  361. package/scripts/restart-server.mjs +104 -0
  362. package/skills/presentation/SKILL.md +30 -0
  363. package/skills/presentation/references/data.md +10 -0
  364. package/skills/presentation/references/quality.md +12 -0
  365. package/skills/presentation/references/story.md +12 -0
  366. package/skills/presentation/references/templates-and-editing.md +11 -0
  367. package/skills/presentation/references/visual-system.md +10 -0
  368. package/skills/super-pi-agent-development/SKILL.md +25 -0
  369. package/skills/super-pi-development/SKILL.md +31 -0
  370. package/skills/super-pi-tool-development/SKILL.md +39 -0
@@ -0,0 +1,59 @@
1
+ import type { Store } from "../store.js";
2
+ import type { RunService } from "./run-service.js";
3
+ export interface SessionReminderItem {
4
+ reminderId: string;
5
+ conversationId: string;
6
+ note: string;
7
+ repeat: boolean;
8
+ delaySeconds: number;
9
+ fireAt: number;
10
+ createdAt: number;
11
+ }
12
+ export declare class SessionReminderService {
13
+ private readonly runService;
14
+ private readonly store;
15
+ private reminders;
16
+ constructor(runService: RunService, store: Store);
17
+ /**
18
+ * Schedules a wake-up for the same conversation after delaySeconds.
19
+ */
20
+ createReminder(params: {
21
+ conversationId: string;
22
+ delaySeconds: number;
23
+ note?: string;
24
+ repeat?: boolean;
25
+ }): {
26
+ ok: true;
27
+ reminderId: string;
28
+ delaySeconds: number;
29
+ note: string;
30
+ repeat: boolean;
31
+ fireAt: string;
32
+ };
33
+ private arm;
34
+ /**
35
+ * Cancels a pending reminder by ID.
36
+ *
37
+ * A REMINDER BELONGS TO THE CONVERSATION THAT SET IT. `conversationId` is optional only so the
38
+ * service can be called without one; when it IS given, a reminder owned by another conversation
39
+ * is refused, because one session taking another session's timer away is exactly the kind of
40
+ * cross-session reach the tree rules exist to prevent.
41
+ */
42
+ cancelReminder(reminderId: string, conversationId?: string): {
43
+ ok: boolean;
44
+ cancelled?: boolean;
45
+ error?: string;
46
+ };
47
+ /**
48
+ * Lists active reminders for a conversation.
49
+ */
50
+ listReminders(conversationId: string): SessionReminderItem[];
51
+ /**
52
+ * Cancels all pending reminders for a given conversation.
53
+ */
54
+ cancelAllForConversation(conversationId: string): void;
55
+ /**
56
+ * Timer callback: wakes the originating conversation.
57
+ */
58
+ private triggerReminder;
59
+ }
@@ -0,0 +1,141 @@
1
+ import { randomUUID } from "node:crypto";
2
+ export class SessionReminderService {
3
+ runService;
4
+ store;
5
+ reminders = new Map();
6
+ constructor(runService, store) {
7
+ this.runService = runService;
8
+ this.store = store;
9
+ }
10
+ /**
11
+ * Schedules a wake-up for the same conversation after delaySeconds.
12
+ */
13
+ createReminder(params) {
14
+ const { conversationId, repeat = false } = params;
15
+ const delaySeconds = Math.max(1, Math.round(params.delaySeconds));
16
+ // A REMINDER THAT FIRES IMMEDIATELY IS NOT A REMINDER. `Math.max(1, ...)` would quietly turn a
17
+ // zero or negative delay into "one second from now", so the bad number is refused in words
18
+ // instead of becoming a timer the caller never asked for.
19
+ if (!Number.isFinite(params.delaySeconds) || params.delaySeconds <= 0) {
20
+ throw new Error("提醒需要正的秒数:请传 delaySeconds(或 seconds)。");
21
+ }
22
+ const conversation = this.store.conversation(conversationId);
23
+ if (!conversation) {
24
+ throw new Error(`找不到会话:${conversationId}`);
25
+ }
26
+ const reminderId = `rem_${randomUUID()}`;
27
+ const now = Date.now();
28
+ const fireAt = now + delaySeconds * 1000;
29
+ if (fireAt > 8.64e15)
30
+ throw new Error("提醒间隔超出日期范围。");
31
+ const item = {
32
+ reminderId,
33
+ conversationId,
34
+ note: repeat ? "" : (params.note?.trim() ?? ""),
35
+ repeat,
36
+ delaySeconds,
37
+ fireAt,
38
+ createdAt: now,
39
+ };
40
+ this.reminders.set(reminderId, { item, timer: this.arm(item), lastFiredAt: now, delivering: false });
41
+ return {
42
+ ok: true,
43
+ reminderId,
44
+ delaySeconds,
45
+ note: item.note,
46
+ repeat,
47
+ fireAt: new Date(fireAt).toISOString(),
48
+ };
49
+ }
50
+ arm(item) {
51
+ // Node treats delays beyond the signed 32-bit limit as 1 ms. Re-arm long waits in chunks.
52
+ const delay = Math.min(item.fireAt - Date.now(), 2 ** 31 - 1);
53
+ const timer = setTimeout(() => void this.triggerReminder(item.reminderId), delay);
54
+ timer.unref();
55
+ return timer;
56
+ }
57
+ /**
58
+ * Cancels a pending reminder by ID.
59
+ *
60
+ * A REMINDER BELONGS TO THE CONVERSATION THAT SET IT. `conversationId` is optional only so the
61
+ * service can be called without one; when it IS given, a reminder owned by another conversation
62
+ * is refused, because one session taking another session's timer away is exactly the kind of
63
+ * cross-session reach the tree rules exist to prevent.
64
+ */
65
+ cancelReminder(reminderId, conversationId) {
66
+ const existing = this.reminders.get(reminderId);
67
+ if (!existing) {
68
+ return { ok: false, error: `找不到提醒 ${reminderId} 或该提醒已触发` };
69
+ }
70
+ if (conversationId && existing.item.conversationId !== conversationId) {
71
+ return { ok: false, error: `提醒 ${reminderId} 不属于本会话,不能取消。` };
72
+ }
73
+ clearTimeout(existing.timer);
74
+ this.reminders.delete(reminderId);
75
+ return { ok: true, cancelled: true };
76
+ }
77
+ /**
78
+ * Lists active reminders for a conversation.
79
+ */
80
+ listReminders(conversationId) {
81
+ const results = [];
82
+ for (const { item } of this.reminders.values()) {
83
+ if (item.conversationId === conversationId) {
84
+ results.push({ ...item });
85
+ }
86
+ }
87
+ return results.sort((a, b) => a.fireAt - b.fireAt);
88
+ }
89
+ /**
90
+ * Cancels all pending reminders for a given conversation.
91
+ */
92
+ cancelAllForConversation(conversationId) {
93
+ for (const [id, entry] of this.reminders.entries()) {
94
+ if (entry.item.conversationId === conversationId) {
95
+ clearTimeout(entry.timer);
96
+ this.reminders.delete(id);
97
+ }
98
+ }
99
+ }
100
+ /**
101
+ * Timer callback: wakes the originating conversation.
102
+ */
103
+ async triggerReminder(reminderId) {
104
+ const entry = this.reminders.get(reminderId);
105
+ if (!entry)
106
+ return;
107
+ const now = Date.now();
108
+ if (now < entry.item.fireAt) {
109
+ entry.timer = this.arm(entry.item);
110
+ return;
111
+ }
112
+ const { conversationId, note, repeat, delaySeconds } = entry.item;
113
+ const conversation = this.store.conversation(conversationId);
114
+ if (!conversation || conversation.archivedAt) {
115
+ this.reminders.delete(reminderId);
116
+ return;
117
+ }
118
+ if (repeat) {
119
+ entry.item.fireAt = now + delaySeconds * 1000;
120
+ entry.timer = this.arm(entry.item);
121
+ // One wake-up is enough while a previous reminder is running or still in the queue.
122
+ if (entry.delivering || this.store.listWaitingMessages(conversationId).some(message => message.source === "session-reminder"))
123
+ return;
124
+ }
125
+ else {
126
+ this.reminders.delete(reminderId);
127
+ }
128
+ const elapsed = Math.round((now - entry.lastFiredAt) / 1000);
129
+ entry.delivering = true;
130
+ try {
131
+ await this.runService.enqueueReminder(conversationId, note || `距上次计时已过 ${elapsed} 秒。`);
132
+ entry.lastFiredAt = now;
133
+ }
134
+ catch (error) {
135
+ console.warn(`Super Pi: delivering session reminder to ${conversationId} failed:`, error instanceof Error ? error.message : String(error));
136
+ }
137
+ finally {
138
+ entry.delivering = false;
139
+ }
140
+ }
141
+ }
@@ -0,0 +1,171 @@
1
+ /**
2
+ * THE WATCHDOG: A TIMER THAT SAYS "THE CHILD WENT QUIET".
3
+ *
4
+ * WHAT IT IS. A parent session declares, for one child session, how long it is willing to wait for a
5
+ * report before it wants to be told. When that silence runs out, the parent receives ONE message - a
6
+ * reminder - and the clock starts again. That is the whole contract.
7
+ *
8
+ * WHAT IT IS NOT. It has no power over the child. It does not stop the run, does not cancel a queue,
9
+ * does not send anything to the child. Interrupting is the PARENT's decision, and the parent has the
10
+ * tools for it (`session_stop`, `session_dispatch`). A watchdog that killed the run would take away the
11
+ * one judgement the user asked to keep: "父节点可以接受暂时不回报,然后让 watchdog 下一次再反馈".
12
+ *
13
+ * WHY THE REPORT ARRIVES AS A MESSAGE AND NOT AS AN EVENT. The parent is a conversation, and a
14
+ * conversation only knows something when a message says so. A watchdog that pushed a state change would
15
+ * leave the parent's model uninformed - it would keep waiting for a report that was never going to come,
16
+ * because the child had simply decided not to send one.
17
+ *
18
+ * WHY THE STATE IS ON DISK. The parent declared an expectation ("I want to hear from this child within
19
+ * five minutes"). An expectation that evaporates when the server restarts is worse than no watchdog at
20
+ * all: the parent believes it is being watched, and it is not. So the state lives in the workspace, next
21
+ * to the other `.super-pi/` artefacts, and the timers are re-armed when the server comes back.
22
+ */
23
+ /** One watchdog: the parent that declared it, the child it watches, and the clock. */
24
+ export interface WatchdogEntry {
25
+ /** The child session being watched. */
26
+ childConversationId: string;
27
+ /** The session the reminder is delivered to. */
28
+ parentConversationId: string;
29
+ /** The workspace whose `.super-pi/watchdogs.json` holds this entry. */
30
+ workspaceId: string;
31
+ /** How long a silence lasts before the parent is reminded. */
32
+ timeoutSeconds: number;
33
+ /** When the last sign of life was. A report moves it; the clock runs from here. */
34
+ lastSignalAt: number;
35
+ /** Whether the parent has ALREADY been told that the child is no longer working. */
36
+ endedReminded: boolean;
37
+ }
38
+ /** What the owner of a watchdog is told when the clock runs out. */
39
+ export interface WatchdogTimeout {
40
+ childConversationId: string;
41
+ parentConversationId: string;
42
+ timeoutSeconds: number;
43
+ /** How long the silence actually was, in seconds. */
44
+ elapsedSeconds: number;
45
+ /** Whether the child still has work in flight when the clock ran out. */
46
+ childWorking: boolean;
47
+ }
48
+ /** The file name the state lives under, inside the workspace's `.super-pi` directory. */
49
+ export declare const WATCHDOG_STATE_FILE = "watchdogs.json";
50
+ /** The longest silence a parent may declare: a day. Beyond that it is not a watchdog, it is a plan. */
51
+ export declare const WATCHDOG_MAX_TIMEOUT_SECONDS = 86400;
52
+ /** The shortest silence a parent may declare. One second is a test's value, not a parent's. */
53
+ export declare const WATCHDOG_MIN_TIMEOUT_SECONDS = 1;
54
+ /**
55
+ * What the watchdog needs from the server it lives in.
56
+ *
57
+ * The watchdog knows NOTHING about sessions: it counts seconds and asks its owner to deliver a message.
58
+ * The owner is the session orchestrator, which is the only thing that knows how a message reaches a
59
+ * parent conversation.
60
+ */
61
+ export interface WatchdogHost {
62
+ /** Delivers the reminder to the parent. */
63
+ onTimeout: (event: WatchdogTimeout) => Promise<void> | void;
64
+ /** The workspace directory a workspace's state file lives in. */
65
+ workspacePath: (workspaceId: string) => string | undefined;
66
+ /** Every workspace id the server knows about. */
67
+ workspaceIds: () => string[];
68
+ /** Whether the child still has work in flight: a live turn, or a queued message waiting for one. */
69
+ isChildWorking: (childConversationId: string) => boolean;
70
+ /** Where complaints go. */
71
+ warn?: (message: string) => void;
72
+ }
73
+ /**
74
+ * The watchdog manager: the timers, the state, and the state file.
75
+ *
76
+ * ONE WATCHDOG PER CHILD. The parent declares a duration for a child, and the duration belongs to that
77
+ * child - not to the pair of sessions in general - because "how long may this child be quiet" is a
78
+ * property of that child, and the parent may well declare different expectations for different children.
79
+ */
80
+ export declare class SessionWatchdog {
81
+ private readonly host;
82
+ private readonly entries;
83
+ private readonly timers;
84
+ /** One write chain per workspace, so two watchdogs in one workspace cannot interleave a write. */
85
+ private readonly writes;
86
+ /** A shutdown is a fence: no timer may start another reminder after it is raised. */
87
+ private disposed;
88
+ /** Reminders already accepted by a timer before shutdown still own their state and disk writes. */
89
+ private readonly inFlight;
90
+ /** Every caller must observe the same drain, including callers that arrive while it is running. */
91
+ private disposal;
92
+ constructor(host: WatchdogHost);
93
+ /**
94
+ * Declares (or re-declares) the watchdog for a child.
95
+ *
96
+ * `timeoutSeconds` of 0 CANCELS the watchdog: "I am no longer waiting for a report from this one".
97
+ * That is the only way a parent takes the expectation back, and it is deliberately explicit - a
98
+ * watchdog that quietly switched itself off would leave the parent believing it was still watched.
99
+ */
100
+ arm(input: {
101
+ childConversationId: string;
102
+ parentConversationId: string;
103
+ workspaceId: string;
104
+ timeoutSeconds: number;
105
+ }): Promise<void>;
106
+ /**
107
+ * Records a sign of life: the child reported, so the clock starts again.
108
+ *
109
+ * A REPORT RESETS THE CLOCK, IT DOES NOT CANCEL THE WATCHDOG. The parent asked to hear from this
110
+ * child while the child was working; a report that says "the first stage is done, I am continuing"
111
+ * must not be read as "the work is over". Cancelling on the first report would turn the watchdog into
112
+ * a one-shot device, and the whole point of the parent's declaration is that it holds for as long as
113
+ * the child is out of sight.
114
+ */
115
+ noteSignal(childConversationId: string): Promise<void>;
116
+ /** Takes the watchdog away. The parent is no longer waiting. */
117
+ cancel(childConversationId: string): Promise<void>;
118
+ /** The watchdogs that are currently armed. */
119
+ list(): WatchdogEntry[];
120
+ /** The watchdog on one child, if there is one. */
121
+ get(childConversationId: string): WatchdogEntry | undefined;
122
+ /**
123
+ * Re-arms the watchdogs that were on disk when the server stopped.
124
+ *
125
+ * A watchdog whose silence had ALREADY run out while the server was down fires as soon as it comes
126
+ * back: the parent asked to be told after N minutes of silence, and the silence did happen. The
127
+ * remaining time is measured from the last sign of life, not from the restart.
128
+ */
129
+ restore(): Promise<void>;
130
+ /**
131
+ * Stops reminder production and drains work the timers had already accepted.
132
+ *
133
+ * `disposed` is set before the first await so a timer cannot sneak a second reminder into the
134
+ * shutdown window. A reminder that was already in `onTimeout` remains owned by this instance:
135
+ * its parent delivery and the state write must finish before its workspace can be removed.
136
+ */
137
+ dispose(): Promise<void>;
138
+ /**
139
+ * Arms the timer for one entry.
140
+ *
141
+ * THE TIMER IS UNREF'D. A pending reminder must not keep the Node process alive: the server is
142
+ * allowed to shut down with a watchdog in flight, and the state is on disk anyway.
143
+ */
144
+ private schedule;
145
+ private clearTimer;
146
+ /**
147
+ * The clock ran out.
148
+ *
149
+ * THE CHILD'S STATE DECIDES WHETHER THIS IS A REPEAT, AND THE REPEAT IS NOT LOUDER - IT IS THE
150
+ * SAME. Every interval the parent gets the same reminder, because the parent's need is the same:
151
+ * "I still have no news". An interval that grew would make the watchdog quietest exactly when the
152
+ * child has been silent longest, which is backwards.
153
+ *
154
+ * THE ONE EXCEPTION IS A CHILD THAT IS NO LONGER WORKING. A child whose turn ended without reporting
155
+ * is not going to report: the turn is over. The parent is told once that the child has gone quiet
156
+ * AND stopped, and the watchdog then takes itself off - a reminder that repeats for a child that
157
+ * cannot speak any more is noise, not information.
158
+ */
159
+ private fire;
160
+ /** The path of one workspace's state file, or undefined when the workspace is unknown. */
161
+ private statePath;
162
+ /**
163
+ * Writes one workspace's state file.
164
+ *
165
+ * THE WRITES ARE SERIALIZED PER WORKSPACE. Two watchdogs in one workspace each rewrite the same
166
+ * file, and an interleaved pair of writes would leave a file that describes one watchdog twice and
167
+ * loses the other.
168
+ */
169
+ private persist;
170
+ private writeStateFile;
171
+ }
@@ -0,0 +1,284 @@
1
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ /** The file name the state lives under, inside the workspace's `.super-pi` directory. */
4
+ export const WATCHDOG_STATE_FILE = "watchdogs.json";
5
+ /** The longest silence a parent may declare: a day. Beyond that it is not a watchdog, it is a plan. */
6
+ export const WATCHDOG_MAX_TIMEOUT_SECONDS = 86_400;
7
+ /** The shortest silence a parent may declare. One second is a test's value, not a parent's. */
8
+ export const WATCHDOG_MIN_TIMEOUT_SECONDS = 1;
9
+ /**
10
+ * The watchdog manager: the timers, the state, and the state file.
11
+ *
12
+ * ONE WATCHDOG PER CHILD. The parent declares a duration for a child, and the duration belongs to that
13
+ * child - not to the pair of sessions in general - because "how long may this child be quiet" is a
14
+ * property of that child, and the parent may well declare different expectations for different children.
15
+ */
16
+ export class SessionWatchdog {
17
+ host;
18
+ entries = new Map();
19
+ timers = new Map();
20
+ /** One write chain per workspace, so two watchdogs in one workspace cannot interleave a write. */
21
+ writes = new Map();
22
+ /** A shutdown is a fence: no timer may start another reminder after it is raised. */
23
+ disposed = false;
24
+ /** Reminders already accepted by a timer before shutdown still own their state and disk writes. */
25
+ inFlight = new Set();
26
+ /** Every caller must observe the same drain, including callers that arrive while it is running. */
27
+ disposal;
28
+ constructor(host) {
29
+ this.host = host;
30
+ }
31
+ /**
32
+ * Declares (or re-declares) the watchdog for a child.
33
+ *
34
+ * `timeoutSeconds` of 0 CANCELS the watchdog: "I am no longer waiting for a report from this one".
35
+ * That is the only way a parent takes the expectation back, and it is deliberately explicit - a
36
+ * watchdog that quietly switched itself off would leave the parent believing it was still watched.
37
+ */
38
+ async arm(input) {
39
+ if (this.disposed)
40
+ return;
41
+ const { childConversationId } = input;
42
+ if (!childConversationId.trim())
43
+ return;
44
+ this.clearTimer(childConversationId);
45
+ if (!Number.isFinite(input.timeoutSeconds) || input.timeoutSeconds <= 0) {
46
+ // CANCEL. The entry is removed and the file rewritten, so a cancelled watchdog stays cancelled
47
+ // across a restart.
48
+ if (this.entries.delete(childConversationId))
49
+ await this.persist(input.workspaceId);
50
+ return;
51
+ }
52
+ const timeoutSeconds = Math.min(WATCHDOG_MAX_TIMEOUT_SECONDS, Math.max(WATCHDOG_MIN_TIMEOUT_SECONDS, Math.floor(input.timeoutSeconds)));
53
+ const entry = {
54
+ childConversationId,
55
+ parentConversationId: input.parentConversationId,
56
+ workspaceId: input.workspaceId,
57
+ timeoutSeconds,
58
+ lastSignalAt: Date.now(),
59
+ endedReminded: false,
60
+ };
61
+ this.entries.set(childConversationId, entry);
62
+ await this.persist(input.workspaceId);
63
+ this.schedule(entry);
64
+ }
65
+ /**
66
+ * Records a sign of life: the child reported, so the clock starts again.
67
+ *
68
+ * A REPORT RESETS THE CLOCK, IT DOES NOT CANCEL THE WATCHDOG. The parent asked to hear from this
69
+ * child while the child was working; a report that says "the first stage is done, I am continuing"
70
+ * must not be read as "the work is over". Cancelling on the first report would turn the watchdog into
71
+ * a one-shot device, and the whole point of the parent's declaration is that it holds for as long as
72
+ * the child is out of sight.
73
+ */
74
+ async noteSignal(childConversationId) {
75
+ if (this.disposed)
76
+ return;
77
+ const entry = this.entries.get(childConversationId);
78
+ if (!entry)
79
+ return;
80
+ entry.lastSignalAt = Date.now();
81
+ entry.endedReminded = false;
82
+ this.clearTimer(childConversationId);
83
+ this.schedule(entry);
84
+ await this.persist(entry.workspaceId);
85
+ }
86
+ /** Takes the watchdog away. The parent is no longer waiting. */
87
+ async cancel(childConversationId) {
88
+ const entry = this.entries.get(childConversationId);
89
+ this.clearTimer(childConversationId);
90
+ if (!entry)
91
+ return;
92
+ this.entries.delete(childConversationId);
93
+ await this.persist(entry.workspaceId);
94
+ }
95
+ /** The watchdogs that are currently armed. */
96
+ list() {
97
+ return [...this.entries.values()].map((entry) => ({ ...entry }));
98
+ }
99
+ /** The watchdog on one child, if there is one. */
100
+ get(childConversationId) {
101
+ const entry = this.entries.get(childConversationId);
102
+ return entry ? { ...entry } : undefined;
103
+ }
104
+ /**
105
+ * Re-arms the watchdogs that were on disk when the server stopped.
106
+ *
107
+ * A watchdog whose silence had ALREADY run out while the server was down fires as soon as it comes
108
+ * back: the parent asked to be told after N minutes of silence, and the silence did happen. The
109
+ * remaining time is measured from the last sign of life, not from the restart.
110
+ */
111
+ async restore() {
112
+ if (this.disposed)
113
+ return;
114
+ for (const workspaceId of this.host.workspaceIds()) {
115
+ const path = this.statePath(workspaceId);
116
+ if (!path)
117
+ continue;
118
+ let parsed;
119
+ try {
120
+ parsed = JSON.parse(await readFile(path, "utf8"));
121
+ }
122
+ catch {
123
+ // A missing file is the normal case (no watchdog was ever declared). A corrupt one is not
124
+ // fatal either: the worst case of losing it is that the parent stops being reminded, which
125
+ // is the state it was in before this feature existed.
126
+ continue;
127
+ }
128
+ const records = parsed?.watchdogs;
129
+ if (!records || typeof records !== "object")
130
+ continue;
131
+ for (const [childConversationId, record] of Object.entries(records)) {
132
+ if (this.disposed)
133
+ return;
134
+ if (!record || typeof record.timeoutSeconds !== "number" || !record.parentConversationId)
135
+ continue;
136
+ const entry = {
137
+ childConversationId,
138
+ parentConversationId: record.parentConversationId,
139
+ workspaceId: record.workspaceId ?? workspaceId,
140
+ timeoutSeconds: record.timeoutSeconds,
141
+ lastSignalAt: typeof record.lastSignalAt === "number" ? record.lastSignalAt : Date.now(),
142
+ endedReminded: Boolean(record.endedReminded),
143
+ };
144
+ this.entries.set(childConversationId, entry);
145
+ this.schedule(entry);
146
+ }
147
+ }
148
+ }
149
+ /**
150
+ * Stops reminder production and drains work the timers had already accepted.
151
+ *
152
+ * `disposed` is set before the first await so a timer cannot sneak a second reminder into the
153
+ * shutdown window. A reminder that was already in `onTimeout` remains owned by this instance:
154
+ * its parent delivery and the state write must finish before its workspace can be removed.
155
+ */
156
+ dispose() {
157
+ if (this.disposal)
158
+ return this.disposal;
159
+ this.disposed = true;
160
+ for (const timer of this.timers.values())
161
+ clearTimeout(timer);
162
+ this.timers.clear();
163
+ this.disposal = (async () => {
164
+ await Promise.allSettled([...this.inFlight]);
165
+ // In-flight reminders can append state writes after the first drain starts, so take this
166
+ // snapshot only after they have settled.
167
+ await Promise.allSettled([...this.writes.values()]);
168
+ })();
169
+ return this.disposal;
170
+ }
171
+ /**
172
+ * Arms the timer for one entry.
173
+ *
174
+ * THE TIMER IS UNREF'D. A pending reminder must not keep the Node process alive: the server is
175
+ * allowed to shut down with a watchdog in flight, and the state is on disk anyway.
176
+ */
177
+ schedule(entry) {
178
+ if (this.disposed || this.entries.get(entry.childConversationId) !== entry)
179
+ return;
180
+ this.clearTimer(entry.childConversationId);
181
+ const elapsed = Date.now() - entry.lastSignalAt;
182
+ const remaining = Math.max(0, entry.timeoutSeconds * 1000 - elapsed);
183
+ const timer = setTimeout(() => {
184
+ if (this.disposed)
185
+ return;
186
+ const firing = this.fire(entry.childConversationId);
187
+ this.inFlight.add(firing);
188
+ void firing.then(() => this.inFlight.delete(firing), () => this.inFlight.delete(firing));
189
+ }, remaining);
190
+ timer.unref?.();
191
+ this.timers.set(entry.childConversationId, timer);
192
+ }
193
+ clearTimer(childConversationId) {
194
+ const timer = this.timers.get(childConversationId);
195
+ if (timer)
196
+ clearTimeout(timer);
197
+ this.timers.delete(childConversationId);
198
+ }
199
+ /**
200
+ * The clock ran out.
201
+ *
202
+ * THE CHILD'S STATE DECIDES WHETHER THIS IS A REPEAT, AND THE REPEAT IS NOT LOUDER - IT IS THE
203
+ * SAME. Every interval the parent gets the same reminder, because the parent's need is the same:
204
+ * "I still have no news". An interval that grew would make the watchdog quietest exactly when the
205
+ * child has been silent longest, which is backwards.
206
+ *
207
+ * THE ONE EXCEPTION IS A CHILD THAT IS NO LONGER WORKING. A child whose turn ended without reporting
208
+ * is not going to report: the turn is over. The parent is told once that the child has gone quiet
209
+ * AND stopped, and the watchdog then takes itself off - a reminder that repeats for a child that
210
+ * cannot speak any more is noise, not information.
211
+ */
212
+ async fire(childConversationId) {
213
+ if (this.disposed)
214
+ return;
215
+ const entry = this.entries.get(childConversationId);
216
+ if (!entry)
217
+ return;
218
+ const childWorking = this.host.isChildWorking(childConversationId);
219
+ if (!childWorking && entry.endedReminded) {
220
+ await this.cancel(childConversationId);
221
+ return;
222
+ }
223
+ const elapsedSeconds = Math.max(0, Math.round((Date.now() - entry.lastSignalAt) / 1000));
224
+ try {
225
+ await this.host.onTimeout({
226
+ childConversationId,
227
+ parentConversationId: entry.parentConversationId,
228
+ timeoutSeconds: entry.timeoutSeconds,
229
+ elapsedSeconds,
230
+ childWorking,
231
+ });
232
+ }
233
+ catch (error) {
234
+ this.host.warn?.(`Super Pi: watchdog reminder for ${childConversationId} failed: ${error instanceof Error ? error.message : String(error)}`);
235
+ }
236
+ // Shutdown must preserve a reminder that was already accepted, but it must not schedule its
237
+ // successor after that reminder has drained.
238
+ entry.endedReminded = !childWorking;
239
+ entry.lastSignalAt = Date.now();
240
+ await this.persist(entry.workspaceId);
241
+ this.schedule(entry);
242
+ }
243
+ /** The path of one workspace's state file, or undefined when the workspace is unknown. */
244
+ statePath(workspaceId) {
245
+ const workspacePath = this.host.workspacePath(workspaceId);
246
+ if (!workspacePath)
247
+ return undefined;
248
+ return join(workspacePath, ".super-pi", WATCHDOG_STATE_FILE);
249
+ }
250
+ /**
251
+ * Writes one workspace's state file.
252
+ *
253
+ * THE WRITES ARE SERIALIZED PER WORKSPACE. Two watchdogs in one workspace each rewrite the same
254
+ * file, and an interleaved pair of writes would leave a file that describes one watchdog twice and
255
+ * loses the other.
256
+ */
257
+ async persist(workspaceId) {
258
+ const path = this.statePath(workspaceId);
259
+ if (!path)
260
+ return;
261
+ const previous = this.writes.get(workspaceId) ?? Promise.resolve();
262
+ const next = previous.then(() => this.writeStateFile(path, workspaceId)).catch((error) => {
263
+ this.host.warn?.(`Super Pi: watchdog state could not be written: ${error instanceof Error ? error.message : String(error)}`);
264
+ });
265
+ this.writes.set(workspaceId, next);
266
+ await next;
267
+ }
268
+ async writeStateFile(path, workspaceId) {
269
+ const watchdogs = {};
270
+ for (const entry of this.entries.values()) {
271
+ if (entry.workspaceId !== workspaceId)
272
+ continue;
273
+ watchdogs[entry.childConversationId] = {
274
+ parentConversationId: entry.parentConversationId,
275
+ workspaceId: entry.workspaceId,
276
+ timeoutSeconds: entry.timeoutSeconds,
277
+ lastSignalAt: entry.lastSignalAt,
278
+ endedReminded: entry.endedReminded,
279
+ };
280
+ }
281
+ await mkdir(join(path, ".."), { recursive: true });
282
+ await writeFile(path, `${JSON.stringify({ version: 1, watchdogs }, null, 2)}\n`, "utf8");
283
+ }
284
+ }