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): stat fallback when buildId watcher can't be armed (#1828)
[._.]/ Adam Eivy31d85329f6b0
diff --git a/server/lib/buildId.fallback.test.js b/server/lib/buildId.fallback.test.jsnew file mode 100644index 0000000000..8a98bd3919--- /dev/null+++ b/server/lib/buildId.fallback.test.js@@ -0,0 +1,57 @@+import { describe, it, expect, beforeEach, vi } from 'vitest';++// Simulate a platform/filesystem where fs.watch is unavailable, with index.html+// fully in-memory so this file never touches the real client/dist/index.html+// (and so can't race the sibling buildId.test.js, which vitest may run in a+// parallel worker). The buildId module must fall back to a throttled stat so a+// rebuild is still detected instead of serving stale chunk URLs until restart+// (codex review #1828).+const state = { mtimeMs: 1000, html: '<html><head></head><body>A</body></html>', indexExists: true };++vi.mock('fs', async (importOriginal) => {+ const actual = await importOriginal();+ return {+ ...actual,+ watch: vi.fn(() => { throw new Error('fs.watch unsupported'); }),+ existsSync: vi.fn((p) => (String(p).endsWith('index.html') ? state.indexExists : true)),+ statSync: vi.fn(() => ({ mtimeMs: state.mtimeMs })),+ readFileSync: vi.fn(() => state.html),+ };+});++beforeEach(() => {+ vi.resetModules();+ state.mtimeMs = 1000;+ state.html = '<html><head></head><body>A</body></html>';+ state.indexExists = true;+});++describe('buildId — degraded mode (fs.watch unavailable)', () => {+ it('falls back to a stat so a rebuild is still detected without the watcher', async () => {+ const mod = await import('./buildId.js');+ const idA = mod.getBuildId(); // primes (the `!cached` path — no stat fallback yet)+ expect(idA).not.toBe('dev');++ // Rebuild: new content + mtime. With no watcher, the next read's throttled+ // stat fallback (lastStatAt is still 0 after the prime) fires and recomputes.+ // NOTE: don't read again before mutating — in degraded mode the fallback is+ // throttled to one stat per STAT_FALLBACK_MS, so an intervening read would+ // consume the window and suppress this rebuild detection (bounded staleness+ // is the intended degraded-mode tradeoff).+ state.html = '<html><head></head><body>B</body></html>';+ state.mtimeMs = 2000;++ const idB = mod.getBuildId();+ expect(idB).not.toBe(idA);+ expect(mod.getStampedIndexHtml()).toContain('body>B');+ });++ it('drops to id=dev when index.html disappears while the watcher is unavailable', async () => {+ const mod = await import('./buildId.js');+ expect(mod.getBuildId()).not.toBe('dev'); // primes from in-memory A++ state.indexExists = false;+ expect(mod.getBuildId()).toBe('dev');+ expect(mod.getStampedIndexHtml()).toBe(null);+ });+});diff --git a/server/lib/buildId.js b/server/lib/buildId.jsindex d6140c1935..7a90bb040f 100644--- a/server/lib/buildId.js+++ b/server/lib/buildId.js@@ -43,6 +43,13 @@ const DIST_DIR = dirname(INDEX_PATH);let cached = null;let dirty = false;let watcher = null;+let lastStatAt = 0;++// When the watcher CAN'T be armed (fs.watch unsupported, or it errored out), a+// rebuild would otherwise go undetected until restart. In that degraded mode we+// fall back to a stat — throttled so it stays cheap — so the cache self-heals.+// No stat is taken while the watcher is healthy.+const STAT_FALLBACK_MS = 1000;const DEV_SNAPSHOT = { id: 'dev', html: null, mtimeMs: 0 };@@ -86,11 +93,30 @@ function ensureWatcher() {});watcher.unref(); // never keep the event loop alivewatcher.on('error', () => { closeWatcher(); }); // re-arm on next read+ // The watcher only catches FUTURE writes; if the dist dir just appeared+ // (a build completed against a running dev server), recompute once so an+ // index.html that landed before the watcher armed isn't missed.+ dirty = true;} catch {watcher = null;}}+// Degraded-mode fallback: only runs when the watcher is NOT armed. A throttled+// stat detects a rebuild so the cache can't go permanently stale on a platform+// without working fs.watch. Cheap (OS inode cache) and bounded to one per+// STAT_FALLBACK_MS regardless of request rate.+function maybeStatRefresh() {+ const now = Date.now();+ if (now - lastStatAt < STAT_FALLBACK_MS) return;+ lastStatAt = now;+ if (!existsSync(INDEX_PATH)) {+ if (cached.id !== 'dev') cached = DEV_SNAPSHOT;+ return;+ }+ if (statSync(INDEX_PATH).mtimeMs !== cached.mtimeMs) cached = computeSync();+}+function closeWatcher() {if (!watcher) return;try { watcher.close(); } catch { /* already closed */ }@@ -102,6 +128,10 @@ function ensureFresh() {if (!cached || dirty) {cached = computeSync();dirty = false;+ } else if (!watcher) {+ // Watcher couldn't be armed/maintained — fall back to a throttled stat so a+ // rebuild is still picked up instead of serving stale chunks until restart.+ maybeStatRefresh();}return cached;}