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: keep shared media-job records from scheduling work or escaping the bucket

[._.]/ Adam Eivy5ae9635cb3d2
diff --git a/server/services/sharing/exporter.js b/server/services/sharing/exporter.js
index 98a1468582..5a6d56de8e 100644
--- a/server/services/sharing/exporter.js
+++ b/server/services/sharing/exporter.js
@@ -38,6 +38,7 @@ import { PORTOS_SCHEMA_VERSIONS } from '../../lib/schemaVersions.js';
import { listSheetPointers } from '../../lib/storyBible.js';
import { resolveBucketSourceName as resolveSourceName } from './annotationIdentity.js';
import { isStr } from '../../lib/textUtils.js';
+import { isSafeRecordId } from '../../lib/validation.js';
/**
* Best-effort cap on the bucket's manifest directory after each export.
@@ -186,7 +187,9 @@ async function jobFromSidecar(jobId) {
// for local renders too (without this, local-render shares lost both fields
// once the in-memory job queue aged out and re-render fell back to defaults).
return {
- id: sc.id || jobId,
+ // The sidecar arrives from buckets and peers verbatim; its own `id` must
+ // not rename the record written into the bucket (a `../` id escapes it).
+ id: jobId,
kind: 'image',
owner: null,
status: 'completed',
@@ -214,7 +217,9 @@ async function jobFromSidecar(jobId) {
* Returns the asset refs (image filenames) discovered.
*/
async function exportMediaJobAndAsset(jobId, bucketPath, cache) {
- if (!jobId || !isStr(jobId)) return [];
+ // jobId comes from shared records (imageJobId) and names the bucket record
+ // file; archives imported before ids were checked may still hold a `../` id.
+ if (!isSafeRecordId(jobId)) return [];
const job = getJob(jobId) || await jobFromSidecar(jobId);
if (!job) {
console.log(`⚠️ sharing.exporter: imageJobId ${jobId} not found in live queue, archive, or sidecar`);
diff --git a/server/services/sharing/importer.js b/server/services/sharing/importer.js
index 77fbb2f1c8..4110b320be 100644
--- a/server/services/sharing/importer.js
+++ b/server/services/sharing/importer.js
@@ -423,6 +423,26 @@ async function processAnnotationManifest(bucket, manifest) {
return { applied: changed.length, changedKeys: changed };
}
+// A bundled media-job record is re-render metadata, never work to resume here:
+// the queue replays a persisted `queued` record at boot and re-enqueues some
+// `running`/remote ones, so a bucket record must not choose its status, carry
+// queue-internal state, or name itself anything but the safe id it was read by.
+// Rebuilt to the exporter's field set (exportMediaJobAndAsset).
+const IMPORTABLE_MEDIA_JOB_KINDS = new Set(['image', 'video']);
+function importableMediaJob(raw, recordId) {
+ if (!isPlainObject(raw) || raw.id !== recordId || raw.status !== 'completed') return null;
+ if (!IMPORTABLE_MEDIA_JOB_KINDS.has(raw.kind)) return null;
+ return {
+ id: raw.id,
+ kind: raw.kind,
+ owner: raw.owner ?? null,
+ status: 'completed',
+ completedAt: isStr(raw.completedAt) ? raw.completedAt : null,
+ params: isPlainObject(raw.params) ? raw.params : {},
+ result: isPlainObject(raw.result) ? raw.result : null,
+ };
+}
+
/** Merge bucket-bundled media-job records into local data/media-jobs.json. */
async function mergeMediaJobRecords(bucketPath, recordIds) {
const mediaDir = bucketRecordsDir(bucketPath, 'media');
@@ -440,8 +460,9 @@ async function mergeMediaJobRecords(bucketPath, recordIds) {
]);
const byId = new Map((persisted.jobs || []).map((j) => [j.id, j]));
let changed = false;
- for (const job of incoming) {
- if (!job?.id || byId.has(job.id)) continue;
+ for (const [index, raw] of incoming.entries()) {
+ const job = importableMediaJob(raw, recordIds[index]);
+ if (!job || byId.has(job.id)) continue;
byId.set(job.id, job);
changed = true;
}
diff --git a/server/services/sharing/integration.test.js b/server/services/sharing/integration.test.js
index 60fd969262..b646037cbf 100644
--- a/server/services/sharing/integration.test.js
+++ b/server/services/sharing/integration.test.js
@@ -338,7 +338,57 @@ describe('sharing round-trip', () => {
id: 'example-manifest', senderInstanceId: 'example-peer', kind: 'media', recordIds: [incoming.id], assetRefs: [],
}));
expect((await importer.processManifest(bucket.id, 'example-media.json')).processed).toBe(true);
- expect(JSON.parse(readFileSync(file, 'utf8'))).toEqual({ jobs: [local, incoming], videoHolds, futureEnvelopeField: 'preserved' });
+ expect(JSON.parse(readFileSync(file, 'utf8'))).toEqual({ jobs: [local, { ...incoming, owner: null, completedAt: null, result: null }],
+ videoHolds, futureEnvelopeField: 'preserved' });
+ });
+
+ it('merges bundled media-job records only as completed re-render metadata under the id they were read by', async () => {
+ const bucket = await buckets.createBucket({ name: 'Example Bucket', path: tempBucket, mode: 'inbox' });
+ const file = join(tempData, 'media-jobs.json');
+ writeFileSync(file, JSON.stringify({ jobs: [] }));
+ const id = (n) => `00000000-0000-4000-8000-00000000001${n}`;
+ // The queue replays a persisted `queued` record at boot; a bucket record
+ // must never schedule work here, rename itself, or pick a non-media kind.
+ const records = {
+ [id(1)]: { id: id(1), kind: 'image', status: 'queued', params: { mode: 'codex', prompt: 'example' } },
+ [id(2)]: { id: '../../escaped', kind: 'image', status: 'completed', params: {} },
+ [id(3)]: { id: id(3), kind: 'training', status: 'completed', params: { runId: 'example' } },
+ [id(4)]: { id: id(4), kind: 'image', status: 'completed', completedAt: '2026-09-05T00:00:00.000Z',
+ durabilityPending: { status: 'completed' }, cancelRequested: true,
+ params: { prompt: 'example' }, result: { filename: `${id(4)}.png` } },
+ };
+ mkdirSync(join(tempBucket, 'records', 'media'), { recursive: true });
+ for (const [recordId, record] of Object.entries(records)) {
+ writeFileSync(join(tempBucket, 'records', 'media', `${recordId}.json`), JSON.stringify(record));
+ }
+ writeFileSync(join(tempBucket, 'manifests', 'example-media.json'), JSON.stringify({
+ id: 'example-manifest', senderInstanceId: 'example-peer', kind: 'media', recordIds: Object.keys(records), assetRefs: [],
+ }));
+ await importer.processManifest(bucket.id, 'example-media.json');
+ expect(JSON.parse(readFileSync(file, 'utf8')).jobs).toEqual([{
+ id: id(4), kind: 'image', owner: null, status: 'completed', completedAt: '2026-09-05T00:00:00.000Z',
+ params: { prompt: 'example' }, result: { filename: `${id(4)}.png` },
+ }]);
+ });
+
+ it('names an exported media record by its job id, never by an imported sidecar id', async () => {
+ const bucket = await buckets.createBucket({ name: 'Example Bucket', path: tempBucket, mode: 'auto-merge' });
+ const universeBuilder = await import('../universeBuilder.js');
+ const mediaCollections = await import('../mediaCollections.js');
+ const u = await universeBuilder.createUniverse({ name: 'Example Universe' });
+ // Sidecars land in data/images verbatim from bucket imports and peer pulls.
+ writeFileSync(join(tempData, 'images', 'shared-job.png'), pngBytes('SHARED'));
+ writeFileSync(join(tempData, 'images', 'shared-job.metadata.json'), JSON.stringify({
+ id: '../../../escaped-record', prompt: 'example', filename: 'shared-job.png',
+ }));
+ const collection = await mediaCollections.findOrCreateCollectionByName({
+ name: `Universe: ${u.name}`, description: 'Linked', universeId: u.id,
+ });
+ await mediaCollections.addItem(collection.id, { kind: 'image', ref: 'shared-job.png' });
+ await exporter.exportUniverse(u.id, bucket.id);
+ expect(existsSync(join(tempBucket, '..', 'escaped-record.json'))).toBe(false);
+ const record = JSON.parse(readFileSync(join(tempBucket, 'records', 'media', 'shared-job.json'), 'utf8'));
+ expect(record).toMatchObject({ id: 'shared-job', params: { prompt: 'example' } });
});
it('exports a series, processes the manifest as inbox, then promotes — id + origin preserved', async () => {