@skillcdn/console 0.6.2 → 0.6.3

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 (179) hide show
  1. package/README.md +13 -6
  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 +40 -3
  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 +4 -3
  12. package/dist/api.d.ts.map +1 -1
  13. package/dist/api.js +4 -3
  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 +304 -130
  18. package/dist/cli/cli.js.map +1 -1
  19. package/dist/cli/format.d.ts +13 -4
  20. package/dist/cli/format.d.ts.map +1 -1
  21. package/dist/cli/format.js +43 -9
  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 +780 -318
  26. package/dist/cli/help.js.map +1 -1
  27. package/dist/cli/pack.d.ts +2 -2
  28. package/dist/cli/pack.d.ts.map +1 -1
  29. package/dist/cli/pack.js +6 -4
  30. package/dist/cli/pack.js.map +1 -1
  31. package/dist/cli/runner-files.d.ts +16 -0
  32. package/dist/cli/runner-files.d.ts.map +1 -0
  33. package/dist/cli/runner-files.js +110 -0
  34. package/dist/cli/runner-files.js.map +1 -0
  35. package/dist/cli/runner-host.d.ts +7 -0
  36. package/dist/cli/runner-host.d.ts.map +1 -1
  37. package/dist/cli/runner-host.js +40 -2
  38. package/dist/cli/runner-host.js.map +1 -1
  39. package/dist/client.d.ts +16 -1
  40. package/dist/client.d.ts.map +1 -1
  41. package/dist/client.js +6 -1
  42. package/dist/client.js.map +1 -1
  43. package/dist/components/agent-page.d.ts +28 -3
  44. package/dist/components/agent-page.d.ts.map +1 -1
  45. package/dist/components/agent-page.js +48 -3
  46. package/dist/components/agent-page.js.map +1 -1
  47. package/dist/components/agents.d.ts +3 -2
  48. package/dist/components/agents.d.ts.map +1 -1
  49. package/dist/components/agents.js +9 -3
  50. package/dist/components/agents.js.map +1 -1
  51. package/dist/console.d.ts.map +1 -1
  52. package/dist/console.js +1 -1
  53. package/dist/console.js.map +1 -1
  54. package/dist/feed-words.d.ts.map +1 -1
  55. package/dist/feed-words.js +12 -1
  56. package/dist/feed-words.js.map +1 -1
  57. package/dist/i18n/en.d.ts +45 -1
  58. package/dist/i18n/en.d.ts.map +1 -1
  59. package/dist/i18n/en.js +36 -1
  60. package/dist/i18n/en.js.map +1 -1
  61. package/dist/i18n/ko.d.ts.map +1 -1
  62. package/dist/i18n/ko.js +28 -0
  63. package/dist/i18n/ko.js.map +1 -1
  64. package/dist/limits.d.ts +10 -0
  65. package/dist/limits.d.ts.map +1 -1
  66. package/dist/limits.js +10 -0
  67. package/dist/limits.js.map +1 -1
  68. package/dist/plugin/context.d.ts +39 -7
  69. package/dist/plugin/context.d.ts.map +1 -1
  70. package/dist/runner/acp.d.ts.map +1 -1
  71. package/dist/runner/acp.js +9 -3
  72. package/dist/runner/acp.js.map +1 -1
  73. package/dist/runner/claude.d.ts.map +1 -1
  74. package/dist/runner/claude.js +5 -1
  75. package/dist/runner/claude.js.map +1 -1
  76. package/dist/runner/codex.d.ts.map +1 -1
  77. package/dist/runner/codex.js +14 -2
  78. package/dist/runner/codex.js.map +1 -1
  79. package/dist/runner/config.d.ts +18 -0
  80. package/dist/runner/config.d.ts.map +1 -1
  81. package/dist/runner/config.js +77 -1
  82. package/dist/runner/config.js.map +1 -1
  83. package/dist/runner/desk.d.ts +7 -0
  84. package/dist/runner/desk.d.ts.map +1 -1
  85. package/dist/runner/desk.js +9 -0
  86. package/dist/runner/desk.js.map +1 -1
  87. package/dist/runner/message.d.ts +16 -22
  88. package/dist/runner/message.d.ts.map +1 -1
  89. package/dist/runner/message.js +72 -120
  90. package/dist/runner/message.js.map +1 -1
  91. package/dist/runner/runner.d.ts +25 -1
  92. package/dist/runner/runner.d.ts.map +1 -1
  93. package/dist/runner/runner.js +404 -19
  94. package/dist/runner/runner.js.map +1 -1
  95. package/dist/runner/slack-status.d.ts +19 -0
  96. package/dist/runner/slack-status.d.ts.map +1 -0
  97. package/dist/runner/slack-status.js +332 -0
  98. package/dist/runner/slack-status.js.map +1 -0
  99. package/dist/runner/slack.d.ts.map +1 -1
  100. package/dist/runner/slack.js +245 -57
  101. package/dist/runner/slack.js.map +1 -1
  102. package/dist/runner/types.d.ts +109 -15
  103. package/dist/runner/types.d.ts.map +1 -1
  104. package/dist/runner/types.js.map +1 -1
  105. package/dist/runner/usage.d.ts +6 -1
  106. package/dist/runner/usage.d.ts.map +1 -1
  107. package/dist/runner/usage.js +9 -2
  108. package/dist/runner/usage.js.map +1 -1
  109. package/dist/runner.d.ts +2 -2
  110. package/dist/runner.d.ts.map +1 -1
  111. package/dist/runner.js.map +1 -1
  112. package/dist/schemas.d.ts +160 -1
  113. package/dist/schemas.d.ts.map +1 -1
  114. package/dist/schemas.js +82 -5
  115. package/dist/schemas.js.map +1 -1
  116. package/dist/vocabulary.d.ts +7 -0
  117. package/dist/vocabulary.d.ts.map +1 -1
  118. package/dist/vocabulary.js +6 -0
  119. package/dist/vocabulary.js.map +1 -1
  120. package/dist/wake-text.d.ts +43 -0
  121. package/dist/wake-text.d.ts.map +1 -0
  122. package/dist/wake-text.js +115 -0
  123. package/dist/wake-text.js.map +1 -0
  124. package/docs/specs/cli.md +19 -11
  125. package/docs/specs/plugins.md +5 -5
  126. package/docs/specs/rest.md +10 -3
  127. package/docs/specs/runner.md +96 -20
  128. package/docs/start.md +26 -5
  129. package/package.json +3 -3
  130. package/skills/building-a-plugin/SKILL.md +2 -2
  131. package/skills/deploying-a-console/SKILL.md +3 -3
  132. package/skills/deploying-a-console/references/access.md +15 -1
  133. package/skills/deploying-a-console/references/resources.md +2 -2
  134. package/skills/deploying-a-console/scripts/github-app.mjs +593 -0
  135. package/skills/running-an-agent/SKILL.md +13 -10
  136. package/skills/running-an-agent/references/preparing-the-machine.md +1 -1
  137. package/skills/running-an-agent/references/slack-app.md +25 -8
  138. package/skills/running-an-agent/references/the-account-and-the-invitation.md +1 -1
  139. package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +1 -1
  140. package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +1 -1
  141. package/skills/running-an-agent/references/your-first-task.md +7 -2
  142. package/skills/trying-a-console/SKILL.md +136 -0
  143. package/skills/trying-a-console/references/when-it-does-not-go.md +30 -0
  144. package/skills/working-the-board/SKILL.md +3 -3
  145. package/skills/working-the-board/references/the-pack.md +3 -3
  146. package/skills/working-the-board/references/what-they-were-told.md +1 -1
  147. package/src/agent-page.tsx +92 -1
  148. package/src/agent-state.ts +33 -0
  149. package/src/api.ts +16 -0
  150. package/src/cli/cli.ts +359 -137
  151. package/src/cli/format.ts +49 -9
  152. package/src/cli/help.ts +904 -359
  153. package/src/cli/pack.ts +6 -4
  154. package/src/cli/runner-files.ts +120 -0
  155. package/src/cli/runner-host.ts +48 -2
  156. package/src/client.ts +53 -0
  157. package/src/components/agent-page.tsx +150 -3
  158. package/src/components/agents.tsx +20 -5
  159. package/src/console.tsx +1 -0
  160. package/src/feed-words.ts +14 -1
  161. package/src/i18n/en.ts +39 -1
  162. package/src/i18n/ko.ts +32 -0
  163. package/src/limits.ts +10 -0
  164. package/src/plugin/context.ts +40 -7
  165. package/src/runner/acp.ts +9 -3
  166. package/src/runner/claude.ts +5 -1
  167. package/src/runner/codex.ts +14 -2
  168. package/src/runner/config.ts +114 -1
  169. package/src/runner/desk.ts +10 -0
  170. package/src/runner/message.ts +88 -126
  171. package/src/runner/runner.ts +487 -22
  172. package/src/runner/slack-status.ts +423 -0
  173. package/src/runner/slack.ts +293 -61
  174. package/src/runner/types.ts +108 -13
  175. package/src/runner/usage.ts +16 -3
  176. package/src/runner.ts +4 -0
  177. package/src/schemas.ts +97 -3
  178. package/src/vocabulary.ts +9 -0
  179. package/src/wake-text.ts +133 -0
@@ -2,6 +2,7 @@ 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 { STATUS_AFTER } from "./config.js";
5
6
  import { composeSignals, deskName, GROUP_WINDOW_MS, introOf, makeMark, sleptOf, wakeTextOf, } from "./message.js";
6
7
  import { cutShown, cutShownEnd, redactorOf } from "./redact.js";
7
8
  import { limitWords, noteUsage, RESET_MARGIN_MS, runnerSays, usageSaid, usageToTell, usageWords, } from "./usage.js";
@@ -18,6 +19,14 @@ import { limitWords, noteUsage, RESET_MARGIN_MS, runnerSays, usageSaid, usageToT
18
19
  // a signal received, a word of the driver's, a timer that fired. What the product prints is
19
20
  // numbered and kept for the next batch as it arrives, so that nothing of a session is left
20
21
  // behind when the session ends.
22
+ /** How long a wake a source raised is waited for on the stream before the source is shown nothing of it. */
23
+ const RAISED_MS = 60_000;
24
+ /** From how far away a moment is said with its day in a status. */
25
+ const FAR_MS = 20 * 60 * 60_000;
26
+ /** How long the runner's end waits for the wake sources to take back what they showed. */
27
+ const SHOWN_END_MS = 3000;
28
+ /** What the wake sources are said as the runner ends. */
29
+ const LAST_STATUS = { state: "idle", last: true };
21
30
  /** How often the runner says it is there. */
22
31
  export const HEARTBEAT_MS = 30_000;
23
32
  /** How often what the product did is sent, while there is something to send. */
@@ -60,6 +69,12 @@ const SOURCE_HELD_MS = 60_000;
60
69
  const SLEPT_MS = 60_000;
61
70
  /** The kinds of event that are the agent's own answer: what it said, thought or called. */
62
71
  const ANSWERS = new Set(["assistant", "thinking", "tool_call"]);
72
+ /**
73
+ * How long before its wake is on the record a message may have been written (ADR-0166): what a
74
+ * wake source reads for a wake, and the wakes ahead of it, are waited for half a minute at the
75
+ * most, and the messenger's clock and the console's differ by less than the rest.
76
+ */
77
+ const RAISED_AFTER_MS = 60_000;
63
78
  /** How many signals the runner writes down as kept for the agent beside what the record sends again. */
64
79
  const MAX_KEPT = 50;
65
80
  /** How long before its moment a timer may ring: a delivery asked for at a moment is taken within it. */
@@ -164,6 +179,19 @@ const lineOf = (value) => {
164
179
  const line = (value ?? "").trim().slice(0, MAX_RUNNER_LINE_LENGTH).trim();
165
180
  return line.length === 0 || hasForbiddenCodePoint(line) ? null : line;
166
181
  };
182
+ /** What a wake source keeps, which says whether it changed since it was last written down. */
183
+ class Kept extends Map {
184
+ changed = false;
185
+ set(name, value) {
186
+ this.changed = true;
187
+ return super.set(name, value);
188
+ }
189
+ delete(name) {
190
+ const had = super.delete(name);
191
+ this.changed ||= had;
192
+ return had;
193
+ }
194
+ }
167
195
  export function createRunner(options) {
168
196
  const { client, driver, config, log } = options;
169
197
  const timing = {
@@ -238,9 +266,34 @@ export function createRunner(options) {
238
266
  let received = 0;
239
267
  let handled = 0;
240
268
  let conversation;
269
+ /**
270
+ * When the conversation the agent goes on with began (ADR-0166), 0 where nothing says, and
271
+ * whether that moment is of a conversation no session has begun yet: the session that begins
272
+ * it then leaves the moment as it stands.
273
+ */
274
+ let began = 0;
275
+ let renewed = false;
276
+ /**
277
+ * The signals the conversation holds already, by number (ADR-0166): the product of the open
278
+ * session took them there as a usage limit was reached, its driver vouching that what it
279
+ * took stands (`can.holdsTaken`), and the agent never answered. Handed over again in that
280
+ * session, they are named and not said a second time. Until a turn completes, the
281
+ * conversation is let go or put in fewer words, or the session ends: nothing says before a
282
+ * session's first message whether its product went on with the conversation, so a session
283
+ * begun after is said them whole.
284
+ */
285
+ const above = new Set();
286
+ /** What each wake source keeps, by its name, and whether it changed since it was last written. */
287
+ const keeps = new Map();
288
+ /**
289
+ * How the agent stands and why, with when the runner said so (`at`): the console is told that
290
+ * moment with the reason, since a reason is said again the same under a state that stays, a
291
+ * turn that completed after another say, and nothing else tells the two apart.
292
+ */
241
293
  let stands = {
242
294
  state: "running",
243
295
  reason: "started",
296
+ at: options.now(),
244
297
  };
245
298
  let said = "";
246
299
  let settings = { model: null, effort: null };
@@ -316,6 +369,25 @@ export function createRunner(options) {
316
369
  let slept;
317
370
  /** How each wake source stands, by its name: what the heartbeat says of them. */
318
371
  const sources = new Map();
372
+ /**
373
+ * The wake sources that show how the agent stands (ADR-0170), by name: the host each runs
374
+ * with, and what it was last said and when.
375
+ */
376
+ const shows = new Map();
377
+ /**
378
+ * What `runner.json` says of how the agent stands is shown: `false` for not at all, and the
379
+ * sources' own way where it says nothing, a desk of before and a config put together by hand
380
+ * among them.
381
+ */
382
+ const shownAs = config.status ?? {};
383
+ /** Since when the agent is at work without a pause: a turn under way, or something that goes on in its session with none. */
384
+ let atWorkSince;
385
+ /**
386
+ * What each wake source raised that the stream has not brought back yet: when it was raised,
387
+ * and its number once the console took it. A message that reached the runner is on its way
388
+ * to the agent from then on, which its source shows.
389
+ */
390
+ const raising = new Map();
319
391
  /**
320
392
  * The signals a product was handed and never took, since it did not start or was not signed
321
393
  * in, which are kept to be sent again; and, of those, the ones a reset gave one more try: a
@@ -351,14 +423,103 @@ export function createRunner(options) {
351
423
  ? {}
352
424
  : { limited: { since: limited.since, until: limited.until } }),
353
425
  ...(kept.length === 0 ? {} : { kept }),
426
+ ...(began === 0 ? {} : { began }),
354
427
  });
355
428
  }
356
429
  catch {
357
430
  log("the runner's state could not be written; it goes on from the console's record");
358
431
  }
359
432
  };
433
+ /**
434
+ * When a conversation that begins now is taken to have begun (ADR-0166): now, or, where wakes
435
+ * of a wake source wait to be handed over, a little before the oldest of them was raised.
436
+ * They are handed over in the new conversation, which holds them from then on: counted as
437
+ * from before it, their messages would be carried once more with the next wake. `handed` is
438
+ * what the conversation holds beside what waits: what a session was handed in it already,
439
+ * where the moment is set once the session is under way.
440
+ */
441
+ const beginsAt = (handed = []) => {
442
+ let at = options.now();
443
+ for (const signal of [...pending, ...handed]) {
444
+ const raised = signal.source === "messenger" ? Date.parse(signal.at) : Number.NaN;
445
+ if (Number.isFinite(raised)) {
446
+ at = Math.min(at, raised - RAISED_AFTER_MS);
447
+ }
448
+ }
449
+ return at;
450
+ };
451
+ /**
452
+ * The driver named the conversation its session runs in. Where it began one in place of the
453
+ * one it was asked to go on with (`anew`), the agent's conversation began with the session
454
+ * (ADR-0166): it holds what the session was handed, and nothing from before.
455
+ */
456
+ const converses = (target, event) => {
457
+ conversation = event.id;
458
+ if (event.anew === true) {
459
+ began = beginsAt([...target.sent, ...target.handed.flatMap((message) => message.signals)]);
460
+ above.clear();
461
+ }
462
+ };
463
+ /**
464
+ * The product put the conversation in fewer words (ADR-0166): what the agent was handed
465
+ * before is a summary at best, so what a wake source brought before counts no more, and what
466
+ * the product took is not named as standing there. What it was handed and has not taken yet
467
+ * it reads after, whole: the conversation is taken to hold that.
468
+ */
469
+ const summarized = (target) => {
470
+ began = beginsAt(target.handed.flatMap((message) => message.signals));
471
+ above.clear();
472
+ for (const signal of target.taken.flat()) {
473
+ target.summarized.add(signal.number);
474
+ }
475
+ };
476
+ /**
477
+ * When the agent's conversation began, as a wake source is told it (ADR-0166): never after
478
+ * now. A moment ahead of the clock, kept by a runner whose machine's clock ran ahead or set
479
+ * back since, is none a message was written before: counted from, every wake of a place
480
+ * would carry again what the last one carried, for as long as the clock is behind it. It is
481
+ * taken as now, and kept so.
482
+ */
483
+ const beganBy = () => {
484
+ const now = options.now();
485
+ if (began > now) {
486
+ began = now;
487
+ void persist();
488
+ }
489
+ return began;
490
+ };
491
+ /**
492
+ * Lets go of the conversation kept: the next session begins a new one, which holds nothing
493
+ * the agent was handed before this moment (ADR-0166), but for what waits to be handed over.
494
+ */
495
+ const letGo = () => {
496
+ conversation = undefined;
497
+ began = beginsAt();
498
+ renewed = true;
499
+ above.clear();
500
+ };
501
+ /** Writes what the wake sources keep where it changed since it was last written. */
502
+ const keepKept = async () => {
503
+ const store = options.wakes?.kept;
504
+ if (store === undefined) {
505
+ return;
506
+ }
507
+ for (const [name, kept] of keeps) {
508
+ if (!kept.changed) {
509
+ continue;
510
+ }
511
+ kept.changed = false;
512
+ try {
513
+ await store.save(name, [...kept]);
514
+ }
515
+ catch {
516
+ // Kept in memory still, and written with the next change.
517
+ sayOnce(`kept ${name}`, `what the wake source ${name} keeps could not be written; it is kept while the runner runs`);
518
+ }
519
+ }
520
+ };
360
521
  const setState = (state, reason) => {
361
- stands = { state, reason };
522
+ stands = { state, reason, at: options.now() };
362
523
  push({ t: "heartbeat" });
363
524
  };
364
525
  /** Asks for a delivery after the window that groups what arrives together, or at a moment. */
@@ -774,6 +935,7 @@ export function createRunner(options) {
774
935
  took: false,
775
936
  taken: [],
776
937
  unread: 0,
938
+ summarized: new Set(),
777
939
  returning: false,
778
940
  background: 0,
779
941
  own: false,
@@ -789,6 +951,16 @@ export function createRunner(options) {
789
951
  started.pumped = pump(started);
790
952
  session = started;
791
953
  renew = false;
954
+ if (resume === undefined) {
955
+ // A conversation of its own: it holds nothing the agent was handed before (ADR-0166).
956
+ // Where it was let go a moment ago, at a reset say, it began then, and what was kept
957
+ // for it since is handed over in it.
958
+ if (!renewed) {
959
+ began = beginsAt();
960
+ }
961
+ above.clear();
962
+ }
963
+ renewed = false;
792
964
  log(`session ${started.id} began${resume === undefined ? "" : ", going on with the conversation before"}`);
793
965
  push({ t: "heartbeat" });
794
966
  return started;
@@ -805,6 +977,9 @@ export function createRunner(options) {
805
977
  }
806
978
  await target.pumped;
807
979
  await hearLast(target);
980
+ // What was named as standing in the conversation was taken by this session's product: a
981
+ // session begun after is said it whole (ADR-0166).
982
+ above.clear();
808
983
  // What the product was handed and never took is sent again, by the console where the
809
984
  // runner itself ends and by the runner where the product had not started; else it is on
810
985
  // the record as never read.
@@ -940,7 +1115,7 @@ export function createRunner(options) {
940
1115
  (session === undefined || !goesOn(session)) &&
941
1116
  limited === undefined) {
942
1117
  await end("restarted");
943
- conversation = undefined;
1118
+ letGo();
944
1119
  }
945
1120
  const target = session ?? (await begin());
946
1121
  if (target === undefined) {
@@ -973,6 +1148,11 @@ export function createRunner(options) {
973
1148
  // what the driver says of its product.
974
1149
  const takesPictures = target.driver.images?.() ?? driver.can.images;
975
1150
  for (const signal of batch) {
1151
+ // What is only named is handed over with nothing: its files stand in the desk and its
1152
+ // pictures in the conversation, with the message the product took (ADR-0166).
1153
+ if (above.has(signal.number)) {
1154
+ continue;
1155
+ }
976
1156
  const taken = new Set();
977
1157
  for (const file of signal.attachments) {
978
1158
  const picture = PICTURE_TYPES.has(file.contentType);
@@ -1020,10 +1200,15 @@ export function createRunner(options) {
1020
1200
  ]);
1021
1201
  }
1022
1202
  }
1203
+ // What the conversation holds already, the product having taken it there as the limit was
1204
+ // reached, is named and not said again (ADR-0166).
1205
+ const named = batch.filter((signal) => above.has(signal.number)).length;
1023
1206
  const composed = composeSignals(batch, {
1024
1207
  mark: target.mark,
1025
1208
  time: options.time,
1026
1209
  attached: (signal) => attached.get(signal.number) ?? [],
1210
+ own: options.account,
1211
+ above: (signal) => above.has(signal.number),
1027
1212
  });
1028
1213
  const intro = target.first ? introOf(target.mark, resetBy) : undefined;
1029
1214
  // What the runner says of its own goes ahead of the signals, under its own header: that
@@ -1037,7 +1222,13 @@ export function createRunner(options) {
1037
1222
  intro,
1038
1223
  returned === undefined
1039
1224
  ? undefined
1040
- : runnerSays(target.mark, limitWords(returned, { now: options.now(), moment, follow: batch.length, early })),
1225
+ : runnerSays(target.mark, limitWords(returned, {
1226
+ now: options.now(),
1227
+ moment,
1228
+ follow: batch.length - named,
1229
+ above: named,
1230
+ early,
1231
+ })),
1041
1232
  told === undefined ? undefined : sleptOf(told, { mark: target.mark, moment }),
1042
1233
  composed.text,
1043
1234
  ]
@@ -1156,7 +1347,9 @@ export function createRunner(options) {
1156
1347
  * Hears what the product said as its session was ended. Its words wait in the queue behind
1157
1348
  * whatever ended the session, and are let go there once the session is no longer the open
1158
1349
  * one: a message it said it took then is taken, and not sent again, and the conversation it
1159
- * named is the one to go on with, where that message is.
1350
+ * named is the one to go on with, where that message is. That it began that conversation
1351
+ * anew, or put it in fewer words, moves the moment the conversation began as it does in a
1352
+ * session that goes on (ADR-0166): the next session goes on with what was said here.
1160
1353
  */
1161
1354
  const hearLast = async (target) => {
1162
1355
  let named = false;
@@ -1166,7 +1359,11 @@ export function createRunner(options) {
1166
1359
  }
1167
1360
  const { event } = message;
1168
1361
  if (event.type === "conversation") {
1169
- conversation = event.id;
1362
+ converses(target, event);
1363
+ named = true;
1364
+ }
1365
+ else if (event.type === "compacted") {
1366
+ summarized(target);
1170
1367
  named = true;
1171
1368
  }
1172
1369
  else if (event.type === "taken") {
@@ -1357,7 +1554,7 @@ export function createRunner(options) {
1357
1554
  limited = undefined;
1358
1555
  await end(signal.kind === "reset" ? "reset" : "restarted");
1359
1556
  if (signal.kind === "reset") {
1360
- conversation = undefined;
1557
+ letGo();
1361
1558
  resetBy = { by, ...(signal.text.trim().length > 0 ? { number: signal.number } : {}) };
1362
1559
  // What a product was handed and never took is tried once more, in the new
1363
1560
  // conversation: a product that does not start with it again is not sent it a
@@ -1464,6 +1661,17 @@ export function createRunner(options) {
1464
1661
  for (const signal of untaken) {
1465
1662
  owed.delete(signal.number);
1466
1663
  }
1664
+ // What the product took stands in the conversation, which the agent goes on in: handed
1665
+ // over again in this session, it is named and not said a second time (ADR-0166). Only
1666
+ // where the driver vouches that what it said was taken stands there, and not for what was
1667
+ // taken before the product put the conversation in fewer words.
1668
+ if (driver.can.holdsTaken === true) {
1669
+ for (const signal of unanswered) {
1670
+ if (!target.summarized.has(signal.number)) {
1671
+ above.add(signal.number);
1672
+ }
1673
+ }
1674
+ }
1467
1675
  target.taken = [];
1468
1676
  target.sent = [];
1469
1677
  target.returning = false;
@@ -1568,7 +1776,7 @@ export function createRunner(options) {
1568
1776
  return;
1569
1777
  }
1570
1778
  await end("reset");
1571
- conversation = undefined;
1779
+ letGo();
1572
1780
  // Where a supervisor reset the agent meanwhile, the first message says so: the account's
1573
1781
  // words are read under their own header all the same.
1574
1782
  resetBy ??= {
@@ -1597,7 +1805,7 @@ export function createRunner(options) {
1597
1805
  return;
1598
1806
  }
1599
1807
  case "conversation": {
1600
- conversation = event.id;
1808
+ converses(target, event);
1601
1809
  await persist();
1602
1810
  return;
1603
1811
  }
@@ -1617,6 +1825,8 @@ export function createRunner(options) {
1617
1825
  }
1618
1826
  target.sent = [];
1619
1827
  target.returning = false;
1828
+ // The agent is done with all it was handed: nothing of it is owed a second saying.
1829
+ above.clear();
1620
1830
  if (limited !== undefined) {
1621
1831
  // A turn completed: whatever stopped the agent is over.
1622
1832
  limited = undefined;
@@ -1634,6 +1844,11 @@ export function createRunner(options) {
1634
1844
  }
1635
1845
  return;
1636
1846
  }
1847
+ case "compacted": {
1848
+ summarized(target);
1849
+ await persist();
1850
+ return;
1851
+ }
1637
1852
  case "usage": {
1638
1853
  noteUsage(usageSeen, event.windows, options.now());
1639
1854
  await onUsage(target, event.windows);
@@ -1789,22 +2004,142 @@ export function createRunner(options) {
1789
2004
  push({ t: "heartbeat" });
1790
2005
  }
1791
2006
  };
2007
+ /**
2008
+ * Whether the agent is thinking over what a wake source raised (ADR-0170): the turn a wake
2009
+ * of that source's was handed over in is under way, or one is on its way to the agent,
2010
+ * raised and not back on the stream yet or waiting to be handed over. What waits is nothing
2011
+ * the agent thinks over while nothing is handed over at all, a supervisor's stop standing
2012
+ * or the agent unable to go on.
2013
+ */
2014
+ const thinksOf = (name, now) => {
2015
+ const own = (signal) => signal.kind === "wake" &&
2016
+ signal.source === "messenger" &&
2017
+ signal.key?.startsWith(`${name}:`) === true;
2018
+ if (session?.turn === true && session.sent.some(own)) {
2019
+ return true;
2020
+ }
2021
+ if (held || waits !== undefined) {
2022
+ return false;
2023
+ }
2024
+ const raised = raising.get(name);
2025
+ for (const one of raised ?? []) {
2026
+ // Back on the stream, and among what waits from then on; or never to come, for all the
2027
+ // runner waits for it.
2028
+ if ((one.number !== undefined && one.number <= handled) || now - one.at >= RAISED_MS) {
2029
+ raised?.delete(one);
2030
+ }
2031
+ }
2032
+ return (raised?.size ?? 0) > 0 || pending.some(own);
2033
+ };
2034
+ /**
2035
+ * How the agent stands to whoever waits for it at a wake source (ADR-0170). Its usage limit
2036
+ * first, until the moment the product named where it named one: nothing is answered before
2037
+ * it, whatever else goes on, and past it the runner brings the agent back in a turn as any
2038
+ * other. Then that it has been at work for longer than `runner.json` gives a turn, whatever
2039
+ * woke it; then that it thinks over what this source raised.
2040
+ */
2041
+ const statusFor = (name, now) => {
2042
+ if (limited !== undefined && (limited.until === undefined || now < limited.until)) {
2043
+ return limited.until === undefined
2044
+ ? { state: "limited" }
2045
+ : { state: "limited", until: limited.until };
2046
+ }
2047
+ if (atWorkSince !== undefined &&
2048
+ shownAs !== false &&
2049
+ now - atWorkSince >= (shownAs.after ?? STATUS_AFTER) * 1000) {
2050
+ return { state: "working" };
2051
+ }
2052
+ return { state: thinksOf(name, now) ? "thinking" : "idle" };
2053
+ };
2054
+ /**
2055
+ * A moment as a status says one: the hour and the minute in the workspace's zone, and with
2056
+ * its day where it is most of a day away, as the command writes a moment's.
2057
+ */
2058
+ const statusTime = (at) => {
2059
+ const iso = new Date(at).toISOString();
2060
+ const written = moment(iso);
2061
+ const day = /^\d{4}-(\d{2}-\d{2})T(\d{2}:\d{2})/.exec(written);
2062
+ return at - options.now() < FAR_MS || day === null
2063
+ ? options.time(iso)
2064
+ : `${day[1]} ${day[2]}${written.endsWith("Z") ? " UTC" : ""}`;
2065
+ };
2066
+ /** Says to a wake source how the agent stands. What the source cannot show costs the agent nothing. */
2067
+ const say = (one, status, signal) => Promise.resolve()
2068
+ .then(() => one.source.show?.(one.host, status, {
2069
+ words: shownAs === false ? {} : shownAs,
2070
+ language: options.language,
2071
+ now: options.now,
2072
+ time: statusTime,
2073
+ signal,
2074
+ }))
2075
+ .catch((error) => {
2076
+ sayOnce(`status ${one.source.name}`, `the wake source ${one.source.name} could not show how the agent stands (${codeOf(error)})`);
2077
+ });
2078
+ /**
2079
+ * Has the wake sources show how the agent stands (ADR-0170): each is said when that changed,
2080
+ * and again every heartbeat while it stands, so that a source keeps what it shows from
2081
+ * lapsing. Nothing is said where `runner.json` turned it off.
2082
+ */
2083
+ const showStatus = () => {
2084
+ if (shownAs === false) {
2085
+ return;
2086
+ }
2087
+ const now = options.now();
2088
+ const active = session !== undefined && (session.turn || goesOn(session));
2089
+ atWorkSince = active ? (atWorkSince ?? now) : undefined;
2090
+ for (const [name, one] of shows) {
2091
+ const status = statusFor(name, now);
2092
+ const word = `${status.state} ${status.state === "limited" ? (status.until ?? "") : ""}`;
2093
+ if (word === one.said && now - one.at < timing.heartbeatMs) {
2094
+ continue;
2095
+ }
2096
+ one.said = word;
2097
+ one.at = now;
2098
+ void say(one, status, abort.signal);
2099
+ }
2100
+ };
2101
+ /**
2102
+ * The last thing the sources are said: nothing to show, as the runner ends, so that what they
2103
+ * showed is taken back. Waited for a few seconds and no longer: a messenger that does not
2104
+ * answer holds no runner's end, and what stays there ends by itself.
2105
+ */
2106
+ const showLast = async () => {
2107
+ if (shownAs === false || shows.size === 0) {
2108
+ return;
2109
+ }
2110
+ const ending = new AbortController();
2111
+ const over = new Promise((resolve) => {
2112
+ ending.signal.addEventListener("abort", () => resolve(), { once: true });
2113
+ });
2114
+ const timer = setTimeout(() => ending.abort(), SHOWN_END_MS);
2115
+ await Promise.race([
2116
+ Promise.all([...shows.values()].map((one) => say(one, LAST_STATUS, ending.signal))),
2117
+ over,
2118
+ ]);
2119
+ clearTimeout(timer);
2120
+ ending.abort();
2121
+ };
1792
2122
  /**
1793
2123
  * Why the agent stands as it does, and until when where the runner knows: when its usage
1794
2124
  * limit resets. A console of before the word `usage_limit` refuses a heartbeat that says it,
1795
2125
  * and with it everything else the heartbeat carries: such a console is said `crashed`, the
1796
- * nearest word it takes, and no moment.
2126
+ * nearest word it takes, and no moment. With it, when the runner said so, which a console of
2127
+ * before reads past.
1797
2128
  */
1798
2129
  const saidOfState = () => {
2130
+ // A moment kept by a runner before this one is taken as it reads; one that is no moment is now.
2131
+ const at = Number.isFinite(stands.at) && Math.abs(stands.at) <= 8.64e15 ? stands.at : options.now();
2132
+ const reasonAt = new Date(at).toISOString();
1799
2133
  if (stands.reason !== "usage_limit") {
1800
- return { reason: stands.reason };
2134
+ return { reason: stands.reason, reasonAt };
1801
2135
  }
1802
2136
  if (plainWords) {
1803
- return { reason: "crashed" };
2137
+ return { reason: "crashed", reasonAt };
1804
2138
  }
1805
2139
  return {
1806
2140
  reason: stands.reason,
1807
2141
  until: limited?.until === undefined ? null : new Date(limited.until).toISOString(),
2142
+ reasonAt,
1808
2143
  };
1809
2144
  };
1810
2145
  /**
@@ -1853,7 +2188,7 @@ export function createRunner(options) {
1853
2188
  if (answer.state === "stopped" && !held && answer.latest <= handled) {
1854
2189
  held = true;
1855
2190
  await end("stopped");
1856
- stands = { state: "stopped", reason: "stop" };
2191
+ stands = { state: "stopped", reason: "stop", at: options.now() };
1857
2192
  }
1858
2193
  }
1859
2194
  catch (error) {
@@ -1994,6 +2329,20 @@ export function createRunner(options) {
1994
2329
  if (source.run === undefined || wakes === undefined) {
1995
2330
  return;
1996
2331
  }
2332
+ // What the source kept under a runner before this one, where the machine keeps it
2333
+ // (ADR-0166): the ids and the moments of what it brought, so that a runner started again
2334
+ // says nothing a second time. What does not read is nothing kept.
2335
+ const kept = new Kept();
2336
+ try {
2337
+ for (const [name, value] of (await wakes.kept?.load(source.name)) ?? []) {
2338
+ kept.set(name, value);
2339
+ }
2340
+ }
2341
+ catch {
2342
+ log(`what the wake source ${source.name} kept could not be read; it begins with nothing`);
2343
+ }
2344
+ kept.changed = false;
2345
+ keeps.set(source.name, kept);
1997
2346
  const host = {
1998
2347
  secret: async (name) => secretValues.get(name),
1999
2348
  channels: options.config.channels ?? [],
@@ -2008,10 +2357,21 @@ export function createRunner(options) {
2008
2357
  // what someone else wrote, and a text with no line of the source's as words whole, so
2009
2358
  // that no word of a message reads as a line of the source's when the signal returns.
2010
2359
  const text = wakeTextOf("parts" in raised ? raised.parts : [{ words: raised.text }]);
2360
+ // On its way to the agent from here, which the source shows (ADR-0170): until the
2361
+ // stream brings it back, or the console did not take it.
2362
+ const underWay = {
2363
+ at: options.now(),
2364
+ number: undefined,
2365
+ };
2366
+ const others = raising.get(source.name) ?? new Set();
2367
+ raising.set(source.name, others.add(underWay));
2368
+ push({ t: "shown" });
2011
2369
  try {
2012
- await client.raiseWake({ source: "messenger", key: raised.key, text });
2370
+ underWay.number = (await client.raiseWake({ source: "messenger", key: raised.key, text })).number;
2013
2371
  }
2014
2372
  catch (error) {
2373
+ others.delete(underWay);
2374
+ push({ t: "shown" });
2015
2375
  if (codeOf(error) !== "signal.too_many") {
2016
2376
  throw error;
2017
2377
  }
@@ -2026,7 +2386,8 @@ export function createRunner(options) {
2026
2386
  }
2027
2387
  return true;
2028
2388
  },
2029
- kept: new Map(),
2389
+ kept,
2390
+ began: beganBy,
2030
2391
  log,
2031
2392
  stands: (state, why) => sourceStands(source.name, state, why),
2032
2393
  fetch: wakes.fetch,
@@ -2043,9 +2404,12 @@ export function createRunner(options) {
2043
2404
  }),
2044
2405
  };
2045
2406
  sourceStands(source.name, "waiting");
2407
+ if (source.show !== undefined) {
2408
+ shows.set(source.name, { source, host, said: "", at: 0 });
2409
+ }
2046
2410
  let wait = timing.reconnectMs;
2047
2411
  while (!stopped) {
2048
- const began = options.now();
2412
+ const startedAt = options.now();
2049
2413
  try {
2050
2414
  await source.run(host, abort.signal);
2051
2415
  // Ended with no error, a connection the other side closed say: it listens no more
@@ -2061,7 +2425,7 @@ export function createRunner(options) {
2061
2425
  }
2062
2426
  }
2063
2427
  wait =
2064
- options.now() - began >= SOURCE_HELD_MS
2428
+ options.now() - startedAt >= SOURCE_HELD_MS
2065
2429
  ? timing.reconnectMs
2066
2430
  : Math.min(wait * 2, MAX_SOURCE_PAUSE_MS);
2067
2431
  await pause(wait);
@@ -2103,10 +2467,13 @@ export function createRunner(options) {
2103
2467
  const kept = await options.state.load().catch(() => undefined);
2104
2468
  delivered = kept?.delivered ?? 0;
2105
2469
  conversation = kept?.conversation;
2470
+ // A moment after now is none the conversation began at: held to the clock here, and
2471
+ // wherever a wake source asks.
2472
+ began = Math.min(kept?.began ?? 0, options.now());
2106
2473
  if (conversation !== undefined && kept?.driver !== undefined && kept.driver !== driver.name) {
2107
2474
  // Another product's conversation is nothing this one can go on with: asked to, it fails
2108
2475
  // at every turn until a supervisor resets it.
2109
- conversation = undefined;
2476
+ letGo();
2110
2477
  log(`the conversation kept was begun under the driver ${kept.driver}: a new one begins under ${driver.name}`);
2111
2478
  }
2112
2479
  // What a runner before this one held for the agent alone is taken from the stream once
@@ -2128,9 +2495,14 @@ export function createRunner(options) {
2128
2495
  tries: 1,
2129
2496
  until: known === undefined ? now + timing.coolDownMs : known + timing.resetMarginMs,
2130
2497
  };
2131
- stands = { state: "failed", reason: "usage_limit" };
2498
+ // Said as of when the limit was reached, which the runner before this one kept.
2499
+ stands = { state: "failed", reason: "usage_limit", at: kept.limited.since };
2132
2500
  log(`the product's usage limit stopped the agent before this runner started: tried again at ${new Date(waits.until).toISOString()}`);
2133
2501
  }
2502
+ if ((kept?.began ?? 0) > began) {
2503
+ // Kept as it is taken, once everything else the file said is in place again.
2504
+ await persist();
2505
+ }
2134
2506
  probe = await driver.probe();
2135
2507
  const left = probe.models.length +
2136
2508
  probe.efforts.length -
@@ -2158,7 +2530,7 @@ export function createRunner(options) {
2158
2530
  delivered = Math.max(delivered, first.delivered);
2159
2531
  if (first.state === "stopped") {
2160
2532
  held = true;
2161
- stands = { state: "stopped", reason: "stop" };
2533
+ stands = { state: "stopped", reason: "stop", at: options.now() };
2162
2534
  }
2163
2535
  break;
2164
2536
  }
@@ -2233,6 +2605,10 @@ export function createRunner(options) {
2233
2605
  if (session !== undefined) {
2234
2606
  await flush(session);
2235
2607
  }
2608
+ await keepKept();
2609
+ }
2610
+ else if (message.t === "shown") {
2611
+ // Looked at below, as after anything else.
2236
2612
  }
2237
2613
  else {
2238
2614
  // A timer counts no time the machine sleeps in, and rings late by as much: what
@@ -2285,6 +2661,9 @@ export function createRunner(options) {
2285
2661
  catch (error) {
2286
2662
  log(`the runner could not do what it was about to (${codeOf(error)}); it goes on`);
2287
2663
  }
2664
+ // Whatever was done may have changed how the agent stands to whoever waits for it,
2665
+ // and so does the time that passed: looked at once a second at the least.
2666
+ showStatus();
2288
2667
  }
2289
2668
  }
2290
2669
  finally {
@@ -2294,12 +2673,18 @@ export function createRunner(options) {
2294
2673
  for (const timer of timers) {
2295
2674
  clearTimeout(timer);
2296
2675
  }
2676
+ // What the wake sources showed of the agent is taken back while its session ends
2677
+ // (ADR-0170): a runner that is gone shows nothing.
2678
+ const shown = showLast();
2297
2679
  // A session ended for an update goes on, in the runner started after, as one a
2298
2680
  // supervisor restarted does: with the same conversation.
2299
2681
  await end(how === "stopped" ? "exited" : "restarted");
2682
+ await shown;
2300
2683
  abort.abort();
2301
2684
  await listening;
2302
2685
  await Promise.all(sourcing);
2686
+ // What the sources raised as they ended is kept too.
2687
+ await keepKept();
2303
2688
  log(how === "updated"
2304
2689
  ? "the runner ended, for the update"
2305
2690
  : how === "changed"