agent-file-tools 0.56.2

Agent File Tools — tree-sitter powered code analysis for AI agents
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
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
#[cfg(unix)]
use std::time::{Duration, Instant};

#[cfg(unix)]
#[path = "helpers/mod.rs"]
mod test_helpers;

use aft::compress::builtin_filters;
use aft::compress::caps::DropClass;
use aft::compress::compress_with_registry_exit_code;
use aft::compress::toml_filter::build_registry;
use aft::list_envelope::{derive_wire_key, render_trailer, ListEnvelope, Reason, Total, Unit};
use aft::list_surfaces::bash::{
    append_envelope_trailer, build_bash_output_envelope, build_envelope_from_output,
    count_output_lines, wire_key, LIST_ID, NARROW, REASON, REASON_KIND, UNIT, WIRE_KEY,
};
use aft::list_surfaces::{ReasonKind, EXCLUSIONS, LIST_SURFACES};
use aft::ndjson_text::build_ndjson_text;
use aft::protocol::Response;
use aft::subc_format::{format_response_with_context, FormatContext};
use serde_json::Value;
#[cfg(unix)]
use test_helpers::{user_config, AftProcess};

fn fixtures_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join("bash")
}

fn assert_no_bare_list_envelope_key_recursive(val: &Value) {
    match val {
        Value::Object(map) => {
            assert!(
                !map.contains_key("list_envelope"),
                "found forbidden bare key 'list_envelope' in object: {val:#?}"
            );
            for value in map.values() {
                assert_no_bare_list_envelope_key_recursive(value);
            }
        }
        Value::Array(arr) => {
            for item in arr {
                assert_no_bare_list_envelope_key_recursive(item);
            }
        }
        _ => {}
    }
}

#[test]
fn test_bash_surface_registration() {
    let surface = LIST_SURFACES
        .iter()
        .find(|s| s.command == "bash" && s.list_id == "bash.output")
        .expect("bash.output must be registered in LIST_SURFACES");

    assert_eq!(surface.command, "bash");
    assert_eq!(surface.mode, "");
    assert_eq!(surface.list_id, "bash.output");
    assert_eq!(surface.unit, Unit::Lines);
    assert_eq!(surface.narrow, &[] as &[&str]);

    assert_eq!(surface.reasons.len(), 1);
    let reason_entry = surface.reasons[0];
    assert_eq!(reason_entry.reason, Reason::Cap);
    assert_eq!(reason_entry.kind, ReasonKind::Selecting);

    // Module constants match registry entry
    assert_eq!(LIST_ID, surface.list_id);
    assert_eq!(WIRE_KEY, "bash_output_list_envelope");
    assert_eq!(UNIT, surface.unit);
    assert_eq!(REASON, reason_entry.reason);
    assert_eq!(REASON_KIND, reason_entry.kind);
    assert_eq!(NARROW, surface.narrow);

    assert_eq!(wire_key(), WIRE_KEY);
    assert_eq!(derive_wire_key(LIST_ID, true), WIRE_KEY);
}

#[test]
fn test_compressor_seam_returns_input_line_count_and_counts_only() {
    let fixture_dir = fixtures_dir().join("capped_4000_in_61_out");
    let input_path = fixture_dir.join("input.txt");
    let input_text = fs::read_to_string(&input_path).expect("read fixture input.txt");

    let total_input_lines = input_text.lines().count();
    assert_eq!(total_input_lines, 4000);

    let registry = build_registry(builtin_filters::ALL, None, None);
    let result = compress_with_registry_exit_code("du -a", &input_text, None, &registry);

    // Compressor seam returns the input line count
    assert_eq!(result.input_line_count, 4000);
    assert_eq!(result.input_line_count(), 4000);

    // Compressor returned counts only, never trailer strings
    assert_eq!(result.text.lines().count(), 60);
    assert!(
        !result.text.contains("shown "),
        "compressor output must not contain trailer prefix 'shown '"
    );
    assert!(
        !result.text.contains("(cap)"),
        "compressor output must not contain trailer reason '(cap)'"
    );
    assert!(
        !result.text.contains("narrow:"),
        "compressor output must not contain narrow clause"
    );
}

#[test]
fn test_4000_in_61_out_fixture() {
    let fixture_dir = fixtures_dir().join("capped_4000_in_61_out");
    let input_path = fixture_dir.join("input.txt");
    let reply_path = fixture_dir.join("reply.json");

    let input_text = fs::read_to_string(&input_path).expect("read fixture input.txt");
    let reply_json = fs::read_to_string(&reply_path).expect("read fixture reply.json");

    let input_line_count = input_text.lines().count();
    assert_eq!(input_line_count, 4000);

    let val: Value = serde_json::from_str(&reply_json).expect("parse reply.json");
    let resp = Response {
        id: val
            .get("id")
            .and_then(Value::as_str)
            .unwrap_or("1")
            .to_string(),
        success: val.get("success").and_then(Value::as_bool).unwrap_or(true),
        data: val.clone(),
    };

    assert!(resp.success);
    assert_no_bare_list_envelope_key_recursive(&val);

    let output_str = resp.data["output"]
        .as_str()
        .expect("output must be a string");
    let shown = count_output_lines(output_str);

    // shown = 61, total = Exact(4000)
    assert_eq!(shown, 61);
    assert_eq!(input_line_count, 4000);

    // Built from agent-facing output text
    let envelope = build_envelope_from_output(output_str, input_line_count)
        .expect("envelope must be constructed for capped output");

    assert_eq!(envelope.shown, 61);
    assert_eq!(envelope.total, Total::Exact(4000));
    assert_eq!(envelope.unit, Unit::Lines);
    assert_eq!(envelope.reason, Some(Reason::Cap));
    assert_eq!(envelope.causes, vec![Reason::Cap]);
    assert_eq!(envelope.narrow, Vec::<String>::new());

    // Rendered trailer grammar check
    let rendered_trailer = render_trailer(&envelope).expect("render trailer");
    assert_eq!(rendered_trailer, "shown 61 of 4000 lines (cap)");
    assert!(
        !rendered_trailer.contains("narrow:"),
        "bash trailer must have no narrow clause"
    );

    // Output text contains the trailer
    assert!(output_str.contains("shown 61 of 4000 lines (cap)"));
    assert_eq!(
        output_str.matches("shown 61 of 4000 lines (cap)").count(),
        1
    );

    // Serialized at reply root beside output
    let env_val = resp
        .data
        .get("bash_output_list_envelope")
        .expect("bash_output_list_envelope must be at reply root");
    let deserialized_env: ListEnvelope =
        serde_json::from_value(env_val.clone()).expect("deserialize ListEnvelope");
    assert_eq!(deserialized_env, envelope);
}

#[test]
fn test_mutation_sourcing_either_count_from_dropped_by_class_reds() {
    let fixture_dir = fixtures_dir().join("capped_4000_in_61_out");
    let input_path = fixture_dir.join("input.txt");
    let input_text = fs::read_to_string(&input_path).expect("read fixture input.txt");

    let registry = build_registry(builtin_filters::ALL, None, None);
    let result = compress_with_registry_exit_code("du -a", &input_text, None, &registry);

    // dropped_by_class on du is empty (plain line cap does not classify blocks)
    let dropped_by_class_sum: usize = result.dropped_by_class.values().sum();
    assert_eq!(dropped_by_class_sum, 0);

    // Expected values on fixture:
    let expected_shown = 61;
    let expected_total = 4000;

    // Mutation 1: Sourcing shown from dropped_by_class produces 0 != 61
    let mutated_shown = dropped_by_class_sum;
    assert_ne!(
        mutated_shown, expected_shown,
        "mutation sourcing shown from dropped_by_class must red"
    );

    // Mutation 2: Sourcing total from shown + dropped_by_class produces 61 != 4000
    let mutated_total = expected_shown + dropped_by_class_sum;
    assert_ne!(
        mutated_total, expected_total,
        "mutation sourcing total from dropped_by_class must red"
    );

    // Mutation 3: Sourcing from non-empty dropped_by_class (synthetic drop blocks)
    let mut synthetic_drops: BTreeMap<DropClass, usize> = BTreeMap::new();
    synthetic_drops.insert(DropClass::Error, 25);
    synthetic_drops.insert(DropClass::Warning, 10);
    let synthetic_sum: usize = synthetic_drops.values().sum(); // 35
    assert_ne!(
        synthetic_sum, expected_shown,
        "mutation sourcing shown from block drops must red"
    );
    assert_ne!(
        expected_shown + synthetic_sum,
        expected_total,
        "mutation sourcing total from block drops must red"
    );
}

#[test]
fn test_subc_and_ndjson_transport_parity_on_fixture() {
    let fixture_dir = fixtures_dir().join("capped_4000_in_61_out");
    let reply_path = fixture_dir.join("reply.json");
    let reply_json = fs::read_to_string(&reply_path).expect("read fixture reply.json");

    let val: Value = serde_json::from_str(&reply_json).expect("parse reply.json");
    let resp = Response {
        id: val
            .get("id")
            .and_then(Value::as_str)
            .unwrap_or("1")
            .to_string(),
        success: val.get("success").and_then(Value::as_bool).unwrap_or(true),
        data: val.clone(),
    };

    let ctx = FormatContext::default();
    let subc_formatted = format_response_with_context("bash", &resp, &ctx);
    let output_raw = resp.data["output"].as_str().unwrap();

    let ndjson_formatted = build_ndjson_text(output_raw, &resp.data, Some("bash.output"), true);

    // Subc formatter passes bash text through unchanged
    assert_eq!(subc_formatted, output_raw);

    // Transport parity across NDJSON and subc: byte-identical
    assert_eq!(subc_formatted, ndjson_formatted);

    // Trailer is never rendered twice
    assert_eq!(
        subc_formatted
            .matches("shown 61 of 4000 lines (cap)")
            .count(),
        1
    );
    assert_eq!(
        ndjson_formatted
            .matches("shown 61 of 4000 lines (cap)")
            .count(),
        1
    );
}

#[test]
fn test_uncompressed_bash_output_stays_byte_identical_to_prespec_golden() {
    let fixture_dir = fixtures_dir().join("uncompressed");
    let input_path = fixture_dir.join("input.txt");
    let reply_path = fixture_dir.join("reply.json");

    let input_text = fs::read_to_string(&input_path).expect("read fixture input.txt");
    let reply_json = fs::read_to_string(&reply_path).expect("read fixture reply.json");

    let input_line_count = input_text.lines().count();
    assert_eq!(input_line_count, 5);

    let val: Value = serde_json::from_str(&reply_json).expect("parse reply.json");
    let resp = Response {
        id: val
            .get("id")
            .and_then(Value::as_str)
            .unwrap_or("1")
            .to_string(),
        success: val.get("success").and_then(Value::as_bool).unwrap_or(true),
        data: val.clone(),
    };

    assert!(resp.success);
    assert_no_bare_list_envelope_key_recursive(&val);

    let output_str = resp.data["output"].as_str().unwrap();
    let shown = count_output_lines(output_str);
    assert_eq!(shown, 5);

    // When shown >= input_line_count, no envelope is built (reason is None)
    let envelope = build_bash_output_envelope(shown, input_line_count);
    assert_eq!(envelope, None);

    let envelope_from_output = build_envelope_from_output(output_str, input_line_count);
    assert_eq!(envelope_from_output, None);

    // No envelope key serialized in response data
    assert!(
        resp.data.get("bash_output_list_envelope").is_none(),
        "uncompressed bash output must not serialize bash_output_list_envelope"
    );
    assert!(resp.data.get("list_envelope").is_none());

    // Output text contains no trailer string
    assert!(
        !output_str.contains("shown "),
        "uncompressed output must not contain trailer"
    );
    assert!(!output_str.contains("(cap)"));

    // Subc and NDJSON formatting match pre-spec golden byte-identically
    let ctx = FormatContext::default();
    let subc_formatted = format_response_with_context("bash", &resp, &ctx);
    let ndjson_formatted = build_ndjson_text(output_str, &resp.data, Some("bash.output"), true);

    assert_eq!(subc_formatted, output_str);
    assert_eq!(ndjson_formatted, output_str);
    assert_eq!(subc_formatted, input_text);
}

#[test]
fn test_bash_live_tail_raw_and_exclusions_entry() {
    let exclusion = EXCLUSIONS
        .iter()
        .find(|e| e.file == "commands/bash_status.rs")
        .expect("commands/bash_status.rs must be in EXCLUSIONS");

    assert_eq!(exclusion.enclosing_item, "handle");
    assert_eq!(
        exclusion.location_or_primitive,
        "bash_status / bash live-tail"
    );
    assert!(
        !exclusion.reason.trim().is_empty(),
        "bash live-tail exclusion must have a non-empty reason"
    );
    assert!(
        exclusion.reason.contains("bash live-tail"),
        "exclusion reason must mention bash live-tail"
    );
    assert!(
        exclusion.reason.contains("raw by design"),
        "exclusion reason must document raw by design"
    );
    assert!(
        exclusion.reason.contains("no truncation envelope"),
        "exclusion reason must document no truncation envelope"
    );
}

#[test]
fn test_envelope_wire_schema_and_grammar() {
    let env = ListEnvelope::new(61, Total::Exact(4000), Unit::Lines, vec![Reason::Cap], &[]);

    let json_val = serde_json::to_value(&env).expect("serialize ListEnvelope");

    // Exact wire shape per R15
    assert_eq!(json_val["shown"], 61);
    assert_eq!(json_val["total"]["kind"], "exact");
    assert_eq!(json_val["total"]["value"], 4000);
    assert_eq!(json_val["unit"], "lines");
    assert_eq!(json_val["reason"], "cap");
    assert_eq!(json_val["causes"], serde_json::json!(["cap"]));
    assert_eq!(json_val["narrow"], serde_json::json!([]));

    // causes is ordered by precedence descending and reason == causes[0]
    assert_eq!(
        json_val["reason"].as_str().unwrap(),
        json_val["causes"][0].as_str().unwrap()
    );

    // narrow is [] and not omitted
    assert!(json_val.get("narrow").is_some());
    assert!(json_val["narrow"].as_array().unwrap().is_empty());

    // Grammar check
    let trailer = render_trailer(&env).expect("render trailer");
    assert_eq!(trailer, "shown 61 of 4000 lines (cap)");
    assert!(!trailer.contains("narrow:"));
    assert!(!trailer.ends_with('.'));
    assert!(!trailer.ends_with(';'));
}

#[test]
fn test_append_envelope_trailer_counts_the_agent_facing_trailer_line() {
    let mut output = "first\nsecond\n".to_string();
    let envelope = append_envelope_trailer(&mut output, 10).expect("lines were dropped");

    assert_eq!(envelope.shown, 3);
    assert_eq!(envelope.total, Total::Exact(10));
    assert_eq!(output.lines().count(), envelope.shown);
    assert!(output.ends_with("shown 3 of 10 lines (cap)"));

    let mut unchanged = "first\nsecond\n".to_string();
    assert_eq!(append_envelope_trailer(&mut unchanged, 2), None);
    assert_eq!(unchanged, "first\nsecond\n");
}

#[cfg(unix)]
fn configure_real_bash(aft: &mut AftProcess, project: &Path) {
    let response = aft.send(
        &serde_json::json!({
            "id": "configure-real-bash-envelope",
            "command": "configure",
            "harness": "opencode",
            "project_root": project,
            "storage_dir": project.join("storage"),
            "config": user_config(serde_json::json!({
                "bash": {
                    "background": true,
                    "compress": true,
                    "rewrite": false,
                    "foreground_wait_window_ms": 30_000
                }
            })),
        })
        .to_string(),
    );
    assert_eq!(response["success"], true, "configure failed: {response:?}");
}

#[cfg(unix)]
fn shell_quote(path: &Path) -> String {
    format!("'{}'", path.display().to_string().replace('\'', "'\\''"))
}

#[cfg(unix)]
fn real_capped_command() -> String {
    let input = fixtures_dir().join("capped_4000_in_61_out/input.txt");
    format!("cat {}", shell_quote(&input))
}

#[cfg(unix)]
fn wait_for_real_completion(aft: &mut AftProcess, task_id: &str) -> Value {
    let deadline = Instant::now() + Duration::from_secs(30);
    loop {
        assert!(
            Instant::now() < deadline,
            "timed out waiting for completion frame for {task_id}"
        );
        let Some(frame) = aft.try_read_next_timeout(Duration::from_millis(250)) else {
            continue;
        };
        if frame["type"] == "bash_completed" && frame["task_id"] == task_id {
            return frame;
        }
    }
}

#[cfg(unix)]
fn assert_real_bash_envelope(value: &Value, output_key: &str, total: usize) {
    let output = value[output_key]
        .as_str()
        .unwrap_or_else(|| panic!("missing {output_key} in {value:?}"));
    let envelope: ListEnvelope = serde_json::from_value(
        value[WIRE_KEY]
            .as_object()
            .unwrap_or_else(|| panic!("missing {WIRE_KEY} in {value:?}"))
            .clone()
            .into(),
    )
    .expect("deserialize bash output envelope");
    assert_eq!(envelope.shown, output.lines().count());
    assert_eq!(envelope.total, Total::Exact(total));
    let trailer = render_trailer(&envelope).expect("cap envelope trailer");
    assert!(
        output.ends_with(&trailer),
        "agent-facing output must end with its trailer: {output:?}"
    );
    assert_eq!(
        output.matches(&trailer).count(),
        1,
        "agent-facing output must render its trailer once"
    );
}

#[cfg(unix)]
#[test]
fn real_background_completion_and_status_attach_compressor_counts() {
    let mut aft = AftProcess::spawn();
    let project = tempfile::tempdir().expect("project tempdir");
    configure_real_bash(&mut aft, project.path());

    let launch = aft.send(
        &serde_json::json!({
            "id": "real-background-capped",
            "command": "bash",
            "params": {
                "command": real_capped_command(),
                "background": true
            }
        })
        .to_string(),
    );
    assert_eq!(
        launch["success"], true,
        "background launch failed: {launch:?}"
    );
    let task_id = launch["task_id"].as_str().expect("task id");
    let completion = wait_for_real_completion(&mut aft, task_id);
    assert_real_bash_envelope(&completion, "output_preview", 4000);

    let snapshot = aft.send(
        &serde_json::json!({
            "id": "real-background-status",
            "command": "bash_status",
            "params": { "task_id": task_id }
        })
        .to_string(),
    );
    assert_eq!(snapshot["success"], true, "status failed: {snapshot:?}");
    assert_real_bash_envelope(&snapshot, "output_preview", 4000);

    let drained = aft.send(
        &serde_json::json!({
            "id": "real-background-drain",
            "command": "bash_drain_completions"
        })
        .to_string(),
    );
    let drained_completion = drained["bg_completions"]
        .as_array()
        .expect("drained completions")
        .iter()
        .find(|completion| completion["task_id"] == task_id)
        .unwrap_or_else(|| panic!("missing drained completion for {task_id}: {drained:?}"));
    assert_real_bash_envelope(drained_completion, "output_preview", 4000);

    let short_launch = aft.send(
        &serde_json::json!({
            "id": "real-background-short",
            "command": "bash",
            "params": {
                "command": "printf 'alpha\\nbeta\\n'",
                "background": true
            }
        })
        .to_string(),
    );
    assert_eq!(
        short_launch["success"], true,
        "short launch failed: {short_launch:?}"
    );
    let short_task_id = short_launch["task_id"].as_str().expect("short task id");
    let short_completion = wait_for_real_completion(&mut aft, short_task_id);
    assert_eq!(short_completion["output_preview"], "alpha\nbeta\n");
    assert!(short_completion.get(WIRE_KEY).is_none());
    assert!(!short_completion["output_preview"]
        .as_str()
        .expect("short completion output")
        .contains("shown "));

    let short_snapshot = aft.send(
        &serde_json::json!({
            "id": "real-background-short-status",
            "command": "bash_status",
            "params": { "task_id": short_task_id }
        })
        .to_string(),
    );
    assert_eq!(short_snapshot["output_preview"], "alpha\nbeta\n");
    assert!(short_snapshot.get(WIRE_KEY).is_none());

    assert!(aft.shutdown().success());
}

#[cfg(unix)]
#[test]
fn real_foreground_transports_attach_compressor_counts() {
    let mut aft = AftProcess::spawn();
    let project = tempfile::tempdir().expect("project tempdir");
    configure_real_bash(&mut aft, project.path());

    let capped = aft.send(
        &serde_json::json!({
            "id": "real-foreground-capped",
            "command": "bash",
            "params": {
                "command": real_capped_command(),
                "foreground_orchestrate": true,
                "block_to_completion": true
            }
        })
        .to_string(),
    );
    assert_eq!(
        capped["success"], true,
        "foreground command failed: {capped:?}"
    );
    assert_real_bash_envelope(&capped, "output", 4000);

    let response = Response {
        id: capped["id"].as_str().expect("response id").to_string(),
        success: true,
        data: capped.clone(),
    };
    let output = capped["output"].as_str().expect("foreground output");
    let subc_text = format_response_with_context("bash", &response, &FormatContext::default());
    let ndjson_text = build_ndjson_text(output, &capped, Some(LIST_ID), true);
    assert_eq!(subc_text, output);
    assert_eq!(ndjson_text, output);
    let trailer: ListEnvelope =
        serde_json::from_value(capped[WIRE_KEY].clone()).expect("foreground envelope");
    let trailer = render_trailer(&trailer).expect("foreground trailer");
    assert_eq!(subc_text.matches(&trailer).count(), 1);
    assert_eq!(ndjson_text.matches(&trailer).count(), 1);

    let short = aft.send(
        &serde_json::json!({
            "id": "real-foreground-short",
            "command": "bash",
            "params": {
                "command": "printf 'alpha\\nbeta\\n'",
                "foreground_orchestrate": true,
                "block_to_completion": true
            }
        })
        .to_string(),
    );
    assert_eq!(short["success"], true, "short foreground failed: {short:?}");
    assert_eq!(short["output"], "alpha\nbeta\n");
    assert!(short.get(WIRE_KEY).is_none());
    assert!(!short["output"]
        .as_str()
        .expect("short output")
        .contains("shown "));

    let short_response = Response {
        id: short["id"].as_str().expect("short response id").to_string(),
        success: true,
        data: short.clone(),
    };
    let short_subc =
        format_response_with_context("bash", &short_response, &FormatContext::default());
    let short_ndjson = build_ndjson_text(
        short["output"].as_str().expect("short output"),
        &short,
        Some(LIST_ID),
        true,
    );
    assert_eq!(short_subc, "alpha\nbeta\n");
    assert_eq!(short_ndjson, "alpha\nbeta\n");

    assert!(aft.shutdown().success());
}

#[cfg(unix)]
#[test]
fn real_live_status_stays_raw_without_an_envelope() {
    let mut aft = AftProcess::spawn();
    let project = tempfile::tempdir().expect("project tempdir");
    configure_real_bash(&mut aft, project.path());
    let input = fixtures_dir().join("capped_4000_in_61_out/input.txt");
    let command = format!("cat {}; sleep 10", shell_quote(&input));

    let launch = aft.send(
        &serde_json::json!({
            "id": "real-live-raw",
            "command": "bash",
            "params": { "command": command, "background": true }
        })
        .to_string(),
    );
    assert_eq!(launch["success"], true, "live launch failed: {launch:?}");
    let task_id = launch["task_id"].as_str().expect("live task id");

    let deadline = Instant::now() + Duration::from_secs(10);
    let live = loop {
        let snapshot = aft.send(
            &serde_json::json!({
                "id": "real-live-status",
                "command": "bash_status",
                "params": { "task_id": task_id }
            })
            .to_string(),
        );
        if snapshot["status"] == "running"
            && snapshot["output_preview"]
                .as_str()
                .is_some_and(|output| output.contains("file_4000"))
        {
            break snapshot;
        }
        assert!(
            Instant::now() < deadline,
            "timed out waiting for raw live tail: {snapshot:?}"
        );
        std::thread::sleep(Duration::from_millis(25));
    };

    let output = live["output_preview"].as_str().expect("live output");
    assert!(output.contains("file_4000"));
    assert!(!output.contains("shown "));
    assert!(live.get(WIRE_KEY).is_none());

    let killed = aft.send(
        &serde_json::json!({
            "id": "real-live-kill",
            "command": "bash_kill",
            "params": { "task_id": task_id }
        })
        .to_string(),
    );
    assert_eq!(killed["success"], true, "kill failed: {killed:?}");
    assert!(aft.shutdown().success());
}