Repository navigation
fix(app): handle question response races clearly #839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f8298d
fix(app): handle question response races clearly
Astro-Han c7b8b39
fix(app): block stale question dock resubmits
Astro-Han a857d01
fix(app): make question closing state reactive
Astro-Han b8732de
test(app): cover pending question dock controls
Astro-Han dfd9c03
fix(app): clarify unknown question errors
Astro-Han File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 132 additions & 6 deletions
138
packages/app/src/pages/session/composer/session-question-dock.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,165 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { resolveSkipAction } from "./session-question-dock" | ||
| import { runBrowserCheck } from "@/testing/browser-subprocess" | ||
| import { | ||
| createQuestionResponseGuard, | ||
| isSameQuestionRequest, | ||
| normalizeToolRespondError, | ||
| resolveSkipAction, | ||
| } from "./session-question-dock" | ||
|
|
||
| describe("resolveSkipAction", () => { | ||
| test("navigates to next unsettled question when one exists after current", () => { | ||
| // 3 questions: Q0 settled, Q1 unsettled, Q2 (current) just skipped → now settled | ||
| const isSettled = (i: number) => i !== 1 | ||
| const result = resolveSkipAction(2, isSettled, 3) | ||
| expect(result).toEqual({ type: "navigate", tab: 1 }) | ||
| }) | ||
|
|
||
| test("navigates to first unsettled overall when nothing after current", () => { | ||
| // 3 questions: Q0 unsettled, Q1 settled, Q2 (current) just skipped → settled | ||
| const isSettled = (i: number) => i !== 0 | ||
| const result = resolveSkipAction(2, isSettled, 3) | ||
| expect(result).toEqual({ type: "navigate", tab: 0 }) | ||
| }) | ||
|
|
||
| test("submits when there is only one question and it was just skipped", () => { | ||
| // Single question: Q0 just skipped → settled | ||
| const isSettled = () => true | ||
| const result = resolveSkipAction(0, isSettled, 1) | ||
| expect(result).toEqual({ type: "submit" }) | ||
| }) | ||
|
|
||
| test("submits when all questions are settled after skipping the last one", () => { | ||
| // 3 questions: all settled (Q0 and Q1 answered, Q2 just skipped) | ||
| const isSettled = () => true | ||
| const result = resolveSkipAction(2, isSettled, 3) | ||
| expect(result).toEqual({ type: "submit" }) | ||
| }) | ||
|
|
||
| test("navigates to next unsettled before current when current is not the last", () => { | ||
| // 3 questions: Q0 settled, Q1 (current) just skipped → settled, Q2 unsettled | ||
| const isSettled = (i: number) => i !== 2 | ||
| const result = resolveSkipAction(1, isSettled, 3) | ||
| expect(result).toEqual({ type: "navigate", tab: 2 }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("normalizeToolRespondError", () => { | ||
| test("normalizes plain already_resolved objects without exposing [object Object]", () => { | ||
| const result = normalizeToolRespondError({ error: "already_resolved" }) | ||
|
|
||
| expect(result).toEqual({ type: "already_resolved", requestID: undefined }) | ||
| expect(JSON.stringify(result)).not.toContain("[object Object]") | ||
| }) | ||
|
|
||
| test("keeps answer_count_mismatch details readable for 422 responses", () => { | ||
| const result = normalizeToolRespondError({ | ||
| response: { status: 422 }, | ||
| error: "answer_count_mismatch", | ||
| details: { expected: 2, received: 1 }, | ||
| }) | ||
|
|
||
| expect(result).toEqual({ | ||
| type: "invalid_payload", | ||
| detail: 'answer_count_mismatch {"expected":2,"received":1}', | ||
| }) | ||
| expect(JSON.stringify(result)).not.toContain("[object Object]") | ||
| }) | ||
|
|
||
| test("maps plain no_pending_tool_call body errors to stale session", () => { | ||
| expect(normalizeToolRespondError({ error: "no_pending_tool_call" })).toEqual({ type: "stale_session" }) | ||
| }) | ||
|
|
||
| test("keeps plain answer_count_mismatch body details readable without a status", () => { | ||
| const result = normalizeToolRespondError({ | ||
| error: "answer_count_mismatch", | ||
| details: { expected: 2, received: 1 }, | ||
| }) | ||
|
|
||
| expect(result).toEqual({ | ||
| type: "invalid_payload", | ||
| detail: 'answer_count_mismatch {"expected":2,"received":1}', | ||
| }) | ||
| expect(JSON.stringify(result)).not.toContain("[object Object]") | ||
| }) | ||
|
|
||
| test("supports common error shapes without stringifying unknown objects", () => { | ||
| expect(normalizeToolRespondError(new Error("network failed"))).toEqual({ | ||
| type: "unknown", | ||
| detail: "network failed", | ||
| }) | ||
| expect(normalizeToolRespondError("offline")).toEqual({ type: "unknown", detail: "offline" }) | ||
| expect(normalizeToolRespondError({ status: 404 })).toEqual({ type: "stale_session" }) | ||
| expect(normalizeToolRespondError({ statusCode: 409, request: { id: "req_1" } })).toEqual({ | ||
| type: "already_resolved", | ||
| requestID: "req_1", | ||
| }) | ||
| expect(normalizeToolRespondError({ nested: true })).toEqual({ type: "unknown" }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("question response local completion guard", () => { | ||
| const request = { id: "req_1", sessionID: "ses_1", messageID: "msg_1", callID: "call_1" } | ||
|
|
||
| test("does not treat already_resolved as completion without a same-request local submit", () => { | ||
| expect(isSameQuestionRequest(undefined, request, "req_1")).toBe(false) | ||
| expect(isSameQuestionRequest({ ...request, id: "req_other" }, request, "req_1")).toBe(false) | ||
| expect(isSameQuestionRequest({ ...request, callID: "call_other" }, request, "req_1")).toBe(false) | ||
| }) | ||
|
|
||
| test("treats already_resolved as idempotent only for the same local request", () => { | ||
| expect(isSameQuestionRequest(request, request, "req_1")).toBe(true) | ||
| expect(isSameQuestionRequest(request, request)).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe("question response duplicate submission guard", () => { | ||
| test("blocks repeated response attempts while pending and while waiting for sync close", () => { | ||
| const guard = createQuestionResponseGuard("req_1") | ||
|
|
||
| expect(guard.begin("req_1")).toBe(true) | ||
| expect(guard.begin("req_1")).toBe(false) | ||
|
|
||
| guard.confirm("req_1") | ||
|
|
||
| expect(guard.canInteract("req_1")).toBe(false) | ||
| expect(guard.begin("req_1")).toBe(false) | ||
| }) | ||
|
|
||
| test("restores interaction after failed submit or a new request", () => { | ||
| const guard = createQuestionResponseGuard("req_1") | ||
|
|
||
| expect(guard.begin("req_1")).toBe(true) | ||
| guard.fail("req_1") | ||
| expect(guard.begin("req_1")).toBe(true) | ||
|
|
||
| guard.confirm("req_1") | ||
|
|
||
| expect(guard.canInteract("req_1")).toBe(false) | ||
| expect(guard.canInteract("req_2")).toBe(true) | ||
| expect(guard.begin("req_2")).toBe(true) | ||
| }) | ||
|
|
||
| test("updates reactive disabled state immediately after begin and confirm", () => { | ||
| runBrowserCheck(String.raw` | ||
| import { createMemo, createRoot } from "solid-js" | ||
| import { createQuestionResponseGuard } from "./src/pages/session/composer/session-question-dock.tsx" | ||
|
|
||
| const assert = (condition, message) => { | ||
| if (!condition) throw new Error(message) | ||
| } | ||
|
|
||
| createRoot((dispose) => { | ||
| const guard = createQuestionResponseGuard("req_1") | ||
| const disabled = createMemo(() => !guard.canInteract("req_1")) | ||
|
|
||
| assert(disabled() === false, "initial request should be interactive") | ||
| assert(guard.begin("req_1") === true, "first begin should submit") | ||
| assert(disabled() === true, "begin should immediately disable reactive UI") | ||
|
|
||
| guard.fail("req_1") | ||
| assert(disabled() === false, "failure should immediately restore interaction") | ||
| assert(guard.begin("req_1") === true, "retry should submit after failure") | ||
| guard.confirm("req_1") | ||
| assert(disabled() === true, "confirmed response should stay disabled while waiting for sync close") | ||
|
|
||
| dispose() | ||
| }) | ||
| `) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.