teapot-coding-agent 0.37.160 → 0.37.164

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.
@@ -1255,7 +1255,17 @@ export class Agent {
1255
1255
  ...(this.compactPhase ? { compacting: this.compactPhase } : {}),
1256
1256
  },
1257
1257
  pendingPrompts: this.pendingPrompts.length,
1258
- pendingPromptIds: this.pendingPrompts.map((p) => p.id).filter((x) => !!x).slice(0, 64),
1258
+ /**
1259
+ * The CANCELLABLE set — user prompts only (#141).
1260
+ *
1261
+ * Harness prompts are named too now (they need a row and a count), but they must not appear here: the ✕ cancel is
1262
+ * offered for what this list contains, and a queued harness message is not the operator's to withdraw.
1263
+ */
1264
+ pendingPromptIds: this.pendingPrompts
1265
+ .filter((p) => p.source === "user")
1266
+ .map((p) => p.id)
1267
+ .filter((x) => !!x)
1268
+ .slice(0, 64),
1259
1269
  pendingPromptQueue: this.pendingPrompts
1260
1270
  .filter((p) => !!p.id)
1261
1271
  .slice(0, 64)
@@ -1287,8 +1297,18 @@ export class Agent {
1287
1297
  this.activityChars = 0;
1288
1298
  this.turnsSinceProgress = 0;
1289
1299
  }
1290
- const promptId = source === "user" ? `u${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}` : "";
1291
- const entry = { source, text, images, ...(promptId ? { id: promptId } : {}) };
1300
+ /**
1301
+ * EVERY queued prompt gets an id, harness ones included.
1302
+ *
1303
+ * Only user prompts were given one, and the snapshot publishes just the prompts it can name
1304
+ * (`pendingPromptQueue`/`pendingPromptIds` filter on `id`). A queued harness prompt was therefore invisible to the
1305
+ * client — no row, no count — which is the remaining half of "harnessからのメッセージにqueuedがついてない問題が
1306
+ * v0.37.160 でもあった".
1307
+ */
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)}`;
1311
+ const entry = { source, text, images, id: promptId };
1292
1312
  this.pendingPrompts.push(entry);
1293
1313
  void this.ensureReady()
1294
1314
  .then(async () => {
@@ -2777,7 +2797,7 @@ export class Agent {
2777
2797
  : `The auditor returned no specific gaps. Re-read the verification contract above and re-run it yourself, item by item, before finishing again:\n\n${this.goal.verify?.slice(0, 2000) || this.goal.text.slice(0, 2000)}`;
2778
2798
  const retryText = `[harness] The completion audit REJECTED this finish. Address these gaps, then finish again with goalComplete=true:\n\n${gaps}`;
2779
2799
  if (this.consecutiveAuditRejects < (this.opts.maxAuditRejects ?? 3)) {
2780
- this.pendingPrompts.push({ source: "harness", text: retryText });
2800
+ this.pendingPrompts.push({ source: "harness", text: retryText, id: `h${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}` });
2781
2801
  await this.log.append("prompt", this.currentSession, this.currentBranch, {
2782
2802
  source: "harness",
2783
2803
  text: retryText,
@@ -2819,7 +2839,7 @@ export class Agent {
2819
2839
  `AUDIT, not a finding about your work. Before finishing again, state plainly and briefly what ` +
2820
2840
  `you verified for each item of the contract, so the auditor has something to judge.\n\n` +
2821
2841
  `${this.goal.verify?.slice(0, 2000) || this.goal.text.slice(0, 2000)}`;
2822
- this.pendingPrompts.push({ source: "harness", text });
2842
+ this.pendingPrompts.push({ source: "harness", text, id: `h${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}` });
2823
2843
  void this.log
2824
2844
  .append("prompt", this.currentSession, this.currentBranch, { source: "harness", text })
2825
2845
  .catch(() => { });
@@ -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"]);
@@ -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.160",
3
+ "version": "0.37.164",
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",