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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: serialize episode switches and no-op stale playback updates during one
[._.]/ Adam Eivyb885c221c524
diff --git a/server/services/fableLoom/hostedSession.js b/server/services/fableLoom/hostedSession.jsindex 0151f5a3c4..a9470693b6 100644--- a/server/services/fableLoom/hostedSession.js+++ b/server/services/fableLoom/hostedSession.js@@ -393,6 +393,17 @@ export async function updateHostedSession(sessionId, patch = {}, { io } = {}) {throw new ServerError('Hosted session not found or ended', { status: 404, code: 'SESSION_NOT_FOUND' });}+ // A `switchHostedEpisode` in flight is about to overwrite currentNodeId /+ // playbackPhase / activeHoldIndex with the NEW episode's values anyway. The+ // client's playback-sync effect fires on the very next React commit — well+ // before the switch's async loom-read/preflight work resolves server-side+ // — so a `currentNodeId` racing in during that window legitimately belonged+ // to the OLD episode a moment ago. Treat it as stale and no-op rather than+ // reject it as a `NODE_NOT_IN_EPISODE` bug (#8112).+ if (session.switchingEpisode) {+ return sanitizeHostedSession(session);+ }+if (patch.audioTarget && FABLELOOM_AUDIO_TARGETS.includes(patch.audioTarget)) {session.audioTarget = patch.audioTarget;}@@ -441,6 +452,14 @@ export async function switchHostedEpisode(sessionId, episodeId, { io } = {}) {throw new ServerError('Hosted session not found or ended', { status: 404, code: 'SESSION_NOT_FOUND' });}+ // Serializes switches — without it, two overlapping calls (a host+ // double-clicking "Next Episode" before the first one's async work+ // resolves) could race and let the EARLIER request commit last, rebinding+ // the session to an episode the UI has already left.+ if (session.switchingEpisode) {+ throw new ServerError('An episode switch is already in progress', { status: 409, code: 'EPISODE_SWITCH_IN_PROGRESS' });+ }+// Abort BEFORE the first await: a turn already in flight against the OLD// episode must not be able to resolve and commit narration/a transition// while this function is off awaiting the loom read below.diff --git a/server/services/fableLoom/hostedSession.test.js b/server/services/fableLoom/hostedSession.test.jsindex 43e40f079c..ce1092fd30 100644--- a/server/services/fableLoom/hostedSession.test.js+++ b/server/services/fableLoom/hostedSession.test.js@@ -586,6 +586,47 @@ describe('fableLoom hostedSession', () => {const listenRes = await startHostedListening(session.id);expect(listenRes.ok).toBe(true);});++ // Without this, a host double-clicking "Next Episode" before the first+ // switch's async loom-read/preflight resolves fires two overlapping+ // calls that race — the earlier request could commit last and rebind the+ // session to an episode the UI already left.+ it('rejects a second switch while one is already in flight', async () => {+ const { session } = await createHostedSession('loom-1', 'ep-1');++ const loomRead = deferred();+ records.getLoom.mockImplementationOnce(() => loomRead.promise);++ const pending = switchHostedEpisode(session.id, 'ep-2');+ await expect(switchHostedEpisode(session.id, 'ep-3-not-ready')).rejects.toMatchObject({+ status: 409,+ code: 'EPISODE_SWITCH_IN_PROGRESS',+ });++ loomRead.resolve(mockLoom);+ await expect(pending).resolves.toMatchObject({ ok: true });+ expect(getHostedSession(session.id).episodeId).toBe('ep-2');+ });++ // The client's playback-sync effect fires on the very next React commit+ // after "Next Episode" — well before switchHostedEpisode's async work+ // resolves server-side — so a `hosted:playback:update` racing in during+ // that window carries a currentNodeId that legitimately belonged to the+ // OLD episode a moment ago. It must no-op, not reject as drift (#8112).+ it('treats a currentNodeId update that races with an in-flight switch as a stale no-op', async () => {+ const { session } = await createHostedSession('loom-1', 'ep-1');++ const loomRead = deferred();+ records.getLoom.mockImplementationOnce(() => loomRead.promise);++ const pending = switchHostedEpisode(session.id, 'ep-2');+ const updated = await updateHostedSession(session.id, { currentNodeId: 'node-2' });+ expect(updated.currentNodeId).toBe('node-start');++ loomRead.resolve(mockLoom);+ await pending;+ expect(getHostedSession(session.id).currentNodeId).toBe('node-ep2-start');+ });});describe('expired-session sweep', () => {