A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
Questions (#216): check answers against the questions, lists for multi-select
Claude771ea3b318cb
diff --git a/windows/hook/src/main.rs b/windows/hook/src/main.rsindex a4eb4565..4cd62465 100644--- a/windows/hook/src/main.rs+++ b/windows/hook/src/main.rs@@ -82,10 +82,11 @@ fn decision_json(decision: &str, question: Option<&serde_json::Value>) -> Optionif decision.trim_start().starts_with('{') {let reply = serde_json::from_str::<serde_json::Value>(decision).ok()?;let answers = reply.get("answers")?.as_object()?;- if answers.is_empty() || !answers.values().all(|v| v.is_string()) {+ let question = question?;+ if !answers_fit(question, answers) {return None;}- let mut input = question?.as_object()?.clone();+ let mut input = question.as_object()?.clone();input.insert("answers".into(), serde_json::Value::Object(answers.clone()));return Some(serde_json::json!({@@ -109,6 +110,38 @@ fn decision_json(decision: &str, question: Option<&serde_json::Value>) -> Option))}+/// True when `answers` answers exactly the questions Claude Code asked: one entry+/// per question, keyed by its text; a single-select answer is one of its option+/// labels (a string), a multi-select answer a non-empty list of distinct labels+/// (Claude Code 2.1.136+ takes the list, as the Mac sends it). Same rule as+/// `QuestionPayload.accepts` on macOS.+fn answers_fit(question: &serde_json::Value, answers: &serde_json::Map<String, serde_json::Value>) -> bool {+ let Some(items) = question.get("questions").and_then(|q| q.as_array()) else { return false };+ if items.is_empty() || items.len() != answers.len() {+ return false;+ }+ items.iter().all(|item| {+ let Some(text) = item.get("question").and_then(|q| q.as_str()) else { return false };+ let labels: Vec<&str> = item+ .get("options")+ .and_then(|o| o.as_array())+ .map(|opts| opts.iter().filter_map(|o| o.get("label").and_then(|l| l.as_str())).collect())+ .unwrap_or_default();+ let multi = item.get("multiSelect").and_then(|m| m.as_bool()).unwrap_or(false);+ match answers.get(text) {+ Some(serde_json::Value::String(pick)) if !multi => labels.contains(&pick.as_str()),+ Some(serde_json::Value::Array(picks)) if multi => {+ let picks: Vec<&str> = picks.iter().filter_map(|p| p.as_str()).collect();+ let mut seen = picks.clone();+ seen.sort_unstable();+ seen.dedup();+ !picks.is_empty() && seen.len() == picks.len() && picks.iter().all(|p| labels.contains(p))+ }+ _ => false,+ }+ })+}+/// Reads stdin and returns the payload to forward, the event name, and — for an/// AskUserQuestion permission request — the question as Claude Code sent it.fn read_event() -> Option<(String, String, Option<serde_json::Value>)> {@@ -298,6 +331,27 @@ mod tests {assert!(decision_json(r#"{"command":"rm -rf /"}"#, Some(&question)).is_none());}+ #[test]+ fn answers_must_match_the_questions_asked() {+ let q = serde_json::json!({ "questions": [+ { "question": "Which one?", "options": [{ "label": "A" }, { "label": "B" }] },+ { "question": "Extras?", "multiSelect": true,+ "options": [{ "label": "Tests" }, { "label": "Docs" }, { "label": "Lint" }] }+ ]});+ let ok = |a: &str| decision_json(a, Some(&q)).is_some();+ assert!(ok(r#"{"answers":{"Which one?":"A","Extras?":["Tests","Docs"]}}"#));+ // Not one of the labels, or an answer to a question nobody asked.+ assert!(!ok(r#"{"answers":{"Which one?":"C","Extras?":["Tests"]}}"#));+ assert!(!ok(r#"{"answers":{"Which one?":"A","Extras?":["Tests"],"Other?":"x"}}"#));+ // A question left out.+ assert!(!ok(r#"{"answers":{"Which one?":"A"}}"#));+ // Shapes: single-select is one string, multi-select a non-empty list.+ assert!(!ok(r#"{"answers":{"Which one?":["A"],"Extras?":["Tests"]}}"#));+ assert!(!ok(r#"{"answers":{"Which one?":"A","Extras?":"Tests"}}"#));+ assert!(!ok(r#"{"answers":{"Which one?":"A","Extras?":[]}}"#));+ assert!(!ok(r#"{"answers":{"Which one?":"A","Extras?":["Tests","Tests"]}}"#));+ }+#[test]fn long_strings_are_cut_on_a_char_boundary() {let mut v = serde_json::json!({ "tool_input": { "content": "é".repeat(4000) } });diff --git a/windows/src-tauri/src/lib.rs b/windows/src-tauri/src/lib.rsindex e341ebbd..264e4c3a 100644--- a/windows/src-tauri/src/lib.rs+++ b/windows/src-tauri/src/lib.rs@@ -232,7 +232,7 @@ fn approval_decision(app: AppHandle, request_id: String, decision: String) {fn approval_answer(app: AppHandle,request_id: String,- answers: std::collections::HashMap<String, String>,+ answers: std::collections::HashMap<String, serde_json::Value>,) {pipe::answer_question(&app, &request_id, &answers);}diff --git a/windows/src-tauri/src/pipe.rs b/windows/src-tauri/src/pipe.rsindex 8e0bbcad..8d3566b3 100644--- a/windows/src-tauri/src/pipe.rs+++ b/windows/src-tauri/src/pipe.rs@@ -306,7 +306,7 @@ pub fn answer(app: &AppHandle, request_id: &str, decision: &str) {/// Called when an option is picked for a question Claude Code asked. `answers`/// maps each question's text to the chosen label, which is the shape/// AskUserQuestion takes them in.-pub fn answer_question(app: &AppHandle, request_id: &str, answers: &HashMap<String, String>) {+pub fn answer_question(app: &AppHandle, request_id: &str, answers: &HashMap<String, serde_json::Value>) {log::line(format!("decision id={request_id} answered a question"));// One line: the relay reads up to the first newline.let line = json!({ "answers": answers }).to_string();diff --git a/windows/src/core/bridge.ts b/windows/src/core/bridge.tsindex 64573a21..c6121c15 100644--- a/windows/src/core/bridge.ts+++ b/windows/src/core/bridge.ts@@ -81,7 +81,7 @@ export const Bridge = {/** "The card is up" — until this lands the relay only waits a moment. */approvalAck: (requestId: string) => call<void>("approval_ack", { requestId }),/** Answers a question Claude Code asked: question text → chosen label. */- approvalAnswer: (requestId: string, answers: Record<string, string>) =>+ approvalAnswer: (requestId: string, answers: Record<string, string | string[]>) =>call<void>("approval_answer", { requestId, answers }),/** "Nobody can act on this" — Claude Code asks in the terminal right away. */diff --git a/windows/src/island/hooks.ts b/windows/src/island/hooks.tsindex 27e310cd..ff03a72f 100644--- a/windows/src/island/hooks.ts+++ b/windows/src/island/hooks.ts@@ -13,6 +13,20 @@ const CLAUDE_ID = "integration_claude";/** Clears the approval card if no decision was made before the hook gave up. */let pendingTimeout: number | null = null;+/** Takes the approval or question card down and gives the island back. */+function dropPendingCard(island: Island): void {+ if (!State.pendingApproval) return;+ State.pendingApproval = null;+ State.isPinned = false;+ island.dropPin();+ State.updateTask(CLAUDE_ID, "working");+ State.setPillBadge(CLAUDE_ID, null);+ if (State.view === "approval" || State.view === "question") {+ island.setView(State.defaultView());+ }+ State.notify();+}+/** The return to idle that Stop arms, per pill, so the next turn can cancel it. */const stopTimers = new Map<string, number>();@@ -255,6 +269,16 @@ function handleHook(island: Island, payload: HookPayload) {case "PostToolUse":supersedeStop();+ // The question was answered in the terminal: the card would be lying.+ if (+ payload.tool_name === "AskUserQuestion" &&+ State.pendingApproval?.questions &&+ State.pendingApproval.sessionId === (payload.session_id ?? "")+ ) {+ if (pendingTimeout != null) window.clearTimeout(pendingTimeout);+ pendingTimeout = null;+ dropPendingCard(island);+ }State.updateTask(agentId, "working");break;@@ -380,16 +404,7 @@ function handleHook(island: Island, payload: HookPayload) {// taken over and the card would be lying.pendingTimeout = window.setTimeout(() => {pendingTimeout = null;- if (!State.pendingApproval) return;- State.pendingApproval = null;- State.isPinned = false;- island.dropPin();- State.updateTask(CLAUDE_ID, "working");- State.setPillBadge(CLAUDE_ID, null);- if (State.view === "approval" || State.view === "question") {- island.setView(State.defaultView());- }- State.notify();+ dropPendingCard(island);}, 110_000);break;}diff --git a/windows/src/views/views.ts b/windows/src/views/views.tsindex f4a990b4..e433651d 100644--- a/windows/src/views/views.ts+++ b/windows/src/views/views.ts@@ -24,7 +24,7 @@ export interface ViewActions {openUrl(url: string): void;decide(d: "allow" | "deny"): void;/** Answers the question Claude Code asked: question text → chosen label. */- answer(answers: Record<string, string>): void;+ answer(answers: Record<string, string | string[]>): void;/** Hands the pending request back to the terminal. */answerInTerminal(): void;toggleSound(): void;@@ -354,13 +354,13 @@ function buildQuestion(actions: ViewActions): ViewHost {// far, and what is ticked in a pick-several question.let requestId = "";let index = 0;- let answers: Record<string, string> = {};+ let answers: Record<string, string | string[]> = {};let picked = new Set<string>();// The buttons are only rebuilt when what they show changes: rebuilding them// between a mouse-down and a mouse-up would swallow the click.let rowKey = "";- const next = (question: string, answer: string, total: number) => {+ const next = (question: string, answer: string | string[], total: number) => {answers[question] = answer;picked = new Set();index += 1;@@ -418,7 +418,7 @@ function buildQuestion(actions: ViewActions): ViewHost {}if (q.multiSelect) {const done = btn("Done", "primary", () => {- if (picked.size > 0) next(q.question, [...picked].join(", "), questions.length);+ if (picked.size > 0) next(q.question, [...picked], questions.length);});if (picked.size === 0) done.classList.add("off");row.append(done);