@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
@@ -13,7 +13,7 @@ import {
13
13
  import { type RestSignal, type RestTranscriptEventInput, RUNNER_CHOICE } from "../schemas.js";
14
14
  import { hasForbiddenCodePoint } from "../text.js";
15
15
  import type { AgentStateReason, TranscriptKind } from "../vocabulary.js";
16
- import type { RunnerConfig } from "./config.js";
16
+ import { type RunnerConfig, STATUS_AFTER } from "./config.js";
17
17
  import {
18
18
  type Attached,
19
19
  composeSignals,
@@ -27,6 +27,7 @@ import {
27
27
  } from "./message.js";
28
28
  import { cutShown, cutShownEnd, type Redactor, redactorOf } from "./redact.js";
29
29
  import type {
30
+ AgentStatus,
30
31
  Driver,
31
32
  DriverEvent,
32
33
  DriverSession,
@@ -63,6 +64,15 @@ import {
63
64
  // numbered and kept for the next batch as it arrives, so that nothing of a session is left
64
65
  // behind when the session ends.
65
66
 
67
+ /** How long a wake a source raised is waited for on the stream before the source is shown nothing of it. */
68
+ const RAISED_MS = 60_000;
69
+ /** From how far away a moment is said with its day in a status. */
70
+ const FAR_MS = 20 * 60 * 60_000;
71
+ /** How long the runner's end waits for the wake sources to take back what they showed. */
72
+ const SHOWN_END_MS = 3000;
73
+ /** What the wake sources are said as the runner ends. */
74
+ const LAST_STATUS: AgentStatus = { state: "idle", last: true };
75
+
66
76
  /** How often the runner says it is there. */
67
77
  export const HEARTBEAT_MS = 30_000;
68
78
  /** How often what the product did is sent, while there is something to send. */
@@ -105,6 +115,12 @@ const SOURCE_HELD_MS = 60_000;
105
115
  const SLEPT_MS = 60_000;
106
116
  /** The kinds of event that are the agent's own answer: what it said, thought or called. */
107
117
  const ANSWERS: ReadonlySet<TranscriptKind> = new Set(["assistant", "thinking", "tool_call"]);
118
+ /**
119
+ * How long before its wake is on the record a message may have been written (ADR-0166): what a
120
+ * wake source reads for a wake, and the wakes ahead of it, are waited for half a minute at the
121
+ * most, and the messenger's clock and the console's differ by less than the rest.
122
+ */
123
+ const RAISED_AFTER_MS = 60_000;
108
124
  /** How many signals the runner writes down as kept for the agent beside what the record sends again. */
109
125
  const MAX_KEPT = 50;
110
126
  /** How long before its moment a timer may ring: a delivery asked for at a moment is taken within it. */
@@ -188,8 +204,18 @@ export interface RunnerState {
188
204
  * runner started meanwhile takes them from the stream once more.
189
205
  */
190
206
  readonly kept?: readonly number[] | undefined;
207
+ /**
208
+ * When the conversation the agent goes on with began, in milliseconds since the epoch
209
+ * (ADR-0166): what it was handed before, it holds no more, and a wake source counts nothing
210
+ * from before as brought. Left out by a runner of before, whose conversation is taken to hold
211
+ * all it was handed.
212
+ */
213
+ readonly began?: number | undefined;
191
214
  }
192
215
 
216
+ /** What a wake source keeps, as it is written down: each name with what stands under it, the oldest first. */
217
+ export type KeptBySource = readonly (readonly [string, string])[];
218
+
193
219
  export interface RunnerOptions {
194
220
  readonly client: RunnerClient;
195
221
  readonly driver: Driver;
@@ -259,8 +285,25 @@ export interface RunnerOptions {
259
285
  readonly sources: readonly WakeSource[];
260
286
  readonly fetch: WakeHost["fetch"];
261
287
  readonly socket: WakeHost["socket"];
288
+ /**
289
+ * Where what a wake source keeps (`WakeHost.kept`) outlasts the runner (ADR-0166): read
290
+ * when the source is started, and written when it changed, a source at a time by its
291
+ * name. Left out, a runner started again begins with nothing kept.
292
+ */
293
+ readonly kept?:
294
+ | {
295
+ load(source: string): Promise<KeptBySource | undefined>;
296
+ save(source: string, kept: KeptBySource): Promise<void>;
297
+ }
298
+ | undefined;
262
299
  }
263
300
  | undefined;
301
+ /**
302
+ * The language of the workspace, where whoever starts the runner knows it: what a wake source
303
+ * shows of the agent to the people at its messenger is in it, where the source has the words
304
+ * (ADR-0170). Left out, the source's own.
305
+ */
306
+ readonly language?: string | undefined;
264
307
  /**
265
308
  * How the desk's `runner.json` stands against the one this runner was started with (ADR-0144):
266
309
  * the same, another that reads, or one that does not read, with what is wrong with it in a few
@@ -376,6 +419,12 @@ interface OpenSession {
376
419
  */
377
420
  taken: (readonly RestSignal[])[];
378
421
  unread: number;
422
+ /**
423
+ * The signals the product took before it put the conversation in fewer words (ADR-0166), by
424
+ * number: the conversation holds a summary of them at best, so a usage limit met afterwards
425
+ * has them said whole again, and not named.
426
+ */
427
+ readonly summarized: Set<number>;
379
428
  /** Whether the agent was handed word that it is back from a usage limit, and has not answered since. */
380
429
  returning: boolean;
381
430
  /** How many sub-agents are at work in its background, as the driver said last (ADR-0162). */
@@ -425,6 +474,8 @@ type Message =
425
474
  | { readonly t: "answered"; readonly session: OpenSession }
426
475
  | { readonly t: "deliver" }
427
476
  | { readonly t: "flush" }
477
+ /** A wake source raised something, or what it raised was not taken: how the agent stands to it is looked at again. */
478
+ | { readonly t: "shown" }
428
479
  | { readonly t: "heartbeat" }
429
480
  | { readonly t: "stop" };
430
481
 
@@ -489,6 +540,22 @@ const lineOf = (value: string | null | undefined): string | null => {
489
540
  return line.length === 0 || hasForbiddenCodePoint(line) ? null : line;
490
541
  };
491
542
 
543
+ /** What a wake source keeps, which says whether it changed since it was last written down. */
544
+ class Kept extends Map<string, string> {
545
+ changed = false;
546
+
547
+ override set(name: string, value: string): this {
548
+ this.changed = true;
549
+ return super.set(name, value);
550
+ }
551
+
552
+ override delete(name: string): boolean {
553
+ const had = super.delete(name);
554
+ this.changed ||= had;
555
+ return had;
556
+ }
557
+ }
558
+
492
559
  export function createRunner(options: RunnerOptions): Runner {
493
560
  const { client, driver, config, log } = options;
494
561
  const timing = {
@@ -568,9 +635,38 @@ export function createRunner(options: RunnerOptions): Runner {
568
635
  let received = 0;
569
636
  let handled = 0;
570
637
  let conversation: string | undefined;
571
- let stands: { state: "running" | "stopped" | "refused" | "failed"; reason: AgentStateReason } = {
638
+ /**
639
+ * When the conversation the agent goes on with began (ADR-0166), 0 where nothing says, and
640
+ * whether that moment is of a conversation no session has begun yet: the session that begins
641
+ * it then leaves the moment as it stands.
642
+ */
643
+ let began = 0;
644
+ let renewed = false;
645
+ /**
646
+ * The signals the conversation holds already, by number (ADR-0166): the product of the open
647
+ * session took them there as a usage limit was reached, its driver vouching that what it
648
+ * took stands (`can.holdsTaken`), and the agent never answered. Handed over again in that
649
+ * session, they are named and not said a second time. Until a turn completes, the
650
+ * conversation is let go or put in fewer words, or the session ends: nothing says before a
651
+ * session's first message whether its product went on with the conversation, so a session
652
+ * begun after is said them whole.
653
+ */
654
+ const above = new Set<number>();
655
+ /** What each wake source keeps, by its name, and whether it changed since it was last written. */
656
+ const keeps = new Map<string, Kept>();
657
+ /**
658
+ * How the agent stands and why, with when the runner said so (`at`): the console is told that
659
+ * moment with the reason, since a reason is said again the same under a state that stays, a
660
+ * turn that completed after another say, and nothing else tells the two apart.
661
+ */
662
+ let stands: {
663
+ state: "running" | "stopped" | "refused" | "failed";
664
+ reason: AgentStateReason;
665
+ at: number;
666
+ } = {
572
667
  state: "running",
573
668
  reason: "started",
669
+ at: options.now(),
574
670
  };
575
671
  let said = "";
576
672
  let settings: { model: string | null; effort: string | null } = { model: null, effort: null };
@@ -653,6 +749,28 @@ export function createRunner(options: RunnerOptions): Runner {
653
749
  string,
654
750
  { state: "connected" | "waiting" | "failing"; since: number; why: string | undefined }
655
751
  >();
752
+ /**
753
+ * The wake sources that show how the agent stands (ADR-0170), by name: the host each runs
754
+ * with, and what it was last said and when.
755
+ */
756
+ const shows = new Map<
757
+ string,
758
+ { readonly source: WakeSource; readonly host: WakeHost; said: string; at: number }
759
+ >();
760
+ /**
761
+ * What `runner.json` says of how the agent stands is shown: `false` for not at all, and the
762
+ * sources' own way where it says nothing, a desk of before and a config put together by hand
763
+ * among them.
764
+ */
765
+ const shownAs: NonNullable<RunnerConfig["status"]> = config.status ?? {};
766
+ /** Since when the agent is at work without a pause: a turn under way, or something that goes on in its session with none. */
767
+ let atWorkSince: number | undefined;
768
+ /**
769
+ * What each wake source raised that the stream has not brought back yet: when it was raised,
770
+ * and its number once the console took it. A message that reached the runner is on its way
771
+ * to the agent from then on, which its source shows.
772
+ */
773
+ const raising = new Map<string, Set<{ readonly at: number; number: number | undefined }>>();
656
774
  /**
657
775
  * The signals a product was handed and never took, since it did not start or was not signed
658
776
  * in, which are kept to be sent again; and, of those, the ones a reset gave one more try: a
@@ -691,14 +809,114 @@ export function createRunner(options: RunnerOptions): Runner {
691
809
  ? {}
692
810
  : { limited: { since: limited.since, until: limited.until } }),
693
811
  ...(kept.length === 0 ? {} : { kept }),
812
+ ...(began === 0 ? {} : { began }),
694
813
  });
695
814
  } catch {
696
815
  log("the runner's state could not be written; it goes on from the console's record");
697
816
  }
698
817
  };
699
818
 
819
+ /**
820
+ * When a conversation that begins now is taken to have begun (ADR-0166): now, or, where wakes
821
+ * of a wake source wait to be handed over, a little before the oldest of them was raised.
822
+ * They are handed over in the new conversation, which holds them from then on: counted as
823
+ * from before it, their messages would be carried once more with the next wake. `handed` is
824
+ * what the conversation holds beside what waits: what a session was handed in it already,
825
+ * where the moment is set once the session is under way.
826
+ */
827
+ const beginsAt = (handed: readonly RestSignal[] = []): number => {
828
+ let at = options.now();
829
+ for (const signal of [...pending, ...handed]) {
830
+ const raised = signal.source === "messenger" ? Date.parse(signal.at) : Number.NaN;
831
+ if (Number.isFinite(raised)) {
832
+ at = Math.min(at, raised - RAISED_AFTER_MS);
833
+ }
834
+ }
835
+ return at;
836
+ };
837
+
838
+ /**
839
+ * The driver named the conversation its session runs in. Where it began one in place of the
840
+ * one it was asked to go on with (`anew`), the agent's conversation began with the session
841
+ * (ADR-0166): it holds what the session was handed, and nothing from before.
842
+ */
843
+ const converses = (
844
+ target: OpenSession,
845
+ event: Extract<DriverEvent, { type: "conversation" }>,
846
+ ): void => {
847
+ conversation = event.id;
848
+ if (event.anew === true) {
849
+ began = beginsAt([...target.sent, ...target.handed.flatMap((message) => message.signals)]);
850
+ above.clear();
851
+ }
852
+ };
853
+
854
+ /**
855
+ * The product put the conversation in fewer words (ADR-0166): what the agent was handed
856
+ * before is a summary at best, so what a wake source brought before counts no more, and what
857
+ * the product took is not named as standing there. What it was handed and has not taken yet
858
+ * it reads after, whole: the conversation is taken to hold that.
859
+ */
860
+ const summarized = (target: OpenSession): void => {
861
+ began = beginsAt(target.handed.flatMap((message) => message.signals));
862
+ above.clear();
863
+ for (const signal of target.taken.flat()) {
864
+ target.summarized.add(signal.number);
865
+ }
866
+ };
867
+
868
+ /**
869
+ * When the agent's conversation began, as a wake source is told it (ADR-0166): never after
870
+ * now. A moment ahead of the clock, kept by a runner whose machine's clock ran ahead or set
871
+ * back since, is none a message was written before: counted from, every wake of a place
872
+ * would carry again what the last one carried, for as long as the clock is behind it. It is
873
+ * taken as now, and kept so.
874
+ */
875
+ const beganBy = (): number => {
876
+ const now = options.now();
877
+ if (began > now) {
878
+ began = now;
879
+ void persist();
880
+ }
881
+ return began;
882
+ };
883
+
884
+ /**
885
+ * Lets go of the conversation kept: the next session begins a new one, which holds nothing
886
+ * the agent was handed before this moment (ADR-0166), but for what waits to be handed over.
887
+ */
888
+ const letGo = (): void => {
889
+ conversation = undefined;
890
+ began = beginsAt();
891
+ renewed = true;
892
+ above.clear();
893
+ };
894
+
895
+ /** Writes what the wake sources keep where it changed since it was last written. */
896
+ const keepKept = async (): Promise<void> => {
897
+ const store = options.wakes?.kept;
898
+ if (store === undefined) {
899
+ return;
900
+ }
901
+ for (const [name, kept] of keeps) {
902
+ if (!kept.changed) {
903
+ continue;
904
+ }
905
+ kept.changed = false;
906
+ try {
907
+ await store.save(name, [...kept]);
908
+ } catch {
909
+ // Kept in memory still, and written with the next change.
910
+ sayOnce(
911
+ `kept ${name}`,
912
+ `what the wake source ${name} keeps could not be written; it is kept while the runner runs`,
913
+ );
914
+ }
915
+ }
916
+ };
917
+
700
918
  const setState = (state: typeof stands.state, reason: AgentStateReason): void => {
701
- stands = { state, reason };
919
+ stands = { state, reason, at: options.now() };
702
920
  push({ t: "heartbeat" });
703
921
  };
704
922
 
@@ -1144,6 +1362,7 @@ export function createRunner(options: RunnerOptions): Runner {
1144
1362
  took: false,
1145
1363
  taken: [],
1146
1364
  unread: 0,
1365
+ summarized: new Set(),
1147
1366
  returning: false,
1148
1367
  background: 0,
1149
1368
  own: false,
@@ -1159,6 +1378,16 @@ export function createRunner(options: RunnerOptions): Runner {
1159
1378
  started.pumped = pump(started);
1160
1379
  session = started;
1161
1380
  renew = false;
1381
+ if (resume === undefined) {
1382
+ // A conversation of its own: it holds nothing the agent was handed before (ADR-0166).
1383
+ // Where it was let go a moment ago, at a reset say, it began then, and what was kept
1384
+ // for it since is handed over in it.
1385
+ if (!renewed) {
1386
+ began = beginsAt();
1387
+ }
1388
+ above.clear();
1389
+ }
1390
+ renewed = false;
1162
1391
  log(
1163
1392
  `session ${started.id} began${resume === undefined ? "" : ", going on with the conversation before"}`,
1164
1393
  );
@@ -1181,6 +1410,9 @@ export function createRunner(options: RunnerOptions): Runner {
1181
1410
  }
1182
1411
  await target.pumped;
1183
1412
  await hearLast(target);
1413
+ // What was named as standing in the conversation was taken by this session's product: a
1414
+ // session begun after is said it whole (ADR-0166).
1415
+ above.clear();
1184
1416
  // What the product was handed and never took is sent again, by the console where the
1185
1417
  // runner itself ends and by the runner where the product had not started; else it is on
1186
1418
  // the record as never read.
@@ -1331,7 +1563,7 @@ export function createRunner(options: RunnerOptions): Runner {
1331
1563
  limited === undefined
1332
1564
  ) {
1333
1565
  await end("restarted");
1334
- conversation = undefined;
1566
+ letGo();
1335
1567
  }
1336
1568
  const target = session ?? (await begin());
1337
1569
  if (target === undefined) {
@@ -1365,6 +1597,11 @@ export function createRunner(options: RunnerOptions): Runner {
1365
1597
  // what the driver says of its product.
1366
1598
  const takesPictures = target.driver.images?.() ?? driver.can.images;
1367
1599
  for (const signal of batch) {
1600
+ // What is only named is handed over with nothing: its files stand in the desk and its
1601
+ // pictures in the conversation, with the message the product took (ADR-0166).
1602
+ if (above.has(signal.number)) {
1603
+ continue;
1604
+ }
1368
1605
  const taken = new Set<string>();
1369
1606
  for (const file of signal.attachments) {
1370
1607
  const picture = PICTURE_TYPES.has(file.contentType);
@@ -1411,10 +1648,15 @@ export function createRunner(options: RunnerOptions): Runner {
1411
1648
  ]);
1412
1649
  }
1413
1650
  }
1651
+ // What the conversation holds already, the product having taken it there as the limit was
1652
+ // reached, is named and not said again (ADR-0166).
1653
+ const named = batch.filter((signal) => above.has(signal.number)).length;
1414
1654
  const composed = composeSignals(batch, {
1415
1655
  mark: target.mark,
1416
1656
  time: options.time,
1417
1657
  attached: (signal) => attached.get(signal.number) ?? [],
1658
+ own: options.account,
1659
+ above: (signal) => above.has(signal.number),
1418
1660
  });
1419
1661
  const intro = target.first ? introOf(target.mark, resetBy) : undefined;
1420
1662
  // What the runner says of its own goes ahead of the signals, under its own header: that
@@ -1430,7 +1672,13 @@ export function createRunner(options: RunnerOptions): Runner {
1430
1672
  ? undefined
1431
1673
  : runnerSays(
1432
1674
  target.mark,
1433
- limitWords(returned, { now: options.now(), moment, follow: batch.length, early }),
1675
+ limitWords(returned, {
1676
+ now: options.now(),
1677
+ moment,
1678
+ follow: batch.length - named,
1679
+ above: named,
1680
+ early,
1681
+ }),
1434
1682
  ),
1435
1683
  told === undefined ? undefined : sleptOf(told, { mark: target.mark, moment }),
1436
1684
  composed.text,
@@ -1564,7 +1812,9 @@ export function createRunner(options: RunnerOptions): Runner {
1564
1812
  * Hears what the product said as its session was ended. Its words wait in the queue behind
1565
1813
  * whatever ended the session, and are let go there once the session is no longer the open
1566
1814
  * 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.
1815
+ * named is the one to go on with, where that message is. That it began that conversation
1816
+ * anew, or put it in fewer words, moves the moment the conversation began as it does in a
1817
+ * session that goes on (ADR-0166): the next session goes on with what was said here.
1568
1818
  */
1569
1819
  const hearLast = async (target: OpenSession): Promise<void> => {
1570
1820
  let named = false;
@@ -1574,7 +1824,10 @@ export function createRunner(options: RunnerOptions): Runner {
1574
1824
  }
1575
1825
  const { event } = message;
1576
1826
  if (event.type === "conversation") {
1577
- conversation = event.id;
1827
+ converses(target, event);
1828
+ named = true;
1829
+ } else if (event.type === "compacted") {
1830
+ summarized(target);
1578
1831
  named = true;
1579
1832
  } else if (event.type === "taken") {
1580
1833
  await take(target);
@@ -1793,7 +2046,7 @@ export function createRunner(options: RunnerOptions): Runner {
1793
2046
  limited = undefined;
1794
2047
  await end(signal.kind === "reset" ? "reset" : "restarted");
1795
2048
  if (signal.kind === "reset") {
1796
- conversation = undefined;
2049
+ letGo();
1797
2050
  resetBy = { by, ...(signal.text.trim().length > 0 ? { number: signal.number } : {}) };
1798
2051
  // What a product was handed and never took is tried once more, in the new
1799
2052
  // conversation: a product that does not start with it again is not sent it a
@@ -1899,6 +2152,17 @@ export function createRunner(options: RunnerOptions): Runner {
1899
2152
  for (const signal of untaken) {
1900
2153
  owed.delete(signal.number);
1901
2154
  }
2155
+ // What the product took stands in the conversation, which the agent goes on in: handed
2156
+ // over again in this session, it is named and not said a second time (ADR-0166). Only
2157
+ // where the driver vouches that what it said was taken stands there, and not for what was
2158
+ // taken before the product put the conversation in fewer words.
2159
+ if (driver.can.holdsTaken === true) {
2160
+ for (const signal of unanswered) {
2161
+ if (!target.summarized.has(signal.number)) {
2162
+ above.add(signal.number);
2163
+ }
2164
+ }
2165
+ }
1902
2166
  target.taken = [];
1903
2167
  target.sent = [];
1904
2168
  target.returning = false;
@@ -2028,7 +2292,7 @@ export function createRunner(options: RunnerOptions): Runner {
2028
2292
  return;
2029
2293
  }
2030
2294
  await end("reset");
2031
- conversation = undefined;
2295
+ letGo();
2032
2296
  // Where a supervisor reset the agent meanwhile, the first message says so: the account's
2033
2297
  // words are read under their own header all the same.
2034
2298
  resetBy ??= {
@@ -2058,7 +2322,7 @@ export function createRunner(options: RunnerOptions): Runner {
2058
2322
  return;
2059
2323
  }
2060
2324
  case "conversation": {
2061
- conversation = event.id;
2325
+ converses(target, event);
2062
2326
  await persist();
2063
2327
  return;
2064
2328
  }
@@ -2078,6 +2342,8 @@ export function createRunner(options: RunnerOptions): Runner {
2078
2342
  }
2079
2343
  target.sent = [];
2080
2344
  target.returning = false;
2345
+ // The agent is done with all it was handed: nothing of it is owed a second saying.
2346
+ above.clear();
2081
2347
  if (limited !== undefined) {
2082
2348
  // A turn completed: whatever stopped the agent is over.
2083
2349
  limited = undefined;
@@ -2095,6 +2361,11 @@ export function createRunner(options: RunnerOptions): Runner {
2095
2361
  }
2096
2362
  return;
2097
2363
  }
2364
+ case "compacted": {
2365
+ summarized(target);
2366
+ await persist();
2367
+ return;
2368
+ }
2098
2369
  case "usage": {
2099
2370
  noteUsage(usageSeen, event.windows, options.now());
2100
2371
  await onUsage(target, event.windows);
@@ -2267,22 +2538,166 @@ export function createRunner(options: RunnerOptions): Runner {
2267
2538
  }
2268
2539
  };
2269
2540
 
2541
+ /**
2542
+ * Whether the agent is thinking over what a wake source raised (ADR-0170): the turn a wake
2543
+ * of that source's was handed over in is under way, or one is on its way to the agent,
2544
+ * raised and not back on the stream yet or waiting to be handed over. What waits is nothing
2545
+ * the agent thinks over while nothing is handed over at all, a supervisor's stop standing
2546
+ * or the agent unable to go on.
2547
+ */
2548
+ const thinksOf = (name: string, now: number): boolean => {
2549
+ const own = (signal: RestSignal): boolean =>
2550
+ signal.kind === "wake" &&
2551
+ signal.source === "messenger" &&
2552
+ signal.key?.startsWith(`${name}:`) === true;
2553
+ if (session?.turn === true && session.sent.some(own)) {
2554
+ return true;
2555
+ }
2556
+ if (held || waits !== undefined) {
2557
+ return false;
2558
+ }
2559
+ const raised = raising.get(name);
2560
+ for (const one of raised ?? []) {
2561
+ // Back on the stream, and among what waits from then on; or never to come, for all the
2562
+ // runner waits for it.
2563
+ if ((one.number !== undefined && one.number <= handled) || now - one.at >= RAISED_MS) {
2564
+ raised?.delete(one);
2565
+ }
2566
+ }
2567
+ return (raised?.size ?? 0) > 0 || pending.some(own);
2568
+ };
2569
+
2570
+ /**
2571
+ * How the agent stands to whoever waits for it at a wake source (ADR-0170). Its usage limit
2572
+ * first, until the moment the product named where it named one: nothing is answered before
2573
+ * it, whatever else goes on, and past it the runner brings the agent back in a turn as any
2574
+ * other. Then that it has been at work for longer than `runner.json` gives a turn, whatever
2575
+ * woke it; then that it thinks over what this source raised.
2576
+ */
2577
+ const statusFor = (name: string, now: number): AgentStatus => {
2578
+ if (limited !== undefined && (limited.until === undefined || now < limited.until)) {
2579
+ return limited.until === undefined
2580
+ ? { state: "limited" }
2581
+ : { state: "limited", until: limited.until };
2582
+ }
2583
+ if (
2584
+ atWorkSince !== undefined &&
2585
+ shownAs !== false &&
2586
+ now - atWorkSince >= (shownAs.after ?? STATUS_AFTER) * 1000
2587
+ ) {
2588
+ return { state: "working" };
2589
+ }
2590
+ return { state: thinksOf(name, now) ? "thinking" : "idle" };
2591
+ };
2592
+
2593
+ /**
2594
+ * A moment as a status says one: the hour and the minute in the workspace's zone, and with
2595
+ * its day where it is most of a day away, as the command writes a moment's.
2596
+ */
2597
+ const statusTime = (at: number): string => {
2598
+ const iso = new Date(at).toISOString();
2599
+ const written = moment(iso);
2600
+ const day = /^\d{4}-(\d{2}-\d{2})T(\d{2}:\d{2})/.exec(written);
2601
+ return at - options.now() < FAR_MS || day === null
2602
+ ? options.time(iso)
2603
+ : `${day[1]} ${day[2]}${written.endsWith("Z") ? " UTC" : ""}`;
2604
+ };
2605
+
2606
+ /** Says to a wake source how the agent stands. What the source cannot show costs the agent nothing. */
2607
+ const say = (
2608
+ one: { readonly source: WakeSource; readonly host: WakeHost },
2609
+ status: AgentStatus,
2610
+ signal: AbortSignal,
2611
+ ): Promise<void> =>
2612
+ Promise.resolve()
2613
+ .then(() =>
2614
+ one.source.show?.(one.host, status, {
2615
+ words: shownAs === false ? {} : shownAs,
2616
+ language: options.language,
2617
+ now: options.now,
2618
+ time: statusTime,
2619
+ signal,
2620
+ }),
2621
+ )
2622
+ .catch((error: unknown) => {
2623
+ sayOnce(
2624
+ `status ${one.source.name}`,
2625
+ `the wake source ${one.source.name} could not show how the agent stands (${codeOf(error)})`,
2626
+ );
2627
+ });
2628
+
2629
+ /**
2630
+ * Has the wake sources show how the agent stands (ADR-0170): each is said when that changed,
2631
+ * and again every heartbeat while it stands, so that a source keeps what it shows from
2632
+ * lapsing. Nothing is said where `runner.json` turned it off.
2633
+ */
2634
+ const showStatus = (): void => {
2635
+ if (shownAs === false) {
2636
+ return;
2637
+ }
2638
+ const now = options.now();
2639
+ const active = session !== undefined && (session.turn || goesOn(session));
2640
+ atWorkSince = active ? (atWorkSince ?? now) : undefined;
2641
+ for (const [name, one] of shows) {
2642
+ const status = statusFor(name, now);
2643
+ const word = `${status.state} ${status.state === "limited" ? (status.until ?? "") : ""}`;
2644
+ if (word === one.said && now - one.at < timing.heartbeatMs) {
2645
+ continue;
2646
+ }
2647
+ one.said = word;
2648
+ one.at = now;
2649
+ void say(one, status, abort.signal);
2650
+ }
2651
+ };
2652
+
2653
+ /**
2654
+ * The last thing the sources are said: nothing to show, as the runner ends, so that what they
2655
+ * showed is taken back. Waited for a few seconds and no longer: a messenger that does not
2656
+ * answer holds no runner's end, and what stays there ends by itself.
2657
+ */
2658
+ const showLast = async (): Promise<void> => {
2659
+ if (shownAs === false || shows.size === 0) {
2660
+ return;
2661
+ }
2662
+ const ending = new AbortController();
2663
+ const over = new Promise<void>((resolve) => {
2664
+ ending.signal.addEventListener("abort", () => resolve(), { once: true });
2665
+ });
2666
+ const timer = setTimeout(() => ending.abort(), SHOWN_END_MS);
2667
+ await Promise.race([
2668
+ Promise.all([...shows.values()].map((one) => say(one, LAST_STATUS, ending.signal))),
2669
+ over,
2670
+ ]);
2671
+ clearTimeout(timer);
2672
+ ending.abort();
2673
+ };
2674
+
2270
2675
  /**
2271
2676
  * Why the agent stands as it does, and until when where the runner knows: when its usage
2272
2677
  * limit resets. A console of before the word `usage_limit` refuses a heartbeat that says it,
2273
2678
  * and with it everything else the heartbeat carries: such a console is said `crashed`, the
2274
- * nearest word it takes, and no moment.
2679
+ * nearest word it takes, and no moment. With it, when the runner said so, which a console of
2680
+ * before reads past.
2275
2681
  */
2276
- const saidOfState = (): { reason: AgentStateReason; until?: string | null } => {
2682
+ const saidOfState = (): {
2683
+ reason: AgentStateReason;
2684
+ until?: string | null;
2685
+ reasonAt: string;
2686
+ } => {
2687
+ // A moment kept by a runner before this one is taken as it reads; one that is no moment is now.
2688
+ const at =
2689
+ Number.isFinite(stands.at) && Math.abs(stands.at) <= 8.64e15 ? stands.at : options.now();
2690
+ const reasonAt = new Date(at).toISOString();
2277
2691
  if (stands.reason !== "usage_limit") {
2278
- return { reason: stands.reason };
2692
+ return { reason: stands.reason, reasonAt };
2279
2693
  }
2280
2694
  if (plainWords) {
2281
- return { reason: "crashed" };
2695
+ return { reason: "crashed", reasonAt };
2282
2696
  }
2283
2697
  return {
2284
2698
  reason: stands.reason,
2285
2699
  until: limited?.until === undefined ? null : new Date(limited.until).toISOString(),
2700
+ reasonAt,
2286
2701
  };
2287
2702
  };
2288
2703
 
@@ -2337,7 +2752,7 @@ export function createRunner(options: RunnerOptions): Runner {
2337
2752
  if (answer.state === "stopped" && !held && answer.latest <= handled) {
2338
2753
  held = true;
2339
2754
  await end("stopped");
2340
- stands = { state: "stopped", reason: "stop" };
2755
+ stands = { state: "stopped", reason: "stop", at: options.now() };
2341
2756
  }
2342
2757
  } catch (error) {
2343
2758
  if (stopped) {
@@ -2503,6 +2918,19 @@ export function createRunner(options: RunnerOptions): Runner {
2503
2918
  if (source.run === undefined || wakes === undefined) {
2504
2919
  return;
2505
2920
  }
2921
+ // What the source kept under a runner before this one, where the machine keeps it
2922
+ // (ADR-0166): the ids and the moments of what it brought, so that a runner started again
2923
+ // says nothing a second time. What does not read is nothing kept.
2924
+ const kept = new Kept();
2925
+ try {
2926
+ for (const [name, value] of (await wakes.kept?.load(source.name)) ?? []) {
2927
+ kept.set(name, value);
2928
+ }
2929
+ } catch {
2930
+ log(`what the wake source ${source.name} kept could not be read; it begins with nothing`);
2931
+ }
2932
+ kept.changed = false;
2933
+ keeps.set(source.name, kept);
2506
2934
  const host: WakeHost = {
2507
2935
  secret: async (name) => secretValues.get(name),
2508
2936
  channels: options.config.channels ?? [],
@@ -2517,9 +2945,22 @@ export function createRunner(options: RunnerOptions): Runner {
2517
2945
  // what someone else wrote, and a text with no line of the source's as words whole, so
2518
2946
  // that no word of a message reads as a line of the source's when the signal returns.
2519
2947
  const text = wakeTextOf("parts" in raised ? raised.parts : [{ words: raised.text }]);
2948
+ // On its way to the agent from here, which the source shows (ADR-0170): until the
2949
+ // stream brings it back, or the console did not take it.
2950
+ const underWay: { readonly at: number; number: number | undefined } = {
2951
+ at: options.now(),
2952
+ number: undefined,
2953
+ };
2954
+ const others = raising.get(source.name) ?? new Set();
2955
+ raising.set(source.name, others.add(underWay));
2956
+ push({ t: "shown" });
2520
2957
  try {
2521
- await client.raiseWake({ source: "messenger", key: raised.key, text });
2958
+ underWay.number = (
2959
+ await client.raiseWake({ source: "messenger", key: raised.key, text })
2960
+ ).number;
2522
2961
  } catch (error) {
2962
+ others.delete(underWay);
2963
+ push({ t: "shown" });
2523
2964
  if (codeOf(error) !== "signal.too_many") {
2524
2965
  throw error;
2525
2966
  }
@@ -2537,7 +2978,8 @@ export function createRunner(options: RunnerOptions): Runner {
2537
2978
  }
2538
2979
  return true;
2539
2980
  },
2540
- kept: new Map(),
2981
+ kept,
2982
+ began: beganBy,
2541
2983
  log,
2542
2984
  stands: (state, why) => sourceStands(source.name, state, why),
2543
2985
  fetch: wakes.fetch,
@@ -2555,9 +2997,12 @@ export function createRunner(options: RunnerOptions): Runner {
2555
2997
  }),
2556
2998
  };
2557
2999
  sourceStands(source.name, "waiting");
3000
+ if (source.show !== undefined) {
3001
+ shows.set(source.name, { source, host, said: "", at: 0 });
3002
+ }
2558
3003
  let wait = timing.reconnectMs;
2559
3004
  while (!stopped) {
2560
- const began = options.now();
3005
+ const startedAt = options.now();
2561
3006
  try {
2562
3007
  await source.run(host, abort.signal);
2563
3008
  // Ended with no error, a connection the other side closed say: it listens no more
@@ -2572,7 +3017,7 @@ export function createRunner(options: RunnerOptions): Runner {
2572
3017
  }
2573
3018
  }
2574
3019
  wait =
2575
- options.now() - began >= SOURCE_HELD_MS
3020
+ options.now() - startedAt >= SOURCE_HELD_MS
2576
3021
  ? timing.reconnectMs
2577
3022
  : Math.min(wait * 2, MAX_SOURCE_PAUSE_MS);
2578
3023
  await pause(wait);
@@ -2620,10 +3065,13 @@ export function createRunner(options: RunnerOptions): Runner {
2620
3065
  const kept = await options.state.load().catch(() => undefined);
2621
3066
  delivered = kept?.delivered ?? 0;
2622
3067
  conversation = kept?.conversation;
3068
+ // A moment after now is none the conversation began at: held to the clock here, and
3069
+ // wherever a wake source asks.
3070
+ began = Math.min(kept?.began ?? 0, options.now());
2623
3071
  if (conversation !== undefined && kept?.driver !== undefined && kept.driver !== driver.name) {
2624
3072
  // Another product's conversation is nothing this one can go on with: asked to, it fails
2625
3073
  // at every turn until a supervisor resets it.
2626
- conversation = undefined;
3074
+ letGo();
2627
3075
  log(
2628
3076
  `the conversation kept was begun under the driver ${kept.driver}: a new one begins under ${driver.name}`,
2629
3077
  );
@@ -2648,11 +3096,16 @@ export function createRunner(options: RunnerOptions): Runner {
2648
3096
  tries: 1,
2649
3097
  until: known === undefined ? now + timing.coolDownMs : known + timing.resetMarginMs,
2650
3098
  };
2651
- stands = { state: "failed", reason: "usage_limit" };
3099
+ // Said as of when the limit was reached, which the runner before this one kept.
3100
+ stands = { state: "failed", reason: "usage_limit", at: kept.limited.since };
2652
3101
  log(
2653
3102
  `the product's usage limit stopped the agent before this runner started: tried again at ${new Date(waits.until).toISOString()}`,
2654
3103
  );
2655
3104
  }
3105
+ if ((kept?.began ?? 0) > began) {
3106
+ // Kept as it is taken, once everything else the file said is in place again.
3107
+ await persist();
3108
+ }
2656
3109
  probe = await driver.probe();
2657
3110
  const left =
2658
3111
  probe.models.length +
@@ -2683,7 +3136,7 @@ export function createRunner(options: RunnerOptions): Runner {
2683
3136
  delivered = Math.max(delivered, first.delivered);
2684
3137
  if (first.state === "stopped") {
2685
3138
  held = true;
2686
- stands = { state: "stopped", reason: "stop" };
3139
+ stands = { state: "stopped", reason: "stop", at: options.now() };
2687
3140
  }
2688
3141
  break;
2689
3142
  } catch (error) {
@@ -2765,6 +3218,9 @@ export function createRunner(options: RunnerOptions): Runner {
2765
3218
  if (session !== undefined) {
2766
3219
  await flush(session);
2767
3220
  }
3221
+ await keepKept();
3222
+ } else if (message.t === "shown") {
3223
+ // Looked at below, as after anything else.
2768
3224
  } else {
2769
3225
  // A timer counts no time the machine sleeps in, and rings late by as much: what
2770
3226
  // was to be handed over at a moment is looked for by the clock at every heartbeat.
@@ -2821,6 +3277,9 @@ export function createRunner(options: RunnerOptions): Runner {
2821
3277
  } catch (error) {
2822
3278
  log(`the runner could not do what it was about to (${codeOf(error)}); it goes on`);
2823
3279
  }
3280
+ // Whatever was done may have changed how the agent stands to whoever waits for it,
3281
+ // and so does the time that passed: looked at once a second at the least.
3282
+ showStatus();
2824
3283
  }
2825
3284
  } finally {
2826
3285
  stopped = true;
@@ -2829,12 +3288,18 @@ export function createRunner(options: RunnerOptions): Runner {
2829
3288
  for (const timer of timers) {
2830
3289
  clearTimeout(timer);
2831
3290
  }
3291
+ // What the wake sources showed of the agent is taken back while its session ends
3292
+ // (ADR-0170): a runner that is gone shows nothing.
3293
+ const shown = showLast();
2832
3294
  // A session ended for an update goes on, in the runner started after, as one a
2833
3295
  // supervisor restarted does: with the same conversation.
2834
3296
  await end(how === "stopped" ? "exited" : "restarted");
3297
+ await shown;
2835
3298
  abort.abort();
2836
3299
  await listening;
2837
3300
  await Promise.all(sourcing);
3301
+ // What the sources raised as they ended is kept too.
3302
+ await keepKept();
2838
3303
  log(
2839
3304
  how === "updated"
2840
3305
  ? "the runner ended, for the update"