dirge-agent 0.19.20

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
//! Headless run path for [`AnyAgent`]. Split out of `provider/mod.rs`
//! (dirge-4y4l stage 8): the `--print` / `--loop` entry point that drives
//! the agent loop and collects output for the non-interactive CLI modes.
//!
//! Child module of `provider`, so it reaches `AnyAgent`'s private fields and
//! `spawn_runner` directly (privacy = defining module + descendants).

use super::AnyAgent;
use crate::agent::runner;
use crate::event::AgentEvent;
#[allow(unused_imports)]
use crate::sync_util::LockExt;

/// How the headless event stream ended (dirge-18v2). The JSON result
/// envelope must reflect this — a run that was truncated by the turn
/// cap or whose runner died without a `Done` is NOT a success, and
/// `--print` consumers parse the envelope, not stderr.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RunEnd {
    /// `Done` arrived and no truncation notice was seen.
    Completed,
    /// `Done` arrived but the max-agent-turns cap stopped the run.
    Truncated,
    /// The event channel closed without a `Done` — the runner died
    /// (panic/abort) and `full_response` is whatever streamed first.
    Incomplete,
    /// A provider usage cap (dirge-u6zc) stopped the run: the quota won't
    /// reset within a retryable window (e.g. Zhipu/GLM's 5-hour limit).
    /// Distinct from `Incomplete` so a consumer can tell "pause and resume
    /// after the quota resets" from a genuine runner failure.
    UsageCapped,
}

/// Build the machine-readable result envelope for the headless modes.
/// Pure so the success/error mapping is unit-testable without a live
/// runner.
pub(crate) fn headless_result_json(
    end: RunEnd,
    duration_ms: u64,
    num_turns: u32,
    result: &str,
    session_id: &str,
    files_changed: &[String],
) -> serde_json::Value {
    let (subtype, is_error) = match end {
        RunEnd::Completed => ("success", false),
        // Matches the Claude Code stream-json convention dirge mimics.
        RunEnd::Truncated => ("error_max_turns", true),
        RunEnd::Incomplete => ("error", true),
        // dirge-u6zc: a quota pause, not a task failure — a resuming
        // wrapper keys on this subtype to re-run after the reset.
        RunEnd::UsageCapped => ("error_usage_cap", true),
    };
    serde_json::json!({
        "type": "result",
        "subtype": subtype,
        "is_error": is_error,
        "duration_ms": duration_ms,
        "num_turns": num_turns,
        "result": result,
        "session_id": session_id,
        // Files the run's edit/write/patch/bash tools touched, from the
        // modified-files tracker. Reported here (not inferred from git by
        // the consumer) so `files_changed` is accurate even when the
        // project isn't a git repo, git isn't on PATH, or the edits were
        // committed mid-run — see dirge MCP `delegate` (issue #704).
        "files_changed": files_changed,
        "total_cost_usd": 0.0,
    })
}

/// Files the run modified — snapshot the process-global modified-files
/// tracker (write/edit/edit_lines/edit_minified/apply_patch/bash all mark
/// it) and project each to a `cwd`-relative string when it lives under the
/// working dir, else its absolute path. Sorted + deduped. Empty when
/// nothing was touched. The tracker is authoritative and git-independent,
/// which is the point: a git-status diff silently reports nothing when the
/// project isn't a repo (issue #704).
fn headless_files_changed() -> Vec<String> {
    // Canonicalize cwd to match the tracker, which stores canonical paths;
    // a raw cwd wouldn't strip on symlinked dirs (e.g. macOS /tmp).
    let cwd = std::env::current_dir()
        .ok()
        .map(|c| crate::permission::path::canonical_or_self(&c));
    let mut v: Vec<String> = crate::agent::tools::modified::recent(usize::MAX)
        .into_iter()
        .map(|p| {
            cwd.as_ref()
                .and_then(|c| p.strip_prefix(c).ok())
                .unwrap_or(p.as_path())
                .to_string_lossy()
                .into_owned()
        })
        .collect();
    v.sort();
    v.dedup();
    v
}

/// Build a stream-json `assistant` event for one turn (dirge-kuqp).
/// The text block carries the turn's streamed text (omitted when
/// empty so a tool-only turn doesn't emit a stray empty block),
/// followed by one `tool_use` block per call. Shape mirrors Claude
/// Code so consumers parsing `claude -p --output-format stream-json`
/// work against dirge unchanged.
pub(crate) fn stream_json_assistant_event(
    text: &str,
    tool_uses: &[(String, String, serde_json::Value)],
    session_id: &str,
) -> serde_json::Value {
    let mut content: Vec<serde_json::Value> = Vec::new();
    if !text.is_empty() {
        content.push(serde_json::json!({"type": "text", "text": text}));
    }
    for (id, name, args) in tool_uses {
        content.push(serde_json::json!({
            "type": "tool_use",
            "id": id,
            "name": name,
            "input": args,
        }));
    }
    serde_json::json!({
        "type": "assistant",
        "message": {
            "role": "assistant",
            "content": content,
        },
        "session_id": session_id,
    })
}

/// Build a stream-json `user` event carrying a turn's tool results
/// (dirge-kuqp) as `tool_result` content blocks keyed by the
/// originating call id — the Claude Code shape for tool output.
pub(crate) fn stream_json_tool_result_event(
    results: &[(String, String)],
    session_id: &str,
) -> serde_json::Value {
    let content: Vec<serde_json::Value> = results
        .iter()
        .map(|(id, output)| {
            serde_json::json!({
                "type": "tool_result",
                "tool_use_id": id,
                "content": output,
            })
        })
        .collect();
    serde_json::json!({
        "type": "user",
        "message": {
            "role": "user",
            "content": content,
        },
        "session_id": session_id,
    })
}

impl AnyAgent {
    pub async fn run_print(
        &self,
        prompt: &str,
        max_turns: usize,
        output_format: crate::cli::OutputFormat,
        // Prior conversation to resume into the model's context. Empty for a
        // fresh run; for `--session <id>` the caller passes the loaded
        // session's history (via `convert_history`) so a headless run
        // continues where it left off instead of starting cold each time.
        history: Vec<rig::completion::Message>,
        // Returns the final response text, the turn's tool calls (so the
        // caller can persist a full-fidelity assistant message), and the
        // run's cumulative provider token usage (so the caller can fold it
        // into the session — headless previously dropped `AgentEvent::Usage`
        // on the floor, leaving `cumulative_input_tokens` at 0 for every
        // --print / --loop session and breaking `/cache` + A/B measurement).
    ) -> anyhow::Result<(
        String,
        Vec<crate::session::ToolCallEntry>,
        crate::agent::agent_loop::message::TokenUsage,
    )> {
        // dirge-nqr: honor the cap explicitly even if the agent was
        // built with a different one. `run_print` is the headless
        // entry point — callers explicitly pass the cap they want.
        let agent = self.clone().with_max_turns(Some(max_turns));
        let start_instant = std::time::Instant::now();
        let session_id = runner::uuid_v4_simple();
        let mut num_turns: u32 = 0;
        let suppress_inline = !matches!(output_format, crate::cli::OutputFormat::Text);

        // Plugin `on-prompt` dispatch. Headless modes (--print, --loop)
        // previously skipped this — plugins that mutate the user prompt
        // or block it never fired in CI/script contexts.
        let effective_prompt: String = {
            #[cfg(feature = "plugin")]
            {
                if let Some(pm_arc) = crate::plugin::hook::global() {
                    let mut mgr = pm_arc.lock_ignore_poison();
                    runner::resolve_prompt_with_hooks(prompt, &mut mgr)
                } else {
                    prompt.to_string()
                }
            }
            #[cfg(not(feature = "plugin"))]
            {
                prompt.to_string()
            }
        };

        // StreamJson init event — fires once at startup so downstream
        // tools can pick up cwd/session/model before any turns stream.
        if matches!(output_format, crate::cli::OutputFormat::StreamJson) {
            let cwd = std::env::current_dir()
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_default();
            runner::emit_stream_json_event(serde_json::json!({
                "type": "system",
                "subtype": "init",
                "cwd": cwd,
                "session_id": session_id,
                "tools": Vec::<String>::new(),
                "model": "",
            }));
        }

        // Wire through the new agent_loop path: clone the agent (cheap
        // — Arc internals + refcounts), spawn a runner, and drain the
        // event channel collecting text. Use the max_turns-stamped
        // `agent` from above so the cap is honored.
        let runner = agent.spawn_runner(
            crate::provider::Prompt::text(effective_prompt.clone()),
            history,
            None,
            None,
        );
        let task = runner.task;
        let mut event_rx = runner.event_rx;

        let mut full_response = String::new();
        let mut had_output = false;
        // dirge-18v2: track how the stream ends so the result envelope
        // can't claim success for a truncated or runner-died run.
        let mut completed = false;
        let mut truncated = false;
        // dirge-u6zc: a provider usage cap stopped the run (quota reset is
        // hours out — not retryable). Flagged from the Error arm so the
        // envelope reports a distinct, resumable outcome.
        let mut usage_capped = false;
        // Accumulate the turn's tool calls so the headless save is
        // full-fidelity (matching the interactive path). Without this the
        // saved assistant message carried only its final text, so a resumed
        // `--session` lost every tool call/result — and a tool-heavy final
        // turn saved an empty/partial message, reading as a cut-off end.
        // Mirrors the UI's ToolCall/ToolResult accumulation
        // (run_handlers/tool_call.rs + tool_result.rs).
        use crate::session::{ToolCallEntry, ToolCallState};
        let mut tool_calls: Vec<ToolCallEntry> = Vec::new();

        // Accumulate provider-reported token usage across every turn so the
        // caller can persist it into the session (mirrors the interactive
        // UI's `AgentEvent::Usage` handler at ui/mod.rs). The `Usage` event
        // carries only the input-side counters.
        let mut usage = crate::agent::agent_loop::message::TokenUsage::default();

        // dirge-kuqp: per-turn buffers for incremental stream-json. The
        // bridge collapses `TurnEnd` to a bare index, so we rebuild each
        // turn's assistant/user envelopes from the streamed events and
        // emit them at the turn boundary — making `stream-json` a true
        // incremental stream instead of one final assistant blob.
        let stream_json = matches!(output_format, crate::cli::OutputFormat::StreamJson);
        let mut turn_text = String::new();
        let mut turn_tool_uses: Vec<(String, String, serde_json::Value)> = Vec::new();
        let mut turn_tool_results: Vec<(String, String)> = Vec::new();

        while let Some(event) = event_rx.recv().await {
            match event {
                AgentEvent::ToolCall { id, name, args } => {
                    if stream_json {
                        turn_tool_uses.push((id.to_string(), name.to_string(), args.clone()));
                    }
                    // Start Interrupted; the matching ToolResult flips it to
                    // Completed. An unanswered call stays Interrupted, which
                    // convert_history re-emits so the model sees no orphan.
                    tool_calls.push(ToolCallEntry {
                        id: id.to_string(),
                        name: name.to_string(),
                        args,
                        state: ToolCallState::Interrupted,
                    });
                }
                AgentEvent::ToolResult { id, output, .. } => {
                    if stream_json {
                        // Key the result by its call id. Providers that
                        // don't emit ids leave it empty for every call in
                        // the turn, so there's nothing to pair against —
                        // consumers fall back to positional matching, same
                        // as the persistence path below. Results can arrive
                        // in completion order under parallel dispatch, so we
                        // deliberately don't try to reconstruct an index.
                        turn_tool_results.push((id.to_string(), output.to_string()));
                    }
                    let target = if !id.is_empty() {
                        tool_calls.iter_mut().rev().find(|e| e.id == id.as_str())
                    } else {
                        tool_calls
                            .iter_mut()
                            .rev()
                            .find(|e| matches!(e.state, ToolCallState::Interrupted))
                    };
                    if let Some(entry) = target {
                        entry.state = ToolCallState::Completed {
                            result: output.to_string(),
                        };
                    }
                }
                AgentEvent::Token(text) => {
                    full_response.push_str(&text);
                    if stream_json {
                        turn_text.push_str(&text);
                    }
                    if !suppress_inline {
                        let safe = crate::ui::ansi::strip_controls(
                            &text,
                            crate::ui::ansi::StripPolicy::KEEP_NEWLINE,
                        );
                        print!("{safe}");
                        let _ = std::io::Write::flush(&mut std::io::stdout());
                    }
                    had_output = true;
                }
                AgentEvent::Done { response, .. } => {
                    // `Done.response` is the authoritative full text.
                    full_response = response.to_string();
                    completed = true;
                    break;
                }
                AgentEvent::Error(err) => {
                    if had_output {
                        println!();
                    }
                    // dirge-u6zc: a provider usage cap isn't a failure to
                    // retry — the quota resets hours out, so retrying just
                    // re-hits it. Stop cleanly with a distinct, resumable
                    // outcome (the session is saved below) and tell the user
                    // when the quota resets, rather than dumping a raw
                    // ProviderError and exiting like a hard failure.
                    if matches!(
                        crate::agent::recovery::classify_error(&err),
                        crate::agent::recovery::ErrorKind::UsageCap
                    ) {
                        usage_capped = true;
                        let resume = format!(
                            "Session {session_id} saved; re-run (e.g. `dirge --continue`) once the quota resets to resume."
                        );
                        match crate::agent::recovery::usage_cap_reset_hint(&err) {
                            Some(reset) => eprintln!(
                                "Provider usage cap reached — quota resets at {reset}. {resume}\n  ↳ cause: {err}"
                            ),
                            None => eprintln!(
                                "Provider usage cap reached — quota won't reset within a retryable window. {resume}\n  ↳ cause: {err}"
                            ),
                        }
                        break;
                    }
                    eprintln!("Error: {}", err);
                    let _ = task.await;
                    return Err(anyhow::anyhow!("{}", err));
                }
                AgentEvent::TurnEnd { .. } => {
                    num_turns += 1;
                    // dirge-kuqp: flush this turn's assistant text +
                    // tool_use blocks, then any tool results, as soon as
                    // the turn closes. This is what makes the stream
                    // incremental for multi-turn agentic runs.
                    if stream_json {
                        if !turn_text.is_empty() || !turn_tool_uses.is_empty() {
                            runner::emit_stream_json_event(stream_json_assistant_event(
                                &turn_text,
                                &turn_tool_uses,
                                &session_id,
                            ));
                        }
                        if !turn_tool_results.is_empty() {
                            runner::emit_stream_json_event(stream_json_tool_result_event(
                                &turn_tool_results,
                                &session_id,
                            ));
                        }
                        turn_text.clear();
                        turn_tool_uses.clear();
                        turn_tool_results.clear();
                    }
                }
                AgentEvent::SystemNotice { content } => {
                    // dirge-originated runtime notice (e.g. the
                    // max-agent-turns cap). Headless drives output from
                    // events, so surface it to stderr — and mark the
                    // run truncated so the JSON envelope reflects it
                    // (dirge-18v2); stderr alone is invisible to
                    // `--print` consumers parsing stdout.
                    if content.starts_with(crate::agent::agent_loop::run::MAX_TURNS_NOTICE_PREFIX) {
                        truncated = true;
                    }
                    if had_output {
                        println!();
                    }
                    eprintln!("{}", content);
                }
                AgentEvent::Usage {
                    input_tokens,
                    cached_input_tokens,
                    cache_creation_input_tokens,
                    ..
                } => {
                    usage.input_tokens = usage.input_tokens.saturating_add(input_tokens);
                    usage.cached_input_tokens = usage
                        .cached_input_tokens
                        .saturating_add(cached_input_tokens);
                    usage.cache_creation_input_tokens = usage
                        .cache_creation_input_tokens
                        .saturating_add(cache_creation_input_tokens);
                }
                // Plugin-driven model swap after last run puts the
                // request in the mgr; caller drains via
                // take_pending_next_model().
                _ => {}
            }
        }

        // Await the spawned task to catch any panics.
        let _ = task.await;

        // dirge-kuqp: a plugin `on-response` replacement rewrites the
        // final answer after the last turn already streamed its
        // assistant event. Track it so the StreamJson arm can emit a
        // corrected assistant event rather than leaving the stream
        // showing only the pre-replacement text.
        #[allow(unused_mut)]
        let mut response_replaced = false;

        // Plugin `on-response` + `on-complete` + `prepare-next-run`
        // dispatch. Headless modes previously skipped these.
        #[cfg(feature = "plugin")]
        if let Some(pm_arc) = crate::plugin::hook::global() {
            let mut mgr = pm_arc.lock_ignore_poison();
            let result = runner::apply_response_hooks(&full_response, &mut mgr);
            if let Some(replacement) = result.replacement {
                response_replaced = true;
                if suppress_inline {
                    full_response = replacement;
                } else {
                    println!();
                    println!("[plugin replace-result]");
                    let safe = crate::ui::ansi::strip_controls(
                        &replacement,
                        crate::ui::ansi::StripPolicy::KEEP_NEWLINE,
                    );
                    println!("{safe}");
                    full_response = replacement;
                }
            }
        }

        // dirge-18v2: classify how the stream ended. A truncated run
        // or one whose runner died without a Done must not produce a
        // success envelope.
        let end = if usage_capped {
            RunEnd::UsageCapped
        } else if !completed {
            RunEnd::Incomplete
        } else if truncated {
            RunEnd::Truncated
        } else {
            RunEnd::Completed
        };
        let result_envelope = headless_result_json(
            end,
            start_instant.elapsed().as_millis() as u64,
            num_turns,
            &full_response,
            &session_id,
            &headless_files_changed(),
        );

        match output_format {
            crate::cli::OutputFormat::Text => {
                println!();
            }
            crate::cli::OutputFormat::Json => {
                if let Ok(s) = serde_json::to_string(&result_envelope) {
                    println!("{}", s);
                }
            }
            crate::cli::OutputFormat::StreamJson => {
                // dirge-kuqp: each completed turn already streamed its
                // assistant event at TurnEnd. Two cases still need an
                // assistant event here:
                //   1. A partial final turn whose TurnEnd never arrived
                //      (runner died / interjected mid-turn) — flush the
                //      buffered text + tool calls.
                //   2. A plugin `on-response` replacement rewrote the final
                //      answer after the last turn streamed — emit the
                //      corrected text so the stream matches the result
                //      envelope (which carries the replacement).
                // The two can co-occur (a runner died mid-turn AND a plugin
                // replaced the result): the replacement is authoritative, so
                // it wins the text, but still carry any buffered tool_use
                // blocks from the dead turn so the stream stays well-formed.
                let leftover = !turn_text.is_empty() || !turn_tool_uses.is_empty();
                if response_replaced {
                    runner::emit_stream_json_event(stream_json_assistant_event(
                        &full_response,
                        &turn_tool_uses,
                        &session_id,
                    ));
                } else if leftover {
                    runner::emit_stream_json_event(stream_json_assistant_event(
                        &turn_text,
                        &turn_tool_uses,
                        &session_id,
                    ));
                }
                if (response_replaced || leftover) && !turn_tool_results.is_empty() {
                    runner::emit_stream_json_event(stream_json_tool_result_event(
                        &turn_tool_results,
                        &session_id,
                    ));
                }
                runner::emit_stream_json_event(result_envelope);
            }
        }

        // The runner died without delivering a Done — the collected
        // text is whatever streamed before it stopped. The envelope
        // above already says is_error; the process must also exit
        // non-zero so script consumers without JSON parsing notice.
        if end == RunEnd::Incomplete {
            return Err(anyhow::anyhow!(
                "run ended without completing — the agent runner stopped before producing a result"
            ));
        }
        // dirge-u6zc: exit non-zero on a usage cap so a non-JSON script
        // consumer notices the run paused; the envelope's `error_usage_cap`
        // subtype and the stderr reset hint distinguish it from a failure.
        if end == RunEnd::UsageCapped {
            return Err(anyhow::anyhow!(
                "provider usage cap reached — run paused before completion; resume after the quota resets"
            ));
        }
        Ok((full_response, tool_calls, usage))
    }
}

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

    /// dirge-18v2: the result envelope must reflect how the run ended
    /// — `--print` consumers parse this JSON, not stderr.
    #[test]
    fn result_envelope_reflects_run_end() {
        let ok = headless_result_json(RunEnd::Completed, 10, 2, "answer", "sid", &[]);
        assert_eq!(ok["subtype"], "success");
        assert_eq!(ok["is_error"], false);
        assert_eq!(ok["result"], "answer");

        let capped = headless_result_json(RunEnd::Truncated, 10, 100, "partial", "sid", &[]);
        assert_eq!(capped["subtype"], "error_max_turns");
        assert_eq!(capped["is_error"], true);
        assert_eq!(capped["result"], "partial", "partial text still delivered");

        let died = headless_result_json(RunEnd::Incomplete, 10, 1, "fragment", "sid", &[]);
        assert_eq!(died["subtype"], "error");
        assert_eq!(died["is_error"], true);

        // dirge-u6zc: a usage-cap pause is a distinct, resumable outcome —
        // its own subtype so a wrapper can re-run after the reset, not the
        // generic "error" it'd share with a runner death.
        let capped = headless_result_json(RunEnd::UsageCapped, 10, 5, "partial", "sid", &[]);
        assert_eq!(capped["subtype"], "error_usage_cap");
        assert_eq!(capped["is_error"], true);
        assert_eq!(capped["result"], "partial", "partial work still delivered");
    }

    /// issue #704: the envelope carries `files_changed` verbatim (a JSON
    /// array) so the MCP `delegate` consumer reads dirge's own record of
    /// edited files instead of guessing from `git status`. Empty when the
    /// run touched nothing; never absent.
    #[test]
    fn result_envelope_carries_files_changed() {
        let none = headless_result_json(RunEnd::Completed, 1, 1, "ok", "sid", &[]);
        assert_eq!(none["files_changed"], serde_json::json!([]));

        let files = ["src/a.rs".to_string(), "tests/b.rs".to_string()];
        let env = headless_result_json(RunEnd::Completed, 1, 1, "ok", "sid", &files);
        assert_eq!(
            env["files_changed"],
            serde_json::json!(["src/a.rs", "tests/b.rs"])
        );
    }

    /// dirge-kuqp: a turn's assistant event carries its streamed text
    /// as a `text` block followed by one `tool_use` block per call,
    /// matching the Claude Code stream-json shape consumers parse.
    #[test]
    fn assistant_event_has_text_then_tool_use_blocks() {
        let uses = vec![(
            "toolu_1".to_string(),
            "read".to_string(),
            serde_json::json!({"path": "a.rs"}),
        )];
        let ev = stream_json_assistant_event("reading the file", &uses, "sid");
        assert_eq!(ev["type"], "assistant");
        assert_eq!(ev["session_id"], "sid");
        assert_eq!(ev["message"]["role"], "assistant");
        let content = ev["message"]["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[0]["type"], "text");
        assert_eq!(content[0]["text"], "reading the file");
        assert_eq!(content[1]["type"], "tool_use");
        assert_eq!(content[1]["id"], "toolu_1");
        assert_eq!(content[1]["name"], "read");
        assert_eq!(content[1]["input"]["path"], "a.rs");
    }

    /// An empty text block is omitted — a tool-only turn (no narration)
    /// emits just the `tool_use` block, not a stray empty text block.
    #[test]
    fn assistant_event_omits_empty_text() {
        let uses = vec![(
            "toolu_1".to_string(),
            "list_dir".to_string(),
            serde_json::json!({}),
        )];
        let ev = stream_json_assistant_event("", &uses, "sid");
        let content = ev["message"]["content"].as_array().unwrap();
        assert_eq!(content.len(), 1);
        assert_eq!(content[0]["type"], "tool_use");
    }

    /// A pure-text turn with no tool calls emits a single text block —
    /// the unchanged single-turn shape (system, assistant, result).
    #[test]
    fn assistant_event_text_only() {
        let ev = stream_json_assistant_event("the answer", &[], "sid");
        let content = ev["message"]["content"].as_array().unwrap();
        assert_eq!(content.len(), 1);
        assert_eq!(content[0]["type"], "text");
        assert_eq!(content[0]["text"], "the answer");
    }

    /// Tool results come back as a `user` event with `tool_result`
    /// content blocks keyed by the originating call id — the Claude
    /// Code shape for a turn's tool output.
    #[test]
    fn tool_result_event_shape() {
        let results = vec![
            ("toolu_1".to_string(), "file contents".to_string()),
            ("toolu_2".to_string(), "dir listing".to_string()),
        ];
        let ev = stream_json_tool_result_event(&results, "sid");
        assert_eq!(ev["type"], "user");
        assert_eq!(ev["session_id"], "sid");
        assert_eq!(ev["message"]["role"], "user");
        let content = ev["message"]["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[0]["type"], "tool_result");
        assert_eq!(content[0]["tool_use_id"], "toolu_1");
        assert_eq!(content[0]["content"], "file contents");
        assert_eq!(content[1]["tool_use_id"], "toolu_2");
    }

    /// The truncation detector matches the notice the agent loop
    /// actually emits — both sides use MAX_TURNS_NOTICE_PREFIX, so a
    /// reworded notice that breaks the coupling fails here.
    #[test]
    fn truncation_notice_prefix_matches_emitter() {
        let cap = 100;
        // Mirror of the format string in agent_loop::run's max-turns
        // branch.
        let notice = format!(
            "{} ({cap}) reached. Stopping the run.",
            crate::agent::agent_loop::run::MAX_TURNS_NOTICE_PREFIX
        );
        assert!(notice.starts_with(crate::agent::agent_loop::run::MAX_TURNS_NOTICE_PREFIX));
        assert!(
            crate::agent::agent_loop::run::MAX_TURNS_NOTICE_PREFIX.starts_with("[dirge]"),
            "notice must stay visually attributable to dirge",
        );
    }
}