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: recheck hosted session liveness after every await in switchHostedEpisode

[._.]/ Adam Eivy0d01b92fc1e2
diff --git a/server/services/fableLoom/hostedSession.js b/server/services/fableLoom/hostedSession.js
index 4cb2389e2a..e5a4030bb5 100644
--- a/server/services/fableLoom/hostedSession.js
+++ b/server/services/fableLoom/hostedSession.js
@@ -449,13 +449,22 @@ export async function switchHostedEpisode(sessionId, episodeId, { io } = {}) {
session.activeTurn = null;
}
+ // 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
+ // loom or running preflight. Without this a switch that loses that race
+ // would resurrect a deleted session's state and emit `hosted:session:sync`
+ // 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);
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;
diff --git a/server/services/fableLoom/hostedSession.test.js b/server/services/fableLoom/hostedSession.test.js
index da9115d29b..24edd505bc 100644
--- a/server/services/fableLoom/hostedSession.test.js
+++ b/server/services/fableLoom/hostedSession.test.js
@@ -543,6 +543,26 @@ describe('fableLoom hostedSession', () => {
const { session } = await createHostedSession('loom-1', 'ep-1');
await expect(switchHostedEpisode(session.id, 'not-an-episode')).rejects.toMatchObject({ status: 404 });
});
+
+ // A host "end", DELETE /sessions/:id, or the TTL sweep can land while
+ // switchHostedEpisode is off awaiting the loom read or preflight. Losing
+ // that race must not resurrect the deleted session's state or emit
+ // hosted:session:sync AFTER the room already saw hosted:session:ended.
+ it('does not resurrect a session torn down while awaiting the loom read', async () => {
+ const io = makeIo();
+ const { session } = await createHostedSession('loom-1', 'ep-1');
+
+ const loomRead = deferred();
+ records.getLoom.mockImplementationOnce(() => loomRead.promise);
+
+ const pending = switchHostedEpisode(session.id, 'ep-2', { io });
+ endHostedSession(session.id, { reason: 'host_ended', io });
+ loomRead.resolve(mockLoom);
+
+ await expect(pending).resolves.toMatchObject({ ok: false, reason: 'session_ended' });
+ expect(getHostedSession(session.id)).toBeNull();
+ expect(io.emits.at(-1).event).toBe('hosted:session:ended');
+ });
});
describe('expired-session sweep', () => {