a4aa 0.5.1

Adapter-For-All-Agents — unified agent client adapter: process supervision, stdio/PTY transports, JSON-RPC and ACP lanes, CLI drivers, and a stdio sidecar for hosts in other languages.
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
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
//! Argv-driven CLI lane: the fallback for agents without a protocol driver.
//!
//! One turn = one spawned process. The prompt travels either through an
//! `{prompt}` argument placeholder or through stdin; output streams back as
//! `MessageChunk` events byte-for-byte — the lane imposes no format, schema,
//! or validation on agent output (zero-assumption: natural language is a
//! legal reply).
//!
//! Two stream shapes: `Stdio` pipes, or `Pty` (Unix) for CLIs that only
//! stream correctly on a terminal. PTY selection degrades to Stdio
//! off-Unix.

use std::io;

use serde_json::Value;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::{mpsc, watch};

use crate::bounds;
use crate::env::SecretResolver;
use crate::error::AdapterError;
use crate::model::{
    token_usage, AgentEvent, BindingReceiptShape, CliLaneSpec, CommandSpec, PromptChannel,
    SessionRequest, StreamMode, TokenUsage, TurnOutcome,
};
use crate::observe::{Direction, SharedObserver};
use crate::supervision::{IoPlan, StdioKind, SupervisedChild};
use crate::template::{
    contains_placeholder, contains_placeholder_text, extract_path, substitute, substitute_text,
    vars, VarSpec, Vars,
};

pub struct CliLane {
    pub command: CommandSpec,
    pub lane: CliLaneSpec,
    pub secrets: std::sync::Arc<dyn SecretResolver>,
    pub observer: SharedObserver,
    pub max_output_bytes: Option<usize>,
    /// Fired by `AgentSession::cancel`; the in-flight turn's process tree is
    /// terminated. Explicit user cancellation only — transport failures and
    /// observer disconnects never reach this.
    pub cancelled: std::sync::Arc<watch::Sender<()>>,
    /// Session binding captured from a `sessionIdPath` match on stdout —
    /// the first observed value wins and later turns resume through it.
    pub observed: std::sync::Arc<std::sync::OnceLock<String>>,
    /// A turn's process-cleanup failure is recorded here and surfaced by
    /// `AgentSession::close`; an I/O failure's own error stays the turn's
    /// result and never shadows the cleanup failure.
    pub cleanup_failure: std::sync::Mutex<Option<AdapterError>>,
}

impl CliLane {
    fn record_cleanup_failure(&self, error: AdapterError) {
        *self.cleanup_failure.lock().unwrap() = Some(error);
    }

    /// The CLI lane has no native MCP registration surface: it cannot pass a
    /// server list to the spawned agent. A non-empty list is refused before
    /// any process exists, instead of a verifier turn silently running
    /// without the tools that carry its verdict.
    pub(crate) fn admit_mcp(&self) -> Result<(), AdapterError> {
        if self.lane.mcp_servers.is_empty() {
            return Ok(());
        }
        Err(AdapterError::permanent(
            "agent_mcp_server_invalid",
            "session/open",
        )
        .with_detail(
            "the cli lane declares no MCP registration surface; refusing to run without the requested servers",
        ))
    }
}

impl CliLane {
    /// Run the declared session-creation command (`open_args`) once when no
    /// resume binding exists; the first non-empty stdout line becomes the
    /// observed session binding (for example `cursor-agent create-chat`).
    pub async fn open_binding(&self, request: &SessionRequest) -> Result<(), AdapterError> {
        self.admit_mcp()?;
        let requested = request
            .resume
            .as_deref()
            .is_some_and(|v| !v.trim().is_empty());
        if self.lane.open_args.is_empty() || requested || self.observed.get().is_some() {
            return Ok(());
        }
        let cwd = request
            .cwd
            .clone()
            .or_else(|| self.command.cwd.clone())
            .map(|path| path.display().to_string())
            .unwrap_or_else(|| std::env::temp_dir().display().to_string());
        let (args, _) = substitute_args(
            &self.lane.open_args,
            &vars(VarSpec {
                cwd: &cwd,
                ..Default::default()
            }),
        );
        let mut spec = self.command.clone();
        spec.args.extend(args);
        let env = crate::env::resolve(&spec.env, &request.env, &*self.secrets).await?;
        let mut command = SupervisedChild::command(&spec, &env, request.cwd.as_deref());
        command
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped());
        let mut child = SupervisedChild::spawn(
            &mut command,
            IoPlan {
                stdin: StdioKind::Preserve,
                stdout: StdioKind::Preserve,
                stderr: StdioKind::Preserve,
            },
        )?;
        if let Some(stderr) = child.stderr() {
            tokio::spawn(bounds::drain_stderr(
                stderr,
                self.observer.clone(),
                bounds::StderrTail::default(),
            ));
        }
        let Some(mut stdout) = child.stdout() else {
            let _ = child.terminate().await;
            return Err(AdapterError::permanent(
                "agent_pipe_unavailable",
                "cli/stdio",
            ));
        };
        // Only the first non-empty line is a binding. Bound that line and
        // drain everything else without retaining it, including on overflow.
        let limit = self
            .max_output_bytes
            .unwrap_or(crate::framing::DEFAULT_MAX_MESSAGE_BYTES);
        let mut line = Vec::new();
        let mut id = None;
        let mut oversized = false;
        let mut buffer = [0u8; bounds::READ_CHUNK];
        loop {
            let count = match AsyncReadExt::read(&mut stdout, &mut buffer).await {
                Ok(count) => count,
                Err(error) => {
                    let _ = child.terminate().await;
                    return Err(AdapterError::io(
                        "agent_output_read_failed",
                        "cli/stdout",
                        error,
                    ));
                }
            };
            if count == 0 {
                break;
            }
            if id.is_some() || oversized {
                continue;
            }
            for byte in &buffer[..count] {
                if *byte == b'\n' {
                    let candidate = String::from_utf8_lossy(&line).trim().to_owned();
                    line.clear();
                    if !candidate.is_empty() {
                        id = Some(candidate);
                        break;
                    }
                } else if line.len() < limit {
                    line.push(*byte);
                } else {
                    oversized = true;
                    line.clear();
                    break;
                }
            }
        }
        let exit = child
            .wait_natural(None)
            .await
            .map_err(|error| AdapterError::io("agent_process_wait_failed", "cli/open", error))?;
        if oversized {
            return Err(AdapterError::permanent(
                "agent_session_id_too_large",
                "cli/open",
            ));
        }
        if exit.is_some_and(|status| !status.success()) {
            return Err(AdapterError::permanent(
                "agent_session_open_failed",
                "cli/open",
            ));
        }
        if id.is_none() {
            let candidate = String::from_utf8_lossy(&line).trim().to_owned();
            if !candidate.is_empty() {
                id = Some(candidate);
            }
        }
        match id {
            Some(id) => {
                let _ = self.observed.set(id);
                Ok(())
            }
            None => Err(AdapterError::permanent(
                "agent_session_id_missing",
                "cli/open",
            )),
        }
    }

    /// Run one turn to completion. `events` receives output chunks as they
    /// arrive. Output retention follows the configured budget; protocol
    /// records use a separate bounded parsing buffer.
    pub async fn run_turn(
        &self,
        request: &SessionRequest,
        prompt: &str,
        model: Option<&str>,
        effort: Option<&str>,
        events: mpsc::UnboundedSender<AgentEvent>,
    ) -> Result<TurnOutcome, AdapterError> {
        // Each active turn observes cancellation even between I/O awaits.
        // A subscription starts at the current version, so idle cancellation
        // never carries over to a later turn.
        let cancelled = self.cancelled.subscribe();
        self.run_turn_subscribed(request, prompt, model, effort, events, cancelled)
            .await
    }

    /// The facade subscribes before its shared-close check, so closing cannot
    /// fall into a gap between admission and cancellation subscription.
    pub(crate) async fn run_turn_subscribed(
        &self,
        request: &SessionRequest,
        prompt: &str,
        model: Option<&str>,
        effort: Option<&str>,
        events: mpsc::UnboundedSender<AgentEvent>,
        cancelled: watch::Receiver<()>,
    ) -> Result<TurnOutcome, AdapterError> {
        // The effective session binding: an explicitly requested resume
        // first, then whatever the agent already reported on stdout or a
        // session-creation command.
        let binding = request
            .resume
            .as_deref()
            .filter(|v| !v.trim().is_empty())
            .map(str::to_owned)
            .or_else(|| self.observed.get().cloned());
        self.admit_mcp()?;
        // An explicitly requested binding the CLI cannot express fails
        // closed — silently running a fresh process would lose the host's
        // session. `resume_args` or a `{sessionId}` placeholder inside the
        // argv template both count as an expressible binding.
        let resume_expressible = self.lane.capabilities().session_resume;
        if request
            .resume
            .as_deref()
            .is_some_and(|v| !v.trim().is_empty())
            && !resume_expressible
        {
            return Err(AdapterError::permanent(
                "agent_session_resume_unsupported",
                "cli/args",
            ));
        }
        // A resume-shaped argv prefix (a subcommand that reorders argv,
        // `codex exec resume <id>`) replaces the spawn args wholesale;
        // in that shape `resume_args` is the complete arg template —
        // flags valid only at session creation (`-C`, `-s`) do not
        // reappear. Without a spawn prefix `resume_args` appends.
        let resume_spawn = binding.is_some() && !self.lane.resume_spawn_args.is_empty();
        let mut args = if resume_spawn {
            self.lane.resume_args.clone()
        } else {
            self.lane.args.clone()
        };
        if !resume_spawn && binding.is_some() && !self.lane.resume_args.is_empty() {
            args.extend(self.lane.resume_args.iter().cloned());
        }
        let model = model.or(request.config.model.as_deref());
        let effort = effort.or(request.config.effort.as_deref());
        // Lanes that encode effort inside the model id rewrite the tier
        // segment — `effort` has no argument of its own on that wire, so
        // the folded value travels through `{model}` alone.
        let effort_carrier = || {
            self.lane
                .args
                .iter()
                .any(|argument| contains_placeholder_text(argument, "effort"))
                || self
                    .lane
                    .stdin_template
                    .as_ref()
                    .is_some_and(|template| contains_placeholder(template, "effort"))
        };
        let folded_model = match (model, effort) {
            (Some(base), Some(level))
                if !self.lane.effort_model_suffixes.is_empty() && !level.trim().is_empty() =>
            {
                Some(effort_suffixed_model(
                    base,
                    level,
                    &self.lane.effort_model_suffixes,
                ))
            }
            (None, Some(level))
                if !self.lane.effort_model_suffixes.is_empty()
                    && !level.trim().is_empty()
                    && !effort_carrier() =>
            {
                // Without a model there is nothing the tier can rewrite,
                // and this lane has no direct effort argument either.
                return Err(AdapterError::permanent(
                    "agent_effort_unsupported",
                    "cli/args",
                ));
            }
            _ => None,
        };
        let (model, effort) = match folded_model.as_deref() {
            Some(folded) => (Some(folded), None),
            None => (model, effort),
        };
        let instructions = request.config.instructions.as_deref();
        let permission = request.config.permission.as_deref();
        let cwd = request
            .cwd
            .clone()
            .or_else(|| self.command.cwd.clone())
            .map(|path| path.display().to_string())
            .unwrap_or_else(|| std::env::temp_dir().display().to_string());
        let turn_vars = vars(VarSpec {
            session_id: binding.as_deref(),
            cwd: &cwd,
            prompt,
            model,
            effort,
            mode: request.config.mode.as_deref(),
            instructions,
            permission,
            provider: request.config.provider.as_deref(),
            ..Default::default()
        });
        let (args, prompt_via_stdin) = substitute_args(&args, &turn_vars);
        let write_stdin = match self.lane.prompt_channel {
            PromptChannel::Stdin => true,
            PromptChannel::ArgOnly if !prompt_via_stdin => false,
            PromptChannel::ArgOnly => {
                return Err(AdapterError::permanent(
                    "agent_prompt_channel_unsatisfied",
                    "cli/args",
                ));
            }
            PromptChannel::ArgOrStdin => prompt_via_stdin,
        };
        let stdin_payload = write_stdin.then(|| match &self.lane.stdin_template {
            Some(template) => {
                let mut rendered = serde_json::to_string(&substitute(template, &turn_vars))
                    .unwrap_or_else(|_| template.to_string());
                rendered.push('\n');
                rendered
            }
            None => {
                let mut payload = prompt.to_owned();
                if !payload.ends_with('\n') {
                    payload.push('\n');
                }
                payload
            }
        });
        let mut spec = self.command.clone();
        if resume_spawn {
            spec.args = substitute_args(&self.lane.resume_spawn_args, &turn_vars).0;
        }
        // The command's own args are the fixed spawn prefix (subcommands,
        // wrapper flags); the lane's resolved template follows them.
        spec.args.extend(args);
        let mut env = crate::env::resolve(&spec.env, &request.env, &*self.secrets).await?;
        // A `binding_file_env` lane mints the scratch receipt path the
        // agent's installed hook writes this turn's session identity to.
        let receipt = self
            .lane
            .binding_file_env
            .as_ref()
            .map(|name| {
                let mut builder = tempfile::Builder::new();
                builder.prefix("a4aa-receipt-");
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    builder.permissions(std::fs::Permissions::from_mode(0o700));
                }
                let directory = builder.tempdir().map_err(|error| {
                    AdapterError::io("agent_receipt_directory_failed", "cli/receipt", error)
                })?;
                let path = directory.path().join("receipt.json");
                env.insert(name.clone(), path.as_os_str().to_os_string());
                Ok::<_, AdapterError>((directory, path))
            })
            .transpose()?;
        let command = SupervisedChild::command(&spec, &env, request.cwd.as_deref());

        let mut outcome = match effective_stream(self.lane.stream) {
            StreamMode::Stdio => {
                self.run_stdio(command, stdin_payload, events, cancelled)
                    .await
            }
            #[cfg(unix)]
            StreamMode::Pty => {
                self.run_pty(command, stdin_payload, events, cancelled)
                    .await
            }
            #[cfg(not(unix))]
            StreamMode::Pty => unreachable!("effective_stream degrades pty off-unix"),
        };
        if let Some((directory, receipt)) = receipt {
            let limit = self
                .max_output_bytes
                .unwrap_or(crate::framing::DEFAULT_MAX_MESSAGE_BYTES)
                .max(crate::framing::DEFAULT_MAX_MESSAGE_BYTES);
            let metadata = match read_receipt(&receipt, self.lane.binding_receipt_shape, limit) {
                Ok(metadata) => metadata,
                Err(mut error) => {
                    // Keep failed receipt evidence; the external turn may have
                    // completed, so this error must never invite whole-turn replay.
                    let mut details = error.detail.take().into_iter().collect::<Vec<_>>();
                    if let Err(turn_error) = &outcome {
                        details.push(format!("turn error: {turn_error}"));
                    } else if let Ok(done) = &outcome {
                        if done.stop_reason != "end_turn" {
                            details.push(format!("turn stop reason: {}", done.stop_reason));
                        }
                    }
                    details.push(format!("receipt evidence: {}", directory.keep().display()));
                    error.detail = Some(details.join("; "));
                    return Err(error);
                }
            };
            if let Some(metadata) = metadata {
                let _ = self.observed.set(metadata.session_id);
                if let Ok(done) = outcome.as_mut() {
                    if done.usage.is_none() {
                        done.usage = metadata.usage;
                    }
                }
            }
            if let Err(error) = &mut outcome {
                let evidence = format!("receipt evidence: {}", directory.keep().display());
                error.detail = Some(match error.detail.take() {
                    Some(detail) => format!("{detail}; {evidence}"),
                    None => evidence,
                });
            } else {
                directory
                    .close()
                    .map_err(|_| receipt_error("agent_receipt_cleanup_failed"))?;
            }
        }
        outcome
    }

    async fn run_stdio(
        &self,
        mut command: tokio::process::Command,
        stdin_payload: Option<String>,
        events: mpsc::UnboundedSender<AgentEvent>,
        mut cancelled: watch::Receiver<()>,
    ) -> Result<TurnOutcome, AdapterError> {
        command
            .stdin(if stdin_payload.is_some() {
                std::process::Stdio::piped()
            } else {
                std::process::Stdio::null()
            })
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped());
        let mut child = SupervisedChild::spawn(
            &mut command,
            IoPlan {
                stdin: StdioKind::Preserve,
                stdout: StdioKind::Preserve,
                stderr: StdioKind::Preserve,
            },
        )?;
        let stderr_tail = bounds::StderrTail::default();
        let stderr_drain = child
            .stderr()
            .map(|stderr| bounds::drain_stderr(stderr, self.observer.clone(), stderr_tail.clone()));
        let stdin = child.stdin();
        let stdout = child
            .stdout()
            .ok_or_else(|| AdapterError::permanent("agent_pipe_unavailable", "cli/stdio"))?;
        let output_cancelled = cancelled.clone();
        let write = async {
            if let Some(payload) = stdin_payload {
                let mut stdin = stdin.ok_or_else(|| {
                    AdapterError::permanent("agent_pipe_unavailable", "cli/stdin")
                })?;
                if let Some(observer) = self.observer.as_ref() {
                    observer.record("cli.stdin", Direction::Sent, payload.as_bytes());
                }
                tokio::select! {
                    biased;
                    _ = cancelled.changed() => {}
                    result = stdin.write_all(payload.as_bytes()) => {
                        match result {
                            // The child already closed stdin — it exited
                            // (or stopped listening) before the prompt
                            // arrived. The prompt is undeliverable, and the
                            // child's own exit outcome is the authoritative
                            // verdict for this turn; keep collecting so the
                            // real exit classification surfaces.
                            Err(error) if error.kind() == io::ErrorKind::BrokenPipe => {}
                            result => result.map_err(|error| {
                                AdapterError::io("agent_prompt_write_failed", "cli/stdin", error)
                            })?,
                        }
                    }
                }
            }
            Ok::<(), AdapterError>(())
        };
        // The prompt write rides inside `collect`'s select so a write
        // failure keeps the child owned: the driver terminates and awaits
        // it explicitly instead of relying on `Drop`'s best effort.
        self.collect(
            child,
            Box::new(stdout),
            events,
            output_cancelled,
            write,
            stderr_tail,
            stderr_drain,
        )
        .await
    }

    #[cfg(unix)]
    async fn run_pty(
        &self,
        command: tokio::process::Command,
        stdin_payload: Option<String>,
        events: mpsc::UnboundedSender<AgentEvent>,
        mut cancelled: watch::Receiver<()>,
    ) -> Result<TurnOutcome, AdapterError> {
        let (mut child, master) = crate::transport::pty::spawn(command)?;
        let stderr_tail = bounds::StderrTail::default();
        let stderr_drain = child
            .stderr()
            .map(|stderr| bounds::drain_stderr(stderr, self.observer.clone(), stderr_tail.clone()));
        let master = master.with_observer(self.observer.clone());
        let writer = master.writer();
        let output_cancelled = cancelled.clone();
        let write = async {
            if let Some(payload) = stdin_payload {
                tokio::select! {
                    biased;
                    _ = cancelled.changed() => {}
                    result = writer.write(payload.as_bytes()) => {
                        match result {
                            // Same verdict as the stdio write: a dead slave
                            // makes the master write fail with EIO — keep
                            // collecting for the child's real exit outcome.
                            Err(error) if error.raw_os_error() == Some(libc::EIO) => {}
                            result => {
                                result.map_err(|error| {
                                    AdapterError::io(
                                        "agent_prompt_write_failed",
                                        "cli/pty",
                                        error,
                                    )
                                })?;
                            }
                        }
                    }
                }
            }
            Ok::<(), AdapterError>(())
        };
        // Same ownership as run_stdio: the write rides inside `collect`.
        self.collect(
            child,
            Box::new(master),
            events,
            output_cancelled,
            write,
            stderr_tail,
            stderr_drain,
        )
        .await
    }

    /// Drain the output stream, emitting chunks, until EOF. There is no
    /// deadline: output EOF is not an exit signal — the process owns its
    /// cleanup and the real exit status is awaited. The prompt `write`
    /// future rides the same select; a write or read failure still
    /// terminates and reaps the child explicitly before the original error
    /// returns, and a cleanup failure is recorded for `close`.
    #[allow(clippy::too_many_arguments)]
    async fn collect<W: std::future::Future<Output = Result<(), AdapterError>>>(
        &self,
        mut child: SupervisedChild,
        mut reader: Box<dyn ChunkRead>,
        events: mpsc::UnboundedSender<AgentEvent>,
        mut cancelled: watch::Receiver<()>,
        write: W,
        stderr_tail: bounds::StderrTail,
        stderr_drain: Option<tokio::task::JoinHandle<io::Result<()>>>,
    ) -> Result<TurnOutcome, AdapterError> {
        // Turn accumulators: retained text (fallback), NDJSON carry,
        // last-seen usage, and the authoritative result-record text.
        let mut state = TurnCollect::default();
        // Undecoded carry: a multi-byte scalar split across read boundaries
        // stays buffered until the rest of it arrives — decoded output must
        // be the agent's bytes verbatim, never U+FFFD-injected.
        let mut pending: Vec<u8> = Vec::new();
        let mut buffer = [0u8; bounds::READ_CHUNK];
        let mut write = std::pin::pin!(write);
        let mut write_done = false;
        loop {
            tokio::select! {
                biased;
                _ = cancelled.changed() => {
                    child.terminate().await?;
                    return Ok(cancelled_outcome(state));
                }
                result = &mut write, if !write_done => {
                    write_done = true;
                    if let Err(error) = result {
                        // The prompt never fully reached the agent: the turn
                        // is broken, but the process stays owned — terminate
                        // and await the reap before the error returns.
                        if let Err(cleanup) = child.terminate().await {
                            self.record_cleanup_failure(cleanup);
                        }
                        return Err(error);
                    }
                }
                count = reader.read(&mut buffer) => {
                    match count {
                        Ok(0) => break,
                        Ok(count) => {
                            pending.extend_from_slice(&buffer[..count]);
                            loop {
                                match std::str::from_utf8(&pending) {
                                    Ok(_) => {
                                        let text = std::mem::take(&mut pending);
                                        // Checked above: the whole buffer is valid.
                                        let text =
                                            String::from_utf8(text).unwrap_or_default();
                                        self.process_text(&text, &mut state, &events);
                                        break;
                                    }
                                    Err(error) => {
                                        let valid = error.valid_up_to();
                                        let mut text =
                                            String::from_utf8(pending[..valid].to_vec())
                                                .unwrap_or_default();
                                        match error.error_len() {
                                            // Incomplete tail: keep it buffered
                                            // for the next read.
                                            None => {
                                                pending.drain(..valid);
                                                self.process_text(&text, &mut state, &events);
                                                break;
                                            }
                                            Some(bad) => {
                                                text.push('\u{FFFD}');
                                                pending.drain(..valid + bad);
                                                self.process_text(&text, &mut state, &events);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        Err(error) => {
                            // A failed read is not EOF — the turn is broken and the
                            // caller must see it, not a quiet half-answer. Same
                            // ownership: terminate and await the reap, and record
                            // a cleanup failure rather than shadowing the read
                            // error with it.
                            if let Err(cleanup) = child.terminate().await {
                                self.record_cleanup_failure(cleanup);
                            }
                            return Err(AdapterError::io(
                                "agent_output_read_failed",
                                "cli/stdout",
                                error,
                            ));
                        }
                    }
                }
            }
        }
        if !pending.is_empty() {
            let tail = String::from_utf8_lossy(&pending).into_owned();
            self.process_text(&tail, &mut state, &events);
        }
        if self.lane.json_lines && !state.lines.is_empty() {
            let tail = std::mem::take(&mut state.lines);
            self.absorb_line(tail.trim_end_matches('\r'), &events, &mut state);
        }
        let exit = tokio::select! {
            biased;
            _ = cancelled.changed() => {
                child.terminate().await?;
                return Ok(cancelled_outcome(state));
            }
            result = child.wait_natural(None) => {
                result.map_err(|e| AdapterError::io("agent_process_wait_failed", "process/wait", e))?
            }
        };
        let succeeded = exit.is_some_and(|status| status.success());
        if !succeeded {
            // The child's own error text names quota, capacity and model
            // refusals a flattened "error" stop reason would lose: surface
            // them as a typed failure so custody can fail over to another
            // candidate instead of retrying in place. Exit can race the
            // drain task — join it first so the final refusal line is in
            // the snapshot, not still in flight.
            if let Some(drain) = stderr_drain {
                let _ = drain.await;
            }
            let tail = stderr_tail.text();
            let haystack = format!("{tail}\n{}", state.output);
            if crate::error::quota_or_capacity_refusal(&haystack) {
                let detail = tail
                    .lines()
                    .rev()
                    .find(|line| !line.trim().is_empty())
                    .map(str::trim)
                    .unwrap_or("child exited non-zero");
                // Any stdout the child produced before the refusal may
                // reflect billable work — the effect stays uncertain. A
                // refusal in stderr alone with empty stdout is an
                // admission rejection: nothing ran, so custody may replay
                // the request on the next candidate.
                return Err(
                    if state.result_text.is_some() || !state.output.trim().is_empty() {
                        AdapterError::uncertain("agent_quota_exhausted", "cli/turn")
                            .with_detail(detail.to_string())
                    } else {
                        AdapterError::retryable("agent_quota_exhausted", "cli/turn")
                            .with_detail(detail.to_string())
                    },
                );
            }
            if crate::error::session_not_found_refusal(&haystack) {
                let detail = tail
                    .lines()
                    .rev()
                    .find(|line| !line.trim().is_empty())
                    .map(str::trim)
                    .unwrap_or("child exited non-zero");
                return Err(
                    AdapterError::permanent("agent_session_not_found", "cli/turn")
                        .with_detail(detail.to_string()),
                );
            }
        }
        Ok(TurnOutcome {
            // `ExitStatus::code()` is `None` when a signal killed the child;
            // success is judged by `success()`, never by a missing code.
            stop_reason: if succeeded { "end_turn" } else { "error" }.to_owned(),
            text: state.result_text.unwrap_or(state.output),
            truncated: state.truncated,
            exit_code: exit.and_then(|status| status.code()),
            usage: state.usage,
        })
    }

    /// One decoded span: cap-retained text plus the chunk event — a
    /// `jsonLines` stream instead reports each record as `Raw` and never
    /// pastes protocol bytes into message chunks.
    fn process_text(
        &self,
        text: &str,
        state: &mut TurnCollect,
        events: &mpsc::UnboundedSender<AgentEvent>,
    ) {
        self.retain_text(text, state, events);
        if self.lane.json_lines {
            self.absorb_lines(text, state, events);
        }
    }

    /// Split complete lines out of the carry and absorb each.
    fn absorb_lines(
        &self,
        text: &str,
        state: &mut TurnCollect,
        events: &mpsc::UnboundedSender<AgentEvent>,
    ) {
        // Like framing::read_frame and Tokio's LinesCodec, inspect only the
        // new chunk and drain an oversized record through its terminator.
        // Keep enough scratch space for small control records even when the
        // caller's retained-output budget is very small.
        let frame_limit = self
            .max_output_bytes
            .map(|limit| limit.max(crate::framing::DEFAULT_MAX_MESSAGE_BYTES));
        for part in text.split_inclusive('\n') {
            let complete = part.ends_with('\n');
            let content = part.strip_suffix('\n').unwrap_or(part);
            if !state.discarding_line {
                if frame_limit
                    .is_some_and(|limit| state.lines.len().saturating_add(content.len()) > limit)
                {
                    state.lines.clear();
                    state.discarding_line = true;
                    signal_truncated(state, events);
                } else {
                    state.lines.push_str(content);
                }
            }
            if complete {
                if !state.discarding_line {
                    let line = std::mem::take(&mut state.lines);
                    self.absorb_line(line.trim_end_matches('\r'), events, state);
                }
                state.discarding_line = false;
            }
        }
    }

    /// One NDJSON record: arrives as `AgentEvent::Raw`, and the declared
    /// session-id, usage and result-text paths are extracted from it.
    fn absorb_line(
        &self,
        line: &str,
        events: &mpsc::UnboundedSender<AgentEvent>,
        state: &mut TurnCollect,
    ) {
        if line.is_empty() {
            return;
        }
        let Ok(raw) = serde_json::from_str::<Value>(line) else {
            return;
        };
        let emit_raw = self
            .max_output_bytes
            .is_none_or(|limit| line.len() <= limit.saturating_sub(state.raw_bytes));
        if self.observed.get().is_none() {
            if let Some(path) = self.lane.session_id_path.as_deref() {
                if let Some(id) = extract_path(&raw, path)
                    .and_then(Value::as_str)
                    .filter(|id| !id.is_empty())
                {
                    let _ = self.observed.set(id.to_owned());
                }
            }
        }
        if let Some(path) = self.lane.usage_path.as_deref() {
            if let Some(found) = extract_path(&raw, path).and_then(token_usage) {
                state.usage = Some(found.clone());
                if emit_raw {
                    let _ = events.send(AgentEvent::Usage {
                        usage: found,
                        raw: None,
                    });
                }
            }
        }
        if let (Some(selector), Some(path)) = (
            self.lane.result_selector.as_deref(),
            self.lane.result_path.as_deref(),
        ) {
            let selector_field = self.lane.result_selector_field.as_deref().unwrap_or("type");
            if raw.get(selector_field).and_then(Value::as_str) == Some(selector) {
                if let Some(text) = extract_path(&raw, path).and_then(Value::as_str) {
                    let retained = bounds::bounded_prefix(text, self.max_output_bytes);
                    state.result_text = Some(retained.to_owned());
                    if retained.len() < text.len() {
                        signal_truncated(state, events);
                    }
                }
            }
        }
        if emit_raw {
            state.raw_bytes = state.raw_bytes.saturating_add(line.len());
            let _ = events.send(AgentEvent::Raw { raw });
        } else {
            signal_truncated(state, events);
        }
    }

    /// Retain one UTF-8 prefix under the shared byte budget. Plain streams
    /// mirror it as a message chunk; structured streams emit Raw separately.
    /// Both paths signal truncation exactly once.
    fn retain_text(
        &self,
        text: &str,
        state: &mut TurnCollect,
        events: &mpsc::UnboundedSender<AgentEvent>,
    ) {
        if state.output_truncated {
            return;
        }
        let remaining = self
            .max_output_bytes
            .map(|limit| limit.saturating_sub(state.output.len()));
        let retained = bounds::bounded_prefix(text, remaining);
        state.output.push_str(retained);
        if !self.lane.json_lines && !retained.is_empty() {
            let _ = events.send(AgentEvent::MessageChunk {
                text: retained.to_owned(),
            });
        }
        if retained.len() < text.len() {
            state.output_truncated = true;
            signal_truncated(state, events);
        }
    }
}

/// Everything one CLI turn accumulates while it collects output.
#[derive(Default)]
struct TurnCollect {
    /// Cap-retained stream text — the outcome's fallback when no
    /// result record supplied authoritative text.
    output: String,
    output_truncated: bool,
    truncated: bool,
    /// NDJSON carry for `jsonLines`: a line split across reads waits for
    /// its newline; an unterminated tail at EOF is still one record.
    lines: String,
    discarding_line: bool,
    raw_bytes: usize,
    usage: Option<TokenUsage>,
    /// A `result_selector`/`result_path` record's authoritative text.
    result_text: Option<String>,
}

fn signal_truncated(state: &mut TurnCollect, events: &mpsc::UnboundedSender<AgentEvent>) {
    if !state.truncated {
        state.truncated = true;
        let _ = events.send(AgentEvent::Truncated);
    }
}

fn cancelled_outcome(state: TurnCollect) -> TurnOutcome {
    TurnOutcome {
        stop_reason: "cancelled".to_owned(),
        text: state.result_text.unwrap_or(state.output),
        truncated: state.truncated,
        exit_code: None,
        usage: state.usage,
    }
}

/// Uniform chunk source over a child stdout pipe or a pty master.
trait ChunkRead: Send + Unpin {
    fn read<'a>(
        &'a mut self,
        buffer: &'a mut [u8],
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::io::Result<usize>> + Send + 'a>>;
}

impl ChunkRead for tokio::process::ChildStdout {
    fn read<'a>(
        &'a mut self,
        buffer: &'a mut [u8],
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::io::Result<usize>> + Send + 'a>>
    {
        Box::pin(AsyncReadExt::read(self, buffer))
    }
}

#[cfg(unix)]
impl ChunkRead for crate::transport::pty::PtyMaster {
    fn read<'a>(
        &'a mut self,
        buffer: &'a mut [u8],
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::io::Result<usize>> + Send + 'a>>
    {
        Box::pin(async move {
            let chunk = self.read().await?;
            let count = chunk.len().min(buffer.len());
            buffer[..count].copy_from_slice(&chunk[..count]);
            Ok(count)
        })
    }
}

fn effective_stream(requested: StreamMode) -> StreamMode {
    match requested {
        StreamMode::Pty if cfg!(unix) => StreamMode::Pty,
        _ => StreamMode::Stdio,
    }
}

/// Substitute `{prompt}`/`{model}`/`{effort}`/`{instructions}`/
/// `{permission}`/`{sessionId}`/`{cwd}`/`{provider}` placeholders from the
/// shared variable map. An argument that is exactly an absent optional
/// selector is dropped, together with the value flag immediately
/// preceding it when that flag exists only to carry the value (`--model
/// "{model}"` with no model must not send an empty string). An argument
/// whose trailing `={placeholder}` or leading `--{placeholder}` resolves
/// empty is dropped too — a dangling `--model=` or bare `--` is not a
/// valid argv token. Returns whether the prompt still needs a stdin write
/// (no placeholder consumed it).
fn substitute_args(args: &[String], vars: &Vars) -> (Vec<String>, bool) {
    let mut used_prompt = false;
    let mut resolved = Vec::with_capacity(args.len());
    let mut previous_literal_flag = false;
    for arg in args {
        if arg.contains("{prompt}") {
            used_prompt = true;
        }
        // Empty-valued placeholders that form a flag tail (`--k={x}`) or
        // a flag body (`--{x}`) leave a malformed token: drop the arg.
        let dangling = vars
            .absent_names()
            .any(|key| arg.ends_with(&format!("={{{key}}}")) || *arg == format!("--{{{key}}}"));
        let text = substitute_text(arg, vars);
        if vars.is_absent_option(arg) {
            if previous_literal_flag {
                resolved.pop();
            }
            previous_literal_flag = false;
            continue;
        }
        if dangling {
            previous_literal_flag = false;
            continue;
        }
        previous_literal_flag = arg.starts_with('-') && !arg.contains('{');
        resolved.push(text);
    }
    (resolved, !used_prompt)
}

struct ReceiptMetadata {
    session_id: String,
    usage: Option<TokenUsage>,
}

fn receipt_error(code: &'static str) -> AdapterError {
    let mut error = AdapterError::uncertain(code, "cli/receipt");
    error.retryable = false;
    error
}

/// A model id carrying effort as a `-<level>` suffix gets the requested
/// level rewritten into that segment (`cursor-grok-4.6-high` + `low` →
/// `cursor-grok-4.6-low`); a trailing `-fast` marker is preserved
/// (`x-high-fast` → `x-low-fast`). Models with no known tier segment
/// gain the level as a new suffix (`gpt-5.2` + `high` → `gpt-5.2-high`).
fn effort_suffixed_model(model: &str, effort: &str, tiers: &[String]) -> String {
    let (stem, fast) = match model.strip_suffix("-fast") {
        Some(stem) => (stem, "-fast"),
        None => (model, ""),
    };
    // Longest declared tier wins: `extra-high` must beat `high` so
    // `gpt-5.5-extra-high` rewrites to `gpt-5.5-low`, never `gpt-5.5-extra-low`.
    let mut matched = 0;
    for tier in tiers {
        if tier.len() > matched
            && stem.len() > tier.len()
            && stem.ends_with(tier.as_str())
            && stem.as_bytes()[stem.len() - tier.len() - 1] == b'-'
        {
            matched = tier.len();
        }
    }
    let strip = if matched == 0 { 0 } else { matched + 1 };
    let family = &stem[..stem.len() - strip];
    format!("{family}-{effort}{fast}")
}

/// Missing optional receipts are allowed. Existing but unreadable, oversized,
/// malformed, or identity-free receipts are failures and remain on disk.
fn read_receipt(
    path: &std::path::Path,
    shape: BindingReceiptShape,
    limit: usize,
) -> Result<Option<ReceiptMetadata>, AdapterError> {
    match std::fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_file() => {}
        Ok(_) => return Err(receipt_error("agent_receipt_read_failed")),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(_) => return Err(receipt_error("agent_receipt_read_failed")),
    }
    let mut options = std::fs::OpenOptions::new();
    options.read(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);
    }
    let file = match options.open(path) {
        Ok(file) => file,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(_) => return Err(receipt_error("agent_receipt_read_failed")),
    };
    if !file
        .metadata()
        .map_err(|_| receipt_error("agent_receipt_read_failed"))?
        .is_file()
    {
        return Err(receipt_error("agent_receipt_read_failed"));
    }
    let mut reader = std::io::Read::take(file, limit.saturating_add(1) as u64);
    let mut bytes = Vec::new();
    std::io::Read::read_to_end(&mut reader, &mut bytes)
        .map_err(|_| receipt_error("agent_receipt_read_failed"))?;
    if bytes.len() > limit {
        return Err(receipt_error("agent_receipt_too_large"));
    }
    let payload: Value =
        serde_json::from_slice(&bytes).map_err(|_| receipt_error("agent_receipt_invalid"))?;
    if !payload.is_object() {
        return Err(receipt_error("agent_receipt_invalid"));
    }
    let nested = payload
        .get("hookPayload")
        .map(|value| {
            let text = value
                .as_str()
                .ok_or_else(|| receipt_error("agent_receipt_invalid"))?;
            let value: Value =
                serde_json::from_str(text).map_err(|_| receipt_error("agent_receipt_invalid"))?;
            if !value.is_object() {
                return Err(receipt_error("agent_receipt_invalid"));
            }
            Ok(value)
        })
        .transpose()?;
    let sources = [Some(&payload), nested.as_ref()];
    let session_id = sources
        .iter()
        .flatten()
        .find_map(|value| {
            [
                "conversationId",
                "conversation_id",
                "sessionId",
                "session_id",
            ]
            .iter()
            .find_map(|key| {
                value
                    .get(*key)?
                    .as_str()
                    .filter(|id| !id.is_empty())
                    .map(str::to_owned)
            })
        })
        .ok_or_else(|| receipt_error("agent_receipt_session_id_missing"))?;
    let usage = if shape == BindingReceiptShape::SessionIdAndUsage {
        sources.iter().flatten().find_map(|value| {
            ["usage", "tokenUsage", "token_usage"]
                .iter()
                .find_map(|key| value.get(*key))
                .and_then(token_usage)
        })
    } else {
        None
    };
    Ok(Some(ReceiptMetadata { session_id, usage }))
}

#[cfg(all(test, unix))]
mod receipt_file_type_tests {
    use super::*;
    use std::os::unix::ffi::OsStrExt;

    #[test]
    fn receipts_reject_links_and_pipes_without_waiting_for_a_writer() {
        let directory = tempfile::tempdir().unwrap();
        let valid = directory.path().join("valid.json");
        std::fs::write(&valid, r#"{"sessionId":"fixture"}"#).unwrap();
        assert!(read_receipt(&valid, BindingReceiptShape::SessionId, 1024)
            .unwrap()
            .is_some());
        let link = directory.path().join("link");
        std::os::unix::fs::symlink(&valid, &link).unwrap();
        assert!(read_receipt(&link, BindingReceiptShape::SessionId, 1024).is_err());
        let pipe = directory.path().join("pipe");
        let name = std::ffi::CString::new(pipe.as_os_str().as_bytes()).unwrap();
        assert_eq!(unsafe { libc::mkfifo(name.as_ptr(), 0o600) }, 0);
        let (send, receive) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let result = read_receipt(&pipe, BindingReceiptShape::SessionId, 1024);
            send.send(result.is_err()).unwrap();
        });
        assert!(receive
            .recv_timeout(crate::test_settings::WAIT_BUDGET)
            .expect("receipt read blocked on a special file"));
    }
}

#[cfg(test)]
mod effort_suffix_tests {
    use super::effort_suffixed_model;

    fn tiers() -> Vec<String> {
        [
            "minimal",
            "none",
            "low",
            "medium",
            "high",
            "extra-high",
            "xhigh",
            "max",
        ]
        .iter()
        .map(|level| (*level).to_owned())
        .collect()
    }

    #[test]
    fn rewrites_the_tier_segment() {
        let tiers = tiers();
        assert_eq!(
            effort_suffixed_model("cursor-grok-4.6-high", "low", &tiers),
            "cursor-grok-4.6-low"
        );
        assert_eq!(
            effort_suffixed_model("claude-opus-5-thinking-high", "medium", &tiers),
            "claude-opus-5-thinking-medium"
        );
        assert_eq!(
            effort_suffixed_model("claude-4.6-opus-high", "max", &tiers),
            "claude-4.6-opus-max"
        );
    }

    #[test]
    fn longest_tier_wins_over_nested_candidates() {
        let tiers = tiers();
        // `extra-high` must beat `high`: otherwise `gpt-5.5-extra-high`
        // would rewrite to the nonexistent `gpt-5.5-extra-low`.
        assert_eq!(
            effort_suffixed_model("gpt-5.5-extra-high", "low", &tiers),
            "gpt-5.5-low"
        );
    }

    #[test]
    fn fast_marker_survives_the_rewrite() {
        let tiers = tiers();
        assert_eq!(
            effort_suffixed_model("cursor-grok-4.6-high-fast", "xhigh", &tiers),
            "cursor-grok-4.6-xhigh-fast"
        );
        assert_eq!(
            effort_suffixed_model("composer-2.5-fast", "high", &tiers),
            "composer-2.5-high-fast"
        );
    }

    #[test]
    fn models_without_a_tier_gain_the_suffix() {
        let tiers = tiers();
        assert_eq!(
            effort_suffixed_model("gpt-5.2", "high", &tiers),
            "gpt-5.2-high"
        );
        // A hyphenated name whose last segment is not a tier is appended,
        // never corrupted mid-name.
        assert_eq!(
            effort_suffixed_model("claude-4.5-sonnet-thinking", "high", &tiers),
            "claude-4.5-sonnet-thinking-high"
        );
    }
}

#[cfg(test)]
mod output_prefix_tests {
    use super::*;

    #[test]
    fn later_ascii_cannot_fill_a_cut_multibyte_prefix() {
        let lane = CliLane {
            command: CommandSpec {
                program: "fixture".into(),
                args: vec![],
                cwd: None,
                env: Default::default(),
            },
            lane: CliLaneSpec::default(),
            secrets: std::sync::Arc::new(crate::env::NoSecrets),
            observer: None,
            max_output_bytes: Some(4),
            cancelled: std::sync::Arc::new(watch::channel(()).0),
            observed: std::sync::Arc::new(std::sync::OnceLock::new()),
            cleanup_failure: std::sync::Mutex::new(None),
        };
        let (send, mut receive) = mpsc::unbounded_channel();
        let mut state = TurnCollect::default();
        lane.retain_text("界界", &mut state, &send);
        lane.retain_text("a", &mut state, &send);
        assert_eq!(state.output, "界");
        assert!(state.truncated);
        assert_eq!(
            receive.try_recv().unwrap(),
            AgentEvent::MessageChunk { text: "界".into() }
        );
        assert_eq!(receive.try_recv().unwrap(), AgentEvent::Truncated);
        assert!(receive.try_recv().is_err());
    }
}