fix(question-log): parse native AskUserQuestion answers — every native answer logged as __unknown__

Current Claude Code returns AskUserQuestion results as an OBJECT map keyed
by question text ({answers: {question: label}}); the hook only handled the
legacy array shapes, so 86% of live records carried user_choice __unknown__
— and the bin then scored every one as followed_recommendation false,
silently poisoning plan-tune metrics. Adds the object-map extraction (exact
+ whitespace-normalized + single-question pairing, multiSelect joins,
annotations as free_text), strips the (Recommended) suffix from BOTH sides
of the comparison, skips the computation entirely on extraction failure,
and logs unrecognized shapes to hook-errors.log instead of embedding them
in the record.

Fixes #2336, #2206.

Based on the working patch in #2336 by @yijisoo; suffix comparison fix
contributed by @chuchu2781 (PR #2400).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-08-14 20:20:52 -07:00
co-authored by Claude Fable 5
parent 540847d038
commit 54612cb9e3
3 changed files with 151 additions and 11 deletions
+55 -8
View File
@@ -156,21 +156,63 @@ function extractRecommended(questionText: string, opts: string[]): string | unde
* AUQ tool_response shape varies by Claude Code variant (native vs MCP),
* and the hook stdin docs don't pin a single canonical shape. We handle
* the common cases gracefully.
*
* Shape D is the current native AskUserQuestion result:
* { answers: { "<question text>": "<answer>" },
* annotations?: { "<question text>": { notes?, preview? } } }
* The map is keyed by the question text exactly as passed in tool_input,
* so extraction needs the questions themselves, not just a count.
*/
function extractUserChoices(
response: unknown,
questionCount: number,
questions: Array<{ question?: string; options?: Array<string | { label?: string; description?: string }> }>,
diag?: (msg: string) => void,
): Array<{ choice: string; free_text?: string }> {
const questionCount = questions.length;
const out: Array<{ choice: string; free_text?: string }> = [];
if (!response) {
diag?.(`answer-extract: empty tool_response (typeof=${typeof response})`);
for (let i = 0; i < questionCount; i++) out.push({ choice: '__unknown__' });
return out;
}
// Shape A: { answers: [{option_label, free_text?}] }
// Shape B: { questions: [{user_answer}] }
// Shape C: { content: [...] } or array.
// We probe lazily.
const rec = response as Record<string, unknown>;
// Shape D: { answers: {questionText: answer}, annotations?: {questionText: {notes}} }
if (rec.answers && typeof rec.answers === 'object' && !Array.isArray(rec.answers)) {
const answers = rec.answers as Record<string, unknown>;
const annotations =
rec.annotations && typeof rec.annotations === 'object' && !Array.isArray(rec.annotations)
? (rec.annotations as Record<string, Record<string, unknown>>)
: {};
const keys = Object.keys(answers);
const norm = (s: string) => s.replace(/\s+/g, ' ').trim().toLowerCase();
for (const q of questions) {
const qText = q.question || '';
let key: string | undefined = Object.prototype.hasOwnProperty.call(answers, qText)
? qText
: keys.find((k) => norm(k) === norm(qText));
// Single question, single answer: pair them even if the key drifted.
if (key === undefined && keys.length === 1 && questionCount === 1) key = keys[0];
if (key === undefined) {
diag?.(`answer-extract: no answers key matched question "${qText.slice(0, 60)}"`);
out.push({ choice: '__unknown__' });
continue;
}
const v = answers[key];
const rawChoice = Array.isArray(v) ? v.map(String).join(', ') : String(v ?? '__unknown__');
// The bin compares user_choice === recommended, and recommended is
// stored with the "(recommended)" suffix stripped — strip it here too.
const choice = rawChoice.replace(RECOMMENDED_LABEL_RE, '').trim() || '__unknown__';
const labels = optionLabels(q.options || []).map((l) =>
l.replace(RECOMMENDED_LABEL_RE, '').trim().toLowerCase(),
);
const notes = annotations[key]?.notes;
const isFreeText = !Array.isArray(v) && labels.length > 0 && !labels.includes(choice.toLowerCase());
const freeText = notes !== undefined ? String(notes) : isFreeText ? rawChoice : undefined;
out.push(freeText !== undefined ? { choice, free_text: freeText } : { choice });
}
return out;
}
// Shape A: { answers: [{option_label, free_text?}] }
if (Array.isArray(rec.answers)) {
for (const a of rec.answers as Array<Record<string, unknown>>) {
const choice = (a.option_label || a.label || a.choice || a.answer || '__unknown__') as string;
@@ -180,6 +222,7 @@ function extractUserChoices(
while (out.length < questionCount) out.push({ choice: '__unknown__' });
return out;
}
// Shape B: { questions: [{user_answer}] }
if (Array.isArray(rec.questions)) {
for (const q of rec.questions as Array<Record<string, unknown>>) {
const choice = (q.user_answer || q.answer || q.choice || '__unknown__') as string;
@@ -188,9 +231,11 @@ function extractUserChoices(
while (out.length < questionCount) out.push({ choice: '__unknown__' });
return out;
}
// Fall back: stringify and log first 100 chars to help future debugging.
// Unrecognized shape: log it for postmortem (never embed it in the record —
// that poisons user_choice for every downstream metric).
diag?.(`answer-extract: unrecognized tool_response shape: ${JSON.stringify(response).slice(0, 300)}`);
for (let i = 0; i < questionCount; i++) {
out.push({ choice: `__response-shape-unknown:${JSON.stringify(response).slice(0, 80)}__` });
out.push({ choice: '__unknown__' });
}
return out;
}
@@ -251,7 +296,9 @@ async function main(): Promise<void> {
}
const skill = detectSkill(stdin.cwd);
const choices = extractUserChoices(stdin.tool_response, questions.length);
const choices = extractUserChoices(stdin.tool_response, questions, (msg) =>
logHookError(`${msg} (tool_use_id=${stdin.tool_use_id || 'n/a'})`),
);
for (let i = 0; i < questions.length; i++) {
const q = questions[i];