Skip to content

Commit bb6e254

Browse files
easonLiangWorldedtecheasonliang28edelauna
authored
fix(webview): ignore blank or missing follow-up suggestion answers (Zoo-Code-Org#1286)
* fix(webview): ignore blank or missing follow-up suggestion answers The model can emit follow-up suggestions with a missing or blank answer. Previously the blank suggestion rendered as an empty button, clicking Copy to input on one pushed undefined into the input state and crashed the webview (TypeError reading trim), and the extension-side auto-approval timeout could still fire and silently auto-answer the question with no content. Fixes Zoo-Code-Org#1226 * harden(webview): normalize non-string chat input and cover remaining branches Address CodeRabbit review on Zoo-Code-Org#1226: normalize inputValue once by type check (not only nullish) so every string operation in ChatTextArea is safe, and treat any non-string value as empty. Add non-string regression tests, an empty-draft shift-click case, and a no-text follow-up auto-approval case so the previously partial branches are covered. * test(webview): cover the remaining normalized-input string operations Add regression tests for the ChatTextArea code paths touched by the Zoo-Code-Org#1226 normalization that were not covered before: the insertTextIntoTextarea message handler, the Backspace mention inspection, the pending-mention removal (both branches), and the pasted-URL insertion. This closes the codecov/patch missing lines (ChatTextArea.tsx 167, 516, 517, 544, 546, 685). * refactor(test): type follow-up auto-approval state instead of casting Address CodeRabbit review: remove the as-never casts in followup.spec.ts and type the helper with Pick<ExtensionState, AutoApprovalState | AutoApprovalStateOptions>, which the partial baseState already satisfies. * test(webview): assert the normalized textarea value for malformed inputs Address the CodeRabbit nitpick on the Zoo-Code-Org#1226 regression tests: assert the rendered textarea value is empty for both the undefined and non-string inputValue cases, covering the normalizedInputValue binding before the Enhance prompt interaction. * refactor(test): model malformed follow-up fixtures as unknown in FollowUpSuggest spec * 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. * fix(webview): share first-usable-suggestion rule and add mutation coverage Address the round-2 mutation-diff results for the blank/missing follow-up suggestion fix: - Add a shared firstUsableSuggestion() helper to @roo-code/types and use it in the extension-host auto-approval path, so the "first usable suggestion" rule lives in one place. - Cover the remaining surviving mutants with regression tests in ChatTextArea, ChatView, and FollowUpSuggest (stale effect deps, cursor handling, trim on shift-click, and the answered state). --------- Co-authored-by: Eason Liang <[email protected]> Co-authored-by: edelauna <[email protected]>
1 parent 2ecbf35 commit bb6e254

10 files changed

Lines changed: 840 additions & 39 deletions

File tree

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

‎packages/types/src/followup.ts‎

Lines changed: 34 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,35 @@ 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 SuggestionItem & { answer: string } =>
58+
typeof suggestion?.answer === "string" && suggestion.answer.trim().length > 0
59+
60+
/**
61+
* The first suggestion with a usable answer, if any.
62+
*
63+
* Shared by the extension-host auto-approval (`checkAutoApproval`) and the
64+
* webview so the "first usable suggestion" rule stays in one place.
65+
*/
66+
export const firstUsableSuggestion = (suggestions?: SuggestionItem[] | null): SuggestionItem | undefined =>
67+
(suggestions ?? []).find((s) => hasUsableAnswer(s))
68+
3869
/**
3970
* Zod schema for SuggestionItem
4071
*/
4172
export const suggestionItemSchema = z.object({
42-
answer: z.string(),
73+
answer: z.string().optional(),
4374
mode: z.string().optional(),
4475
})
4576

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import type { ExtensionState } from "@roo-code/types"
2+
import { checkAutoApproval, type AutoApprovalState, type AutoApprovalStateOptions } from ".."
3+
4+
type AutoApprovalFields = Pick<ExtensionState, AutoApprovalState | AutoApprovalStateOptions>
5+
6+
describe("Follow-up question auto-approval", () => {
7+
const baseState: AutoApprovalFields = {
8+
autoApprovalEnabled: true,
9+
alwaysAllowFollowupQuestions: true,
10+
followupAutoApproveTimeoutMs: 10_000,
11+
}
12+
13+
const followupText = (suggest: unknown) => JSON.stringify({ question: "Pick one?", suggest }) as string
14+
15+
const run = (state: AutoApprovalFields, text: string) => checkAutoApproval({ state, ask: "followup", text })
16+
17+
it("schedules a timeout that auto-answers with the first valid suggestion", async () => {
18+
const result = await run(baseState, followupText([{ answer: "Yes, proceed" }]))
19+
20+
expect(result.decision).toBe("timeout")
21+
if (result.decision === "timeout") {
22+
expect(result.timeout).toBe(10_000)
23+
expect(result.fn()).toEqual({
24+
askResponse: "messageResponse",
25+
text: "Yes, proceed",
26+
})
27+
}
28+
})
29+
30+
it("falls back to asking when the follow-up has no text payload", async () => {
31+
// Exercises the `text || "{}"` fallback: a follow-up without any payload must
32+
// not schedule an auto-answer timeout.
33+
const result = await checkAutoApproval({ state: baseState, ask: "followup" })
34+
35+
expect(result).toEqual({ decision: "ask" })
36+
})
37+
38+
it("skips a blank or missing first answer and uses the next valid suggestion (issue #1226)", async () => {
39+
// Mirrors a malformed model response where JSON round-tripping drops
40+
// `answer: undefined` and the first item is unusable.
41+
const result = await run(baseState, followupText([{}, { answer: " " }, { answer: "Valid answer" }]))
42+
43+
expect(result.decision).toBe("timeout")
44+
if (result.decision === "timeout") {
45+
expect(result.fn()).toEqual({
46+
askResponse: "messageResponse",
47+
text: "Valid answer",
48+
})
49+
}
50+
})
51+
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+
66+
it("falls back to asking when every suggestion answer is blank or missing (issue #1226)", async () => {
67+
// Before the #1226 fix this scheduled a timeout that auto-answered the
68+
// follow-up with `undefined` text, silently accepting an empty answer.
69+
const result = await run(baseState, followupText([{ answer: "" }, { answer: " \n\t " }, {}]))
70+
71+
expect(result).toEqual({ decision: "ask" })
72+
})
73+
74+
it("falls back to asking when the suggestion answer is not a string", async () => {
75+
const result = await run(baseState, followupText([{ answer: 42 }]))
76+
77+
expect(result).toEqual({ decision: "ask" })
78+
})
79+
80+
it("falls back to asking when the follow-up has no suggestions", async () => {
81+
const result = await run(baseState, JSON.stringify({ question: "Pick one?" }))
82+
83+
expect(result).toEqual({ decision: "ask" })
84+
})
85+
86+
it("falls back to asking when the follow-up text is not valid JSON", async () => {
87+
const result = await run(baseState, "not-json")
88+
89+
expect(result).toEqual({ decision: "ask" })
90+
})
91+
92+
it("falls back to asking when the auto-approve timeout is not positive", async () => {
93+
const result = await run({ ...baseState, followupAutoApproveTimeoutMs: 0 }, followupText([{ answer: "Yes" }]))
94+
95+
expect(result).toEqual({ decision: "ask" })
96+
})
97+
98+
it("does not auto-approve when follow-up auto-approval is disabled", async () => {
99+
const result = await run(
100+
{ ...baseState, alwaysAllowFollowupQuestions: false },
101+
followupText([{ answer: "Yes" }]),
102+
)
103+
104+
expect(result).toEqual({ decision: "ask" })
105+
})
106+
107+
it("does not auto-approve when global auto-approval is disabled", async () => {
108+
const result = await run({ ...baseState, autoApprovalEnabled: false }, followupText([{ answer: "Yes" }]))
109+
110+
expect(result).toEqual({ decision: "ask" })
111+
})
112+
})

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
firstUsableSuggestion,
23
type ClineAsk,
34
type ClineSayTool,
45
type McpServerUse,
@@ -179,7 +180,11 @@ export async function checkAutoApproval({
179180
if (ask === "followup") {
180181
if (state.alwaysAllowFollowupQuestions === true) {
181182
try {
182-
const suggestion = (JSON.parse(text || "{}") as FollowUpData).suggest?.[0]
183+
// A missing or blank answer would auto-approve the follow-up with no
184+
// content after the timeout (issue #1226), so pick the first suggestion
185+
// with a usable answer. This mirrors the webview's visible-suggestions
186+
// filter in FollowUpSuggest.
187+
const suggestion = firstUsableSuggestion((JSON.parse(text || "{}") as FollowUpData).suggest)
183188

184189
if (
185190
suggestion &&

0 commit comments

Comments
 (0)