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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): recheck the full prune predicate inside the per-id queue
[._.]/ Adam Eivyd0295dbb30b5
diff --git a/server/services/creativeCommissions/db.js b/server/services/creativeCommissions/db.jsindex 815b234df3..cf4c67627a 100644--- a/server/services/creativeCommissions/db.js+++ b/server/services/creativeCommissions/db.js@@ -114,6 +114,18 @@ export async function listPrunable(olderThanMs) {return rows.map((r) => r.id);}+/** Whether ONE id currently satisfies the prune predicate (same column truth as listPrunable). */+export async function isPrunable(id, olderThanMs) {+ if (!Number.isFinite(olderThanMs)) return false;+ const cutoffIso = new Date(olderThanMs).toISOString();+ const { rows } = await query(+ `SELECT 1 FROM creative_commissions+ WHERE id = $1 AND deleted = TRUE AND deleted_at IS NOT NULL AND deleted_at < $2`,+ [id, cutoffIso],+ );+ return rows.length > 0;+}+/** Hard-remove tombstoned commissions whose `deleted_at` is older than the cutoff; returns their ids. */export async function pruneTombstoned(olderThanMs) {if (!Number.isFinite(olderThanMs)) return { pruned: 0, ids: [] };diff --git a/server/services/creativeCommissions/store.js b/server/services/creativeCommissions/store.jsindex a840e94880..58f8808eb0 100644--- a/server/services/creativeCommissions/store.js+++ b/server/services/creativeCommissions/store.js@@ -322,6 +322,11 @@ function makeFileBackend(dir) {.filter((r) => r?.deleted === true && isStr(r.deletedAt) && Date.parse(r.deletedAt) < olderThanMs).map((r) => r.id);},+ isPrunable: async (id, olderThanMs) => {+ if (!Number.isFinite(olderThanMs)) return false;+ const r = await cs.loadOne(id);+ return !!r && r.deleted === true && isStr(r.deletedAt) && Date.parse(r.deletedAt) < olderThanMs;+ },pruneTombstoned: async (olderThanMs) => {if (!Number.isFinite(olderThanMs)) return { pruned: 0, ids: [] };const stale = (await cs.loadAll()).filter((r) => r?.deleted === true && isStr(r.deletedAt) && Date.parse(r.deletedAt) < olderThanMs);@@ -344,6 +349,7 @@ function makePgBackend(db) {writeRaw: db.writeRaw,deleteRaw: db.deleteRaw,listPrunable: db.listPrunable,+ isPrunable: db.isPrunable,pruneTombstoned: db.pruneTombstoned,};}@@ -388,6 +394,7 @@ function createFacade(dir) {writeRaw: async (id, record) => (await getBackend()).writeRaw(id, record),deleteRaw: async (id) => (await getBackend()).deleteRaw(id),listPrunable: async (olderThanMs) => (await getBackend()).listPrunable(olderThanMs),+ isPrunable: async (id, olderThanMs) => (await getBackend()).isPrunable(id, olderThanMs),pruneTombstoned: async (olderThanMs) => (await getBackend()).pruneTombstoned(olderThanMs),queueRecordWrite,// Under PG, report ok WITHOUT forcing backend selection (the early boot@@ -798,18 +805,22 @@ export async function pruneTombstonedCommissions(olderThanMs) {// writeRaw corrects while leaving the JSON verbatim), so the set we tombstone// feedback for is exactly the set the delete would remove. Then each id is// revalidated and deleted INSIDE its per-id write queue — the same queue- // restoreCommission and the peer merge write through — so a restore that- // lands mid-sweep either runs before us (we see deleted:false and keep its- // feedback) or after (it finds the record gone and reports ERR_TARGET_GONE,- // exactly as if it raced the old bulk prune). Feedback is tombstoned before+ // restoreCommission and the peer merge write through — so a restore or+ // fresher tombstone landing mid-sweep either runs before us (the record no+ // longer passes the prune predicate and keeps its feedback) or after (it+ // finds the record gone and reports ERR_TARGET_GONE, exactly as if it raced+ // the old bulk prune). Feedback is tombstoned before// the row delete: a failure there throws, the commission tombstone stays// put, and the next sweep retries both halves — never an orphaned rating.const candidates = await store.listPrunable(olderThanMs);const ids = [];for (const id of candidates) {const pruned = await store.queueRecordWrite(id, async () => {- const rec = await store.readRaw(id, { includeDeleted: true });- if (!rec || rec.deleted !== true) return false; // restored mid-sweep — keep its feedback+ // Recheck the backend's FULL prune predicate, not just deleted:true — a+ // peer merge that rewrote this tombstone with a fresher deletedAt mid-+ // sweep restarted its GC grace period, and hard-deleting it early would+ // let an offline peer resurrect the stale record.+ if (!(await store.isPrunable(id, olderThanMs))) return false;await tombstoneFeedbackForCommission(id);await store.deleteRaw(id);return true;diff --git a/server/services/creativeCommissions/store.test.js b/server/services/creativeCommissions/store.test.jsindex 818536e4dd..f886e162ec 100644--- a/server/services/creativeCommissions/store.test.js+++ b/server/services/creativeCommissions/store.test.js@@ -320,6 +320,33 @@ describe('pruneTombstonedCommissions', () => {expect(rows.length).toBeGreaterThan(0);for (const f of rows) expect(f.deleted).not.toBe(true); // ratings survived});++ it('a FRESHER tombstone landing mid-sweep restarts its grace period instead of being pruned', async () => {+ const created = await createCommission(validInput());+ await deleteCommission(created.id);+ records.set(created.id, { ...records.get(created.id), deletedAt: '2020-01-01T00:00:00.000Z' });++ // Mid-sweep, a peer merge rewrites the tombstone with a fresh deletedAt+ // (still deleted:true). The queued revalidation must recheck the FULL+ // prune predicate — deleted alone would pass and hard-delete early,+ // letting an offline peer resurrect the stale record.+ const store = commissionStore();+ let releaseMerge;+ const gate = new Promise((resolve) => { releaseMerge = resolve; });+ const mergeDone = store.queueRecordWrite(created.id, async () => {+ await gate;+ const rec = await store.readRaw(created.id, { includeDeleted: true });+ await store.writeRaw(created.id, { ...rec, deletedAt: new Date().toISOString() });+ });+ const prunePromise = pruneTombstonedCommissions(Date.parse('2021-01-01T00:00:00.000Z'));+ await new Promise((resolve) => setTimeout(resolve, 10));+ releaseMerge();+ const result = await prunePromise;+ await mergeDone;++ expect(result).toEqual({ pruned: 0, ids: [] });+ expect(records.get(created.id)).toMatchObject({ deleted: true }); // tombstone retained for its new grace period+ });});describe('sanitizeFeedbackEntry', () => {