From 8bd943a8cacc91230a920c1a0ce6a710b33b71b0 Mon Sep 17 00:00:00 2001 From: Miguel Palhas Date: Sat, 5 Sep 2026 08:16:14 +0100 Subject: [PATCH] fix(web): release accepted reconnect submit --- web/src/structured.test.ts | 29 +++++++++++++++++++++++++++++ web/src/structured.ts | 17 +++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/web/src/structured.test.ts b/web/src/structured.test.ts index 7e60ebd..1ea3243 100644 --- a/web/src/structured.test.ts +++ b/web/src/structured.test.ts @@ -452,6 +452,35 @@ describe("structured connection: reconnection/error/loading", () => { conn.dispose(); }); + it("releases when reconnect idle arrives before submission acceptance", async () => { + let accept!: () => void; + vi.mocked(sendStructuredPrompt).mockImplementationOnce( + () => new Promise((resolve) => { accept = resolve; }), + ); + const first = new FakeSocket(); + const second = new FakeSocket(); + const conn = mount({ sockets: [first, second], shouldReconnect: () => true }); + first.open(); + first.emit({ type: "transcript_snapshot", snapshot: { cursor: 0, rows: [] } }); + first.emit({ type: "reduced_state", state: idleState }); + const input = conn.el.querySelector(".structured-input") as HTMLTextAreaElement; + input.value = "accepted after reconnect"; + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true })); + + first.drop(); + vi.advanceTimersByTime(1000); + second.open(); + second.emit({ type: "transcript_snapshot", snapshot: { cursor: 0, rows: [] } }); + second.emit({ type: "reduced_state", state: idleState }); + expect(input.disabled).toBe(true); + + accept(); + await Promise.resolve().then(() => Promise.resolve()); + + expect(input.disabled).toBe(false); + conn.dispose(); + }); + it("reports a daemon rejection reason and does not schedule a retry", () => { const only = new FakeSocket(); const conn = mount({ sockets: [only], shouldReconnect: () => true }); diff --git a/web/src/structured.ts b/web/src/structured.ts index 93f5736..0425dc6 100644 --- a/web/src/structured.ts +++ b/web/src/structured.ts @@ -113,6 +113,7 @@ export function createStructuredConnection( let awaitingBeginGeneration: number | null = null; let acceptedSubmission: { generation: number; socketGeneration: number } | null = null; let socketGeneration = 0; + let stateSocketGeneration = 0; function lastRowIdOfKind(kind: TranscriptRowKind): string | null { for (let i = rows.length - 1; i >= 0; i--) if (rows[i].kind === kind) return rows[i].id; @@ -227,17 +228,22 @@ export function createStructuredConnection( cancelBtn.hidden = !state.turn.responding && !state.turn.thinking; } - function applyState(next: ReducedState, connectionGeneration: number): void { - state = next; + function reconcileAwaitingBegin(): void { if (state.turn.responding || state.turn.thinking) { awaitingBeginGeneration = null; } else if ( awaitingBeginGeneration !== null && acceptedSubmission?.generation === awaitingBeginGeneration - && connectionGeneration > acceptedSubmission.socketGeneration + && stateSocketGeneration > acceptedSubmission.socketGeneration ) { awaitingBeginGeneration = null; } + } + + function applyState(next: ReducedState, connectionGeneration: number): void { + state = next; + stateSocketGeneration = connectionGeneration; + reconcileAwaitingBegin(); const newPendingId = state.pending_approval ? lastRowIdOfKind("approval") : null; if (newPendingId !== pendingApprovalRowId) { const prev = pendingApprovalRowId; @@ -282,6 +288,7 @@ export function createStructuredConnection( const text = input.value; if (!text.trim() || input.disabled) return; const generation = ++submissionGeneration; + const submissionSocketGeneration = socketGeneration; awaitingBeginGeneration = generation; acceptedSubmission = null; input.value = ""; @@ -291,7 +298,9 @@ export function createStructuredConnection( sendStructuredPrompt(sessionId, text).then( () => { if (generation === submissionGeneration) { - acceptedSubmission = { generation, socketGeneration }; + acceptedSubmission = { generation, socketGeneration: submissionSocketGeneration }; + reconcileAwaitingBegin(); + renderComposerState(); } }, (e: unknown) => {