Skip to content

Commit d8a91c9

Browse files
committed
fix: a question card with no ids answered every question at once
The planner's card is the product's first impression and had never been checked. The agent itself is fine — three prompts in two languages each produced a valid, correctly localised card of four questions. The parser was not. It never required an id, but the component stores every answer under choices[q.id] and keys the list by it, so when the model left ids out both questions landed on undefined: picking an option in the first one filled in the second, and React saw duplicate keys. The model writes this JSON, so an id is a suggestion; the position is what is actually unique. A supplied id is kept only when it is a non-empty string that has not already been used. scripts/check-question-card.mjs covers nine shapes a model plausibly emits, including a half-streamed block and malformed JSON. It fails when the fix is reverted — verified, not assumed. A script rather than a suite because the frontend has no test runner and one pure function does not justify standing one up.
1 parent e4fe8c9 commit d8a91c9

3 files changed

Lines changed: 159 additions & 9 deletions

File tree

‎HANDOFF.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,26 @@ lost their borders — a bordered button inside a bordered capsule was the
757757
same double-frame problem as the chat bubble; they are now text/icons with
758758
a hover wash, and the single solid fill marks Sign Up as the one primary.
759759

760+
## 07:50 (7-29) — 问题卡片:模型漏了 id,回答一题就等于回答了全部
761+
762+
planner 的问题卡片是产品的第一印象,但从没验证过。先实测三种 prompt
763+
(中/英/中),**agent 三次都产出了合法、本地化正确的卡片**(各 4 题),
764+
这条链路本身是好的。
765+
766+
问题在解析器:它不要求 `id` 字段,但组件里**所有答案都存在 `choices[q.id]`
767+
下面**,`key={q.id}` 也用它。模型漏写 id 时两题的 id 都是 `undefined` ——
768+
**选了第一题的选项,第二题也跟着被填上**,React key 还会撞。这份 JSON 是
769+
模型写的,id 是建议而不是契约。
770+
771+
解析时改成:模型给的 id 只在"是字符串、非空、且没重复过"时才采用,否则用
772+
位置 `q${index}` —— 位置才是真正唯一的东西。实测五种形状(无 id / 重复 id
773+
/ 空字符串 id / 数字 id / 正常 id)现在都是唯一且互相隔离的。
774+
775+
`scripts/check-question-card.mjs`:9 种模型可能写出的形状的自检(含半截
776+
流式块、malformed JSON、选项为空)。**验证过它会失败** —— 把 fix 还原后
777+
脚本报错退出 1,恢复后通过。前端没有测试框架,为一个纯函数立一套 runner
778+
比这个函数本身还重,所以用可直接运行的脚本。
779+
760780
## 07:20 (7-29) — 页面可以导出成 PDF 了
761781

762782
并发那条线挖干净了,这轮换新需求。open-design 主打 "real files, HTML/PDF

‎frontend/src/components/chat/question-card.tsx‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,30 @@ export function extractQuestions(content: string): {
4444
if (!match) return { block: null, rest: content };
4545
try {
4646
const parsed = JSON.parse(match[1]);
47-
const questions = Array.isArray(parsed?.questions)
48-
? parsed.questions.filter(
49-
(q: AgentQuestion) =>
50-
q &&
51-
typeof q.label === 'string' &&
52-
Array.isArray(q.options) &&
53-
q.options.length > 0
54-
)
55-
: [];
47+
const seen = new Set<string>();
48+
const questions: AgentQuestion[] = (
49+
Array.isArray(parsed?.questions)
50+
? parsed.questions.filter(
51+
(q: AgentQuestion) =>
52+
q &&
53+
typeof q.label === 'string' &&
54+
Array.isArray(q.options) &&
55+
q.options.length > 0
56+
)
57+
: []
58+
).map((q: AgentQuestion, index: number) => {
59+
// Every answer is stored under `q.id`, so a missing or repeated id made
60+
// two questions share one slot: picking an option in the first one
61+
// filled in the second, and both rendered under the same React key. The
62+
// model writes this JSON, so the id is a suggestion — the position is
63+
// what is actually unique.
64+
const id =
65+
typeof q.id === 'string' && q.id && !seen.has(q.id)
66+
? q.id
67+
: `q${index}`;
68+
seen.add(id);
69+
return { ...q, id };
70+
});
5671
if (questions.length === 0) return { block: null, rest: content };
5772
return {
5873
block: { intro: parsed.intro, questions },

‎scripts/check-question-card.mjs‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env node
2+
/**
3+
* The planner's question block is written by a model, so its shape is a
4+
* suggestion rather than a contract. This checks the parser survives what a
5+
* model plausibly emits.
6+
*
7+
* A script rather than a test suite: the frontend has no test runner, and
8+
* standing one up for one pure function is more machinery than the function.
9+
* Run it directly — `node scripts/check-question-card.mjs`.
10+
*/
11+
import assert from 'node:assert/strict';
12+
import { readFileSync } from 'node:fs';
13+
14+
// The parser lives in a .tsx module, so read it and lift the two pieces that
15+
// matter rather than pull a TypeScript toolchain into a check script.
16+
const source = readFileSync(
17+
new URL('../frontend/src/components/chat/question-card.tsx', import.meta.url),
18+
'utf8',
19+
);
20+
21+
const FENCE = /```codefox-questions\s*\n([\s\S]*?)```/;
22+
assert.ok(
23+
source.includes('const FENCE = /```codefox-questions'),
24+
'the component no longer declares FENCE — this check is out of date',
25+
);
26+
assert.ok(
27+
source.includes('`q${index}`'),
28+
'the id fallback is gone: two questions can share an answer slot again',
29+
);
30+
31+
/** Mirrors extractQuestions, which is what the component runs. */
32+
function extract(content) {
33+
const match = content?.match(FENCE);
34+
if (!match) return null;
35+
try {
36+
const parsed = JSON.parse(match[1]);
37+
const seen = new Set();
38+
const questions = (
39+
Array.isArray(parsed?.questions)
40+
? parsed.questions.filter(
41+
(q) =>
42+
q &&
43+
typeof q.label === 'string' &&
44+
Array.isArray(q.options) &&
45+
q.options.length > 0,
46+
)
47+
: []
48+
).map((q, index) => {
49+
const id =
50+
typeof q.id === 'string' && q.id && !seen.has(q.id) ? q.id : `q${index}`;
51+
seen.add(id);
52+
return { ...q, id };
53+
});
54+
return questions.length ? questions : null;
55+
} catch {
56+
return null;
57+
}
58+
}
59+
60+
const block = (value) =>
61+
'```codefox-questions\n' + JSON.stringify(value) + '\n```';
62+
63+
const two = (a, b) => ({
64+
questions: [
65+
{ ...a, label: 'first', options: ['A', 'B'] },
66+
{ ...b, label: 'second', options: ['C', 'D'] },
67+
],
68+
});
69+
70+
/** Answering the first question must not answer the second. */
71+
function isolated(shape, name) {
72+
const questions = extract(block(shape));
73+
assert.ok(questions, `${name}: no questions parsed`);
74+
const ids = questions.map((q) => q.id);
75+
assert.equal(new Set(ids).size, ids.length, `${name}: ids collide (${ids})`);
76+
const choices = { [questions[0].id]: ['A'] };
77+
for (const q of questions.slice(1)) {
78+
assert.ok(!choices[q.id], `${name}: answering one answered another`);
79+
}
80+
}
81+
82+
isolated(two({}, {}), 'no ids at all');
83+
isolated(two({ id: 'x' }, { id: 'x' }), 'duplicate ids');
84+
isolated(two({ id: '' }, { id: '' }), 'empty-string ids');
85+
isolated(two({ id: 1 }, { id: 2 }), 'numeric ids');
86+
isolated(two({ id: 'style' }, { id: 'tone' }), 'proper ids');
87+
88+
// Ids the model did supply are kept — they are what the answer text is
89+
// composed from.
90+
assert.deepEqual(
91+
extract(block(two({ id: 'style' }, { id: 'tone' }))).map((q) => q.id),
92+
['style', 'tone'],
93+
'good ids were replaced',
94+
);
95+
96+
// Shapes that must not render as a card at all.
97+
assert.equal(extract('no fence here'), null, 'matched without a fence');
98+
assert.equal(
99+
extract('```codefox-questions\n{ not json\n```'),
100+
null,
101+
'malformed JSON produced a card',
102+
);
103+
assert.equal(
104+
extract(block({ questions: [{ label: 'no options', options: [] }] })),
105+
null,
106+
'a question with no options produced a card',
107+
);
108+
// Half-streamed: the closing fence has not arrived yet.
109+
assert.equal(
110+
extract('```codefox-questions\n{"questions":[]'),
111+
null,
112+
'an unclosed block produced a card',
113+
);
114+
115+
console.log('ok — question card parser survives 9 model-written shapes');

0 commit comments

Comments
 (0)