teapot-coding-agent 0.37.161 → 0.37.165

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.
@@ -1284,7 +1284,18 @@ export class Agent {
1284
1284
  autoCompact: this.opts.autoCompact,
1285
1285
  };
1286
1286
  }
1287
- enqueuePrompt(text, source = "user", images) {
1287
+ enqueuePrompt(text, source = "user", images,
1288
+ /**
1289
+ * The id the CLIENT already drew the row with.
1290
+ *
1291
+ * A prompt used to get one id from the browser (`@p…`, the optimistic row) and another from the server (`p…`, the
1292
+ * log row, the queue entry, the delivery note). Two ids for one prompt means the row has to be re-created when the
1293
+ * second one arrives, and every "is it still queued?" test has to guess which id it is looking at — MEASURED as
1294
+ * four different symptoms of the same thing: a prompt sent to an idle agent blinking "queued…", the FIRST prompt of
1295
+ * a queued run showing a timestamp instead of "queued…", and adding a prompt behind a queue briefly rendering as
1296
+ * settled. The client now proposes the id and this is it.
1297
+ */
1298
+ clientId) {
1288
1299
  if (this.parkedByTool) {
1289
1300
  this.toolAbort.abort();
1290
1301
  this.toolAbort = new AbortController();
@@ -1305,9 +1316,12 @@ export class Agent {
1305
1316
  * client — no row, no count — which is the remaining half of "harnessからのメッセージにqueuedがついてない問題が
1306
1317
  * v0.37.160 でもあった".
1307
1318
  */
1308
- const promptId = source === "user"
1309
- ? `u${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`
1310
- : `h${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
1319
+ const proposed = String(clientId ?? "").trim();
1320
+ const usable = /^[A-Za-z0-9@_-]{1,64}$/.test(proposed) ? proposed : "";
1321
+ const promptId = usable ||
1322
+ (source === "user"
1323
+ ? `u${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`
1324
+ : `h${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`);
1311
1325
  const entry = { source, text, images, id: promptId };
1312
1326
  this.pendingPrompts.push(entry);
1313
1327
  void this.ensureReady()
@@ -238,6 +238,133 @@ export async function readEvents(filePath) {
238
238
  return [];
239
239
  }
240
240
  }
241
+ /**
242
+ * A bounded page of the log, read by streaming — nothing is retained after the call.
243
+ *
244
+ * The whole-file cache this replaces held every parsed event of every session the UI had opened: MEASURED on the
245
+ * operator's data, two sessions of ~100k events each retained **221MB of heap** for a page the UI shows 400 rows of.
246
+ * `readEventsTail` already answers the newest page and the `after=` catch-up from the end of the file, so the only
247
+ * remaining readers are "the page before this id" (scrolling up) and the branch filter; both are answered here with a
248
+ * ring buffer of `limit` events, so the memory cost is the page, not the session.
249
+ */
250
+ export async function readEventsWindow(filePath, opts) {
251
+ const limit = Math.max(0, opts.limit);
252
+ const empty = { events: [], total: 0, found: false };
253
+ if (limit <= 0)
254
+ return empty;
255
+ const accept = (e) => !opts.branch || e.branch === opts.branch;
256
+ const ring = [];
257
+ const push = (e) => {
258
+ ring.push(e);
259
+ if (ring.length > limit)
260
+ ring.shift();
261
+ };
262
+ try {
263
+ const [{ createReadStream }, { createInterface }] = await Promise.all([
264
+ import("node:fs"),
265
+ import("node:readline"),
266
+ ]);
267
+ const rl = createInterface({
268
+ input: createReadStream(filePath, { encoding: "utf8" }),
269
+ crlfDelay: Infinity,
270
+ });
271
+ let seen = 0;
272
+ let collecting = opts.after === undefined;
273
+ let found = opts.after === undefined;
274
+ for await (const line of rl) {
275
+ if (!line.trim())
276
+ continue;
277
+ let e;
278
+ try {
279
+ e = JSON.parse(line);
280
+ }
281
+ catch {
282
+ continue;
283
+ }
284
+ if (!accept(e))
285
+ continue;
286
+ if (!collecting) {
287
+ if (e.id === opts.after) {
288
+ collecting = true;
289
+ found = true;
290
+ seen++;
291
+ continue;
292
+ }
293
+ seen++;
294
+ continue;
295
+ }
296
+ if (opts.before !== undefined && e.id === opts.before) {
297
+ return { events: ring.slice(-limit), total: seen, found: true };
298
+ }
299
+ push(e);
300
+ seen++;
301
+ if (opts.after !== undefined && ring.length >= limit) {
302
+ // the count still has to be complete: `total` drives the client's "is there more history" decision
303
+ for await (const rest of rl) {
304
+ if (!rest.trim())
305
+ continue;
306
+ try {
307
+ if (accept(JSON.parse(rest)))
308
+ seen++;
309
+ }
310
+ catch { }
311
+ }
312
+ return { events: ring.slice(-limit), total: seen, found };
313
+ }
314
+ }
315
+ return { events: opts.before !== undefined ? [] : ring.slice(-limit), total: seen, found };
316
+ }
317
+ catch {
318
+ return empty;
319
+ }
320
+ }
321
+ /** Branch counts without holding the log: the endpoint only needs `{branch, events}` pairs. */
322
+ export async function branchCounts(filePath) {
323
+ const out = new Map();
324
+ try {
325
+ const [{ createReadStream }, { createInterface }] = await Promise.all([
326
+ import("node:fs"),
327
+ import("node:readline"),
328
+ ]);
329
+ const rl = createInterface({
330
+ input: createReadStream(filePath, { encoding: "utf8" }),
331
+ crlfDelay: Infinity,
332
+ });
333
+ for await (const line of rl) {
334
+ if (!line.trim())
335
+ continue;
336
+ let e;
337
+ try {
338
+ e = JSON.parse(line);
339
+ }
340
+ catch {
341
+ continue;
342
+ }
343
+ const b = out.get(e.branch) ?? { events: 0 };
344
+ b.events++;
345
+ if (e.type === "fork") {
346
+ const created = String(e.data?.newBranch ?? "");
347
+ if (created && created !== e.branch) {
348
+ const nb = out.get(created) ?? { events: 0 };
349
+ nb.forkedFrom = e.data;
350
+ out.set(created, nb);
351
+ b.events--;
352
+ if (b.events > 0)
353
+ out.set(e.branch, b);
354
+ else
355
+ out.delete(e.branch);
356
+ continue;
357
+ }
358
+ b.forkedFrom = e.data;
359
+ }
360
+ out.set(e.branch, b);
361
+ }
362
+ return out;
363
+ }
364
+ catch {
365
+ return out;
366
+ }
367
+ }
241
368
  const TAIL_MIN_BYTES = 64 * 1024;
242
369
  export async function readEventsTail(filePath, limit) {
243
370
  const empty = { events: [], total: 0, bytesScanned: 0, truncated: false };
@@ -33,7 +33,7 @@ async function listModels(baseUrl, apiKey) {
33
33
  }
34
34
  import path from "node:path";
35
35
  import { bus } from "../bus.js";
36
- import { readEvents, readEventsTail, filterByBranch } from "../log/events.js";
36
+ import { branchCounts, readEvents, readEventsTail, readEventsWindow, filterByBranch } from "../log/events.js";
37
37
  import { mcp } from "../agent/mcp-registry.js";
38
38
  const IMAGE_EXT = new Set(["png", "jpg", "jpeg", "gif", "webp", "svg", "bmp", "ico", "avif"]);
39
39
  const VIDEO_EXT = new Set(["mp4", "webm", "mov", "mkv", "avi", "m4v"]);
@@ -870,7 +870,7 @@ export function buildApp(master) {
870
870
  });
871
871
  const text = body.text ?? "";
872
872
  const src = body.source === "harness" ? "harness" : "user";
873
- const promptId = a.enqueuePrompt(text || "(see attached image)", src, images.length ? images : undefined);
873
+ const promptId = a.enqueuePrompt(text || "(see attached image)", src, images.length ? images : undefined, body.clientId);
874
874
  if (body.start !== false && a.status !== "running")
875
875
  a.start("prompt");
876
876
  return c.json({ ok: true, promptId, queued: a.snapshot().pendingPrompts });
@@ -1194,45 +1194,12 @@ export function buildApp(master) {
1194
1194
  const r = await a.fork(body.fromEvent ?? null);
1195
1195
  return c.json({ ok: true, ...r });
1196
1196
  });
1197
- const eventsCache = new Map();
1198
1197
  /**
1199
- * The whole-file cache is bounded by EVENTS, not by file count.
1200
- *
1201
- * `if (eventsCache.size > 64) eventsCache.clear()` counted FILES, so eight sessions with 90k events each were all
1202
- * retained — hundreds of MB the process never gives back, since nothing evicts them until the 65th file appears.
1203
- * A parsed event is roughly a few hundred bytes, so the budget below is a few tens of MB.
1198
+ * No whole-file cache. MEASURED: keeping the parsed log of every session the UI opened retained **221MB of heap** for
1199
+ * two sessions of ~100k events — "master rss 812MB · heap 352.2MB" with 1316 messages in memory. The pages the client
1200
+ * actually asks for are answered by streaming (`readEventsTail` for the newest page, `readEventsWindow` for the page
1201
+ * before an id, `branchCounts` for the branch list), so the cost is the page instead of the session.
1204
1202
  */
1205
- const EVENTS_CACHE_BUDGET = 250_000;
1206
- let eventsCached = 0;
1207
- async function readEventsCached(filePath) {
1208
- try {
1209
- const st = await fs.stat(filePath);
1210
- const hit = eventsCache.get(filePath);
1211
- if (hit && hit.mtimeMs === st.mtimeMs && hit.size === st.size) {
1212
- // refresh insertion order so the eviction below stays LRU
1213
- eventsCache.delete(filePath);
1214
- eventsCache.set(filePath, hit);
1215
- return hit.events;
1216
- }
1217
- const events = await readEvents(filePath);
1218
- if (hit)
1219
- eventsCached -= hit.events.length;
1220
- eventsCache.set(filePath, { mtimeMs: st.mtimeMs, size: st.size, events });
1221
- eventsCached += events.length;
1222
- for (const [key, entry] of eventsCache) {
1223
- if (eventsCached <= EVENTS_CACHE_BUDGET && eventsCache.size <= 8)
1224
- break;
1225
- eventsCache.delete(key);
1226
- eventsCached -= entry.events.length;
1227
- }
1228
- if (eventsCached < 0)
1229
- eventsCached = 0;
1230
- return events;
1231
- }
1232
- catch {
1233
- return readEvents(filePath);
1234
- }
1235
- }
1236
1203
  app.get("/api/agents/:id/events", async (c) => {
1237
1204
  const a = master.agents.get(c.req.param("id"));
1238
1205
  if (!a) {
@@ -1279,28 +1246,31 @@ export function buildApp(master) {
1279
1246
  const events = tailFirst.events.slice(inTail + 1);
1280
1247
  return c.json({ events, total: tailFirst.total, appended: events.length > 0 });
1281
1248
  }
1282
- const all = await readEventsCached(filePath);
1283
- const idx = all.findIndex((e) => e.id === after);
1284
- if (idx === -1) {
1249
+ const win = await readEventsWindow(filePath, { after, limit });
1250
+ if (!win.found) {
1285
1251
  const tail = await readEventsTail(filePath, limit);
1286
1252
  return c.json({ events: tail.events, total: tail.total, partial: tail.truncated, gap: true });
1287
1253
  }
1288
- const events = all.slice(idx + 1);
1289
- return c.json({ events, total: all.length, appended: events.length > 0 });
1254
+ return c.json({ events: win.events, total: win.total, appended: win.events.length > 0 });
1290
1255
  }
1291
1256
  if (!before && !branch) {
1292
1257
  const tail = await readEventsTail(filePath, limit);
1293
1258
  return c.json({ events: tail.events, total: tail.total, partial: tail.truncated });
1294
1259
  }
1295
- let events = await readEventsCached(filePath);
1296
- if (branch)
1297
- events = filterByBranch(events, branch, c.req.query("lineage") === "true");
1298
- if (before) {
1299
- const idx = events.findIndex((e) => e.id === before);
1300
- events = idx === -1 ? [] : events.slice(0, idx);
1260
+ /**
1261
+ * `lineage=true` is the one shape that still needs the whole log (it walks parents to the fork point), so it reads
1262
+ * the file for the request and lets it go — a transient peak instead of a permanent one.
1263
+ */
1264
+ if (branch && c.req.query("lineage") === "true") {
1265
+ let events = filterByBranch(await readEvents(filePath), branch, true);
1266
+ if (before) {
1267
+ const idx = events.findIndex((e) => e.id === before);
1268
+ events = idx === -1 ? [] : events.slice(0, idx);
1269
+ }
1301
1270
  return c.json({ events: events.slice(-limit), total: events.length });
1302
1271
  }
1303
- return c.json({ events: events.slice(-limit), total: events.length });
1272
+ const win = await readEventsWindow(filePath, { before, limit, branch });
1273
+ return c.json({ events: win.events, total: win.total });
1304
1274
  });
1305
1275
  app.get("/api/sessions", (c) => c.json({ sessions: master.allSessions() }));
1306
1276
  app.get("/api/agents/:id/sessions", (c) => {
@@ -1315,28 +1285,10 @@ export function buildApp(master) {
1315
1285
  const a = master.agents.get(c.req.param("id"));
1316
1286
  if (!a)
1317
1287
  return notFound(c, "agent", c.req.param("id"));
1318
- const events = await readEventsCached(a.log.filePath);
1319
- const branches = new Map();
1320
- for (const e of events) {
1321
- const b = branches.get(e.branch) ?? { branch: e.branch, events: 0 };
1322
- b.events++;
1323
- branches.set(e.branch, b);
1324
- if (e.type === "fork") {
1325
- const created = String(e.data?.newBranch ?? "");
1326
- if (created && created !== e.branch) {
1327
- const nb = branches.get(created) ?? { branch: created, events: 0 };
1328
- nb.forkedFrom = e.data;
1329
- branches.set(created, nb);
1330
- b.events--;
1331
- if (b.events <= 0)
1332
- branches.delete(e.branch);
1333
- }
1334
- else {
1335
- b.forkedFrom = e.data;
1336
- }
1337
- }
1338
- }
1339
- return c.json({ branches: [...branches.values()] });
1288
+ const counts = await branchCounts(a.log.filePath);
1289
+ return c.json({
1290
+ branches: [...counts].map(([branch, c2]) => ({ branch, events: c2.events, forkedFrom: c2.forkedFrom })),
1291
+ });
1340
1292
  });
1341
1293
  app.get("/api/metrics", (c) => c.json(master.metrics()));
1342
1294
  const notFound = (c, what, id) => c.json({ error: `${what} not found: ${id || "(no id given)"}` }, 404);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teapot-coding-agent",
3
- "version": "0.37.161",
3
+ "version": "0.37.165",
4
4
  "description": "A lightweight, always-on multi-agent harness for AI coding agents",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-or-later",