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

fix: fingerprint cached image-to-3D sources (#4654)

[._.]/ Adam Eivy56b2e64888aa
diff --git a/server/services/imageTo3d/sourceKeying.js b/server/services/imageTo3d/sourceKeying.js
index 3678241bf4..935fe3e15c 100644
--- a/server/services/imageTo3d/sourceKeying.js
+++ b/server/services/imageTo3d/sourceKeying.js
@@ -42,7 +42,7 @@ import {
detectSolidBorderColor as detectSolidBorderColorShared,
hasMeaningfulAlpha as hasMeaningfulAlphaShared,
} from '../../lib/borderKey.js';
-import { atomicWrite, readJSONFile } from '../../lib/fileUtils.js';
+import { atomicWrite, readJSONFile, sha256File } from '../../lib/fileUtils.js';
import { decodeRgbaFrame, encodePng } from '../../lib/imageRgba.js';
/** Euclidean RGB distance for the border/flood match (0–441 scale). */
@@ -219,11 +219,9 @@ const hasFreshKeyedSource = async (sourcePath, targetPath) => {
const targetStats = await stat(targetPath).catch(() => null);
if (!targetStats?.isFile() || targetStats.mtimeMs <= sourceStats.mtimeMs) return false;
- // A missing metadata sidecar is accepted for compatibility with the first
- // cache implementation. New outputs write one, so future algorithm/tolerance
- // bumps invalidate all outputs produced by this version.
const metadata = await readJSONFile(keyedCacheMetadataPath(targetPath), null, { logError: false });
- return !metadata?.version || metadata.version === KEYING_CACHE_VERSION;
+ if (metadata?.version !== KEYING_CACHE_VERSION || !metadata.sourceSha256) return false;
+ return metadata.sourceSha256 === await sha256File(sourcePath);
};
/**
@@ -259,6 +257,9 @@ export async function prepareSourceImage({ sourcePath, targetPath }) {
width: frame.width,
height: frame.height,
}));
- await atomicWrite(keyedCacheMetadataPath(targetPath), { version: KEYING_CACHE_VERSION }).catch(() => {});
+ await atomicWrite(keyedCacheMetadataPath(targetPath), {
+ version: KEYING_CACHE_VERSION,
+ sourceSha256: await sha256File(sourcePath),
+ }).catch(() => {});
return targetPath;
}
diff --git a/server/services/imageTo3d/sourceKeying.test.js b/server/services/imageTo3d/sourceKeying.test.js
index 9b394eaf42..d94222379e 100644
--- a/server/services/imageTo3d/sourceKeying.test.js
+++ b/server/services/imageTo3d/sourceKeying.test.js
@@ -10,6 +10,7 @@ import {
KEYING_CACHE_VERSION,
prepareSourceImage,
} from './sourceKeying.js';
+import { sha256File } from '../../lib/fileUtils.js';
// Build a raw RGBA buffer from a painter function (x, y) => [r, g, b, a?].
const makeImage = (width, height, paint) => {
@@ -161,6 +162,11 @@ describe('prepareSourceImage', () => {
it('reuses a keyed target newer than the gallery source', async () => {
const sourcePath = await writePng('cache-source.png', subjectOnGreen());
const cachedTarget = await writePng('cache-target.png', makeImage(20, 20, () => [255, 0, 0, 255]));
+ const sourceSha256 = await sha256File(sourcePath);
+ await writeFile(`${cachedTarget}.meta.json`, JSON.stringify({
+ version: KEYING_CACHE_VERSION,
+ sourceSha256,
+ }));
const fresh = new Date(Date.now() + 60_000);
await utimes(cachedTarget, fresh, fresh);
@@ -170,6 +176,30 @@ describe('prepareSourceImage', () => {
expect([...data.slice(0, 4)]).toEqual([255, 0, 0, 255]);
});
+ it('rekeys when source bytes change despite an older source mtime', async () => {
+ const sourcePath = await writePng('fingerprint-source.png', subjectOnGreen());
+ const cachedTarget = await writePng('fingerprint-target.png', makeImage(20, 20, () => [255, 0, 0, 255]));
+ await writeFile(`${cachedTarget}.meta.json`, JSON.stringify({
+ version: KEYING_CACHE_VERSION,
+ sourceSha256: await sha256File(sourcePath),
+ }));
+ const targetTime = new Date(Date.now() + 60_000);
+ await utimes(cachedTarget, targetTime, targetTime);
+
+ await writePng('fingerprint-source.png', makeImage(20, 20, (x, y) => (
+ x >= 6 && x < 14 && y >= 6 && y < 14 ? [90, 80, 200] : GREEN
+ )));
+ const oldSourceTime = new Date(Date.now() - 60_000);
+ await utimes(sourcePath, oldSourceTime, oldSourceTime);
+
+ const result = await prepareSourceImage({ sourcePath, targetPath: cachedTarget });
+ expect(result).toBe(cachedTarget);
+ const { data } = await sharp(cachedTarget).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
+ expect(alphaAt({ data, width: 20, height: 20 }, 0, 0)).toBe(0);
+ expect(JSON.parse(await readFile(`${cachedTarget}.meta.json`, 'utf8')).sourceSha256)
+ .toBe(await sha256File(sourcePath));
+ });
+
it('recomputes a fresh target when its keying-version metadata is stale', async () => {
const sourcePath = await writePng('version-source.png', subjectOnGreen());
const targetPath = await writePng('version-target.png', makeImage(20, 20, () => [255, 0, 0, 255]));
@@ -182,6 +212,6 @@ describe('prepareSourceImage', () => {
const { data } = await sharp(targetPath).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
expect(alphaAt({ data, width: 20, height: 20 }, 0, 0)).toBe(0);
expect(JSON.parse(await readFile(`${targetPath}.meta.json`, 'utf8')))
- .toEqual({ version: KEYING_CACHE_VERSION });
+ .toEqual({ version: KEYING_CACHE_VERSION, sourceSha256: await sha256File(sourcePath) });
});
});