@skillcdn/console 0.6.0 → 0.6.2

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 (415) hide show
  1. package/README.md +7 -7
  2. package/dist/agent-page.d.ts.map +1 -1
  3. package/dist/agent-page.js +58 -35
  4. package/dist/agent-page.js.map +1 -1
  5. package/dist/agent-usage.d.ts +51 -0
  6. package/dist/agent-usage.d.ts.map +1 -0
  7. package/dist/agent-usage.js +77 -0
  8. package/dist/agent-usage.js.map +1 -0
  9. package/dist/api.d.ts +4 -2
  10. package/dist/api.d.ts.map +1 -1
  11. package/dist/api.js +4 -2
  12. package/dist/api.js.map +1 -1
  13. package/dist/cli/cli.d.ts +9 -2
  14. package/dist/cli/cli.d.ts.map +1 -1
  15. package/dist/cli/cli.js +491 -125
  16. package/dist/cli/cli.js.map +1 -1
  17. package/dist/cli/format.d.ts +46 -4
  18. package/dist/cli/format.d.ts.map +1 -1
  19. package/dist/cli/format.js +163 -45
  20. package/dist/cli/format.js.map +1 -1
  21. package/dist/cli/help.d.ts.map +1 -1
  22. package/dist/cli/help.js +62 -13
  23. package/dist/cli/help.js.map +1 -1
  24. package/dist/cli/machine-check.d.ts +24 -5
  25. package/dist/cli/machine-check.d.ts.map +1 -1
  26. package/dist/cli/machine-check.js +128 -16
  27. package/dist/cli/machine-check.js.map +1 -1
  28. package/dist/cli/pack.d.ts +11 -8
  29. package/dist/cli/pack.d.ts.map +1 -1
  30. package/dist/cli/pack.js +63 -28
  31. package/dist/cli/pack.js.map +1 -1
  32. package/dist/cli/runner-host.d.ts +2 -0
  33. package/dist/cli/runner-host.d.ts.map +1 -1
  34. package/dist/cli/runner-host.js +1 -0
  35. package/dist/cli/runner-host.js.map +1 -1
  36. package/dist/cli/secret-notes.d.ts.map +1 -1
  37. package/dist/cli/secret-notes.js +9 -1
  38. package/dist/cli/secret-notes.js.map +1 -1
  39. package/dist/cli/terminal.d.ts +10 -2
  40. package/dist/cli/terminal.d.ts.map +1 -1
  41. package/dist/cli/terminal.js +16 -4
  42. package/dist/cli/terminal.js.map +1 -1
  43. package/dist/client.d.ts +30 -7
  44. package/dist/client.d.ts.map +1 -1
  45. package/dist/client.js +13 -1
  46. package/dist/client.js.map +1 -1
  47. package/dist/components/agent-page.d.ts +76 -18
  48. package/dist/components/agent-page.d.ts.map +1 -1
  49. package/dist/components/agent-page.js +183 -53
  50. package/dist/components/agent-page.js.map +1 -1
  51. package/dist/components/agents.d.ts +2 -0
  52. package/dist/components/agents.d.ts.map +1 -1
  53. package/dist/components/agents.js +6 -5
  54. package/dist/components/agents.js.map +1 -1
  55. package/dist/components/calendar.d.ts +24 -4
  56. package/dist/components/calendar.d.ts.map +1 -1
  57. package/dist/components/calendar.js +79 -36
  58. package/dist/components/calendar.js.map +1 -1
  59. package/dist/components/categories.js +2 -2
  60. package/dist/components/categories.js.map +1 -1
  61. package/dist/components/comments.js +2 -2
  62. package/dist/components/comments.js.map +1 -1
  63. package/dist/components/decision-list.d.ts.map +1 -1
  64. package/dist/components/decision-list.js +5 -5
  65. package/dist/components/decision-list.js.map +1 -1
  66. package/dist/components/documents.d.ts.map +1 -1
  67. package/dist/components/documents.js +4 -4
  68. package/dist/components/documents.js.map +1 -1
  69. package/dist/components/event-feed.d.ts +2 -3
  70. package/dist/components/event-feed.d.ts.map +1 -1
  71. package/dist/components/event-feed.js +4 -184
  72. package/dist/components/event-feed.js.map +1 -1
  73. package/dist/components/goals.d.ts.map +1 -1
  74. package/dist/components/goals.js +3 -3
  75. package/dist/components/goals.js.map +1 -1
  76. package/dist/components/home.js +9 -9
  77. package/dist/components/home.js.map +1 -1
  78. package/dist/components/icon.d.ts.map +1 -1
  79. package/dist/components/icon.js +11 -2
  80. package/dist/components/icon.js.map +1 -1
  81. package/dist/components/notifications.d.ts +65 -0
  82. package/dist/components/notifications.d.ts.map +1 -0
  83. package/dist/components/notifications.js +162 -0
  84. package/dist/components/notifications.js.map +1 -0
  85. package/dist/components/pack-connect.js +1 -1
  86. package/dist/components/pack-connect.js.map +1 -1
  87. package/dist/components/pack-token.js +1 -1
  88. package/dist/components/pack-token.js.map +1 -1
  89. package/dist/components/people.js +2 -2
  90. package/dist/components/people.js.map +1 -1
  91. package/dist/components/pictures.js +1 -1
  92. package/dist/components/pictures.js.map +1 -1
  93. package/dist/components/plugin-settings.d.ts +22 -1
  94. package/dist/components/plugin-settings.d.ts.map +1 -1
  95. package/dist/components/plugin-settings.js +63 -11
  96. package/dist/components/plugin-settings.js.map +1 -1
  97. package/dist/components/projects.js +1 -1
  98. package/dist/components/projects.js.map +1 -1
  99. package/dist/components/roadmap.js +2 -2
  100. package/dist/components/roadmap.js.map +1 -1
  101. package/dist/components/rules.js +2 -2
  102. package/dist/components/rules.js.map +1 -1
  103. package/dist/components/runs.d.ts.map +1 -1
  104. package/dist/components/runs.js +1 -1
  105. package/dist/components/runs.js.map +1 -1
  106. package/dist/components/sessions.js +1 -1
  107. package/dist/components/sessions.js.map +1 -1
  108. package/dist/components/shell.d.ts +2 -0
  109. package/dist/components/shell.d.ts.map +1 -1
  110. package/dist/components/shell.js +1 -1
  111. package/dist/components/shell.js.map +1 -1
  112. package/dist/components/sign-in.js +2 -2
  113. package/dist/components/sign-in.js.map +1 -1
  114. package/dist/components/skills.d.ts.map +1 -1
  115. package/dist/components/skills.js +1 -1
  116. package/dist/components/skills.js.map +1 -1
  117. package/dist/components/task-view.d.ts +6 -0
  118. package/dist/components/task-view.d.ts.map +1 -1
  119. package/dist/components/task-view.js +3 -2
  120. package/dist/components/task-view.js.map +1 -1
  121. package/dist/components/tokens.js +1 -1
  122. package/dist/components/tokens.js.map +1 -1
  123. package/dist/components/ui.d.ts +21 -2
  124. package/dist/components/ui.d.ts.map +1 -1
  125. package/dist/components/ui.js +24 -3
  126. package/dist/components/ui.js.map +1 -1
  127. package/dist/components/work.js +1 -1
  128. package/dist/components/work.js.map +1 -1
  129. package/dist/console.css +506 -289
  130. package/dist/console.d.ts.map +1 -1
  131. package/dist/console.js +193 -16
  132. package/dist/console.js.map +1 -1
  133. package/dist/data.d.ts +17 -1
  134. package/dist/data.d.ts.map +1 -1
  135. package/dist/data.js +31 -0
  136. package/dist/data.js.map +1 -1
  137. package/dist/desk-page.js +5 -5
  138. package/dist/desk-page.js.map +1 -1
  139. package/dist/feed-words.d.ts +5 -0
  140. package/dist/feed-words.d.ts.map +1 -0
  141. package/dist/feed-words.js +193 -0
  142. package/dist/feed-words.js.map +1 -0
  143. package/dist/i18n/en.d.ts +93 -1
  144. package/dist/i18n/en.d.ts.map +1 -1
  145. package/dist/i18n/en.js +103 -15
  146. package/dist/i18n/en.js.map +1 -1
  147. package/dist/i18n/ko.d.ts.map +1 -1
  148. package/dist/i18n/ko.js +105 -22
  149. package/dist/i18n/ko.js.map +1 -1
  150. package/dist/icons/glyphs.d.ts +370 -104
  151. package/dist/icons/glyphs.d.ts.map +1 -1
  152. package/dist/icons/glyphs.js +72 -66
  153. package/dist/icons/glyphs.js.map +1 -1
  154. package/dist/icons/index.d.ts.map +1 -1
  155. package/dist/icons/index.js +9 -1
  156. package/dist/icons/index.js.map +1 -1
  157. package/dist/index.d.ts +3 -2
  158. package/dist/index.d.ts.map +1 -1
  159. package/dist/index.js +3 -2
  160. package/dist/index.js.map +1 -1
  161. package/dist/limits.d.ts +7 -0
  162. package/dist/limits.d.ts.map +1 -1
  163. package/dist/limits.js +7 -0
  164. package/dist/limits.js.map +1 -1
  165. package/dist/notifications.d.ts +124 -0
  166. package/dist/notifications.d.ts.map +1 -0
  167. package/dist/notifications.js +147 -0
  168. package/dist/notifications.js.map +1 -0
  169. package/dist/onboarding.d.ts.map +1 -1
  170. package/dist/onboarding.js +7 -12
  171. package/dist/onboarding.js.map +1 -1
  172. package/dist/plugin/capabilities.d.ts +1 -1
  173. package/dist/plugin/capabilities.d.ts.map +1 -1
  174. package/dist/plugin/capabilities.js +10 -0
  175. package/dist/plugin/capabilities.js.map +1 -1
  176. package/dist/plugin/context.d.ts +85 -1
  177. package/dist/plugin/context.d.ts.map +1 -1
  178. package/dist/plugin/limits.d.ts +7 -0
  179. package/dist/plugin/limits.d.ts.map +1 -1
  180. package/dist/plugin/limits.js +7 -0
  181. package/dist/plugin/limits.js.map +1 -1
  182. package/dist/plugin/manifest.d.ts +8 -0
  183. package/dist/plugin/manifest.d.ts.map +1 -1
  184. package/dist/plugin/manifest.js +13 -0
  185. package/dist/plugin/manifest.js.map +1 -1
  186. package/dist/plugin/rest-schemas.d.ts +22 -0
  187. package/dist/plugin/rest-schemas.d.ts.map +1 -1
  188. package/dist/plugin/rest-schemas.js +19 -3
  189. package/dist/plugin/rest-schemas.js.map +1 -1
  190. package/dist/plugin/testing.d.ts +2 -0
  191. package/dist/plugin/testing.d.ts.map +1 -1
  192. package/dist/plugin/testing.js +2 -0
  193. package/dist/plugin/testing.js.map +1 -1
  194. package/dist/plugin.d.ts +1 -1
  195. package/dist/plugin.d.ts.map +1 -1
  196. package/dist/plugin.js +1 -1
  197. package/dist/plugin.js.map +1 -1
  198. package/dist/router.d.ts +6 -0
  199. package/dist/router.d.ts.map +1 -1
  200. package/dist/router.js +5 -0
  201. package/dist/router.js.map +1 -1
  202. package/dist/routes.d.ts +2 -0
  203. package/dist/routes.d.ts.map +1 -1
  204. package/dist/routes.js +2 -0
  205. package/dist/routes.js.map +1 -1
  206. package/dist/rules.js +1 -1
  207. package/dist/rules.js.map +1 -1
  208. package/dist/runner/acp.d.ts.map +1 -1
  209. package/dist/runner/acp.js +12 -97
  210. package/dist/runner/acp.js.map +1 -1
  211. package/dist/runner/browser.d.ts +50 -0
  212. package/dist/runner/browser.d.ts.map +1 -0
  213. package/dist/runner/browser.js +104 -0
  214. package/dist/runner/browser.js.map +1 -0
  215. package/dist/runner/claude.d.ts +1 -2
  216. package/dist/runner/claude.d.ts.map +1 -1
  217. package/dist/runner/claude.js +352 -13
  218. package/dist/runner/claude.js.map +1 -1
  219. package/dist/runner/codex.d.ts +9 -10
  220. package/dist/runner/codex.d.ts.map +1 -1
  221. package/dist/runner/codex.js +809 -346
  222. package/dist/runner/codex.js.map +1 -1
  223. package/dist/runner/config.d.ts +14 -3
  224. package/dist/runner/config.d.ts.map +1 -1
  225. package/dist/runner/config.js +27 -7
  226. package/dist/runner/config.js.map +1 -1
  227. package/dist/runner/desk.d.ts +3 -1
  228. package/dist/runner/desk.d.ts.map +1 -1
  229. package/dist/runner/desk.js +17 -5
  230. package/dist/runner/desk.js.map +1 -1
  231. package/dist/runner/message.d.ts +53 -3
  232. package/dist/runner/message.d.ts.map +1 -1
  233. package/dist/runner/message.js +201 -20
  234. package/dist/runner/message.js.map +1 -1
  235. package/dist/runner/registry.d.ts +6 -0
  236. package/dist/runner/registry.d.ts.map +1 -1
  237. package/dist/runner/registry.js.map +1 -1
  238. package/dist/runner/rpc.d.ts +31 -0
  239. package/dist/runner/rpc.d.ts.map +1 -0
  240. package/dist/runner/rpc.js +102 -0
  241. package/dist/runner/rpc.js.map +1 -0
  242. package/dist/runner/runner.d.ts +41 -1
  243. package/dist/runner/runner.d.ts.map +1 -1
  244. package/dist/runner/runner.js +695 -52
  245. package/dist/runner/runner.js.map +1 -1
  246. package/dist/runner/slack.d.ts +17 -8
  247. package/dist/runner/slack.d.ts.map +1 -1
  248. package/dist/runner/slack.js +1250 -78
  249. package/dist/runner/slack.js.map +1 -1
  250. package/dist/runner/types.d.ts +160 -8
  251. package/dist/runner/types.d.ts.map +1 -1
  252. package/dist/runner/types.js.map +1 -1
  253. package/dist/runner/usage.d.ts +105 -0
  254. package/dist/runner/usage.d.ts.map +1 -0
  255. package/dist/runner/usage.js +218 -0
  256. package/dist/runner/usage.js.map +1 -0
  257. package/dist/runner.d.ts +4 -4
  258. package/dist/runner.d.ts.map +1 -1
  259. package/dist/runner.js +3 -3
  260. package/dist/runner.js.map +1 -1
  261. package/dist/schemas.d.ts +673 -0
  262. package/dist/schemas.d.ts.map +1 -1
  263. package/dist/schemas.js +205 -1
  264. package/dist/schemas.js.map +1 -1
  265. package/dist/services.js +3 -3
  266. package/dist/services.js.map +1 -1
  267. package/dist/setup-guide.d.ts +4 -3
  268. package/dist/setup-guide.d.ts.map +1 -1
  269. package/dist/setup-guide.js +11 -13
  270. package/dist/setup-guide.js.map +1 -1
  271. package/dist/text.d.ts +7 -0
  272. package/dist/text.d.ts.map +1 -1
  273. package/dist/text.js +18 -0
  274. package/dist/text.js.map +1 -1
  275. package/dist/vocabulary.d.ts +4 -2
  276. package/dist/vocabulary.d.ts.map +1 -1
  277. package/dist/vocabulary.js +4 -1
  278. package/dist/vocabulary.js.map +1 -1
  279. package/docs/specs/cli.md +26 -19
  280. package/docs/specs/plugins.md +16 -10
  281. package/docs/specs/rest.md +28 -10
  282. package/docs/specs/runner.md +132 -44
  283. package/package.json +4 -4
  284. package/skills/building-a-console/SKILL.md +2 -2
  285. package/skills/building-a-console/references/configuring.md +1 -1
  286. package/skills/building-a-plugin/SKILL.md +4 -4
  287. package/skills/planning-the-board/SKILL.md +1 -1
  288. package/skills/planning-the-board/references/setting-up.md +2 -2
  289. package/skills/running-an-agent/SKILL.md +15 -64
  290. package/skills/running-an-agent/references/preparing-the-machine.md +17 -0
  291. package/skills/running-an-agent/references/several-tasks.md +37 -0
  292. package/skills/running-an-agent/references/slack-app.md +70 -13
  293. package/skills/running-an-agent/references/the-account-and-the-invitation.md +5 -0
  294. package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +3 -0
  295. package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +16 -0
  296. package/skills/running-an-agent/references/your-first-task.md +14 -0
  297. package/skills/working-the-board/SKILL.md +9 -12
  298. package/skills/working-the-board/references/a-pack-skills-questions.md +3 -0
  299. package/skills/working-the-board/references/a-plugins-commands.md +3 -0
  300. package/skills/working-the-board/references/a-review.md +3 -0
  301. package/skills/working-the-board/references/an-open-run.md +3 -0
  302. package/skills/working-the-board/references/requirements.md +7 -0
  303. package/skills/working-the-board/references/the-pack.md +2 -2
  304. package/skills/working-the-board/references/what-they-were-told.md +3 -0
  305. package/skills/working-the-board/references/work-given-in-chat.md +5 -0
  306. package/src/agent-page.tsx +76 -47
  307. package/src/agent-usage.ts +113 -0
  308. package/src/api.ts +62 -0
  309. package/src/cli/cli.ts +634 -162
  310. package/src/cli/format.ts +190 -47
  311. package/src/cli/help.ts +77 -12
  312. package/src/cli/machine-check.ts +182 -29
  313. package/src/cli/pack.ts +81 -40
  314. package/src/cli/runner-host.ts +3 -0
  315. package/src/cli/secret-notes.ts +9 -1
  316. package/src/cli/terminal.ts +20 -4
  317. package/src/client.ts +94 -6
  318. package/src/components/agent-page.tsx +280 -63
  319. package/src/components/agents.tsx +23 -3
  320. package/src/components/calendar.tsx +93 -37
  321. package/src/components/categories.tsx +2 -2
  322. package/src/components/comments.tsx +5 -5
  323. package/src/components/decision-list.tsx +13 -10
  324. package/src/components/documents.tsx +13 -8
  325. package/src/components/event-feed.tsx +4 -203
  326. package/src/components/goals.tsx +6 -4
  327. package/src/components/home.tsx +12 -12
  328. package/src/components/icon.tsx +16 -3
  329. package/src/components/notifications.tsx +419 -0
  330. package/src/components/pack-connect.tsx +1 -1
  331. package/src/components/pack-token.tsx +5 -5
  332. package/src/components/people.tsx +3 -3
  333. package/src/components/pictures.tsx +1 -1
  334. package/src/components/plugin-settings.tsx +128 -23
  335. package/src/components/projects.tsx +1 -1
  336. package/src/components/roadmap.tsx +2 -2
  337. package/src/components/rules.tsx +2 -2
  338. package/src/components/runs.tsx +6 -3
  339. package/src/components/sessions.tsx +2 -2
  340. package/src/components/shell.tsx +3 -0
  341. package/src/components/sign-in.tsx +3 -3
  342. package/src/components/skills.tsx +5 -3
  343. package/src/components/task-view.tsx +18 -7
  344. package/src/components/tokens.tsx +2 -2
  345. package/src/components/ui.tsx +70 -5
  346. package/src/components/work.tsx +2 -2
  347. package/src/console.tsx +269 -12
  348. package/src/data.ts +54 -0
  349. package/src/desk-page.tsx +5 -5
  350. package/src/feed-words.ts +214 -0
  351. package/src/i18n/en.ts +114 -15
  352. package/src/i18n/ko.ts +115 -22
  353. package/src/icons/glyphs.ts +74 -68
  354. package/src/icons/index.ts +9 -1
  355. package/src/index.ts +11 -0
  356. package/src/limits.ts +7 -0
  357. package/src/notifications.ts +205 -0
  358. package/src/onboarding.ts +10 -14
  359. package/src/plugin/capabilities.ts +13 -0
  360. package/src/plugin/context.ts +87 -0
  361. package/src/plugin/limits.ts +7 -0
  362. package/src/plugin/manifest.ts +14 -0
  363. package/src/plugin/rest-schemas.ts +27 -3
  364. package/src/plugin/testing.ts +4 -0
  365. package/src/plugin.ts +2 -0
  366. package/src/router.ts +7 -0
  367. package/src/routes.ts +2 -0
  368. package/src/rules.ts +1 -1
  369. package/src/runner/acp.ts +12 -118
  370. package/src/runner/browser.ts +128 -0
  371. package/src/runner/claude.ts +399 -12
  372. package/src/runner/codex.ts +904 -400
  373. package/src/runner/config.ts +30 -7
  374. package/src/runner/desk.ts +22 -6
  375. package/src/runner/message.ts +251 -21
  376. package/src/runner/registry.ts +6 -0
  377. package/src/runner/rpc.ts +140 -0
  378. package/src/runner/runner.ts +863 -53
  379. package/src/runner/slack.ts +1535 -85
  380. package/src/runner/types.ts +150 -5
  381. package/src/runner/usage.ts +299 -0
  382. package/src/runner.ts +16 -1
  383. package/src/schemas.ts +249 -0
  384. package/src/services.ts +3 -3
  385. package/src/setup-guide.ts +12 -14
  386. package/src/styles/README.md +11 -7
  387. package/src/styles/console.css +2 -0
  388. package/src/styles/drawn-by-pages.json +7 -82
  389. package/src/styles/pages/agents.css +46 -35
  390. package/src/styles/pages/calendar.css +0 -1
  391. package/src/styles/pages/connect.css +0 -14
  392. package/src/styles/pages/decisions.css +2 -43
  393. package/src/styles/pages/documents.css +0 -29
  394. package/src/styles/pages/home.css +4 -16
  395. package/src/styles/pages/people.css +2 -17
  396. package/src/styles/pages/plugins.css +9 -12
  397. package/src/styles/pages/roadmap.css +0 -16
  398. package/src/styles/pages/settings.css +0 -28
  399. package/src/styles/pages/sign-in.css +2 -17
  400. package/src/styles/pages/thread.css +4 -50
  401. package/src/styles/pages/work.css +0 -5
  402. package/src/styles/parts/button.css +31 -0
  403. package/src/styles/parts/count.css +10 -0
  404. package/src/styles/parts/divider.css +17 -0
  405. package/src/styles/parts/frame.css +12 -0
  406. package/src/styles/parts/icon.css +39 -0
  407. package/src/styles/parts/menu.css +184 -0
  408. package/src/styles/parts/panel.css +7 -0
  409. package/src/styles/parts/progress.css +63 -0
  410. package/src/styles/parts/settings.css +2 -0
  411. package/src/styles/parts/sheet.css +28 -0
  412. package/src/styles/parts/text.css +34 -0
  413. package/src/text.ts +19 -0
  414. package/src/vocabulary.ts +4 -1
  415. package/template/plugin/package.json +1 -1
@@ -2,12 +2,15 @@ import { ApiError } from "../client.js";
2
2
  import { MAX_RUNNER_LINE_LENGTH, MAX_RUNNER_OFFERS, MAX_TRANSCRIPT_ATTACHMENTS, MAX_TRANSCRIPT_BATCH, MAX_TRANSCRIPT_BATCH_BYTES, MAX_TRANSCRIPT_FACT_LENGTH, MAX_TRANSCRIPT_FACT_NAME_LENGTH, MAX_TRANSCRIPT_FACTS, MAX_TRANSCRIPT_TEXT_BYTES, } from "../limits.js";
3
3
  import { RUNNER_CHOICE } from "../schemas.js";
4
4
  import { hasForbiddenCodePoint } from "../text.js";
5
- import { composeSignals, deskName, GROUP_WINDOW_MS, introOf, makeMark, } from "./message.js";
5
+ import { composeSignals, deskName, GROUP_WINDOW_MS, introOf, makeMark, sleptOf, wakeTextOf, } from "./message.js";
6
6
  import { cutShown, cutShownEnd, redactorOf } from "./redact.js";
7
+ import { limitWords, noteUsage, RESET_MARGIN_MS, runnerSays, usageSaid, usageToTell, usageWords, } from "./usage.js";
7
8
  // The runner (ADR-0085, docs/specs/runner.md): one process per agent account on its machine. It
8
9
  // keeps one session of the agent's product at a time, feeds it what the console sends, reports
9
10
  // what the product does, and says every thirty seconds that it is there. It restarts nothing by
10
- // itself: an agent that cannot go on waits, and a supervisor's signal is what is tried next.
11
+ // itself: an agent that cannot go on waits, and a supervisor's signal is what is tried next,
12
+ // but for an agent its product's usage limit stopped, which the runner brings back by itself
13
+ // when the limit resets (ADR-0150).
11
14
  // The one thing it ends for by itself is an update of the command it runs from (ADR-0101): when
12
15
  // no turn is under way, for the service that keeps it to start it again with the new one.
13
16
  //
@@ -22,6 +25,18 @@ export const BATCH_MS = 1000;
22
25
  /** How long what arrives by itself waits after the agent could not go on: at first, and at most, twice as long after each try that ends the same way. */
23
26
  export const COOL_DOWN_MS = 15 * 60_000;
24
27
  export const MAX_COOL_DOWN_MS = 4 * 60 * 60_000;
28
+ /**
29
+ * How long the runner holds an ending that waits for the agent's turn, a model chosen, the
30
+ * account's own reset, an update or a `runner.json` that changed, for the sub-agents at work
31
+ * in the background of the session (ADR-0162): counted from when it first held one for them.
32
+ */
33
+ export const SUBAGENTS_MS = 60 * 60_000;
34
+ /**
35
+ * How long a session that had sub-agents in the background is not taken for idle after one
36
+ * ended or a turn did: the product hands the agent a sub-agent's report in a turn it starts
37
+ * itself, a moment after.
38
+ */
39
+ export const REPORT_MS = 10_000;
25
40
  /** How long the runner waits before it asks for its stream of signals again. */
26
41
  export const RECONNECT_MS = 5000;
27
42
  /** What an autosave's commit says: the runner's, and never the agent's own words. */
@@ -37,6 +52,26 @@ export const MAX_HOLD_MS = 15 * 60_000;
37
52
  export const MAX_SOURCE_PAUSE_MS = 5 * 60_000;
38
53
  /** How long a wake source holds before the runner takes it to have worked. */
39
54
  const SOURCE_HELD_MS = 60_000;
55
+ /**
56
+ * How far past a heartbeat's interval the clock is, when the runner looks at it, before the
57
+ * runner takes itself not to have run in between: a process that runs is never a minute late
58
+ * for a timer of its own.
59
+ */
60
+ const SLEPT_MS = 60_000;
61
+ /** The kinds of event that are the agent's own answer: what it said, thought or called. */
62
+ const ANSWERS = new Set(["assistant", "thinking", "tool_call"]);
63
+ /** How many signals the runner writes down as kept for the agent beside what the record sends again. */
64
+ const MAX_KEPT = 50;
65
+ /** How long before its moment a timer may ring: a delivery asked for at a moment is taken within it. */
66
+ const TIMER_EARLY_MS = 20;
67
+ /**
68
+ * The furthest off a product's usage limit is waited for at the moment it names: longer than
69
+ * any window a subscription counts in, a month. A moment past it is taken for none, and the
70
+ * runner tries after the cool-down instead.
71
+ */
72
+ const MAX_LIMIT_MS = 32 * 24 * 3_600_000;
73
+ /** How many messages a turn took are remembered, for a turn that ends on the usage limit: the oldest is let go past it. */
74
+ const MAX_TAKEN = 200;
40
75
  /** The kinds of file a console keeps as a picture: what a product is handed as one. */
41
76
  const PICTURE_TYPES = new Set([
42
77
  "image/png",
@@ -59,6 +94,11 @@ const FILE_ID_BYTES = 48;
59
94
  const LEAST_CUT_BYTES = 1024;
60
95
  /** The name of the secret an account keeps its git host's token under (ADR-0088): what the autosave's push is handed. */
61
96
  export const GIT_TOKEN_SECRET = "git.token";
97
+ /** What an ending is for, as the record says it of one that waits for sub-agents (ADR-0162). */
98
+ const FOR_UPDATE = "for an update of the command the runner runs from";
99
+ const FOR_CHANGE = "for the desk's runner.json, which changed";
100
+ const FOR_CHOICE = "for the model or the effort chosen";
101
+ const FOR_RESET = "for the account's own reset";
62
102
  /** What an event takes of a batch as the console counts one: its JSON, and the ids of its pictures. */
63
103
  const sentBytes = (event) => Buffer.byteLength(JSON.stringify({
64
104
  n: event.n,
@@ -135,11 +175,15 @@ export function createRunner(options) {
135
175
  reconnectMs: RECONNECT_MS,
136
176
  holdMs: HOLD_MS,
137
177
  maxHoldMs: MAX_HOLD_MS,
178
+ resetMarginMs: RESET_MARGIN_MS,
179
+ subagentsMs: SUBAGENTS_MS,
180
+ reportMs: REPORT_MS,
138
181
  ...options.timing,
139
182
  };
140
183
  /** How long what an hour's bound refused is held back: the first time, and twice as long each time after. */
141
184
  const heldFor = (before) => before === undefined ? timing.holdMs : Math.min(before.pause * 2, timing.maxHoldMs);
142
185
  const bounds = { outboxBytes: MAX_OUTBOX_BYTES, ...options.bounds };
186
+ const moment = options.moment ?? ((iso) => iso);
143
187
  // What keeps failing is said once, and again when what is said of it changes, so that an hour
144
188
  // without the console is a line and not thousands; that it is over is said once too.
145
189
  const saidOf = new Map();
@@ -209,8 +253,32 @@ export function createRunner(options) {
209
253
  let held = false;
210
254
  /** The agent could not go on: how many tries ended so, and when what arrives by itself goes next. */
211
255
  let waits;
212
- /** The next session begins a new conversation, and says who reset it. */
256
+ /**
257
+ * The product's usage limit stopped the agent (ADR-0150): since when, until when where the
258
+ * product said, and whether the agent was handed word of it since the product last refused.
259
+ * `tried` is the supervisor's signals that were tried ahead of the reset and refused, by
260
+ * number: each is tried so once, and waits with the rest from then on. It stands until the
261
+ * agent answers again.
262
+ */
263
+ let limited;
264
+ /** What the agent was told of each window of its product's allowance: the mark it had reached, and when that window resets. */
265
+ let usageTold = new Map();
266
+ /** What the product last said of each window of its allowance, which the console is told with the heartbeat (ADR-0156). */
267
+ const usageSeen = new Map();
268
+ /**
269
+ * The signals a runner before this one kept unread, by number: taken from the stream once
270
+ * more, and until when the runner waits for them before it brings the agent back. And what
271
+ * the state file last said is kept so.
272
+ */
273
+ const reread = new Set();
274
+ let rereadBy = 0;
275
+ let keptSaved = "";
276
+ /** The console is one of before the word `usage_limit`: it is said the nearest word it takes. */
277
+ let plainWords = false;
278
+ /** The next session begins a new conversation, and says who reset it, and under which header why. */
213
279
  let resetBy;
280
+ /** The account's own reset (ADR-0154), which waits for the turn under way to end: owed until then, so not delivered. */
281
+ let ownReset;
214
282
  /** The open session is to end as soon as no turn is under way: what was chosen for it is taken when one starts. */
215
283
  let renew = false;
216
284
  let deliverAt;
@@ -240,6 +308,12 @@ export function createRunner(options) {
240
308
  let sighted;
241
309
  let reconfigure = false;
242
310
  let toldOf;
311
+ /**
312
+ * When the runner last looked at the clock, and the time it did not run in that its agent is
313
+ * still to be told of: said with the next message the agent is handed.
314
+ */
315
+ let lookedAt = options.now();
316
+ let slept;
243
317
  /** How each wake source stands, by its name: what the heartbeat says of them. */
244
318
  const sources = new Map();
245
319
  /**
@@ -249,9 +323,35 @@ export function createRunner(options) {
249
323
  */
250
324
  const sentBefore = new Set();
251
325
  const lastTry = new Set();
326
+ /**
327
+ * The signals this runner alone still holds for the agent: delivered by the record, since the
328
+ * product took them, and to be handed over again, since the agent never read them.
329
+ */
330
+ const keptOf = () => [
331
+ ...new Set([
332
+ ...reread,
333
+ ...pending.map((signal) => signal.number),
334
+ ...owed,
335
+ // The account's own reset, which waits for its turn to end.
336
+ ...(ownReset === undefined ? [] : [ownReset.number]),
337
+ ]),
338
+ ]
339
+ .filter((number) => number <= delivered)
340
+ .sort((one, other) => one - other)
341
+ .slice(0, MAX_KEPT);
252
342
  const persist = async () => {
343
+ const kept = keptOf();
344
+ keptSaved = kept.join(",");
253
345
  try {
254
- await options.state.save({ delivered, conversation, driver: driver.name });
346
+ await options.state.save({
347
+ delivered,
348
+ conversation,
349
+ driver: driver.name,
350
+ ...(limited === undefined
351
+ ? {}
352
+ : { limited: { since: limited.since, until: limited.until } }),
353
+ ...(kept.length === 0 ? {} : { kept }),
354
+ });
255
355
  }
256
356
  catch {
257
357
  log("the runner's state could not be written; it goes on from the console's record");
@@ -507,14 +607,83 @@ export function createRunner(options) {
507
607
  flushing = false;
508
608
  }
509
609
  };
610
+ /** Whether something goes on in a session with no turn of the runner's under way: sub-agents in the background, or a turn the product started or is about to. */
611
+ const goesOn = (target) => target.background > 0 || target.own || options.now() < target.reportBy;
612
+ /**
613
+ * Whether an ending is held still for what goes on in the session (ADR-0162): for as long as
614
+ * the runner waits for it, counted from when it first held an ending so, and said on the
615
+ * record once where sub-agents are what it waits for, and once more where it waited in vain.
616
+ */
617
+ const within = (target, why) => {
618
+ const now = options.now();
619
+ target.heldSince ??= now;
620
+ const waits = now - target.heldSince < timing.subagentsMs;
621
+ const word = waits ? "waits" : "over";
622
+ if (target.background > 0 && target.heldSaid !== word) {
623
+ target.heldSaid = word;
624
+ const many = `${target.background} ${target.background === 1 ? "sub-agent" : "sub-agents"} at work in the background`;
625
+ const most = `${Math.round(timing.subagentsMs / 60_000)} minutes`;
626
+ record(target, "status", waits
627
+ ? `The session is to end ${why}, with ${many}: the runner waits until none is at work, ${most} at most.`
628
+ : `The runner waited ${most} for the sub-agents in the background, and waits no longer: the session ends ${why}, with ${many}.`);
629
+ log(`session ${target.id} is to end ${why}, with ${many}: the runner ${waits ? "waits for them" : "waited for long enough"}`);
630
+ }
631
+ return waits;
632
+ };
633
+ /**
634
+ * Whether a session is at work, for an ending that waits until it is not: a turn is under
635
+ * way, or sub-agents work on in the background, or the product started a turn of its own,
636
+ * the last two for as long as the runner waits for them. What a supervisor ends, a stop, a
637
+ * restart or a reset, asks nothing of it.
638
+ */
639
+ const busy = (target, why) => target !== undefined && (target.turn || (goesOn(target) && within(target, why)));
640
+ /**
641
+ * Whether the runner is to end for an update and hands nothing more over (ADR-0101). Not
642
+ * while it waits for sub-agents in the background: that may take an hour, in which the agent
643
+ * goes on reading what it is sent.
644
+ */
645
+ const closing = () => updatedTo !== undefined &&
646
+ !(session !== undefined && session.background > 0 && within(session, FOR_UPDATE));
510
647
  /** Reads what the product does: the blocks are numbered and kept as they arrive, its own words go to the queue. */
511
648
  const pump = async (target) => {
512
649
  try {
513
650
  for await (const event of target.driver.events) {
514
651
  if (event.type === "event") {
652
+ // What the agent itself said or did, and no sub-agent of its: it has read what it
653
+ // was handed before.
654
+ if (ANSWERS.has(event.kind) && event.data?.parent === undefined) {
655
+ target.unread = 0;
656
+ if (target.returning) {
657
+ target.returning = false;
658
+ push({ t: "answered", session: target });
659
+ }
660
+ }
515
661
  record(target, event.kind, event.text, event.data, event.pictures);
516
662
  }
663
+ else if (event.type === "background") {
664
+ target.background = event.agents;
665
+ target.reportBy = options.now() + timing.reportMs;
666
+ }
667
+ else if (event.type === "turn_started") {
668
+ target.own = true;
669
+ }
670
+ else if (event.type === "cannot_go_on") {
671
+ target.own = false;
672
+ // Counted here, in the order the product wrote: the queue is behind what is read.
673
+ push({ t: "driver", session: target, event, unread: target.unread });
674
+ }
517
675
  else {
676
+ if (event.type === "taken") {
677
+ target.unread += 1;
678
+ }
679
+ else if (event.type === "turn_completed") {
680
+ target.unread = 0;
681
+ target.own = false;
682
+ // A report that came while the turn was under way is read in the next.
683
+ if (target.reportBy > 0) {
684
+ target.reportBy = options.now() + timing.reportMs;
685
+ }
686
+ }
518
687
  push({ t: "driver", session: target, event });
519
688
  }
520
689
  }
@@ -603,6 +772,12 @@ export function createRunner(options) {
603
772
  sent: [],
604
773
  handed: [],
605
774
  took: false,
775
+ taken: [],
776
+ unread: 0,
777
+ returning: false,
778
+ background: 0,
779
+ own: false,
780
+ reportBy: 0,
606
781
  n: 0,
607
782
  outbox: [],
608
783
  waiting: 0,
@@ -696,16 +871,56 @@ export function createRunner(options) {
696
871
  log(`the desk was not saved (${codeOf(error)})`);
697
872
  }
698
873
  };
874
+ /**
875
+ * Looks whether the runner ran since it last looked, with every heartbeat's timer and before
876
+ * a message is handed over. Where the clock is a minute or more past where such a timer
877
+ * leaves it, the runner did not run in between: the machine was asleep, most likely, whatever
878
+ * holds it awake. Read from the clock alone, which is the same on every system, and from no
879
+ * log of one. Said in the runner's own line, and kept for the agent with what was kept before.
880
+ * A look is the timer's own and waits for no turn of the queue: a step of the runner's that
881
+ * takes long is no time it did not run in.
882
+ */
883
+ const look = () => {
884
+ const now = options.now();
885
+ const before = lookedAt;
886
+ lookedAt = now;
887
+ if (now - before < timing.heartbeatMs + SLEPT_MS) {
888
+ return;
889
+ }
890
+ slept = {
891
+ from: slept?.from ?? before,
892
+ to: now,
893
+ lasted: (slept?.lasted ?? 0) + now - before,
894
+ times: (slept?.times ?? 0) + 1,
895
+ };
896
+ log(`the runner did not run from ${new Date(before).toISOString()} to ${new Date(now).toISOString()}: the machine was asleep, most likely`);
897
+ };
699
898
  /** Hands the product what is pending, as one message; begins a session where none is open. */
700
899
  const deliver = async (priority) => {
701
900
  deliverAt = undefined;
702
901
  // Nothing more is handed over once the runner is to end for an update: what waits is sent
703
902
  // again by the console, in order, to the runner started after.
704
- if (held || updatedTo !== undefined || pending.length === 0) {
903
+ if (held || closing()) {
904
+ return;
905
+ }
906
+ // An agent its usage limit stopped is brought back by the runner itself, with nothing to
907
+ // hand it too: word that it is back is a message of its own.
908
+ const back = limited !== undefined && !limited.handed;
909
+ // What a runner before this one kept unread comes from the stream first: the agent is not
910
+ // brought back without it, for the moment the stream takes.
911
+ if (reread.size > 0 && options.now() < rereadBy) {
912
+ scheduleDeliver(rereadBy);
705
913
  return;
706
914
  }
707
- // An agent that could not go on is tried at once for a supervisor, and after the cool-down for the rest.
708
- if (waits !== undefined && !pending.some((signal) => signal.source === "person")) {
915
+ if (pending.length === 0 && !back) {
916
+ return;
917
+ }
918
+ // An agent that could not go on is tried at once for a supervisor, and after the cool-down
919
+ // for the rest: for one a usage limit stopped, when the limit resets. Under a usage limit
920
+ // a supervisor's signal is tried so once: one the product refused waits with the rest, or
921
+ // everything that arrived afterwards would hand it to a product that refuses again.
922
+ const ahead = (signal) => signal.source === "person" && limited?.tried.has(signal.number) !== true;
923
+ if (waits !== undefined && !pending.some(ahead)) {
709
924
  if (options.now() < waits.until) {
710
925
  scheduleDeliver(waits.until);
711
926
  return;
@@ -716,7 +931,14 @@ export function createRunner(options) {
716
931
  signal.source === "schedule" &&
717
932
  ((config.conversation === "daily" && signal.key === "day") ||
718
933
  (config.conversation !== "continuous" && signal.key === "week")));
719
- if (anew && session?.turn !== true) {
934
+ // An agent that is back from a usage limit goes on in the conversation it was stopped in:
935
+ // what it was doing is there, and the day after begins the next one.
936
+ // Not with sub-agents at work in its background either (ADR-0162): the conversation they
937
+ // report to goes on, as it does under a turn.
938
+ if (anew &&
939
+ session?.turn !== true &&
940
+ (session === undefined || !goesOn(session)) &&
941
+ limited === undefined) {
720
942
  await end("restarted");
721
943
  conversation = undefined;
722
944
  }
@@ -727,8 +949,19 @@ export function createRunner(options) {
727
949
  scheduleDeliver(waits.until);
728
950
  return;
729
951
  }
730
- const batch = pending;
731
- pending = [];
952
+ // Under a usage limit, before the runner's own try, a supervisor's signals alone are tried:
953
+ // what else is kept stays kept, and undelivered, for a product that most likely refuses.
954
+ // The runner's own try is the one its timer rings for, which may be a moment before the
955
+ // clock says it is time: taken for a supervisor's, it would be said in the words of a try
956
+ // that is ahead of the reset, and counted as one.
957
+ const early = limited !== undefined && waits !== undefined && options.now() < waits.until - TIMER_EARLY_MS;
958
+ const batch = early ? pending.filter(ahead) : pending;
959
+ pending = early ? pending.filter((signal) => !ahead(signal)) : [];
960
+ if (early) {
961
+ for (const signal of batch) {
962
+ limited?.tried.add(signal.number);
963
+ }
964
+ }
732
965
  // The files a supervisor sent, read from the console and written in the desk, under its
733
966
  // ignored work/, for the agent to open; a picture is handed to the product as a picture
734
967
  // too, where the product takes one. What could not be handed over is said to the agent in
@@ -792,19 +1025,45 @@ export function createRunner(options) {
792
1025
  time: options.time,
793
1026
  attached: (signal) => attached.get(signal.number) ?? [],
794
1027
  });
795
- const intro = target.first
796
- ? introOf(target.mark, resetBy === undefined ? undefined : { by: resetBy })
797
- : undefined;
798
- const text = [intro, composed.text].filter((part) => part !== undefined).join("\n\n");
1028
+ const intro = target.first ? introOf(target.mark, resetBy) : undefined;
1029
+ // What the runner says of its own goes ahead of the signals, under its own header: that
1030
+ // the agent is back from a usage limit, and a stretch the runner did not run in, which is
1031
+ // said in the turn that follows anyway and in none of its own. The stretch is looked for
1032
+ // here too: a timer that counts no time the machine sleeps in fires a while after it woke.
1033
+ const returned = back ? limited : undefined;
1034
+ look();
1035
+ const told = slept;
1036
+ const text = [
1037
+ intro,
1038
+ returned === undefined
1039
+ ? undefined
1040
+ : runnerSays(target.mark, limitWords(returned, { now: options.now(), moment, follow: batch.length, early })),
1041
+ told === undefined ? undefined : sleptOf(told, { mark: target.mark, moment }),
1042
+ composed.text,
1043
+ ]
1044
+ .filter((part) => part !== undefined)
1045
+ .join("\n\n");
799
1046
  try {
800
1047
  await target.driver.send({ text, images, priority });
801
1048
  }
802
1049
  catch (error) {
803
1050
  // Not handed over: kept, and what the product says of itself decides what is next.
804
1051
  log(`a message could not be handed to the product (${codeOf(error)})`);
805
- pending = [...batch, ...pending];
1052
+ pending = [...batch, ...pending].sort((one, other) => one.number - other.number);
806
1053
  return;
807
1054
  }
1055
+ // Said, once it is handed over, as what the runner says of its own is. A stretch found
1056
+ // while the message was handed over is said with the next one.
1057
+ if (slept === told) {
1058
+ slept = undefined;
1059
+ }
1060
+ if (returned !== undefined) {
1061
+ // Handed over, and not yet read: the agent stands as stopped until it answers.
1062
+ returned.handed = true;
1063
+ target.returning = true;
1064
+ record(target, "status", `The runner handed the agent word that its usage limit stopped it, since ${moment(new Date(returned.since).toISOString())}, with what reached it meanwhile.`, { limit: "back", kept: batch.length });
1065
+ log(`the agent is handed word of its usage limit, with ${batch.length} kept for it`);
1066
+ }
808
1067
  if (target.first) {
809
1068
  target.first = false;
810
1069
  resetBy = undefined;
@@ -865,6 +1124,11 @@ export function createRunner(options) {
865
1124
  const take = async (target) => {
866
1125
  target.took = true;
867
1126
  const message = target.handed.shift();
1127
+ // Remembered for the turn: one that ends on the usage limit left the last of them unread.
1128
+ target.taken.push(message?.signals ?? []);
1129
+ if (target.taken.length > MAX_TAKEN) {
1130
+ target.taken.shift();
1131
+ }
868
1132
  if (message !== undefined) {
869
1133
  if (!message.written) {
870
1134
  message.write();
@@ -878,6 +1142,7 @@ export function createRunner(options) {
878
1142
  */
879
1143
  const settle = async (target) => {
880
1144
  target.took = true;
1145
+ target.taken = [];
881
1146
  const counted = [];
882
1147
  for (const message of target.handed.splice(0)) {
883
1148
  if (!message.written) {
@@ -998,6 +1263,10 @@ export function createRunner(options) {
998
1263
  delivered = next;
999
1264
  await persist();
1000
1265
  }
1266
+ else if (keptOf().join(",") !== keptSaved) {
1267
+ // What is kept for the agent changed, and what is delivered did not: written all the same.
1268
+ await persist();
1269
+ }
1001
1270
  };
1002
1271
  /** Stops the turn under way through the driver, and says so where it could not. */
1003
1272
  const stopTurn = async (target) => {
@@ -1007,6 +1276,30 @@ export function createRunner(options) {
1007
1276
  };
1008
1277
  const onSignal = async (signal) => {
1009
1278
  handled = Math.max(handled, signal.number);
1279
+ // What a runner before this one kept unread is taken from the stream once more: for its
1280
+ // words alone, whatever its kind, since it was acted on when it first came.
1281
+ const again = reread.delete(signal.number) && signal.number <= delivered;
1282
+ for (const number of reread) {
1283
+ // The stream is in order: one it passed is not coming.
1284
+ if (number < signal.number) {
1285
+ reread.delete(number);
1286
+ }
1287
+ }
1288
+ if (again) {
1289
+ // The account's own reset, which a runner before this one held for its turn to end: it
1290
+ // is taken as one, once no turn is under way, and never handed over as words alone.
1291
+ if (signal.kind === "reset" &&
1292
+ options.account !== undefined &&
1293
+ signal.by?.id === options.account) {
1294
+ ownReset = signal;
1295
+ return;
1296
+ }
1297
+ if (!pending.some((kept) => kept.number === signal.number)) {
1298
+ pending = [...pending, signal].sort((one, other) => one.number - other.number);
1299
+ scheduleDeliver();
1300
+ }
1301
+ return;
1302
+ }
1010
1303
  if (signal.number <= delivered ||
1011
1304
  owed.has(signal.number) ||
1012
1305
  pending.some((kept) => kept.number === signal.number)) {
@@ -1022,6 +1315,17 @@ export function createRunner(options) {
1022
1315
  await leave(session, session.took ? undefined : "next");
1023
1316
  record(session, "person", signal.text, signalData(signal));
1024
1317
  }
1318
+ // The account's own reset that waits is let go with the stop (ADR-0154), here and not
1319
+ // when the loop next looks: an agent that could not go on is not looked at until it
1320
+ // can, and a restart would then begin the new conversation nobody asked it for.
1321
+ if (ownReset !== undefined) {
1322
+ const gone = ownReset;
1323
+ ownReset = undefined;
1324
+ if (session !== undefined) {
1325
+ record(session, "status", `The account's own reset #${gone.number} was let go: the agent was stopped before it was taken.`, { let_go: `#${gone.number}` });
1326
+ }
1327
+ log(`#${gone.number} let go: the agent was stopped before its own reset was taken`);
1328
+ }
1025
1329
  await end("stopped");
1026
1330
  await account(signal);
1027
1331
  setState("stopped", "stop");
@@ -1029,12 +1333,32 @@ export function createRunner(options) {
1029
1333
  }
1030
1334
  case "restart":
1031
1335
  case "reset": {
1336
+ if (signal.kind === "reset" &&
1337
+ options.account !== undefined &&
1338
+ signal.by?.id === options.account) {
1339
+ // The account's own: taken by the loop once no turn is under way, the one it was
1340
+ // sent in among them. Counted as delivered at once and kept by this runner until
1341
+ // then, as what the agent never read is: owed, it would hold back the count of every
1342
+ // signal taken after it, and a runner started meanwhile would be sent those again,
1343
+ // into a conversation that knows nothing of having acted on them. A second stands
1344
+ // for the first.
1345
+ const earlier = ownReset;
1346
+ ownReset = signal;
1347
+ if (earlier !== undefined && session !== undefined) {
1348
+ record(session, "status", `The account's own reset #${earlier.number} was let go: #${signal.number} stands for it.`, { let_go: `#${earlier.number}` });
1349
+ }
1350
+ await account(signal);
1351
+ await persist();
1352
+ return;
1353
+ }
1032
1354
  held = false;
1033
1355
  waits = undefined;
1356
+ // A supervisor starts it now, whatever a limit said: the product says so again where it still stands.
1357
+ limited = undefined;
1034
1358
  await end(signal.kind === "reset" ? "reset" : "restarted");
1035
1359
  if (signal.kind === "reset") {
1036
1360
  conversation = undefined;
1037
- resetBy = by;
1361
+ resetBy = { by, ...(signal.text.trim().length > 0 ? { number: signal.number } : {}) };
1038
1362
  // What a product was handed and never took is tried once more, in the new
1039
1363
  // conversation: a product that does not start with it again is not sent it a
1040
1364
  // third time, so that no signal stands in the way of every one after it for ever.
@@ -1064,9 +1388,9 @@ export function createRunner(options) {
1064
1388
  }
1065
1389
  await account(signal);
1066
1390
  if (session !== undefined && !(await session.driver.set(settings))) {
1067
- // Taken when the product starts: the session ends when no turn is under way, and the next has it.
1391
+ // Taken when the product starts: the session ends when it is not at work, and the next has it.
1068
1392
  renew = true;
1069
- if (!session.turn) {
1393
+ if (!busy(session, FOR_CHOICE)) {
1070
1394
  await end("restarted");
1071
1395
  }
1072
1396
  }
@@ -1085,7 +1409,7 @@ export function createRunner(options) {
1085
1409
  }
1086
1410
  // Nothing is handed over once the runner is to end for an update: the turn is
1087
1411
  // stopped all the same, and what waits is sent again to the runner started after.
1088
- if (pending.length > 0 && updatedTo === undefined) {
1412
+ if (pending.length > 0 && !closing()) {
1089
1413
  if (stops) {
1090
1414
  await deliver("now");
1091
1415
  }
@@ -1100,7 +1424,7 @@ export function createRunner(options) {
1100
1424
  return;
1101
1425
  }
1102
1426
  pending.push(signal);
1103
- if (stops && updatedTo !== undefined && session !== undefined) {
1427
+ if (stops && closing() && session !== undefined) {
1104
1428
  // Its words are not handed to a runner that is to end: the turn is stopped, the
1105
1429
  // runner ends for the update, and the console sends them again to the one after.
1106
1430
  await stopTurn(session);
@@ -1124,7 +1448,140 @@ export function createRunner(options) {
1124
1448
  }
1125
1449
  }
1126
1450
  };
1127
- const onDriver = async (target, event) => {
1451
+ /**
1452
+ * The product's usage limit stopped the agent (ADR-0150): nothing goes better before a window
1453
+ * of its allowance resets. What the agent was handed and never came to is kept to be handed
1454
+ * over again, the state says why and until when, and the runner brings the agent back by
1455
+ * itself: at the moment the product named, a little after, or after the cool-down where it
1456
+ * named none or one that is past, longer each time the product refuses again.
1457
+ */
1458
+ const limitReached = async (target, until, unread) => {
1459
+ const now = options.now();
1460
+ // Taken by the product and never answered: on the record, and delivered, so kept by this
1461
+ // runner alone. Handed over and never taken: not delivered, and owed no word any more.
1462
+ const unanswered = unread > 0 ? target.taken.slice(-unread).flat() : [];
1463
+ const untaken = target.handed.splice(0).flatMap((message) => message.signals);
1464
+ for (const signal of untaken) {
1465
+ owed.delete(signal.number);
1466
+ }
1467
+ target.taken = [];
1468
+ target.sent = [];
1469
+ target.returning = false;
1470
+ const again = [...unanswered, ...untaken].filter((signal, index, all) => all.findIndex((one) => one.number === signal.number) === index &&
1471
+ !pending.some((kept) => kept.number === signal.number));
1472
+ pending = [...again, ...pending].sort((one, other) => one.number - other.number);
1473
+ const before = limited;
1474
+ // The first refusal, or the refusal of what the runner tried since: one that follows no
1475
+ // try of the runner's, a message the product still had on its input say, is the same
1476
+ // stretch, and waits no longer for it.
1477
+ const tried = before === undefined || before.handed;
1478
+ // A moment further off than any window lasts is no moment to wait for: the agent would
1479
+ // stand stopped until then with nothing tried, for a number read wrongly.
1480
+ const named = until !== undefined && until > now && until - now <= MAX_LIMIT_MS ? until : undefined;
1481
+ const known = named ?? (tried ? undefined : before.until);
1482
+ if (before === undefined) {
1483
+ // A limit met anew is said in its own word first, whatever a console of before took last.
1484
+ plainWords = false;
1485
+ }
1486
+ limited = {
1487
+ since: before?.since ?? now,
1488
+ until: known,
1489
+ handed: false,
1490
+ // What a supervisor said and the limit kept from being read was tried, and refused with
1491
+ // the limit itself: it waits for the reset as what was tried ahead of it does.
1492
+ tried: new Set([
1493
+ ...(before?.tried ?? []),
1494
+ ...again.filter((signal) => signal.source === "person").map((signal) => signal.number),
1495
+ ]),
1496
+ };
1497
+ if (tried || waits === undefined || known !== before.until) {
1498
+ waits =
1499
+ known === undefined
1500
+ ? cooled(tried ? waits : undefined)
1501
+ : { tries: (waits?.tries ?? 0) + 1, until: known + timing.resetMarginMs };
1502
+ }
1503
+ const when = known === undefined ? undefined : new Date(known).toISOString();
1504
+ // Said once for a stretch: again only where there is something new to say of it.
1505
+ if (tried || again.length > 0 || known !== before.until) {
1506
+ record(target, "status", when === undefined
1507
+ ? "The product's usage limit stopped the agent, and the product did not say when it resets: what reaches the agent meanwhile is kept, and the runner tries again by itself after a while."
1508
+ : `The product's usage limit stopped the agent until ${moment(when)}: what reaches the agent meanwhile is kept, and the runner brings it back then by itself.`, { limit: "reached", until: when ?? null, kept: again.length });
1509
+ log(`the product's usage limit stopped the agent ${when === undefined ? "and names no reset" : `until ${when}`}: tried again at ${new Date(waits.until).toISOString()}, ${again.length} kept to hand over again`);
1510
+ }
1511
+ await persist();
1512
+ setState("failed", "usage_limit");
1513
+ scheduleDeliver(waits.until);
1514
+ };
1515
+ /** The agent answered again after a usage limit: it is back, and stands as running. */
1516
+ const onAnswered = async (target) => {
1517
+ if (target !== session || limited === undefined) {
1518
+ return;
1519
+ }
1520
+ limited = undefined;
1521
+ waits = undefined;
1522
+ await persist();
1523
+ log("the agent is back from its usage limit");
1524
+ setState("running", "resumed");
1525
+ // What stayed kept while a supervisor's signal was tried is the agent's now.
1526
+ if (pending.length > 0) {
1527
+ scheduleDeliver();
1528
+ }
1529
+ };
1530
+ /**
1531
+ * What the product said of its allowance (ADR-0150): the agent is told once for each mark a
1532
+ * window reaches, at once, in the open session. After the tool call under way where a turn is
1533
+ * under way; in a turn of its own where none is, since sub-agents left at work spend the
1534
+ * allowance after the session's own turn ended, and a word kept for the next message may come
1535
+ * after the limit. What cannot be said now is not counted as told: it is said at what the
1536
+ * product says next.
1537
+ */
1538
+ const onUsage = async (target, windows) => {
1539
+ const looked = usageToTell(windows, usageTold, options.now());
1540
+ if (looked.tell.length > 0) {
1541
+ if (target.first || limited !== undefined) {
1542
+ return;
1543
+ }
1544
+ if (!(await sayToAgent(target, usageWords(looked.tell, moment)))) {
1545
+ return;
1546
+ }
1547
+ for (const one of looked.tell) {
1548
+ const used = one.window.used === undefined ? null : Math.floor(one.window.used * 100);
1549
+ const resets = one.window.resetsAt === undefined ? null : new Date(one.window.resetsAt).toISOString();
1550
+ record(target, "status", `The product's allowance is ${used === null ? "near its limit" : `${used}% used`} in its ${one.window.name} window${resets === null ? "" : `, which resets ${moment(resets)}`}: the agent is told.`, { usage: one.window.name, used, resets });
1551
+ log(`the product's allowance: ${one.window.name} ${used === null ? "near its limit" : `${used}%`}${resets === null ? "" : `, resets ${resets}`}; the agent is told`);
1552
+ }
1553
+ }
1554
+ usageTold = looked.told;
1555
+ };
1556
+ /**
1557
+ * Takes the account's own reset (ADR-0154), once no turn is under way: the open session ends
1558
+ * as reset, and the next begins a new conversation with the account's words, at once. It
1559
+ * changes nothing of how the agent stands: where a supervisor's stop came meanwhile it is let
1560
+ * go, and whoever starts the agent again says with which conversation.
1561
+ */
1562
+ const beginAnew = async (signal) => {
1563
+ ownReset = undefined;
1564
+ if (held) {
1565
+ log(`#${signal.number} let go: the agent was stopped before its own reset was taken`);
1566
+ await account(signal);
1567
+ await persist();
1568
+ return;
1569
+ }
1570
+ await end("reset");
1571
+ conversation = undefined;
1572
+ // Where a supervisor reset the agent meanwhile, the first message says so: the account's
1573
+ // words are read under their own header all the same.
1574
+ resetBy ??= {
1575
+ by: signal.by?.name ?? signal.by?.login ?? "you",
1576
+ own: true,
1577
+ number: signal.number,
1578
+ };
1579
+ pending = [...pending, signal].sort((one, other) => one.number - other.number);
1580
+ await persist();
1581
+ setState("running", "reset");
1582
+ scheduleDeliver();
1583
+ };
1584
+ const onDriver = async (target, event, unread = 0) => {
1128
1585
  if (target !== session) {
1129
1586
  return;
1130
1587
  }
@@ -1159,11 +1616,17 @@ export function createRunner(options) {
1159
1616
  lastTry.delete(signal.number);
1160
1617
  }
1161
1618
  target.sent = [];
1619
+ target.returning = false;
1620
+ if (limited !== undefined) {
1621
+ // A turn completed: whatever stopped the agent is over.
1622
+ limited = undefined;
1623
+ await persist();
1624
+ }
1162
1625
  if (waits !== undefined || stands.state !== "running") {
1163
1626
  waits = undefined;
1164
1627
  setState("running", "completed");
1165
1628
  }
1166
- if (renew) {
1629
+ if (renew && !busy(target, FOR_CHOICE)) {
1167
1630
  await end("restarted");
1168
1631
  }
1169
1632
  if (pending.length > 0) {
@@ -1171,9 +1634,25 @@ export function createRunner(options) {
1171
1634
  }
1172
1635
  return;
1173
1636
  }
1637
+ case "usage": {
1638
+ noteUsage(usageSeen, event.windows, options.now());
1639
+ await onUsage(target, event.windows);
1640
+ return;
1641
+ }
1174
1642
  case "cannot_go_on": {
1175
1643
  // Reported, and nothing restarted: a supervisor's signal is tried at once, the rest after the cool-down.
1176
1644
  target.turn = false;
1645
+ if (event.reason === "usage_limit") {
1646
+ await limitReached(target, event.until, unread);
1647
+ return;
1648
+ }
1649
+ // What the runner tried after a usage limit ended some other way: the limit is not
1650
+ // what stops the agent now.
1651
+ if (limited?.handed === true) {
1652
+ limited = undefined;
1653
+ target.returning = false;
1654
+ await persist();
1655
+ }
1177
1656
  // A product that did not start, or is not signed in, took nothing: what was sent is tried again.
1178
1657
  if (event.reason === "start" || event.reason === "sign_in") {
1179
1658
  // What a reset gave one more try and the product did not start with again is let
@@ -1214,7 +1693,32 @@ export function createRunner(options) {
1214
1693
  return;
1215
1694
  }
1216
1695
  case "ended": {
1696
+ // A product that ended before it took the word that it is back took nothing of what
1697
+ // that word was handed with: it is kept, and handed over again with the word, as what
1698
+ // a limit kept from being read is. Left to the session's end, it would be written as
1699
+ // never read and the agent told that nothing reached it meanwhile.
1700
+ if (limited?.handed === true) {
1701
+ const untaken = target.handed.splice(0).flatMap((message) => message.signals);
1702
+ for (const signal of untaken) {
1703
+ owed.delete(signal.number);
1704
+ }
1705
+ pending = [
1706
+ ...untaken.filter((signal) => !pending.some((kept) => kept.number === signal.number)),
1707
+ ...pending,
1708
+ ].sort((one, other) => one.number - other.number);
1709
+ }
1217
1710
  await end(event.how, true);
1711
+ // A product that ended before it answered the word that it is back, or refused it, was
1712
+ // not brought back: the word is owed still, at the moment the runner waits for, or
1713
+ // after the cool-down where that moment is past, longer each time it ends so.
1714
+ if (limited?.handed === true) {
1715
+ limited.handed = false;
1716
+ if (waits === undefined || options.now() >= waits.until) {
1717
+ waits = cooled(waits);
1718
+ }
1719
+ scheduleDeliver(waits.until);
1720
+ return;
1721
+ }
1218
1722
  if (pending.length > 0 && waits === undefined) {
1219
1723
  scheduleDeliver();
1220
1724
  }
@@ -1285,13 +1789,46 @@ export function createRunner(options) {
1285
1789
  push({ t: "heartbeat" });
1286
1790
  }
1287
1791
  };
1792
+ /**
1793
+ * Why the agent stands as it does, and until when where the runner knows: when its usage
1794
+ * limit resets. A console of before the word `usage_limit` refuses a heartbeat that says it,
1795
+ * and with it everything else the heartbeat carries: such a console is said `crashed`, the
1796
+ * nearest word it takes, and no moment.
1797
+ */
1798
+ const saidOfState = () => {
1799
+ if (stands.reason !== "usage_limit") {
1800
+ return { reason: stands.reason };
1801
+ }
1802
+ if (plainWords) {
1803
+ return { reason: "crashed" };
1804
+ }
1805
+ return {
1806
+ reason: stands.reason,
1807
+ until: limited?.until === undefined ? null : new Date(limited.until).toISOString(),
1808
+ };
1809
+ };
1810
+ /**
1811
+ * Whether a heartbeat was refused for the word `usage_limit`, by a console of before it: such
1812
+ * a console is said the nearest word it takes from then on, and the runner says so in a line.
1813
+ */
1814
+ const wordRefused = (error) => {
1815
+ if (!(error instanceof ApiError) ||
1816
+ error.code !== "request.invalid" ||
1817
+ stands.reason !== "usage_limit" ||
1818
+ plainWords) {
1819
+ return false;
1820
+ }
1821
+ plainWords = true;
1822
+ log("the console does not take the word usage_limit, being one of before it: the agent is said to it as failed, crashed, until the console is updated");
1823
+ return true;
1824
+ };
1288
1825
  const beat = async () => {
1289
1826
  const now = `${stands.state} ${stands.reason} ${session?.id ?? ""}`;
1290
1827
  said = now;
1291
1828
  try {
1292
1829
  const answer = await client.runnerHeartbeat({
1293
1830
  state: stands.state,
1294
- reason: stands.reason,
1831
+ ...saidOfState(),
1295
1832
  delivered,
1296
1833
  session: session?.id ?? null,
1297
1834
  machine: options.machine,
@@ -1305,6 +1842,7 @@ export function createRunner(options) {
1305
1842
  effort: choiceOf(session?.running.effort),
1306
1843
  },
1307
1844
  sources: sourcesOf(),
1845
+ usage: usageSaid(usageSeen, options.now()),
1308
1846
  });
1309
1847
  settings = answer.settings;
1310
1848
  tooOldSaid = false;
@@ -1322,6 +1860,13 @@ export function createRunner(options) {
1322
1860
  if (stopped) {
1323
1861
  return;
1324
1862
  }
1863
+ if (wordRefused(error)) {
1864
+ // Said again at once in a word that console takes, so that the account is heard from
1865
+ // and its count of what is delivered goes on.
1866
+ said = "";
1867
+ push({ t: "heartbeat" });
1868
+ return;
1869
+ }
1325
1870
  if (error instanceof ApiError && error.code === "client.outdated") {
1326
1871
  if (!tooOldSaid) {
1327
1872
  tooOldSaid = true;
@@ -1451,15 +1996,20 @@ export function createRunner(options) {
1451
1996
  }
1452
1997
  const host = {
1453
1998
  secret: async (name) => secretValues.get(name),
1999
+ channels: options.config.channels ?? [],
1454
2000
  raise: async (raised) => {
1455
2001
  // The console takes so many wakes a runner raises itself within an hour (ADR-0132):
1456
2002
  // past that, what the sources raise is let go for a while, longer each time, and said
1457
2003
  // once, so that a source that repeats itself costs the console nothing more.
1458
2004
  if (wakesHeld !== undefined && options.now() < wakesHeld.until) {
1459
- return;
2005
+ return false;
1460
2006
  }
2007
+ // Written in parts whatever the source handed over (ADR-0163): its own lines apart from
2008
+ // what someone else wrote, and a text with no line of the source's as words whole, so
2009
+ // that no word of a message reads as a line of the source's when the signal returns.
2010
+ const text = wakeTextOf("parts" in raised ? raised.parts : [{ words: raised.text }]);
1461
2011
  try {
1462
- await client.raiseWake({ source: "messenger", key: raised.key, text: raised.text });
2012
+ await client.raiseWake({ source: "messenger", key: raised.key, text });
1463
2013
  }
1464
2014
  catch (error) {
1465
2015
  if (codeOf(error) !== "signal.too_many") {
@@ -1468,18 +2018,29 @@ export function createRunner(options) {
1468
2018
  const pause = heldFor(wakesHeld);
1469
2019
  wakesHeld = { until: options.now() + pause, pause };
1470
2020
  sayOnce("wakes", "the console takes no more wakes of this runner's within the hour (signal.too_many); what its wake sources raise is let go for a while");
1471
- return;
2021
+ return false;
1472
2022
  }
1473
2023
  if (wakesHeld !== undefined) {
1474
2024
  wakesHeld = undefined;
1475
2025
  sayOver("wakes", "the console takes the wakes of this runner's wake sources again");
1476
2026
  }
2027
+ return true;
1477
2028
  },
2029
+ kept: new Map(),
1478
2030
  log,
1479
2031
  stands: (state, why) => sourceStands(source.name, state, why),
1480
2032
  fetch: wakes.fetch,
1481
2033
  socket: wakes.socket,
1482
2034
  wait: pause,
2035
+ // Who wrote, as the console answers the account's own sign-in (ADR-0164), of the
2036
+ // messenger the source is named for. A console that does not know the question, or does
2037
+ // not answer, says nothing, and the source says of an account what the messenger does.
2038
+ ...(client.writers === undefined
2039
+ ? {}
2040
+ : {
2041
+ writers: async (accounts, signal) => (await client.writers?.({ messenger: source.name, accounts: [...accounts] }, signal))
2042
+ ?.items ?? [],
2043
+ }),
1483
2044
  };
1484
2045
  sourceStands(source.name, "waiting");
1485
2046
  let wait = timing.reconnectMs;
@@ -1548,6 +2109,28 @@ export function createRunner(options) {
1548
2109
  conversation = undefined;
1549
2110
  log(`the conversation kept was begun under the driver ${kept.driver}: a new one begins under ${driver.name}`);
1550
2111
  }
2112
+ // What a runner before this one held for the agent alone is taken from the stream once
2113
+ // more, whether or not a limit still stands: a supervisor's restart under a limit lifts
2114
+ // the limit and leaves them kept, and a runner started before they were handed over
2115
+ // again would otherwise read on from after them.
2116
+ for (const number of kept?.kept ?? []) {
2117
+ reread.add(number);
2118
+ }
2119
+ if (kept?.limited !== undefined) {
2120
+ // A usage limit stopped the agent under a runner before this one: it is brought back
2121
+ // all the same, when the limit resets or after the cool-down where no moment is known.
2122
+ const now = options.now();
2123
+ const known = kept.limited.until !== undefined && kept.limited.until - now <= MAX_LIMIT_MS
2124
+ ? kept.limited.until
2125
+ : undefined;
2126
+ limited = { since: kept.limited.since, until: known, handed: false, tried: new Set() };
2127
+ waits = {
2128
+ tries: 1,
2129
+ until: known === undefined ? now + timing.coolDownMs : known + timing.resetMarginMs,
2130
+ };
2131
+ stands = { state: "failed", reason: "usage_limit" };
2132
+ log(`the product's usage limit stopped the agent before this runner started: tried again at ${new Date(waits.until).toISOString()}`);
2133
+ }
1551
2134
  probe = await driver.probe();
1552
2135
  const left = probe.models.length +
1553
2136
  probe.efforts.length -
@@ -1558,34 +2141,52 @@ export function createRunner(options) {
1558
2141
  }
1559
2142
  await readSecrets();
1560
2143
  // The first heartbeat says the runner is there, and learns where the console's record stands.
1561
- try {
1562
- const first = await client.runnerHeartbeat({
1563
- state: "running",
1564
- reason: "started",
1565
- delivered,
1566
- session: null,
1567
- machine: options.machine,
1568
- runner: options.version,
1569
- product: productOf(),
1570
- offers: offersOf(),
1571
- running: { model: null, effort: null },
1572
- });
1573
- settings = first.settings;
1574
- delivered = Math.max(delivered, first.delivered);
1575
- if (first.state === "stopped") {
1576
- held = true;
1577
- stands = { state: "stopped", reason: "stop" };
2144
+ for (;;) {
2145
+ try {
2146
+ const first = await client.runnerHeartbeat({
2147
+ state: stands.state,
2148
+ ...saidOfState(),
2149
+ delivered,
2150
+ session: null,
2151
+ machine: options.machine,
2152
+ runner: options.version,
2153
+ product: productOf(),
2154
+ offers: offersOf(),
2155
+ running: { model: null, effort: null },
2156
+ });
2157
+ settings = first.settings;
2158
+ delivered = Math.max(delivered, first.delivered);
2159
+ if (first.state === "stopped") {
2160
+ held = true;
2161
+ stands = { state: "stopped", reason: "stop" };
2162
+ }
2163
+ break;
2164
+ }
2165
+ catch (error) {
2166
+ // A runner started under a usage limit, at a console of before the word: said once more, in a word it takes.
2167
+ if (wordRefused(error)) {
2168
+ continue;
2169
+ }
2170
+ log(`the console did not take the first heartbeat (${codeOf(error)}); the runner goes on`);
2171
+ break;
1578
2172
  }
1579
2173
  }
1580
- catch (error) {
1581
- log(`the console did not take the first heartbeat (${codeOf(error)}); the runner goes on`);
1582
- }
1583
- received = delivered;
2174
+ // From before what a runner before this one kept unread, where it kept any.
2175
+ received = reread.size === 0 ? delivered : Math.min(delivered, Math.min(...reread) - 1);
2176
+ rereadBy = options.now() + timing.reconnectMs;
1584
2177
  handled = delivered;
1585
2178
  said = `${stands.state} ${stands.reason} `;
2179
+ if (waits !== undefined) {
2180
+ scheduleDeliver(waits.until);
2181
+ }
1586
2182
  const listening = listen();
1587
2183
  const sourcing = (options.wakes?.sources ?? []).map((source) => keepSource(source));
1588
- const beating = setInterval(() => push({ t: "heartbeat" }), timing.heartbeatMs);
2184
+ // Counted from here: what the runner waited for before it began is no time it did not run in.
2185
+ lookedAt = options.now();
2186
+ const beating = setInterval(() => {
2187
+ look();
2188
+ push({ t: "heartbeat" });
2189
+ }, timing.heartbeatMs);
1589
2190
  const batching = setInterval(() => push({ t: "flush" }), timing.batchMs);
1590
2191
  log(`the runner is there: ${driver.product.name} ${probe.version ?? "of a version it did not say"}, signals after #${delivered}`);
1591
2192
  if (probe.version === undefined) {
@@ -1596,8 +2197,10 @@ export function createRunner(options) {
1596
2197
  try {
1597
2198
  for (;;) {
1598
2199
  // An update is taken between two turns, and at once where none is under way; so is
1599
- // a runner.json that changed.
1600
- if ((updatedTo !== undefined || reconfigure) && session?.turn !== true) {
2200
+ // a runner.json that changed. Neither while sub-agents work on in the background of
2201
+ // the session, for as long as the runner waits for them (ADR-0162).
2202
+ if ((updatedTo !== undefined || reconfigure) &&
2203
+ !busy(session, updatedTo !== undefined ? FOR_UPDATE : FOR_CHANGE)) {
1601
2204
  how = updatedTo !== undefined ? "updated" : "changed";
1602
2205
  if (session !== undefined) {
1603
2206
  record(session, "status", updatedTo !== undefined
@@ -1615,11 +2218,14 @@ export function createRunner(options) {
1615
2218
  await onSignal(message.signal);
1616
2219
  }
1617
2220
  else if (message.t === "driver") {
1618
- await onDriver(message.session, message.event);
2221
+ await onDriver(message.session, message.event, message.unread);
2222
+ }
2223
+ else if (message.t === "answered") {
2224
+ await onAnswered(message.session);
1619
2225
  }
1620
2226
  else if (message.t === "deliver") {
1621
2227
  // A timer may fire a moment before the clock says it is time.
1622
- if (deliverAt !== undefined && options.now() >= deliverAt - 20) {
2228
+ if (deliverAt !== undefined && options.now() >= deliverAt - TIMER_EARLY_MS) {
1623
2229
  await deliver("next");
1624
2230
  }
1625
2231
  }
@@ -1629,6 +2235,11 @@ export function createRunner(options) {
1629
2235
  }
1630
2236
  }
1631
2237
  else {
2238
+ // A timer counts no time the machine sleeps in, and rings late by as much: what
2239
+ // was to be handed over at a moment is looked for by the clock at every heartbeat.
2240
+ if (deliverAt !== undefined && options.now() >= deliverAt) {
2241
+ await deliver("next");
2242
+ }
1632
2243
  await lookForUpdate();
1633
2244
  await lookForChange();
1634
2245
  // Said every thirty seconds, and at once when how the agent stands changed.
@@ -1638,6 +2249,38 @@ export function createRunner(options) {
1638
2249
  await readSecrets();
1639
2250
  }
1640
2251
  }
2252
+ // The account's own reset is taken between two turns, where the agent can go on:
2253
+ // it changes nothing of how the agent stands, so one that could not go on, the
2254
+ // model having refused or a usage limit standing, waits as it would without it,
2255
+ // and the reset is taken once a turn completed again.
2256
+ if (ownReset !== undefined &&
2257
+ waits === undefined &&
2258
+ limited === undefined &&
2259
+ !busy(session, FOR_RESET)) {
2260
+ await beginAnew(ownReset);
2261
+ }
2262
+ // A model or an effort the product takes only when it starts, held for what went
2263
+ // on in the session: it ends once nothing does. Not where the agent could not go
2264
+ // on, which keeps its session as it does under a turn.
2265
+ if (renew &&
2266
+ session !== undefined &&
2267
+ stands.state === "running" &&
2268
+ !busy(session, FOR_CHOICE)) {
2269
+ await end("restarted");
2270
+ if (pending.length > 0) {
2271
+ scheduleDeliver();
2272
+ }
2273
+ }
2274
+ // Nothing waits to end the session: the next thing that does counts from its own
2275
+ // moment how long it waits for the sub-agents.
2276
+ if (session !== undefined &&
2277
+ !renew &&
2278
+ ownReset === undefined &&
2279
+ updatedTo === undefined &&
2280
+ !reconfigure) {
2281
+ session.heldSince = undefined;
2282
+ session.heldSaid = undefined;
2283
+ }
1641
2284
  }
1642
2285
  catch (error) {
1643
2286
  log(`the runner could not do what it was about to (${codeOf(error)}); it goes on`);