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

test: strengthen base-hash batch regressions (#5539)

Adam Eivy565ad26ac977
diff --git a/server/lib/conflictJournal.js b/server/lib/conflictJournal.js
index 104a7b3481..5c06c3afa6 100644
--- a/server/lib/conflictJournal.js
+++ b/server/lib/conflictJournal.js
@@ -381,8 +381,9 @@ export function flushBaseHashes() {
* Depth is module-level, not per-async-context, so CONCURRENT batches merge
* rather than nest. The tombstone GC fans its `pruneTombstoned*` calls out
* through one `Promise.all`, so their scopes overlap: the last scope to close
- * flushes every eviction they accumulated. Nothing is dropped — a deferred
- * flush leaves `_baseDirty` set.
+ * flushes every eviction they accumulated. An unbatched concurrent flush that
+ * lands while the depth is still positive rides along in that same write.
+ * Nothing is dropped — a deferred flush leaves `_baseDirty` set.
*/
export async function withBaseHashFlushBatch(fn) {
_flushBatchDepth += 1;
diff --git a/server/services/pipeline/issues.test.js b/server/services/pipeline/issues.test.js
index 41d2841096..5e8d16fa8d 100644
--- a/server/services/pipeline/issues.test.js
+++ b/server/services/pipeline/issues.test.js
@@ -3,6 +3,7 @@ import { mockNoPeerSync, mockNoPeers } from '../../lib/mockPathsDataRoot.js';
const fileStore = new Map();
const writeCounter = vi.hoisted(() => ({ baseHash: 0 }));
+const baseHashEvictionSequence = vi.hoisted(() => ({ enabled: false, tail: Promise.resolve() }));
// One-shot write delay hook for the concurrency regression test: when set, the
// first atomicWrite whose (path, data) matches is held for `ms` before landing,
// letting a test force a specific read→write interleaving deterministically.
@@ -25,6 +26,25 @@ tryReadFile: vi.fn().mockResolvedValue(null),
readJSONFile: vi.fn(async (path, fallback) => (fileStore.has(path) ? fileStore.get(path) : fallback)),
}));
+vi.mock('../../lib/conflictJournal.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ deleteSyncBaseHash: async (...args) => {
+ if (!baseHashEvictionSequence.enabled) return actual.deleteSyncBaseHash(...args);
+ const previous = baseHashEvictionSequence.tail;
+ let release;
+ baseHashEvictionSequence.tail = new Promise((resolve) => { release = resolve; });
+ await previous;
+ try {
+ return await actual.deleteSyncBaseHash(...args);
+ } finally {
+ release();
+ }
+ },
+ };
+});
+
let uuidCounter = 0;
vi.mock('crypto', async () => {
const actual = await vi.importActual('crypto');
@@ -46,6 +66,8 @@ describe('pipeline issues service', () => {
fileStore.clear();
cj.__resetBaseHashCacheForTests();
writeCounter.baseHash = 0;
+ baseHashEvictionSequence.enabled = false;
+ baseHashEvictionSequence.tail = Promise.resolve();
uuidCounter = 0;
pendingWriteDelay = null;
coverRefresh.refreshSeriesCoverImage.mockClear();
@@ -1206,6 +1228,9 @@ describe('pipeline issues service', () => {
await cj.setSyncBaseHash('issue', live.id, 'hash-live');
await cj.flushBaseHashes();
writeCounter.baseHash = 0;
+ // Serialize the eviction calls without sleeping. Without the batch,
+ // each call would finish its own side-store write before the next one.
+ baseHashEvictionSequence.enabled = true;
const cutoff = Date.now() - 50_000;
const result = await svc.pruneTombstonedIssues(cutoff);
expect(result.pruned).toBe(2);
diff --git a/server/services/pipeline/series.test.js b/server/services/pipeline/series.test.js
index 1b2b1916b7..050139a568 100644
--- a/server/services/pipeline/series.test.js
+++ b/server/services/pipeline/series.test.js
@@ -3,6 +3,7 @@ import { mockNoPeerSync, mockNoPeers } from '../../lib/mockPathsDataRoot.js';
const fileStore = new Map();
const writeCounter = vi.hoisted(() => ({ baseHash: 0 }));
+const baseHashEvictionSequence = vi.hoisted(() => ({ enabled: false, tail: Promise.resolve() }));
vi.mock('../../lib/fileUtils.js', () => ({
tryReadFile: vi.fn().mockResolvedValue(null),
@@ -15,6 +16,25 @@ tryReadFile: vi.fn().mockResolvedValue(null),
readJSONFile: vi.fn(async (path, fallback) => (fileStore.has(path) ? fileStore.get(path) : fallback)),
}));
+vi.mock('../../lib/conflictJournal.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ deleteSyncBaseHash: async (...args) => {
+ if (!baseHashEvictionSequence.enabled) return actual.deleteSyncBaseHash(...args);
+ const previous = baseHashEvictionSequence.tail;
+ let release;
+ baseHashEvictionSequence.tail = new Promise((resolve) => { release = resolve; });
+ await previous;
+ try {
+ return await actual.deleteSyncBaseHash(...args);
+ } finally {
+ release();
+ }
+ },
+ };
+});
+
let uuidCounter = 0;
vi.mock('crypto', async () => {
const actual = await vi.importActual('crypto');
@@ -33,6 +53,8 @@ describe('pipeline series service', () => {
fileStore.clear();
cj.__resetBaseHashCacheForTests();
writeCounter.baseHash = 0;
+ baseHashEvictionSequence.enabled = false;
+ baseHashEvictionSequence.tail = Promise.resolve();
uuidCounter = 0;
});
@@ -449,6 +471,9 @@ describe('pipeline series service', () => {
await cj.setSyncBaseHash('series', oldT2.id, 'hash-old-b');
await cj.flushBaseHashes();
writeCounter.baseHash = 0;
+ // Serialize the eviction calls without sleeping. Without the batch,
+ // each call would finish its own side-store write before the next one.
+ baseHashEvictionSequence.enabled = true;
const cutoff = Date.now() - 50_000;
const result = await svc.pruneTombstonedSeries(cutoff);
expect(result.pruned).toBe(2);
diff --git a/server/services/universeBuilder.test.js b/server/services/universeBuilder.test.js
index cc0d5518a6..cbf5acf503 100644
--- a/server/services/universeBuilder.test.js
+++ b/server/services/universeBuilder.test.js
@@ -10,6 +10,7 @@ import { mockNoPeerSync, mockNoPeers } from "../lib/mockPathsDataRoot.js";
// universeBuilder + collectionStore land here instead of the real ./data.
const TEST_DATA_ROOT = mkdtempSync(join(tmpdir(), "universe-builder-test-"));
const writeCounter = vi.hoisted(() => ({ baseHash: 0 }));
+const baseHashEvictionSequence = vi.hoisted(() => ({ enabled: false, tail: Promise.resolve() }));
// Per-test reference-sheet "what exists on disk" — keys are filenames the
// referenceSheetImageRef preservation guard / pruner asks about. Default
@@ -42,6 +43,25 @@ vi.mock("../lib/fileUtils.js", async (importOriginal) => {
};
});
+vi.mock("../lib/conflictJournal.js", async (importOriginal) => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ deleteSyncBaseHash: async (...args) => {
+ if (!baseHashEvictionSequence.enabled) return actual.deleteSyncBaseHash(...args);
+ const previous = baseHashEvictionSequence.tail;
+ let release;
+ baseHashEvictionSequence.tail = new Promise((resolve) => { release = resolve; });
+ await previous;
+ try {
+ return await actual.deleteSyncBaseHash(...args);
+ } finally {
+ release();
+ }
+ },
+ };
+});
+
// Stub instances.js so createUniverse's fire-and-forget autoSubscribeRecordToAllPeers
// doesn't fan out to real peers. getPeers reads the live peer registry through a
// dataPath closure that resolves to the REAL PATHS once the post-return microtask
@@ -130,6 +150,8 @@ describe("universeBuilder service", () => {
mkdirSync(TEST_DATA_ROOT, { recursive: true });
cj.__resetBaseHashCacheForTests();
writeCounter.baseHash = 0;
+ baseHashEvictionSequence.enabled = false;
+ baseHashEvictionSequence.tail = Promise.resolve();
uuidCounter = 0;
refSheetFilesByName.clear();
});
@@ -1357,6 +1379,9 @@ describe("universeBuilder service", () => {
await cj.setSyncBaseHash('universe', oldTombstone2.id, 'hash-old-b');
await cj.flushBaseHashes();
writeCounter.baseHash = 0;
+ // Serialize the eviction calls without sleeping. Without the batch,
+ // each call would finish its own side-store write before the next one.
+ baseHashEvictionSequence.enabled = true;
// Cutoff = now - 50s — old (100s ago) is past, new (just now) is not.
const cutoff = Date.now() - 50_000;