charness 4.1.1 → 4.3.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 (643) hide show
  1. package/CHANGELOG.md +186 -2
  2. package/DEVELOPMENT.md +65 -11
  3. package/LICENSE +331 -151
  4. package/MIGRATION_3_TO_4.md +8 -5
  5. package/README.md +23 -8
  6. package/SETUP.md +57 -11
  7. package/agent-plugin/README.md +10 -3
  8. package/agent-plugin/skills/charness-delegate-claude/SKILL.md +18 -26
  9. package/agent-plugin/skills/charness-delegate-opencode/SKILL.md +5 -3
  10. package/agent-plugin/skills/charness-orchestration/SKILL.md +57 -16
  11. package/dist/adapters/adapterNames.d.ts +2 -0
  12. package/dist/adapters/adapterNames.d.ts.map +1 -1
  13. package/dist/adapters/adapterNames.js +3 -1
  14. package/dist/adapters/adapterNames.js.map +1 -1
  15. package/dist/adapters/claudeCliAdapter.d.ts +4 -0
  16. package/dist/adapters/claudeCliAdapter.d.ts.map +1 -1
  17. package/dist/adapters/claudeCliAdapter.js +13 -0
  18. package/dist/adapters/claudeCliAdapter.js.map +1 -1
  19. package/dist/adapters/claudeJsonStreamAdapter.d.ts +150 -10
  20. package/dist/adapters/claudeJsonStreamAdapter.d.ts.map +1 -1
  21. package/dist/adapters/claudeJsonStreamAdapter.js +401 -83
  22. package/dist/adapters/claudeJsonStreamAdapter.js.map +1 -1
  23. package/dist/adapters/createAdapter.d.ts +12 -3
  24. package/dist/adapters/createAdapter.d.ts.map +1 -1
  25. package/dist/adapters/createAdapter.js +39 -13
  26. package/dist/adapters/createAdapter.js.map +1 -1
  27. package/dist/adapters/openCodeAdapter.d.ts +65 -2
  28. package/dist/adapters/openCodeAdapter.d.ts.map +1 -1
  29. package/dist/adapters/openCodeAdapter.js +202 -19
  30. package/dist/adapters/openCodeAdapter.js.map +1 -1
  31. package/dist/adapters/terminalAdapter.d.ts.map +1 -1
  32. package/dist/adapters/terminalAdapter.js +2 -1
  33. package/dist/adapters/terminalAdapter.js.map +1 -1
  34. package/dist/apiErrorRetry.d.ts +11 -3
  35. package/dist/apiErrorRetry.d.ts.map +1 -1
  36. package/dist/apiErrorRetry.js +33 -3
  37. package/dist/apiErrorRetry.js.map +1 -1
  38. package/dist/apiRetryKick.d.ts +3 -2
  39. package/dist/apiRetryKick.d.ts.map +1 -1
  40. package/dist/apiRetryKick.js +13 -5
  41. package/dist/apiRetryKick.js.map +1 -1
  42. package/dist/bot.d.ts +2 -6
  43. package/dist/bot.d.ts.map +1 -1
  44. package/dist/bot.js +2586 -693
  45. package/dist/bot.js.map +1 -1
  46. package/dist/cli/claudeProfiles.d.ts +31 -6
  47. package/dist/cli/claudeProfiles.d.ts.map +1 -1
  48. package/dist/cli/claudeProfiles.js +97 -22
  49. package/dist/cli/claudeProfiles.js.map +1 -1
  50. package/dist/cli.d.ts.map +1 -1
  51. package/dist/cli.js +2 -1
  52. package/dist/cli.js.map +1 -1
  53. package/dist/connectors/jira/answerSink.d.ts +102 -7
  54. package/dist/connectors/jira/answerSink.d.ts.map +1 -1
  55. package/dist/connectors/jira/answerSink.js +364 -60
  56. package/dist/connectors/jira/answerSink.js.map +1 -1
  57. package/dist/connectors/jira/answerThreads.d.ts +44 -0
  58. package/dist/connectors/jira/answerThreads.d.ts.map +1 -0
  59. package/dist/connectors/jira/answerThreads.js +85 -0
  60. package/dist/connectors/jira/answerThreads.js.map +1 -0
  61. package/dist/connectors/jira/client.d.ts +67 -2
  62. package/dist/connectors/jira/client.d.ts.map +1 -1
  63. package/dist/connectors/jira/client.js +176 -4
  64. package/dist/connectors/jira/client.js.map +1 -1
  65. package/dist/connectors/jira/config.d.ts +6 -0
  66. package/dist/connectors/jira/config.d.ts.map +1 -1
  67. package/dist/connectors/jira/config.js +14 -1
  68. package/dist/connectors/jira/config.js.map +1 -1
  69. package/dist/connectors/jira/connector.d.ts +19 -0
  70. package/dist/connectors/jira/connector.d.ts.map +1 -1
  71. package/dist/connectors/jira/connector.js +68 -6
  72. package/dist/connectors/jira/connector.js.map +1 -1
  73. package/dist/connectors/jira/contextPreamble.d.ts.map +1 -1
  74. package/dist/connectors/jira/contextPreamble.js +6 -1
  75. package/dist/connectors/jira/contextPreamble.js.map +1 -1
  76. package/dist/connectors/jira/inbound.d.ts +21 -6
  77. package/dist/connectors/jira/inbound.d.ts.map +1 -1
  78. package/dist/connectors/jira/inbound.js +22 -16
  79. package/dist/connectors/jira/inbound.js.map +1 -1
  80. package/dist/connectors/jira/issueBlocks.d.ts +8 -1
  81. package/dist/connectors/jira/issueBlocks.d.ts.map +1 -1
  82. package/dist/connectors/jira/issueBlocks.js +38 -2
  83. package/dist/connectors/jira/issueBlocks.js.map +1 -1
  84. package/dist/connectors/jira/operatorIssue.d.ts +28 -0
  85. package/dist/connectors/jira/operatorIssue.d.ts.map +1 -0
  86. package/dist/connectors/jira/operatorIssue.js +81 -0
  87. package/dist/connectors/jira/operatorIssue.js.map +1 -0
  88. package/dist/connectors/jira/outbound.d.ts +8 -4
  89. package/dist/connectors/jira/outbound.d.ts.map +1 -1
  90. package/dist/connectors/jira/outbound.js +16 -8
  91. package/dist/connectors/jira/outbound.js.map +1 -1
  92. package/dist/connectors/jira/outgoingFileCheck.d.ts +37 -0
  93. package/dist/connectors/jira/outgoingFileCheck.d.ts.map +1 -0
  94. package/dist/connectors/jira/outgoingFileCheck.js +279 -0
  95. package/dist/connectors/jira/outgoingFileCheck.js.map +1 -0
  96. package/dist/connectors/jira/outgoingFiles.d.ts +81 -0
  97. package/dist/connectors/jira/outgoingFiles.d.ts.map +1 -0
  98. package/dist/connectors/jira/outgoingFiles.js +268 -0
  99. package/dist/connectors/jira/outgoingFiles.js.map +1 -0
  100. package/dist/connectors/jira/progressComments.d.ts +69 -0
  101. package/dist/connectors/jira/progressComments.d.ts.map +1 -0
  102. package/dist/connectors/jira/progressComments.js +136 -0
  103. package/dist/connectors/jira/progressComments.js.map +1 -0
  104. package/dist/connectors/jira/prompt.d.ts +5 -1
  105. package/dist/connectors/jira/prompt.d.ts.map +1 -1
  106. package/dist/connectors/jira/prompt.js +23 -3
  107. package/dist/connectors/jira/prompt.js.map +1 -1
  108. package/dist/connectors/jira/promptSpill.js +1 -1
  109. package/dist/connectors/jira/promptSpill.js.map +1 -1
  110. package/dist/connectors/jira/trigger.d.ts +13 -5
  111. package/dist/connectors/jira/trigger.d.ts.map +1 -1
  112. package/dist/connectors/jira/trigger.js +13 -7
  113. package/dist/connectors/jira/trigger.js.map +1 -1
  114. package/dist/connectors/jira/uploadedFiles.d.ts +48 -0
  115. package/dist/connectors/jira/uploadedFiles.d.ts.map +1 -0
  116. package/dist/connectors/jira/uploadedFiles.js +123 -0
  117. package/dist/connectors/jira/uploadedFiles.js.map +1 -0
  118. package/dist/connectors/telegram/commands/backendSwitch.d.ts +9 -1
  119. package/dist/connectors/telegram/commands/backendSwitch.d.ts.map +1 -1
  120. package/dist/connectors/telegram/commands/backendSwitch.js +17 -8
  121. package/dist/connectors/telegram/commands/backendSwitch.js.map +1 -1
  122. package/dist/connectors/telegram/commands/botCore.d.ts +5 -2
  123. package/dist/connectors/telegram/commands/botCore.d.ts.map +1 -1
  124. package/dist/connectors/telegram/commands/botSkills.d.ts +2 -0
  125. package/dist/connectors/telegram/commands/botSkills.d.ts.map +1 -1
  126. package/dist/connectors/telegram/commands/botSkills.js +17 -2
  127. package/dist/connectors/telegram/commands/botSkills.js.map +1 -1
  128. package/dist/connectors/telegram/commands/claudeAccounts.d.ts +11 -0
  129. package/dist/connectors/telegram/commands/claudeAccounts.d.ts.map +1 -1
  130. package/dist/connectors/telegram/commands/claudeAccounts.js +36 -42
  131. package/dist/connectors/telegram/commands/claudeAccounts.js.map +1 -1
  132. package/dist/connectors/telegram/commands/compaction.d.ts +20 -3
  133. package/dist/connectors/telegram/commands/compaction.d.ts.map +1 -1
  134. package/dist/connectors/telegram/commands/compaction.js +268 -96
  135. package/dist/connectors/telegram/commands/compaction.js.map +1 -1
  136. package/dist/connectors/telegram/commands/limitReminders.d.ts +21 -0
  137. package/dist/connectors/telegram/commands/limitReminders.d.ts.map +1 -0
  138. package/dist/connectors/telegram/commands/limitReminders.js +95 -0
  139. package/dist/connectors/telegram/commands/limitReminders.js.map +1 -0
  140. package/dist/connectors/telegram/commands/limitsBoard.d.ts +4 -0
  141. package/dist/connectors/telegram/commands/limitsBoard.d.ts.map +1 -1
  142. package/dist/connectors/telegram/commands/limitsBoard.js +27 -6
  143. package/dist/connectors/telegram/commands/limitsBoard.js.map +1 -1
  144. package/dist/connectors/telegram/commands/modelProviders.d.ts +2 -2
  145. package/dist/connectors/telegram/commands/modelProviders.d.ts.map +1 -1
  146. package/dist/connectors/telegram/commands/modelProviders.js +27 -16
  147. package/dist/connectors/telegram/commands/modelProviders.js.map +1 -1
  148. package/dist/connectors/telegram/commands/questionSetAside.d.ts +111 -0
  149. package/dist/connectors/telegram/commands/questionSetAside.d.ts.map +1 -0
  150. package/dist/connectors/telegram/commands/questionSetAside.js +637 -0
  151. package/dist/connectors/telegram/commands/questionSetAside.js.map +1 -0
  152. package/dist/connectors/telegram/commands/questionWait.d.ts +21 -0
  153. package/dist/connectors/telegram/commands/questionWait.d.ts.map +1 -0
  154. package/dist/connectors/telegram/commands/questionWait.js +109 -0
  155. package/dist/connectors/telegram/commands/questionWait.js.map +1 -0
  156. package/dist/connectors/telegram/commands/topicAccess.js +1 -1
  157. package/dist/connectors/telegram/commands/topicAccess.js.map +1 -1
  158. package/dist/connectors/telegram/commands/topicCommand.d.ts +0 -2
  159. package/dist/connectors/telegram/commands/topicCommand.d.ts.map +1 -1
  160. package/dist/connectors/telegram/commands/topicCommand.js +11 -7
  161. package/dist/connectors/telegram/commands/topicCommand.js.map +1 -1
  162. package/dist/connectors/telegram/commands/topicStatus.d.ts +24 -0
  163. package/dist/connectors/telegram/commands/topicStatus.d.ts.map +1 -0
  164. package/dist/connectors/telegram/commands/topicStatus.js +148 -0
  165. package/dist/connectors/telegram/commands/topicStatus.js.map +1 -0
  166. package/dist/connectors/telegram/commands/workerRoles.d.ts +25 -0
  167. package/dist/connectors/telegram/commands/workerRoles.d.ts.map +1 -0
  168. package/dist/connectors/telegram/commands/workerRoles.js +259 -0
  169. package/dist/connectors/telegram/commands/workerRoles.js.map +1 -0
  170. package/dist/connectors/telegram/firstUpdateBatchNote.d.ts +30 -0
  171. package/dist/connectors/telegram/firstUpdateBatchNote.d.ts.map +1 -0
  172. package/dist/connectors/telegram/firstUpdateBatchNote.js +39 -0
  173. package/dist/connectors/telegram/firstUpdateBatchNote.js.map +1 -0
  174. package/dist/connectors/telegram/groupWelcome.d.ts +12 -0
  175. package/dist/connectors/telegram/groupWelcome.d.ts.map +1 -0
  176. package/dist/connectors/telegram/groupWelcome.js +12 -0
  177. package/dist/connectors/telegram/groupWelcome.js.map +1 -0
  178. package/dist/connectors/telegram/inbound.d.ts +19 -0
  179. package/dist/connectors/telegram/inbound.d.ts.map +1 -1
  180. package/dist/connectors/telegram/inbound.js +41 -3
  181. package/dist/connectors/telegram/inbound.js.map +1 -1
  182. package/dist/connectors/telegram/outputFlushPlan.d.ts +12 -0
  183. package/dist/connectors/telegram/outputFlushPlan.d.ts.map +1 -1
  184. package/dist/connectors/telegram/outputFlushPlan.js +12 -0
  185. package/dist/connectors/telegram/outputFlushPlan.js.map +1 -1
  186. package/dist/connectors/telegram/runnerPairingMessages.d.ts +1 -1
  187. package/dist/connectors/telegram/topicIndicator.d.ts +86 -0
  188. package/dist/connectors/telegram/topicIndicator.d.ts.map +1 -0
  189. package/dist/connectors/telegram/topicIndicator.js +588 -0
  190. package/dist/connectors/telegram/topicIndicator.js.map +1 -0
  191. package/dist/connectors/telegram/waitingTopicRenames.d.ts +39 -0
  192. package/dist/connectors/telegram/waitingTopicRenames.d.ts.map +1 -0
  193. package/dist/connectors/telegram/waitingTopicRenames.js +48 -0
  194. package/dist/connectors/telegram/waitingTopicRenames.js.map +1 -0
  195. package/dist/effortLevels.d.ts +6 -0
  196. package/dist/effortLevels.d.ts.map +1 -1
  197. package/dist/effortLevels.js +6 -1
  198. package/dist/effortLevels.js.map +1 -1
  199. package/dist/i18n/de.d.ts.map +1 -1
  200. package/dist/i18n/de.js +141 -13
  201. package/dist/i18n/de.js.map +1 -1
  202. package/dist/i18n/en.d.ts.map +1 -1
  203. package/dist/i18n/en.js +141 -13
  204. package/dist/i18n/en.js.map +1 -1
  205. package/dist/i18n/es.d.ts.map +1 -1
  206. package/dist/i18n/es.js +141 -13
  207. package/dist/i18n/es.js.map +1 -1
  208. package/dist/i18n/fr.d.ts.map +1 -1
  209. package/dist/i18n/fr.js +141 -13
  210. package/dist/i18n/fr.js.map +1 -1
  211. package/dist/i18n/hi.d.ts.map +1 -1
  212. package/dist/i18n/hi.js +141 -13
  213. package/dist/i18n/hi.js.map +1 -1
  214. package/dist/i18n/ja.d.ts.map +1 -1
  215. package/dist/i18n/ja.js +141 -13
  216. package/dist/i18n/ja.js.map +1 -1
  217. package/dist/i18n/ka.d.ts.map +1 -1
  218. package/dist/i18n/ka.js +141 -13
  219. package/dist/i18n/ka.js.map +1 -1
  220. package/dist/i18n/pt.d.ts.map +1 -1
  221. package/dist/i18n/pt.js +141 -13
  222. package/dist/i18n/pt.js.map +1 -1
  223. package/dist/i18n/ru.d.ts.map +1 -1
  224. package/dist/i18n/ru.js +141 -13
  225. package/dist/i18n/ru.js.map +1 -1
  226. package/dist/i18n/uk.d.ts.map +1 -1
  227. package/dist/i18n/uk.js +141 -13
  228. package/dist/i18n/uk.js.map +1 -1
  229. package/dist/i18n/uz.d.ts.map +1 -1
  230. package/dist/i18n/uz.js +141 -13
  231. package/dist/i18n/uz.js.map +1 -1
  232. package/dist/i18n/zh.d.ts.map +1 -1
  233. package/dist/i18n/zh.js +141 -13
  234. package/dist/i18n/zh.js.map +1 -1
  235. package/dist/instanceSettings.d.ts +43 -3
  236. package/dist/instanceSettings.d.ts.map +1 -1
  237. package/dist/instanceSettings.js +109 -4
  238. package/dist/instanceSettings.js.map +1 -1
  239. package/dist/limitNotes/engine.d.ts +47 -0
  240. package/dist/limitNotes/engine.d.ts.map +1 -0
  241. package/dist/limitNotes/engine.js +98 -0
  242. package/dist/limitNotes/engine.js.map +1 -0
  243. package/dist/limitNotes/noteText.d.ts +33 -0
  244. package/dist/limitNotes/noteText.d.ts.map +1 -0
  245. package/dist/limitNotes/noteText.js +55 -0
  246. package/dist/limitNotes/noteText.js.map +1 -0
  247. package/dist/limitNotes/plan.d.ts +147 -0
  248. package/dist/limitNotes/plan.d.ts.map +1 -0
  249. package/dist/limitNotes/plan.js +323 -0
  250. package/dist/limitNotes/plan.js.map +1 -0
  251. package/dist/limitNotes/reminderChoices.d.ts +30 -0
  252. package/dist/limitNotes/reminderChoices.d.ts.map +1 -0
  253. package/dist/limitNotes/reminderChoices.js +48 -0
  254. package/dist/limitNotes/reminderChoices.js.map +1 -0
  255. package/dist/limitNotes/service.d.ts +106 -0
  256. package/dist/limitNotes/service.d.ts.map +1 -0
  257. package/dist/limitNotes/service.js +169 -0
  258. package/dist/limitNotes/service.js.map +1 -0
  259. package/dist/openCodeOpenQuestion.d.ts +12 -4
  260. package/dist/openCodeOpenQuestion.d.ts.map +1 -1
  261. package/dist/openCodeOpenQuestion.js +16 -7
  262. package/dist/openCodeOpenQuestion.js.map +1 -1
  263. package/dist/openCodeQuestionFlow.d.ts +10 -2
  264. package/dist/openCodeQuestionFlow.d.ts.map +1 -1
  265. package/dist/openCodeQuestionFlow.js +27 -5
  266. package/dist/openCodeQuestionFlow.js.map +1 -1
  267. package/dist/openCodeSessionTitle.js +1 -1
  268. package/dist/openCodeSessionTitle.js.map +1 -1
  269. package/dist/outputTrace.d.ts +19 -0
  270. package/dist/outputTrace.d.ts.map +1 -1
  271. package/dist/outputTrace.js +23 -2
  272. package/dist/outputTrace.js.map +1 -1
  273. package/dist/platform/answerSink.d.ts +23 -0
  274. package/dist/platform/answerSink.d.ts.map +1 -1
  275. package/dist/platform/answerSink.js.map +1 -1
  276. package/dist/platform/operatorAlert.d.ts +24 -0
  277. package/dist/platform/operatorAlert.d.ts.map +1 -0
  278. package/dist/platform/operatorAlert.js +9 -0
  279. package/dist/platform/operatorAlert.js.map +1 -0
  280. package/dist/postToSession.d.ts +43 -2
  281. package/dist/postToSession.d.ts.map +1 -1
  282. package/dist/postToSession.js +46 -4
  283. package/dist/postToSession.js.map +1 -1
  284. package/dist/remoteRun/exitCodes.d.ts +2 -2
  285. package/dist/remoteRun/exitCodes.d.ts.map +1 -1
  286. package/dist/remoteRun/exitCodes.js +2 -2
  287. package/dist/remoteRun/exitCodes.js.map +1 -1
  288. package/dist/remoteRun/foreground.d.ts.map +1 -1
  289. package/dist/remoteRun/foreground.js +5 -1
  290. package/dist/remoteRun/foreground.js.map +1 -1
  291. package/dist/remoteRun/runnerClient.d.ts +1 -1
  292. package/dist/remoteRun/runnerClient.d.ts.map +1 -1
  293. package/dist/remoteRun/runnerClient.js +3 -3
  294. package/dist/remoteRun/runnerClient.js.map +1 -1
  295. package/dist/requests/answerRequest.d.ts +3 -0
  296. package/dist/requests/answerRequest.d.ts.map +1 -1
  297. package/dist/requests/answerRequest.js +9 -2
  298. package/dist/requests/answerRequest.js.map +1 -1
  299. package/dist/requests/limitWaitAnswer.d.ts +14 -0
  300. package/dist/requests/limitWaitAnswer.d.ts.map +1 -1
  301. package/dist/requests/limitWaitAnswer.js +26 -4
  302. package/dist/requests/limitWaitAnswer.js.map +1 -1
  303. package/dist/requests/requestHeader.d.ts +37 -5
  304. package/dist/requests/requestHeader.d.ts.map +1 -1
  305. package/dist/requests/requestHeader.js +46 -17
  306. package/dist/requests/requestHeader.js.map +1 -1
  307. package/dist/requests/scheduleWait.d.ts +79 -0
  308. package/dist/requests/scheduleWait.d.ts.map +1 -0
  309. package/dist/requests/scheduleWait.js +101 -0
  310. package/dist/requests/scheduleWait.js.map +1 -0
  311. package/dist/requests/sessionTurnProbe.d.ts +21 -3
  312. package/dist/requests/sessionTurnProbe.d.ts.map +1 -1
  313. package/dist/requests/sessionTurnProbe.js +18 -3
  314. package/dist/requests/sessionTurnProbe.js.map +1 -1
  315. package/dist/requests/topicRequestSettle.d.ts +13 -7
  316. package/dist/requests/topicRequestSettle.d.ts.map +1 -1
  317. package/dist/requests/topicRequestSettle.js +2 -2
  318. package/dist/requests/topicRequestSettle.js.map +1 -1
  319. package/dist/requests/trackerTurnPoster.d.ts +30 -0
  320. package/dist/requests/trackerTurnPoster.d.ts.map +1 -0
  321. package/dist/requests/trackerTurnPoster.js +36 -0
  322. package/dist/requests/trackerTurnPoster.js.map +1 -0
  323. package/dist/requests/types.d.ts +9 -2
  324. package/dist/requests/types.d.ts.map +1 -1
  325. package/dist/requests/wakeUpEngine.d.ts +188 -11
  326. package/dist/requests/wakeUpEngine.d.ts.map +1 -1
  327. package/dist/requests/wakeUpEngine.js +452 -43
  328. package/dist/requests/wakeUpEngine.js.map +1 -1
  329. package/dist/requests/wakeUpRules.d.ts +122 -12
  330. package/dist/requests/wakeUpRules.d.ts.map +1 -1
  331. package/dist/requests/wakeUpRules.js +117 -15
  332. package/dist/requests/wakeUpRules.js.map +1 -1
  333. package/dist/resumeContext.d.ts +2 -2
  334. package/dist/resumeContext.js +3 -3
  335. package/dist/resumeContext.js.map +1 -1
  336. package/dist/runnerUtility/adminService.js +1 -1
  337. package/dist/runnerUtility/adminService.js.map +1 -1
  338. package/dist/runnerUtility/guiAgent.d.ts +1 -1
  339. package/dist/runnerUtility/guiAgent.d.ts.map +1 -1
  340. package/dist/runnerUtility/guiAgent.js +1 -1
  341. package/dist/runnerUtility/guiAgent.js.map +1 -1
  342. package/dist/runnerUtility/managedNode.d.ts +3 -3
  343. package/dist/runnerUtility/managedNode.d.ts.map +1 -1
  344. package/dist/runnerUtility/managedNode.js +2 -2
  345. package/dist/runnerUtility/managedNode.js.map +1 -1
  346. package/dist/runnerUtility/runnerConfig.d.ts +1 -1
  347. package/dist/runnerUtility/runnerHome.d.ts +1 -1
  348. package/dist/runnerUtility/runnerHome.js +1 -1
  349. package/dist/runnerUtility/serviceCommon.d.ts +1 -1
  350. package/dist/runnerUtility/serviceCommon.d.ts.map +1 -1
  351. package/dist/runnerUtility/serviceCommon.js +1 -1
  352. package/dist/runnerUtility/serviceCommon.js.map +1 -1
  353. package/dist/runners/bootstrapLine.js +1 -1
  354. package/dist/runners/hubPackage.d.ts +1 -1
  355. package/dist/runners/hubPackage.js +3 -3
  356. package/dist/runners/hubPackage.js.map +1 -1
  357. package/dist/runners/hubPaths.d.ts +1 -1
  358. package/dist/runners/hubPaths.d.ts.map +1 -1
  359. package/dist/runners/runnerLock.d.ts +2 -2
  360. package/dist/runners/runnerLock.d.ts.map +1 -1
  361. package/dist/runners/runnerLock.js +2 -2
  362. package/dist/runners/runnerLock.js.map +1 -1
  363. package/dist/scheduler/checkRun.d.ts +2 -0
  364. package/dist/scheduler/checkRun.d.ts.map +1 -1
  365. package/dist/scheduler/checkRun.js +5 -1
  366. package/dist/scheduler/checkRun.js.map +1 -1
  367. package/dist/scheduler/delivery.d.ts +57 -0
  368. package/dist/scheduler/delivery.d.ts.map +1 -1
  369. package/dist/scheduler/delivery.js +152 -14
  370. package/dist/scheduler/delivery.js.map +1 -1
  371. package/dist/scheduler/injection.d.ts +6 -0
  372. package/dist/scheduler/injection.d.ts.map +1 -1
  373. package/dist/scheduler/injection.js +10 -0
  374. package/dist/scheduler/injection.js.map +1 -1
  375. package/dist/scheduler/mcpSurface.d.ts +38 -3
  376. package/dist/scheduler/mcpSurface.d.ts.map +1 -1
  377. package/dist/scheduler/mcpSurface.js +291 -175
  378. package/dist/scheduler/mcpSurface.js.map +1 -1
  379. package/dist/scheduler/rebindResume.d.ts +24 -0
  380. package/dist/scheduler/rebindResume.d.ts.map +1 -1
  381. package/dist/scheduler/rebindResume.js +32 -0
  382. package/dist/scheduler/rebindResume.js.map +1 -1
  383. package/dist/scheduler/scheduleToolWording.d.ts +29 -0
  384. package/dist/scheduler/scheduleToolWording.d.ts.map +1 -0
  385. package/dist/scheduler/scheduleToolWording.js +117 -0
  386. package/dist/scheduler/scheduleToolWording.js.map +1 -0
  387. package/dist/scheduler/scheduledRunTarget.d.ts +90 -0
  388. package/dist/scheduler/scheduledRunTarget.d.ts.map +1 -0
  389. package/dist/scheduler/scheduledRunTarget.js +92 -0
  390. package/dist/scheduler/scheduledRunTarget.js.map +1 -0
  391. package/dist/scheduler/store.d.ts +2 -0
  392. package/dist/scheduler/store.d.ts.map +1 -1
  393. package/dist/scheduler/store.js +2 -0
  394. package/dist/scheduler/store.js.map +1 -1
  395. package/dist/scheduler/types.d.ts +10 -0
  396. package/dist/scheduler/types.d.ts.map +1 -1
  397. package/dist/scheduler/workerWatch.d.ts +2 -0
  398. package/dist/scheduler/workerWatch.d.ts.map +1 -1
  399. package/dist/scheduler/workerWatch.js +3 -3
  400. package/dist/scheduler/workerWatch.js.map +1 -1
  401. package/dist/selfUpdate/constants.d.ts +2 -0
  402. package/dist/selfUpdate/constants.d.ts.map +1 -1
  403. package/dist/selfUpdate/constants.js +3 -1
  404. package/dist/selfUpdate/constants.js.map +1 -1
  405. package/dist/selfUpdate/emittedSource.d.ts.map +1 -1
  406. package/dist/selfUpdate/emittedSource.js +1 -1
  407. package/dist/selfUpdate/emittedSource.js.map +1 -1
  408. package/dist/selfUpdate/updateRuntime.js +1 -1
  409. package/dist/selfUpdate/updateRuntime.js.map +1 -1
  410. package/dist/sessionPick.d.ts +1 -1
  411. package/dist/sessionPick.js +1 -1
  412. package/dist/startupPromptBuffer.d.ts +35 -6
  413. package/dist/startupPromptBuffer.d.ts.map +1 -1
  414. package/dist/startupPromptBuffer.js +70 -21
  415. package/dist/startupPromptBuffer.js.map +1 -1
  416. package/dist/state.d.ts +270 -30
  417. package/dist/state.d.ts.map +1 -1
  418. package/dist/state.js +403 -37
  419. package/dist/state.js.map +1 -1
  420. package/dist/testTimings.d.ts +17 -4
  421. package/dist/testTimings.d.ts.map +1 -1
  422. package/dist/testTimings.js +35 -3
  423. package/dist/testTimings.js.map +1 -1
  424. package/dist/threadContextPreamble.d.ts +7 -0
  425. package/dist/threadContextPreamble.d.ts.map +1 -1
  426. package/dist/threadContextPreamble.js +11 -0
  427. package/dist/threadContextPreamble.js.map +1 -1
  428. package/dist/threadRouting.d.ts +12 -0
  429. package/dist/threadRouting.d.ts.map +1 -1
  430. package/dist/threadRouting.js +15 -0
  431. package/dist/threadRouting.js.map +1 -1
  432. package/dist/todos/midTurn.d.ts +15 -41
  433. package/dist/todos/midTurn.d.ts.map +1 -1
  434. package/dist/todos/midTurn.js +27 -171
  435. package/dist/todos/midTurn.js.map +1 -1
  436. package/dist/todos/registry.d.ts +2 -0
  437. package/dist/todos/registry.d.ts.map +1 -1
  438. package/dist/todos/registry.js +3 -1
  439. package/dist/todos/registry.js.map +1 -1
  440. package/dist/types.d.ts +235 -23
  441. package/dist/types.d.ts.map +1 -1
  442. package/dist/types.js +10 -0
  443. package/dist/types.js.map +1 -1
  444. package/dist/utils/backendSwitchPlan.d.ts +3 -3
  445. package/dist/utils/backendSwitchPlan.js +3 -3
  446. package/dist/utils/claudeAccountAlert.d.ts +104 -0
  447. package/dist/utils/claudeAccountAlert.d.ts.map +1 -0
  448. package/dist/utils/claudeAccountAlert.js +130 -0
  449. package/dist/utils/claudeAccountAlert.js.map +1 -0
  450. package/dist/utils/claudeAccountPickerPlan.d.ts +7 -0
  451. package/dist/utils/claudeAccountPickerPlan.d.ts.map +1 -1
  452. package/dist/utils/claudeAccountPickerPlan.js +13 -2
  453. package/dist/utils/claudeAccountPickerPlan.js.map +1 -1
  454. package/dist/utils/claudeAuthRefusal.d.ts +122 -0
  455. package/dist/utils/claudeAuthRefusal.d.ts.map +1 -0
  456. package/dist/utils/claudeAuthRefusal.js +228 -0
  457. package/dist/utils/claudeAuthRefusal.js.map +1 -0
  458. package/dist/utils/claudeCompactHook.d.ts +9 -7
  459. package/dist/utils/claudeCompactHook.d.ts.map +1 -1
  460. package/dist/utils/claudeCompactHook.js +11 -9
  461. package/dist/utils/claudeCompactHook.js.map +1 -1
  462. package/dist/utils/claudeLimitSwitch.d.ts +1 -1
  463. package/dist/utils/claudeLimitSwitch.d.ts.map +1 -1
  464. package/dist/utils/claudeLimitSwitch.js +1 -1
  465. package/dist/utils/claudeLimitSwitch.js.map +1 -1
  466. package/dist/utils/claudeProfileSync.d.ts +88 -0
  467. package/dist/utils/claudeProfileSync.d.ts.map +1 -0
  468. package/dist/utils/claudeProfileSync.js +198 -0
  469. package/dist/utils/claudeProfileSync.js.map +1 -0
  470. package/dist/utils/claudeProfileUsageViews.d.ts +54 -2
  471. package/dist/utils/claudeProfileUsageViews.d.ts.map +1 -1
  472. package/dist/utils/claudeProfileUsageViews.js +44 -1
  473. package/dist/utils/claudeProfileUsageViews.js.map +1 -1
  474. package/dist/utils/claudeProfiles.d.ts +101 -6
  475. package/dist/utils/claudeProfiles.d.ts.map +1 -1
  476. package/dist/utils/claudeProfiles.js +297 -23
  477. package/dist/utils/claudeProfiles.js.map +1 -1
  478. package/dist/utils/claudeRuntimeInfo.d.ts +24 -0
  479. package/dist/utils/claudeRuntimeInfo.d.ts.map +1 -1
  480. package/dist/utils/claudeRuntimeInfo.js +128 -3
  481. package/dist/utils/claudeRuntimeInfo.js.map +1 -1
  482. package/dist/utils/claudeScheduledJobs.d.ts +6 -2
  483. package/dist/utils/claudeScheduledJobs.d.ts.map +1 -1
  484. package/dist/utils/claudeScheduledJobs.js +10 -2
  485. package/dist/utils/claudeScheduledJobs.js.map +1 -1
  486. package/dist/utils/claudeSessionTitle.js +1 -1
  487. package/dist/utils/claudeSessionTitle.js.map +1 -1
  488. package/dist/utils/claudeStreamJson.d.ts +28 -2
  489. package/dist/utils/claudeStreamJson.d.ts.map +1 -1
  490. package/dist/utils/claudeStreamJson.js +33 -3
  491. package/dist/utils/claudeStreamJson.js.map +1 -1
  492. package/dist/utils/claudeTranscriptEpoch.d.ts +14 -0
  493. package/dist/utils/claudeTranscriptEpoch.d.ts.map +1 -1
  494. package/dist/utils/claudeTranscriptEpoch.js +79 -19
  495. package/dist/utils/claudeTranscriptEpoch.js.map +1 -1
  496. package/dist/utils/claudeUsage.d.ts +103 -7
  497. package/dist/utils/claudeUsage.d.ts.map +1 -1
  498. package/dist/utils/claudeUsage.js +108 -15
  499. package/dist/utils/claudeUsage.js.map +1 -1
  500. package/dist/utils/claudeUsageRefresher.d.ts +8 -1
  501. package/dist/utils/claudeUsageRefresher.d.ts.map +1 -1
  502. package/dist/utils/claudeUsageRefresher.js +11 -1
  503. package/dist/utils/claudeUsageRefresher.js.map +1 -1
  504. package/dist/utils/compactOnIdle.d.ts +139 -29
  505. package/dist/utils/compactOnIdle.d.ts.map +1 -1
  506. package/dist/utils/compactOnIdle.js +163 -36
  507. package/dist/utils/compactOnIdle.js.map +1 -1
  508. package/dist/utils/conversationEnding.d.ts +76 -0
  509. package/dist/utils/conversationEnding.d.ts.map +1 -0
  510. package/dist/utils/conversationEnding.js +117 -0
  511. package/dist/utils/conversationEnding.js.map +1 -0
  512. package/dist/utils/cutButtonLabel.d.ts +7 -0
  513. package/dist/utils/cutButtonLabel.d.ts.map +1 -0
  514. package/dist/utils/cutButtonLabel.js +17 -0
  515. package/dist/utils/cutButtonLabel.js.map +1 -0
  516. package/dist/utils/doctorLine.d.ts +10 -0
  517. package/dist/utils/doctorLine.d.ts.map +1 -1
  518. package/dist/utils/doctorLine.js +16 -0
  519. package/dist/utils/doctorLine.js.map +1 -1
  520. package/dist/utils/fallbackBridge.d.ts.map +1 -1
  521. package/dist/utils/fallbackBridge.js +2 -1
  522. package/dist/utils/fallbackBridge.js.map +1 -1
  523. package/dist/utils/fallbackChain.d.ts +2 -0
  524. package/dist/utils/fallbackChain.d.ts.map +1 -1
  525. package/dist/utils/fallbackChain.js +4 -3
  526. package/dist/utils/fallbackChain.js.map +1 -1
  527. package/dist/utils/fallbackSwitchNotice.d.ts +11 -4
  528. package/dist/utils/fallbackSwitchNotice.d.ts.map +1 -1
  529. package/dist/utils/fallbackSwitchNotice.js +9 -5
  530. package/dist/utils/fallbackSwitchNotice.js.map +1 -1
  531. package/dist/utils/fileSendService.d.ts +31 -0
  532. package/dist/utils/fileSendService.d.ts.map +1 -1
  533. package/dist/utils/fileSendService.js +45 -2
  534. package/dist/utils/fileSendService.js.map +1 -1
  535. package/dist/utils/jsonStreamHost.d.ts +2 -0
  536. package/dist/utils/jsonStreamHost.d.ts.map +1 -1
  537. package/dist/utils/jsonStreamHost.js +4 -1
  538. package/dist/utils/jsonStreamHost.js.map +1 -1
  539. package/dist/utils/jsonStreamPendingWork.d.ts +14 -2
  540. package/dist/utils/jsonStreamPendingWork.d.ts.map +1 -1
  541. package/dist/utils/jsonStreamPendingWork.js +25 -5
  542. package/dist/utils/jsonStreamPendingWork.js.map +1 -1
  543. package/dist/utils/jsonStreamUnconsumedInput.d.ts +80 -0
  544. package/dist/utils/jsonStreamUnconsumedInput.d.ts.map +1 -0
  545. package/dist/utils/jsonStreamUnconsumedInput.js +99 -0
  546. package/dist/utils/jsonStreamUnconsumedInput.js.map +1 -0
  547. package/dist/utils/limitEpisodeRecovery.d.ts +43 -4
  548. package/dist/utils/limitEpisodeRecovery.d.ts.map +1 -1
  549. package/dist/utils/limitEpisodeRecovery.js +107 -20
  550. package/dist/utils/limitEpisodeRecovery.js.map +1 -1
  551. package/dist/utils/limitHeldPrompts.d.ts +21 -11
  552. package/dist/utils/limitHeldPrompts.d.ts.map +1 -1
  553. package/dist/utils/limitHeldPrompts.js +12 -9
  554. package/dist/utils/limitHeldPrompts.js.map +1 -1
  555. package/dist/utils/limitRemindersScreenPlan.d.ts +63 -0
  556. package/dist/utils/limitRemindersScreenPlan.d.ts.map +1 -0
  557. package/dist/utils/limitRemindersScreenPlan.js +121 -0
  558. package/dist/utils/limitRemindersScreenPlan.js.map +1 -0
  559. package/dist/utils/limitSwitchPlan.d.ts +44 -0
  560. package/dist/utils/limitSwitchPlan.d.ts.map +1 -0
  561. package/dist/utils/limitSwitchPlan.js +54 -0
  562. package/dist/utils/limitSwitchPlan.js.map +1 -0
  563. package/dist/utils/limitsBoardPlan.d.ts +16 -7
  564. package/dist/utils/limitsBoardPlan.d.ts.map +1 -1
  565. package/dist/utils/limitsBoardPlan.js +41 -9
  566. package/dist/utils/limitsBoardPlan.js.map +1 -1
  567. package/dist/utils/lostWork.d.ts +16 -2
  568. package/dist/utils/lostWork.d.ts.map +1 -1
  569. package/dist/utils/lostWork.js +16 -3
  570. package/dist/utils/lostWork.js.map +1 -1
  571. package/dist/utils/messageContext.d.ts +2 -2
  572. package/dist/utils/messageContext.js +2 -2
  573. package/dist/utils/midTurnInbox.d.ts +79 -0
  574. package/dist/utils/midTurnInbox.d.ts.map +1 -0
  575. package/dist/utils/midTurnInbox.js +265 -0
  576. package/dist/utils/midTurnInbox.js.map +1 -0
  577. package/dist/utils/promptDelivery.d.ts +70 -22
  578. package/dist/utils/promptDelivery.d.ts.map +1 -1
  579. package/dist/utils/promptDelivery.js +100 -42
  580. package/dist/utils/promptDelivery.js.map +1 -1
  581. package/dist/utils/questionWaitChoices.d.ts +20 -0
  582. package/dist/utils/questionWaitChoices.d.ts.map +1 -0
  583. package/dist/utils/questionWaitChoices.js +31 -0
  584. package/dist/utils/questionWaitChoices.js.map +1 -0
  585. package/dist/utils/questionWaitPlan.d.ts +166 -0
  586. package/dist/utils/questionWaitPlan.d.ts.map +1 -0
  587. package/dist/utils/questionWaitPlan.js +237 -0
  588. package/dist/utils/questionWaitPlan.js.map +1 -0
  589. package/dist/utils/replyQuote.d.ts +10 -0
  590. package/dist/utils/replyQuote.d.ts.map +1 -1
  591. package/dist/utils/replyQuote.js +18 -7
  592. package/dist/utils/replyQuote.js.map +1 -1
  593. package/dist/utils/talkerAccess.d.ts +2 -2
  594. package/dist/utils/talkerAccess.d.ts.map +1 -1
  595. package/dist/utils/talkerAccess.js +5 -4
  596. package/dist/utils/talkerAccess.js.map +1 -1
  597. package/dist/utils/telegramError.d.ts +6 -0
  598. package/dist/utils/telegramError.d.ts.map +1 -1
  599. package/dist/utils/telegramError.js +12 -0
  600. package/dist/utils/telegramError.js.map +1 -1
  601. package/dist/utils/topicAccessScreenPlan.d.ts +1 -1
  602. package/dist/utils/topicAccessScreenPlan.d.ts.map +1 -1
  603. package/dist/utils/topicAccessScreenPlan.js +3 -7
  604. package/dist/utils/topicAccessScreenPlan.js.map +1 -1
  605. package/dist/utils/topicIndicatorPlan.d.ts +153 -0
  606. package/dist/utils/topicIndicatorPlan.d.ts.map +1 -0
  607. package/dist/utils/topicIndicatorPlan.js +224 -0
  608. package/dist/utils/topicIndicatorPlan.js.map +1 -0
  609. package/dist/utils/topicIndicatorSources.d.ts +40 -0
  610. package/dist/utils/topicIndicatorSources.d.ts.map +1 -0
  611. package/dist/utils/topicIndicatorSources.js +31 -0
  612. package/dist/utils/topicIndicatorSources.js.map +1 -0
  613. package/dist/utils/topicRequest.d.ts +3 -0
  614. package/dist/utils/topicRequest.d.ts.map +1 -1
  615. package/dist/utils/topicRequest.js +4 -0
  616. package/dist/utils/topicRequest.js.map +1 -1
  617. package/dist/utils/topicStatusPlan.d.ts +90 -0
  618. package/dist/utils/topicStatusPlan.d.ts.map +1 -0
  619. package/dist/utils/topicStatusPlan.js +160 -0
  620. package/dist/utils/topicStatusPlan.js.map +1 -0
  621. package/dist/utils/workerRoles.d.ts +122 -0
  622. package/dist/utils/workerRoles.d.ts.map +1 -0
  623. package/dist/utils/workerRoles.js +160 -0
  624. package/dist/utils/workerRoles.js.map +1 -0
  625. package/dist/utils/workerRolesScreenPlan.d.ts +118 -0
  626. package/dist/utils/workerRolesScreenPlan.d.ts.map +1 -0
  627. package/dist/utils/workerRolesScreenPlan.js +232 -0
  628. package/dist/utils/workerRolesScreenPlan.js.map +1 -0
  629. package/dist/utils/workingSignals.d.ts +49 -0
  630. package/dist/utils/workingSignals.d.ts.map +1 -0
  631. package/dist/utils/workingSignals.js +41 -0
  632. package/dist/utils/workingSignals.js.map +1 -0
  633. package/licensing.json +88 -0
  634. package/npm-shrinkwrap.json +2 -2
  635. package/package.json +7 -7
  636. package/{remoteRun → runner-scripts}/runner.sh +6 -3
  637. package/COMMERCIAL-LICENSE.md +0 -142
  638. package/dist/agentTrigger.d.ts +0 -28
  639. package/dist/agentTrigger.d.ts.map +0 -1
  640. package/dist/agentTrigger.js +0 -53
  641. package/dist/agentTrigger.js.map +0 -1
  642. /package/{remoteRun → runner-scripts}/ensureNode.sh +0 -0
  643. /package/{remoteRun → runner-scripts}/runner-package-lock.json +0 -0
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RequestWakeUpEngine = exports.restartWakeStaggerMs = exports.turnActivityPersistStepMs = exports.unwatchedSweepMs = exports.watchedTurnPollMs = void 0;
4
4
  const sessionKey_1 = require("../sessionKey");
5
+ const scheduleWait_1 = require("./scheduleWait");
5
6
  const requestHeader_1 = require("./requestHeader");
6
7
  const answerTail_1 = require("./answerTail");
7
8
  const wakeUpRules_1 = require("./wakeUpRules");
@@ -23,6 +24,12 @@ const wakeUpRules_1 = require("./wakeUpRules");
23
24
  *
24
25
  * A wake-up is a reminder forwarded into the SAME session — never a request.
25
26
  * The answer tails are in memory as well: a restart forgets them.
27
+ *
28
+ * A request waits for its schedule while a job armed under it counts (`scheduleWait.ts`, read through the optional
29
+ * `getScheduleWait` port at every decision): the rules then neither prod it nor wake it at a restart, and a turn a scheduler
30
+ * fire started (`isScheduledRun`) that ends without an answer is not silence. A request that owes a person an answer
31
+ * (`checkIsAnswerOwed`) is never held: it is read from the persisted request, so a turn whose watch a restart lost still reaches the
32
+ * backstop. The complete table is `README.md` § Wake-ups.
26
33
  */
27
34
  /** How often a watched turn is polled for its end. */
28
35
  exports.watchedTurnPollMs = 10_000;
@@ -36,23 +43,59 @@ exports.turnActivityPersistStepMs = 60_000;
36
43
  * whose post never settled, superseding the old one — has the time to run before anything is woken.
37
44
  */
38
45
  exports.restartWakeStaggerMs = 30_000;
46
+ /**
47
+ * R21's test: the live process the request prompt was written to has taken it in (a process that replaced it — a re-spawn — lists
48
+ * nothing unread, and that is not the prompt read; one never written to any process is not taken in).
49
+ */
50
+ function checkIsPromptConsumedByLiveProcess(turn, probe) {
51
+ const isAnotherProcess = turn.writtenToProcess !== undefined && probe.processGeneration !== undefined && turn.writtenToProcess !== probe.processGeneration;
52
+ return turn.isRequestPrompt && probe.isActive && !isAnotherProcess && (0, wakeUpRules_1.checkIsTurnInputConsumed)(turn, probe);
53
+ }
54
+ /**
55
+ * Whether a request prompt is in the conversation the agent had at this moment — what an ending command reads to tell a person whose
56
+ * words nobody will read ({@link RequestWakeUpEngine.readPromptsReachedOrHeld}): taken in by the live process it was written to (R21, on
57
+ * the backend's own unread signal — the json-stream Claude's echo, OpenCode's count of the prompts not yet seen as a user message), or,
58
+ * on the one backend with no such signal (the tmux Claude), written to it — it keeps what was typed in its own conversation, where a
59
+ * move's bridge or a later resume reads it, busy or output seen yet or not (review 27 nit 3).
60
+ */
61
+ function checkIsPromptTakenInNow(turn, probe) {
62
+ if (probe.hasUnconsumedInput !== null)
63
+ return checkIsPromptConsumedByLiveProcess(turn, probe);
64
+ return turn.isRequestPrompt && probe.isActive && turn.writtenToProcess !== undefined && turn.writtenToProcess === probe.processGeneration;
65
+ }
66
+ /** Whether a request prompt waits in a start's or a stop's window (no process named yet): that window's end delivers or drops it. */
67
+ function checkIsPromptWaitingInWindow(turn, probe) {
68
+ return turn.isRequestPrompt && turn.writtenToProcess === undefined && probe.isWindowOpen === true;
69
+ }
39
70
  class RequestWakeUpEngine {
40
71
  deps;
41
72
  now;
42
73
  /** The watched turns by REQUEST id (a conversation may hold one per requester). */
43
74
  watched = new Map();
75
+ /**
76
+ * The process the last prompt written into each conversation went to (serialized key → process), stamped at the write choke point
77
+ * ({@link noteWrite}): a watch tracked after that process went down — a re-spawn takes it down right after a post's write, and the
78
+ * post tracks several awaits later — is named from it, never left unnamed (review 22 nit 2).
79
+ */
80
+ writtenProcesses = new Map();
44
81
  /** Last persisted live-turn activity per request id, for the persist step. */
45
82
  activityPersistedAt = new Map();
46
83
  /** The tail after the latest final answer, by serialized conversation key. */
47
84
  answerTails = new Map();
48
85
  /** The request whose tail was last reminded and how often, by serialized conversation key (the reminder cap). */
49
86
  answerTailReminders = new Map();
87
+ /** How many posts are on their way to their write, by serialized conversation key ({@link notePostInFlight}). */
88
+ postsInFlight = new Map();
89
+ /** The wake-up deliveries handed to a post in flight, by serialized conversation key, in the order they were decided. */
90
+ wakesHandedToPost = new Map();
50
91
  pollTimer = null;
51
92
  sweepTimer = null;
52
93
  isPolling = false;
53
94
  isSweeping = false;
54
95
  /** The `restarted` wakes waiting for their turn, by serialized conversation key (one per conversation). */
55
96
  restartWakeTimers = new Map();
97
+ /** Conversations whose pending restart wake was asked for by a LOST-WORK loss, not by a boot: no watch holds that one. */
98
+ lostWorkWakes = new Set();
56
99
  /** When the last scheduled `restarted` wake is due: the next one is a stagger after it. */
57
100
  lastRestartWakeDueAt = 0;
58
101
  constructor(deps) {
@@ -85,24 +128,39 @@ class RequestWakeUpEngine {
85
128
  for (const timer of this.restartWakeTimers.values())
86
129
  clearTimeout(timer);
87
130
  this.restartWakeTimers.clear();
131
+ this.lostWorkWakes.clear();
88
132
  }
89
133
  /**
90
134
  * @description The conversation's process is gone while it holds open requests (a boot that did not find it, or a
91
135
  * process that died with its own work pending): wake them `restarted`, a stagger after the last wake scheduled so,
92
136
  * instead of at the backstop. One wake per conversation at a time — a second call while one waits changes nothing.
93
137
  * Whether the requests still need it is decided when it is due (`decideRestartWake`). Returns `false` when one
94
- * was already waiting. The timer is unref'd: it never keeps the process alive.
138
+ * was already waiting, or when every open request is held by a watch that is not failing and owes nobody an answer
139
+ * (`checkIsHeldBetweenTurns`: a BOOT wakes nobody then, as in a topic). `isLostWork` — the process ended with the agent's own work
140
+ * pending — is the other event: a topic resumes on it whatever is armed, so no watch holds the wake, and a boot wake already
141
+ * waiting becomes one (the flag is remembered, the call still returns `false`). The timer is unref'd: it never keeps the process alive.
95
142
  */
96
- scheduleRestartWake(key) {
143
+ scheduleRestartWake(key, options = {}) {
97
144
  const conversationKey = (0, sessionKey_1.keyToString)(key);
98
- if (this.restartWakeTimers.has(conversationKey))
145
+ const isLostWork = options.isLostWork === true;
146
+ if (this.restartWakeTimers.has(conversationKey)) {
147
+ if (isLostWork)
148
+ this.lostWorkWakes.add(conversationKey);
99
149
  return false;
150
+ }
151
+ // Every open request is held by a watch that is not failing and owes nobody an answer: nobody is woken at a boot, and no stagger slot is spent on it.
152
+ const openRequests = this.deps.ledger.listOpenRequestsOf(key);
153
+ if (!isLostWork && openRequests.length > 0 && openRequests.every((request) => (0, scheduleWait_1.checkIsHeldBetweenTurns)(request, this.getScheduleWait(key, request))))
154
+ return false;
155
+ if (isLostWork)
156
+ this.lostWorkWakes.add(conversationKey);
100
157
  const staggerMs = this.deps.restartWakeStaggerMs ?? exports.restartWakeStaggerMs;
101
158
  const dueAt = Math.max(this.now(), this.lastRestartWakeDueAt) + staggerMs;
102
159
  this.lastRestartWakeDueAt = dueAt;
103
160
  const timer = setTimeout(() => {
104
161
  this.restartWakeTimers.delete(conversationKey);
105
- void this.wakeAfterRestart(key).catch((e) => logWakeUpFailure(`the restart wake of ${conversationKey}`, e));
162
+ const isLostWorkWake = this.lostWorkWakes.delete(conversationKey);
163
+ void this.wakeAfterRestart(key, { isLostWork: isLostWorkWake }).catch((e) => logWakeUpFailure(`the restart wake of ${conversationKey}`, e));
106
164
  }, dueAt - this.now());
107
165
  timer.unref?.();
108
166
  this.restartWakeTimers.set(conversationKey, timer);
@@ -115,14 +173,14 @@ class RequestWakeUpEngine {
115
173
  * continuation nudge's turn is. A request at the cap gets its alert instead. A delivery that fails is logged and
116
174
  * left to the backstop (counted from now): no alert of its own. Exported for tests (the timer calls it).
117
175
  */
118
- async wakeAfterRestart(key) {
176
+ async wakeAfterRestart(key, options = {}) {
119
177
  const probe = this.deps.probeTurn(key);
120
178
  const nowMs = this.now();
121
179
  const woken = [];
122
180
  for (const request of this.deps.ledger.listOpenRequestsOf(key)) {
123
181
  if (this.watched.has(request.id))
124
182
  continue;
125
- const decision = (0, wakeUpRules_1.decideRestartWake)(request, probe, nowMs);
183
+ const decision = (0, wakeUpRules_1.decideRestartWake)(request, probe, nowMs, this.getScheduleWait(key, request), options.isLostWork === true);
126
184
  if (decision.kind === 'alert') {
127
185
  await this.applyDecision(key, request, decision);
128
186
  continue;
@@ -137,11 +195,25 @@ class RequestWakeUpEngine {
137
195
  if (updated && !isStoppedMeanwhile)
138
196
  woken.push(updated);
139
197
  }
140
- if (woken.length === 0)
198
+ if (woken.length === 0) {
199
+ // The bot already said the conversation is woken for its lost work: when no request could be, say so (each skip is a decision of
200
+ // `decideRestartWake`: a request given up on, at the cap, with a post retry or a follow-up that can still fire, busy or blocked).
201
+ if (options.isLostWork === true)
202
+ console.warn(`[requests] ${(0, sessionKey_1.keyToString)(key)}: the lost-work wake woke no request; the note rides the next prompt`);
141
203
  return;
204
+ }
205
+ // A lost-work wake of a request a passing watch holds and that owes nobody: the agent is asked to answer only with what the requester
206
+ // must know (the fire header's wording). Every other request — a boot's, one that owes an answer — gets the plain «answer it».
207
+ const isAnswerOptional = (request) => options.isLostWork === true && (0, scheduleWait_1.checkIsHeldBetweenTurns)(request, this.getScheduleWait(key, request));
142
208
  const buildMessage = (requests) => {
143
- const messages = requests.map((request) => (0, requestHeader_1.getWakeUpMessage)(request, 'restarted'));
144
- return { reason: 'restarted', text: messages.map((message) => message.text).join('\n\n'), isRequestPrompt: messages.some((message) => message.isRequestPrompt) };
209
+ const messages = requests.map((request) => (0, requestHeader_1.getWakeUpMessage)(request, 'restarted', { isAnswerOptional: isAnswerOptional(request) }));
210
+ return {
211
+ reason: 'restarted',
212
+ text: messages.map((message) => message.text).join('\n\n'),
213
+ isRequestPrompt: messages.some((message) => message.isRequestPrompt),
214
+ // The one lost-work note rides the whole forward: optional only when EVERY request in it is.
215
+ ...(messages.every((message) => message.isAnswerOptional === true) ? { isAnswerOptional: true } : {}),
216
+ };
145
217
  };
146
218
  try {
147
219
  if (!(await this.deps.prepareWakeUpSession(key, buildMessage(woken)))) {
@@ -158,7 +230,10 @@ class RequestWakeUpEngine {
158
230
  this.deps.noteRestartWakeDelivered?.(key);
159
231
  console.log(`[requests] ${(0, sessionKey_1.keyToString)(key)}: woken after a restart for ${current.map((request) => request.id).join(', ')}`);
160
232
  for (const request of current) {
161
- await this.trackForwardedTurn(key, request.id, { isRequestPrompt: (0, requestHeader_1.getWakeUpMessage)(request, 'restarted').isRequestPrompt });
233
+ await this.trackForwardedTurn(key, request.id, {
234
+ isRequestPrompt: (0, requestHeader_1.getWakeUpMessage)(request, 'restarted').isRequestPrompt,
235
+ isConversationForward: current.length > 1,
236
+ });
162
237
  }
163
238
  }
164
239
  catch (e) {
@@ -170,6 +245,27 @@ class RequestWakeUpEngine {
170
245
  const entry = this.deps.ledger.getOpenRequestById(requestId);
171
246
  return entry && (0, sessionKey_1.keyToString)(entry.key) === (0, sessionKey_1.keyToString)(key) ? entry.request : undefined;
172
247
  }
248
+ /** The ids of the requests whose turns the engine watches in `key`'s conversation: the requests the agent is working on right now. */
249
+ getWatchedRequestIdsOf(key) {
250
+ return this.getWatchedTurnsOf(key).map((turn) => turn.requestId);
251
+ }
252
+ /**
253
+ * The watched requests of `key`'s conversation whose watch a forward of their OWN started (not a conversation-wide nudge), the
254
+ * oldest own forward first: the first is the request whose forward started the running turn.
255
+ */
256
+ getOwnForwardWatchedRequestIdsOf(key) {
257
+ return this.getWatchedTurnsOf(key)
258
+ .filter((turn) => turn.isOwnForward)
259
+ .sort((first, second) => (first.ownForwardStartedAt ?? 0) - (second.ownForwardStartedAt ?? 0))
260
+ .map((turn) => turn.requestId);
261
+ }
262
+ /**
263
+ * @description The request a job armed NOW in `key`'s conversation is armed under (`pickRequestToArmUnder` over the ledger's open
264
+ * requests and this engine's watches); `undefined` when no request is open.
265
+ */
266
+ getArmingRequestIdOf(key) {
267
+ return (0, scheduleWait_1.pickRequestToArmUnder)(this.deps.ledger.listOpenRequestsOf(key), this.getWatchedRequestIdsOf(key), this.getOwnForwardWatchedRequestIdsOf(key));
268
+ }
173
269
  /** The watched turns of `key`'s conversation. */
174
270
  getWatchedTurnsOf(key) {
175
271
  const conversationKey = (0, sessionKey_1.keyToString)(key);
@@ -178,9 +274,22 @@ class RequestWakeUpEngine {
178
274
  /**
179
275
  * @description A request's message (or a reminder for it) was just forwarded to
180
276
  * the conversation's session: watch the turn it starts. A watch already there
181
- * for the SAME request is replaced — its newest message is the one that counts;
182
- * another requester's request in the conversation keeps its own watch. A post
183
- * retry still pending is moot from now on (R28): the session has the request.
277
+ * for the SAME request is MERGED with it (the forward joined a turn that still runs: one that ended was settled before the write,
278
+ * `settleEndedTurnsOf`): the progress count keeps
279
+ * its earlier start, and the turn is the alarm's only if both were — except for the bot's own «continue»
280
+ * (`isContinuation`: the nudge after an API error or a limit wait), which keeps the alarm's kind but
281
+ * starts the progress count afresh: a note sent before the error is not this turn's — and for a person's
282
+ * words that join the running turn, which start the count afresh too: a note sent before them is not their answer. Another requester's
283
+ * request in the conversation keeps its own watch. Whether the turn is a scheduled run's (`isScheduledRun`) is said by the FORWARD
284
+ * that started it — a fire riding the request, or the fire that opened the scheduler's request — and never read from the request's
285
+ * state: an answered request's fire turn is a fire's as much as an unanswered one's (`src/requests/README.md` § Scheduled runs).
286
+ * A fire of ANOTHER job that only rides the request (`isRequestJob` false) is not the request's activity: its forward records none,
287
+ * and its watch keeps that (a merge is the request's own if either side is; a «continue» keeps what the turn was), so its busy time and
288
+ * its quiet end leave the request's follow-up and backstop alone (`src/requests/README.md` § Scheduled runs).
289
+ * A post retry still pending is moot from now on (R28): the session has the request — unless the forward is a fire riding the
290
+ * request, which carries none of its prompt. A forward that carries a person's words (a request prompt, an issue update) marks the
291
+ * request as owing an answer (`isPersonInputUnanswered`, persisted): that, not the kind of the turn, is
292
+ * what stops the turn's end from reading as a quiet one (`decideTurnEnd`).
184
293
  */
185
294
  async trackForwardedTurn(key, requestId, options = { isRequestPrompt: false }) {
186
295
  const request = this.getOpenRequestOf(key, requestId);
@@ -188,17 +297,73 @@ class RequestWakeUpEngine {
188
297
  return;
189
298
  // The agent works on an open request now: what it writes is owed to that one, not a tail.
190
299
  this.answerTails.delete((0, sessionKey_1.keyToString)(key));
300
+ const previous = this.watched.get(requestId);
301
+ const isFireForward = options.isScheduledRun === true;
302
+ // The scheduler's request (a Telegram topic's `scheduledRun`) is no person's: its prompt carries no person's words.
303
+ const isSchedulerRequest = request.origin.kind === 'scheduledRun';
304
+ const carriesPersonWords = options.isRequestPrompt || options.isPersonInput === true;
305
+ // A person's words that reach the agent — not a fire's prompt, not the scheduler's request's.
306
+ const isPersonWordsForward = carriesPersonWords && !isFireForward && !isSchedulerRequest;
307
+ const isOwnForward = options.isConversationForward !== true || previous?.isOwnForward === true;
308
+ const ownForwardStartedAt = previous?.ownForwardStartedAt ?? (isOwnForward ? this.now() : undefined);
309
+ // A fire's turn stays one through a fire that joins it and through the bot's «continue»; a person's words or a reminder that joined
310
+ // it make it theirs.
311
+ const isScheduledRun = previous === undefined
312
+ ? isFireForward
313
+ : previous.isScheduledRun === true && (isFireForward || (options.isContinuation === true && !isPersonWordsForward));
314
+ // Whose turn it is: another job's fire says «not the request's own», a «continue» says nothing (it continues the turn it joins), any
315
+ // other forward — a person's words, a reminder, the request's own job's fire — is the request's own.
316
+ const isOtherJobFireForward = isFireForward && options.isRequestJob === false;
317
+ const isForwardOwn = options.isContinuation === true && !isPersonWordsForward ? undefined : !isOtherJobFireForward;
318
+ const isRequestJob = previous === undefined ? isForwardOwn !== false : previous.isRequestJob !== false || isForwardOwn === true;
319
+ // Where the request's prompt now is: a forward that carries it was written to the live process (none named while a window holds it
320
+ // in the buffer, for its replay to name); one that does not carry it leaves the prompt where it was. A process that went down since
321
+ // the write — no live one, and no window — is the one the write stamped.
322
+ const probe = this.deps.probeTurn(key);
323
+ const writtenNow = probe.processGeneration ?? (probe.isWindowOpen === true ? undefined : this.writtenProcesses.get((0, sessionKey_1.keyToString)(key)));
324
+ const writtenToProcess = previous === undefined || options.isRequestPrompt ? writtenNow : previous.writtenToProcess;
191
325
  this.watched.set(requestId, {
192
326
  key,
193
327
  requestId,
194
- progressCountAtTurnStart: request.progressAnswerCount,
328
+ // The progress count starts afresh at a «continue» and at words that join a turn already running: a note sent before them is
329
+ // not their answer (it would end the turn as a «progress» one — quiet under a watch, and the words left to the 90-minute backstop).
330
+ progressCountAtTurnStart: options.isContinuation === true || (previous !== undefined && isPersonWordsForward)
331
+ ? request.progressAnswerCount
332
+ : (previous?.progressCountAtTurnStart ?? request.progressAnswerCount),
195
333
  hasSeenBusy: false,
196
334
  hasSeenOutput: false,
197
- isRequestPrompt: options.isRequestPrompt,
335
+ isRequestPrompt: options.isRequestPrompt || previous?.isRequestPrompt === true,
336
+ isScheduledRun,
337
+ isRequestJob,
338
+ isOwnForward,
339
+ ...(ownForwardStartedAt === undefined ? {} : { ownForwardStartedAt }),
340
+ ...(writtenToProcess === undefined ? {} : { writtenToProcess }),
198
341
  });
199
- if (request.nextPostRetryAt !== undefined)
200
- await this.deps.ledger.updateOpenRequest(request.id, { nextPostRetryAt: undefined });
201
- await this.recordTurnActivity(request, true);
342
+ // A fire RIDING the request carries none of its prompt; the fire that opened the scheduler's request carries its own.
343
+ const isMoot = request.nextPostRetryAt !== undefined && (!isFireForward || options.isRequestPrompt);
344
+ const isPersonWordsOwed = isPersonWordsForward && request.isPersonInputUnanswered !== true;
345
+ if (isMoot || isPersonWordsOwed) {
346
+ await this.deps.ledger.updateOpenRequest(request.id, {
347
+ ...(isMoot ? { nextPostRetryAt: undefined } : {}),
348
+ ...(isPersonWordsOwed ? { isPersonInputUnanswered: true } : {}),
349
+ });
350
+ }
351
+ // Keyed on the watch just stored, so a fire of another job AND a «continue» of its turn alike write nothing on the request.
352
+ if (isRequestJob)
353
+ await this.recordTurnActivity(request, true);
354
+ }
355
+ /**
356
+ * @description A person's words for the request were HELD — an armed usage-limit wait keeps them back, and they reach the agent
357
+ * only inside whatever prompt the wait's end forwards first (a «continue», a scheduled run, the operator's next message). No
358
+ * turn starts for them now, so nothing is tracked, but the request owes an answer from this moment: persisted, so the turn
359
+ * that finally carries the words — under any watch, or none after a restart during the wait — is read against it
360
+ * (`checkIsAnswerOwed`). A request that owes it already, and a scheduler fire's own request, are left as they are.
361
+ */
362
+ async notePersonInputHeld(key, requestId) {
363
+ const request = this.getOpenRequestOf(key, requestId);
364
+ if (!request || request.isPersonInputUnanswered === true || request.origin.kind === 'scheduledRun')
365
+ return;
366
+ await this.deps.ledger.updateOpenRequest(request.id, { isPersonInputUnanswered: true });
202
367
  }
203
368
  /**
204
369
  * @description The request's prompt could not be posted to the session (no
@@ -259,28 +424,73 @@ class RequestWakeUpEngine {
259
424
  this.watched.delete(turn.requestId);
260
425
  this.answerTails.delete((0, sessionKey_1.keyToString)(key));
261
426
  }
427
+ /**
428
+ * @description The requests of `key` whose prompt is NOT lost if the conversation ends now, read off its live process at this moment:
429
+ * a prompt the process took in — R21's own test ({@link checkIsPromptTakenInNow}) on a probe taken now, once what the process wrote so
430
+ * far is read (`refreshProcessState`), which the persisted mark lags behind until a settle or the 10-second poll (review 26 #1) — and a
431
+ * prompt still waiting in a start's or a stop's window, whose window's drop tells its person. Synchronous: a command reads it BEFORE it
432
+ * stops or replaces the process (the default of {@link cancelConversation}; the `/resume` pick ahead of its resume).
433
+ */
434
+ readPromptsReachedOrHeld(key) {
435
+ // What the process already wrote counts: a switch moments after a message, before the poll read its echo, told its person the
436
+ // agent never read it while the bridge carried it.
437
+ this.deps.refreshProcessState?.(key);
438
+ const probe = this.deps.probeTurn(key);
439
+ const requestIds = new Set();
440
+ for (const turn of this.getWatchedTurnsOf(key)) {
441
+ if (checkIsPromptTakenInNow(turn, probe) || checkIsPromptWaitingInWindow(turn, probe))
442
+ requestIds.add(turn.requestId);
443
+ }
444
+ return requestIds;
445
+ }
262
446
  /**
263
447
  * @description The person took over (interrupted, quit, restarted, switched or
264
448
  * resumed the session, left the folder): every open request of the
265
- * conversation closes silently and nothing wakes them.
449
+ * conversation closes and nothing wakes them. Resolves the ids of the requests
450
+ * this call closed that carry a person's words the agent never took in (not the
451
+ * scheduler's request, whose prompt is no person's): a command that ends or replaces the
452
+ * conversation tells those people, since nobody reads their words (review 25 #3, review 26 #2);
453
+ * an interrupt closes silently. «Never taken in» reads the persisted mark and `reachedOrHeld`
454
+ * ({@link readPromptsReachedOrHeld}), read by default when this is called — synchronously, before
455
+ * the first await, so off the process the command has not stopped yet.
266
456
  */
267
- async cancelConversation(key) {
457
+ async cancelConversation(key, reachedOrHeld = this.readPromptsReachedOrHeld(key)) {
268
458
  this.unwatchConversation(key);
269
- for (const request of this.deps.ledger.listOpenRequestsOf(key))
270
- await this.deps.ledger.closeRequest(request.id, 'cancelled');
459
+ // The conversation's writes end with it: its next watch is named from a write of the next conversation (review 23 nit 6).
460
+ this.writtenProcesses.delete((0, sessionKey_1.keyToString)(key));
461
+ const unreadRequestIds = [];
462
+ for (const request of this.deps.ledger.listOpenRequestsOf(key)) {
463
+ const isUnreadPersonWords = request.origin.kind !== 'scheduledRun' && (0, requestHeader_1.getPromptNotTakenIn)(request) !== undefined && !reachedOrHeld.has(request.id);
464
+ // Named only when THIS close closed it: one another path closed meanwhile is that path's to tell.
465
+ const closed = await this.deps.ledger.closeRequest(request.id, 'cancelled');
466
+ if (closed !== null && isUnreadPersonWords)
467
+ unreadRequestIds.push(request.id);
468
+ }
469
+ return unreadRequestIds;
271
470
  }
272
471
  /**
273
472
  * @description The bot itself forwarded a "continue" nudge into the session (an
274
- * API-error retry fired, or a usage-limit wait ended): watch the turn it starts
275
- * as a fresh one, so the idle the error left behind is never read as its end.
473
+ * API-error retry fired, or a usage-limit wait ended): watch the turn it starts, so the idle the
474
+ * error left behind is never read as its end. The nudge carries no person's words and is no alarm,
475
+ * so it KEEPS the alarm's kind of the turn it continues but its progress count starts afresh: a progress
476
+ * note sent before the error does not make a silent continuation a «progress» turn. What the turn owes a
477
+ * person is not its kind but the persisted request (`checkIsAnswerOwed`): words answered before the error
478
+ * are owed no more, words still unanswered stay owed.
276
479
  * `isCountersReset` (the end of a limit wait) starts every open request's
277
480
  * wake-up bookkeeping from zero — the wait was not the agent's silence — and
278
481
  * lifts a limit stop: the work continues, so does the waking. A request the
279
482
  * rules already gave up on stays given up. The one session turn the nudge
280
483
  * starts is watched for every open request of the conversation.
484
+ * `carriedRequestIds` — the requests whose prompts the message carries, never taken in (R21: a limit or account resume re-posts
485
+ * them) — is a fact about EACH request, named when the message was BUILT (`getPromptsNotTakenIn`): only such a request has its words
486
+ * in the message (they are owed an answer, as any request prompt's), every other open request gets the plain «continue». It is
487
+ * never read again here: a buffered message is tracked at its replay, and a request opened in between did not ride it (review 21
488
+ * nit 3). A carried request whose prompt the session took in meanwhile is a plain «continue» too.
281
489
  */
282
490
  async trackContinuationTurn(key, options) {
283
491
  for (const request of this.deps.ledger.listOpenRequestsOf(key)) {
492
+ // Read before the counters update below: it never touches `isPromptTakenIn`, but the request's words are what is asked here.
493
+ const isOwnPromptCarried = options.carriedRequestIds?.includes(request.id) === true && (0, requestHeader_1.getPromptNotTakenIn)(request) !== undefined;
284
494
  if (options.isCountersReset) {
285
495
  await this.deps.ledger.updateOpenRequest(request.id, {
286
496
  silentTurnCount: 0,
@@ -290,7 +500,7 @@ class RequestWakeUpEngine {
290
500
  limitWaitAnsweredFor: undefined,
291
501
  });
292
502
  }
293
- await this.trackForwardedTurn(key, request.id, { isRequestPrompt: options.isRequestPrompt === true });
503
+ await this.trackForwardedTurn(key, request.id, { isRequestPrompt: isOwnPromptCarried, isContinuation: true, isConversationForward: true });
294
504
  }
295
505
  }
296
506
  /**
@@ -328,6 +538,168 @@ class RequestWakeUpEngine {
328
538
  this.isPolling = false;
329
539
  }
330
540
  }
541
+ /**
542
+ * @description A new prompt is about to be written into `key`'s conversation: settle every watch of it whose turn has already
543
+ * ENDED, on a probe taken now — before the write — exactly as the poll's next tick would have. A prompt that waited behind a running
544
+ * turn is forwarded within seconds of its end (`postToSession` polls busy every 5 s, this engine every 10 s), usually before the tick
545
+ * saw the idle; the old watch would then read the NEW turn as its own: arm its jobs under the old request, count its busy time as
546
+ * the old request's activity, and decide the old request only at the new turn's end. A watch whose turn still runs is left — the
547
+ * forward joins it (`trackForwardedTurn` merges), its prompt marked taken in if the live process took it (R21, as the poll marks); a
548
+ * session that is gone is left to the poll.
549
+ * Synchronous up to the decisions: the ended watches are dropped and decided against the request as it is NOW, so a forward that
550
+ * does not await (a write must run in its caller's tick) is never read into them. The decisions are persisted after, in order; the
551
+ * returned promise resolves once they are (a post that picks a request awaits it first, so the pick and the header see the result)
552
+ * with the delivery of the wake-ups they decided. The CALLER delivers them — after its own write, so the prompt it writes starts the
553
+ * turn and a reminder joins it, as the tick would have reminded seconds later; a call with no write of its own (the adapter's turn end)
554
+ * hands them to a post on its way to its write, else delivers them at once ({@link deliverOrHandOverWake}). Never rejects, and neither
555
+ * does the delivery.
556
+ */
557
+ settleEndedTurnsOf(key) {
558
+ const probe = this.deps.probeTurn(key);
559
+ const persisting = [];
560
+ for (const turn of this.getWatchedTurnsOf(key)) {
561
+ const request = this.getOpenRequestOf(key, turn.requestId);
562
+ if (!request) {
563
+ this.watched.delete(turn.requestId);
564
+ continue;
565
+ }
566
+ if (probe.isBusy)
567
+ turn.hasSeenBusy = true;
568
+ if ((0, wakeUpRules_1.getWatchedTurnState)(turn, probe) !== 'ended') {
569
+ // A turn still running, or held, may have taken its prompt in already: marked now (R21), as the poll would. The process that took
570
+ // it may be gone before the poll's next tick — a deferred account switch re-spawns it one timer tick after the turn's end, the
571
+ // per-turn stop ends it — and a prompt is read as taken in only off the process it was written to.
572
+ persisting.push(this.markPromptTakenIn(turn, request, probe).then(() => null, (e) => {
573
+ logWakeUpFailure(`marking the prompt of ${turn.requestId} taken in`, e);
574
+ return null;
575
+ }));
576
+ continue;
577
+ }
578
+ this.watched.delete(turn.requestId);
579
+ const decision = this.decideWatchedTurnEnd(turn, request);
580
+ persisting.push(this.persistEndedTurn(turn, request, probe, decision).catch((e) => {
581
+ logWakeUpFailure(`settling the turn of ${turn.requestId}`, e);
582
+ return null;
583
+ }));
584
+ }
585
+ return Promise.all(persisting).then((deliveries) => async () => {
586
+ for (const deliver of deliveries)
587
+ await deliver?.().catch((e) => logWakeUpFailure(`delivering a settled turn's wake-up in ${(0, sessionKey_1.keyToString)(key)}`, e));
588
+ });
589
+ }
590
+ /**
591
+ * @description A post (`postToSession`) is on its way to write a prompt into `key`'s conversation, waiting for the running turn's
592
+ * idle maybe. Until the returned release, a wake-up decided for the conversation — at the adapter's turn end, by the poll, by the sweep
593
+ * ({@link deliverOrHandOverWake}) — is HANDED to the post instead of written at once: the post takes it with its settle
594
+ * ({@link settleEndedTurnsForPost}) and delivers it after its own write, so the prompt that waited for the turn starts the next one and
595
+ * the reminder joins it (written first, the reminder would start that turn and the reminded request would read as its starter). The
596
+ * release delivers what is still handed over once the last post in flight is done: one that never took it (held over a limit wait,
597
+ * an ensure that failed) or a wake decided after its settle. It never rejects, and a second call does nothing.
598
+ */
599
+ notePostInFlight(key) {
600
+ const conversationKey = (0, sessionKey_1.keyToString)(key);
601
+ this.postsInFlight.set(conversationKey, (this.postsInFlight.get(conversationKey) ?? 0) + 1);
602
+ let isReleased = false;
603
+ return async () => {
604
+ if (isReleased)
605
+ return;
606
+ isReleased = true;
607
+ const count = (this.postsInFlight.get(conversationKey) ?? 1) - 1;
608
+ if (count > 0) {
609
+ this.postsInFlight.set(conversationKey, count);
610
+ return;
611
+ }
612
+ this.postsInFlight.delete(conversationKey);
613
+ await this.takeWakesHandedToPost(conversationKey)();
614
+ };
615
+ }
616
+ /** @description Deliver a decided wake-up now, or hand it to a post in flight in `key`'s conversation ({@link notePostInFlight}). Never rejects. */
617
+ async deliverOrHandOverWake(key, deliver) {
618
+ const conversationKey = (0, sessionKey_1.keyToString)(key);
619
+ if (this.postsInFlight.has(conversationKey)) {
620
+ this.wakesHandedToPost.set(conversationKey, [...(this.wakesHandedToPost.get(conversationKey) ?? []), deliver]);
621
+ return;
622
+ }
623
+ await deliver().catch((e) => logWakeUpFailure(`delivering a wake-up in ${conversationKey}`, e));
624
+ }
625
+ /**
626
+ * @description A write into `key`'s live process (the write choke point, synchronously before the adapter write): the process it goes
627
+ * to is stamped, for a watch that is tracked only after that process went down ({@link writtenProcesses}). Synchronous; a probe that
628
+ * names no process (a window holds the prompt) stamps nothing.
629
+ */
630
+ noteWrite(key) {
631
+ const { processGeneration } = this.deps.probeTurn(key);
632
+ if (processGeneration !== undefined)
633
+ this.writtenProcesses.set((0, sessionKey_1.keyToString)(key), processGeneration);
634
+ }
635
+ /**
636
+ * @description A write into `key`'s live process during the replay of a start's buffer (the write choke point, right after its settle;
637
+ * the bot calls it at every write while `StartupPromptBuffer.checkIsReplaying`: a replayed prompt, the held prompts after them, a prompt
638
+ * written at once while the replay runs — all go to the process the replay writes into). A watch tracked while its prompt waited in that
639
+ * buffer names no process yet, and the replay took the whole buffer to write it into this process, so its prompt is in this one. Never
640
+ * called for a write outside a replay: a watch whose buffer was DROPPED stays unnamed — its prompt reached no process
641
+ * (`checkIsPromptNeverWritten`) — and the request it opened is closed with the person told (`bot.ts`, the person's buffered prompt's
642
+ * settle). Only a person's prompt is tracked before its replay: every bot-made caller watches its turn once its prompt reached a session
643
+ * (`src/requests/README.md`, the callers table). Synchronous; a session with no process named does nothing.
644
+ */
645
+ noteReplayedWrite(key) {
646
+ const { processGeneration } = this.deps.probeTurn(key);
647
+ if (processGeneration === undefined)
648
+ return;
649
+ for (const turn of this.getWatchedTurnsOf(key)) {
650
+ if (turn.writtenToProcess === undefined)
651
+ turn.writtenToProcess = processGeneration;
652
+ }
653
+ }
654
+ /**
655
+ * @description The settle of a post about to write ({@link settleEndedTurnsOf}): resolves ONE delivery for the post to run after its
656
+ * write — the wake-ups handed to it so far, in order, then this settle's own. Never rejects, and neither does the delivery.
657
+ */
658
+ settleEndedTurnsForPost(key) {
659
+ const deliverHandedOver = this.takeWakesHandedToPost((0, sessionKey_1.keyToString)(key));
660
+ return this.settleEndedTurnsOf(key).then((deliverSettled) => async () => {
661
+ await deliverHandedOver();
662
+ await deliverSettled();
663
+ });
664
+ }
665
+ /** The delivery of the wake-ups handed to a post in `conversationKey`, taken (a later post or release finds none of them). */
666
+ takeWakesHandedToPost(conversationKey) {
667
+ const handedOver = this.wakesHandedToPost.get(conversationKey) ?? [];
668
+ this.wakesHandedToPost.delete(conversationKey);
669
+ return async () => {
670
+ for (const deliver of handedOver)
671
+ await deliver().catch((e) => logWakeUpFailure(`delivering a wake-up handed to a post in ${conversationKey}`, e));
672
+ };
673
+ }
674
+ /** The decision at the end of `turn`, read against `request` and its jobs as they are now. */
675
+ decideWatchedTurnEnd(turn, request) {
676
+ return (0, wakeUpRules_1.decideTurnEnd)(request, turn.progressCountAtTurnStart, this.now(), {
677
+ isScheduledRun: turn.isScheduledRun === true,
678
+ isRequestJob: turn.isRequestJob !== false,
679
+ scheduleWait: this.getScheduleWait(turn.key, request),
680
+ progressFollowUpMs: this.deps.progressFollowUpMs,
681
+ });
682
+ }
683
+ /** R21 for a request prompt the ended turn took in (a later wake-up is a reminder), then `decision` persisted; resolves its wake's delivery. */
684
+ async persistEndedTurn(turn, request, probe, decision) {
685
+ await this.markPromptTakenIn(turn, request, probe);
686
+ return this.persistDecision(turn.key, request, decision);
687
+ }
688
+ /**
689
+ * R21: once the backend took a request prompt in, a wake-up is a reminder — the agent has read the request. A held turn end is read like
690
+ * any other: what the backend still lists as unread is unread. A prompt queued behind a turn that ended in an error result stays listed
691
+ * until its own echo (the json-stream adapter keeps it; the CLI runs it as its next turn, measured), and the prompt of the turn that
692
+ * failed was read by it — a resume that posted it again would repeat words the agent already has.
693
+ * Read only off the process the prompt was written to (`writtenToProcess`): a re-spawn that replaced it — an account move after a limit,
694
+ * a model change — kills a message still queued there, and the new process lists nothing unread. Left unmarked, the prompt is posted
695
+ * again: by the resume's «continue» (`getPromptNotTakenIn`), or as the wake-up of the turn's end. A prompt that names no process while
696
+ * one is live never reached any (`checkIsTurnInputConsumed`): nothing is marked.
697
+ */
698
+ async markPromptTakenIn(turn, request, probe) {
699
+ if (request.isPromptTakenIn !== true && checkIsPromptConsumedByLiveProcess(turn, probe)) {
700
+ await this.deps.ledger.updateOpenRequest(request.id, { isPromptTakenIn: true });
701
+ }
702
+ }
331
703
  async pollWatchedTurn(turn) {
332
704
  // Replaced or dropped since this poll took its snapshot: the newer watch (if any) decides.
333
705
  if (this.watched.get(turn.requestId) !== turn)
@@ -341,24 +713,29 @@ class RequestWakeUpEngine {
341
713
  const probe = this.deps.probeTurn(turn.key);
342
714
  if (probe.isBusy)
343
715
  turn.hasSeenBusy = true;
344
- if (turn.isRequestPrompt && request.isPromptTakenIn !== true && probe.isActive && (0, wakeUpRules_1.checkIsTurnInputConsumed)(turn, probe)) {
345
- // R21: from now on a wake-up is a reminder — the agent has read the request.
346
- await this.deps.ledger.updateOpenRequest(request.id, { isPromptTakenIn: true });
347
- }
716
+ await this.markPromptTakenIn(turn, request, probe);
717
+ // Settled by a forward, or replaced by a new watch, during that write: the watch present now decides, not this one.
718
+ if (this.watched.get(turn.requestId) !== turn)
719
+ return;
348
720
  const turnState = (0, wakeUpRules_1.getWatchedTurnState)(turn, probe);
349
721
  if (turnState === 'running') {
350
- if (probe.isBusy)
351
- await this.recordTurnActivity(request, false);
352
- // Its end can no longer be seen: the sweep's backstop takes the request over.
353
- else if ((0, wakeUpRules_1.checkIsWatchedTurnStale)(request, probe, this.now(), this.deps.backstopMs))
722
+ if (probe.isBusy) {
723
+ // Another job's fire that only rides the request keeps the session busy for its own work, not for this request's — and so does
724
+ // any turn while this request's prompt has reached no process (`checkIsPromptNeverWritten`): that busy time is another turn's.
725
+ if (turn.isRequestJob !== false && !(0, wakeUpRules_1.checkIsPromptNeverWritten)(turn, probe))
726
+ await this.recordTurnActivity(request, false);
727
+ }
728
+ else if ((0, wakeUpRules_1.checkIsWatchedTurnStale)(request, probe, this.now(), this.deps.backstopMs)) {
729
+ // Its end can no longer be seen: the sweep's backstop takes the request over.
354
730
  this.watched.delete(turn.requestId);
731
+ }
355
732
  return;
356
733
  }
357
734
  this.watched.delete(turn.requestId);
358
735
  // A dead session is not a turn end: the backstop resumes it later.
359
736
  if (turnState === 'sessionGone')
360
737
  return;
361
- await this.applyDecision(turn.key, request, (0, wakeUpRules_1.decideTurnEnd)(request, turn.progressCountAtTurnStart, this.now()));
738
+ await this.applyDecision(turn.key, request, this.decideWatchedTurnEnd(turn, request));
362
739
  }
363
740
  /** @description One sweep over the open requests nobody watches; exported for tests. */
364
741
  async sweepUnwatchedRequests() {
@@ -389,10 +766,22 @@ class RequestWakeUpEngine {
389
766
  async sweepOpenRequest(key, request) {
390
767
  const probe = this.deps.probeTurn(key);
391
768
  if (probe.isBusy) {
392
- await this.recordTurnActivity(request, false);
769
+ // A busy session is activity of THIS request's own lost turn — so a long-running one is not prodded mid-work — but only while no
770
+ // turn is watched in the conversation: then the busy time is that turn's, never this request's, whatever it owes. A recurring
771
+ // job's fires keep the session busy every few minutes, so recorded they would push this request's backstop ahead for good — a
772
+ // requester's lost message, or a watch that fired and nobody handled (`armed`), would never be taken back. A request a watch
773
+ // holds ignores the backstop anyway, and a due follow-up is woken by `nextWakeAt`, which activity does not move. The same for a
774
+ // turn the CLI started by itself (`isCliOwnTurn`: a finished task's, a session `CronCreate` job's): only a turn the bot wrote can
775
+ // be the request's lost one, and a looping job would otherwise push an owed request's backstop ahead for as long as it loops.
776
+ if (this.getWatchedTurnsOf(key).length === 0 && probe.isCliOwnTurn !== true)
777
+ await this.recordTurnActivity(request, false);
393
778
  return;
394
779
  }
395
- await this.applyDecision(key, request, (0, wakeUpRules_1.decideUnwatchedRequest)(request, probe, this.now(), this.deps.backstopMs));
780
+ await this.applyDecision(key, request, (0, wakeUpRules_1.decideUnwatchedRequest)(request, probe, this.now(), this.deps.backstopMs, this.getScheduleWait(key, request)));
781
+ }
782
+ /** The request's reading of its armed jobs now; `none` when the port is not wired. */
783
+ getScheduleWait(key, request) {
784
+ return this.deps.getScheduleWait?.(key, request) ?? 'none';
396
785
  }
397
786
  async sweepAnswerTail(conversationKey, entry) {
398
787
  if (this.answerTails.get(conversationKey) !== entry)
@@ -424,8 +813,23 @@ class RequestWakeUpEngine {
424
813
  await this.deps.ledger.updateOpenRequest(request.id, { lastTurnActivityAt: nowMs });
425
814
  }
426
815
  async applyDecision(key, request, decision) {
816
+ const deliverWake = await this.persistDecision(key, request, decision);
817
+ // A post on its way to its write takes it and writes it after its own prompt (`notePostInFlight`).
818
+ if (deliverWake)
819
+ await this.deliverOrHandOverWake(key, deliverWake);
820
+ }
821
+ /**
822
+ * Persist `decision` (an alert is raised here too) and resolve the delivery of its wake-up, or `null` when it decided none or the
823
+ * request changed meanwhile. Split from the delivery so a settle before a write can persist at once and deliver after the write.
824
+ */
825
+ async persistDecision(key, request, decision) {
427
826
  if (decision.kind === 'none')
428
- return;
827
+ return null;
828
+ if (decision.kind === 'quiet' && decision.isRequestSettled === true) {
829
+ // The alarm's own request, left unanswered by an agent with nothing to report: closed without a word, never woken at the backstop.
830
+ await this.deps.ledger.closeRequest(request.id, 'cancelled');
831
+ return null;
832
+ }
429
833
  let isStoppedMeanwhile = false;
430
834
  const updated = await this.deps.ledger.updateOpenRequest(request.id, (current) => {
431
835
  // `request` is a snapshot: a limit wait may have stopped the wake-ups since (no alert either).
@@ -434,14 +838,19 @@ class RequestWakeUpEngine {
434
838
  });
435
839
  // Closed or superseded while we decided: the decision belonged to the old one.
436
840
  if (!updated || isStoppedMeanwhile)
437
- return;
438
- if (decision.kind === 'followUpLater')
439
- return;
841
+ return null;
842
+ if (decision.kind === 'followUpLater' || decision.kind === 'quiet')
843
+ return null;
440
844
  if (decision.kind === 'alert') {
441
845
  await this.raiseAlert(key, updated, decision.reason);
442
- return;
846
+ return null;
443
847
  }
444
- const message = (0, requestHeader_1.getWakeUpMessage)(updated, decision.reason);
848
+ const { reason } = decision;
849
+ return () => this.deliverWake(key, updated, reason);
850
+ }
851
+ /** Deliver a persisted wake-up: the reminder (or the request's prompt again, R21), then its turn watched, or the failure handled. */
852
+ async deliverWake(key, updated, reason) {
853
+ const message = (0, requestHeader_1.getWakeUpMessage)(updated, reason);
445
854
  const outcome = await this.deliverWakeUpSafely(key, updated, message);
446
855
  if (outcome === 'delivered') {
447
856
  await this.trackForwardedTurn(key, updated.id, { isRequestPrompt: message.isRequestPrompt });
@@ -449,7 +858,7 @@ class RequestWakeUpEngine {
449
858
  }
450
859
  if (outcome === 'requestGone')
451
860
  return;
452
- if (decision.reason === 'postRetry') {
861
+ if (reason === 'postRetry') {
453
862
  // R28: a retry that failed again tries later; once spent, the backstop decides — no alert yet.
454
863
  const retry = (0, wakeUpRules_1.getPostRetryUpdate)(updated, this.now(), this.deps.firstPostRetryDelayMs);
455
864
  if (retry)