Skip to content

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

fix(memory): sweep quarantined palace segments, and mine less often

Chaitanya Girid360d6797cc7
diff --git a/src/main/memory.ts b/src/main/memory.ts
index 7c96ae79..6446f0de 100644
--- a/src/main/memory.ts
+++ b/src/main/memory.ts
@@ -13,10 +13,11 @@
*
* Runs in the Electron main process.
*/
-import { existsSync, statSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
+import { existsSync, statSync, readdirSync, readFileSync, writeFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { spawn, spawnSync } from 'node:child_process';
import { ensureKilled } from './procKill';
+import { quarantineDirsToReap } 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
@@ -61,7 +62,17 @@ export interface MemoryStatus {
bin: string | null;
}
-const MINE_INTERVAL_MS = 180_000; // re-mine changed memories every 3 min
+// Re-mine changed memories every 10 min, up from 3.
+//
+// Every `mempalace mine` opens the palace, and every open runs MemPalace's
+// quarantine gate — which on a palace stuck in the rename loop means another
+// full-size copy of the segment left on disk. The gate is not ours to fix, but
+// how often we invoke it is. Mining is already skipped for agents whose
+// memory.md has not changed, so this only affects an agent editing its notes
+// repeatedly: its changes are batched into one mine instead of three. A memory
+// 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;
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
@@ -182,6 +193,12 @@ export class MemoryManager {
if (!this.active() || this.initStarted) return;
if (!this.bin() || !this.getHome() || !this.palacePath()) return;
this.initStarted = true;
+ // Sweep once at boot, before the first mine. An app updating into this fix
+ // arrives at a palace that has been accumulating copies for as long as it
+ // has been running — 357 of them here — and waiting for the first agent to
+ // edit its memory.md would leave all of that on disk for an arbitrary
+ // while. This is the pass that makes the existing pile go away by itself.
+ this.reapPalace();
this.startMineLoop();
}
@@ -248,6 +265,35 @@ export class MemoryManager {
} finally {
this.mining = false;
}
+ this.reapPalace(); // every pass above may have left another copy behind
+ }
+
+ /**
+ * Delete quarantined segment copies MemPalace renamed aside and never removed.
+ *
+ * Safe to delete: the rename is precisely what takes them OUT of the palace's
+ * live set, and Chroma has already rebuilt by the time we see one. They are
+ * diagnostic residue. `quarantineDirsToReap` keeps the newest couple so there
+ * is still something to look at, and refuses to touch anything recent enough
+ * to still be mid-recovery.
+ *
+ * Best-effort throughout. A palace we cannot read, or a directory we cannot
+ * remove, must never take down the mine loop — this is disk hygiene, not a
+ * correctness path.
+ */
+ private reapPalace(): void {
+ const palace = this.palacePath();
+ if (!palace || !existsSync(palace)) return;
+ let names: string[];
+ try { names = readdirSync(palace); } catch { return; }
+ const doomed = quarantineDirsToReap(names.map((name) => ({ name })), Date.now());
+ if (!doomed.length) return;
+ 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)`);
}
private mineAgent(agentDir: string, id: string): Promise<void> {
diff --git a/src/main/palaceReap.ts b/src/main/palaceReap.ts
new file mode 100644
index 00000000..23649a80
--- /dev/null
+++ b/src/main/palaceReap.ts
@@ -0,0 +1,100 @@
+/**
+ * Which quarantined MemPalace segments to delete.
+ *
+ * MemPalace recovers from a suspect ChromaDB HNSW segment by RENAMING the whole
+ * segment directory out of the way — `<uuid>.drift-<stamp>` for mtime drift,
+ * `<uuid>.corrupt-<stamp>` for unreadable metadata — and letting Chroma rebuild
+ * a clean one. Sound recovery. What it never does, at any version we have read
+ * (3.3.5 through the current 3.7.1), is DELETE the copy it renamed aside.
+ *
+ * That is fine for a one-off. It is not fine here, because on this palace the
+ * quarantine is not a one-off: the drawers segment is one 100-vector batch
+ * (167,600 bytes at 384 dims), which never reaches Chroma's sync threshold of
+ * 1000, so no persist ever fires, so `index_metadata.pickle` is never written,
+ * so the health gate fails and quarantines it again. Byte-identical, every few
+ * minutes, forever. A user on Discord passed 100GB inside 8 hours this way.
+ *
+ * The cure belongs upstream — MemPalace's "has real data" floor is 1024 bytes,
+ * set for tiny test collections, and a normal vector batch clears it by 163x.
+ * We cannot ship their fix and cannot make anyone upgrade. So we contain it:
+ * sweep the copies and the disk stops growing on every version, with nothing
+ * for the user to notice or do.
+ *
+ * Pure and browser-global-free so the rules are unit-testable without a palace
+ * on disk. Same split as `store/focusMode.ts`: the decision lives here, the
+ * `rm` lives in `memory.ts`.
+ */
+
+/** A palace directory entry, as far as these rules care. */
+export interface PalaceEntry {
+ name: string;
+}
+
+/**
+ * Matches ONLY MemPalace's own quarantine suffix.
+ *
+ * Deliberately anchored and fully specified — `%Y%m%d-%H%M%S` from
+ * `backends/chroma.py`, so exactly 8 digits, a dash, 6 digits, end of name. A
+ * looser "contains .corrupt" test is the kind that one day matches a live
+ * collection and deletes somebody's index. The live segments are bare UUIDs
+ * with no suffix at all, so nothing without this exact shape is ever a
+ * candidate.
+ */
+const QUARANTINE = /\.(?:drift|corrupt)-(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})$/;
+
+/** The quarantine stamp as local epoch ms, or null if this is not one.
+ *
+ * The stamp, not the directory's mtime: renaming a directory does not change
+ * its own mtime (only the parent's), so mtime still reports when the segment
+ * was last WRITTEN, not when it was quarantined. The stamp is written by
+ * MemPalace's `datetime.now()` on this same machine, so local time is the
+ * right reading of it. */
+export function quarantineStampMs(name: string): number | null {
+ const m = QUARANTINE.exec(name);
+ if (!m) return null;
+ const [, y, mo, d, h, mi, s] = m;
+ const t = new Date(+y, +mo - 1, +d, +h, +mi, +s).getTime();
+ return Number.isNaN(t) ? null : t;
+}
+
+export interface ReapOptions {
+ /** Newest N kept for diagnosis. Something has to survive for anyone to be
+ * able to look at what was quarantined and why. */
+ keep?: number;
+ /** Never touch a quarantine younger than this. The rename and the rebuild are
+ * not atomic with respect to us, so leave the freshest ones alone rather
+ * than race a MemPalace process mid-recovery. */
+ minAgeMs?: number;
+}
+
+const DEFAULTS = { keep: 2, minAgeMs: 10 * 60_000 };
+
+/**
+ * Names to delete, given everything currently in the palace directory.
+ *
+ * Newest-first by stamp, skip the newest `keep`, then drop whatever is left
+ * that is also older than `minAgeMs`. Returns names, not paths — the caller
+ * owns the palace path and the filesystem.
+ */
+export function quarantineDirsToReap(
+ entries: PalaceEntry[],
+ nowMs: number,
+ opts: ReapOptions = {}
+): string[] {
+ const keep = opts.keep ?? DEFAULTS.keep;
+ const minAgeMs = opts.minAgeMs ?? DEFAULTS.minAgeMs;
+
+ const stamped: { name: string; ts: number }[] = [];
+ for (const e of entries) {
+ const ts = quarantineStampMs(e.name);
+ if (ts !== null) stamped.push({ name: e.name, ts });
+ }
+ // Newest first. Tie-break on name so two quarantines inside the same second
+ // produce a stable, reproducible ordering rather than depending on readdir.
+ stamped.sort((a, b) => (b.ts - a.ts) || b.name.localeCompare(a.name));
+
+ return stamped
+ .slice(Math.max(0, keep))
+ .filter((q) => nowMs - q.ts >= minAgeMs)
+ .map((q) => q.name);
+}
diff --git a/test/palace-reap.test.cjs b/test/palace-reap.test.cjs
new file mode 100644
index 00000000..4f6a09a0
--- /dev/null
+++ b/test/palace-reap.test.cjs
@@ -0,0 +1,123 @@
+'use strict';
+
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const loadTs = require('./load-ts.cjs');
+
+const { quarantineDirsToReap, quarantineStampMs } =
+ loadTs('src/main/palaceReap.ts');
+
+const at = (y, mo, d, h, mi, s) => new Date(y, mo - 1, d, h, mi, s).getTime();
+const NOW = at(2026, 8, 22, 12, 0, 0);
+const names = (...n) => n.map((name) => ({ name }));
+
+// Real names from the affected palace. Note the second one: MemPalace inherits
+// whatever Chroma named the segment, and Finder-style " 2" duplicates DO occur,
+// so the pattern has to survive a space and a digit before the suffix.
+const DRIFT = '47258cb2-e535-4990-9dc7-829966c6ff07.drift-20260809-000813';
+const DRIFT_DUP = '47258cb2-e535-4990-9dc7-829966c6ff07 2.drift-20260812-191936';
+const CORRUPT = '83f1acc5-5589-4c47-b457-996f22add88e.corrupt-20260810-005310';
+const LIVE = '83f1acc5-5589-4c47-b457-996f22add88e';
+
+// --- what counts as a quarantine --------------------------------------------
+// The whole risk of this feature is deleting something that is not ours. The
+// live segments are bare UUIDs; only MemPalace's exact strftime suffix counts.
+
+test('the live collection directory is never a candidate', () => {
+ assert.equal(quarantineStampMs(LIVE), null);
+ assert.deepEqual(quarantineDirsToReap(names(LIVE), NOW, { keep: 0, minAgeMs: 0 }), []);
+});
+
+test('chroma.sqlite3 and other palace files are never candidates', () => {
+ assert.deepEqual(
+ quarantineDirsToReap(names('chroma.sqlite3', 'roster.json'), NOW, { keep: 0, minAgeMs: 0 }),
+ []
+ );
+});
+
+test('both quarantine kinds are recognised, including a duplicated segment name', () => {
+ assert.equal(quarantineStampMs(DRIFT), at(2026, 8, 9, 0, 8, 13));
+ assert.equal(quarantineStampMs(DRIFT_DUP), at(2026, 8, 12, 19, 19, 36));
+ assert.equal(quarantineStampMs(CORRUPT), at(2026, 8, 10, 0, 53, 10));
+});
+
+test('a near-miss suffix is left alone', () => {
+ // No timestamp, wrong digit counts, or the marker sitting mid-name rather
+ // than at the end: all of these must read as "not mine".
+ for (const n of [
+ 'seg.drift',
+ 'seg.drift-2026-08-09',
+ 'seg.drift-20260809-00081',
+ 'seg.corrupt-20260809-000813.bak',
+ 'seg.drifted-20260809-000813'
+ ]) {
+ assert.equal(quarantineStampMs(n), null, n);
+ }
+});
+
+// --- what actually gets deleted ---------------------------------------------
+
+test('the newest few are kept so there is still something to diagnose with', () => {
+ const all = names(
+ 'a.drift-20260822-010000',
+ 'a.drift-20260822-020000',
+ 'a.drift-20260822-030000',
+ 'a.drift-20260822-040000'
+ );
+ const doomed = quarantineDirsToReap(all, NOW, { keep: 2, minAgeMs: 0 });
+ assert.deepEqual(doomed, ['a.drift-20260822-020000', 'a.drift-20260822-010000']);
+});
+
+test('a fresh quarantine is never touched, even past the keep count', () => {
+ // 04:00 and 03:00 are kept by count; 02:00 is only 10 minutes old at this
+ // "now" and could still be mid-recovery, so it survives too.
+ const now = at(2026, 8, 22, 2, 5, 0);
+ const all = names(
+ 'a.drift-20260822-020000',
+ 'a.drift-20260822-010000',
+ 'a.drift-20260821-010000'
+ );
+ const doomed = quarantineDirsToReap(all, now, { keep: 0, minAgeMs: 10 * 60_000 });
+ assert.deepEqual(doomed, ['a.drift-20260822-010000', 'a.drift-20260821-010000'],
+ 'the 02:00 one is 5 minutes old and stays');
+});
+
+test('nothing to do on a healthy palace', () => {
+ assert.deepEqual(quarantineDirsToReap(names(LIVE, 'chroma.sqlite3'), NOW), []);
+});
+
+test('drift and corrupt are ranked together, not kept separately', () => {
+ // `keep` is a budget for the palace, not per kind — otherwise a palace
+ // producing both kinds keeps twice as much as asked.
+ const all = names(
+ 'a.drift-20260822-030000',
+ 'b.corrupt-20260822-020000',
+ 'a.drift-20260822-010000'
+ );
+ assert.deepEqual(
+ quarantineDirsToReap(all, NOW, { keep: 1, minAgeMs: 0 }),
+ ['b.corrupt-20260822-020000', 'a.drift-20260822-010000']
+ );
+});
+
+test('the real palace state reaps the backlog and keeps the newest two', () => {
+ // 357 drift + 4 corrupt + the live segment + the sqlite file, which is what
+ // this machine actually looked like when the bug was reported.
+ const entries = [{ name: LIVE }, { name: 'chroma.sqlite3' }];
+ for (let i = 0; i < 357; i += 1) {
+ entries.push({ name: `a.drift-20260820-${String(i).padStart(6, '0')}` });
+ }
+ entries.push({ name: CORRUPT });
+ const doomed = quarantineDirsToReap(entries, NOW, { keep: 2, minAgeMs: 0 });
+ assert.equal(doomed.length, 356);
+ assert.ok(!doomed.includes(LIVE));
+ assert.ok(!doomed.includes('chroma.sqlite3'));
+});
+
+test('ties inside the same second are ordered deterministically', () => {
+ const all = names('b.drift-20260822-010000', 'a.drift-20260822-010000');
+ assert.deepEqual(
+ quarantineDirsToReap(all, NOW, { keep: 1, minAgeMs: 0 }),
+ ['a.drift-20260822-010000']
+ );
+});