an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
fix(memory): back the mine loop off while the palace is quarantining
Chaitanya Girib1cfa2fb36b6
diff --git a/src/main/memory.ts b/src/main/memory.tsindex 6446f0de..6ed1b774 100644--- a/src/main/memory.ts+++ b/src/main/memory.ts@@ -17,7 +17,7 @@ import { existsSync, statSync, readdirSync, readFileSync, writeFileSync, rmSyncimport { join } from 'node:path';import { spawn, spawnSync } from 'node:child_process';import { ensureKilled } from './procKill';-import { quarantineDirsToReap } from './palaceReap';+import { quarantineDirsToReap, quarantineStampMs, nextMineDelayMs } from './palaceReap';/** Non-memory files `mempalace mine` must not ingest: the Claude Code hooks* config (a large JSON blob that swamps the wake-up digest), the cursor, raw@@ -73,6 +73,10 @@ export interface MemoryStatus {// written now is searchable within ten minutes rather than three, which no one// is waiting on. `reapPalace` handles the copies that still get made.const MINE_INTERVAL_MS = 600_000;+// Ceiling for the quarantine backoff below. Low on purpose: a memory is not+// searchable until it has been mined, and the reaper already handles the disk,+// so there is nothing here worth making recall half an hour stale for.+const MINE_BACKOFF_MAX_MS = 1_800_000;const MINE_TIMEOUT_MS = 10 * 60_000; // hard cap per mine (first run downloads the embedding model)/** mempalace's device "auto" picks the CoreML execution provider on Apple* Silicon, and CoreML runs the quantized embeddinggemma ONNX graph partially@@ -88,6 +92,12 @@ const MEMPALACE_DEVICE = process.platform === 'darwin' ? 'cpu' : undefined;export class MemoryManager {private binCache: string | null | undefined;private mineTimer: NodeJS.Timeout | null = null;+ private mineStopped = false;+ /** Current gap between mine passes. Widens while the palace is quarantining. */+ private mineDelayMs = MINE_INTERVAL_MS;+ /** Newest quarantine stamp seen so far, so a LATER one means the palace+ * quarantined again. A count would be useless: the reaper deletes them. */+ private lastQuarantineTs = 0;private initStarted = false;/** True while a mineNow() pass is in flight — serializes palace writers. */private mining = false;@@ -203,7 +213,8 @@ export class MemoryManager {}stop(): void {- if (this.mineTimer) { clearInterval(this.mineTimer); this.mineTimer = null; }+ this.mineStopped = true;+ if (this.mineTimer) { clearTimeout(this.mineTimer); this.mineTimer = null; }}/**@@ -229,10 +240,23 @@ export class MemoryManager {return this.status();}+ /** Self-scheduling rather than `setInterval`, so the gap can widen when the+ * palace is quarantining and snap back the moment it stops. */private startMineLoop(): void {if (this.mineTimer) return;- this.mineNow();- this.mineTimer = setInterval(() => this.mineNow(), MINE_INTERVAL_MS);+ const tick = () => {+ void this.mineNow().finally(() => {+ if (this.mineStopped) return;+ this.mineTimer = setTimeout(tick, this.mineDelayMs);+ this.mineTimer.unref?.();+ });+ };+ // Armed synchronously. `mineTimer` is the "is the loop running" signal that+ // `refresh()` reports on right after `start()`, and setting it only once the+ // first mine resolves would report the loop as dead for a whole pass —+ // which is exactly the re-arm-after-install case that has its own test.+ this.mineTimer = setTimeout(tick, 0);+ this.mineTimer.unref?.();}// — mining (store) —@@ -265,7 +289,12 @@ export class MemoryManager {} finally {this.mining = false;}- this.reapPalace(); // every pass above may have left another copy behind+ // Every pass above may have left another copy behind, and whether it did+ // decides how long we wait before the next one.+ const quarantined = this.reapPalace();+ this.mineDelayMs = nextMineDelayMs(+ this.mineDelayMs, MINE_INTERVAL_MS, MINE_BACKOFF_MAX_MS, quarantined+ );}/**@@ -281,19 +310,32 @@ export class MemoryManager {* remove, must never take down the mine loop — this is disk hygiene, not a* correctness path.*/- private reapPalace(): void {+ private reapPalace(): boolean {const palace = this.palacePath();- if (!palace || !existsSync(palace)) return;+ if (!palace || !existsSync(palace)) return false;let names: string[];- try { names = readdirSync(palace); } catch { return; }+ try { names = readdirSync(palace); } catch { return false; }++ let newest = 0;+ for (const name of names) {+ const ts = quarantineStampMs(name);+ if (ts !== null && ts > newest) newest = ts;+ }+ // The boot sweep runs before the first mine precisely so it can seed this:+ // otherwise a palace that arrives with a backlog would read as "just+ // quarantined" and back the loop off before it has mined anything.+ const fresh = this.lastQuarantineTs > 0 && newest > this.lastQuarantineTs;+ if (newest > this.lastQuarantineTs) this.lastQuarantineTs = newest;+const doomed = quarantineDirsToReap(names.map((name) => ({ name })), Date.now());- if (!doomed.length) return;+ if (!doomed.length) return fresh;let removed = 0;for (const name of doomed) {try { rmSync(join(palace, name), { recursive: true, force: true }); removed += 1; }catch { /* locked, gone, or not ours — leave it and try again next pass */ }}if (removed) console.log(`[memory] reaped ${removed} quarantined palace segment(s)`);+ return fresh;}private mineAgent(agentDir: string, id: string): Promise<void> {diff --git a/src/main/palaceReap.ts b/src/main/palaceReap.tsindex 23649a80..859df5ac 100644--- a/src/main/palaceReap.ts+++ b/src/main/palaceReap.ts@@ -98,3 +98,28 @@ export function quarantineDirsToReap(.filter((q) => nowMs - q.ts >= minAgeMs).map((q) => q.name);}++/**+ * How long to wait before the next mine pass.+ *+ * Mining into a palace that just quarantined buys nothing: the segment it+ * rebuilds is the same one the next open will quarantine again, so the only+ * product of a fast cadence is another copy for `quarantineDirsToReap` to+ * clean up. So each pass that sees a fresh quarantine doubles the wait, and+ * the first clean pass drops straight back to the base interval.+ *+ * Capped deliberately, and not high. On a palace stuck in the loop the backoff+ * would otherwise climb forever, and the cost of waiting is real: a memory is+ * not searchable until it has been mined. The reaper already removes 100% of+ * the disk growth, so this is only trimming CPU and IO — not worth making+ * recall an hour stale to save.+ */+export function nextMineDelayMs(+ currentMs: number,+ baseMs: number,+ maxMs: number,+ quarantinedThisPass: boolean+): number {+ if (!quarantinedThisPass) return baseMs;+ return Math.min(Math.max(currentMs, baseMs) * 2, maxMs);+}diff --git a/test/palace-reap.test.cjs b/test/palace-reap.test.cjsindex 4f6a09a0..87414801 100644--- a/test/palace-reap.test.cjs+++ b/test/palace-reap.test.cjs@@ -121,3 +121,35 @@ test('ties inside the same second are ordered deterministically', () => {['a.drift-20260822-010000']);});++// --- backing off while the palace is quarantining ---------------------------+// Mining into a palace that just quarantined only produces another copy: the+// segment it rebuilds is the one the next open quarantines again.++const { nextMineDelayMs } = loadTs('src/main/palaceReap.ts');+const BASE = 600_000;+const MAX = 1_800_000;++test('a clean pass mines at the base interval', () => {+ assert.equal(nextMineDelayMs(BASE, BASE, MAX, false), BASE);+});++test('each pass that quarantines doubles the wait', () => {+ const first = nextMineDelayMs(BASE, BASE, MAX, true);+ assert.equal(first, 1_200_000);+ assert.equal(nextMineDelayMs(first, BASE, MAX, true), MAX);+});++test('the backoff is capped, so recall never goes badly stale', () => {+ assert.equal(nextMineDelayMs(MAX, BASE, MAX, true), MAX);+ assert.equal(nextMineDelayMs(MAX * 4, BASE, MAX, true), MAX);+});++test('one clean pass snaps straight back — no slow climb down', () => {+ assert.equal(nextMineDelayMs(MAX, BASE, MAX, false), BASE,+ 'the palace stopped quarantining, so there is nothing left to back off from');+});++test('a delay below base is never used to shrink the interval', () => {+ assert.equal(nextMineDelayMs(1_000, BASE, MAX, true), 1_200_000);+});