@stratusagent/core 0.11.2 → 0.11.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.
package/dist/index.js CHANGED
@@ -1,6 +1,232 @@
1
+ /**
2
+ * The image formats a model request may carry. The list is the Messages
3
+ * API's, and it is exported so a channel deciding which of a message's
4
+ * attachments to fetch asks this rather than keeping its own copy — a
5
+ * format the API rejects is one the channel should never have downloaded.
6
+ */
7
+ export const IMAGE_ATTACHMENT_MEDIA_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
8
+ export const isImageAttachmentMediaType = (value) => typeof value === 'string' && IMAGE_ATTACHMENT_MEDIA_TYPES.includes(value);
9
+ /**
10
+ * The largest image a message may carry, in decoded bytes. The Messages
11
+ * API refuses anything over 5 MB per image, and a session is one JSON row
12
+ * that every later turn re-reads — an image is stored with the message it
13
+ * arrived on, so the cap is also what keeps a transcript readable.
14
+ */
15
+ export const IMAGE_ATTACHMENT_MAX_BYTES = 5 * 1024 * 1024;
16
+ /**
17
+ * The most image bytes one message may carry in total, decoded. The
18
+ * Messages API caps a request body at 32 MiB, and base64 costs a third
19
+ * again — so five images each just under the per-image cap pass one by one
20
+ * and still fail the turn together. 20 MiB decoded is 26.7 MiB encoded,
21
+ * leaving room for the text around it.
22
+ */
23
+ export const IMAGE_ATTACHMENTS_MAX_TOTAL_BYTES = 20 * 1024 * 1024;
24
+ /**
25
+ * The most images one request may carry, whatever they weigh. The Messages
26
+ * API accepts 100 per request, but past 20 it also shrinks what each may
27
+ * measure to 2000 pixels a side — and an image was accepted at up to
28
+ * `IMAGE_ATTACHMENT_MAX_DIMENSION` when it arrived. Twenty is the count at
29
+ * which every image already stored is still one the API takes.
30
+ */
31
+ export const IMAGE_ATTACHMENTS_MAX_REPLAY_COUNT = 20;
32
+ /**
33
+ * The most an image may measure on either side, in pixels. The Messages
34
+ * API refuses anything larger, and a large flat PNG compresses far below
35
+ * the byte cap — so bytes alone would let an image in that fails the turn
36
+ * it arrives on and, once stored, every turn after.
37
+ */
38
+ export const IMAGE_ATTACHMENT_MAX_DIMENSION = 8000;
39
+ /**
40
+ * An image's pixel size, read from its header without decoding it, or
41
+ * nothing when the bytes do not carry the header and trailer their media
42
+ * type promises — which a caller should treat as "not an image it can
43
+ * send". The trailer is what catches a download cut short: a PNG ends in
44
+ * IEND, a JPEG in an end-of-image marker, a GIF in a trailer byte, and a
45
+ * RIFF container declares its own length. It is not a decode — bytes that
46
+ * open and close correctly can still be refused by a model API, and a
47
+ * provider handles that when it happens (see `omitImage`) — but it is
48
+ * everything that can be checked without an image library, which core
49
+ * does not carry. One reader for the four accepted formats, so a channel
50
+ * deciding whether to keep a download asks this rather than trusting the
51
+ * uploader's metadata.
52
+ */
53
+ export const imageDimensions = (bytes, mediaType) => {
54
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
55
+ const ascii = (at, length) => String.fromCharCode(...bytes.subarray(at, at + length));
56
+ const last = bytes.length;
57
+ switch (mediaType) {
58
+ case 'image/png': {
59
+ if (bytes.length < 36 || ascii(1, 3) !== 'PNG' || ascii(12, 4) !== 'IHDR' || ascii(last - 8, 4) !== 'IEND') {
60
+ return undefined;
61
+ }
62
+ return { width: view.getUint32(16), height: view.getUint32(20) };
63
+ }
64
+ case 'image/gif': {
65
+ if (bytes.length < 11 || ascii(0, 4) !== 'GIF8' || bytes[last - 1] !== 0x3b) {
66
+ return undefined;
67
+ }
68
+ return { width: view.getUint16(6, true), height: view.getUint16(8, true) };
69
+ }
70
+ case 'image/webp': {
71
+ if (bytes.length < 30 || ascii(0, 4) !== 'RIFF' || ascii(8, 4) !== 'WEBP' || view.getUint32(4, true) !== last - 8) {
72
+ return undefined;
73
+ }
74
+ // Three container layouts, each keeping its size somewhere different.
75
+ const chunk = ascii(12, 4);
76
+ if (chunk === 'VP8 ') {
77
+ return { width: view.getUint16(26, true) & 0x3fff, height: view.getUint16(28, true) & 0x3fff };
78
+ }
79
+ if (chunk === 'VP8L') {
80
+ const [b0, b1, b2, b3] = [bytes[21], bytes[22], bytes[23], bytes[24]];
81
+ return {
82
+ width: 1 + (((b1 & 0x3f) << 8) | b0),
83
+ height: 1 + (((b3 & 0x0f) << 10) | (b2 << 2) | ((b1 & 0xc0) >> 6)),
84
+ };
85
+ }
86
+ if (chunk === 'VP8X') {
87
+ return {
88
+ width: 1 + (bytes[24] | (bytes[25] << 8) | (bytes[26] << 16)),
89
+ height: 1 + (bytes[27] | (bytes[28] << 8) | (bytes[29] << 16)),
90
+ };
91
+ }
92
+ return undefined;
93
+ }
94
+ case 'image/jpeg': {
95
+ if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8 || bytes[last - 2] !== 0xff || bytes[last - 1] !== 0xd9) {
96
+ return undefined;
97
+ }
98
+ // Walk the segments to the first start-of-frame, which carries the size.
99
+ let at = 2;
100
+ while (at + 9 <= bytes.length) {
101
+ if (bytes[at] !== 0xff) {
102
+ return undefined;
103
+ }
104
+ const marker = bytes[at + 1];
105
+ if (marker === 0xff) {
106
+ at += 1;
107
+ continue;
108
+ }
109
+ const startOfFrame = marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc;
110
+ if (startOfFrame) {
111
+ return { height: view.getUint16(at + 5), width: view.getUint16(at + 7) };
112
+ }
113
+ const standalone = marker === 0x01 || (marker >= 0xd0 && marker <= 0xd8);
114
+ at += standalone ? 2 : 2 + view.getUint16(at + 2);
115
+ }
116
+ return undefined;
117
+ }
118
+ default:
119
+ return undefined;
120
+ }
121
+ };
122
+ /** Decoded size of a base64 string, without decoding it. */
123
+ const base64DecodedBytes = (data) => Math.floor((data.length * 3) / 4) - (data.endsWith('==') ? 2 : data.endsWith('=') ? 1 : 0);
124
+ /**
125
+ * Which of a transcript's images still travel on a request. Every image a
126
+ * session ever received is stored with its message and replayed on every
127
+ * later turn, so a per-message budget alone is not one: two messages that
128
+ * each fit would together exceed the request limit on the turn after. The
129
+ * budget — bytes and a count, both API limits — is spent newest first,
130
+ * within a message as well as across them: the latest message always
131
+ * arrives whole, because a channel already holds one message to this same
132
+ * budget, and once an image does not fit, nothing older does either, so
133
+ * what the model sees is a contiguous recent window rather than a
134
+ * scatter. A provider
135
+ * that sends image bytes asks this and sends a note in place of the rest;
136
+ * the transcript itself is never trimmed.
137
+ *
138
+ * Membership is by identity: the set holds the very objects on the
139
+ * messages, so a provider walking them asks `has(image)`.
140
+ */
141
+ export const imagesWithinReplayBudget = (messages, budget = {}) => {
142
+ const kept = new Set();
143
+ let remaining = budget.bytes ?? IMAGE_ATTACHMENTS_MAX_TOTAL_BYTES;
144
+ const count = budget.count ?? IMAGE_ATTACHMENTS_MAX_REPLAY_COUNT;
145
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
146
+ const images = messages[index]?.images;
147
+ if (images === undefined) {
148
+ continue;
149
+ }
150
+ for (let position = images.length - 1; position >= 0; position -= 1) {
151
+ const image = images[position];
152
+ // Nothing left to send, and nothing to charge: an omitted image is
153
+ // always older than every kept one, so skipping it keeps the window
154
+ // contiguous.
155
+ if (image.omitted === true) {
156
+ continue;
157
+ }
158
+ const bytes = base64DecodedBytes(image.data);
159
+ if (bytes > remaining || kept.size >= count) {
160
+ return kept;
161
+ }
162
+ remaining -= bytes;
163
+ kept.add(image);
164
+ }
165
+ }
166
+ return kept;
167
+ };
168
+ /**
169
+ * Lets go of the bytes of every image outside the replay window, in
170
+ * place, keeping its name and the fact that it was there. The runner calls
171
+ * this as it stores a turn: the replay budget bounds what a provider
172
+ * sends, but a session is one JSON row that every turn re-reads and
173
+ * re-writes whole, and without this every image a thread ever received
174
+ * would stay in it — so anyone who can post to a channel could grow a row
175
+ * by the whole budget per message, forever. What the model can be shown is
176
+ * exactly what is kept, so nothing the trim removes could have been sent.
177
+ * Returns how many images it emptied.
178
+ */
179
+ export const omitImagesOutsideReplayBudget = (messages, budget = {}) => {
180
+ const kept = imagesWithinReplayBudget(messages, budget);
181
+ let omitted = 0;
182
+ for (const message of messages) {
183
+ for (const image of message.images ?? []) {
184
+ if (image.omitted === true || kept.has(image)) {
185
+ continue;
186
+ }
187
+ omitImage(image);
188
+ omitted += 1;
189
+ }
190
+ }
191
+ return omitted;
192
+ };
193
+ /**
194
+ * Lets go of one image's bytes in place, keeping the record that it was
195
+ * there. Besides the replay window, this is what a provider does to an
196
+ * image the model API refused: the header checks a channel runs are not a
197
+ * decode, so a file that opens and closes correctly can still be rejected
198
+ * — and because an image is stored before the provider is called, a
199
+ * rejection left alone would replay on every later turn of the session.
200
+ * Emptied in place on the session's own object, so the runner's next save
201
+ * carries it and the turn goes on with a note in the image's place.
202
+ */
203
+ export const omitImage = (image) => {
204
+ image.data = '';
205
+ image.omitted = true;
206
+ };
207
+ /**
208
+ * What stands in for an image `imagesWithinReplayBudget` left out — one
209
+ * wording, so every provider tells the model the same thing about the
210
+ * same gap.
211
+ */
212
+ export const droppedImageNote = (image) => `[An image attached here${image.name !== undefined ? ` (${image.name})` : ''} is no longer sent: this conversation's images have passed what one request can carry, and only the most recent are kept.]`;
213
+ export const LISTENS_MODES = ['mentions', 'thread', 'judge'];
214
+ export const isListensMode = (value) => typeof value === 'string' && LISTENS_MODES.includes(value);
1
215
  export class AgentRegistry {
2
216
  agents = new Map();
217
+ /**
218
+ * `*` in a `delegates` list means any agent on the roster, so an agent
219
+ * with that id could never be granted alone — `delegates: ['*']` would
220
+ * open the whole roster to reach it. The soul loader refuses the id;
221
+ * this refuses it for a definition built in code, since the wildcard's
222
+ * meaning is decided here, where the roster lives, and not by which
223
+ * path put the agent on it.
224
+ */
3
225
  register(agent) {
226
+ if (agent.id === '*') {
227
+ throw new Error('Invalid agent id: "*". * is the delegates wildcard (delegates: [\'*\'] means any agent on the roster), '
228
+ + 'so no agent may have it as an id — give this agent another id.');
229
+ }
4
230
  this.agents.set(agent.id, agent);
5
231
  return agent;
6
232
  }
@@ -64,6 +290,37 @@ export const leastTrusted = (...levels) => {
64
290
  }
65
291
  return lowest;
66
292
  };
293
+ /**
294
+ * The paths a tool result refers to as files for a channel to deliver: an
295
+ * ok result whose object output carries `file: string` or `files:
296
+ * string[]`. The one reading of that convention, shared by the channel
297
+ * that uploads them and the gateway that counts a turn which produced one
298
+ * as having said something — a chart in the thread is the last thing said
299
+ * as surely as a sentence is.
300
+ */
301
+ export const filePathsOf = (result) => {
302
+ if (!result.ok) {
303
+ return [];
304
+ }
305
+ const output = result.output;
306
+ if (typeof output !== 'object' || output === null || Array.isArray(output)) {
307
+ return [];
308
+ }
309
+ const paths = [];
310
+ const single = output.file;
311
+ if (typeof single === 'string' && single.length > 0) {
312
+ paths.push(single);
313
+ }
314
+ const many = output.files;
315
+ if (Array.isArray(many)) {
316
+ for (const entry of many) {
317
+ if (typeof entry === 'string' && entry.length > 0) {
318
+ paths.push(entry);
319
+ }
320
+ }
321
+ }
322
+ return paths;
323
+ };
67
324
  export const DEFAULT_TOOL_RISK = 'gated';
68
325
  /** The declared risk, or the fail-closed default for a tool that omits it. */
69
326
  export const resolveToolRisk = (tool) => tool?.risk ?? DEFAULT_TOOL_RISK;
@@ -146,6 +403,23 @@ const originForSession = (tool, session) => {
146
403
  * is a second answer to "what may this agent do".
147
404
  */
148
405
  export const matchesToolAllowlist = (toolName, allowlist) => matchesGlobbedAllowlist(toolName, allowlist, '.*');
406
+ /**
407
+ * Whether an allowlist entry and a declared namespace select any of the
408
+ * same tools — true when either sits under the other.
409
+ *
410
+ * Namespaces nest, so both shapes are real: `mcp.linear.*` sits under a
411
+ * declared `mcp.*`, and a granted `mcp.*` covers everything a narrower
412
+ * declared `mcp.linear.*` will register. Both arguments are passed where a
413
+ * tool name goes on purpose — entry and namespace are the same dotted-prefix
414
+ * glob, so this is the reading the allowlists already use rather than a
415
+ * second one.
416
+ *
417
+ * Exported because the dead-entry check is no longer the only caller:
418
+ * `stratus plugins` answers which agents a namespace reaches before any of
419
+ * its tools exist, and a second hand-rolled overlap would drift from this
420
+ * one immediately.
421
+ */
422
+ export const toolScopesOverlap = (entry, namespace) => matchesToolAllowlist(entry, [namespace]) || matchesToolAllowlist(namespace, [entry]);
149
423
  /**
150
424
  * Whether an agent's `skills:` allowlist permits a skill id.
151
425
  *
@@ -210,13 +484,7 @@ export const unmatchedToolAllowlist = (agent, registered, options = {}) => {
210
484
  // arrived yet, not a typo.
211
485
  //
212
486
  // Overlap either way excuses the entry, because either way the entry
213
- // selects tools that namespace will bring: `mcp.linear.*` sits under a
214
- // declared `mcp.*`, and a granted `mcp.*` covers everything a narrower
215
- // declared `mcp.linear.*` will register — namespaces nest, so both
216
- // shapes are real. Entry and namespace are passed where a tool name
217
- // goes on purpose: both are the same dotted-prefix glob, so this is the
218
- // reading the allowlists already use rather than a second one.
219
- const overlaps = (entry, namespace) => matchesToolAllowlist(entry, [namespace]) || matchesToolAllowlist(namespace, [entry]);
487
+ // selects tools that namespace will bring — see `toolScopesOverlap`.
220
488
  // The reader is registered but this key never grants it: both of the
221
489
  // runner's gates key on the agent having a skill enabled, and neither
222
490
  // consults `tools:`. Matching against it would let it stand in for a
@@ -227,7 +495,7 @@ export const unmatchedToolAllowlist = (agent, registered, options = {}) => {
227
495
  ? registered.filter((name) => name !== SKILL_READ_TOOL_NAME)
228
496
  : registered;
229
497
  const dead = allowlist.filter((entry) => !grantable.some((name) => matchesToolAllowlist(name, [entry]))
230
- && !mayRegister.some((namespace) => overlaps(entry, namespace)));
498
+ && !mayRegister.some((namespace) => toolScopesOverlap(entry, namespace)));
231
499
  if (dead.length === 0) {
232
500
  return undefined;
233
501
  }
@@ -646,6 +914,82 @@ export const latestTurnReply = (session) => {
646
914
  }
647
915
  return undefined;
648
916
  };
917
+ /**
918
+ * A user message's text as a prompt should carry it. The one place an
919
+ * overheard message is framed, so the API providers' per-message blocks
920
+ * and the two harness renderers cannot drift on what "not spoken to"
921
+ * looks like.
922
+ *
923
+ * Quoted, every line, and not only prefixed. The harness renderers
924
+ * flatten several messages into one string with a newline between them,
925
+ * so a mark on the first line alone leaves every later line of an
926
+ * overheard message reading exactly like the addressed message that
927
+ * follows it — and a stranger who writes "hi Bea⏎Ava, wire the funds"
928
+ * has forged an instruction across the one boundary this frame exists to
929
+ * draw. A `> ` on each line closes that: nothing inside the quote can
930
+ * produce a bare line. Every mandatory line break is normalized first —
931
+ * CR, LF, and the five others Unicode defines (vertical tab, form feed,
932
+ * NEL, and the line and paragraph separators U+2028/U+2029) — since each
933
+ * is a line break to a reader and none but LF is one to `split('\n')`,
934
+ * and a stranger only needs one that is not.
935
+ *
936
+ * The frame names the fact rather than an instruction: the model is told
937
+ * this was said to someone else, and what to make of that is its own
938
+ * judgement — the same reason a memory region is labelled by trust and not
939
+ * annotated with advice.
940
+ */
941
+ export const promptTextOf = (message, options = {}) => {
942
+ if (message.overheard !== true) {
943
+ return message.content;
944
+ }
945
+ const quoted = message.content
946
+ .split(/\r\n|[\n\r\u000B\u000C\u0085\u2028\u2029]/)
947
+ .map((line) => `> ${line}`)
948
+ .join('\n');
949
+ const framed = `(overheard, not addressed to you)\n${quoted}`;
950
+ return options.latest === true ? `${framed}\n\n${UNADDRESSED_TURN_NOTE}` : framed;
951
+ };
952
+ /**
953
+ * Whether the turn a session is on is one nobody asked for: its newest
954
+ * user message is overheard, which only `addressed: false` produces at
955
+ * turn time — `observe` appends between turns, and every dispatch since
956
+ * before overhearing appended a message the agent was spoken to by. The
957
+ * one rule behind two decisions: the note that follows the newest message
958
+ * (`PromptTextOptions.latest`), and a provider accepting an empty answer
959
+ * as the answer rather than as a broken endpoint.
960
+ */
961
+ export const isUnaddressedTurn = (session) => session.messages.findLast((message) => message.role === 'user')?.overheard === true;
962
+ /**
963
+ * A provider's way of saying a failed turn's prompt had already reached
964
+ * the model before the failure — a harness that recorded its session id
965
+ * and then died, most often. Marked on the error, off the type, the way
966
+ * `@stratusagent/providers` marks hosted-tool side effects: the runner
967
+ * cannot tell from the outside whether a harness holding its own history
968
+ * has this turn's message, and a turn it has must not be sent again.
969
+ */
970
+ const PROMPT_DELIVERED = Symbol.for('stratus.promptDelivered');
971
+ export const markPromptDelivered = (error) => {
972
+ if (typeof error === 'object' && error !== null) {
973
+ error[PROMPT_DELIVERED] = true;
974
+ }
975
+ return error;
976
+ };
977
+ export const promptWasDelivered = (error) => typeof error === 'object' && error !== null
978
+ && error[PROMPT_DELIVERED] === true;
979
+ /**
980
+ * What a turn nobody asked for is told, after the message it ends on.
981
+ *
982
+ * The load-bearing sentence of reading the room, and the one that cannot
983
+ * be specified into correctness: a model asked "should you answer?" says
984
+ * yes far more often than a person would, so the note leans on silence
985
+ * hard, names the few things worth breaking it for, and says what an
986
+ * answer must not be — an acknowledgement is the interruption it exists
987
+ * to prevent. In the message and not the system prompt, because it has
988
+ * to reach a harness that holds its own history and takes only the newest
989
+ * message, and because a rule read next to the thing it applies to is
990
+ * followed more often than one read an hour ago.
991
+ */
992
+ export const UNADDRESSED_TURN_NOTE = 'Nobody said that to you, and nobody is waiting on you. Reply only if you have something these people would want from you and do not have — an answer to a question left open, a correction to something wrong, a thing you were asked to watch for. Otherwise reply with nothing at all: no acknowledgement, no summary, no offer to help. Saying nothing is usually the right answer here.';
649
993
  /** Reads the checkpoint off a session, if it is parked. */
650
994
  export const readPendingApproval = (session) => {
651
995
  const raw = session.metadata?.[PENDING_APPROVAL_METADATA_KEY];
@@ -720,6 +1064,117 @@ export class InMemorySessionStore {
720
1064
  .map((session) => session.id);
721
1065
  }
722
1066
  }
1067
+ // ---- contribution kinds a plugin registers besides tools ------------------
1068
+ /**
1069
+ * The providers the host builds itself. Here, in the dependency-free
1070
+ * package, because two packages that never import each other need the one
1071
+ * list: `@stratusagent/state` resolves a selection against it, and
1072
+ * `@stratusagent/plugins` refuses a manifest that declares one of these as
1073
+ * a provider of its own — `provider: anthropic` in a soul has to keep
1074
+ * meaning the first-party adapter, and a plugin that could shadow it would
1075
+ * be a plugin that could receive every key the operator stored for it.
1076
+ */
1077
+ export const BUILTIN_PROVIDER_NAMES = ['demo', 'openai', 'anthropic', 'codex'];
1078
+ /**
1079
+ * The names a trusted config's `executor` and `memoryStore` keys mean the
1080
+ * built-ins by — the local child-process executor and the file store —
1081
+ * and so the names a plugin may not register: the host's selection
1082
+ * special-cases them and would never look a contribution up under them.
1083
+ * Here for the same reason the provider list is: the manifest parser and
1084
+ * the selection logic live in packages that never import each other.
1085
+ */
1086
+ export const BUILTIN_EXECUTOR_NAME = 'local';
1087
+ export const BUILTIN_MEMORY_STORE_NAME = 'file';
1088
+ /**
1089
+ * A name-keyed registry for one contribution kind — the counterpart of
1090
+ * `ToolRegistry` for providers, memory stores, and executors. Like it, a
1091
+ * bare `Map.set`: it keeps no provenance and refuses nothing, and a plugin
1092
+ * never touches one directly. The manifest-bound view in
1093
+ * `@stratusagent/plugins` is what refuses an undeclared name or a
1094
+ * collision; this is where a whole plugin's contributions land once it has
1095
+ * loaded.
1096
+ */
1097
+ export class ContributionRegistry {
1098
+ entries = new Map();
1099
+ register(name, value) {
1100
+ this.entries.set(name, value);
1101
+ }
1102
+ get(name) {
1103
+ return this.entries.get(name);
1104
+ }
1105
+ has(name) {
1106
+ return this.entries.has(name);
1107
+ }
1108
+ names() {
1109
+ return [...this.entries.keys()];
1110
+ }
1111
+ list() {
1112
+ return [...this.entries.values()];
1113
+ }
1114
+ }
1115
+ /** The (agent, kind) key a channel registration claims. */
1116
+ export const channelClaimKey = (kind, agentId) => `${kind}:${agentId}`;
1117
+ /**
1118
+ * The registry for contributed channels. Keyed by (agent, kind) rather than
1119
+ * by name, because that is what collides — see `ChannelContribution` — and
1120
+ * listed as adapters, because that is what a host starts. Same discipline
1121
+ * as the other registries: no refusal here, the view refuses.
1122
+ */
1123
+ export class ChannelRegistry {
1124
+ contributions = [];
1125
+ /** Undefined marks a claim the host made for an adapter it wired itself. */
1126
+ claims = new Map();
1127
+ register(contribution) {
1128
+ this.contributions.push(contribution);
1129
+ for (const agentId of contribution.agents) {
1130
+ this.claims.set(channelClaimKey(contribution.adapter.name, agentId), contribution);
1131
+ }
1132
+ }
1133
+ /**
1134
+ * Claim (agent, kind) pairs the host carries itself, so a plugin
1135
+ * registering the same kind for the same agent collides with the host's
1136
+ * adapter exactly as it would with another plugin's — two adapters on
1137
+ * one agent's Slack would each answer every message.
1138
+ */
1139
+ claim(kind, agentIds) {
1140
+ for (const agentId of agentIds) {
1141
+ if (!this.claims.has(channelClaimKey(kind, agentId))) {
1142
+ this.claims.set(channelClaimKey(kind, agentId), undefined);
1143
+ }
1144
+ }
1145
+ }
1146
+ /** Whether some adapter — the host's or a plugin's — already carries this agent on this kind. */
1147
+ claimed(kind, agentId) {
1148
+ return this.claims.has(channelClaimKey(kind, agentId));
1149
+ }
1150
+ list() {
1151
+ return [...this.contributions];
1152
+ }
1153
+ }
1154
+ /**
1155
+ * A memory store that resolves which store serves an agent **per call**.
1156
+ *
1157
+ * The seam a host selects a contributed store through. The runner and the
1158
+ * memory tools are built before any plugin has loaded, so the store they
1159
+ * hold has to be one that can change its mind — and an agent-by-agent
1160
+ * answer is what keeps a contributed store from being process-global.
1161
+ * `reassertTrust` is forwarded only when the chosen store has it: a store
1162
+ * that omitted it cannot be re-asserted, and this must not claim otherwise.
1163
+ */
1164
+ export const createRoutedMemoryStore = (storeFor) => ({
1165
+ append: (agentId, content, metadata, provenance) => storeFor(agentId).append(agentId, content, metadata, provenance),
1166
+ list: (agentId, options) => storeFor(agentId).list(agentId, options),
1167
+ search: (agentId, query, limit) => storeFor(agentId).search(agentId, query, limit),
1168
+ forget: (agentId, entryId) => storeFor(agentId).forget(agentId, entryId),
1169
+ audit: (agentId) => storeFor(agentId).audit(agentId),
1170
+ async reassertTrust(agentId, entryId, trust) {
1171
+ const store = storeFor(agentId);
1172
+ if (!store.reassertTrust) {
1173
+ return false;
1174
+ }
1175
+ return store.reassertTrust(agentId, entryId, trust);
1176
+ },
1177
+ });
723
1178
  export class PluginRegistry {
724
1179
  plugins = [];
725
1180
  register(plugin) {
@@ -1034,16 +1489,27 @@ export const memoryRegionHeading = (trust) => {
1034
1489
  * message started producing it. An entry with no label renders in the
1035
1490
  * `unknown` region — never as the agent's own conclusion.
1036
1491
  */
1492
+ /**
1493
+ * Unicode's `Bidi_Control` property as a character-class fragment: the
1494
+ * marks, the Arabic letter mark, the embeddings and overrides, and the
1495
+ * isolates. Exported so every escaper spells out the same set — text that
1496
+ * reads one way to a person and another to the model is the same trick
1497
+ * whether it arrives in a memory entry, a Slack name, or a tool description.
1498
+ */
1499
+ export const BIDI_CONTROL_CHARACTERS = '\\u061c\\u200e\\u200f\\u202a-\\u202e\\u2066-\\u2069';
1500
+ const CONTROL_CHARACTERS = new RegExp(`[\\u0000-\\u001f\\u007f-\\u009f\\u2028\\u2029${BIDI_CONTROL_CHARACTERS}]`, 'g');
1037
1501
  /**
1038
1502
  * `text` on one line, every control character spelled out the way JSON
1039
- * does (`\n`, `\u001b`), C1 controls and the Unicode line separators
1040
- * included. The memory block and `stratus memory list` both frame an entry
1041
- * by its line — `- ` under a region heading, an indent under an id — and a
1042
- * fact holding a newline could otherwise start a line of its own that reads
1043
- * as a heading, the trusted region's heading among them. Escaped rather
1044
- * than dropped, so what is shown is still what is stored.
1045
- */
1046
- export const escapeControlCharacters = (text) => text.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, (character) => {
1503
+ * does (`\n`, `\u001b`), C1 controls, the Unicode line separators and
1504
+ * the bidi controls included. The memory block and `stratus memory list`
1505
+ * both frame an entry by its line — `- ` under a region heading, an indent
1506
+ * under an id — and a fact holding a newline could otherwise start a line
1507
+ * of its own that reads as a heading, the trusted region's heading among
1508
+ * them; a right-to-left override would make the line read differently to
1509
+ * a person than to the model. Escaped rather than dropped, so what is
1510
+ * shown is still what is stored.
1511
+ */
1512
+ export const escapeControlCharacters = (text) => text.replace(CONTROL_CHARACTERS, (character) => {
1047
1513
  switch (character) {
1048
1514
  case '\n': return '\\n';
1049
1515
  case '\r': return '\\r';
@@ -1258,6 +1724,22 @@ export class AllowAllApprovalPolicy {
1258
1724
  return true;
1259
1725
  }
1260
1726
  }
1727
+ /**
1728
+ * The `images` field of a user message, or nothing: a message that carried
1729
+ * none has no field, so a transcript written before images existed and one
1730
+ * written after read the same.
1731
+ */
1732
+ const userImages = (images) => images !== undefined && images.length > 0 ? { images } : {};
1733
+ /**
1734
+ * See `RunInput.addressed`: an image reaches the model as pixels, before
1735
+ * and outside the text that frames what was said as somebody else's, so
1736
+ * a turn nobody asked for takes none.
1737
+ */
1738
+ const refuseUnaddressedImages = (input) => {
1739
+ if (input.addressed === false && input.images !== undefined && input.images.length > 0) {
1740
+ throw new Error('A turn nobody asked for cannot carry images: an image enters the prompt ahead of the frame that marks the message as somebody else\'s. Name the attachment in the message instead.');
1741
+ }
1742
+ };
1261
1743
  /**
1262
1744
  * Thrown when a run is stopped by its abort signal. The session ends up
1263
1745
  * `failed` with this error's message as `lastError`, so an aborted turn is
@@ -1306,10 +1788,12 @@ export class AgentRunner {
1306
1788
  maxTurns;
1307
1789
  streaming;
1308
1790
  options;
1791
+ imageReplayBudget;
1309
1792
  constructor(options) {
1310
1793
  this.options = options;
1311
1794
  this.bus = options.bus ?? new EventBus();
1312
1795
  this.store = options.store ?? new InMemorySessionStore();
1796
+ this.imageReplayBudget = options.imageReplayBudget;
1313
1797
  this.tools = options.tools ?? new ToolRegistry();
1314
1798
  this.executor = options.executor ?? new DefaultExecutor();
1315
1799
  this.approvals = options.approvals ?? new AllowAllApprovalPolicy();
@@ -1332,23 +1816,28 @@ export class AgentRunner {
1332
1816
  await this.plugins.loadAll({ bus: this.bus, tools: this.tools });
1333
1817
  }
1334
1818
  async run(input) {
1819
+ refuseUnaddressedImages(input);
1335
1820
  // The sender's trust is the turn's, not the session's: it is read here
1336
1821
  // and kept out of the persisted metadata, where a later reader would
1337
1822
  // take the first sender's label for every turn that follows.
1338
1823
  const { [SENDER_TRUST_METADATA_KEY]: _sender, ...persisted } = input.metadata ?? {};
1339
1824
  const senderTrust = senderTrustOf(input.metadata);
1825
+ // The first stored turn is held to the window like every later one:
1826
+ // a one-shot session is a row too.
1827
+ const opening = {
1828
+ id: `${input.sessionId}:user:1`,
1829
+ role: 'user',
1830
+ content: input.userMessage,
1831
+ createdAt: new Date().toISOString(),
1832
+ ...userImages(input.images),
1833
+ ...(input.addressed === false ? { overheard: true } : {}),
1834
+ };
1835
+ omitImagesOutsideReplayBudget([opening], this.imageReplayBudget);
1340
1836
  const sessionInput = {
1341
1837
  id: input.sessionId,
1342
1838
  agent: input.agent,
1343
1839
  status: 'running',
1344
- messages: [
1345
- {
1346
- id: `${input.sessionId}:user:1`,
1347
- role: 'user',
1348
- content: input.userMessage,
1349
- createdAt: new Date().toISOString(),
1350
- },
1351
- ],
1840
+ messages: [opening],
1352
1841
  // Labelled from the first write, whatever the dispatching surface
1353
1842
  // put in `metadata` under this key: the label is the runner's to
1354
1843
  // write, and a fresh session starts at the top of the lattice lowered
@@ -1399,6 +1888,7 @@ export class AgentRunner {
1399
1888
  }
1400
1889
  }
1401
1890
  async resume(input) {
1891
+ refuseUnaddressedImages(input);
1402
1892
  const session = await this.store.get(input.sessionId);
1403
1893
  if (!session) {
1404
1894
  throw new Error(`Session not found: ${input.sessionId}`);
@@ -1428,7 +1918,15 @@ export class AgentRunner {
1428
1918
  role: 'user',
1429
1919
  content: input.userMessage,
1430
1920
  createdAt: new Date().toISOString(),
1921
+ ...userImages(input.images),
1922
+ // Marked like a message `observe` appended, because it is one: the
1923
+ // difference is only that a turn runs on it, and every renderer
1924
+ // frames it from the same mark.
1925
+ ...(input.addressed === false ? { overheard: true } : {}),
1431
1926
  });
1927
+ // Before the save below: the row that carries this turn is the row
1928
+ // that stops carrying the pixels nothing can send any more.
1929
+ omitImagesOutsideReplayBudget(session.messages, this.imageReplayBudget);
1432
1930
  // The sender is evaluated on EVERY turn, from this turn's metadata: a
1433
1931
  // thread keys one session for everyone in it, and an unauthorized
1434
1932
  // member's message lowers the session to `unknown` and leaves it there
@@ -1444,6 +1942,48 @@ export class AgentRunner {
1444
1942
  await this.bus.emit({ type: 'session.updated', sessionId: working.id, status: working.status });
1445
1943
  return this.executeTurns(working, input.signal);
1446
1944
  }
1945
+ /**
1946
+ * A message reaching the session with no turn run on it: something said
1947
+ * in a conversation the agent is in, to somebody else. Appended and
1948
+ * saved, so the next turn the agent does take has it in hand; no
1949
+ * provider call, no reply, no status change, and its own event rather
1950
+ * than `session.updated`, since nothing is running.
1951
+ *
1952
+ * Refused while a turn is in flight. A parked turn's transcript ends in
1953
+ * a tool call awaiting its result, and a user message spliced in ahead
1954
+ * of that result is a wire-format violation on the API path — the one
1955
+ * shape `reconcileInterruptedToolCalls` exists to repair, and repairing
1956
+ * it here would close a call a human is still deciding on. A host that
1957
+ * serializes writes per session (the gateway's chain) only ever sees
1958
+ * this for a turn parked across a restart, and says so rather than
1959
+ * dropping the message silently.
1960
+ */
1961
+ async observe(input) {
1962
+ const session = await this.store.get(input.sessionId);
1963
+ if (!session) {
1964
+ throw new Error(`Session not found: ${input.sessionId}`);
1965
+ }
1966
+ if (session.status === 'running' || session.status === 'pending_approval') {
1967
+ throw new Error(`Session ${input.sessionId} has a turn in flight (${session.status}); a message cannot be overheard into it until the turn has finished.`);
1968
+ }
1969
+ session.messages.push({
1970
+ id: `${session.id}:user:${session.messages.length + 1}`,
1971
+ role: 'user',
1972
+ content: input.message,
1973
+ createdAt: new Date().toISOString(),
1974
+ overheard: true,
1975
+ });
1976
+ // The speaker is judged like any sender: their words are in the
1977
+ // transcript from here on, whether or not they were talking to the
1978
+ // agent — which is exactly the case the label exists for.
1979
+ await this.labelLegacySession(session);
1980
+ await this.taint(session, senderTrustOf(input.metadata), 'sender');
1981
+ await this.store.save(session);
1982
+ const stored = await this.store.get(session.id);
1983
+ const working = stored ?? session;
1984
+ await this.bus.emit({ type: 'session.observed', sessionId: working.id, agentId: working.agent.id });
1985
+ return working;
1986
+ }
1447
1987
  /**
1448
1988
  * Appends a synthetic failed result directly after every tool call that
1449
1989
  * has none — the durable trace of a turn interrupted between the call's
@@ -1707,6 +2247,23 @@ export class AgentRunner {
1707
2247
  toolCalls: [part.call],
1708
2248
  });
1709
2249
  }
2250
+ if (response.parts.length === 0) {
2251
+ // Silence, recorded. Only a turn nobody asked for ends this way —
2252
+ // every provider refuses an empty answer on one somebody did —
2253
+ // and the decision has to leave a mark: a harness holding its own
2254
+ // history is sent every user message since the agent last spoke
2255
+ // (`latestUserMessagePrompt`), and without an assistant message
2256
+ // here the message this turn already judged would go again on
2257
+ // the next one, and again on the one after that. Empty text is
2258
+ // no reply (`latestTurnReply`) and no speaking (`lastSpokeAt`);
2259
+ // it is the boundary of a turn that happened.
2260
+ session.messages.push({
2261
+ id: `${session.id}:assistant:${session.messages.length + 1}`,
2262
+ role: 'assistant',
2263
+ content: '',
2264
+ createdAt: new Date().toISOString(),
2265
+ });
2266
+ }
1710
2267
  const sawToolCall = calls.length > 0;
1711
2268
  await this.store.save(session);
1712
2269
  await this.bus.emit({ type: 'provider.response', sessionId: session.id, parts: response.parts });
@@ -1754,6 +2311,26 @@ export class AgentRunner {
1754
2311
  : caught)
1755
2312
  : caught;
1756
2313
  const lastError = error instanceof Error ? error.message : String(error);
2314
+ // Read off what the provider threw, not the normalized error: an
2315
+ // abort replaces it with a fresh `RunAbortedError`, and a harness
2316
+ // cancelled mid-turn — the watchdog's doing, most often — has the
2317
+ // prompt just the same.
2318
+ if (promptWasDelivered(caught) && isUnaddressedTurn(session)) {
2319
+ // A turn nobody asked for whose prompt the harness took before it
2320
+ // failed: the boundary goes in as if it had ended in silence, or
2321
+ // `latestUserMessagePrompt` would send the harness this message
2322
+ // again next time — the same double send the empty-answer
2323
+ // boundary above closes, on the failure path. Only for such a
2324
+ // turn: an addressed one that failed is retried by its sender
2325
+ // with a new message, which is then the newest and the only one
2326
+ // sent, as it always was.
2327
+ session.messages.push({
2328
+ id: `${session.id}:assistant:${session.messages.length + 1}`,
2329
+ role: 'assistant',
2330
+ content: '',
2331
+ createdAt: new Date().toISOString(),
2332
+ });
2333
+ }
1757
2334
  session.status = 'failed';
1758
2335
  session.lastError = lastError;
1759
2336
  await this.store.save(session);