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: stage a repeated federated conditioning path once (#4827)

[._.]/ Adam Eivy257d26358f8f
diff --git a/server/services/federatedMedia/inputAssets.js b/server/services/federatedMedia/inputAssets.js
index a676649aff..b874553db2 100644
--- a/server/services/federatedMedia/inputAssets.js
+++ b/server/services/federatedMedia/inputAssets.js
@@ -130,11 +130,11 @@ export function inputAssetRejection(capability, assets = []) {
* allowlist, the digest verification — lives here, where the next non-image
* sub-request will not inherit it.
*
- * The `localPath -> assetId` memo is per run and deliberately not persisted: an
- * id names a slot in the provider's TTL-swept staging area, so caching it across
- * a restart would produce a confident reference to bytes that are gone. Within
- * one run the memo means a retried submission re-sends the body once, not once
- * per attempt.
+ * The `localPath` memo is per run and deliberately not persisted: an id names a
+ * slot in the provider's TTL-swept staging area, so caching it across a restart
+ * would produce a confident reference to bytes that are gone. Within one run it
+ * collapses a repeated path — two identical reference images, or a source frame
+ * reused as the last frame — into a single upload.
*
* @param {object} ctx
* @param {(path: string, options?: object, requestOptions?: object) => Promise<any>} ctx.requestJson
@@ -142,12 +142,9 @@ export function inputAssetRejection(capability, assets = []) {
* @returns {(localPath: string) => Promise<string>}
*/
function createInputAssetStager({ requestJson, emitStatus }) {
- const assetIds = new Map();
-
- return async function stageInputAsset(localPath) {
- const cached = assetIds.get(localPath);
- if (cached) return cached;
+ const uploads = new Map();
+ async function uploadInputAsset(localPath) {
// Re-anchored against the approved image roots on every attempt, exactly as
// the LOCAL runner re-validates the same input. The marker is persisted,
// user-editable queue state, so the path that becomes an outbound upload has
@@ -184,10 +181,7 @@ function createInputAssetStager({ requestJson, emitStatus }) {
// normal miss (never uploaded, or swept), not an error.
const staged = await requestJson(`/api/federation/media/v1/assets/${assetId}`)
.then((body) => federatedMediaAssetSchema.safeParse(body), () => null);
- if (staged?.success && staged.data.sha256 === digest) {
- assetIds.set(localPath, staged.data.assetId);
- return staged.data.assetId;
- }
+ if (staged?.success && staged.data.sha256 === digest) return staged.data.assetId;
const body = await readFile(resolved).catch(() => null);
if (!body) {
@@ -222,8 +216,19 @@ function createInputAssetStager({ requestJson, emitStatus }) {
'MEDIA_PROVIDER_ASSET_INTEGRITY',
);
}
- assetIds.set(localPath, parsed.data.assetId);
return parsed.data.assetId;
+ }
+
+ // Keyed on the in-flight PROMISE, not the resolved id: the caller stages every
+ // path concurrently, so memoizing only completed uploads would let a repeated
+ // path start a second transfer of the same bytes before the first could
+ // populate the cache — the exact case the memo exists to collapse.
+ return function stageInputAsset(localPath) {
+ const pending = uploads.get(localPath);
+ if (pending) return pending;
+ const upload = uploadInputAsset(localPath);
+ uploads.set(localPath, upload);
+ return upload;
};
}
diff --git a/server/services/imageGen/remote.test.js b/server/services/imageGen/remote.test.js
index 9bbdd0d371..9bedf5069e 100644
--- a/server/services/imageGen/remote.test.js
+++ b/server/services/imageGen/remote.test.js
@@ -276,6 +276,52 @@ describe('federated image consumer adapter', () => {
expect(submission[1].body).not.toContain('init.png');
});
+ // A render can legitimately name one file twice — two identical reference
+ // images, or a video source frame reused as the last frame. Every path is
+ // staged concurrently, so the per-run memo has to hold the in-flight upload
+ // rather than the finished id, or both copies transfer the same bytes.
+ it('stages a repeated conditioning path once', async () => {
+ writeFileSync(join(tempImageDir, 'init.png'), png);
+ transport.fetch.mockImplementation(async (url, options) => {
+ if (url.includes('/assets/')) return jsonResponse({ code: 'MEDIA_PROVIDER_ASSET_NOT_FOUND' }, 404);
+ if (url.endsWith('/assets') && options.method === 'POST') {
+ return jsonResponse({
+ wireVersion: 1,
+ assetId: ASSET_ID,
+ sha256: sha256(png),
+ sizeBytes: png.length,
+ mimeType: 'image/png',
+ expiresAt: '2026-08-22T18:00:00.000Z',
+ }, 201);
+ }
+ if (url.endsWith('/jobs') && options.method === 'POST') return jsonResponse(providerJob('canceled'), 202);
+ throw new Error(`Unexpected test URL: ${url}`);
+ });
+
+ const base = params();
+ const terminal = captureTerminal(LOCAL_JOB_ID);
+ await generateImage({
+ ...base,
+ remoteMedia: {
+ ...base.remoteMedia,
+ inputAssets: [
+ { role: 'referenceImages', path: 'init.png' },
+ { role: 'referenceImages', path: 'init.png' },
+ ],
+ },
+ }).catch(() => {});
+ await terminal;
+
+ const uploads = transport.fetch.mock.calls
+ .filter(([url, options]) => url.endsWith('/assets') && options.method === 'POST');
+ expect(uploads).toHaveLength(1);
+ // Both slots still resolve — deduping the transfer must not drop a ref.
+ const submission = transport.fetch.mock.calls
+ .find(([url, options]) => url.endsWith('/jobs') && options.method === 'POST');
+ expect(JSON.parse(submission[1].body).referenceImages)
+ .toEqual([{ assetId: ASSET_ID }, { assetId: ASSET_ID }]);
+ });
+
// Content addressing only pays off if BOTH sides can compute the address.
// The consumer derives the id from its own instance id and the file digest,
// asks, and sends nothing when the peer already has it — which is what makes