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: self-correct a hosted episode switch that settles on a stale target

[._.]/ Adam Eivy17c762ddaaf9
diff --git a/client/src/components/fableloom/LoomPlayPanel.jsx b/client/src/components/fableloom/LoomPlayPanel.jsx
index c000470692..fc4cc04283 100644
--- a/client/src/components/fableloom/LoomPlayPanel.jsx
+++ b/client/src/components/fableloom/LoomPlayPanel.jsx
@@ -119,6 +119,11 @@ export default function LoomPlayPanel({ loom, episode: initialEpisode, onClose }
// resulting `hosted:session:ended{reason:'episode_not_ready'}` can name it
// in the toast even though the server payload carries only the reason.
const pendingHostedEpisodeSwitchRef = useRef(null);
+ // Always the CURRENT episode id, read from the `hosted:session:sync`
+ // handler below (which closes over the socket-connect effect's render and
+ // would otherwise see a stale `episode`).
+ const latestEpisodeIdRef = useRef(episode.id);
+ useEffect(() => { latestEpisodeIdRef.current = episode.id; }, [episode.id]);
// Socket connection when hosted session is active
useEffect(() => {
@@ -152,6 +157,22 @@ export default function LoomPlayPanel({ loom, episode: initialEpisode, onClose }
}
});
+ // The server rejects a second `hosted:episode:switch` sent while an
+ // earlier one is still in flight (409 EPISODE_SWITCH_IN_PROGRESS) — a
+ // host who advances episodes faster than that round trip would otherwise
+ // strand the hosted session on whichever earlier target won the race.
+ // Every `hosted:session:sync` (including the one the in-flight switch
+ // itself broadcasts on commit) names the server's current episode; if it
+ // no longer matches what the host is actually showing, request the
+ // CURRENT one again rather than leaving the mismatch unresolved (#8112).
+ socket.on('hosted:session:sync', (data) => {
+ if (data?.episodeId && data.episodeId !== latestEpisodeIdRef.current) {
+ hostedEpisodeIdRef.current = latestEpisodeIdRef.current;
+ pendingHostedEpisodeSwitchRef.current = { id: latestEpisodeIdRef.current, label: pendingHostedEpisodeSwitchRef.current?.label };
+ hostedSocketRef.current?.emit('hosted:episode:switch', { episodeId: latestEpisodeIdRef.current });
+ }
+ });
+
socket.on('hosted:story:transition', (data) => {
if (data.node) {
setScene(data.node);
diff --git a/client/src/components/fableloom/LoomPlayPanel.test.jsx b/client/src/components/fableloom/LoomPlayPanel.test.jsx
index a8823c6a0e..97cab26f99 100644
--- a/client/src/components/fableloom/LoomPlayPanel.test.jsx
+++ b/client/src/components/fableloom/LoomPlayPanel.test.jsx
@@ -451,6 +451,47 @@ describe('LoomPlayPanel', () => {
expect(toast.error).toHaveBeenCalledWith(expect.stringContaining('Episode 2'));
});
+ // #8112: the server rejects a second hosted:episode:switch sent while an
+ // earlier one is still in flight (409 EPISODE_SWITCH_IN_PROGRESS). A host
+ // who advances past the in-flight target before it settles would otherwise
+ // strand the hosted session on that earlier, now-stale episode with no
+ // retry. Every hosted:session:sync — including the one the in-flight
+ // switch itself broadcasts on commit — names the server's current
+ // episode; a mismatch against what the host is showing now must trigger a
+ // corrective re-request.
+ it('re-requests the current episode when session:sync settles on a stale target', async () => {
+ const user = userEvent.setup();
+ const first = {
+ id: 'ep-1', number: 1, title: 'One', startNodeId: 'end-1',
+ nodes: [{ id: 'end-1', title: 'First ending', prose: 'Episode one ends.', isEnding: true, transitions: [] }],
+ };
+ const second = {
+ id: 'ep-2', number: 2, title: 'Two', startNodeId: 'end-2',
+ nodes: [{ id: 'end-2', title: 'Second ending', prose: 'Episode two ends too.', isEnding: true, transitions: [] }],
+ };
+ const third = {
+ id: 'ep-3', number: 3, title: 'Three', startNodeId: 'start-3',
+ nodes: [{ id: 'start-3', title: 'Third opening', prose: 'Episode three begins.', transitions: [] }],
+ };
+ render(<LoomPlayPanel loom={{ ...loom, episodes: [first, second, third] }} episode={first} />);
+
+ await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
+ await user.click(screen.getByRole('button', { name: 'Start hosted session' }));
+
+ await user.click(screen.getByRole('button', { name: 'Next: Episode 2' }));
+ await user.click(screen.getByRole('button', { name: 'Next: Episode 3' }));
+
+ const hostedSocket = io.mock.results.at(-1).value;
+ hostedSocket.emit.mockClear();
+
+ // The in-flight switch for episode 2 finally commits and broadcasts its
+ // own sync — stale, because the host is already showing episode 3.
+ const syncHandler = hostedSocket.on.mock.calls.find(([event]) => event === 'hosted:session:sync')?.[1];
+ act(() => { syncHandler({ episodeId: 'ep-2' }); });
+
+ expect(hostedSocket.emit).toHaveBeenCalledWith('hosted:episode:switch', { episodeId: 'ep-3' });
+ });
+
it('shows an authored overnight voicemail at an episode boundary', async () => {
const first = {
id: 'ep-1', number: 1, title: 'One', startNodeId: 'end-1',