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

address review (codex round 2): treat unreadable video-history as failure, not empty

Adam Eivy4b5fb1fe7863
diff --git a/server/services/mediaAssetIndex/db.js b/server/services/mediaAssetIndex/db.js
index 875acdeabf..44fb3b180a 100644
--- a/server/services/mediaAssetIndex/db.js
+++ b/server/services/mediaAssetIndex/db.js
@@ -18,9 +18,10 @@
* without it.
*/
+import { readFile } from 'fs/promises';
import { join } from 'path';
import { query } from '../../lib/db.js';
-import { PATHS, tryReadFile } from '../../lib/fileUtils.js';
+import { PATHS } from '../../lib/fileUtils.js';
import { imageToRow, videoToRow } from './logic.js';
function rowToAsset(row) {
@@ -92,9 +93,18 @@ export async function listAssets({ kind } = {}) {
// wipe every video row whose file is still on disk. This reader distinguishes
// the two: file absent → genuinely empty (ok); present-but-unparseable → failure
// (not ok), so the caller skips pruning videos. Returns { ok, list }.
-async function readVideoHistoryStrict() {
- const raw = await tryReadFile(join(PATHS.data, 'video-history.json'));
- if (raw == null) return { ok: true, list: [] }; // file absent → no videos yet
+export async function readVideoHistoryStrict(historyPath = join(PATHS.data, 'video-history.json')) {
+ // Read with explicit error-code handling — NOT tryReadFile/readJSONFile, which
+ // both collapse "missing" and "unreadable" to the same value. Only a genuine
+ // ENOENT (file never written) counts as trusted-empty; an EACCES/EIO/transient
+ // failure must be a non-ok read so the caller skips pruning videos.
+ let raw;
+ try {
+ raw = await readFile(historyPath, 'utf-8');
+ } catch (err) {
+ if (err.code === 'ENOENT') return { ok: true, list: [] }; // never written → no videos yet
+ return { ok: false, list: [] }; // unreadable → do NOT treat as empty
+ }
let parsed;
try {
parsed = JSON.parse(raw);
diff --git a/server/services/mediaAssetIndex/reconcile.test.js b/server/services/mediaAssetIndex/reconcile.test.js
new file mode 100644
index 0000000000..b9a0b59e3d
--- /dev/null
+++ b/server/services/mediaAssetIndex/reconcile.test.js
@@ -0,0 +1,78 @@
+/**
+ * No-DB unit test for readVideoHistoryStrict — the failure-vs-empty distinction
+ * that keeps reconcile's prune from wiping live video rows on a transient read.
+ *
+ * The whole point of this reader (vs the live store's loadHistory, which uses
+ * readJSONFile and collapses missing + unreadable + corrupt all to []) is that
+ * ONLY a genuinely-absent file (ENOENT) is trusted-empty; an unreadable or
+ * corrupt file must report ok:false so the caller skips pruning. We exercise all
+ * branches against real temp files (no Postgres needed — the pool is never hit).
+ */
+
+import { describe, it, expect, afterAll } from 'vitest';
+import { mkdtemp, writeFile, rm, chmod } from 'fs/promises';
+import { tmpdir } from 'os';
+import { join } from 'path';
+import { readVideoHistoryStrict } from './db.js';
+
+const dirs = [];
+async function tmpDir() {
+ const d = await mkdtemp(join(tmpdir(), 'mai-reconcile-'));
+ dirs.push(d);
+ return d;
+}
+
+afterAll(async () => {
+ for (const d of dirs) await rm(d, { recursive: true, force: true }).catch(() => {});
+});
+
+describe('readVideoHistoryStrict', () => {
+ it('missing file (ENOENT) → ok, empty (trusted-empty, safe to prune)', async () => {
+ const d = await tmpDir();
+ const res = await readVideoHistoryStrict(join(d, 'does-not-exist.json'));
+ expect(res).toEqual({ ok: true, list: [] });
+ });
+
+ it('valid array → ok, parsed list', async () => {
+ const d = await tmpDir();
+ const p = join(d, 'video-history.json');
+ await writeFile(p, JSON.stringify([{ id: 'a' }, { id: 'b' }]));
+ const res = await readVideoHistoryStrict(p);
+ expect(res.ok).toBe(true);
+ expect(res.list).toHaveLength(2);
+ });
+
+ it('corrupt JSON → NOT ok (must not look empty → skip prune)', async () => {
+ const d = await tmpDir();
+ const p = join(d, 'video-history.json');
+ await writeFile(p, '{ this is not json');
+ const res = await readVideoHistoryStrict(p);
+ expect(res.ok).toBe(false);
+ });
+
+ it('valid JSON but not an array → NOT ok', async () => {
+ const d = await tmpDir();
+ const p = join(d, 'video-history.json');
+ await writeFile(p, JSON.stringify({ not: 'an array' }));
+ const res = await readVideoHistoryStrict(p);
+ expect(res.ok).toBe(false);
+ });
+
+ it('unreadable file (EACCES, not ENOENT) → NOT ok (the bug codex caught)', async () => {
+ // chmod 000 makes the open fail with EACCES, which is NOT ENOENT — the old
+ // tryReadFile path collapsed this to null/empty and would have pruned every
+ // video row. Skip where chmod is ineffective (root, or some CI filesystems).
+ const d = await tmpDir();
+ const p = join(d, 'video-history.json');
+ await writeFile(p, JSON.stringify([{ id: 'a' }]));
+ await chmod(p, 0o000);
+ const stillReadable = await readVideoHistoryStrict(p).then((r) => r.ok && r.list.length > 0).catch(() => false);
+ if (stillReadable) {
+ console.log('⏭️ EACCES case skipped: chmod 000 ineffective on this filesystem/user');
+ return;
+ }
+ const res = await readVideoHistoryStrict(p);
+ expect(res.ok).toBe(false);
+ await chmod(p, 0o644).catch(() => {}); // restore so cleanup can remove it
+ });
+});