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: reject hosted listens after an episode switch
[._.]/ Adam Eivyf6838d449bdb
diff --git a/server/services/fableLoom/hostedSession.js b/server/services/fableLoom/hostedSession.jsindex a9470693b6..1b85e331d9 100644--- a/server/services/fableLoom/hostedSession.js+++ b/server/services/fableLoom/hostedSession.js@@ -312,6 +312,8 @@ export async function createHostedSession(loomId, episodeId, {hostSocketId: null,audienceSocketId: null,activeTurn: null,+ switchingEpisode: false,+ episodeSwitchGeneration: 0,createdAt: now.toISOString(),expiresAt,};@@ -473,6 +475,7 @@ export async function switchHostedEpisode(sessionId, episodeId, { io } = {}) {// switch's `hosted:session:sync`, mixing old-episode content into the new// episode. Always cleared in `finally`, including on every early return.session.switchingEpisode = true;+ session.episodeSwitchGeneration += 1;// Re-checked after every await below — a host "end", DELETE /sessions/:id,// or the TTL sweep can tear this session down while we're off reading the@@ -568,8 +571,17 @@ export async function startHostedListening(sessionId, { io } = {}) {throw new ServerError('An episode switch is in progress', { status: 409, code: 'EPISODE_SWITCH_IN_PROGRESS' });}+ const episodeId = session.episodeId;+ const switchGeneration = session.episodeSwitchGeneration;const loom = await getLoom(session.loomId);- const episode = loom.episodes?.find((e) => e.id === session.episodeId) || null;+ if (activeSessions.get(sessionId) !== session || session.status !== 'active') {+ throw new ServerError('Session is not active', { status: 400, code: 'SESSION_INACTIVE' });+ }+ if (session.switchingEpisode || session.episodeSwitchGeneration !== switchGeneration || session.episodeId !== episodeId) {+ throw new ServerError('Session changed while starting the listening turn', { status: 409, code: 'EPISODE_SWITCH_IN_PROGRESS' });+ }++ const episode = loom.episodes?.find((e) => e.id === episodeId) || null;const node = episode?.nodes?.find((n) => n.id === session.currentNodeId) || null;const asset = resolvePlaybackPhaseAsset({node,diff --git a/server/services/fableLoom/hostedSession.test.js b/server/services/fableLoom/hostedSession.test.jsindex ce1092fd30..7d4603b9ff 100644--- a/server/services/fableLoom/hostedSession.test.js+++ b/server/services/fableLoom/hostedSession.test.js@@ -587,6 +587,28 @@ describe('fableLoom hostedSession', () => {expect(listenRes.ok).toBe(true);});+ it('rejects a listen request whose loom read resumes after an episode switch', async () => {+ const io = makeIo();+ const { session } = await createHostedSession('loom-1', 'ep-1');+ const pendingRead = deferred();+ records.getLoom+ .mockImplementationOnce(() => pendingRead.promise)+ .mockResolvedValueOnce(mockLoom);++ const pendingListen = startHostedListening(session.id, { io });+ await switchHostedEpisode(session.id, 'ep-2', { io });+ const syncIndex = io.emits.findLastIndex((event) => event.event === 'hosted:session:sync');++ pendingRead.resolve(mockLoom);+ await expect(pendingListen).rejects.toMatchObject({+ status: 409,+ code: 'EPISODE_SWITCH_IN_PROGRESS',+ });++ expect(getHostedSession(session.id)).toMatchObject({ episodeId: 'ep-2', turnPhase: 'idle' });+ expect(io.emits.slice(syncIndex + 1).some((event) => event.event === 'hosted:turn:phase')).toBe(false);+ });+// 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