a3s 0.9.7

a3s — A3S coding agent CLI; `a3s code` launches the interactive TUI
Documentation
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
//! Shared text-only CLI transport for account-backed providers.
//!
//! Claude Code and WorkBuddy expose the same Anthropic-shaped `stream-json`
//! event protocol. Their own tools are disabled and A3S host tools are bridged
//! through a small text envelope, keeping execution and tool-card rendering in
//! A3S rather than in the external CLI process.

use super::host_tools::{host_tool_instructions, parse_host_tool_calls, HostToolParseResult};
use super::protocol::{
    parse_account_cli_stream_event, AnthropicEventMapper, AnthropicStreamEvent, StreamMeta,
};
use a3s_code_core::llm::{
    ContentBlock, LlmResponse, LlmResponseMeta, Message, StreamEvent, TokenUsage, ToolDefinition,
};
use anyhow::{Context, Result};
use serde_json::json;
use std::ffi::OsString;
use std::fmt::Write as _;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::Instant;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

#[derive(Clone, Debug)]
pub(crate) struct CliInvocation {
    program: PathBuf,
    args: Vec<OsString>,
    env: Vec<(OsString, OsString)>,
    provider: &'static str,
    model: String,
    request_label: String,
}

impl CliInvocation {
    pub(crate) fn new(
        program: impl Into<PathBuf>,
        args: Vec<OsString>,
        provider: &'static str,
        model: impl Into<String>,
        request_label: impl Into<String>,
    ) -> Self {
        Self {
            program: program.into(),
            args,
            env: Vec::new(),
            provider,
            model: model.into(),
            request_label: request_label.into(),
        }
    }

    pub(crate) fn with_env(mut self, key: impl Into<OsString>, value: impl Into<OsString>) -> Self {
        self.env.push((key.into(), value.into()));
        self
    }

    #[cfg(test)]
    pub(crate) fn request_label(&self) -> &str {
        &self.request_label
    }
}

pub(crate) fn account_cli_system_prompt(
    system: Option<&str>,
    tools: &[ToolDefinition],
    transport_name: &str,
) -> Option<String> {
    let mut prompt = String::new();
    if let Some(system) = system.filter(|value| !value.trim().is_empty()) {
        prompt.push_str("# A3S System\n\n");
        prompt.push_str(system.trim());
        prompt.push_str("\n\n");
    }
    if let Some(instructions) = host_tool_instructions(transport_name, tools) {
        prompt.push_str(&instructions);
    }
    (!prompt.trim().is_empty()).then_some(prompt)
}

pub(crate) fn account_cli_prompt(messages: &[Message]) -> String {
    let mut prompt = String::from("# Conversation\n");
    for message in messages {
        prompt.push('\n');
        prompt.push_str(match message.role.as_str() {
            "assistant" => "Assistant",
            "user" => "User",
            "system" => "System",
            role => role,
        });
        prompt.push_str(":\n");
        for block in &message.content {
            match block {
                ContentBlock::Text { text } => {
                    prompt.push_str(text);
                    prompt.push('\n');
                }
                ContentBlock::Image { source } => {
                    let _ = writeln!(prompt, "[image omitted: {}]", source.media_type);
                }
                ContentBlock::ToolUse { id, name, input } => {
                    let block = json!({"id": id, "name": name, "input": input});
                    prompt.push_str(
                        "### A3S host tool call record\n\n\
                         This call has already been requested. Do not repeat it unless the user explicitly asks.\n\n\
                         ```json\n",
                    );
                    let _ = writeln!(prompt, "{block}");
                    prompt.push_str("```\n");
                }
                ContentBlock::ToolResult {
                    tool_use_id,
                    content,
                    is_error,
                } => {
                    let status = if is_error.unwrap_or(false) {
                        "error"
                    } else {
                        "ok"
                    };
                    let block = json!({
                        "tool_use_id": tool_use_id,
                        "status": status,
                        "content": content.as_text(),
                    });
                    prompt.push_str("### A3S host tool result record\n\n```json\n");
                    let _ = writeln!(prompt, "{block}");
                    prompt.push_str("```\n");
                }
            }
        }
    }
    prompt
}

pub(crate) async fn complete_streaming(
    invocation: CliInvocation,
    messages: &[Message],
    tools: &[ToolDefinition],
    cancel_token: CancellationToken,
) -> Result<mpsc::Receiver<StreamEvent>> {
    let request_started_at = Instant::now();
    let prompt = account_cli_prompt(messages);
    let mut child = Command::new(&invocation.program)
        .args(&invocation.args)
        .envs(invocation.env.iter().cloned())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .with_context(|| format!("start {} account CLI", invocation.provider))?;

    let mut stdin = child
        .stdin
        .take()
        .with_context(|| format!("open {} account CLI stdin", invocation.provider))?;
    tokio::spawn(async move {
        let _ = stdin.write_all(prompt.as_bytes()).await;
    });

    let stdout = child
        .stdout
        .take()
        .with_context(|| format!("capture {} account CLI stdout", invocation.provider))?;
    let stderr = child
        .stderr
        .take()
        .with_context(|| format!("capture {} account CLI stderr", invocation.provider))?;

    let (tx, rx) = mpsc::channel(100);
    let host_tools = tools.to_vec();
    tokio::spawn(async move {
        let mut stderr_reader = BufReader::new(stderr);
        let stderr_task = tokio::spawn(async move {
            let mut stderr = String::new();
            let _ = stderr_reader.read_to_string(&mut stderr).await;
            stderr
        });

        let meta = StreamMeta {
            provider: invocation.provider,
            request_model: invocation.model.clone(),
            request_url: invocation.request_label,
            started_at: request_started_at,
        };
        let mut lines = BufReader::new(stdout).lines();
        let mut native_mapper = host_tools
            .is_empty()
            .then(|| AnthropicEventMapper::new(meta.clone()));
        let mut host_tool_mapper =
            (!host_tools.is_empty()).then(|| AccountCliHostToolMapper::new(meta, host_tools));
        let mut failure = None;
        let mut completed = false;

        loop {
            tokio::select! {
                _ = cancel_token.cancelled() => {
                    let _ = child.kill().await;
                    let _ = child.wait().await;
                    let _ = stderr_task.await;
                    return;
                }
                line = lines.next_line() => {
                    let line = match line {
                        Ok(Some(line)) => line,
                        Ok(None) => break,
                        Err(_) => {
                            failure = Some(CliFailure::StreamRead);
                            break;
                        }
                    };
                    let Some(event) = parse_account_cli_stream_event(&line) else {
                        failure = failure.or_else(|| classify_unstructured_output(&line));
                        continue;
                    };
                    if matches!(event, AnthropicStreamEvent::Error) {
                        failure = Some(CliFailure::Provider);
                        break;
                    }
                    let done = if completed {
                        false
                    } else if let Some(mapper) = native_mapper.as_mut() {
                        mapper.handle(event, &tx).await
                    } else if let Some(mapper) = host_tool_mapper.as_mut() {
                        mapper.handle(event, &tx).await
                    } else {
                        false
                    };
                    if done {
                        // WorkBuddy emits assistant/result records after
                        // `message_stop`, sometimes duplicating a large final
                        // answer. Keep draining stdout so the child cannot
                        // deadlock on a full pipe while preserving exactly one
                        // A3S `Done` event.
                        completed = true;
                    }
                }
            }
        }

        let status = child.wait().await.ok();
        let stderr = stderr_task.await.unwrap_or_default();
        if completed {
            return;
        }
        let failure = failure
            .or_else(|| classify_unstructured_output(&stderr))
            .unwrap_or(CliFailure::Incomplete);
        let detail = failure_message(
            invocation.provider,
            &invocation.model,
            failure,
            status.as_ref().and_then(std::process::ExitStatus::code),
        );
        let _ = tx.send(StreamEvent::TextDelta(detail)).await;
    });

    Ok(rx)
}

#[derive(Clone, Copy)]
enum CliFailure {
    Auth,
    Model,
    Provider,
    StreamRead,
    Incomplete,
}

fn classify_unstructured_output(text: &str) -> Option<CliFailure> {
    let lower = text.to_ascii_lowercase();
    if lower.contains("currently supported models") || lower.contains("service info not found") {
        Some(CliFailure::Model)
    } else if lower.contains("not logged in")
        || lower.contains("please login")
        || lower.contains("please log in")
        || lower.contains("authentication required")
        || lower.contains("unauthorized")
    {
        Some(CliFailure::Auth)
    } else {
        None
    }
}

fn failure_message(
    provider: &str,
    model: &str,
    failure: CliFailure,
    status: Option<i32>,
) -> String {
    match failure {
        CliFailure::Auth => format!(
            "{provider} account is not signed in; sign in with the local application and retry"
        ),
        CliFailure::Model => format!(
            "{provider} no longer offers model `{model}`; reopen /model to refresh the account list"
        ),
        CliFailure::Provider => format!("{provider} returned a provider error for `{model}`"),
        CliFailure::StreamRead => format!("{provider} account stream could not be read"),
        CliFailure::Incomplete => match status {
            Some(code) => format!("{provider} account CLI exited with status {code}"),
            None => format!("{provider} account stream ended before completion"),
        },
    }
}

struct AccountCliHostToolMapper {
    meta: StreamMeta,
    tools: Vec<ToolDefinition>,
    text: String,
    usage: TokenUsage,
    stop_reason: Option<String>,
    response_id: Option<String>,
    response_model: Option<String>,
    response_object: Option<String>,
    first_token_ms: Option<u64>,
    visible_pending: String,
    protocol_started: bool,
}

impl AccountCliHostToolMapper {
    fn new(meta: StreamMeta, tools: Vec<ToolDefinition>) -> Self {
        Self {
            meta,
            tools,
            text: String::new(),
            usage: TokenUsage::default(),
            stop_reason: None,
            response_id: None,
            response_model: None,
            response_object: Some("message".into()),
            first_token_ms: None,
            visible_pending: String::new(),
            protocol_started: false,
        }
    }

    async fn handle(
        &mut self,
        event: AnthropicStreamEvent,
        tx: &mpsc::Sender<StreamEvent>,
    ) -> bool {
        match event {
            AnthropicStreamEvent::MessageStart { message } => {
                self.response_id = message.id;
                self.response_model = message.model;
                self.response_object = message.message_type;
                self.usage.prompt_tokens = message.usage.input_tokens;
                self.usage.cache_read_tokens = message.usage.cache_read_input_tokens;
                self.usage.cache_write_tokens = message.usage.cache_creation_input_tokens;
            }
            AnthropicStreamEvent::ContentBlockDelta {
                delta: super::protocol::AnthropicDelta::TextDelta { text },
                ..
            } => {
                self.mark_first_token();
                self.text.push_str(&text);
                self.stream_visible_text(&text, tx).await;
            }
            AnthropicStreamEvent::ContentBlockDelta { .. } => {}
            AnthropicStreamEvent::MessageDelta { delta, usage } => {
                self.stop_reason = Some(delta.stop_reason);
                if let Some(input_tokens) = usage.input_tokens.filter(|tokens| *tokens > 0) {
                    self.usage.prompt_tokens = input_tokens;
                }
                if let Some(cache_read_tokens) =
                    usage.cache_read_input_tokens.filter(|tokens| *tokens > 0)
                {
                    self.usage.cache_read_tokens = Some(cache_read_tokens);
                }
                if let Some(cache_write_tokens) = usage
                    .cache_creation_input_tokens
                    .filter(|tokens| *tokens > 0)
                {
                    self.usage.cache_write_tokens = Some(cache_write_tokens);
                }
                self.usage.completion_tokens = usage.output_tokens;
                self.usage.total_tokens = self.usage.prompt_tokens + self.usage.completion_tokens;
            }
            AnthropicStreamEvent::MessageStop => {
                self.finish(tx).await;
                return true;
            }
            AnthropicStreamEvent::Error => return false,
            _ => {}
        }
        false
    }

    async fn finish(&mut self, tx: &mpsc::Sender<StreamEvent>) {
        let mut content = Vec::new();
        let mut stop_reason = self.stop_reason.clone();

        match parse_host_tool_calls(&self.text, &self.tools) {
            HostToolParseResult::Calls(calls) => {
                stop_reason = Some("tool_use".into());
                for call in calls {
                    let input_delta = call.input.to_string();
                    let _ = tx
                        .send(StreamEvent::ToolUseStart {
                            id: call.id.clone(),
                            name: call.name.clone(),
                        })
                        .await;
                    let _ = tx
                        .send(StreamEvent::ToolUseInputDelta {
                            id: None,
                            delta: input_delta,
                        })
                        .await;
                    content.push(call.into_content_block());
                }
            }
            HostToolParseResult::Invalid(reason) => {
                stop_reason = Some("host_tool_protocol_error".into());
                content.push(ContentBlock::Text {
                    text: format!(
                        "The account model returned an invalid host-tool request ({reason}). Retry the turn."
                    ),
                });
            }
            HostToolParseResult::NoCall if !self.text.is_empty() => {
                let text = std::mem::take(&mut self.text);
                if !self.visible_pending.is_empty() {
                    let pending = std::mem::take(&mut self.visible_pending);
                    let _ = tx.send(StreamEvent::TextDelta(pending)).await;
                }
                content.push(ContentBlock::Text { text });
            }
            HostToolParseResult::NoCall => {}
        }

        let response = LlmResponse {
            message: Message {
                role: "assistant".into(),
                content,
                reasoning_content: None,
            },
            usage: self.usage.clone(),
            stop_reason,
            token_logprobs: Vec::new(),
            meta: Some(LlmResponseMeta {
                provider: Some(self.meta.provider.into()),
                request_model: Some(self.meta.request_model.clone()),
                request_url: Some(self.meta.request_url.clone()),
                response_id: self.response_id.clone(),
                response_model: self.response_model.clone(),
                response_object: self.response_object.clone(),
                first_token_ms: self.first_token_ms,
                duration_ms: Some(self.meta.started_at.elapsed().as_millis() as u64),
            }),
        };
        let _ = tx.send(StreamEvent::Done(response)).await;
    }

    fn mark_first_token(&mut self) {
        if self.first_token_ms.is_none() {
            self.first_token_ms = Some(self.meta.started_at.elapsed().as_millis() as u64);
        }
    }

    async fn stream_visible_text(&mut self, delta: &str, tx: &mpsc::Sender<StreamEvent>) {
        if self.protocol_started {
            return;
        }
        self.visible_pending.push_str(delta);

        if let Some(start) = tool_protocol_start(&self.visible_pending) {
            let visible = self.visible_pending[..start].to_string();
            self.visible_pending.clear();
            self.protocol_started = true;
            if !visible.is_empty() {
                let _ = tx.send(StreamEvent::TextDelta(visible)).await;
            }
            return;
        }

        let held = partial_tool_protocol_suffix_len(&self.visible_pending);
        let visible_end = self.visible_pending.len().saturating_sub(held);
        if visible_end > 0 {
            let visible = self.visible_pending[..visible_end].to_string();
            self.visible_pending.drain(..visible_end);
            let _ = tx.send(StreamEvent::TextDelta(visible)).await;
        }
    }
}

const TOOL_PROTOCOL_PREFIXES: &[&str] = &[
    "<function_calls>",
    "<invoke ",
    "<A3S_TOOL_CALLS>",
    "<tool_calls:",
    "<tool_call:",
    "<A3S_ASSISTANT_TOOL_CALL>",
    "<A3S_TOOL_RESULT>",
];

fn tool_protocol_start(text: &str) -> Option<usize> {
    TOOL_PROTOCOL_PREFIXES
        .iter()
        .filter_map(|prefix| text.find(prefix))
        .min()
}

fn partial_tool_protocol_suffix_len(text: &str) -> usize {
    TOOL_PROTOCOL_PREFIXES
        .iter()
        .map(|prefix| {
            (1..prefix.len())
                .rev()
                .find(|length| text.ends_with(&prefix[..*length]))
                .unwrap_or(0)
        })
        .max()
        .unwrap_or(0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn prompt_preserves_tool_history_as_structured_blocks() {
        let prompt = account_cli_prompt(&[
            Message::user("hello"),
            Message {
                role: "assistant".into(),
                content: vec![ContentBlock::ToolUse {
                    id: "toolu_1".into(),
                    name: "read".into(),
                    input: json!({"file_path":"README.md"}),
                }],
                reasoning_content: None,
            },
            Message::tool_result("toolu_1", "contents", false),
        ]);

        assert!(prompt.contains("User:\nhello"));
        assert!(prompt.contains("### A3S host tool call record"));
        assert!(prompt.contains("### A3S host tool result record"));
        assert!(!prompt.contains("<A3S_ASSISTANT_TOOL_CALL>"));
        assert!(!prompt.contains("<A3S_TOOL_RESULT>"));
        assert!(prompt.contains("\"status\":\"ok\""));
    }

    #[test]
    fn system_prompt_names_transport_and_never_embeds_tools_in_user_history() {
        let prompt = account_cli_system_prompt(
            Some("Be concise."),
            &[ToolDefinition {
                name: "read".into(),
                description: "Read a file".into(),
                parameters: json!({"type":"object"}),
            }],
            "WorkBuddy",
        )
        .unwrap();

        assert!(prompt.contains("# A3S System"));
        assert!(prompt.contains("WorkBuddy's own built-in tools"));
        assert!(prompt.contains("<function_calls>"));
    }

    #[tokio::test]
    async fn host_tool_mapper_emits_a3s_tool_events() {
        let tools = vec![ToolDefinition {
            name: "read".into(),
            description: "Read a file".into(),
            parameters: json!({
                "type":"object",
                "properties":{"file_path":{"type":"string"}},
                "required":["file_path"]
            }),
        }];
        let mut mapper = AccountCliHostToolMapper::new(
            StreamMeta {
                provider: "fake-account-cli",
                request_model: "model".into(),
                request_url: "fake account CLI".into(),
                started_at: Instant::now(),
            },
            tools,
        );
        let (tx, mut rx) = mpsc::channel(10);
        let lines = [
            r#"{"type":"stream_event","event":{"type":"message_start","message":{"id":"msg_1","type":"message","model":"model","usage":{"input_tokens":3,"output_tokens":0}}}}"#,
            r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"<function_calls><invoke name=\"Read\"><parameter name=\"file_path\">README.md</parameter></invoke></function_calls>"}}}"#,
            r#"{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":7}}}"#,
            r#"{"type":"stream_event","event":{"type":"message_stop"}}"#,
        ];
        for line in lines {
            let event = parse_account_cli_stream_event(line).unwrap();
            if mapper.handle(event, &tx).await {
                break;
            }
        }

        assert!(matches!(
            rx.recv().await,
            Some(StreamEvent::ToolUseStart { name, .. }) if name == "read"
        ));
        assert!(matches!(
            rx.recv().await,
            Some(StreamEvent::ToolUseInputDelta { delta, .. }) if delta.contains("README.md")
        ));
        let Some(StreamEvent::Done(response)) = rx.recv().await else {
            panic!("expected done");
        };
        assert_eq!(
            response.tool_calls()[0].args,
            json!({"file_path":"README.md"})
        );
    }

    #[tokio::test]
    async fn host_tool_mapper_streams_normal_text_without_waiting_for_message_stop() {
        let mut mapper = AccountCliHostToolMapper::new(
            StreamMeta {
                provider: "fake-account-cli",
                request_model: "model".into(),
                request_url: "fake account CLI".into(),
                started_at: Instant::now(),
            },
            vec![ToolDefinition {
                name: "read".into(),
                description: "Read a file".into(),
                parameters: json!({"type":"object"}),
            }],
        );
        let (tx, mut rx) = mpsc::channel(10);
        let first = parse_account_cli_stream_event(
            r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}}"#,
        )
        .unwrap();

        assert!(!mapper.handle(first, &tx).await);
        assert!(matches!(
            rx.recv().await,
            Some(StreamEvent::TextDelta(text)) if text == "Hello"
        ));
    }

    #[tokio::test]
    async fn host_tool_mapper_hides_split_workbuddy_protocol_after_prose() {
        let mut mapper = AccountCliHostToolMapper::new(
            StreamMeta {
                provider: "fake-account-cli",
                request_model: "hy3".into(),
                request_url: "fake account CLI".into(),
                started_at: Instant::now(),
            },
            vec![ToolDefinition {
                name: "ls".into(),
                description: "List a directory".into(),
                parameters: json!({
                    "type":"object",
                    "properties":{"path":{"type":"string"}},
                    "required":["path"]
                }),
            }],
        );
        let (tx, mut rx) = mpsc::channel(10);
        for line in [
            r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"I'll list the workspace.<tool_"}}}"#,
            r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"calls:group_1>\n<tool_call:call_1>ls\">\n<parameter name=\"path\">/work/a3s</parameter>\n</invoke>\n</function_calls>"}}}"#,
            r#"{"type":"stream_event","event":{"type":"message_stop"}}"#,
        ] {
            let event = parse_account_cli_stream_event(line).unwrap();
            if mapper.handle(event, &tx).await {
                break;
            }
        }

        let mut events = Vec::new();
        while let Ok(event) = rx.try_recv() {
            events.push(event);
        }
        assert!(events.iter().any(
            |event| matches!(event, StreamEvent::TextDelta(text) if text == "I'll list the workspace.")
        ));
        assert!(!events
            .iter()
            .any(|event| matches!(event, StreamEvent::TextDelta(text) if text.contains('<'))));
        assert!(events
            .iter()
            .any(|event| matches!(event, StreamEvent::ToolUseStart { name, .. } if name == "ls")));
        let response = events.into_iter().find_map(|event| match event {
            StreamEvent::Done(response) => Some(response),
            _ => None,
        });
        assert_eq!(
            response.unwrap().tool_calls()[0].args,
            json!({"path":"/work/a3s"})
        );
    }

    #[tokio::test]
    async fn host_tool_mapper_never_exposes_echoed_legacy_history_tags() {
        let mut mapper = AccountCliHostToolMapper::new(
            StreamMeta {
                provider: "fake-account-cli",
                request_model: "hy3".into(),
                request_url: "fake account CLI".into(),
                started_at: Instant::now(),
            },
            vec![],
        );
        let (tx, mut rx) = mpsc::channel(10);
        for line in [
            r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Continuing from the previous result.<A3S_ASSISTANT_"}}}"#,
            r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"TOOL_CALL>\n{\"id\":\"call_1\"}\n</A3S_ASSISTANT_TOOL_CALL>\n<A3S_TOOL_RESULT>\n{\"status\":\"ok\"}\n</A3S_TOOL_RESULT>"}}}"#,
            r#"{"type":"stream_event","event":{"type":"message_stop"}}"#,
        ] {
            let event = parse_account_cli_stream_event(line).unwrap();
            if mapper.handle(event, &tx).await {
                break;
            }
        }

        let mut response = None;
        while let Ok(event) = rx.try_recv() {
            match event {
                StreamEvent::TextDelta(text) => {
                    assert!(!text.contains("<A3S"));
                }
                StreamEvent::Done(done) => response = Some(done),
                _ => {}
            }
        }

        let response = response.expect("expected final response");
        for block in response.message.content {
            if let ContentBlock::Text { text } = block {
                assert!(!text.contains("<A3S"));
            }
        }
        assert_eq!(
            response.stop_reason.as_deref(),
            Some("host_tool_protocol_error")
        );
    }
}