Skip to content

Commit aa48e39

Browse files
committed
fix(followup): share hasUsableAnswer guard; make SuggestionItem.answer optional
Address review comments on this PR: - SuggestionItem.answer is now optional (the model can emit malformed follow-up payloads with a missing answer, issue Zoo-Code-Org#1226) and the Zod schema matches with z.string().optional(), so unguarded reads are caught at compile time instead of failing at runtime. - Add a shared hasUsableAnswer() type guard in @roo-code/types, used by checkAutoApproval, the FollowUpSuggest visible-suggestions filter, and ChatView's suggestion click handler, so the definition of "usable answer" stays in one place. Tests: unit tests for hasUsableAnswer and the relaxed schema in packages/types; new auto-approval case for a non-string first answer; FollowUpSuggest spec now also covers non-string answers.
1 parent b4618e8 commit aa48e39

7 files changed

Lines changed: 108 additions & 18 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { followUpDataSchema, hasUsableAnswer, suggestionItemSchema, type SuggestionItem } from "../followup.js"
2+
3+
describe("hasUsableAnswer", () => {
4+
it("accepts a non-blank string answer", () => {
5+
expect(hasUsableAnswer({ answer: "Yes, proceed" })).toBe(true)
6+
})
7+
8+
it("accepts a non-blank answer with surrounding whitespace, including one that also carries a mode", () => {
9+
const withMode: SuggestionItem = { answer: " spaced ", mode: "code" }
10+
expect(hasUsableAnswer(withMode)).toBe(true)
11+
})
12+
13+
it("rejects an empty or whitespace-only answer", () => {
14+
expect(hasUsableAnswer({ answer: "" })).toBe(false)
15+
expect(hasUsableAnswer({ answer: " \n\t " })).toBe(false)
16+
})
17+
18+
it("rejects a missing answer (issue #1226)", () => {
19+
expect(hasUsableAnswer({})).toBe(false)
20+
expect(hasUsableAnswer({ answer: undefined })).toBe(false)
21+
})
22+
23+
it("rejects non-string answers from malformed transport data (issue #1226)", () => {
24+
expect(hasUsableAnswer({ answer: 42 })).toBe(false)
25+
expect(hasUsableAnswer({ answer: { mode_slug: "code" } })).toBe(false)
26+
expect(hasUsableAnswer({ answer: null })).toBe(false)
27+
})
28+
29+
it("rejects a null or undefined suggestion item", () => {
30+
expect(hasUsableAnswer(null)).toBe(false)
31+
expect(hasUsableAnswer(undefined)).toBe(false)
32+
})
33+
})
34+
35+
describe("suggestionItemSchema", () => {
36+
it("accepts a suggestion without an answer (issue #1226)", () => {
37+
expect(suggestionItemSchema.parse({ mode: "code" })).toEqual({ mode: "code" })
38+
})
39+
40+
it("still rejects a non-string answer", () => {
41+
expect(() => suggestionItemSchema.parse({ answer: 42 })).toThrow()
42+
})
43+
})
44+
45+
describe("followUpDataSchema", () => {
46+
it("accepts suggestions with a mix of usable and missing answers (issue #1226)", () => {
47+
const parsed = followUpDataSchema.parse({
48+
question: "Pick one?",
49+
suggest: [{ answer: "Yes" }, { mode: "code" }, { answer: undefined }],
50+
})
51+
52+
expect(parsed).toEqual({
53+
question: "Pick one?",
54+
suggest: [{ answer: "Yes" }, { mode: "code" }, {}],
55+
})
56+
})
57+
})

‎packages/types/src/followup.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ export interface FollowUpData {
1616
* Interface for a suggestion item with optional mode switching
1717
*/
1818
export interface SuggestionItem {
19-
/** The text of the suggestion */
20-
answer: string
19+
/**
20+
* The text of the suggestion.
21+
*
22+
* Optional because the model can emit malformed follow-up payloads with a
23+
* missing or blank answer (issue #1226) and the extension-host transport
24+
* does not validate `FollowUpData`. Guard with `hasUsableAnswer()` before
25+
* treating the value as usable text.
26+
*/
27+
answer?: string
2128
/** Optional mode to switch to when selecting this suggestion */
2229
mode?: string
2330
}
@@ -35,11 +42,25 @@ export const getSuggestionMode = (mode: unknown): string | undefined => {
3542
return undefined
3643
}
3744

45+
/**
46+
* Whether a follow-up suggestion carries a usable answer: a non-blank string.
47+
*
48+
* `SuggestionItem.answer` is optional because a malformed model payload may
49+
* omit it (issue #1226), and the unvalidated transport may deliver a
50+
* non-string value at runtime. The extension-host auto-approval
51+
* (`checkAutoApproval`), the `FollowUpSuggest` visible-suggestions filter, and
52+
* `ChatView`'s suggestion click handler all guard through this helper so the
53+
* definition of "usable answer" stays in one place.
54+
*/
55+
export const hasUsableAnswer = (
56+
suggestion: { answer?: unknown } | null | undefined,
57+
): suggestion is { answer: string } => typeof suggestion?.answer === "string" && suggestion.answer.trim().length > 0
58+
3859
/**
3960
* Zod schema for SuggestionItem
4061
*/
4162
export const suggestionItemSchema = z.object({
42-
answer: z.string(),
63+
answer: z.string().optional(),
4364
mode: z.string().optional(),
4465
})
4566

‎src/core/auto-approval/__tests__/followup.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ describe("Follow-up question auto-approval", () => {
4949
}
5050
})
5151

52+
it("skips a non-string first answer and uses the next valid suggestion (issue #1226)", async () => {
53+
// A malformed payload may carry a non-string `answer`; the first usable
54+
// suggestion still wins, so the auto-answer must not be dropped.
55+
const result = await run(baseState, followupText([{ answer: 42 }, { answer: "Valid answer" }]))
56+
57+
expect(result.decision).toBe("timeout")
58+
if (result.decision === "timeout") {
59+
expect(result.fn()).toEqual({
60+
askResponse: "messageResponse",
61+
text: "Valid answer",
62+
})
63+
}
64+
})
65+
5266
it("falls back to asking when every suggestion answer is blank or missing (issue #1226)", async () => {
5367
// Before the #1226 fix this scheduled a timeout that auto-answered the
5468
// follow-up with `undefined` text, silently accepting an empty answer.

‎src/core/auto-approval/index.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
hasUsableAnswer,
23
type ClineAsk,
34
type ClineSayTool,
45
type McpServerUse,
@@ -185,9 +186,7 @@ export async function checkAutoApproval({
185186
// content after the timeout (issue #1226), so pick the first suggestion
186187
// with a usable answer. This mirrors the webview's visible-suggestions
187188
// filter in FollowUpSuggest.
188-
const suggestion = suggestions.find(
189-
(item) => typeof item?.answer === "string" && item.answer.trim().length > 0,
190-
)
189+
const suggestion = suggestions.find(hasUsableAnswer)
191190

192191
if (
193192
suggestion &&

‎webview-ui/src/components/chat/ChatView.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { batchNearby } from "@src/utils/batchNearby"
2121
import { isBoundary, isIgnorableBetweenTargets } from "@src/utils/chatBatchingPredicates"
2222

2323
import type { ClineAsk, ClineSayTool, ClineMessage, ExtensionMessage, AudioType, SuggestionItem } from "@roo-code/types"
24-
import { getCompletionCheckpoint, getSuggestionMode, isRetiredProvider } from "@roo-code/types"
24+
import { getCompletionCheckpoint, getSuggestionMode, hasUsableAnswer, isRetiredProvider } from "@roo-code/types"
2525

2626
import { findLast } from "@roo/array"
2727
import { combineApiRequests } from "@roo/combineApiRequests"
@@ -1434,7 +1434,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
14341434
// The model may emit suggestions with missing or blank answers (issue #1226).
14351435
// Ignore them instead of pushing an undefined value into the input, which
14361436
// would crash the text area (inputValue.trim on undefined).
1437-
const answer = typeof suggestion?.answer === "string" ? suggestion.answer.trim() : ""
1437+
const answer = hasUsableAnswer(suggestion) ? suggestion.answer.trim() : ""
14381438
if (!answer) {
14391439
return
14401440
}

‎webview-ui/src/components/chat/FollowUpSuggest.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Button, StandardTooltip } from "@/components/ui"
55

66
import { useAppTranslation } from "@src/i18n/TranslationContext"
77
import { useExtensionState } from "@src/context/ExtensionStateContext"
8-
import { getSuggestionMode, type SuggestionItem } from "@roo-code/types"
8+
import { getSuggestionMode, hasUsableAnswer, type SuggestionItem } from "@roo-code/types"
99
import { cn } from "@/lib/utils"
1010

1111
const DEFAULT_FOLLOWUP_TIMEOUT_MS = 60000
@@ -37,10 +37,7 @@ export const FollowUpSuggest = ({
3737
// as empty buttons, and clicking "Copy to input" on one pushes an undefined
3838
// string into the chat input and crashes the text area (issue #1226), so hide them.
3939
const visibleSuggestions = useMemo(
40-
() =>
41-
suggestions.filter(
42-
(suggestion) => typeof suggestion?.answer === "string" && suggestion.answer.trim().length > 0,
43-
),
40+
() => suggestions.filter((suggestion) => hasUsableAnswer(suggestion)),
4441
[suggestions],
4542
)
4643

‎webview-ui/src/components/chat/__tests__/FollowUpSuggest.spec.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,16 +714,17 @@ describe("FollowUpSuggest", () => {
714714
* Malformed follow-up payloads as they can arrive from the model: the
715715
* extension-host transport does not validate `FollowUpData`, so a
716716
* suggestion item may be missing `answer` or carry a blank value.
717-
* `SuggestionItem` cannot model that, so the fixtures keep their
718-
* malformed shape and cross into the component prop at one documented
719-
* boundary instead of disabling type checks with `any`.
717+
* `SuggestionItem` models a missing or blank `answer` (see
718+
* `hasUsableAnswer`) but not a non-string one, so the fixtures keep
719+
* their malformed shape and cross into the component prop at one
720+
* documented boundary instead of disabling type checks with `any`.
720721
*/
721722
type MalformedSuggestionPayload = { answer?: unknown }
722723

723724
const renderWithMalformedSuggestions = (payloads: MalformedSuggestionPayload[]) => {
724725
// Documented double assertion at the component boundary: FollowUpSuggest
725726
// must tolerate malformed transport data, which `SuggestionItem`
726-
// cannot express (issue #1226).
727+
// cannot fully express (issue #1226).
727728
const suggestions = payloads as unknown as SuggestionItem[]
728729

729730
return renderWithTestProviders(
@@ -737,11 +738,12 @@ describe("FollowUpSuggest", () => {
737738
)
738739
}
739740

740-
it("should not render anything when all answers are blank or missing", () => {
741+
it("should not render anything when all answers are blank, missing, or non-string", () => {
741742
const { container } = renderWithMalformedSuggestions([
742743
{ answer: "" },
743744
{ answer: " \n\t " },
744745
{ answer: undefined },
746+
{ answer: 42 },
745747
])
746748

747749
// Blank suggestions are filtered out, so no buttons or countdown render.

0 commit comments

Comments
 (0)