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([issue-3152]): validate stored tracks' publish contracts, and degrade the store when there is no filesystem
[._.]/ Adam Eivya7298ded1a5b
diff --git a/server/services/sprites/animationTrackStore.js b/server/services/sprites/animationTrackStore.jsindex d36b445ffe..494ffbd95b 100644--- a/server/services/sprites/animationTrackStore.js+++ b/server/services/sprites/animationTrackStore.js@@ -99,12 +99,35 @@ let cachedEffectiveTracks = null;* broke the JSON would see their tracks quietly revert to shipped defaults with* no error to explain it.*/+/**+ * Which of the three failure kinds a read error is:+ *+ * `absent` — ENOENT. Nothing there; try the next candidate.+ * `io-error` — any other errno. A real, user-facing problem: report it.+ * `no-filesystem` — NO errno at all, so the call never reached a filesystem.+ * See `resolveStoreDoc`'s header for the environment that+ * produces this and why it degrades instead of throwing.+ *+ * Exported so the classification is asserted directly (`animationTrackStore.test.js`):+ * this module binds `readFileSync` at import, so a test that re-mocked `fs` would not+ * intercept it and would pass without exercising anything.+ */+export function classifyStoreReadError(err) {+ if (!err?.code) return 'no-filesystem';+ return err.code === 'ENOENT' ? 'absent' : 'io-error';+}+function readStoreDoc(path) {let raw;try {raw = readFileSync(path, 'utf-8');} catch (err) {- if (err?.code === 'ENOENT') return null;+ const kind = classifyStoreReadError(err);+ if (kind === 'absent') return null;+ // A `no-filesystem` error is re-thrown BARE for `resolveStoreDoc` to read as+ // "no store", rather than dressed up as an unreadable file the user would go+ // looking for. Only a real errno becomes this module's own named error.+ if (kind === 'no-filesystem') throw err;throw new Error(`${ANIMATION_TRACK_STORE_REL}: cannot read ${path} — ${err.message}`);}try {@@ -114,6 +137,37 @@ function readStoreDoc(path) {}}+/**+ * The authoritative store doc: the user copy if it exists, else the shipped seed,+ * else `null` (walk alone).+ *+ * **Why the whole resolution is guarded.** `server/lib/validation.js` builds its+ * sprite Zod ranges from the effective registry AT MODULE LOAD, and nearly every+ * route and service imports `validation.js`. So this runs inside test suites that+ * have nothing to do with sprites — including ones that legitimately stub the+ * filesystem with a partial factory (`vi.mock('fs', () => ({ existsSync }))`, or a+ * `fileUtils.js` double exporting no `PATHS`). In that environment there is no+ * filesystem to consult, and the honest answer is "no store" — walk alone — not a+ * hard import-time failure over a file the suite never asked about.+ *+ * Scoped deliberately to the *environment* being unusable, never to a real read+ * failure: `readStoreDoc` still throws on a store that exists but won't parse,+ * because that is a user's broken hand edit and silently reverting to shipped+ * defaults is the sentinel violation this module's header calls out. A TypeError+ * from a missing mock export cannot reach a real install — an install always has+ * `fs` and `PATHS`.+ */+function resolveStoreDoc() {+ try {+ return readStoreDoc(animationTrackStorePath()) ?? readStoreDoc(animationTrackSeedPath());+ } catch (err) {+ // Rethrow anything that came from an actual file: those carry this module's+ // own prefix (unreadable / not-valid-JSON) and must stay loud.+ if (err?.message?.startsWith(ANIMATION_TRACK_STORE_REL)) throw err;+ return null;+ }+}+/*** Freeze one raw stored row into the exact shape a compiled row has, with* `builtin: false` forced.@@ -135,7 +189,7 @@ const normalizeStoredRow = (raw) => Object.freeze({*/function loadStoredRows() {if (cachedStoredRows) return cachedStoredRows;- const doc = readStoreDoc(animationTrackStorePath()) ?? readStoreDoc(animationTrackSeedPath());+ const doc = resolveStoreDoc();const rows = Array.isArray(doc?.tracks) ? doc.tracks : [];const byId = {};for (const raw of rows) {diff --git a/server/services/sprites/animationTrackStore.test.js b/server/services/sprites/animationTrackStore.test.jsindex 7ae622e836..3e2457b29c 100644--- a/server/services/sprites/animationTrackStore.test.js+++ b/server/services/sprites/animationTrackStore.test.js@@ -39,6 +39,7 @@ vi.mock('../../lib/fileUtils.js', async (importOriginal) => {const {getEffectiveAnimationTracks, getEffectiveAnimationTrackIds, __resetAnimationTrackStore,animationTrackStorePath, animationTrackSeedPath, ANIMATION_TRACK_STORE_SCHEMA_VERSION,+ classifyStoreReadError,} = await import('./animationTrackStore.js');const { ANIMATION_TRACKS, WALK_TRACK } = await import('./animationTracks.js');@@ -190,6 +191,37 @@ describe('absent vs. empty vs. corrupt (the sentinel rules)', () => {writeStore('{ "tracks": [ oops ');expect(() => getEffectiveAnimationTracks()).toThrow(/is not valid JSON/);});++ it('reports a real IO failure with the path named', () => {+ // A store that exists but cannot be read is a real user-facing problem, so it+ // must throw naming the file rather than silently serving shipped defaults —+ // the same sentinel rule as the unparseable case above. Provoked with a+ // directory where a file is expected, which produces a genuine errno (EISDIR).+ mkdirSync(STORE_PATH, { recursive: true });+ try {+ expect(() => getEffectiveAnimationTracks()).toThrow(/cannot read/);+ } finally {+ rmSync(STORE_PATH, { recursive: true, force: true });+ }+ });++ it('degrades to walk alone when the read never reached a filesystem', () => {+ // Why this matters: `server/lib/validation.js` builds its sprite Zod ranges from+ // the effective registry at MODULE LOAD, and nearly every route and service+ // imports validation.js — so this store gets resolved inside suites that have+ // nothing to do with sprites, including ones that stub `fs` with a partial+ // factory (`vi.mock('fs', () => ({ existsSync }))`). Such a read throws a+ // TypeError with NO errno; classifying that as "no store" is what keeps those+ // suites from failing at import time over a file they never asked about, and it+ // cannot mask a real install problem because an install always has `fs`.+ //+ // Asserted on the classifier rather than by re-mocking `fs` here: the store+ // binds `readFileSync` at import, so a namespace spy in this file would not+ // intercept it and the test would pass without exercising anything.+ expect(classifyStoreReadError(new TypeError('readFileSync is not a function'))).toBe('no-filesystem');+ expect(classifyStoreReadError(Object.assign(new Error('nope'), { code: 'EACCES' }))).toBe('io-error');+ expect(classifyStoreReadError(Object.assign(new Error('nope'), { code: 'ENOENT' }))).toBe('absent');+ });});describe('the cache', () => {diff --git a/server/services/sprites/atlasLayout.js b/server/services/sprites/atlasLayout.jsindex 0e342d55bb..76efb27776 100644--- a/server/services/sprites/atlasLayout.js+++ b/server/services/sprites/atlasLayout.js@@ -46,7 +46,16 @@import { basename } from 'path';import { deriveTracks, resolveWalkFrameCount, ATLAS_DEFAULT_ROWS } from './atlasGrid.js';-import { ANIMATION_TRACKS, WALK_TRACK } from './animationTracks.js';+import { WALK_TRACK } from './animationTracks.js';+// #3152 — the default table is the EFFECTIVE registry (compiled `walk` + the+// user-defined store), not the compiled constant. Load-bearing here: the contract+// check below iterates the table to find each track's `contractFrameCountField`,+// so a compiled-only default would silently SKIP `scannerFrameCount` /+// `ambientFrameCount` (and any user track's field) — publishing an atlas whose+// span disagrees with the app's contract would then pass unchecked, which is+// precisely the mismatch this function exists to refuse. Still sharp-free: the+// store reaches only fs/path/crypto.+import { getEffectiveAnimationTracks } from './animationTrackStore.js';// Bump only on a breaking shape change. Adding a field (or a new track) is// additive — consumers read `tracks`/`columns` by name, not by position.@@ -133,7 +142,7 @@ export function runtimeContractMismatch(geometry,contract,appLabel = 'the bound app',- tracks = ANIMATION_TRACKS,+ tracks = getEffectiveAnimationTracks(),) {if (!contract) return null;diff --git a/server/services/sprites/atlasLayout.test.js b/server/services/sprites/atlasLayout.test.jsindex cb681848c7..f8e24b9f85 100644--- a/server/services/sprites/atlasLayout.test.js+++ b/server/services/sprites/atlasLayout.test.js@@ -215,4 +215,22 @@ describe('runtimeContractMismatch', () => {jetpack,)).toMatch(/5 jetpack burst frames.*expects 7/);});++ it('checks the STORED tracks\' contract fields with no table injected (#3152)', () => {+ // The case the injected-table test above cannot cover. `scanner`/`ambient` are+ // no longer compiled rows, so this function's DEFAULT table has to be the+ // effective registry (compiled + store). With the compiled-only default it+ // returns null here — every stored track's `contractFrameCountField` is simply+ // absent from the loop, so a scanner span that disagrees with the app's+ // contract publishes unchecked, which is exactly the mismatch this refuses.+ const geometry = {+ ...geometryFor(8),+ columns: ['idle', ...walkPhaseLabels(8), 'scanner-00', 'scanner-01', 'scanner-02', 'scanner-03'],+ tracks: { idle: span(0, 1, 8), walk: span(1, 8, 8), scanner: span(9, 4, 8) },+ scannerFrameCount: 4,+ };+ expect(runtimeContractMismatch(geometry, { walkFrameCount: 8, scannerFrameCount: 4 })).toBeNull();+ expect(runtimeContractMismatch(geometry, { walkFrameCount: 8, scannerFrameCount: 6 }, 'Example App'))+ .toMatch(/4 scanner action frames.*expects 6/);+ });});