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: normalize exported media timing and bound speech acknowledgement reads

[._.]/ Adam Eivyf81170b26862
diff --git a/server/services/fableLoom/exportMedia.js b/server/services/fableLoom/exportMedia.js
index 9330c8f6e4..f1ad50e821 100644
--- a/server/services/fableLoom/exportMedia.js
+++ b/server/services/fableLoom/exportMedia.js
@@ -69,15 +69,22 @@ async function probe(path, format = 'mov') {
// MP4 container duration can include discarded subtitles/data. Only retained
// picture/audio timelines determine clip length. WebM output has no extra tracks
// but may omit stream durations, so its own container is a safe fallback.
- for (const stream of [...video, ...audio]) {
- const seconds = stream.duration === undefined && format === 'matroska'
- ? Number(result.format?.duration) : Number(stream.duration);
- stream.durationMs = Math.round((seconds + Math.max(0, Number(stream.start_time) || 0)) * 1000);
- if (!Number.isSafeInteger(stream.durationMs) || stream.durationMs <= 0) {
+ const streams = [...video, ...audio];
+ const origin = Math.max(0, Math.min(...streams.map(stream => Number(stream.start_time) || 0)));
+ for (const stream of streams) {
+ const start = Number(stream.start_time) || 0;
+ const containerEnd = stream.duration === undefined && format === 'matroska';
+ const seconds = containerEnd ? Number(result.format?.duration) : Number(stream.duration);
+ // Sample duration proves truncation independently of ffmpeg's timestamp
+ // rebasing. Playback span retains relative stream offsets, not their common origin.
+ stream.durationMs = Math.round((containerEnd ? seconds - Math.max(0, start) : seconds) * 1000);
+ stream.endMs = Math.round(((containerEnd ? seconds : seconds + start) - origin) * 1000);
+ if (!Number.isSafeInteger(stream.durationMs) || stream.durationMs <= 0
+ || !Number.isSafeInteger(stream.endMs) || stream.endMs <= 0) {
throw refusal('EXPORT_MEDIA_INVALID', 'Retained video/audio duration could not be verified.');
}
}
- return { video: video[0], audio: audio[0], durationMs: Math.max(...[...video, ...audio].map(s => s.durationMs)) };
+ return { video: video[0], audio: audio[0], durationMs: Math.max(...streams.map(stream => stream.endMs)) };
}
export async function fableLoomExportEncoder(kind) {
diff --git a/server/services/fableLoom/exports.test.js b/server/services/fableLoom/exports.test.js
index 7cb824c42c..fe2fdb285d 100644
--- a/server/services/fableLoom/exports.test.js
+++ b/server/services/fableLoom/exports.test.js
@@ -417,6 +417,46 @@ describe('portable FableLoom export boundary', () => {
}
});
+ videoIt('normalizes a common positive timestamp origin while retaining the audio delay', async () => {
+ const source = join(PATHS.videos, 'offset-timeline.mp4');
+ expect(await runFfmpegProcess({ bin, timeoutMs: 10_000, args: [
+ '-nostdin', '-y', '-v', 'error', '-f', 'lavfi', '-i', 'color=c=blue:s=32x32:r=10:d=1',
+ '-itsoffset', '0.3', '-f', 'lavfi', '-i', 'sine=frequency=440:sample_rate=48000:duration=0.4',
+ '-t', '1', '-c:v', 'libx264', '-c:a', 'aac', '-threads', '1', '-output_ts_offset', '5', source,
+ ] })).toEqual({ ok: true });
+ const inspect = file => JSON.parse(execFileSync(probe, ['-v', 'error', '-show_streams', '-of', 'json', file], { timeout: 10_000 }).toString()).streams;
+ const original = inspect(source);
+ const originalDelay = Number(original.find(stream => stream.codec_type === 'audio').start_time)
+ - Number(original.find(stream => stream.codec_type === 'video').start_time);
+ expect(originalDelay).toBeGreaterThan(0.25);
+ const audibleStart = file => {
+ const pcm = execFileSync(bin, ['-nostdin', '-v', 'error', '-i', file, '-map', '0:a',
+ '-af', 'aresample=async=1:first_pts=0', '-ar', '48000', '-ac', '1', '-f', 'f32le', '-'],
+ { timeout: 10_000, maxBuffer: 1024 * 1024 });
+ for (let offset = 0; offset < pcm.length; offset += 4) {
+ if (Math.abs(pcm.readFloatLE(offset)) > 0.005) return offset / 4 / 48000;
+ }
+ throw new Error('Expected the authored delayed tone.');
+ };
+ const originalStart = audibleStart(source);
+ loadHistory.mockResolvedValue(['entry-record', 'hold-record', 'exit-record'].map(id => ({ id, filename: 'offset-timeline.mp4' })));
+ for (const mediaProfile of ['source', 'web-720p']) {
+ const result = await archive(await begin({ episodeIds: ['episode-one'], mediaProfile }));
+ const asset = JSON.parse(result.files.get('manifest.json')).loom.episodes[0].nodes[0].playbackAssets.entry;
+ expect(asset.durationMs).toBe(1000);
+ const output = join(PATHS.videos, 'normalized-timeline.mp4');
+ await writeFile(output, result.files.get(asset.file));
+ const streams = inspect(output);
+ const picture = streams.find(stream => stream.codec_type === 'video');
+ const audio = streams.find(stream => stream.codec_type === 'audio');
+ expect(Number(picture.duration)).toBe(1);
+ expect(Number(audio.duration)).toBeGreaterThanOrEqual(0.4);
+ // AAC priming changes packet start timestamps; compare the audible
+ // timeline with allowance for the loudness filter's short look-ahead.
+ expect(Math.abs(audibleStart(output) - originalStart)).toBeLessThan(0.01);
+ }
+ });
+
videoIt('drains cancelled and failed encoder children before exposing terminal failure without an artifact', async () => {
const realRun = ffmpeg.runFfmpegProcess;
const started = Promise.withResolvers();
diff --git a/server/services/fableLoom/hostedSession.js b/server/services/fableLoom/hostedSession.js
index c57f975341..274302475f 100644
--- a/server/services/fableLoom/hostedSession.js
+++ b/server/services/fableLoom/hostedSession.js
@@ -808,7 +808,10 @@ export async function acknowledgeHostedSpeech(sessionId, {
return { accepted: false, reason: 'INVALID_STATUS' };
}
- const claim = takePendingSpeech(session);
+ if (status === 'ended' && pending.acknowledging) return { accepted: false, reason: 'ACKNOWLEDGEMENT_IN_PROGRESS' };
+ // Keep the original deadline while the authoritative scene read is pending.
+ // Its timeout invalidates this turn, so a delayed read cannot commit later.
+ const claim = status === 'ended' ? pending : takePendingSpeech(session);
if (status !== 'ended') {
if (session.activeTurn?.id === claim.turnId) {
session.activeTurn.abortController?.abort(status === 'failed' ? 'playback_failed' : 'playback_aborted');
@@ -825,12 +828,12 @@ export async function acknowledgeHostedSpeech(sessionId, {
return { accepted: true, committed: false };
}
+ claim.acknowledging = true;
try {
const loom = await getLoom(session.loomId);
if (!speechClaimStillCurrent(session, sessionId, claim)) {
- // The ack already consumed the wait. If the turn is still the speaking
- // one, a scene change that lost the race with this commit has to unlock
- // it — otherwise the room stays in `speaking` with nothing left to ack.
+ // The scene changed while this acknowledgement was being checked.
+ // Release this speaking turn without changing a successor's state.
if (activeSessions.get(sessionId) === session
&& session.status === 'active'
&& session.turnPhase === 'speaking'
@@ -883,6 +886,8 @@ export async function acknowledgeHostedSpeech(sessionId, {
}
console.error(`❌ Hosted speech commit failed: ${err?.message || err}`);
return { accepted: false, committed: false, reason: 'COMMIT_FAILED' };
+ } finally {
+ if (session.pendingSpeech === claim) takePendingSpeech(session);
}
}
diff --git a/server/services/fableLoom/hostedSession.test.js b/server/services/fableLoom/hostedSession.test.js
index 13ea6d8019..9bfc10b67f 100644
--- a/server/services/fableLoom/hostedSession.test.js
+++ b/server/services/fableLoom/hostedSession.test.js
@@ -602,6 +602,32 @@ describe('fableLoom hostedSession', () => {
}
});
+ it('bounds acknowledgement processing when its loom read stalls and rejects the late read', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
+ try {
+ const { session } = await createHostedSession('loom-1', 'ep-1');
+ const io = makeIo();
+ const socketId = connectOutput(session, 'host');
+ const result = await speakMove(session, io);
+ const gate = deferred();
+ records.getLoom.mockImplementationOnce(() => gate.promise);
+ const acknowledgement = acknowledgeHostedSpeech(session.id, {
+ turnId: result.turnId, role: 'host', socketId, status: 'ended', io,
+ });
+ await vi.advanceTimersByTimeAsync(HOSTED_SPEECH_ACK_TIMEOUT_MS);
+ expect(getHostedSession(session.id)).toMatchObject({ currentNodeId: 'node-start', turnPhase: 'idle' });
+ expect(transitions(io)).toHaveLength(0);
+ const next = await startHostedListening(session.id, { io });
+ gate.resolve(mockLoom);
+ await expect(acknowledgement).resolves.toMatchObject({ accepted: false, committed: false });
+ expect(getHostedSession(session.id)).toMatchObject({ currentNodeId: 'node-start', turnPhase: 'listening' });
+ expect(_getInternalSession(session.id).activeTurn.id).toBe(next.turnId);
+ expect(transitions(io)).toHaveLength(0);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('does not emit a transition when playback is aborted, the output disconnects, or the session ends', async () => {
const io = makeIo();
const { session } = await createHostedSession('loom-1', 'ep-1');
diff --git a/server/services/fableLoom/static-player/runtime.js b/server/services/fableLoom/static-player/runtime.js
index 8416c7fc23..fb2f5ac00b 100644
--- a/server/services/fableLoom/static-player/runtime.js
+++ b/server/services/fableLoom/static-player/runtime.js
@@ -175,7 +175,10 @@ export function createSession(manifest, options = {}) {
if (!holds.length) return assetFile(node?.playbackAssets?.entry);
const count = holds.length;
const mode = node?.interactionWindow?.holdLoopRotation;
- const index = mode === 'shuffle' ? Math.abs(holdIteration * 17) % count : holdIteration % count;
+ // Seventeen is prime: fall back to a unit stride when it divides the
+ // collection size so every clip is visited before the rotation repeats.
+ const stride = count % 17 === 0 ? 1 : 17;
+ const index = mode === 'shuffle' ? Math.abs(holdIteration * stride) % count : holdIteration % count;
return holds[index] || holds[0];
}
function currentAsset() {
diff --git a/server/services/fableLoom/static-player/runtime.test.js b/server/services/fableLoom/static-player/runtime.test.js
index 3ff5bd87ef..0e635fddc2 100644
--- a/server/services/fableLoom/static-player/runtime.test.js
+++ b/server/services/fableLoom/static-player/runtime.test.js
@@ -305,6 +305,23 @@ describe('static reference player runtime', () => {
expect(session.snapshot().media.url).not.toBe(first);
});
+ it('makes every hold reachable when shuffle has seventeen assets', () => {
+ const manifest = story();
+ const gate = gateOf(manifest);
+ gate.interactionWindow = { enabled: true, holdLoopRotation: 'shuffle' };
+ gate.playbackAssets.holds = Array.from({ length: 17 }, (_, index) => ({
+ ...clip(), file: `media/${index.toString(16).padStart(64, '0')}.mp4`,
+ }));
+ const session = atGate(manifest);
+ const visited = new Set();
+ for (let index = 0; index < 17; index++) {
+ const { media } = session.snapshot();
+ visited.add(media.url);
+ session.onMediaEnded(media.generation);
+ }
+ expect(visited.size).toBe(17);
+ });
+
it('rejects an unsupported package and an unsafe media path', () => {
expect(() => createSession({ ...story(), schemaVersion: '2.0.0' })).toThrow(/major version 1/);
expect(() => createSession({ ...story(), runtimeVersion: '9.0.0' })).toThrow(/major version 1/);