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([issue-4188]): codex review — stable frame-extraction id and full gallery-URL normalization

[._.]/ Adam Eivy6609d0f33818
diff --git a/client/src/lib/moodBoardItemSrc.js b/client/src/lib/moodBoardItemSrc.js
index 47341212c8..cc46f4220b 100644
--- a/client/src/lib/moodBoardItemSrc.js
+++ b/client/src/lib/moodBoardItemSrc.js
@@ -30,13 +30,36 @@ export function moodBoardItemSrc(item) {
return null;
}
+// Reduce an item's `imageUrl` to its on-disk gallery filename, mirroring the
+// server's `imageUrlToAppAsset`/`localImageFilename` contract (and the
+// client's `startingImageSrc`): remote/inline schemes and non-gallery
+// absolute paths are rejected; a `/data/images/...` path OR a legacy
+// bare/relative ref reduces to its basename, stripping any query/hash
+// BEFORE the basename (a suffix can itself contain a slash). The URL path
+// segment is percent-encoded, so decode it back to the on-disk name.
+function galleryFilenameFromImageUrl(imageUrl) {
+ if (typeof imageUrl !== 'string' || !imageUrl.trim()) return null;
+ const raw = imageUrl.trim();
+ if (/^(https?:|data:|blob:)/i.test(raw)) return null;
+ const GALLERY_PREFIX = '/data/images/';
+ let name;
+ if (raw.startsWith(GALLERY_PREFIX)) name = raw.slice(GALLERY_PREFIX.length);
+ else if (raw.startsWith('/')) return null; // some other absolute path → not a gallery image
+ else name = raw; // legacy bare gallery ref
+ let filename = name.split(/[?#]/)[0].split('/').pop() || '';
+ try { filename = decodeURIComponent(filename); } catch { /* keep raw */ }
+ if (!filename || filename === '.' || filename === '..' || filename.includes('/')) return null;
+ return filename;
+}
+
// Resolve a board item to a prompt-from-media source (#4188 Phase 3) — the
// gallery-item shape PromptFromMedia's `initialSource` expects. Returns null
// when the item's media isn't a local gallery asset the analyzer can read:
// text items, external-URL pins, and legacy `video:<id>` pins on image items.
// A video item resolves by FILENAME (`kind:'video'` with no id — the server
// accepts filename in place of the history id); an image item resolves by its
-// `image:<file>` media-key or a `/data/images/<file>` app-path imageUrl.
+// `image:<file>` media-key or a gallery-shaped imageUrl (app path or legacy
+// bare ref).
export function moodBoardItemAnalysisSource(item) {
if (item?.type === 'video') {
if (typeof item?.mediaKey === 'string' && item.mediaKey.startsWith(VIDEO_PREFIX)) {
@@ -50,11 +73,9 @@ export function moodBoardItemAnalysisSource(item) {
const filename = item.mediaKey.slice(IMAGE_PREFIX.length);
if (filename) return { filename, previewUrl: moodBoardItemSrc(item) };
}
- const GALLERY_PREFIX = '/data/images/';
- if (typeof item?.imageUrl === 'string' && item.imageUrl.startsWith(GALLERY_PREFIX)) {
- let filename = item.imageUrl.slice(GALLERY_PREFIX.length);
- try { filename = decodeURIComponent(filename); } catch { /* keep raw */ }
- if (filename && !filename.includes('/')) return { filename, previewUrl: item.imageUrl };
+ const filename = galleryFilenameFromImageUrl(item?.imageUrl);
+ if (filename) {
+ return { filename, previewUrl: `/data/images/${encodeURIComponent(filename)}` };
}
return null;
}
diff --git a/client/src/lib/moodBoardItemSrc.test.js b/client/src/lib/moodBoardItemSrc.test.js
index efb073a6d6..0332a0469a 100644
--- a/client/src/lib/moodBoardItemSrc.test.js
+++ b/client/src/lib/moodBoardItemSrc.test.js
@@ -64,6 +64,17 @@ describe('moodBoardItemAnalysisSource (#4188 Phase 3)', () => {
expect(moodBoardItemAnalysisSource({ type: 'image', imageUrl: '/data/images/my%20render.png' }))
.toEqual({ filename: 'my render.png', previewUrl: '/data/images/my%20render.png' });
});
+ it('normalizes legacy bare refs and query/hash-suffixed gallery URLs (server imageUrlToAppAsset parity)', () => {
+ expect(moodBoardItemAnalysisSource({ type: 'image', imageUrl: 'render.png' }))
+ .toEqual({ filename: 'render.png', previewUrl: '/data/images/render.png' });
+ expect(moodBoardItemAnalysisSource({ type: 'image', imageUrl: '/data/images/ref.png?v=2#top' }))
+ .toEqual({ filename: 'ref.png', previewUrl: '/data/images/ref.png' });
+ // Query/hash strips BEFORE the basename — a suffix containing a slash
+ // must not swap in a different asset name.
+ expect(moodBoardItemAnalysisSource({ type: 'image', imageUrl: '/data/images/photo.png?source=/other.png' }))
+ .toEqual({ filename: 'photo.png', previewUrl: '/data/images/photo.png' });
+ expect(moodBoardItemAnalysisSource({ type: 'image', imageUrl: '/data/other/ref.png' })).toBeNull();
+ });
it('returns null for text items, external pins, and legacy video: pins on image items', () => {
expect(moodBoardItemAnalysisSource({ type: 'text', text: 'n' })).toBeNull();
expect(moodBoardItemAnalysisSource({ type: 'image', imageUrl: 'https://x/y.png' })).toBeNull();
diff --git a/server/services/mediaPromptFromMedia.js b/server/services/mediaPromptFromMedia.js
index d9bdd6a1d2..17706e0ff4 100644
--- a/server/services/mediaPromptFromMedia.js
+++ b/server/services/mediaPromptFromMedia.js
@@ -208,7 +208,10 @@ async function resolveVideoSource(videoId) {
// (`video:<file>.mp4` — see isVideoItemMediaKey), not by history id, so the
// board's analyze flow resolves a gallery video directly by filename instead
// of a history lookup. safeUnder guards traversal; the extension gate keeps
-// this to actual clip files.
+// this to actual clip files. The extraction id is STABLE (derived from the
+// filename stem, like the id-based path's `pfm-<videoId>`) so re-analyzing
+// the same clip overwrites its frames instead of accumulating new sets under
+// data/video-thumbnails.
async function resolveVideoFilename(filename) {
if (!VIDEO_EXT_RE.test(filename || '')) {
throw new ServerError(`Not a gallery video filename: ${filename}`, {
@@ -220,7 +223,8 @@ async function resolveVideoFilename(filename) {
if (!videoPath || !existsSync(videoPath)) {
throw new ServerError('Video file not found on disk', { status: 404, code: 'NOT_FOUND' });
}
- return sampleVideoFrames(videoPath, `pfm-vf-${randomUUID()}`);
+ const stem = filename.replace(/\.[^.]+$/, '').replace(/[^a-zA-Z0-9_-]/g, '_');
+ return sampleVideoFrames(videoPath, `pfm-vf-${stem}`);
}
async function resolveUploadSource(filename) {
diff --git a/server/services/mediaPromptFromMedia.test.js b/server/services/mediaPromptFromMedia.test.js
index 9b36f8f886..c1a1b75809 100644
--- a/server/services/mediaPromptFromMedia.test.js
+++ b/server/services/mediaPromptFromMedia.test.js
@@ -205,9 +205,11 @@ describe('promptFromMedia', () => {
});
expect(history.loadHistory).not.toHaveBeenCalled();
+ // The extraction id is stable per clip (filename stem), so re-analysis
+ // overwrites the same frame set instead of accumulating new ones.
expect(ffmpeg.extractEvaluationFrames).toHaveBeenCalledWith(
'/mock/videos/clip.webm',
- expect.stringMatching(/^pfm-vf-/),
+ 'pfm-vf-clip',
expect.any(Number),
);
expect(result.mediaKind).toBe('video');