dirge-agent 0.12.5

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
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
//! dirge-ywj — plugin hook contract tests.
//!
//! These exercise the full pi-style hook contract surface by driving
//! `run_agent_loop` with the hook closures BUILT BY
//! `plugin_hooks::{before_hook_from_plugin_manager, after_hook_from_plugin_manager,
//! prepare_next_turn_from_plugin_manager, should_stop_after_turn_from_plugin_manager,
//! get_steering_messages_from_plugin_manager, get_followup_messages_from_plugin_manager}`
//! against a real `PluginManager` and asserting the resulting behaviour
//! of the loop (not just the hook output in isolation).
//!
//! Why drive the whole loop here? `plugin_hooks::tests` already covers
//! the input-shape level — given a Janet script, the hook returns the
//! expected `BeforeToolCallReturn` / `AfterToolCallResult`. What was
//! missing was contract-level coverage: does the loop ACTUALLY honour
//! a `block` (i.e. NOT invoke the underlying tool), does the mutated
//! args reach the tool's execute, etc.

#![cfg(feature = "plugin")]

use super::*;
use crate::agent::agent_loop::hooks::AfterToolCallContext;
use crate::agent::agent_loop::message::{StreamEvent, UserMessage};
use crate::agent::agent_loop::plugin_hooks::{
    after_hook_from_plugin_manager, before_hook_from_plugin_manager,
    compaction_hooks_from_plugin_manager, get_followup_messages_from_plugin_manager,
    get_steering_messages_from_plugin_manager, prepare_next_turn_from_plugin_manager,
    should_stop_after_turn_from_plugin_manager, transform_context_from_plugin_manager,
};
use crate::agent::agent_loop::result::LoopToolResult;
use crate::agent::agent_loop::stream::StreamFn;
use crate::agent::agent_loop::tool::{AbortSignal, LoopTool, LoopToolUpdate};
use crate::agent::agent_loop::types::{ConvertToLlmFn, LoopConfig, ToolExecutionMode};
use crate::plugin::PluginManager;
use serde_json::Value;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;

// ---------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------

/// Skip when Janet VM init fails (matches plugin_hooks::tests::try_pm).
fn try_pm() -> Option<Arc<Mutex<PluginManager>>> {
    match PluginManager::try_new() {
        Ok(mgr) => Some(Arc::new(Mutex::new(mgr))),
        Err(_) => None,
    }
}

fn identity_converter() -> ConvertToLlmFn {
    Arc::new(|messages: &[Value]| {
        messages
            .iter()
            .filter(|m| {
                let role = m.get("role").and_then(|r| r.as_str()).unwrap_or("");
                matches!(role, "user" | "assistant" | "tool" | "toolResult")
            })
            .cloned()
            .collect()
    })
}

fn build_config() -> LoopConfig {
    LoopConfig {
        convert_to_llm: identity_converter(),
        transform_context: None,
        compaction_hooks: None,
        get_api_key: None,
        api_key: None,
        tool_execution: ToolExecutionMode::Sequential,
        before_tool_call: None,
        after_tool_call: None,
        prepare_next_turn: None,
        should_stop_after_turn: None,
        get_steering_messages: None,
        get_followup_messages: None,
        reasoning: None,
        thinking_budgets: None,
        headers: std::collections::HashMap::new(),
        metadata: std::collections::HashMap::new(),
        request_timeout: None,
        provider_name: None,
        model_name: None,
        compact_model: None,
        storm_mutating_tools: None,
        storm_exempt_tools: None,
        repair_stats: Arc::new(crate::agent::agent_loop::tool_input_repair::RepairStats::new()),
        truncation_notes: Arc::new(Mutex::new(std::collections::HashMap::new())),
        tool_def_filter: None,
        dynamic_tool_search: false,
        escalation_stream_fn: None,
        escalation_provider_name: None,
        escalation_pending: Arc::new(Mutex::new(None)),
        escalation_max_per_session: 3,
        escalation_remaining: Arc::new(std::sync::atomic::AtomicUsize::new(3)),
        file_touch_tracker: None,
        verifier: None,
        critic_fn: None,
        goal: None,
        max_turns: None,
    }
}

fn empty_context() -> Context {
    Context {
        system_prompt: String::new(),
        messages: Vec::new(),
        tools: Vec::new(),
    }
}

fn user(text: &str) -> LoopMessage {
    LoopMessage::User(UserMessage {
        content: text.to_string(),
    })
}

fn text_response(text: &str) -> AssistantMessage {
    AssistantMessage::new(
        vec![ContentBlock::Text {
            text: text.to_string(),
        }],
        StopReason::Stop,
    )
}

fn tool_use_response(id: &str, name: &str, args: Value) -> AssistantMessage {
    AssistantMessage::new(
        vec![ContentBlock::ToolCall {
            id: id.to_string(),
            name: name.to_string(),
            arguments: args,
        }],
        StopReason::ToolUse,
    )
}

/// Stream factory: yields one canned `Done` per call from a scripted list.
fn canned_factory(responses: Vec<AssistantMessage>) -> StreamFn {
    let counter = Arc::new(AtomicUsize::new(0));
    let responses = Arc::new(responses);
    Arc::new(move |_ctx, _opts| {
        let n = counter.fetch_add(1, Ordering::SeqCst);
        let msg = responses.get(n).cloned().unwrap_or_else(|| {
            AssistantMessage::new(
                vec![ContentBlock::Text {
                    text: "end".to_string(),
                }],
                StopReason::Stop,
            )
        });
        let reason = msg.stop_reason;
        Box::pin(futures::stream::iter(vec![StreamEvent::Done {
            reason,
            message: msg,
            usage: None,
        }]))
    })
}

/// Recording tool that captures (id, args) per call.
#[derive(Debug)]
struct RecordingTool {
    name_str: String,
    calls: Arc<Mutex<Vec<(String, Value)>>>,
}

impl RecordingTool {
    fn new(name: &str) -> Self {
        Self {
            name_str: name.to_string(),
            calls: Arc::new(Mutex::new(Vec::new())),
        }
    }

    fn calls(&self) -> Vec<(String, Value)> {
        self.calls.lock().unwrap().clone()
    }
}

impl LoopTool for RecordingTool {
    fn name(&self) -> &str {
        &self.name_str
    }
    fn description(&self) -> &str {
        "Recording mock"
    }
    fn label(&self) -> &str {
        "Recording"
    }
    fn parameters(&self) -> &Value {
        static EMPTY: std::sync::OnceLock<Value> = std::sync::OnceLock::new();
        EMPTY.get_or_init(|| serde_json::json!({"type": "object"}))
    }
    fn execute<'a>(
        &'a self,
        id: &'a str,
        args: Value,
        _signal: AbortSignal,
        _on_update: LoopToolUpdate,
    ) -> Pin<Box<dyn Future<Output = Result<LoopToolResult, String>> + Send + 'a>> {
        let calls = self.calls.clone();
        let id = id.to_string();
        Box::pin(async move {
            calls.lock().unwrap().push((id.clone(), args.clone()));
            Ok(LoopToolResult {
                content: vec![serde_json::json!({"type": "text", "text": "original output"})],
                details: args,
                terminate: None,
            })
        })
    }
}

async fn drain(rx: &mut mpsc::Receiver<LoopEvent>) -> Vec<LoopEvent> {
    let mut out = Vec::new();
    while let Some(e) = rx.recv().await {
        out.push(e);
    }
    out
}

// ===============================================================
// 1. before_tool_call — block prevents underlying invocation
// ===============================================================

/// Install a plugin that calls `harness/block` in `on-tool-start`.
/// When the loop dispatches `bash`, the underlying tool must NOT
/// be invoked; the tool result content must carry the block reason.
#[tokio::test]
async fn ywj_before_tool_call_block_prevents_invocation() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(r#"(defn deny [_ctx] (harness/block "policy denial"))"#)
            .expect("install deny");
        mgr.register("on-tool-start", "deny");
    }

    let tool = Arc::new(RecordingTool::new("bash"));
    let mut ctx = empty_context();
    ctx.tools.push(tool.clone());

    let factory = canned_factory(vec![
        tool_use_response("call-1", "bash", serde_json::json!({"cmd": "ls"})),
        text_response("done"),
    ]);

    let mut cfg = build_config();
    cfg.before_tool_call = Some(before_hook_from_plugin_manager(pm.clone()));

    let (tx, _rx) = mpsc::channel::<LoopEvent>(64);
    let messages = run_agent_loop(
        vec![user("run bash")],
        ctx,
        cfg,
        AbortSignal::new(),
        &tx,
        &factory,
        None,
        None, // memory_provider — test default
    )
    .await;
    drop(tx);

    // Underlying tool was NEVER invoked.
    assert!(
        tool.calls().is_empty(),
        "blocked tool must not be invoked; got calls: {:?}",
        tool.calls(),
    );

    // Tool result message lands with the block reason text.
    let saw_block_reason = messages.iter().any(|m| match m {
        LoopMessage::ToolResult(t) => t.content.iter().any(|c| match c {
            ContentBlock::Text { text } => text.contains("policy denial"),
            _ => false,
        }),
        _ => false,
    });
    assert!(
        saw_block_reason,
        "tool result should carry the block reason text",
    );
}

// ===============================================================
// 2. before_tool_call mutation — mutated args reach the tool
// ===============================================================

/// Install a plugin that rewrites args via `harness/mutate-input`.
/// The RecordingTool must observe the MUTATED args, not the original.
#[tokio::test]
async fn ywj_before_tool_call_mutation_threads_through() {
    let Some(pm) = try_pm() else {
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(r#"(defn rewrite [_ctx] (harness/mutate-input "{\"cmd\":\"echo mutated\"}"))"#)
            .expect("install rewrite");
        mgr.register("on-tool-start", "rewrite");
    }

    let tool = Arc::new(RecordingTool::new("bash"));
    let mut ctx = empty_context();
    ctx.tools.push(tool.clone());

    let factory = canned_factory(vec![
        tool_use_response("call-1", "bash", serde_json::json!({"cmd": "ls"})),
        text_response("done"),
    ]);

    let mut cfg = build_config();
    cfg.before_tool_call = Some(before_hook_from_plugin_manager(pm.clone()));

    let (tx, _rx) = mpsc::channel::<LoopEvent>(64);
    let _ = run_agent_loop(
        vec![user("run bash")],
        ctx,
        cfg,
        AbortSignal::new(),
        &tx,
        &factory,
        None,
        None, // memory_provider — test default
    )
    .await;

    let calls = tool.calls();
    assert_eq!(calls.len(), 1, "tool should run exactly once");
    assert_eq!(
        calls[0].1,
        serde_json::json!({"cmd": "echo mutated"}),
        "tool must observe MUTATED args, not original",
    );
}

// ===============================================================
// 3. after_tool_call replacement — replaced content surfaces
// ===============================================================

/// Plugin calls `harness/replace-result` from `on-tool-end`. The
/// emitted ToolResult must carry the REPLACED content, not the
/// original.
#[tokio::test]
async fn ywj_after_tool_call_replaces_result_content() {
    let Some(pm) = try_pm() else {
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(r#"(defn rewrite [_ctx] (harness/replace-result "REPLACED"))"#)
            .expect("install rewrite");
        mgr.register("on-tool-end", "rewrite");
    }

    let tool = Arc::new(RecordingTool::new("bash"));
    let mut ctx = empty_context();
    ctx.tools.push(tool.clone());

    let factory = canned_factory(vec![
        tool_use_response("call-1", "bash", serde_json::json!({"cmd": "ls"})),
        text_response("done"),
    ]);

    let mut cfg = build_config();
    cfg.after_tool_call = Some(after_hook_from_plugin_manager(pm.clone()));

    let (tx, mut rx) = mpsc::channel::<LoopEvent>(128);
    let messages = run_agent_loop(
        vec![user("run bash")],
        ctx,
        cfg,
        AbortSignal::new(),
        &tx,
        &factory,
        None,
        None, // memory_provider — test default
    )
    .await;
    drop(tx);

    // Tool was invoked (the after-hook only runs after dispatch).
    assert_eq!(tool.calls().len(), 1);

    // The ToolResult message in the transcript must carry the
    // REPLACED content — `original output` should NOT appear.
    let tool_result = messages
        .iter()
        .find_map(|m| match m {
            LoopMessage::ToolResult(t) => Some(t.clone()),
            _ => None,
        })
        .expect("tool result message present");
    let saw_replaced = tool_result.content.iter().any(|c| match c {
        ContentBlock::Text { text } => text.contains("REPLACED"),
        _ => false,
    });
    let saw_original = tool_result.content.iter().any(|c| match c {
        ContentBlock::Text { text } => text.contains("original output"),
        _ => false,
    });
    assert!(saw_replaced, "tool result should carry replaced content");
    assert!(
        !saw_original,
        "tool result must NOT carry original content after replacement",
    );

    // The ToolExecutionEnd event also reflects the replaced content.
    let events = drain(&mut rx).await;
    let exec_end_replaced = events.iter().any(|e| match e {
        LoopEvent::ToolExecutionEnd { result, .. } => result.content.iter().any(|c| {
            c.as_object()
                .and_then(|o| o.get("text"))
                .and_then(|t| t.as_str())
                .map(|s| s.contains("REPLACED"))
                .unwrap_or(false)
        }),
        _ => false,
    });
    assert!(exec_end_replaced, "ToolExecutionEnd should carry REPLACED");
}

// ===============================================================
// 4. prepare_next_turn — TurnUpdate observed by next turn
// ===============================================================

/// Plugin sets the next thinking level via
/// `harness/set-next-thinking-level`. After a tool dispatch,
/// the prepare_next_turn hook returns Some(TurnUpdate{thinking_level: High}).
/// The hook is exercised by the loop; we verify it actually fired
/// (and didn't crash) by completing a full multi-turn run.
#[tokio::test]
async fn ywj_prepare_next_turn_returns_turn_update() {
    let Some(pm) = try_pm() else {
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        // Set the slot on every tool-end so the prepare_next_turn
        // hook has something to return.
        mgr.eval(r#"(defn bump [_ctx] (harness/set-next-thinking-level "high"))"#)
            .unwrap();
        mgr.register("on-tool-end", "bump");
    }

    let tool = Arc::new(RecordingTool::new("noop"));
    let mut ctx = empty_context();
    ctx.tools.push(tool.clone());

    let factory = canned_factory(vec![
        tool_use_response("call-1", "noop", serde_json::json!({})),
        text_response("done"),
    ]);

    let mut cfg = build_config();
    cfg.prepare_next_turn = Some(prepare_next_turn_from_plugin_manager(pm.clone()));

    let (tx, _rx) = mpsc::channel::<LoopEvent>(128);
    let messages = run_agent_loop(
        vec![user("hi")],
        ctx,
        cfg,
        AbortSignal::new(),
        &tx,
        &factory,
        None,
        None, // memory_provider — test default
    )
    .await;

    // The loop completed both turns — prepare_next_turn fired
    // between them without crashing.
    let roles: Vec<&'static str> = messages.iter().map(|m| m.role()).collect();
    assert_eq!(roles, vec!["user", "assistant", "toolResult", "assistant"]);
    // Sanity: the slot was drained (subsequent reads return None).
    let pending = pm.lock().unwrap().take_pending_next_thinking_level();
    assert!(
        pending.is_none(),
        "prepare_next_turn should have drained the slot",
    );
}

// ===============================================================
// 5. should_stop_after_turn — true exits loop
// ===============================================================

/// Plugin calls `harness/request-stop-after-turn` on every tool-end.
/// After dispatch, should_stop_after_turn returns true and the loop
/// exits without making the second LLM call.
#[tokio::test]
async fn ywj_should_stop_after_turn_terminates_loop() {
    let Some(pm) = try_pm() else {
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(r#"(defn stop [_ctx] (harness/request-stop-after-turn))"#)
            .unwrap();
        mgr.register("on-tool-end", "stop");
    }

    let tool = Arc::new(RecordingTool::new("noop"));
    let mut ctx = empty_context();
    ctx.tools.push(tool.clone());

    let llm_calls = Arc::new(AtomicUsize::new(0));
    let llm_calls_clone = llm_calls.clone();
    let factory: StreamFn = Arc::new(move |_ctx, _opts| {
        let n = llm_calls_clone.fetch_add(1, Ordering::SeqCst);
        let msg = if n == 0 {
            tool_use_response("call-1", "noop", serde_json::json!({}))
        } else {
            text_response("should not appear")
        };
        let reason = msg.stop_reason;
        Box::pin(futures::stream::iter(vec![StreamEvent::Done {
            reason,
            message: msg,
            usage: None,
        }]))
    });

    let mut cfg = build_config();
    // BOTH hooks needed: `on-tool-end` fires `harness/request-stop-after-turn`
    // (via after_tool_call dispatching the hook); should_stop_after_turn
    // then drains that slot.
    cfg.after_tool_call = Some(after_hook_from_plugin_manager(pm.clone()));
    cfg.should_stop_after_turn = Some(should_stop_after_turn_from_plugin_manager(pm.clone()));

    let (tx, mut rx) = mpsc::channel::<LoopEvent>(128);
    let _messages = run_agent_loop(
        vec![user("hi")],
        ctx,
        cfg,
        AbortSignal::new(),
        &tx,
        &factory,
        None,
        None, // memory_provider — test default
    )
    .await;
    drop(tx);

    // Only ONE LLM call should have happened — the hook stops the
    // loop after the first turn finishes its tool dispatch.
    assert_eq!(
        llm_calls.load(Ordering::SeqCst),
        1,
        "should_stop_after_turn must prevent the second LLM call",
    );

    let kinds: Vec<&str> = drain(&mut rx).await.iter().map(|e| e.kind()).collect();
    assert!(kinds.contains(&"agent_end"), "agent_end fires on stop");
}

// ===============================================================
// 6. get_steering_messages — queued user message at turn boundary
// ===============================================================

/// Plugin pre-queues a steering message before the loop starts.
/// The hook fires at the first inner-turn-boundary; the next
/// assistant LLM call sees the steering message in its context.
#[tokio::test]
async fn ywj_get_steering_messages_injects_user_message_at_boundary() {
    let Some(pm) = try_pm() else {
        return;
    };
    // Queue a steering message NOW so the very first hook poll
    // (called by the loop right before turn 1 starts) returns it.
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(r#"(harness/add-steering "queued steering")"#)
            .unwrap();
    }

    // Single-turn run — assistant emits text, no tools.
    let factory = canned_factory(vec![text_response("ok")]);

    let mut cfg = build_config();
    cfg.get_steering_messages = Some(get_steering_messages_from_plugin_manager(pm.clone()));

    let (tx, _rx) = mpsc::channel::<LoopEvent>(64);
    let messages = run_agent_loop(
        vec![user("hi")],
        empty_context(),
        cfg,
        AbortSignal::new(),
        &tx,
        &factory,
        None,
        None, // memory_provider — test default
    )
    .await;

    // The steering message landed in the transcript as a User
    // message right after the initial user prompt.
    let user_contents: Vec<String> = messages
        .iter()
        .filter_map(|m| match m {
            LoopMessage::User(u) => Some(u.content.clone()),
            _ => None,
        })
        .collect();
    assert!(
        user_contents.contains(&"hi".to_string()),
        "original prompt preserved",
    );
    assert!(
        user_contents.contains(&"queued steering".to_string()),
        "steering message injected as User; got {user_contents:?}",
    );
}

// ===============================================================
// 7. get_followup_messages — outer-loop re-entry
// ===============================================================

/// After the inner loop ends naturally (assistant says "done"),
/// the outer-loop boundary polls get_followup_messages. The hook
/// returns ONE followup; the loop re-enters and the second
/// assistant turn fires.
#[tokio::test]
async fn ywj_get_followup_messages_reenters_outer_loop() {
    let Some(pm) = try_pm() else {
        return;
    };
    // Queue a followup message — drained the first time
    // get_followup_messages is polled.
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(r#"(harness/add-followup "followup question")"#)
            .unwrap();
    }

    // Two turns of canned text responses — both complete normally
    // so the inner loop ends after each, and the outer-loop poll
    // drives re-entry exactly once (the followup queue is drained
    // on the first poll; the second poll returns empty so the
    // outer loop exits).
    let factory = canned_factory(vec![
        text_response("first done"),
        text_response("second done"),
    ]);

    let mut cfg = build_config();
    cfg.get_followup_messages = Some(get_followup_messages_from_plugin_manager(pm.clone()));

    let (tx, _rx) = mpsc::channel::<LoopEvent>(64);
    let messages = run_agent_loop(
        vec![user("hi")],
        empty_context(),
        cfg,
        AbortSignal::new(),
        &tx,
        &factory,
        None,
        None, // memory_provider — test default
    )
    .await;

    // Messages: user("hi"), assistant("first done"),
    //           user("followup question"), assistant("second done").
    let roles: Vec<&'static str> = messages.iter().map(|m| m.role()).collect();
    assert_eq!(
        roles,
        vec!["user", "assistant", "user", "assistant"],
        "outer-loop re-entered with followup as new user prompt",
    );

    let user_contents: Vec<String> = messages
        .iter()
        .filter_map(|m| match m {
            LoopMessage::User(u) => Some(u.content.clone()),
            _ => None,
        })
        .collect();
    assert_eq!(user_contents, vec!["hi", "followup question"]);
}

// ── dirge plugin-gap closure: new hooks ─────────────────────

/// dirge-wqxj: a `before-agent-start` hook calling
/// harness/append-system-prompt populates the slot the spawn path
/// drains and appends to the system prompt. Proves the
/// slot/builtin/HOOK_NAME/drain chain end-to-end.
#[test]
fn before_agent_start_appends_system_prompt() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    let mut mgr = pm.lock().unwrap();
    mgr.eval(r#"(defn aug [_ctx] (harness/append-system-prompt "TEAM RULES"))"#)
        .expect("install aug");
    mgr.register("before-agent-start", "aug");
    mgr.dispatch("before-agent-start", "@{:system-prompt \"base preamble\"}")
        .expect("dispatch");
    assert_eq!(
        mgr.take_system_prompt_append().as_deref(),
        Some("TEAM RULES"),
        "append slot must carry the hook's text",
    );
    // Drained once → cleared.
    assert_eq!(mgr.take_system_prompt_append(), None);
}

/// dirge-lsoq: a `message-end` hook calling harness/rewrite-message
/// populates the slot the done-handler drains to replace the stored
/// response.
#[test]
fn message_end_rewrites_response() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    let mut mgr = pm.lock().unwrap();
    mgr.eval(r#"(defn redact [_ctx] (harness/rewrite-message "[redacted]"))"#)
        .expect("install redact");
    mgr.register("message-end", "redact");
    mgr.dispatch("message-end", "@{:message \"secret output\"}")
        .expect("dispatch");
    assert_eq!(mgr.take_message_rewrite().as_deref(), Some("[redacted]"));
    assert_eq!(mgr.take_message_rewrite(), None);
}

/// dirge-264x: the transform_context factory dispatches the
/// `transform-context` hook and applies harness/replace-context —
/// the returned messages REPLACE the input for that call.
#[tokio::test]
async fn transform_context_replaces_messages() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(
            r#"(defn xform [_ctx] (harness/replace-context "[{\"role\":\"user\",\"content\":\"pruned\"}]"))"#,
        )
        .expect("install xform");
        mgr.register("transform-context", "xform");
    }
    let f = transform_context_from_plugin_manager(pm.clone());
    let original = vec![
        serde_json::json!({"role": "user", "content": "a"}),
        serde_json::json!({"role": "assistant", "content": "b"}),
    ];
    let out = f(original).await;
    assert_eq!(out.len(), 1, "context replaced by the hook's array");
    assert_eq!(out[0]["content"], "pruned");
}

/// dirge-264x safety: no hook (or a hook that sets nothing) → the
/// original messages pass through UNCHANGED. A plugin can never
/// corrupt the context by being absent.
#[tokio::test]
async fn transform_context_passthrough_without_hook() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    let f = transform_context_from_plugin_manager(pm.clone());
    let original = vec![serde_json::json!({"role": "user", "content": "keep me"})];
    let out = f(original.clone()).await;
    assert_eq!(out, original, "no transform-context hook → unchanged");
}

/// dirge-264x safety: a hook that sets malformed JSON must NOT
/// corrupt the context — the original messages pass through.
#[tokio::test]
async fn transform_context_passthrough_on_malformed_json() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(r#"(defn bad [_ctx] (harness/replace-context "not json {{{"))"#)
            .expect("install bad");
        mgr.register("transform-context", "bad");
    }
    let f = transform_context_from_plugin_manager(pm.clone());
    let original = vec![serde_json::json!({"role": "user", "content": "keep me"})];
    let out = f(original.clone()).await;
    assert_eq!(out, original, "malformed JSON → original context preserved");
}

/// dirge-jia8: a Janet `on-compact` hook calling
/// harness/set-compact-summary flows through the factory's
/// on_compact closure (slot/builtin/drain chain end-to-end).
#[tokio::test]
async fn on_compact_hook_supplies_custom_summary() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    {
        let mut mgr = pm.lock().unwrap();
        mgr.eval(
            r#"(defn summ [_ctx] (harness/set-compact-summary "Active Task: plugin summary"))"#,
        )
        .expect("install summ");
        mgr.register("on-compact", "summ");
    }
    let hooks = compaction_hooks_from_plugin_manager(pm.clone());
    let middle = vec![serde_json::json!({"role": "user", "content": "old turn"})];
    let summary = (hooks.on_compact)(middle).await;
    assert_eq!(
        summary.as_deref(),
        Some("Active Task: plugin summary"),
        "on-compact hook's summary must flow through the factory",
    );
}

/// dirge-jia8: no `on-compact` hook → factory returns None (the
/// loop falls through to the LLM summarizer). on-before is
/// observe-only and never panics.
#[tokio::test]
async fn compaction_hooks_passthrough_without_hooks() {
    let Some(pm) = try_pm() else {
        eprintln!("[skipped] PluginManager::try_new failed");
        return;
    };
    let hooks = compaction_hooks_from_plugin_manager(pm.clone());
    // observe-only before hook: must not panic.
    (hooks.on_before)(5, 1234).await;
    // no on-compact hook registered → None.
    let summary =
        (hooks.on_compact)(vec![serde_json::json!({"role": "user", "content": "x"})]).await;
    assert_eq!(summary, None, "no on-compact hook → fall through to LLM");
}