@skillcdn/console 0.6.2 → 0.6.4

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 (253) hide show
  1. package/README.md +16 -9
  2. package/SKILLCDN.md +3 -2
  3. package/dist/agent-page.d.ts +2 -0
  4. package/dist/agent-page.d.ts.map +1 -1
  5. package/dist/agent-page.js +46 -5
  6. package/dist/agent-page.js.map +1 -1
  7. package/dist/agent-state.d.ts +9 -0
  8. package/dist/agent-state.d.ts.map +1 -0
  9. package/dist/agent-state.js +28 -0
  10. package/dist/agent-state.js.map +1 -0
  11. package/dist/api.d.ts +6 -4
  12. package/dist/api.d.ts.map +1 -1
  13. package/dist/api.js +6 -4
  14. package/dist/api.js.map +1 -1
  15. package/dist/cli/cli.d.ts +2 -0
  16. package/dist/cli/cli.d.ts.map +1 -1
  17. package/dist/cli/cli.js +948 -252
  18. package/dist/cli/cli.js.map +1 -1
  19. package/dist/cli/format.d.ts +44 -7
  20. package/dist/cli/format.d.ts.map +1 -1
  21. package/dist/cli/format.js +168 -13
  22. package/dist/cli/format.js.map +1 -1
  23. package/dist/cli/help.d.ts +82 -9
  24. package/dist/cli/help.d.ts.map +1 -1
  25. package/dist/cli/help.js +855 -331
  26. package/dist/cli/help.js.map +1 -1
  27. package/dist/cli/pack.d.ts +13 -12
  28. package/dist/cli/pack.d.ts.map +1 -1
  29. package/dist/cli/pack.js +33 -45
  30. package/dist/cli/pack.js.map +1 -1
  31. package/dist/cli/plugin-cli.d.ts.map +1 -1
  32. package/dist/cli/plugin-cli.js +8 -5
  33. package/dist/cli/plugin-cli.js.map +1 -1
  34. package/dist/cli/runner-files.d.ts +16 -0
  35. package/dist/cli/runner-files.d.ts.map +1 -0
  36. package/dist/cli/runner-files.js +125 -0
  37. package/dist/cli/runner-files.js.map +1 -0
  38. package/dist/cli/runner-host.d.ts +7 -0
  39. package/dist/cli/runner-host.d.ts.map +1 -1
  40. package/dist/cli/runner-host.js +40 -2
  41. package/dist/cli/runner-host.js.map +1 -1
  42. package/dist/cli/skill-path.d.ts +11 -0
  43. package/dist/cli/skill-path.d.ts.map +1 -0
  44. package/dist/cli/skill-path.js +44 -0
  45. package/dist/cli/skill-path.js.map +1 -0
  46. package/dist/client.d.ts +32 -1
  47. package/dist/client.d.ts.map +1 -1
  48. package/dist/client.js +15 -1
  49. package/dist/client.js.map +1 -1
  50. package/dist/components/agent-page.d.ts +93 -6
  51. package/dist/components/agent-page.d.ts.map +1 -1
  52. package/dist/components/agent-page.js +202 -10
  53. package/dist/components/agent-page.js.map +1 -1
  54. package/dist/components/agents.d.ts +6 -6
  55. package/dist/components/agents.d.ts.map +1 -1
  56. package/dist/components/agents.js +16 -9
  57. package/dist/components/agents.js.map +1 -1
  58. package/dist/components/people.d.ts.map +1 -1
  59. package/dist/components/people.js +7 -2
  60. package/dist/components/people.js.map +1 -1
  61. package/dist/components/picker.d.ts +16 -0
  62. package/dist/components/picker.d.ts.map +1 -1
  63. package/dist/components/picker.js +69 -4
  64. package/dist/components/picker.js.map +1 -1
  65. package/dist/console.css +40 -0
  66. package/dist/console.js +2 -2
  67. package/dist/console.js.map +1 -1
  68. package/dist/feed-words.d.ts.map +1 -1
  69. package/dist/feed-words.js +12 -1
  70. package/dist/feed-words.js.map +1 -1
  71. package/dist/i18n/en.d.ts +64 -6
  72. package/dist/i18n/en.d.ts.map +1 -1
  73. package/dist/i18n/en.js +59 -10
  74. package/dist/i18n/en.js.map +1 -1
  75. package/dist/i18n/ko.d.ts.map +1 -1
  76. package/dist/i18n/ko.js +49 -9
  77. package/dist/i18n/ko.js.map +1 -1
  78. package/dist/limits.d.ts +26 -0
  79. package/dist/limits.d.ts.map +1 -1
  80. package/dist/limits.js +26 -0
  81. package/dist/limits.js.map +1 -1
  82. package/dist/onboarding.d.ts +18 -6
  83. package/dist/onboarding.d.ts.map +1 -1
  84. package/dist/onboarding.js +42 -7
  85. package/dist/onboarding.js.map +1 -1
  86. package/dist/plugin/args.d.ts +24 -2
  87. package/dist/plugin/args.d.ts.map +1 -1
  88. package/dist/plugin/args.js +20 -2
  89. package/dist/plugin/args.js.map +1 -1
  90. package/dist/plugin/context.d.ts +39 -7
  91. package/dist/plugin/context.d.ts.map +1 -1
  92. package/dist/plugin/define.d.ts.map +1 -1
  93. package/dist/plugin/define.js +12 -3
  94. package/dist/plugin/define.js.map +1 -1
  95. package/dist/plugin/rest-schemas.d.ts +2 -0
  96. package/dist/plugin/rest-schemas.d.ts.map +1 -1
  97. package/dist/plugin/rest-schemas.js +1 -0
  98. package/dist/plugin/rest-schemas.js.map +1 -1
  99. package/dist/routes.d.ts +2 -0
  100. package/dist/routes.d.ts.map +1 -1
  101. package/dist/routes.js +2 -0
  102. package/dist/routes.js.map +1 -1
  103. package/dist/rules.d.ts +4 -1
  104. package/dist/rules.d.ts.map +1 -1
  105. package/dist/rules.js +5 -2
  106. package/dist/rules.js.map +1 -1
  107. package/dist/runner/acp.d.ts.map +1 -1
  108. package/dist/runner/acp.js +9 -3
  109. package/dist/runner/acp.js.map +1 -1
  110. package/dist/runner/claude.d.ts.map +1 -1
  111. package/dist/runner/claude.js +53 -1
  112. package/dist/runner/claude.js.map +1 -1
  113. package/dist/runner/codex.d.ts.map +1 -1
  114. package/dist/runner/codex.js +18 -3
  115. package/dist/runner/codex.js.map +1 -1
  116. package/dist/runner/config.d.ts +18 -0
  117. package/dist/runner/config.d.ts.map +1 -1
  118. package/dist/runner/config.js +77 -1
  119. package/dist/runner/config.js.map +1 -1
  120. package/dist/runner/desk.d.ts +7 -0
  121. package/dist/runner/desk.d.ts.map +1 -1
  122. package/dist/runner/desk.js +14 -4
  123. package/dist/runner/desk.js.map +1 -1
  124. package/dist/runner/message.d.ts +16 -22
  125. package/dist/runner/message.d.ts.map +1 -1
  126. package/dist/runner/message.js +72 -120
  127. package/dist/runner/message.js.map +1 -1
  128. package/dist/runner/runner.d.ts +41 -1
  129. package/dist/runner/runner.d.ts.map +1 -1
  130. package/dist/runner/runner.js +557 -23
  131. package/dist/runner/runner.js.map +1 -1
  132. package/dist/runner/slack-status.d.ts +19 -0
  133. package/dist/runner/slack-status.d.ts.map +1 -0
  134. package/dist/runner/slack-status.js +332 -0
  135. package/dist/runner/slack-status.js.map +1 -0
  136. package/dist/runner/slack.d.ts.map +1 -1
  137. package/dist/runner/slack.js +258 -59
  138. package/dist/runner/slack.js.map +1 -1
  139. package/dist/runner/types.d.ts +128 -15
  140. package/dist/runner/types.d.ts.map +1 -1
  141. package/dist/runner/types.js.map +1 -1
  142. package/dist/runner/usage.d.ts +6 -1
  143. package/dist/runner/usage.d.ts.map +1 -1
  144. package/dist/runner/usage.js +9 -2
  145. package/dist/runner/usage.js.map +1 -1
  146. package/dist/runner.d.ts +2 -2
  147. package/dist/runner.d.ts.map +1 -1
  148. package/dist/runner.js.map +1 -1
  149. package/dist/schemas.d.ts +635 -6
  150. package/dist/schemas.d.ts.map +1 -1
  151. package/dist/schemas.js +197 -15
  152. package/dist/schemas.js.map +1 -1
  153. package/dist/secret-file.d.ts +21 -0
  154. package/dist/secret-file.d.ts.map +1 -0
  155. package/dist/secret-file.js +59 -0
  156. package/dist/secret-file.js.map +1 -0
  157. package/dist/services.d.ts.map +1 -1
  158. package/dist/services.js +3 -2
  159. package/dist/services.js.map +1 -1
  160. package/dist/setup-guide.d.ts +2 -2
  161. package/dist/setup-guide.d.ts.map +1 -1
  162. package/dist/setup-guide.js +6 -5
  163. package/dist/setup-guide.js.map +1 -1
  164. package/dist/vocabulary.d.ts +17 -2
  165. package/dist/vocabulary.d.ts.map +1 -1
  166. package/dist/vocabulary.js +15 -2
  167. package/dist/vocabulary.js.map +1 -1
  168. package/dist/wake-text.d.ts +43 -0
  169. package/dist/wake-text.d.ts.map +1 -0
  170. package/dist/wake-text.js +115 -0
  171. package/dist/wake-text.js.map +1 -0
  172. package/docs/specs/cli.md +39 -25
  173. package/docs/specs/plugins.md +10 -9
  174. package/docs/specs/rest.md +34 -15
  175. package/docs/specs/runner.md +108 -25
  176. package/docs/start.md +28 -6
  177. package/package.json +3 -3
  178. package/skills/building-a-plugin/SKILL.md +2 -2
  179. package/skills/deploying-a-console/SKILL.md +3 -3
  180. package/skills/deploying-a-console/references/access.md +15 -1
  181. package/skills/deploying-a-console/references/resources.md +2 -2
  182. package/skills/deploying-a-console/scripts/github-app.mjs +593 -0
  183. package/skills/keeping-the-pack/SKILL.md +87 -0
  184. package/skills/planning-the-board/SKILL.md +6 -6
  185. package/skills/planning-the-board/references/a-task.md +1 -1
  186. package/skills/planning-the-board/references/setting-up.md +9 -5
  187. package/skills/running-an-agent/SKILL.md +21 -17
  188. package/skills/running-an-agent/references/preparing-the-machine.md +4 -3
  189. package/skills/running-an-agent/references/several-tasks.md +8 -2
  190. package/skills/running-an-agent/references/slack-app.md +31 -8
  191. package/skills/running-an-agent/references/the-account-and-the-invitation.md +1 -1
  192. package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +3 -1
  193. package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +4 -4
  194. package/skills/running-an-agent/references/your-first-task.md +11 -5
  195. package/skills/trying-a-console/SKILL.md +136 -0
  196. package/skills/trying-a-console/references/when-it-does-not-go.md +30 -0
  197. package/skills/working-the-board/SKILL.md +6 -6
  198. package/skills/working-the-board/references/terms.md +3 -1
  199. package/skills/working-the-board/references/the-pack.md +14 -7
  200. package/skills/working-the-board/references/what-they-were-told.md +1 -1
  201. package/src/agent-page.tsx +111 -4
  202. package/src/agent-state.ts +33 -0
  203. package/src/api.ts +43 -0
  204. package/src/cli/cli.ts +1190 -313
  205. package/src/cli/format.ts +207 -13
  206. package/src/cli/help.ts +991 -372
  207. package/src/cli/pack.ts +55 -62
  208. package/src/cli/plugin-cli.ts +8 -5
  209. package/src/cli/runner-files.ts +137 -0
  210. package/src/cli/runner-host.ts +48 -2
  211. package/src/cli/skill-path.ts +51 -0
  212. package/src/client.ts +103 -0
  213. package/src/components/agent-page.tsx +446 -20
  214. package/src/components/agents.tsx +27 -28
  215. package/src/components/people.tsx +7 -3
  216. package/src/components/picker.tsx +79 -5
  217. package/src/console.tsx +1 -1
  218. package/src/feed-words.ts +14 -1
  219. package/src/i18n/en.ts +64 -11
  220. package/src/i18n/ko.ts +55 -9
  221. package/src/limits.ts +26 -0
  222. package/src/onboarding.ts +60 -21
  223. package/src/plugin/args.ts +29 -3
  224. package/src/plugin/context.ts +40 -7
  225. package/src/plugin/define.ts +18 -3
  226. package/src/plugin/rest-schemas.ts +3 -0
  227. package/src/routes.ts +2 -0
  228. package/src/rules.ts +5 -2
  229. package/src/runner/acp.ts +9 -3
  230. package/src/runner/claude.ts +58 -1
  231. package/src/runner/codex.ts +18 -3
  232. package/src/runner/config.ts +114 -1
  233. package/src/runner/desk.ts +15 -4
  234. package/src/runner/message.ts +88 -126
  235. package/src/runner/runner.ts +682 -27
  236. package/src/runner/slack-status.ts +423 -0
  237. package/src/runner/slack.ts +310 -67
  238. package/src/runner/types.ts +124 -13
  239. package/src/runner/usage.ts +16 -3
  240. package/src/runner.ts +4 -0
  241. package/src/schemas.ts +245 -13
  242. package/src/secret-file.ts +76 -0
  243. package/src/services.ts +3 -2
  244. package/src/setup-guide.ts +6 -5
  245. package/src/styles/README.md +1 -1
  246. package/src/styles/pages/agents.css +40 -0
  247. package/src/vocabulary.ts +21 -2
  248. package/src/wake-text.ts +133 -0
  249. package/dist/cli/command-words.d.ts +0 -20
  250. package/dist/cli/command-words.d.ts.map +0 -1
  251. package/dist/cli/command-words.js +0 -51
  252. package/dist/cli/command-words.js.map +0 -1
  253. package/src/cli/command-words.ts +0 -56
@@ -1,4 +1,5 @@
1
1
  import { ApiError, type ConsoleClient } from "../client.js";
2
+ import { compareVersions, judgeClient, parseVersion } from "../compat.js";
2
3
  import {
3
4
  MAX_RUNNER_LINE_LENGTH,
4
5
  MAX_RUNNER_OFFERS,
@@ -13,7 +14,7 @@ import {
13
14
  import { type RestSignal, type RestTranscriptEventInput, RUNNER_CHOICE } from "../schemas.js";
14
15
  import { hasForbiddenCodePoint } from "../text.js";
15
16
  import type { AgentStateReason, TranscriptKind } from "../vocabulary.js";
16
- import type { RunnerConfig } from "./config.js";
17
+ import { type RunnerConfig, STATUS_AFTER } from "./config.js";
17
18
  import {
18
19
  type Attached,
19
20
  composeSignals,
@@ -27,6 +28,7 @@ import {
27
28
  } from "./message.js";
28
29
  import { cutShown, cutShownEnd, type Redactor, redactorOf } from "./redact.js";
29
30
  import type {
31
+ AgentStatus,
30
32
  Driver,
31
33
  DriverEvent,
32
34
  DriverSession,
@@ -63,6 +65,15 @@ import {
63
65
  // numbered and kept for the next batch as it arrives, so that nothing of a session is left
64
66
  // behind when the session ends.
65
67
 
68
+ /** How long a wake a source raised is waited for on the stream before the source is shown nothing of it. */
69
+ const RAISED_MS = 60_000;
70
+ /** From how far away a moment is said with its day in a status. */
71
+ const FAR_MS = 20 * 60 * 60_000;
72
+ /** How long the runner's end waits for the wake sources to take back what they showed. */
73
+ const SHOWN_END_MS = 3000;
74
+ /** What the wake sources are said as the runner ends. */
75
+ const LAST_STATUS: AgentStatus = { state: "idle", last: true };
76
+
66
77
  /** How often the runner says it is there. */
67
78
  export const HEARTBEAT_MS = 30_000;
68
79
  /** How often what the product did is sent, while there is something to send. */
@@ -105,8 +116,22 @@ const SOURCE_HELD_MS = 60_000;
105
116
  const SLEPT_MS = 60_000;
106
117
  /** The kinds of event that are the agent's own answer: what it said, thought or called. */
107
118
  const ANSWERS: ReadonlySet<TranscriptKind> = new Set(["assistant", "thinking", "tool_call"]);
119
+ /**
120
+ * How long before its wake is on the record a message may have been written (ADR-0166): what a
121
+ * wake source reads for a wake, and the wakes ahead of it, are waited for half a minute at the
122
+ * most, and the messenger's clock and the console's differ by less than the rest.
123
+ */
124
+ const RAISED_AFTER_MS = 60_000;
125
+ /** What an agent told to update the command is told with it (ADR-0101): the runner does the rest. */
126
+ const UPDATE_FOLLOWS =
127
+ "The runner sees the update and is started again with it by itself, once no turn is under way.";
128
+ const UPDATE_OTHERWISE = "Where you cannot update it, say so to whom you report to.";
108
129
  /** How many signals the runner writes down as kept for the agent beside what the record sends again. */
109
130
  const MAX_KEPT = 50;
131
+ /** How many kinds of output a driver has no reading for the runner keeps as said on a record already. */
132
+ const MAX_UNKNOWN_SAID = 32;
133
+ /** How many of them a session names where it says that the product's version may be why. */
134
+ const MAX_UNKNOWN_NAMED = 3;
110
135
  /** How long before its moment a timer may ring: a delivery asked for at a moment is taken within it. */
111
136
  const TIMER_EARLY_MS = 20;
112
137
  /**
@@ -188,8 +213,29 @@ export interface RunnerState {
188
213
  * runner started meanwhile takes them from the stream once more.
189
214
  */
190
215
  readonly kept?: readonly number[] | undefined;
216
+ /**
217
+ * When the conversation the agent goes on with began, in milliseconds since the epoch
218
+ * (ADR-0166): what it was handed before, it holds no more, and a wake source counts nothing
219
+ * from before as brought. Left out by a runner of before, whose conversation is taken to hold
220
+ * all it was handed.
221
+ */
222
+ readonly began?: number | undefined;
223
+ /**
224
+ * The kinds of the product's output the driver had no reading for that are on a session's
225
+ * record already (ADR-0182), each in the driver's own words: a kind is said there once, and
226
+ * not at every session a version of the product writes it in.
227
+ */
228
+ readonly unknown?: readonly string[] | undefined;
229
+ /**
230
+ * The version of the console the agent was told a newer command exists for (ADR-0182): said
231
+ * once for a version, whichever runner said it.
232
+ */
233
+ readonly newer?: string | undefined;
191
234
  }
192
235
 
236
+ /** What a wake source keeps, as it is written down: each name with what stands under it, the oldest first. */
237
+ export type KeptBySource = readonly (readonly [string, string])[];
238
+
193
239
  export interface RunnerOptions {
194
240
  readonly client: RunnerClient;
195
241
  readonly driver: Driver;
@@ -215,6 +261,11 @@ export interface RunnerOptions {
215
261
  * for an agent whose runner the console refuses as too old; nothing where it cannot be said.
216
262
  */
217
263
  readonly updateCommand?: (() => string | undefined) | undefined;
264
+ /**
265
+ * The console's version, as its last answer said it (ADR-0025); nothing where none said one.
266
+ * Left out, the runner says nothing to its agent of a newer command (ADR-0182).
267
+ */
268
+ readonly consoleVersion?: (() => string | undefined) | undefined;
218
269
  readonly now: () => number;
219
270
  readonly random: () => number;
220
271
  /** A moment as a header says it: the hour and the minute in the workspace's zone. */
@@ -259,8 +310,25 @@ export interface RunnerOptions {
259
310
  readonly sources: readonly WakeSource[];
260
311
  readonly fetch: WakeHost["fetch"];
261
312
  readonly socket: WakeHost["socket"];
313
+ /**
314
+ * Where what a wake source keeps (`WakeHost.kept`) outlasts the runner (ADR-0166): read
315
+ * when the source is started, and written when it changed, a source at a time by its
316
+ * name. Left out, a runner started again begins with nothing kept.
317
+ */
318
+ readonly kept?:
319
+ | {
320
+ load(source: string): Promise<KeptBySource | undefined>;
321
+ save(source: string, kept: KeptBySource): Promise<void>;
322
+ }
323
+ | undefined;
262
324
  }
263
325
  | undefined;
326
+ /**
327
+ * The language of the workspace, where whoever starts the runner knows it: what a wake source
328
+ * shows of the agent to the people at its messenger is in it, where the source has the words
329
+ * (ADR-0170). Left out, the source's own.
330
+ */
331
+ readonly language?: string | undefined;
264
332
  /**
265
333
  * How the desk's `runner.json` stands against the one this runner was started with (ADR-0144):
266
334
  * the same, another that reads, or one that does not read, with what is wrong with it in a few
@@ -376,6 +444,12 @@ interface OpenSession {
376
444
  */
377
445
  taken: (readonly RestSignal[])[];
378
446
  unread: number;
447
+ /**
448
+ * The signals the product took before it put the conversation in fewer words (ADR-0166), by
449
+ * number: the conversation holds a summary of them at best, so a usage limit met afterwards
450
+ * has them said whole again, and not named.
451
+ */
452
+ readonly summarized: Set<number>;
379
453
  /** Whether the agent was handed word that it is back from a usage limit, and has not answered since. */
380
454
  returning: boolean;
381
455
  /** How many sub-agents are at work in its background, as the driver said last (ADR-0162). */
@@ -406,6 +480,10 @@ interface OpenSession {
406
480
  /** Why the console did not take what waits, in its code: said where the session ends with it unsent. */
407
481
  unsent?: string | undefined;
408
482
  running: { model: string | null; effort: string | null };
483
+ /** The kinds of output the driver said it has no reading for, in this session (ADR-0182). */
484
+ readonly unknown: string[];
485
+ /** Whether its record says that the product's version may be why something went wrong. */
486
+ versionSaid: boolean;
409
487
  /** The files written for it, to remove at its end. */
410
488
  readonly written: string[];
411
489
  /** Resolves when the product's output has been read to its end. */
@@ -425,6 +503,8 @@ type Message =
425
503
  | { readonly t: "answered"; readonly session: OpenSession }
426
504
  | { readonly t: "deliver" }
427
505
  | { readonly t: "flush" }
506
+ /** A wake source raised something, or what it raised was not taken: how the agent stands to it is looked at again. */
507
+ | { readonly t: "shown" }
428
508
  | { readonly t: "heartbeat" }
429
509
  | { readonly t: "stop" };
430
510
 
@@ -489,6 +569,22 @@ const lineOf = (value: string | null | undefined): string | null => {
489
569
  return line.length === 0 || hasForbiddenCodePoint(line) ? null : line;
490
570
  };
491
571
 
572
+ /** What a wake source keeps, which says whether it changed since it was last written down. */
573
+ class Kept extends Map<string, string> {
574
+ changed = false;
575
+
576
+ override set(name: string, value: string): this {
577
+ this.changed = true;
578
+ return super.set(name, value);
579
+ }
580
+
581
+ override delete(name: string): boolean {
582
+ const had = super.delete(name);
583
+ this.changed ||= had;
584
+ return had;
585
+ }
586
+ }
587
+
492
588
  export function createRunner(options: RunnerOptions): Runner {
493
589
  const { client, driver, config, log } = options;
494
590
  const timing = {
@@ -568,14 +664,45 @@ export function createRunner(options: RunnerOptions): Runner {
568
664
  let received = 0;
569
665
  let handled = 0;
570
666
  let conversation: string | undefined;
571
- let stands: { state: "running" | "stopped" | "refused" | "failed"; reason: AgentStateReason } = {
667
+ /**
668
+ * When the conversation the agent goes on with began (ADR-0166), 0 where nothing says, and
669
+ * whether that moment is of a conversation no session has begun yet: the session that begins
670
+ * it then leaves the moment as it stands.
671
+ */
672
+ let began = 0;
673
+ let renewed = false;
674
+ /**
675
+ * The signals the conversation holds already, by number (ADR-0166): the product of the open
676
+ * session took them there as a usage limit was reached, its driver vouching that what it
677
+ * took stands (`can.holdsTaken`), and the agent never answered. Handed over again in that
678
+ * session, they are named and not said a second time. Until a turn completes, the
679
+ * conversation is let go or put in fewer words, or the session ends: nothing says before a
680
+ * session's first message whether its product went on with the conversation, so a session
681
+ * begun after is said them whole.
682
+ */
683
+ const above = new Set<number>();
684
+ /** What each wake source keeps, by its name, and whether it changed since it was last written. */
685
+ const keeps = new Map<string, Kept>();
686
+ /**
687
+ * How the agent stands and why, with when the runner said so (`at`): the console is told that
688
+ * moment with the reason, since a reason is said again the same under a state that stays, a
689
+ * turn that completed after another say, and nothing else tells the two apart.
690
+ */
691
+ let stands: {
692
+ state: "running" | "stopped" | "refused" | "failed";
693
+ reason: AgentStateReason;
694
+ at: number;
695
+ } = {
572
696
  state: "running",
573
697
  reason: "started",
698
+ at: options.now(),
574
699
  };
575
700
  let said = "";
576
701
  let settings: { model: string | null; effort: string | null } = { model: null, effort: null };
577
702
  let probe: Probe = { version: undefined, models: [], efforts: [] };
578
703
  let productVersion: string | null = null;
704
+ /** The kinds of output the driver had no reading for that a record says already (ADR-0182). */
705
+ const unknownSaid = new Set<string>();
579
706
  let session: OpenSession | undefined;
580
707
  /** What was received and not handed to the product yet, in order. */
581
708
  let pending: RestSignal[] = [];
@@ -622,6 +749,8 @@ export function createRunner(options: RunnerOptions): Runner {
622
749
  let secretValues = new Map<string, string>();
623
750
  let redact: Redactor = redactorOf([]);
624
751
  let tooOldSaid = false;
752
+ /** The version of the console the agent was told a newer command exists for (ADR-0182). */
753
+ let toldNewer: string | undefined;
625
754
  /** The console took as many wakes of this runner's as an hour takes: until when none is raised, and how long that was. */
626
755
  let wakesHeld: { readonly until: number; readonly pause: number } | undefined;
627
756
  /**
@@ -653,6 +782,28 @@ export function createRunner(options: RunnerOptions): Runner {
653
782
  string,
654
783
  { state: "connected" | "waiting" | "failing"; since: number; why: string | undefined }
655
784
  >();
785
+ /**
786
+ * The wake sources that show how the agent stands (ADR-0170), by name: the host each runs
787
+ * with, and what it was last said and when.
788
+ */
789
+ const shows = new Map<
790
+ string,
791
+ { readonly source: WakeSource; readonly host: WakeHost; said: string; at: number }
792
+ >();
793
+ /**
794
+ * What `runner.json` says of how the agent stands is shown: `false` for not at all, and the
795
+ * sources' own way where it says nothing, a desk of before and a config put together by hand
796
+ * among them.
797
+ */
798
+ const shownAs: NonNullable<RunnerConfig["status"]> = config.status ?? {};
799
+ /** Since when the agent is at work without a pause: a turn under way, or something that goes on in its session with none. */
800
+ let atWorkSince: number | undefined;
801
+ /**
802
+ * What each wake source raised that the stream has not brought back yet: when it was raised,
803
+ * and its number once the console took it. A message that reached the runner is on its way
804
+ * to the agent from then on, which its source shows.
805
+ */
806
+ const raising = new Map<string, Set<{ readonly at: number; number: number | undefined }>>();
656
807
  /**
657
808
  * The signals a product was handed and never took, since it did not start or was not signed
658
809
  * in, which are kept to be sent again; and, of those, the ones a reset gave one more try: a
@@ -691,14 +842,116 @@ export function createRunner(options: RunnerOptions): Runner {
691
842
  ? {}
692
843
  : { limited: { since: limited.since, until: limited.until } }),
693
844
  ...(kept.length === 0 ? {} : { kept }),
845
+ ...(began === 0 ? {} : { began }),
846
+ ...(unknownSaid.size === 0 ? {} : { unknown: [...unknownSaid] }),
847
+ ...(toldNewer === undefined ? {} : { newer: toldNewer }),
694
848
  });
695
849
  } catch {
696
850
  log("the runner's state could not be written; it goes on from the console's record");
697
851
  }
698
852
  };
699
853
 
854
+ /**
855
+ * When a conversation that begins now is taken to have begun (ADR-0166): now, or, where wakes
856
+ * of a wake source wait to be handed over, a little before the oldest of them was raised.
857
+ * They are handed over in the new conversation, which holds them from then on: counted as
858
+ * from before it, their messages would be carried once more with the next wake. `handed` is
859
+ * what the conversation holds beside what waits: what a session was handed in it already,
860
+ * where the moment is set once the session is under way.
861
+ */
862
+ const beginsAt = (handed: readonly RestSignal[] = []): number => {
863
+ let at = options.now();
864
+ for (const signal of [...pending, ...handed]) {
865
+ const raised = signal.source === "messenger" ? Date.parse(signal.at) : Number.NaN;
866
+ if (Number.isFinite(raised)) {
867
+ at = Math.min(at, raised - RAISED_AFTER_MS);
868
+ }
869
+ }
870
+ return at;
871
+ };
872
+
873
+ /**
874
+ * The driver named the conversation its session runs in. Where it began one in place of the
875
+ * one it was asked to go on with (`anew`), the agent's conversation began with the session
876
+ * (ADR-0166): it holds what the session was handed, and nothing from before.
877
+ */
878
+ const converses = (
879
+ target: OpenSession,
880
+ event: Extract<DriverEvent, { type: "conversation" }>,
881
+ ): void => {
882
+ conversation = event.id;
883
+ if (event.anew === true) {
884
+ began = beginsAt([...target.sent, ...target.handed.flatMap((message) => message.signals)]);
885
+ above.clear();
886
+ }
887
+ };
888
+
889
+ /**
890
+ * The product put the conversation in fewer words (ADR-0166): what the agent was handed
891
+ * before is a summary at best, so what a wake source brought before counts no more, and what
892
+ * the product took is not named as standing there. What it was handed and has not taken yet
893
+ * it reads after, whole: the conversation is taken to hold that.
894
+ */
895
+ const summarized = (target: OpenSession): void => {
896
+ began = beginsAt(target.handed.flatMap((message) => message.signals));
897
+ above.clear();
898
+ for (const signal of target.taken.flat()) {
899
+ target.summarized.add(signal.number);
900
+ }
901
+ };
902
+
903
+ /**
904
+ * When the agent's conversation began, as a wake source is told it (ADR-0166): never after
905
+ * now. A moment ahead of the clock, kept by a runner whose machine's clock ran ahead or set
906
+ * back since, is none a message was written before: counted from, every wake of a place
907
+ * would carry again what the last one carried, for as long as the clock is behind it. It is
908
+ * taken as now, and kept so.
909
+ */
910
+ const beganBy = (): number => {
911
+ const now = options.now();
912
+ if (began > now) {
913
+ began = now;
914
+ void persist();
915
+ }
916
+ return began;
917
+ };
918
+
919
+ /**
920
+ * Lets go of the conversation kept: the next session begins a new one, which holds nothing
921
+ * the agent was handed before this moment (ADR-0166), but for what waits to be handed over.
922
+ */
923
+ const letGo = (): void => {
924
+ conversation = undefined;
925
+ began = beginsAt();
926
+ renewed = true;
927
+ above.clear();
928
+ };
929
+
930
+ /** Writes what the wake sources keep where it changed since it was last written. */
931
+ const keepKept = async (): Promise<void> => {
932
+ const store = options.wakes?.kept;
933
+ if (store === undefined) {
934
+ return;
935
+ }
936
+ for (const [name, kept] of keeps) {
937
+ if (!kept.changed) {
938
+ continue;
939
+ }
940
+ kept.changed = false;
941
+ try {
942
+ await store.save(name, [...kept]);
943
+ } catch {
944
+ // Kept in memory still, and written with the next change.
945
+ sayOnce(
946
+ `kept ${name}`,
947
+ `what the wake source ${name} keeps could not be written; it is kept while the runner runs`,
948
+ );
949
+ }
950
+ }
951
+ };
952
+
700
953
  const setState = (state: typeof stands.state, reason: AgentStateReason): void => {
701
- stands = { state, reason };
954
+ stands = { state, reason, at: options.now() };
702
955
  push({ t: "heartbeat" });
703
956
  };
704
957
 
@@ -1144,6 +1397,7 @@ export function createRunner(options: RunnerOptions): Runner {
1144
1397
  took: false,
1145
1398
  taken: [],
1146
1399
  unread: 0,
1400
+ summarized: new Set(),
1147
1401
  returning: false,
1148
1402
  background: 0,
1149
1403
  own: false,
@@ -1153,12 +1407,24 @@ export function createRunner(options: RunnerOptions): Runner {
1153
1407
  waiting: 0,
1154
1408
  refused: false,
1155
1409
  running: { model: settings.model, effort: settings.effort },
1410
+ unknown: [],
1411
+ versionSaid: false,
1156
1412
  written: handed.written,
1157
1413
  pumped: Promise.resolve(),
1158
1414
  };
1159
1415
  started.pumped = pump(started);
1160
1416
  session = started;
1161
1417
  renew = false;
1418
+ if (resume === undefined) {
1419
+ // A conversation of its own: it holds nothing the agent was handed before (ADR-0166).
1420
+ // Where it was let go a moment ago, at a reset say, it began then, and what was kept
1421
+ // for it since is handed over in it.
1422
+ if (!renewed) {
1423
+ began = beginsAt();
1424
+ }
1425
+ above.clear();
1426
+ }
1427
+ renewed = false;
1162
1428
  log(
1163
1429
  `session ${started.id} began${resume === undefined ? "" : ", going on with the conversation before"}`,
1164
1430
  );
@@ -1181,6 +1447,9 @@ export function createRunner(options: RunnerOptions): Runner {
1181
1447
  }
1182
1448
  await target.pumped;
1183
1449
  await hearLast(target);
1450
+ // What was named as standing in the conversation was taken by this session's product: a
1451
+ // session begun after is said it whole (ADR-0166).
1452
+ above.clear();
1184
1453
  // What the product was handed and never took is sent again, by the console where the
1185
1454
  // runner itself ends and by the runner where the product had not started; else it is on
1186
1455
  // the record as never read.
@@ -1331,7 +1600,7 @@ export function createRunner(options: RunnerOptions): Runner {
1331
1600
  limited === undefined
1332
1601
  ) {
1333
1602
  await end("restarted");
1334
- conversation = undefined;
1603
+ letGo();
1335
1604
  }
1336
1605
  const target = session ?? (await begin());
1337
1606
  if (target === undefined) {
@@ -1365,6 +1634,11 @@ export function createRunner(options: RunnerOptions): Runner {
1365
1634
  // what the driver says of its product.
1366
1635
  const takesPictures = target.driver.images?.() ?? driver.can.images;
1367
1636
  for (const signal of batch) {
1637
+ // What is only named is handed over with nothing: its files stand in the desk and its
1638
+ // pictures in the conversation, with the message the product took (ADR-0166).
1639
+ if (above.has(signal.number)) {
1640
+ continue;
1641
+ }
1368
1642
  const taken = new Set<string>();
1369
1643
  for (const file of signal.attachments) {
1370
1644
  const picture = PICTURE_TYPES.has(file.contentType);
@@ -1411,10 +1685,15 @@ export function createRunner(options: RunnerOptions): Runner {
1411
1685
  ]);
1412
1686
  }
1413
1687
  }
1688
+ // What the conversation holds already, the product having taken it there as the limit was
1689
+ // reached, is named and not said again (ADR-0166).
1690
+ const named = batch.filter((signal) => above.has(signal.number)).length;
1414
1691
  const composed = composeSignals(batch, {
1415
1692
  mark: target.mark,
1416
1693
  time: options.time,
1417
1694
  attached: (signal) => attached.get(signal.number) ?? [],
1695
+ own: options.account,
1696
+ above: (signal) => above.has(signal.number),
1418
1697
  });
1419
1698
  const intro = target.first ? introOf(target.mark, resetBy) : undefined;
1420
1699
  // What the runner says of its own goes ahead of the signals, under its own header: that
@@ -1424,15 +1703,23 @@ export function createRunner(options: RunnerOptions): Runner {
1424
1703
  const returned = back ? limited : undefined;
1425
1704
  look();
1426
1705
  const told = slept;
1706
+ const newer = newerCommand();
1427
1707
  const text = [
1428
1708
  intro,
1429
1709
  returned === undefined
1430
1710
  ? undefined
1431
1711
  : runnerSays(
1432
1712
  target.mark,
1433
- limitWords(returned, { now: options.now(), moment, follow: batch.length, early }),
1713
+ limitWords(returned, {
1714
+ now: options.now(),
1715
+ moment,
1716
+ follow: batch.length - named,
1717
+ above: named,
1718
+ early,
1719
+ }),
1434
1720
  ),
1435
1721
  told === undefined ? undefined : sleptOf(told, { mark: target.mark, moment }),
1722
+ newer === undefined ? undefined : runnerSays(target.mark, newer.words),
1436
1723
  composed.text,
1437
1724
  ]
1438
1725
  .filter((part) => part !== undefined)
@@ -1450,6 +1737,14 @@ export function createRunner(options: RunnerOptions): Runner {
1450
1737
  if (slept === told) {
1451
1738
  slept = undefined;
1452
1739
  }
1740
+ if (newer !== undefined) {
1741
+ // Told once for that version of the console, whichever runner is started after.
1742
+ toldNewer = newer.version;
1743
+ log(
1744
+ `a newer command exists: the console runs ${newer.version}, and this runner runs from ${options.version}; the agent was told, with what installs it`,
1745
+ );
1746
+ await persist();
1747
+ }
1453
1748
  if (returned !== undefined) {
1454
1749
  // Handed over, and not yet read: the agent stands as stopped until it answers.
1455
1750
  returned.handed = true;
@@ -1564,7 +1859,9 @@ export function createRunner(options: RunnerOptions): Runner {
1564
1859
  * Hears what the product said as its session was ended. Its words wait in the queue behind
1565
1860
  * whatever ended the session, and are let go there once the session is no longer the open
1566
1861
  * one: a message it said it took then is taken, and not sent again, and the conversation it
1567
- * named is the one to go on with, where that message is.
1862
+ * named is the one to go on with, where that message is. That it began that conversation
1863
+ * anew, or put it in fewer words, moves the moment the conversation began as it does in a
1864
+ * session that goes on (ADR-0166): the next session goes on with what was said here.
1568
1865
  */
1569
1866
  const hearLast = async (target: OpenSession): Promise<void> => {
1570
1867
  let named = false;
@@ -1574,7 +1871,10 @@ export function createRunner(options: RunnerOptions): Runner {
1574
1871
  }
1575
1872
  const { event } = message;
1576
1873
  if (event.type === "conversation") {
1577
- conversation = event.id;
1874
+ converses(target, event);
1875
+ named = true;
1876
+ } else if (event.type === "compacted") {
1877
+ summarized(target);
1578
1878
  named = true;
1579
1879
  } else if (event.type === "taken") {
1580
1880
  await take(target);
@@ -1793,7 +2093,7 @@ export function createRunner(options: RunnerOptions): Runner {
1793
2093
  limited = undefined;
1794
2094
  await end(signal.kind === "reset" ? "reset" : "restarted");
1795
2095
  if (signal.kind === "reset") {
1796
- conversation = undefined;
2096
+ letGo();
1797
2097
  resetBy = { by, ...(signal.text.trim().length > 0 ? { number: signal.number } : {}) };
1798
2098
  // What a product was handed and never took is tried once more, in the new
1799
2099
  // conversation: a product that does not start with it again is not sent it a
@@ -1899,6 +2199,17 @@ export function createRunner(options: RunnerOptions): Runner {
1899
2199
  for (const signal of untaken) {
1900
2200
  owed.delete(signal.number);
1901
2201
  }
2202
+ // What the product took stands in the conversation, which the agent goes on in: handed
2203
+ // over again in this session, it is named and not said a second time (ADR-0166). Only
2204
+ // where the driver vouches that what it said was taken stands there, and not for what was
2205
+ // taken before the product put the conversation in fewer words.
2206
+ if (driver.can.holdsTaken === true) {
2207
+ for (const signal of unanswered) {
2208
+ if (!target.summarized.has(signal.number)) {
2209
+ above.add(signal.number);
2210
+ }
2211
+ }
2212
+ }
1902
2213
  target.taken = [];
1903
2214
  target.sent = [];
1904
2215
  target.returning = false;
@@ -2028,7 +2339,7 @@ export function createRunner(options: RunnerOptions): Runner {
2028
2339
  return;
2029
2340
  }
2030
2341
  await end("reset");
2031
- conversation = undefined;
2342
+ letGo();
2032
2343
  // Where a supervisor reset the agent meanwhile, the first message says so: the account's
2033
2344
  // words are read under their own header all the same.
2034
2345
  resetBy ??= {
@@ -2042,6 +2353,76 @@ export function createRunner(options: RunnerOptions): Runner {
2042
2353
  scheduleDeliver();
2043
2354
  };
2044
2355
 
2356
+ /**
2357
+ * What is said of the product's version where the driver was not tried with it (ADR-0182),
2358
+ * or nothing: where it was, where the product said no version, and for a driver that names
2359
+ * no version it was tried with, a protocol's say, which is held to the protocol and to no
2360
+ * build. Said where something went wrong and never as a standing word of the account: a
2361
+ * product updates itself every few days and works on, and a line that always stands is read
2362
+ * by nobody.
2363
+ */
2364
+ const untried = (): string | undefined => {
2365
+ const version = lineOf(productVersion ?? probe.version ?? null);
2366
+ const { name, tried } = driver.product;
2367
+ const newest = tried.at(-1);
2368
+ return version === null || newest === undefined || tried.includes(version)
2369
+ ? undefined
2370
+ : `${name} ${version} is a version the driver was not tried with (the newest it was tried with is ${newest})`;
2371
+ };
2372
+
2373
+ /**
2374
+ * The driver had no reading for something the product wrote (ADR-0182). In the runner's own
2375
+ * lines once for a kind in a session, whatever the version. On the record once for a kind,
2376
+ * whichever session first met it, and only under a version the driver was not tried with:
2377
+ * there it may be why the agent is not heard as it should be, and whoever reads the record
2378
+ * for that finds it where it happened.
2379
+ */
2380
+ const meetsUnknown = async (target: OpenSession, what: string): Promise<void> => {
2381
+ if (target.unknown.includes(what)) {
2382
+ return;
2383
+ }
2384
+ target.unknown.push(what);
2385
+ const version = untried();
2386
+ log(
2387
+ `the product wrote ${what}, which the driver does not read${version === undefined ? "" : `; ${version}`}`,
2388
+ );
2389
+ if (version === undefined || unknownSaid.has(what) || unknownSaid.size >= MAX_UNKNOWN_SAID) {
2390
+ return;
2391
+ }
2392
+ unknownSaid.add(what);
2393
+ record(
2394
+ target,
2395
+ "status",
2396
+ `The product wrote ${what}, which the driver does not read: something it says may be missed. ${version}. A newer console command may know it.`,
2397
+ { unknown: what },
2398
+ );
2399
+ await persist();
2400
+ };
2401
+
2402
+ /**
2403
+ * The product failed, or its session ended as none should: where the driver was not tried
2404
+ * with the product's version, the record says that the version may be why (ADR-0182), once
2405
+ * in a session, with what the driver had no reading for in it.
2406
+ */
2407
+ const versionMayBeWhy = (target: OpenSession): void => {
2408
+ const version = untried();
2409
+ if (version === undefined || target.versionSaid) {
2410
+ return;
2411
+ }
2412
+ target.versionSaid = true;
2413
+ const met =
2414
+ target.unknown.length === 0
2415
+ ? ""
2416
+ : ` In this session it wrote what the driver does not read: ${target.unknown.slice(0, MAX_UNKNOWN_NAMED).join("; ")}${target.unknown.length > MAX_UNKNOWN_NAMED ? ", and more" : ""}.`;
2417
+ record(
2418
+ target,
2419
+ "status",
2420
+ `The product's version may be why: ${version}.${met} A newer console command may know it.`,
2421
+ { untried: true },
2422
+ );
2423
+ log(`the product's version may be why the agent could not go on: ${version}`);
2424
+ };
2425
+
2045
2426
  const onDriver = async (target: OpenSession, event: DriverEvent, unread = 0): Promise<void> => {
2046
2427
  if (target !== session) {
2047
2428
  return;
@@ -2058,7 +2439,7 @@ export function createRunner(options: RunnerOptions): Runner {
2058
2439
  return;
2059
2440
  }
2060
2441
  case "conversation": {
2061
- conversation = event.id;
2442
+ converses(target, event);
2062
2443
  await persist();
2063
2444
  return;
2064
2445
  }
@@ -2078,6 +2459,8 @@ export function createRunner(options: RunnerOptions): Runner {
2078
2459
  }
2079
2460
  target.sent = [];
2080
2461
  target.returning = false;
2462
+ // The agent is done with all it was handed: nothing of it is owed a second saying.
2463
+ above.clear();
2081
2464
  if (limited !== undefined) {
2082
2465
  // A turn completed: whatever stopped the agent is over.
2083
2466
  limited = undefined;
@@ -2095,11 +2478,20 @@ export function createRunner(options: RunnerOptions): Runner {
2095
2478
  }
2096
2479
  return;
2097
2480
  }
2481
+ case "compacted": {
2482
+ summarized(target);
2483
+ await persist();
2484
+ return;
2485
+ }
2098
2486
  case "usage": {
2099
2487
  noteUsage(usageSeen, event.windows, options.now());
2100
2488
  await onUsage(target, event.windows);
2101
2489
  return;
2102
2490
  }
2491
+ case "unknown": {
2492
+ await meetsUnknown(target, event.what);
2493
+ return;
2494
+ }
2103
2495
  case "cannot_go_on": {
2104
2496
  // Reported, and nothing restarted: a supervisor's signal is tried at once, the rest after the cool-down.
2105
2497
  target.turn = false;
@@ -2155,6 +2547,10 @@ export function createRunner(options: RunnerOptions): Runner {
2155
2547
  target.sent = [];
2156
2548
  waits = cooled(waits);
2157
2549
  setState(event.state, event.reason);
2550
+ // The product failed, and no sign-in or model's refusal is why: its version may be.
2551
+ if (event.reason === "crashed" || event.reason === "start") {
2552
+ versionMayBeWhy(target);
2553
+ }
2158
2554
  if (pending.length > 0) {
2159
2555
  scheduleDeliver(waits.until);
2160
2556
  }
@@ -2175,6 +2571,15 @@ export function createRunner(options: RunnerOptions): Runner {
2175
2571
  ...pending,
2176
2572
  ].sort((one, other) => one.number - other.number);
2177
2573
  }
2574
+ // An end as none should be, which nothing the product said explains: not after a
2575
+ // usage limit, a sign-in that ran out or a model's refusal, which say why themselves.
2576
+ if (
2577
+ event.how === "crashed" &&
2578
+ limited === undefined &&
2579
+ !["usage_limit", "sign_in", "model_refused"].includes(stands.reason)
2580
+ ) {
2581
+ versionMayBeWhy(target);
2582
+ }
2178
2583
  await end(event.how, true);
2179
2584
  // A product that ended before it answered the word that it is back, or refused it, was
2180
2585
  // not brought back: the word is owed still, at the moment the runner waits for, or
@@ -2216,6 +2621,40 @@ export function createRunner(options: RunnerOptions): Runner {
2216
2621
  resume: driver.can.resume,
2217
2622
  });
2218
2623
 
2624
+ /**
2625
+ * What the agent is told of a newer command (ADR-0182), or nothing: the console runs a
2626
+ * version newer than the one the runner runs from, within what it takes, and the agent was
2627
+ * not told of that version or of a newer one yet: a console that goes back, or two of one
2628
+ * deployment that answer in turn, tell nothing again. Said with the next message the agent is handed, ahead of
2629
+ * it and under the runner's own header: a turn begins with that message anyway, so none is
2630
+ * begun for an update, and an agent with nothing to do is told when it next has. Nothing
2631
+ * while the package on the machine is another version already, which the runner ends for by
2632
+ * itself. Nothing installs anything: the agent does, and the runner sees it (ADR-0101).
2633
+ */
2634
+ const newerCommand = (): { readonly version: string; readonly words: string } | undefined => {
2635
+ const seen = options.consoleVersion?.();
2636
+ const theirs = parseVersion(seen);
2637
+ const own = parseVersion(options.version);
2638
+ if (seen === undefined || theirs === undefined || own === undefined) {
2639
+ return undefined;
2640
+ }
2641
+ const told = parseVersion(toldNewer);
2642
+ if (told !== undefined && compareVersions(theirs, told) <= 0) {
2643
+ return undefined;
2644
+ }
2645
+ if (updatedTo !== undefined || seenVersion !== undefined) {
2646
+ return undefined;
2647
+ }
2648
+ const install = options.updateCommand?.();
2649
+ if (install === undefined || judgeClient(own, theirs).verdict !== "behind") {
2650
+ return undefined;
2651
+ }
2652
+ return {
2653
+ version: seen,
2654
+ words: `A newer command exists: the console runs ${seen}, and this runner runs from ${options.version}. Nothing is refused meanwhile. Update it once what you are at allows: ${install}\n${UPDATE_FOLLOWS}\n${UPDATE_OTHERWISE}`,
2655
+ };
2656
+ };
2657
+
2219
2658
  /**
2220
2659
  * Says something of the runner's own to the agent, in the open session, under a header that
2221
2660
  * carries the session's mark: read after the tool call under way. The agent has a turn's work
@@ -2267,22 +2706,166 @@ export function createRunner(options: RunnerOptions): Runner {
2267
2706
  }
2268
2707
  };
2269
2708
 
2709
+ /**
2710
+ * Whether the agent is thinking over what a wake source raised (ADR-0170): the turn a wake
2711
+ * of that source's was handed over in is under way, or one is on its way to the agent, a
2712
+ * message the source took and reads for, a wake raised and not back on the stream yet, or
2713
+ * one waiting to be handed over. What waits is nothing the agent thinks over while nothing
2714
+ * is handed over at all, a supervisor's stop standing or the agent unable to go on.
2715
+ */
2716
+ const thinksOf = (name: string, now: number): boolean => {
2717
+ const own = (signal: RestSignal): boolean =>
2718
+ signal.kind === "wake" &&
2719
+ signal.source === "messenger" &&
2720
+ signal.key?.startsWith(`${name}:`) === true;
2721
+ if (session?.turn === true && session.sent.some(own)) {
2722
+ return true;
2723
+ }
2724
+ if (held || waits !== undefined) {
2725
+ return false;
2726
+ }
2727
+ const raised = raising.get(name);
2728
+ for (const one of raised ?? []) {
2729
+ // Back on the stream, and among what waits from then on; or never to come, for all the
2730
+ // runner waits for it.
2731
+ if ((one.number !== undefined && one.number <= handled) || now - one.at >= RAISED_MS) {
2732
+ raised?.delete(one);
2733
+ }
2734
+ }
2735
+ return (raised?.size ?? 0) > 0 || pending.some(own);
2736
+ };
2737
+
2738
+ /**
2739
+ * How the agent stands to whoever waits for it at a wake source (ADR-0170). Its usage limit
2740
+ * first, until the moment the product named where it named one: nothing is answered before
2741
+ * it, whatever else goes on, and past it the runner brings the agent back in a turn as any
2742
+ * other. Then that it has been at work for longer than `runner.json` gives a turn, whatever
2743
+ * woke it; then that it thinks over what this source raised.
2744
+ */
2745
+ const statusFor = (name: string, now: number): AgentStatus => {
2746
+ if (limited !== undefined && (limited.until === undefined || now < limited.until)) {
2747
+ return limited.until === undefined
2748
+ ? { state: "limited" }
2749
+ : { state: "limited", until: limited.until };
2750
+ }
2751
+ if (
2752
+ atWorkSince !== undefined &&
2753
+ shownAs !== false &&
2754
+ now - atWorkSince >= (shownAs.after ?? STATUS_AFTER) * 1000
2755
+ ) {
2756
+ return { state: "working" };
2757
+ }
2758
+ return { state: thinksOf(name, now) ? "thinking" : "idle" };
2759
+ };
2760
+
2761
+ /**
2762
+ * A moment as a status says one: the hour and the minute in the workspace's zone, and with
2763
+ * its day where it is most of a day away, as the command writes a moment's.
2764
+ */
2765
+ const statusTime = (at: number): string => {
2766
+ const iso = new Date(at).toISOString();
2767
+ const written = moment(iso);
2768
+ const day = /^\d{4}-(\d{2}-\d{2})T(\d{2}:\d{2})/.exec(written);
2769
+ return at - options.now() < FAR_MS || day === null
2770
+ ? options.time(iso)
2771
+ : `${day[1]} ${day[2]}${written.endsWith("Z") ? " UTC" : ""}`;
2772
+ };
2773
+
2774
+ /** Says to a wake source how the agent stands. What the source cannot show costs the agent nothing. */
2775
+ const say = (
2776
+ one: { readonly source: WakeSource; readonly host: WakeHost },
2777
+ status: AgentStatus,
2778
+ signal: AbortSignal,
2779
+ ): Promise<void> =>
2780
+ Promise.resolve()
2781
+ .then(() =>
2782
+ one.source.show?.(one.host, status, {
2783
+ words: shownAs === false ? {} : shownAs,
2784
+ language: options.language,
2785
+ now: options.now,
2786
+ time: statusTime,
2787
+ signal,
2788
+ }),
2789
+ )
2790
+ .catch((error: unknown) => {
2791
+ sayOnce(
2792
+ `status ${one.source.name}`,
2793
+ `the wake source ${one.source.name} could not show how the agent stands (${codeOf(error)})`,
2794
+ );
2795
+ });
2796
+
2797
+ /**
2798
+ * Has the wake sources show how the agent stands (ADR-0170): each is said when that changed,
2799
+ * and again every heartbeat while it stands, so that a source keeps what it shows from
2800
+ * lapsing. Nothing is said where `runner.json` turned it off.
2801
+ */
2802
+ const showStatus = (): void => {
2803
+ if (shownAs === false) {
2804
+ return;
2805
+ }
2806
+ const now = options.now();
2807
+ const active = session !== undefined && (session.turn || goesOn(session));
2808
+ atWorkSince = active ? (atWorkSince ?? now) : undefined;
2809
+ for (const [name, one] of shows) {
2810
+ const status = statusFor(name, now);
2811
+ const word = `${status.state} ${status.state === "limited" ? (status.until ?? "") : ""}`;
2812
+ if (word === one.said && now - one.at < timing.heartbeatMs) {
2813
+ continue;
2814
+ }
2815
+ one.said = word;
2816
+ one.at = now;
2817
+ void say(one, status, abort.signal);
2818
+ }
2819
+ };
2820
+
2821
+ /**
2822
+ * The last thing the sources are said: nothing to show, as the runner ends, so that what they
2823
+ * showed is taken back. Waited for a few seconds and no longer: a messenger that does not
2824
+ * answer holds no runner's end, and what stays there ends by itself.
2825
+ */
2826
+ const showLast = async (): Promise<void> => {
2827
+ if (shownAs === false || shows.size === 0) {
2828
+ return;
2829
+ }
2830
+ const ending = new AbortController();
2831
+ const over = new Promise<void>((resolve) => {
2832
+ ending.signal.addEventListener("abort", () => resolve(), { once: true });
2833
+ });
2834
+ const timer = setTimeout(() => ending.abort(), SHOWN_END_MS);
2835
+ await Promise.race([
2836
+ Promise.all([...shows.values()].map((one) => say(one, LAST_STATUS, ending.signal))),
2837
+ over,
2838
+ ]);
2839
+ clearTimeout(timer);
2840
+ ending.abort();
2841
+ };
2842
+
2270
2843
  /**
2271
2844
  * Why the agent stands as it does, and until when where the runner knows: when its usage
2272
2845
  * limit resets. A console of before the word `usage_limit` refuses a heartbeat that says it,
2273
2846
  * and with it everything else the heartbeat carries: such a console is said `crashed`, the
2274
- * nearest word it takes, and no moment.
2847
+ * nearest word it takes, and no moment. With it, when the runner said so, which a console of
2848
+ * before reads past.
2275
2849
  */
2276
- const saidOfState = (): { reason: AgentStateReason; until?: string | null } => {
2850
+ const saidOfState = (): {
2851
+ reason: AgentStateReason;
2852
+ until?: string | null;
2853
+ reasonAt: string;
2854
+ } => {
2855
+ // A moment kept by a runner before this one is taken as it reads; one that is no moment is now.
2856
+ const at =
2857
+ Number.isFinite(stands.at) && Math.abs(stands.at) <= 8.64e15 ? stands.at : options.now();
2858
+ const reasonAt = new Date(at).toISOString();
2277
2859
  if (stands.reason !== "usage_limit") {
2278
- return { reason: stands.reason };
2860
+ return { reason: stands.reason, reasonAt };
2279
2861
  }
2280
2862
  if (plainWords) {
2281
- return { reason: "crashed" };
2863
+ return { reason: "crashed", reasonAt };
2282
2864
  }
2283
2865
  return {
2284
2866
  reason: stands.reason,
2285
2867
  until: limited?.until === undefined ? null : new Date(limited.until).toISOString(),
2868
+ reasonAt,
2286
2869
  };
2287
2870
  };
2288
2871
 
@@ -2337,7 +2920,7 @@ export function createRunner(options: RunnerOptions): Runner {
2337
2920
  if (answer.state === "stopped" && !held && answer.latest <= handled) {
2338
2921
  held = true;
2339
2922
  await end("stopped");
2340
- stands = { state: "stopped", reason: "stop" };
2923
+ stands = { state: "stopped", reason: "stop", at: options.now() };
2341
2924
  }
2342
2925
  } catch (error) {
2343
2926
  if (stopped) {
@@ -2361,15 +2944,12 @@ export function createRunner(options: RunnerOptions): Runner {
2361
2944
  // runner then sees and starts again for; and whom to tell where it cannot.
2362
2945
  if (session !== undefined && !session.first) {
2363
2946
  const target = session;
2364
- const update =
2365
- install === undefined
2366
- ? ""
2367
- : ` Update it: ${install}\nThe runner sees the update and is started again with it by itself, once no turn is under way.`;
2947
+ const update = install === undefined ? "" : ` Update it: ${install}\n${UPDATE_FOLLOWS}`;
2368
2948
  // Not told where it could not be: the runner's own line above says it to whoever
2369
2949
  // reads its output.
2370
2950
  await sayToAgent(
2371
2951
  target,
2372
- `The console no longer takes this version of the command the runner runs from, and hears nothing of this session until it is updated.${update}\nWhere you cannot update it, say so to whom you report to.`,
2952
+ `The console no longer takes this version of the command the runner runs from, and hears nothing of this session until it is updated.${update}\n${UPDATE_OTHERWISE}`,
2373
2953
  );
2374
2954
  }
2375
2955
  }
@@ -2503,6 +3083,19 @@ export function createRunner(options: RunnerOptions): Runner {
2503
3083
  if (source.run === undefined || wakes === undefined) {
2504
3084
  return;
2505
3085
  }
3086
+ // What the source kept under a runner before this one, where the machine keeps it
3087
+ // (ADR-0166): the ids and the moments of what it brought, so that a runner started again
3088
+ // says nothing a second time. What does not read is nothing kept.
3089
+ const kept = new Kept();
3090
+ try {
3091
+ for (const [name, value] of (await wakes.kept?.load(source.name)) ?? []) {
3092
+ kept.set(name, value);
3093
+ }
3094
+ } catch {
3095
+ log(`what the wake source ${source.name} kept could not be read; it begins with nothing`);
3096
+ }
3097
+ kept.changed = false;
3098
+ keeps.set(source.name, kept);
2506
3099
  const host: WakeHost = {
2507
3100
  secret: async (name) => secretValues.get(name),
2508
3101
  channels: options.config.channels ?? [],
@@ -2517,9 +3110,22 @@ export function createRunner(options: RunnerOptions): Runner {
2517
3110
  // what someone else wrote, and a text with no line of the source's as words whole, so
2518
3111
  // that no word of a message reads as a line of the source's when the signal returns.
2519
3112
  const text = wakeTextOf("parts" in raised ? raised.parts : [{ words: raised.text }]);
3113
+ // On its way to the agent from here, which the source shows (ADR-0170): until the
3114
+ // stream brings it back, or the console did not take it.
3115
+ const underWay: { readonly at: number; number: number | undefined } = {
3116
+ at: options.now(),
3117
+ number: undefined,
3118
+ };
3119
+ const others = raising.get(source.name) ?? new Set();
3120
+ raising.set(source.name, others.add(underWay));
3121
+ push({ t: "shown" });
2520
3122
  try {
2521
- await client.raiseWake({ source: "messenger", key: raised.key, text });
3123
+ underWay.number = (
3124
+ await client.raiseWake({ source: "messenger", key: raised.key, text })
3125
+ ).number;
2522
3126
  } catch (error) {
3127
+ others.delete(underWay);
3128
+ push({ t: "shown" });
2523
3129
  if (codeOf(error) !== "signal.too_many") {
2524
3130
  throw error;
2525
3131
  }
@@ -2537,7 +3143,29 @@ export function createRunner(options: RunnerOptions): Runner {
2537
3143
  }
2538
3144
  return true;
2539
3145
  },
2540
- kept: new Map(),
3146
+ // Taken by the source, and on its way to the agent before any wake of it went up
3147
+ // (ADR-0170): shown from here, until the source says the wake was raised or will not be.
3148
+ heard: () => {
3149
+ // A message whose wake the console takes none of for a while is let go when it is
3150
+ // raised (ADR-0132): it reaches no agent, and nothing shows the agent thinking it over.
3151
+ if (wakesHeld !== undefined && options.now() < wakesHeld.until) {
3152
+ return () => undefined;
3153
+ }
3154
+ const taken: { readonly at: number; number: number | undefined } = {
3155
+ at: options.now(),
3156
+ number: undefined,
3157
+ };
3158
+ const others = raising.get(source.name) ?? new Set();
3159
+ raising.set(source.name, others.add(taken));
3160
+ push({ t: "shown" });
3161
+ return () => {
3162
+ if (others.delete(taken)) {
3163
+ push({ t: "shown" });
3164
+ }
3165
+ };
3166
+ },
3167
+ kept,
3168
+ began: beganBy,
2541
3169
  log,
2542
3170
  stands: (state, why) => sourceStands(source.name, state, why),
2543
3171
  fetch: wakes.fetch,
@@ -2555,9 +3183,12 @@ export function createRunner(options: RunnerOptions): Runner {
2555
3183
  }),
2556
3184
  };
2557
3185
  sourceStands(source.name, "waiting");
3186
+ if (source.show !== undefined) {
3187
+ shows.set(source.name, { source, host, said: "", at: 0 });
3188
+ }
2558
3189
  let wait = timing.reconnectMs;
2559
3190
  while (!stopped) {
2560
- const began = options.now();
3191
+ const startedAt = options.now();
2561
3192
  try {
2562
3193
  await source.run(host, abort.signal);
2563
3194
  // Ended with no error, a connection the other side closed say: it listens no more
@@ -2572,7 +3203,7 @@ export function createRunner(options: RunnerOptions): Runner {
2572
3203
  }
2573
3204
  }
2574
3205
  wait =
2575
- options.now() - began >= SOURCE_HELD_MS
3206
+ options.now() - startedAt >= SOURCE_HELD_MS
2576
3207
  ? timing.reconnectMs
2577
3208
  : Math.min(wait * 2, MAX_SOURCE_PAUSE_MS);
2578
3209
  await pause(wait);
@@ -2620,10 +3251,13 @@ export function createRunner(options: RunnerOptions): Runner {
2620
3251
  const kept = await options.state.load().catch(() => undefined);
2621
3252
  delivered = kept?.delivered ?? 0;
2622
3253
  conversation = kept?.conversation;
3254
+ // A moment after now is none the conversation began at: held to the clock here, and
3255
+ // wherever a wake source asks.
3256
+ began = Math.min(kept?.began ?? 0, options.now());
2623
3257
  if (conversation !== undefined && kept?.driver !== undefined && kept.driver !== driver.name) {
2624
3258
  // Another product's conversation is nothing this one can go on with: asked to, it fails
2625
3259
  // at every turn until a supervisor resets it.
2626
- conversation = undefined;
3260
+ letGo();
2627
3261
  log(
2628
3262
  `the conversation kept was begun under the driver ${kept.driver}: a new one begins under ${driver.name}`,
2629
3263
  );
@@ -2635,6 +3269,10 @@ export function createRunner(options: RunnerOptions): Runner {
2635
3269
  for (const number of kept?.kept ?? []) {
2636
3270
  reread.add(number);
2637
3271
  }
3272
+ for (const what of (kept?.unknown ?? []).slice(-MAX_UNKNOWN_SAID)) {
3273
+ unknownSaid.add(what);
3274
+ }
3275
+ toldNewer = kept?.newer;
2638
3276
  if (kept?.limited !== undefined) {
2639
3277
  // A usage limit stopped the agent under a runner before this one: it is brought back
2640
3278
  // all the same, when the limit resets or after the cool-down where no moment is known.
@@ -2648,11 +3286,16 @@ export function createRunner(options: RunnerOptions): Runner {
2648
3286
  tries: 1,
2649
3287
  until: known === undefined ? now + timing.coolDownMs : known + timing.resetMarginMs,
2650
3288
  };
2651
- stands = { state: "failed", reason: "usage_limit" };
3289
+ // Said as of when the limit was reached, which the runner before this one kept.
3290
+ stands = { state: "failed", reason: "usage_limit", at: kept.limited.since };
2652
3291
  log(
2653
3292
  `the product's usage limit stopped the agent before this runner started: tried again at ${new Date(waits.until).toISOString()}`,
2654
3293
  );
2655
3294
  }
3295
+ if ((kept?.began ?? 0) > began) {
3296
+ // Kept as it is taken, once everything else the file said is in place again.
3297
+ await persist();
3298
+ }
2656
3299
  probe = await driver.probe();
2657
3300
  const left =
2658
3301
  probe.models.length +
@@ -2683,7 +3326,7 @@ export function createRunner(options: RunnerOptions): Runner {
2683
3326
  delivered = Math.max(delivered, first.delivered);
2684
3327
  if (first.state === "stopped") {
2685
3328
  held = true;
2686
- stands = { state: "stopped", reason: "stop" };
3329
+ stands = { state: "stopped", reason: "stop", at: options.now() };
2687
3330
  }
2688
3331
  break;
2689
3332
  } catch (error) {
@@ -2765,6 +3408,9 @@ export function createRunner(options: RunnerOptions): Runner {
2765
3408
  if (session !== undefined) {
2766
3409
  await flush(session);
2767
3410
  }
3411
+ await keepKept();
3412
+ } else if (message.t === "shown") {
3413
+ // Looked at below, as after anything else.
2768
3414
  } else {
2769
3415
  // A timer counts no time the machine sleeps in, and rings late by as much: what
2770
3416
  // was to be handed over at a moment is looked for by the clock at every heartbeat.
@@ -2821,6 +3467,9 @@ export function createRunner(options: RunnerOptions): Runner {
2821
3467
  } catch (error) {
2822
3468
  log(`the runner could not do what it was about to (${codeOf(error)}); it goes on`);
2823
3469
  }
3470
+ // Whatever was done may have changed how the agent stands to whoever waits for it,
3471
+ // and so does the time that passed: looked at once a second at the least.
3472
+ showStatus();
2824
3473
  }
2825
3474
  } finally {
2826
3475
  stopped = true;
@@ -2829,12 +3478,18 @@ export function createRunner(options: RunnerOptions): Runner {
2829
3478
  for (const timer of timers) {
2830
3479
  clearTimeout(timer);
2831
3480
  }
3481
+ // What the wake sources showed of the agent is taken back while its session ends
3482
+ // (ADR-0170): a runner that is gone shows nothing.
3483
+ const shown = showLast();
2832
3484
  // A session ended for an update goes on, in the runner started after, as one a
2833
3485
  // supervisor restarted does: with the same conversation.
2834
3486
  await end(how === "stopped" ? "exited" : "restarted");
3487
+ await shown;
2835
3488
  abort.abort();
2836
3489
  await listening;
2837
3490
  await Promise.all(sourcing);
3491
+ // What the sources raised as they ended is kept too.
3492
+ await keepKept();
2838
3493
  log(
2839
3494
  how === "updated"
2840
3495
  ? "the runner ended, for the update"