Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

perf(bots): mtime-cached manifests + tail-read journals

Salman Qureshi49173530d478
diff --git a/src/bots/journal.ts b/src/bots/journal.ts
index e4433f37..4c4d26c2 100644
--- a/src/bots/journal.ts
+++ b/src/bots/journal.ts
@@ -1,4 +1,4 @@
-import { appendFileSync, existsSync, readdirSync, readFileSync, renameSync, rmSync, statSync } from 'node:fs';
+import { appendFileSync, closeSync, existsSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { BOT_JOURNAL_FILENAME, assertValidBotId } from './store.js';
import type { BotRunRecord } from './types.js';
@@ -36,8 +36,7 @@ export class BotJournal {
assertValidBotId(botId);
const file = join(this.dir, BOT_JOURNAL_FILENAME);
if (!existsSync(file)) return [];
- const lines = readLines(file);
- return lines.slice(-limit);
+ return readTailRecords(file, limit);
}
counts(botId: string): { total: number; completed: number; failed: number; bytes: number } {
@@ -72,6 +71,50 @@ export class BotJournal {
}
}
+/** Tail-read chunk: one journal line is ~300 bytes, so 64KB ≈ 200 records —
+ * far more than any viewer asks for, without scanning a 5MB rotated file. */
+const READ_CHUNK_BYTES = 64 * 1024;
+
+/**
+ * Read the newest `limit` records from ONE file without reading it whole:
+ * a 64KB tail usually suffices; double the window until enough records (or
+ * the whole file) are covered. Used by read() — counts() still scans fully.
+ */
+function readTailRecords(file: string, limit: number): BotRunRecord[] {
+ let size: number;
+ try {
+ size = statSync(file).size;
+ } catch {
+ return [];
+ }
+ if (size === 0) return [];
+ let start = Math.max(0, size - READ_CHUNK_BYTES);
+ while (true) {
+ const records: BotRunRecord[] = [];
+ const fd = openSync(file, 'r');
+ try {
+ const buf = Buffer.alloc(size - start);
+ readSync(fd, buf, 0, buf.length, start);
+ const text = buf.toString('utf-8');
+ // A mid-file window starts inside some line — drop that partial line.
+ const lines = (start === 0 ? text : text.slice(text.indexOf('\n') + 1)).split('\n');
+ for (const line of lines) {
+ const trimmed = line.trim();
+ if (!trimmed) continue;
+ try {
+ records.push(JSON.parse(trimmed) as BotRunRecord);
+ } catch { /* skip corrupt line — never break reads on one bad row */ }
+ }
+ } catch {
+ return [];
+ } finally {
+ closeSync(fd);
+ }
+ if (records.length >= limit || start === 0) return records.slice(-limit);
+ start = Math.max(0, size - (size - start) * 2); // double the window
+ }
+}
+
function readLines(file: string): BotRunRecord[] {
const out: BotRunRecord[] = [];
try {
diff --git a/src/bots/store.test.ts b/src/bots/store.test.ts
index 41bb4ef4..48618fd1 100644
--- a/src/bots/store.test.ts
+++ b/src/bots/store.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { BotStore, assertValidBotId, validateBotManifest, BOT_PERSONA_FILENAME, BOT_PERMISSIONS_FILENAME } from './store.js';
@@ -96,8 +96,11 @@ describe('BotStore', () => {
expect(p.tools?.allow).toEqual(['read_file', 'shell']);
});
- it('update mutates and persists', () => {
+ it('update mutates and persists', async () => {
store.create({ id: 'alpha', name: 'Alpha', manifest: { enabled: true } });
+ // updatedAt/createdAt are ms-resolution ISO stamps — nudge the clock so
+ // the two writes never land in the same millisecond (otherwise flaky).
+ await new Promise(r => setTimeout(r, 5));
const updated = store.update('alpha', m => { m.enabled = false; m.description = 'paused'; });
expect(updated.enabled).toBe(false);
expect((store.get('alpha') as BotManifest).enabled).toBe(false);
@@ -260,4 +263,53 @@ describe('BotJournal', () => {
expect(recent.every(r => r.runId)).toBe(true);
expect(recent).toHaveLength(2);
});
-});
\ No newline at end of file
+
+ it('tail-reads correctly across the 64KB window boundary', () => {
+ const journal = new BotJournal(dir);
+ // Wide records push the newest entries beyond a single 64KB tail window —
+ // the reader must grow the window, not silently miss records.
+ const wide = { ...record('pad'), summary: 'x'.repeat(2000) };
+ for (let i = 0; i < 100; i++) journal.append(wide);
+ journal.append(record('last-2'));
+ journal.append(record('last-1'));
+ expect(journal.read('alpha', 3).map(r => r.runId)).toEqual(['pad', 'last-2', 'last-1']);
+ expect(journal.read('alpha', 1).map(r => r.runId)).toEqual(['last-1']);
+ });
+});
+describe('BotStore manifest cache (mtime-validated)', () => {
+ let root: string;
+ let store: BotStore;
+
+ beforeEach(() => {
+ root = mkdtempSync(join(tmpdir(), 'mercury-bot-cache-'));
+ store = new BotStore(join(root, 'bots'));
+ });
+
+ afterEach(() => {
+ rmSync(root, { recursive: true, force: true });
+ });
+
+ it('reflects same-millisecond saves (write path refreshes the cache directly)', () => {
+ const manifest = store.create({ id: 'fast', name: 'Fast' });
+ manifest.name = 'Renamed';
+ store.save(manifest); // same millisecond as the create — mtime alone could alias
+ expect(store.get('fast')?.name).toBe('Renamed');
+ expect(store.list().find(b => b.id === 'fast')?.name).toBe('Renamed');
+ });
+
+ it('picks up externally edited bot.yaml (mtime invalidation)', () => {
+ store.create({ id: 'ext', name: 'Before' });
+ const file = join(root, 'bots', 'ext', 'bot.yaml');
+ writeFileSync(file, readFileSync(file, 'utf-8').replace('Before', 'After'));
+ utimesSync(file, new Date(), new Date()); // force a distinct mtime — no same-ms aliasing
+ expect(store.get('ext')?.name).toBe('After');
+ });
+
+ it('delete clears the cache (get → null afterwards)', () => {
+ store.create({ id: 'gone', name: 'Gone' });
+ expect(store.get('gone')).not.toBeNull();
+ store.delete('gone');
+ expect(store.get('gone')).toBeNull();
+ expect(store.list()).toHaveLength(0);
+ });
+});
diff --git a/src/bots/store.ts b/src/bots/store.ts
index 1189f32d..4f458b6e 100644
--- a/src/bots/store.ts
+++ b/src/bots/store.ts
@@ -137,6 +137,8 @@ function atomicWrite(filePath: string, content: string, mode: number = 0o600): v
*/
export class BotStore {
readonly botsRoot: string;
+ /** mtime-validated manifest cache (see get()) — write paths keep it fresh. */
+ private manifestCache = new Map<string, { mtimeMs: number; manifest: BotManifest }>();
constructor(botsRoot?: string) {
this.botsRoot = resolve(botsRoot ?? join(getMercuryHome(), 'bots'));
@@ -186,12 +188,26 @@ export class BotStore {
get(id: string): BotManifest | null {
const file = join(this.botDir(id), BOT_MANIFEST_FILENAME);
- if (!existsSync(file)) return null;
+ if (!existsSync(file)) {
+ this.manifestCache.delete(id);
+ return null;
+ }
+ // mtime-validated cache: getStatusSummaries() runs on the TUI's 2s status
+ // poller, per chat message (system-prompt section), and per web API hit —
+ // re-reading + re-parsing every bot.yaml each time is pure event-loop
+ // tax. statSync per call keeps externally-edited manifests fresh; write
+ // paths also refresh the entry directly (same-millisecond writes would
+ // otherwise alias to the stale mtime).
+ const mtimeMs = statSync(file).mtimeMs;
+ const cached = this.manifestCache.get(id);
+ if (cached && cached.mtimeMs === mtimeMs) return cached.manifest;
const raw = parseYaml(readFileSync(file, 'utf-8')) as Partial<BotManifest>;
if (!raw || raw.id !== id) {
throw new Error(`Bot manifest for "${id}" is missing or has a mismatched id`);
}
- return normalizeBotManifest(raw);
+ const manifest = normalizeBotManifest(raw);
+ this.manifestCache.set(id, { mtimeMs, manifest });
+ return manifest;
}
exists(id: string): boolean {
@@ -253,6 +269,9 @@ export class BotStore {
}
manifest.updatedAt = new Date().toISOString();
atomicWrite(join(dir, BOT_MANIFEST_FILENAME), stringifyYaml(manifest));
+ // Same-millisecond writes would make the mtime check in get() alias to
+ // the stale entry — refresh it directly instead of relying on mtime.
+ this.manifestCache.set(manifest.id, { mtimeMs: statSync(join(dir, BOT_MANIFEST_FILENAME)).mtimeMs, manifest });
}
update(id: string, mutator: (m: BotManifest) => void): BotManifest {
@@ -272,6 +291,7 @@ export class BotStore {
const dir = this.botDir(id);
if (existsSync(dir)) {
rmSync(dir, { recursive: true, force: true });
+ this.manifestCache.delete(id);
logger.info({ botId: id }, 'Bot profile deleted');
}
}