fix: make session resume atomic across restarts

This commit is contained in:
User
2026-07-15 00:42:35 +02:00
parent 2b4797f133
commit 08b1f4909e
12 changed files with 1082 additions and 107 deletions
+19 -12
View File
@@ -32,7 +32,8 @@ import { useEffect, useMemo, useRef, useState } from "react";
*/
export function AppShell() {
const [activeSessionId, setActiveSessionId] = useState<string | null>(null);
const [streamGeneration, setStreamGeneration] = useState(0);
const activeSessionIdRef = useRef<string | null>(null);
const [streamCursorResetEpoch, setStreamCursorResetEpoch] = useState(0);
const [creatingSession, setCreatingSession] = useState(false);
const [awaitingQuestion, setAwaitingQuestion] = useState(false);
const { data: sessions = [] } = useQuery<SessionSummary[]>({
@@ -65,6 +66,12 @@ export function AppShell() {
const selectedSessions = sessions.filter((session) => selectedSessionIds.has(session.id));
const allSessionsSelected = sessions.length > 0 && selectedSessions.length === sessions.length;
function selectActiveSession(id: string | null) {
// Keep async Resume completions synchronized before React commits the state update.
activeSessionIdRef.current = id;
setActiveSessionId(id);
}
// A background refresh can remove a session (for example from another browser).
// Keep the local selection aligned with the authoritative list.
useEffect(() => {
@@ -100,9 +107,9 @@ export function AppShell() {
});
}
async function doResume(id: string) {
const reconnectSameSession = activeSessionId === id;
try {
const result = await resumeSession(id);
const reconnectSameSession = activeSessionIdRef.current === id;
setPanelSession(null);
setAwaitingQuestion(false);
@@ -115,11 +122,11 @@ export function AppShell() {
recordLifecycle("Resuming session");
setAgentActive(true);
}
setActiveSessionId(id);
// The backend has now completed clear/rebind. Recreate a same-id source with its cursor;
// switching ids naturally creates a new source and resets the cursor in the stream hook.
selectActiveSession(id);
// A cold runtime starts a fresh SSE id sequence. Recreate a same-id source only after
// Resume succeeds, and explicitly discard the old runtime's cursor.
if (!result.alreadyActive && reconnectSameSession) {
setStreamGeneration((value) => value + 1);
setStreamCursorResetEpoch((value) => value + 1);
}
// Paint the persisted re-entry phase while the replacement stream starts replaying.
@@ -182,7 +189,7 @@ export function AppShell() {
targets.filter((_, index) => results[index].status === "fulfilled").map((session) => session.id),
);
if (deletedIds.has(panelSession?.id ?? "")) setPanelSession(null);
if (deletedIds.has(activeSessionId ?? "")) { resetSession(); setActiveSessionId(null); }
if (deletedIds.has(activeSessionId ?? "")) { resetSession(); selectActiveSession(null); }
setSelectedSessionIds((current) => new Set([...current].filter((id) => !deletedIds.has(id))));
refresh();
if (deletedIds.size !== targets.length) {
@@ -226,7 +233,7 @@ export function AppShell() {
// session sits idle or a gate awaits the reviewer (pendingWidget).
const running = working && !finalized;
useSessionStream(activeSessionId, streamGeneration);
useSessionStream(activeSessionId, 0, streamCursorResetEpoch);
// A backend "session_exit" system event (e.g. the replay server emitting it
// when the reviewer picks "Esci") asks us to leave the live session view and
@@ -240,7 +247,7 @@ export function AppShell() {
// Never let a streamed event terminate the managed Pi child. Only the
// explicit “Stop & save” action is allowed to call /close.
resetSession();
setActiveSessionId(null);
selectActiveSession(null);
setAwaitingQuestion(false);
}
// The final workflow turn ends with the session already finalized on disk:
@@ -254,7 +261,7 @@ export function AppShell() {
resetSession();
setAwaitingQuestion(true);
setCreatingSession(false);
setActiveSessionId(null);
selectActiveSession(null);
// Best effort only: session creation keeps the authoritative readiness gate.
// Composer focus is deliberately independent of this network request.
void prewarmRuntime().catch(() => undefined);
@@ -269,7 +276,7 @@ export function AppShell() {
function finishSessionCreation(id: string) {
// React batches these updates, preserving the provisional view and timer
// while useSessionStream opens the durable session's SSE channel.
setActiveSessionId(id);
selectActiveSession(id);
setCreatingSession(false);
setAwaitingQuestion(false);
refresh();
@@ -287,7 +294,7 @@ export function AppShell() {
await closeSession(activeSessionId);
} finally {
resetSession();
setActiveSessionId(null);
selectActiveSession(null);
setAwaitingQuestion(false);
}
}