Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): persist primedTypes so a restart mid-drain stays warm

[._.]/ Adam Eivycb21474e67df
diff --git a/server/services/meatspacePostDrillCache.js b/server/services/meatspacePostDrillCache.js
index a6dbfc65aa..5dfe2d5a55 100644
--- a/server/services/meatspacePostDrillCache.js
+++ b/server/services/meatspacePostDrillCache.js
@@ -35,18 +35,41 @@ let saveQueued = false;
// meatspacePostDrillCache.test.js "stays warm after draining to zero").
const primedTypes = new Set();
+// Pre-existing on-disk cache files (before primedTypes existed) store the
+// per-type drill arrays directly at the top level: { [type]: [...] }. Newer
+// files nest them under `drills` alongside a persisted `primedTypes` list, so
+// "primed" survives a restart that happens to land while a type is fully
+// drained (debouncedSave fires on every consumption, so 0-length is a
+// legitimate on-disk snapshot mid-drain — without persisting the flag, that
+// restart would re-classify an already-warmed type as cold, reproducing the
+// same permanent-replenish-stall bug this file's primedTypes fix targets,
+// just gated behind a restart instead of an in-process drain).
+function isNestedCacheShape(parsed) {
+ return !!(parsed && typeof parsed === 'object' && parsed.drills && typeof parsed.drills === 'object' && !Array.isArray(parsed.drills));
+}
+
async function loadCache() {
const raw = await readFile(CACHE_FILE, 'utf-8').catch(() => '{}');
- cache = safeJSONParse(raw, {});
+ const parsed = safeJSONParse(raw, {});
+ const nested = isNestedCacheShape(parsed);
+ cache = nested ? parsed.drills : parsed;
+ if (nested && Array.isArray(parsed.primedTypes)) {
+ for (const type of parsed.primedTypes) {
+ if (CACHEABLE_TYPES.includes(type)) primedTypes.add(type);
+ }
+ }
for (const type of CACHEABLE_TYPES) {
if (!Array.isArray(cache[type])) cache[type] = [];
+ // Also infer from non-empty arrays — covers pre-migration flat-shape
+ // files (no primedTypes key) and defensively covers any type a stale
+ // persisted primedTypes list somehow missed.
if (cache[type].length > 0) primedTypes.add(type);
}
}
async function saveCache() {
await ensureDir(PATHS.meatspace);
- await atomicWrite(CACHE_FILE, cache);
+ await atomicWrite(CACHE_FILE, { drills: cache, primedTypes: [...primedTypes] });
}
function debouncedSave() {
diff --git a/server/services/meatspacePostDrillCache.test.js b/server/services/meatspacePostDrillCache.test.js
index a9690af1f9..294a19e4e9 100644
--- a/server/services/meatspacePostDrillCache.test.js
+++ b/server/services/meatspacePostDrillCache.test.js
@@ -85,4 +85,55 @@ describe('meatspacePostDrillCache', () => {
const triggered = requestCacheFill(['not-a-real-type'], null, null);
expect(triggered).toEqual([]);
});
+
+ it('saveCache persists primedTypes alongside the drills so a restart mid-drain stays warm', async () => {
+ const { atomicWrite } = await import('../lib/fileUtils.js');
+ requestCacheFill(['compound-chain'], null, null);
+ await vi.advanceTimersByTimeAsync(20000);
+
+ const [, written] = atomicWrite.mock.calls.at(-1);
+ expect(written.primedTypes).toContain('compound-chain');
+ expect(Array.isArray(written.drills['compound-chain'])).toBe(true);
+ });
+});
+
+// Disk-shape scenarios need an isolated module instance per test (cache/
+// primedTypes are module-level state that would otherwise leak between
+// cases), so these reset modules and re-import instead of sharing the
+// describe block above.
+describe('meatspacePostDrillCache — on-disk cache shape', () => {
+ beforeEach(() => {
+ vi.resetModules();
+ });
+
+ it('infers primed from a legacy flat-shape file ({ [type]: [...] }, no primedTypes key)', async () => {
+ const { readFile } = await import('fs/promises');
+ readFile.mockResolvedValueOnce(JSON.stringify({
+ 'compound-chain': [{ type: 'compound-chain', challenges: [] }],
+ 'bridge-word': [],
+ }));
+ const { initDrillCache, getCacheStats } = await import('./meatspacePostDrillCache.js');
+ await initDrillCache();
+
+ expect(getCacheStats()['compound-chain'].cold).toBe(false);
+ expect(getCacheStats()['bridge-word'].cold).toBe(true);
+ });
+
+ it('stays warm across a restart that lands mid-drain (persisted primedTypes, 0 on-disk drills)', async () => {
+ const { readFile } = await import('fs/promises');
+ readFile.mockResolvedValueOnce(JSON.stringify({
+ drills: { 'compound-chain': [] },
+ primedTypes: ['compound-chain'],
+ }));
+ const { initDrillCache, getCacheStats } = await import('./meatspacePostDrillCache.js');
+ await initDrillCache();
+
+ // Before persisting primedTypes, a restart landing here (0 cached on
+ // disk) would re-classify this type as cold, permanently stalling future
+ // replenishment — the same bug the in-process primedTypes fix targeted,
+ // just triggered by a restart instead of a drain.
+ const stats = getCacheStats()['compound-chain'];
+ expect(stats.count).toBe(0);
+ expect(stats.cold).toBe(false);
+ });
});