pi_agent_rust 0.3.0

Native AI coding agent CLI - Rust port of Pi Agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
//! The `ask` tool: structured mid-turn option picker (bd-cv653.3.8).
//!
//! Ambiguity routes through `ask` instead of prose: the model presents 2-5
//! options per question (optionally multi-select, optionally with a
//! recommended choice) and the host surfaces a picker — the TUI as an
//! option card, RPC/ACP as request frames — then the selection returns as
//! the tool result.
//!
//! Layering (deliberate, per the chrome-epic stack-layering rule): this
//! module owns the QUESTION MODEL, validation, policy resolution, and
//! answer formatting only. Rendering lives entirely behind [`AskHandler`],
//! which hosts install; a session with no handler (print/JSON mode, SDK
//! embedders without UI) resolves through [`AskPolicy`] instead of hanging.

use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Schema identifier for the tool-result details payload.
pub const ASK_RESPONSE_SCHEMA: &str = "ask_response.v1";

/// Bounds on the option list, per the omp card contract.
pub const MIN_OPTIONS: usize = 2;
pub const MAX_OPTIONS: usize = 5;
/// Bound on questions per call (one card each).
pub const MAX_QUESTIONS: usize = 4;

/// One selectable option.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AskOption {
    pub label: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// One question card.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AskQuestion {
    /// Stable identifier for pairing answers; defaults to the question index.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub question: String,
    /// Short chip/tag label for compact card headers.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub header: Option<String>,
    pub options: Vec<AskOption>,
    /// Index into `options` of the recommended choice.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub recommended: Option<usize>,
    /// Allow selecting multiple options.
    #[serde(default)]
    pub multi: bool,
}

/// A validated ask request (invariants enforced by [`validate_request`]).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AskRequest {
    pub questions: Vec<AskQuestion>,
}

/// Answer to one question.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AskAnswer {
    /// The question's `id` (or its index rendered as a string).
    pub question_id: String,
    /// Selected option labels (one unless the question was `multi`).
    pub selected: Vec<String>,
    /// Free-text answer when the user chose "Other".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub other: Option<String>,
}

/// The host's response to an ask request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AskResponse {
    pub answers: Vec<AskAnswer>,
    /// The user dismissed/cancelled the card instead of answering.
    #[serde(default)]
    pub dismissed: bool,
}

/// Host-installed picker surface. Implementations own their timeout; a
/// dismissal is expressed via [`AskResponse::dismissed`], errors via `Err`.
pub type AskHandler = Arc<
    dyn Fn(AskRequest) -> futures::future::BoxFuture<'static, Result<AskResponse>> + Send + Sync,
>;

/// Non-interactive resolution policy when no picker surface exists.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AskPolicy {
    /// Auto-answer with the recommended option (or the first option when no
    /// recommendation is given), loudly annotating the result.
    #[default]
    Recommended,
    /// Fail the tool call: this session cannot answer questions.
    Error,
}

impl AskPolicy {
    /// Parse a settings string; unknown values fall back to the default with
    /// a warning.
    #[must_use]
    pub fn from_config(value: Option<&str>) -> Self {
        match value.map(str::trim) {
            Some("error") => Self::Error,
            Some("recommended" | "") | None => Self::Recommended,
            Some(other) => {
                tracing::warn!(
                    "unknown ask_policy setting '{other}' (expected 'recommended' or 'error'); using recommended"
                );
                Self::Recommended
            }
        }
    }
}

/// Validate the raw tool input into an [`AskRequest`].
pub fn validate_request(request: AskRequest) -> Result<AskRequest> {
    if request.questions.is_empty() {
        return Err(Error::validation("ask requires at least one question"));
    }
    if request.questions.len() > MAX_QUESTIONS {
        return Err(Error::validation(format!(
            "ask accepts at most {MAX_QUESTIONS} questions per call"
        )));
    }
    let mut seen_ids = std::collections::HashSet::new();
    // ubs:ignore-block validation loop: every format! below is a cold
    // return-error exit taken at most once, not a hot-loop allocation.
    for (index, question) in request.questions.iter().enumerate() {
        if question.question.trim().is_empty() {
            return Err(Error::validation(format!(
                "question {index} must not be blank"
            )));
        }
        if question.options.len() < MIN_OPTIONS || question.options.len() > MAX_OPTIONS {
            return Err(Error::validation(format!(
                "question {index} needs {MIN_OPTIONS}-{MAX_OPTIONS} options (got {})",
                question.options.len()
            )));
        }
        if let Some(option) = question
            .options
            .iter()
            .find(|option| option.label.trim().is_empty())
        {
            let _ = option;
            return Err(Error::validation(format!(
                "question {index} has an option with a blank label"
            )));
        }
        if let Some(recommended) = question.recommended
            && recommended >= question.options.len()
        {
            return Err(Error::validation(format!(
                "question {index}: recommended index {recommended} is out of bounds for {} options",
                question.options.len()
            )));
        }
        if !seen_ids.insert(effective_question_id(question, index)) {
            return Err(Error::validation(format!(
                "question {index} reuses an id; ids must be unique"
            )));
        }
    }
    Ok(request)
}

/// A question's answer-pairing id: explicit `id`, else its index.
#[must_use]
pub fn effective_question_id(question: &AskQuestion, index: usize) -> String {
    question.id.clone().unwrap_or_else(|| index.to_string())
}

/// Resolve a validated request under a non-interactive policy.
pub fn resolve_by_policy(request: &AskRequest, policy: AskPolicy) -> Result<AskResponse> {
    match policy {
        AskPolicy::Error => Err(Error::tool(
            "ask",
            "this session has no interactive picker and ask_policy is 'error'",
        )),
        AskPolicy::Recommended => Ok(AskResponse {
            answers: request
                .questions
                .iter()
                .enumerate()
                .map(|(index, question)| {
                    let choice = question.recommended.unwrap_or(0);
                    AskAnswer {
                        question_id: effective_question_id(question, index),
                        selected: question
                            .options
                            .get(choice)
                            .map(|option| vec![option.label.clone()])
                            .unwrap_or_default(),
                        other: None,
                    }
                })
                .collect(),
            dismissed: false,
        }),
    }
}

/// Render the tool-result text for a resolved response.
#[must_use]
pub fn render_answers(request: &AskRequest, response: &AskResponse, auto: bool) -> String {
    use std::fmt::Write as _;

    let mut out = String::new();
    if auto {
        let _ = writeln!(
            out,
            "[non-interactive session: answered with the recommended option(s)]"
        );
    }
    for (index, question) in request.questions.iter().enumerate() {
        let id = effective_question_id(question, index);
        let answer = response
            .answers
            .iter()
            .find(|answer| answer.question_id == id);
        let rendered = answer.map_or_else(
            || "(unanswered)".to_string(),
            |answer| {
                answer.other.as_deref().map_or_else(
                    || {
                        if answer.selected.is_empty() {
                            "(unanswered)".to_string()
                        } else {
                            answer.selected.join(", ")
                        }
                    },
                    |other| format!("Other: {other}"),
                )
            },
        );
        let _ = writeln!(out, "Q: {}\nA: {rendered}", question.question.trim());
    }
    out.trim_end().to_string()
}

/// Picker card wait budget for interactive surfaces.
pub const ASK_UI_TIMEOUT_MS: u64 = 300_000;

/// A picker request in flight to an interactive surface.
#[derive(Debug, Clone)]
pub struct AskUiRequest {
    pub id: String,
    pub request: AskRequest,
}

/// One question's parsed reply from raw picker input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuestionReply {
    /// The user cancelled the card.
    Cancel,
    /// Selected option labels (possibly several for `multi`).
    Selected(Vec<String>),
    /// Free-text "Other" answer.
    Other(String),
}

/// Parse raw picker input for one question.
///
/// `cancel` dismisses; numbers (1-based) or exact labels select —
/// comma-separated when `multi`; anything else is a free-text "Other"
/// answer. Returns `Err(message)` for selections that reference unknown
/// options mixed with known ones (likely typos).
pub fn parse_question_reply(
    question: &AskQuestion,
    input: &str,
) -> std::result::Result<QuestionReply, String> {
    let trimmed = input.trim();
    if trimmed.eq_ignore_ascii_case("cancel") {
        return Ok(QuestionReply::Cancel);
    }
    let resolve = |token: &str| -> Option<String> {
        let token = token.trim();
        if let Ok(number) = token.parse::<usize>()
            && (1..=question.options.len()).contains(&number)
        {
            return question
                .options
                .get(number - 1)
                .map(|option| option.label.clone());
        }
        question
            .options
            .iter()
            .find(|option| option.label.eq_ignore_ascii_case(token))
            .map(|option| option.label.clone())
    };

    if question.multi && trimmed.contains(',') {
        let tokens: Vec<&str> = trimmed.split(',').map(str::trim).collect();
        let resolved: Vec<Option<String>> = tokens.iter().map(|token| resolve(token)).collect();
        if resolved.iter().all(Option::is_some) {
            let mut selected: Vec<String> = resolved.into_iter().flatten().collect();
            selected.dedup();
            return Ok(QuestionReply::Selected(selected));
        }
        if resolved.iter().any(Option::is_some) {
            return Err("mixed known and unknown selections; use option numbers, exact labels, or free text".to_string());
        }
        return Ok(QuestionReply::Other(trimmed.to_string()));
    }

    resolve(trimmed).map_or_else(
        || Ok(QuestionReply::Other(trimmed.to_string())),
        |label| Ok(QuestionReply::Selected(vec![label])),
    )
}

/// Plain-text card rendering for one question (host styles it).
#[must_use]
pub fn format_question_card(question: &AskQuestion, index: usize, total: usize) -> String {
    use std::fmt::Write as _;

    let mut out = String::new();
    let header = question.header.as_deref().unwrap_or("ask");
    let _ = writeln!(out, "[{header}] question {} of {total}", index + 1);
    let _ = writeln!(out, "{}", question.question.trim());
    for (option_index, option) in question.options.iter().enumerate() {
        let badge = if question.recommended == Some(option_index) {
            " (recommended)"
        } else {
            ""
        };
        match &option.description {
            Some(description) => {
                let _ = writeln!(
                    out,
                    "  {}) {}{badge} — {description}",
                    option_index + 1,
                    option.label
                );
            }
            None => {
                let _ = writeln!(out, "  {}) {}{badge}", option_index + 1, option.label);
            }
        }
    }
    let hint = if question.multi {
        "Enter numbers/labels (comma-separated for several), free text for Other, or 'cancel'."
    } else {
        "Enter a number, label, free text for Other, or 'cancel'."
    };
    out.push_str(hint);
    out
}

type PendingAskReplies = Arc<
    std::sync::Mutex<
        std::collections::HashMap<String, asupersync::channel::oneshot::Sender<AskResponse>>,
    >,
>;

/// The `ask` tool. Logic-only: rendering is the installed handler's job.
///
/// Cloning shares the handler slot, so the host can keep one clone to
/// install its picker surface after the registry boxed another.
#[derive(Clone)]
pub struct AskTool {
    handler: Arc<std::sync::RwLock<Option<AskHandler>>>,
    pending_ui: PendingAskReplies,
    policy: AskPolicy,
}

impl AskTool {
    #[must_use]
    pub fn new(policy: AskPolicy) -> Self {
        Self {
            handler: Arc::new(std::sync::RwLock::new(None)),
            pending_ui: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
            policy,
        }
    }

    /// Install the host's picker surface (shared across clones).
    pub fn set_handler(&self, handler: AskHandler) {
        *self
            .handler
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(handler);
    }

    /// Install a channel-based picker surface (the TUI pattern): requests
    /// flow out through `sender`, and the host answers via
    /// [`Self::respond_ui`]. Mirrors the extension-UI request/response
    /// bridge, including the wait budget.
    pub fn install_channel_ui(&self, sender: asupersync::channel::mpsc::Sender<AskUiRequest>) {
        let pending = Arc::clone(&self.pending_ui);
        self.set_handler(Arc::new(move |request: AskRequest| {
            let sender = sender.clone();
            let pending = Arc::clone(&pending);
            Box::pin(async move {
                let cx = crate::agent_cx::AgentCx::for_current_or_request();
                let id = uuid::Uuid::new_v4().to_string();
                let (reply_tx, mut reply_rx) = asupersync::channel::oneshot::channel();
                pending
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner)
                    .insert(id.clone(), reply_tx);
                let ui_request = AskUiRequest {
                    id: id.clone(),
                    request,
                };
                if sender.send(cx.cx(), ui_request).await.is_err() {
                    pending
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .remove(&id);
                    return Err(Error::tool("ask", "picker surface unavailable"));
                }
                let waited = asupersync::time::timeout(
                    asupersync::time::wall_now(),
                    std::time::Duration::from_millis(ASK_UI_TIMEOUT_MS),
                    reply_rx.recv(cx.cx()),
                )
                .await;
                match waited {
                    Ok(Ok(response)) => Ok(response),
                    Ok(Err(_)) => {
                        pending
                            .lock()
                            .unwrap_or_else(std::sync::PoisonError::into_inner)
                            .remove(&id);
                        Err(Error::tool("ask", "picker surface closed"))
                    }
                    Err(_) => {
                        pending
                            .lock()
                            .unwrap_or_else(std::sync::PoisonError::into_inner)
                            .remove(&id);
                        Err(Error::tool(
                            "ask",
                            "question timed out waiting for the user",
                        ))
                    }
                }
            })
        }));
    }

    /// Deliver the host's answer for a pending picker request.
    pub fn respond_ui(&self, id: &str, response: AskResponse) -> bool {
        let cx = crate::agent_cx::AgentCx::for_current_or_request();
        let sender = self
            .pending_ui
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .remove(id);
        sender.is_some_and(|sender| sender.send(cx.cx(), response).is_ok())
    }

    fn handler(&self) -> Option<AskHandler> {
        self.handler
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone()
    }
}

#[async_trait::async_trait]
#[allow(clippy::unnecessary_literal_bound)]
impl crate::tools::Tool for AskTool {
    fn name(&self) -> &str {
        "ask"
    }

    fn label(&self) -> &str {
        "Ask"
    }

    fn description(&self) -> &str {
        "Ask the user structured questions mid-turn instead of guessing. Each question card offers 2-5 mutually exclusive options (set multi=true to allow several); mark the option you would pick with `recommended` (its index) and put it first. Use ONLY when different answers lead to materially different work — never for choices with an obvious default. The user can always answer with free text ('Other'). In non-interactive sessions the recommended option is auto-selected (or the call errors, per ask_policy)."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "questions": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": MAX_QUESTIONS,
                    "items": {
                        "type": "object",
                        "required": ["question", "options"],
                        "properties": {
                            "id": {"type": "string", "description": "Stable id for pairing the answer (defaults to the question index)."},
                            "question": {"type": "string", "description": "The complete question, ending with a question mark."},
                            "header": {"type": "string", "description": "Very short chip label (max ~12 chars)."},
                            "options": {
                                "type": "array",
                                "minItems": MIN_OPTIONS,
                                "maxItems": MAX_OPTIONS,
                                "items": {
                                    "type": "object",
                                    "required": ["label"],
                                    "properties": {
                                        "label": {"type": "string", "description": "Concise display text (1-5 words)."},
                                        "description": {"type": "string", "description": "What choosing this means."}
                                    }
                                }
                            },
                            "recommended": {"type": "integer", "description": "Index of the recommended option."},
                            "multi": {"type": "boolean", "default": false}
                        }
                    }
                }
            },
            "required": ["questions"],
            "additionalProperties": false
        })
    }

    fn effects(&self) -> crate::tools::ToolEffects {
        crate::tools::ToolEffects::read()
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        input: serde_json::Value,
        _on_update: Option<Box<dyn Fn(crate::tools::ToolUpdate) + Send + Sync>>,
    ) -> Result<crate::tools::ToolOutput> {
        let request: AskRequest = serde_json::from_value(input)
            .map_err(|error| Error::validation(format!("Invalid ask input: {error}")))?; // ubs:ignore false positive: cold error path
        let request = validate_request(request)?;

        let (response, auto) = if let Some(handler) = self.handler() {
            let response = handler(request.clone()).await?;
            if response.dismissed {
                return Err(Error::tool("ask", "user dismissed the question"));
            }
            (response, false)
        } else {
            (resolve_by_policy(&request, self.policy)?, true)
        };

        if auto {
            tracing::info!(
                event = "pi.ask.auto_answered",
                questions = request.questions.len(),
                "ask auto-answered with recommended options (non-interactive session)"
            );
        }

        let details = serde_json::json!({
            "schema": ASK_RESPONSE_SCHEMA,
            "answers": response.answers,
            "autoAnswered": auto,
        });
        Ok(crate::tools::ToolOutput {
            content: vec![crate::model::ContentBlock::Text(
                crate::model::TextContent::new(render_answers(&request, &response, auto)),
            )],
            details: Some(details),
            is_error: false,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::Tool as _;

    fn question(json: serde_json::Value) -> AskRequest {
        serde_json::from_value(json).expect("parse ask request")
    }

    /// Validation matrix: option-count bounds, recommended bounds, blank
    /// content, duplicate ids, question-count bound.
    #[test]
    fn validation_matrix() {
        let valid = question(serde_json::json!({
            "questions": [{
                "question": "Which path?",
                "options": [{"label": "A"}, {"label": "B"}],
                "recommended": 1
            }]
        }));
        assert!(validate_request(valid).is_ok());

        for (label, bad) in [
            (
                "one option",
                serde_json::json!({"questions": [{"question": "Q?", "options": [{"label": "A"}]}]}),
            ),
            (
                "six options",
                serde_json::json!({"questions": [{"question": "Q?", "options": [
                    {"label": "1"}, {"label": "2"}, {"label": "3"},
                    {"label": "4"}, {"label": "5"}, {"label": "6"}
                ]}]}),
            ),
            (
                "recommended out of bounds",
                serde_json::json!({"questions": [{"question": "Q?", "recommended": 2,
                    "options": [{"label": "A"}, {"label": "B"}]}]}),
            ),
            (
                "blank question",
                serde_json::json!({"questions": [{"question": "  ",
                    "options": [{"label": "A"}, {"label": "B"}]}]}),
            ),
            (
                "blank option label",
                serde_json::json!({"questions": [{"question": "Q?",
                    "options": [{"label": "A"}, {"label": " "}]}]}),
            ),
            (
                "duplicate ids",
                serde_json::json!({"questions": [
                    {"id": "x", "question": "Q1?", "options": [{"label": "A"}, {"label": "B"}]},
                    {"id": "x", "question": "Q2?", "options": [{"label": "A"}, {"label": "B"}]}
                ]}),
            ),
            ("no questions", serde_json::json!({"questions": []})),
        ] {
            assert!(
                validate_request(question(bad)).is_err(),
                "must reject: {label}"
            );
        }
    }

    /// Policy resolution: recommended picks the marked option (or the first
    /// without a mark); error policy fails.
    #[test]
    fn policy_resolution() {
        let request = validate_request(question(serde_json::json!({
            "questions": [
                {"id": "q1", "question": "Pick?", "recommended": 1,
                 "options": [{"label": "A"}, {"label": "B"}]},
                {"question": "Fallback?", "options": [{"label": "X"}, {"label": "Y"}]}
            ]
        })))
        .expect("valid");

        let resolved = resolve_by_policy(&request, AskPolicy::Recommended).expect("resolve");
        assert_eq!(resolved.answers.len(), 2);
        assert_eq!(resolved.answers[0].question_id, "q1");
        assert_eq!(resolved.answers[0].selected, vec!["B"]);
        assert_eq!(resolved.answers[1].question_id, "1");
        assert_eq!(resolved.answers[1].selected, vec!["X"]);

        assert!(resolve_by_policy(&request, AskPolicy::Error).is_err());
        assert_eq!(AskPolicy::from_config(Some("error")), AskPolicy::Error);
        assert_eq!(AskPolicy::from_config(None), AskPolicy::Recommended);
        assert_eq!(
            AskPolicy::from_config(Some("bogus")),
            AskPolicy::Recommended
        );
    }

    /// Acceptance 3: no handler + recommended policy auto-answers with a
    /// loud notice and autoAnswered details flag.
    #[test]
    fn auto_answer_path_via_tool() {
        asupersync::test_utils::run_test(|| async {
            let tool = AskTool::new(AskPolicy::Recommended);
            let output = tool
                .execute(
                    "ask-1",
                    serde_json::json!({
                        "questions": [{
                            "question": "Deploy now?",
                            "recommended": 0,
                            "options": [
                                {"label": "Deploy", "description": "ship it"},
                                {"label": "Wait"}
                            ]
                        }]
                    }),
                    None,
                )
                .await
                .expect("auto-answer");
            let crate::model::ContentBlock::Text(text) = &output.content[0] else {
                unreachable!("ask renders text");
            };
            assert!(text.text.contains("non-interactive session"));
            assert!(text.text.contains("A: Deploy"));
            let details = output.details.expect("details");
            assert_eq!(details["schema"], ASK_RESPONSE_SCHEMA);
            assert_eq!(details["autoAnswered"], true);
        });
    }

    /// Handler path: selections flow through; dismissal becomes the
    /// documented tool error (acceptance 4's logic half).
    #[test]
    fn handler_path_and_dismissal() {
        asupersync::test_utils::run_test(|| async {
            let tool = AskTool::new(AskPolicy::Recommended);
            tool.set_handler(Arc::new(|request: AskRequest| {
                Box::pin(async move {
                    Ok(AskResponse {
                        answers: request
                            .questions
                            .iter()
                            .enumerate()
                            .map(|(index, question)| AskAnswer {
                                question_id: effective_question_id(question, index),
                                selected: vec![question.options[1].label.clone()],
                                other: None,
                            })
                            .collect(),
                        dismissed: false,
                    })
                })
            }));
            let output = tool
                .execute(
                    "ask-2",
                    serde_json::json!({
                        "questions": [{
                            "question": "Pick?",
                            "options": [{"label": "A"}, {"label": "B"}]
                        }]
                    }),
                    None,
                )
                .await
                .expect("handler answer");
            let crate::model::ContentBlock::Text(text) = &output.content[0] else {
                unreachable!("ask renders text");
            };
            assert!(text.text.contains("A: B"));
            assert!(!text.text.contains("non-interactive"));

            tool.set_handler(Arc::new(|_request: AskRequest| {
                Box::pin(async move {
                    Ok(AskResponse {
                        answers: Vec::new(),
                        dismissed: true,
                    })
                })
            }));
            let error = tool
                .execute(
                    "ask-3",
                    serde_json::json!({
                        "questions": [{
                            "question": "Pick?",
                            "options": [{"label": "A"}, {"label": "B"}]
                        }]
                    }),
                    None,
                )
                .await
                .expect_err("dismissal errors");
            assert!(error.to_string().contains("dismissed"), "{error}"); // ubs:ignore test assertion
        });
    }

    /// Channel-UI bridge round trip: the handler queues an AskUiRequest,
    /// respond_ui delivers the answer back into the awaiting execute call.
    #[test]
    fn channel_ui_round_trip() {
        asupersync::test_utils::run_test(|| async {
            let tool = AskTool::new(AskPolicy::Error);
            let (tx, mut rx) = asupersync::channel::mpsc::channel::<AskUiRequest>(4);
            tool.install_channel_ui(tx);

            let responder_tool = tool.clone();
            let cx = crate::agent_cx::AgentCx::for_request();
            let responder = async {
                let request = rx.recv(cx.cx()).await.expect("ui request arrives");
                assert_eq!(request.request.questions.len(), 1);
                let answered = responder_tool.respond_ui(
                    &request.id,
                    AskResponse {
                        answers: vec![AskAnswer {
                            question_id: "q".to_string(),
                            selected: vec!["B".to_string()],
                            other: None,
                        }],
                        dismissed: false,
                    },
                );
                assert!(answered, "pending reply slot must exist");
            };
            let execute = tool.execute(
                "ask-rt",
                serde_json::json!({
                    "questions": [{
                        "id": "q",
                        "question": "Pick?",
                        "options": [{"label": "A"}, {"label": "B"}]
                    }]
                }),
                None,
            );
            let (output, ()) = futures::join!(execute, responder);
            let output = output.expect("round trip");
            let crate::model::ContentBlock::Text(text) = &output.content[0] else {
                unreachable!("ask renders text");
            };
            assert!(text.text.contains("A: B"));

            // Unknown id → no pending slot.
            assert!(!tool.respond_ui(
                "missing",
                AskResponse {
                    answers: Vec::new(),
                    dismissed: true
                }
            ));
        });
    }

    /// Card parsing matrix: numbers, labels, multi comma lists, cancel,
    /// free-text Other, and mixed-typo rejection.
    #[test]
    fn question_reply_parsing_matrix() {
        let single: AskQuestion = serde_json::from_value(serde_json::json!({
            "question": "Pick?",
            "options": [{"label": "Alpha"}, {"label": "Beta"}]
        }))
        .expect("question");
        assert_eq!(
            parse_question_reply(&single, "2"),
            Ok(QuestionReply::Selected(vec!["Beta".to_string()]))
        );
        assert_eq!(
            parse_question_reply(&single, "alpha"),
            Ok(QuestionReply::Selected(vec!["Alpha".to_string()]))
        );
        assert_eq!(
            parse_question_reply(&single, "CANCEL"),
            Ok(QuestionReply::Cancel)
        );
        assert_eq!(
            parse_question_reply(&single, "do something else"),
            Ok(QuestionReply::Other("do something else".to_string()))
        );

        let multi: AskQuestion = serde_json::from_value(serde_json::json!({
            "question": "Pick several?",
            "multi": true,
            "options": [{"label": "Alpha"}, {"label": "Beta"}, {"label": "Gamma"}]
        }))
        .expect("multi question");
        assert_eq!(
            parse_question_reply(&multi, "1, gamma"),
            Ok(QuestionReply::Selected(vec![
                "Alpha".to_string(),
                "Gamma".to_string()
            ]))
        );
        assert!(parse_question_reply(&multi, "1, nonsense").is_err());
    }

    /// Free-text 'Other' answers render distinctly.
    #[test]
    fn other_answers_render() {
        let request = validate_request(question(serde_json::json!({
            "questions": [{"id": "q", "question": "Pick?",
                "options": [{"label": "A"}, {"label": "B"}]}]
        })))
        .expect("valid");
        let response = AskResponse {
            answers: vec![AskAnswer {
                question_id: "q".to_string(),
                selected: Vec::new(),
                other: Some("do it differently".to_string()),
            }],
            dismissed: false,
        };
        let rendered = render_answers(&request, &response, false);
        assert!(rendered.contains("A: Other: do it differently"));
    }
}