teapot-coding-agent 0.37.138 → 0.37.139

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.
@@ -201,11 +201,29 @@ export class EventLog {
201
201
  await new Promise((res) => s.end(res));
202
202
  }
203
203
  }
204
+ /**
205
+ * Parse a whole log file, STREAMING it.
206
+ *
207
+ * It used to `readFile()` the entire file into one string and `.split("\n")` it, so a large session paid for three
208
+ * copies at once: the file's bytes, one string per line, and the parsed objects. MEASURED on the operator's server:
209
+ * `master rss 1034.4MB · heap 599.1MB` at the moment of a burst, back to `heap 248.2MB` a moment later — a transient
210
+ * allocation that GC could reclaim, not a leak.
211
+ *
212
+ * Reading line-by-line through a stream removes the first two copies: the only thing retained is the events, and the
213
+ * peak no longer scales with (file bytes × 3).
214
+ */
204
215
  export async function readEvents(filePath) {
216
+ const out = [];
205
217
  try {
206
- const { readFile } = await import("node:fs/promises");
207
- const out = [];
208
- for (const line of (await readFile(filePath, "utf8")).split("\n")) {
218
+ const [{ createReadStream }, { createInterface }] = await Promise.all([
219
+ import("node:fs"),
220
+ import("node:readline"),
221
+ ]);
222
+ const rl = createInterface({
223
+ input: createReadStream(filePath, { encoding: "utf8" }),
224
+ crlfDelay: Infinity,
225
+ });
226
+ for await (const line of rl) {
209
227
  if (!line.trim())
210
228
  continue;
211
229
  try {
@@ -1195,16 +1195,38 @@ export function buildApp(master) {
1195
1195
  return c.json({ ok: true, ...r });
1196
1196
  });
1197
1197
  const eventsCache = new Map();
1198
+ /**
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.
1204
+ */
1205
+ const EVENTS_CACHE_BUDGET = 250_000;
1206
+ let eventsCached = 0;
1198
1207
  async function readEventsCached(filePath) {
1199
1208
  try {
1200
1209
  const st = await fs.stat(filePath);
1201
1210
  const hit = eventsCache.get(filePath);
1202
- if (hit && hit.mtimeMs === st.mtimeMs && hit.size === st.size)
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);
1203
1215
  return hit.events;
1216
+ }
1204
1217
  const events = await readEvents(filePath);
1205
- if (eventsCache.size > 64)
1206
- eventsCache.clear();
1218
+ if (hit)
1219
+ eventsCached -= hit.events.length;
1207
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;
1208
1230
  return events;
1209
1231
  }
1210
1232
  catch {
@@ -1245,6 +1267,18 @@ export function buildApp(master) {
1245
1267
  const before = c.req.query("before");
1246
1268
  const after = c.req.query("after");
1247
1269
  if (after && !before && !branch) {
1270
+ /**
1271
+ * The incremental case must not read the whole file.
1272
+ *
1273
+ * `after=` is what a client sends to catch up after a reconnect, and it is almost always near the END of the log,
1274
+ * so the tail window answers it — reading 90k events to find one id was the burst path.
1275
+ */
1276
+ const tailFirst = await readEventsTail(filePath, Math.max(limit, 4_000));
1277
+ const inTail = tailFirst.events.findIndex((e) => e.id === after);
1278
+ if (inTail !== -1) {
1279
+ const events = tailFirst.events.slice(inTail + 1);
1280
+ return c.json({ events, total: tailFirst.total, appended: events.length > 0 });
1281
+ }
1248
1282
  const all = await readEventsCached(filePath);
1249
1283
  const idx = all.findIndex((e) => e.id === after);
1250
1284
  if (idx === -1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teapot-coding-agent",
3
- "version": "0.37.138",
3
+ "version": "0.37.139",
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",