my-durable 0.0.73 → 0.0.74

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.
@@ -491,7 +491,7 @@ export interface SessionSearchMatch {
491
491
  dbPath: string;
492
492
  compaction?: CompactionSettings;
493
493
  where: Array<"name" | "cwd" | "model" | "transcript">;
494
- /** Present when scope=all found transcript text (up to a few per session, newest first). */
494
+ /** Present when scope=all found transcript text (up to 12 per session, newest first — R19 cap). */
495
495
  hits?: TranscriptSearchHit[];
496
496
  }
497
497
 
@@ -99,6 +99,12 @@ export function applyMessageChange(message: WireAssistantMessage, change: WireMe
99
99
  if (change.type === "message") return structuredClone(change.message);
100
100
  const content = [...message.content] as WireAssistantBlock[];
101
101
  const i = change.contentIndex;
102
+ // R19 (run-5 h10, leader-probed): the wire number was indexed raw — any
103
+ // index > content.length creates holes that crash toTranscript's for-of
104
+ // ("Cannot read properties of undefined"), which the SSE follow loop turns
105
+ // into a disconnect/reconnect churn loop; a huge index also allocates a
106
+ // length-2^32 sparse array. Validate like setPath: drop the whole change.
107
+ if (!Number.isInteger(i) || i < 0 || i > content.length) return message;
102
108
  if (change.type === "block" || change.type.endsWith("_start")) {
103
109
  content[i] = structuredClone((change as { block: WireAssistantBlock }).block);
104
110
  } else if (change.type === "text_delta") {
@@ -170,6 +176,7 @@ export class AgentModel extends Emitter {
170
176
 
171
177
  private reset(snapshot: WireSnapshotEvent) {
172
178
  this.entries = new Map(snapshot.entries.map((entry) => [entry.id, entry]));
179
+ this.capEntries(); // a hostile snapshot is as unbounded as a hostile stream
173
180
  this.live = snapshot.generation?.message ? structuredClone(snapshot.generation.message) : undefined;
174
181
  this.tools = new Map(
175
182
  snapshot.tools.map((slot) => [
@@ -178,7 +185,7 @@ export class AgentModel extends Emitter {
178
185
  ]),
179
186
  );
180
187
  // Nested calls of running tools: they have no assistant entry, so the snapshot is the only source on attach.
181
- for (const slot of snapshot.nestedTools) {
188
+ for (const slot of snapshot.nestedTools.slice(-2_000)) {
182
189
  const existing = this.tools.get(slot.callId);
183
190
  this.tools.set(slot.callId, {
184
191
  name: slot.name,
@@ -194,11 +201,41 @@ export class AgentModel extends Emitter {
194
201
  this.state.agent = snapshot.agent;
195
202
  this.state.usage = snapshot.usage;
196
203
  this.state.retry = snapshot.generation?.retry;
197
- this.state.compactions = snapshot.compactions.map((c) => c.taskId);
204
+ this.state.compactions = snapshot.compactions.map((c) => c.taskId).slice(-64);
198
205
  this.state.compactionFailure = snapshot.compactionFailure;
199
206
  this.state.ready = true;
200
207
  }
201
208
 
209
+ /**
210
+ * R19 (run-5 h10, leader-probed): the events stream had NO aggregate cap — a
211
+ * hostile device streaming unique ids under the 16 MiB per-event cap grew this
212
+ * Map linearly (10k entries → +11 MB, no ceiling) until the operator's tab/TUI
213
+ * OOMs. Cold-review MAJOR 3: the cap first lived only in commit(), so the
214
+ * snapshot and tool_execution_end paths bypassed it — it is enforced in ONE
215
+ * place, on every mutation.
216
+ */
217
+ /**
218
+ * R19 cold-review follow-up: `tools` is the SDK's other unbounded map — 30k
219
+ * `tool_execution_start` events grew it to 30k slots AND cost ~140 s of CPU
220
+ * (rebuild → toTranscript is O(tools) per frame, so the stream is quadratic).
221
+ * Same hostile-peer class as the entries cap, so the same bound.
222
+ */
223
+ private capTools() {
224
+ while (this.tools.size > 2_000) {
225
+ const oldest = this.tools.keys().next().value;
226
+ if (oldest === undefined) break;
227
+ this.tools.delete(oldest);
228
+ }
229
+ }
230
+
231
+ private capEntries() {
232
+ while (this.entries.size > 5_000) {
233
+ const oldest = this.entries.keys().next().value;
234
+ if (oldest === undefined) break;
235
+ this.entries.delete(oldest);
236
+ }
237
+ }
238
+
202
239
  /** Store a committed entry. Resets (sent as message_end) and compactions start a new context head. */
203
240
  private commit(entry: WireEntry) {
204
241
  this.entries.set(entry.id, entry);
@@ -206,6 +243,7 @@ export class AgentModel extends Emitter {
206
243
  const head = entry.head ?? entry.id;
207
244
  for (const id of this.entries.keys()) if (id < head) this.entries.delete(id);
208
245
  }
246
+ this.capEntries();
209
247
  }
210
248
 
211
249
  private event(event: WireAgentEvent) {
@@ -242,6 +280,7 @@ export class AgentModel extends Emitter {
242
280
  case "entry_appended":
243
281
  return this.commit(event.entry);
244
282
  case "tool_execution_start":
283
+ this.capTools(); // R19: bound before the insert adds one more
245
284
  this.tools.set(event.toolCallId, {
246
285
  name: event.toolName,
247
286
  status: "running",
@@ -271,6 +310,8 @@ export class AgentModel extends Emitter {
271
310
  // A model-issued call renders from its result entry; drop the live slot so it is never duplicated.
272
311
  this.tools.delete(event.toolCallId);
273
312
  this.entries.set(event.entry.id, event.entry);
313
+ this.capEntries();
314
+ this.capTools();
274
315
  return;
275
316
  }
276
317
  if (event.result) {
@@ -299,6 +340,8 @@ export class AgentModel extends Emitter {
299
340
  return;
300
341
  case "compaction_start":
301
342
  if (!this.state.compactions.includes(event.taskId)) this.state.compactions.push(event.taskId);
343
+ // R19: bound alongside the entries cap — fresh taskIds grow unbounded.
344
+ if (this.state.compactions.length > 64) this.state.compactions = this.state.compactions.slice(-64);
302
345
  return;
303
346
  case "compaction_end":
304
347
  this.state.compactions = this.state.compactions.filter((id) => id !== event.taskId);
@@ -117,6 +117,26 @@ const KEYS: [string, string][] = [
117
117
  ];
118
118
 
119
119
  /** Returns false when `text` is not a command, so the caller sends it to the agent. */
120
+ /**
121
+ * R19 (run-5 h2 + cold-review): every field of a wire row is untrusted at the
122
+ * terminal boundary. sanitizeTerminal strips escape sequences but keeps
123
+ * newlines and tabs, so single-line chrome collapses whitespace too — otherwise
124
+ * a wire id can split the row it belongs to (visual spoof, no escape bytes).
125
+ */
126
+ export function oneLine(text: string): string {
127
+ return sanitizeTerminal(text).replace(/\s+/g, " ");
128
+ }
129
+
130
+ /** R19 (run-5 h2): every field of a TranscriptSearchHit is relay-borne — the
131
+ * numeric `entry` included. The compose site used to interpolate it raw, so a
132
+ * hostile hub/device could ride an OSC/CSI payload in a string-typed `entry`
133
+ * straight into the terminal (pi-tui writes Panel lines verbatim). One
134
+ * chokepoint, one test.
135
+ */
136
+ export function findHitLine(name: string, hit: { agent: string; entry: number; text: string }): string {
137
+ return ` ${oneLine(name)}:${oneLine(hit.agent)} #${oneLine(String(hit.entry))} — ${oneLine(hit.text.slice(0, 140))}`;
138
+ }
139
+
120
140
  export async function runCommand(app: App, text: string): Promise<boolean> {
121
141
  const match = text.match(/^\/(\S+)\s*([\s\S]*)$/);
122
142
  const command = COMMANDS.find((c) => c.name === match?.[1]);
@@ -238,7 +258,7 @@ export async function runCommand(app: App, text: string): Promise<boolean> {
238
258
  app.overlay(
239
259
  new Panel(() => {
240
260
  const lines = [
241
- c.bold(c.accent(`"${sanitizeTerminal(arg)}" — ${result.matches.length} session${result.matches.length === 1 ? "" : "s"}`)),
261
+ c.bold(c.accent(`"${sanitizeTerminal(arg)}" — ${sanitizeTerminal(String(result.matches.length))} session${result.matches.length === 1 ? "" : "s"}`)),
242
262
  "",
243
263
  ];
244
264
  if (!result.matches.length) lines.push(c.muted("No matches (names, folders, models or transcripts)."));
@@ -247,15 +267,11 @@ export async function runCommand(app: App, text: string): Promise<boolean> {
247
267
  `${c.secondary(sanitizeTerminal(m.name))} ${c.muted(sanitizeTerminal(m.cwd))}${m.where.includes("transcript") ? " " + c.warn("◍ transcript") : ""}`,
248
268
  );
249
269
  for (const hit of (m.hits ?? []).slice(0, 3))
250
- lines.push(
251
- c.muted(
252
- ` ${sanitizeTerminal(m.name)}:${sanitizeTerminal(hit.agent)} #${hit.entry} — ${sanitizeTerminal(hit.text.slice(0, 140))}`,
253
- ),
254
- );
270
+ lines.push(c.muted(findHitLine(m.name, hit)));
255
271
  }
256
- if (result.matches.length > shown.length) lines.push(c.muted(`… ${result.matches.length - shown.length} more`));
272
+ if (result.matches.length > shown.length) lines.push(c.muted(`… ${sanitizeTerminal(String(result.matches.length - shown.length))} more`));
257
273
  if (result.scanned?.truncated)
258
- lines.push(c.muted(`scan budget reached (${result.scanned.entries} entries) — refine the query`));
274
+ lines.push(c.muted(`scan budget reached (${sanitizeTerminal(String(result.scanned.entries))} entries) — refine the query`));
259
275
  lines.push("", c.muted("esc close"));
260
276
  return lines;
261
277
  }, () => app.closeOverlay()),
@@ -271,7 +287,13 @@ export async function runCommand(app: App, text: string): Promise<boolean> {
271
287
  if (!tasks.length) lines.push(c.muted("No live tasks."));
272
288
  for (const t of tasks)
273
289
  lines.push(
274
- `${t.background ? c.secondary("◌ bg") : c.warn("● fg")} #${t.id} ${sanitizeTerminal(t.kind)} ${c.muted(sanitizeTerminal(`agent #${t.conversationId} │ ${t.state.status}${t.state.phase ? `/${t.state.phase}` : ""}`))}`,
290
+ // R19 cold-review MAJOR 4: task ids are wire-borne too — a hostile
291
+ // relay can type them as strings carrying escape sequences (the
292
+ // same class as the /find `entry` finding). Every scalar goes
293
+ // through the chokepoint.
294
+ // sanitizeTerminal keeps \n/\t, so a one-line row also collapses
295
+ // whitespace — a wire id must not split the row it belongs to.
296
+ `${t.background ? c.secondary("◌ bg") : c.warn("● fg")} #${oneLine(String(t.id))} ${oneLine(String(t.kind))} ${c.muted(`agent #${oneLine(String(t.conversationId))} │ ${oneLine(String(t.state.status))}${t.state.phase ? `/${oneLine(String(t.state.phase))}` : ""}`)}`,
275
297
  );
276
298
  lines.push("", c.muted("esc close"));
277
299
  return lines;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "my-durable",
3
- "version": "0.0.73",
3
+ "version": "0.0.74",
4
4
  "description": "Durable Pi — the durable agent network: hub web UI, device backend daemon, TUI admin and the shared SDK, packaged as one runnable monorepo install",
5
5
  "license": "MIT",
6
6
  "repository": {