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: block new hosted turns while switchHostedEpisode is in flight

[._.]/ Adam Eivy90e6cdd22098
diff --git a/server/services/fableLoom/hostedSession.js b/server/services/fableLoom/hostedSession.js
index e5a4030bb5..0151f5a3c4 100644
--- a/server/services/fableLoom/hostedSession.js
+++ b/server/services/fableLoom/hostedSession.js
@@ -448,6 +448,12 @@ export async function switchHostedEpisode(sessionId, episodeId, { io } = {}) {
session.activeTurn.abortController.abort('episode_switch');
session.activeTurn = null;
}
+ // Blocks `startHostedListening` from starting a NEW turn for the rest of
+ // this function — without it, a turn that starts during the awaits below
+ // would run against the OLD episode and could commit/emit AFTER this
+ // 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;
// 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
@@ -456,23 +462,31 @@ export async function switchHostedEpisode(sessionId, episodeId, { io } = {}) {
// AFTER the room already saw the terminal `hosted:session:ended`.
const isLive = () => activeSessions.get(sessionId) === session && session.status === 'active';
- const loom = await getLoom(session.loomId);
- if (!isLive()) return { ok: false, ended: true, reason: 'session_ended' };
- if (!loom) {
- throw new ServerError('Loom not found', { status: 404, code: 'NOT_FOUND' });
- }
- const episode = findEpisode(loom, episodeId);
+ try {
+ const loom = await getLoom(session.loomId);
+ if (!isLive()) return { ok: false, ended: true, reason: 'session_ended' };
+ if (!loom) {
+ throw new ServerError('Loom not found', { status: 404, code: 'NOT_FOUND' });
+ }
+ const episode = findEpisode(loom, episodeId);
+
+ const preflight = await checkHostedSessionReadiness({ loomId: session.loomId, episodeId, loom, episode });
+ if (!isLive()) return { ok: false, ended: true, reason: 'session_ended' };
+ const startNode = preflight.ready
+ ? episode.nodes?.find((n) => n.id === episode.startNodeId) || null
+ : null;
+ if (!preflight.ready || !startNode) {
+ endHostedSession(sessionId, { reason: 'episode_not_ready', io });
+ return { ok: false, ended: true, reason: 'episode_not_ready', preflight };
+ }
- const preflight = await checkHostedSessionReadiness({ loomId: session.loomId, episodeId, loom, episode });
- if (!isLive()) return { ok: false, ended: true, reason: 'session_ended' };
- const startNode = preflight.ready
- ? episode.nodes?.find((n) => n.id === episode.startNodeId) || null
- : null;
- if (!preflight.ready || !startNode) {
- endHostedSession(sessionId, { reason: 'episode_not_ready', io });
- return { ok: false, ended: true, reason: 'episode_not_ready', preflight };
+ return commitHostedEpisodeSwitch(session, sessionId, episodeId, startNode, { io });
+ } finally {
+ session.switchingEpisode = false;
}
+}
+function commitHostedEpisodeSwitch(session, sessionId, episodeId, startNode, { io }) {
const now = new Date();
session.episodeId = episodeId;
session.currentNodeId = startNode.id;
@@ -527,6 +541,14 @@ export async function startHostedListening(sessionId, { io } = {}) {
throw new ServerError('Session is not active', { status: 400, code: 'SESSION_INACTIVE' });
}
+ // `switchHostedEpisode` holds this while it's off awaiting the loom read
+ // and readiness preflight. Without this gate a turn could start against the
+ // OLD episode mid-switch, then commit and emit AFTER the new episode's
+ // `hosted:session:sync` — mixing old-episode content into the new episode.
+ if (session.switchingEpisode) {
+ throw new ServerError('An episode switch is in progress', { status: 409, code: 'EPISODE_SWITCH_IN_PROGRESS' });
+ }
+
const loom = await getLoom(session.loomId);
const episode = loom.episodes?.find((e) => e.id === session.episodeId) || null;
const node = episode?.nodes?.find((n) => n.id === session.currentNodeId) || null;
diff --git a/server/services/fableLoom/hostedSession.test.js b/server/services/fableLoom/hostedSession.test.js
index 24edd505bc..43e40f079c 100644
--- a/server/services/fableLoom/hostedSession.test.js
+++ b/server/services/fableLoom/hostedSession.test.js
@@ -563,6 +563,29 @@ describe('fableLoom hostedSession', () => {
expect(getHostedSession(session.id)).toBeNull();
expect(io.emits.at(-1).event).toBe('hosted:session:ended');
});
+
+ // Without this gate a turn that starts while the switch is off awaiting
+ // the loom read/preflight would run against the OLD episode and could
+ // commit and emit AFTER the new episode's hosted:session:sync, mixing
+ // old-episode content into the new episode.
+ it('refuses to start a new turn while a switch is in flight, and allows one once it settles', 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(startHostedListening(session.id)).rejects.toMatchObject({
+ status: 409,
+ code: 'EPISODE_SWITCH_IN_PROGRESS',
+ });
+
+ loomRead.resolve(mockLoom);
+ await pending;
+
+ const listenRes = await startHostedListening(session.id);
+ expect(listenRes.ok).toBe(true);
+ });
});
describe('expired-session sweep', () => {