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
fix: fence Beeper attachment sweep row healing against concurrent downloads (#10105)
Adam Eivy05c98124c111
diff --git a/server/services/beeperAttachments.js b/server/services/beeperAttachments.jsindex fbf866a3f6..630cbc1224 100644--- a/server/services/beeperAttachments.js+++ b/server/services/beeperAttachments.js@@ -677,6 +677,9 @@ export async function sweepAttachmentOrphans() {// really is "empty"; making it once means every later listing failure is a// genuine anomaly worth logging and refusing to act on.await mkdir(attachmentsRoot(), { recursive: true }).catch(() => {});+ // Fence for the row-healing direction: a download that lands after this+ // instant is newer than the listing and must not be read as "missing".+ const listingStartedAt = new Date();const { files: onDisk, complete: storeListed } = await listStoredFiles();const referenced = await query(`SELECT DISTINCT local_path FROM beeper_attachments WHERE local_path IS NOT NULL`,@@ -710,12 +713,19 @@ export async function sweepAttachmentOrphans() {if (!storeListed) {console.error('❌ Beeper attachment sweep: the store did not list completely — no row healed this pass');} else {- const missing = [...referencedSet].filter((relativePath) => !diskSet.has(relativePath));+ // A file committed between the listing and the reference query is in+ // `referencedSet` but not `diskSet`; re-check the disk, and fence the UPDATE+ // on `fetched_at` so a row stamped after the listing is never cleared.+ const absent = [...referencedSet].filter((relativePath) => !diskSet.has(relativePath));+ const missing = [];+ for (const relativePath of absent) {+ if (!(await pathExists(join(attachmentsRoot(), relativePath)))) missing.push(relativePath);+ }if (missing.length > 0) {const result = await query(`UPDATE beeper_attachments SET local_path = NULL, fetched_at = NULL, updated_at = NOW()- WHERE local_path = ANY($1::text[])`,- [missing],+ WHERE local_path = ANY($1::text[]) AND (fetched_at IS NULL OR fetched_at < $2)`,+ [missing, listingStartedAt],);healedRows = result?.rowCount || 0;}diff --git a/server/services/beeperAttachments.test.js b/server/services/beeperAttachments.test.jsindex ab3a59fb49..50ecd4650e 100644--- a/server/services/beeperAttachments.test.js+++ b/server/services/beeperAttachments.test.js@@ -539,6 +539,33 @@ describe('sweepAttachmentOrphans — the backstop, both directions', () => {expect(healed[0]).toEqual([missing]);});+ it('never heals a row whose file landed after the listing, and fences the UPDATE on fetched_at', async () => {+ const landed = `cc/${'c'.repeat(64)}.png`;+ const gone = `11/${'1'.repeat(64)}.png`;+ // The download commits its file after listStoredFiles() but before the+ // reference query runs, so the file is referenced yet absent from the listing.+ vi.mocked(query).mockImplementation(async (sql, params) => {+ const flat = String(sql).replace(/\s+/g, ' ');+ if (flat.includes('SELECT DISTINCT local_path FROM beeper_attachments WHERE local_path IS NOT NULL')) {+ mkdirSync(join(attachmentsRoot(), 'cc'), { recursive: true });+ writeFileSync(join(attachmentsRoot(), landed), Buffer.alloc(8));+ return { rows: [{ local_path: landed }, { local_path: gone }] };+ }+ if (flat.includes('WHERE local_path = ANY')) {+ expect(flat).toContain('fetched_at < $2');+ expect(params[1]).toBeInstanceOf(Date);+ return { rowCount: params[0].length };+ }+ return { rows: [] };+ });++ const result = await sweepAttachmentOrphans();+ expect(result.healedRows).toBe(1);+ const update = vi.mocked(query).mock.calls.find(([sql]) => String(sql).includes('WHERE local_path = ANY'));+ expect(update[1][0]).toEqual([gone]);+ expect(existsSync(join(attachmentsRoot(), landed))).toBe(true);+ });+it('spares a file that gained a reference between the snapshot and the unlink', async () => {const shared = `ff/${'f'.repeat(64)}.png`;mkdirSync(join(attachmentsRoot(), 'ff'), { recursive: true });