dirge-agent 0.11.0

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
//! `AgentEvent::Done` handler extracted from `run_interactive`.
//!
//! This is the largest handler — it closes a successful turn,
//! finalizes the streamed response, runs the plugin `on-response` /
//! `on-complete` / `prepare-next-run` chain (with optional model
//! swap), auto-compacts when the session crossed the threshold,
//! decides via `decide_post_done_action` whether to launch a
//! follow-up / loop iteration / stop, hands off to `plan_review` when a
//! phased `/plan` implement turn just finished (the reviewer-runs-code
//! loop), spawns a background review + curator pass when idle, handles
//! git-worktree return, and finally drains any user interjections queued
//! during the run.
//!
//! Behavior is identical to the original inline body; only the
//! lexical home moved.

#[allow(unused_imports)]
use crate::sync_util::LockExt;
use compact_str::CompactString;
use crossterm::style::Color;
use tokio::sync::mpsc;

use crate::context::ContextFiles;
use crate::event::AgentEvent;
#[cfg(feature = "plugin")]
use crate::plugin::PluginManager;
use crate::provider::AnyAgent;
use crate::session::MessageRole;
use crate::ui::agent_io::persist_turn_to_db;
use crate::ui::avatar;
use crate::ui::colors::{c_agent, c_error};
use crate::ui::run_handlers::{AgentBuildDeps, RunCtx};
use crate::ui::slash::handle_compress;
use crate::ui::theme;
use crate::ui::tool_display::{chamber_bottom, chamber_widths};

/// Optional loop-feature state passed through to `handle_done`.
/// Behind `cfg(feature = "loop")` we hand the real mutable state;
/// without the feature, the placeholder type is `()` so the call
/// site doesn't need to thread a sentinel.
#[cfg(feature = "loop")]
pub(crate) struct LoopBits<'a> {
    pub state: &'a mut Option<crate::extras::r#loop::LoopState>,
    pub label: &'a mut Option<String>,
}

// False positive for `await_holding_lock`: the plugin-manager guard
// IS held during dispatch calls (sync) but is explicitly `drop(mgr)`-ed
// at line 209 BEFORE the `build_agent(...).await` at line 236, and
// re-acquired in a new scope at line 263. Clippy can't trace the
// `drop()` so it flags the outer `let mut mgr` as held across the
// await even though it isn't.
// `unused_mut` allowed: `response`'s `mut` is consumed only by the
// plugin-gated `message-end` rewrite, so non-plugin builds see it
// as unused.
#[allow(clippy::too_many_arguments, clippy::await_holding_lock, unused_mut)]
pub(crate) async fn handle_done(
    ctx: &mut RunCtx<'_>,
    // dirge-lsoq: `mut` so the `message-end` plugin hook can rewrite
    // the finalized assistant text before it is stored/persisted.
    mut response: CompactString,
    tokens: u64,
    cost: f64,
    was_reasoning: &mut bool,
    is_running: &mut bool,
    agent: &mut AnyAgent,
    context: &mut ContextFiles,
    // dirge-4y4l: the ~10 build_agent inputs bundled (see AgentBuildDeps).
    deps: &AgentBuildDeps<'_>,
    agent_rx: &mut Option<mpsc::Receiver<AgentEvent>>,
    agent_abort: &mut Option<tokio::task::JoinHandle<()>>,
    agent_interject: &mut Option<mpsc::Sender<()>>,
    agent_cancel: &mut Option<mpsc::Sender<()>>,
    interjection_queue: &std::sync::Arc<std::sync::Mutex<std::collections::VecDeque<String>>>,
    #[cfg(feature = "plugin")] plugin_manager: Option<
        &std::sync::Arc<std::sync::Mutex<PluginManager>>,
    >,
    #[cfg(feature = "loop")] loop_bits: LoopBits<'_>,
) -> anyhow::Result<()> {
    // Rebind the bundled deps to locals so the body below reads unchanged.
    let client = deps.client;
    let permission = deps.permission;
    let ask_tx = deps.ask_tx;
    let question_tx = deps.question_tx;
    let plan_tx = deps.plan_tx;
    let user_tx = deps.user_tx;
    let bg_store = deps.bg_store;
    let sandbox = deps.sandbox;
    #[cfg(feature = "mcp")]
    let mcp_manager = deps.mcp_manager;
    #[cfg(feature = "semantic")]
    let semantic_manager = deps.semantic_manager;
    #[cfg(feature = "lsp")]
    let lsp_manager = deps.lsp_manager;
    *was_reasoning = false;
    // A successful turn must not leave a chamber
    // half-painted. If anything slipped through
    // — show_details=false skipping the body, an
    // in-flight Ask the user resolved with a path
    // that didn't reach the bottom paint, etc. —
    // close with a plain chamber bottom (not the
    // `⚠ tool denied · aborted` wording, which
    // would mislead the user about an otherwise-
    // successful run).
    if *ctx.tool_chamber_open {
        // Same drop-or-close logic as
        // close_tool_chamber_passive: if no
        // body content was added since the
        // TOP was painted (result never
        // arrived from the agent — MCP timeout,
        // network blip, agent loop bug), drop
        // the chamber entirely instead of
        // leaving an empty box on screen.
        // Otherwise close with a bottom border.
        let drop_chamber = match (*ctx.chamber_top_start, *ctx.chamber_top_end) {
            (Some(_), Some(end)) => ctx.renderer.buffer_len() == end,
            _ => false,
        };
        if drop_chamber {
            if let Some(start) = *ctx.chamber_top_start {
                ctx.renderer.replace_from(start, Vec::new());
            }
        } else {
            let (frame_w, _) = chamber_widths(ctx.renderer);
            ctx.renderer
                .write_line_raw(&chamber_bottom(frame_w), theme::dim())?;
        }
        *ctx.tool_chamber_open = false;
        *ctx.chamber_top_start = None;
        *ctx.chamber_top_end = None;
    }
    *ctx.last_tool_name = None;
    ctx.renderer.set_avatar_state(avatar::AvatarState::Done);
    #[cfg(feature = "experimental-ui-terminal-tab")]
    ctx.renderer.set_last_tool_name("");

    #[allow(unused_mut, unused_variables)]
    let mut plugin_followup: Option<String> = None;
    #[cfg(feature = "plugin")]
    if let Some(pm) = plugin_manager {
        let mut mgr = pm.lock_ignore_poison();
        match mgr.dispatch(
            "on-response",
            &format!(
                "@{{:response \"{}\"}}",
                crate::plugin::escape_janet_string(&response)
            ),
        ) {
            Ok(results) if !results.is_empty() => {
                for line in &results {
                    // Sanitize plugin output (ANSI injection defense).
                    let safe = crate::ui::events::sanitize_output(line);
                    ctx.renderer
                        .write_line(&format!("[plugin] {}", safe), theme::dim())?;
                }
                plugin_followup = Some(results.join("\n"));
            }
            Ok(_) => {}
            Err(e) => {
                ctx.renderer
                    .write_line(&format!("[plugin] on-response error: {e}"), c_error())?;
            }
        }
        // Check for pending prompts queued by on-response
        if let Some(pending) = mgr.take_pending_prompt() {
            plugin_followup = Some(pending);
        }
        // dirge-lsoq: fire `message-end` so a plugin can rewrite the
        // finalized assistant text via `harness/rewrite-message`. The
        // text already streamed to the screen; this rewrites what is
        // STORED + persisted (session DB, store_response), enabling
        // post-hoc redaction/annotation of stored history.
        match mgr.dispatch(
            "message-end",
            &format!(
                "@{{:message \"{}\"}}",
                crate::plugin::escape_janet_string(&response)
            ),
        ) {
            Ok(_) => {
                if let Some(rewritten) = mgr.take_message_rewrite() {
                    response = compact_str::CompactString::new(&rewritten);
                }
            }
            Err(e) => {
                ctx.renderer
                    .write_line(&format!("[plugin] message-end error: {e}"), c_error())?;
            }
        }
        mgr.store_response(&response);
        // Fire on-complete after on-response so
        // plugins can react to "turn fully done."
        // Previously this hook was in HOOK_NAMES
        // (so plugins defining it got auto-aliased)
        // but no host site dispatched — silent fail.
        match mgr.dispatch("on-complete", "@{}") {
            Ok(_) => {}
            Err(e) => {
                ctx.renderer
                    .write_line(&format!("[plugin] on-complete error: {e}"), c_error())?;
            }
        }
        // Fire `prepare-next-run` so plugins can
        // signal session-level state changes for
        // the next run. Closes the gap vs pi's
        // `prepareNextTurn` for the auto-apply
        // piece: when `harness-next-model` is
        // set, the agent is rebuilt with the new
        // model RIGHT HERE so the next user
        // prompt runs against it without
        // requiring `/model X`.
        //
        // Scope difference vs pi: pi fires
        // `prepareNextTurn` between TURNS within
        // a single agent run (and can swap model
        // mid-stream). dirge fires
        // `prepare-next-run` only between RUNS
        // (after Done). Mid-stream swap requires
        // breaking rig's multi-turn stream and
        // restarting with a new agent — that
        // would lose partial assistant state, so
        // we keep the swap at run boundaries.
        match mgr.dispatch("prepare-next-run", "@{}") {
            Ok(_) => {}
            Err(e) => {
                ctx.renderer
                    .write_line(&format!("[plugin] prepare-next-run error: {e}"), c_error())?;
            }
        }
        let pending_next_model = mgr.take_pending_next_model();
        // Release the plugin-manager guard before any `.await` below —
        // `std::sync::MutexGuard` is `!Send`, and the agent rebuild
        // path awaits a future. Re-acquire after the await for the
        // final `set harness-response nil`.
        drop(mgr);
        if let Some(next_model) = pending_next_model {
            // Validate: empty string is a
            // misconfiguration. Don't replace the
            // active model with nothing.
            let trimmed = next_model.trim();
            if !trimmed.is_empty() && trimmed != ctx.session.model.as_str() {
                let new_model_compact = CompactString::new(trimmed);
                let model_obj = client.completion_model(new_model_compact.to_string());
                *agent = crate::provider::build_agent(
                    model_obj,
                    ctx.cli,
                    ctx.cfg,
                    context,
                    permission.clone(),
                    ask_tx.clone(),
                    question_tx.clone(),
                    plan_tx.clone(),
                    bg_store.clone(),
                    #[cfg(feature = "lsp")]
                    lsp_manager.cloned(),
                    sandbox.clone(),
                    #[cfg(feature = "mcp")]
                    mcp_manager,
                    #[cfg(feature = "semantic")]
                    semantic_manager,
                    Some(ctx.session.id.to_string()),
                )
                .await;
                let old_model = ctx.session.model.clone();
                ctx.session.model = new_model_compact.clone();
                ctx.session.provider = ctx.cli.resolve_provider(ctx.cfg);
                // Re-resolve context window for
                // the new model — mirrors the
                // `/model` slash behavior so a
                // 128k→1M jump (or vice versa)
                // updates the status indicator.
                let new_ctx = ctx.cfg.resolve_context_window(new_model_compact.as_str());
                if new_ctx != ctx.session.context_window {
                    ctx.session.context_window = new_ctx;
                }
                ctx.renderer.write_line(
                    &format!(
                        "[plugin] swapped model: {}{}",
                        old_model, new_model_compact,
                    ),
                    c_agent(),
                )?;
            }
        }
        // Clear `harness-response` so the next hook
        // doesn't see stale text from this turn. Re-acquire the
        // lock here since we released it above to satisfy
        // `clippy::await_holding_lock`.
        {
            let mut mgr = pm.lock_ignore_poison();
            let _ = mgr.eval("(set harness-response nil)");
        }
    }

    if !ctx.response_buf.is_empty() {
        // dirge-qy3y: final render through the source-tracked stream API (the
        // open block created during streaming), so the committed response is a
        // reflowable markdown block. `commit_stream` seals it.
        ctx.renderer.stream(ctx.response_buf, c_agent(), true);
        ctx.renderer.render_viewport()?;
    } else if !*ctx.agent_line_started {
        ctx.renderer.write("<dirge> ", c_agent())?;
    }
    // Seal any open streamed block (response above, or reasoning-only turn).
    ctx.renderer.commit_stream();

    ctx.renderer.write_line("", Color::White)?;
    ctx.renderer.write_line("", Color::White)?;
    // Phase 3: persist structured tool calls
    // alongside the assistant text so the next
    // resume sees the full tool_use/tool_result
    // pairs in convert_history.
    ctx.session.add_message_with_tool_calls(
        MessageRole::Assistant,
        &response,
        std::mem::take(ctx.tool_calls_buf),
    );
    // TODO(cost-tracking): `tokens` here is the heuristic
    // estimate (text.len()/4) and `cost` is always 0.0 —
    // these accumulate into placeholder fields and won't
    // reflect actual provider usage / billing until we
    // pipe rig's `FinalResponse.usage()` through into
    // `AgentEvent::Done`. Kept as no-op-ish additions so
    // the wiring is in place when real values arrive.
    ctx.session.total_tokens = ctx.session.total_tokens.saturating_add(tokens);
    ctx.session.total_cost += cost;
    // Run ended cleanly — reset the per-run tool-
    // call counter so the next user submission
    // starts at zero. Mirrored in the Interjected
    // branch + both abort paths below.
    *ctx.tool_calls_this_run = 0;
    *ctx.agent_line_started = false;
    ctx.response_buf.clear();
    *ctx.response_start_line = None;
    // Stash the turn's thinking before clearing so Ctrl+O can still expand
    // it after the turn completes.
    ctx.end_reasoning();
    *ctx.reasoning_start_line = None;

    #[cfg(feature = "loop")]
    let loop_running = loop_bits.state.as_ref().is_some_and(|ls| ls.active);
    #[cfg(not(feature = "loop"))]
    let loop_running = false;

    if !loop_running
        && ctx.cfg.resolve_compact_enabled()
        && ctx
            .session
            .needs_compaction(ctx.cfg.resolve_reserve_tokens())
        && !ctx.cli.no_session
    {
        // Auto-compact failure used to render as a
        // single dim red line that scrolled past
        // unnoticed — users kept typing into an
        // over-full context and saw mysterious
        // context-length errors next turn. Frame
        // the warning so it visibly stops the eye
        // and tells the user what to do next.
        ctx.renderer
            .write_line("▒░ auto-compacting context ░▒", theme::accent())?;
        let compress_result = handle_compress(
            None,
            false, // forced: auto-compaction stays threshold-gated [dirge-fgtj]
            agent,
            client,
            ctx.renderer,
            ctx.session,
            ctx.cli,
            ctx.cfg,
            context,
            permission,
            ask_tx,
            question_tx,
            plan_tx,
            user_tx,
            bg_store,
            sandbox,
            #[cfg(feature = "mcp")]
            mcp_manager,
            #[cfg(feature = "semantic")]
            semantic_manager,
            #[cfg(feature = "lsp")]
            lsp_manager,
        )
        .await;
        if let Err(e) = compress_result {
            ctx.renderer.write_line(
                "╭─ ⚠ AUTO-COMPACT FAILED ─────────────────────────────╮",
                c_error(),
            )?;
            // Cap the cause length so a sprawling
            // multi-line error doesn't blow out the
            // box's visual rhythm. The full error
            // is still in the agent's recovery
            // path; this is for the user-facing
            // hint only.
            let cause = {
                let s = e.to_string().replace('\n', " ");
                if s.chars().count() > 64 {
                    let mut out: String = s.chars().take(63).collect();
                    out.push('');
                    out
                } else {
                    s
                }
            };
            ctx.renderer
                .write_line(&format!("│ cause: {}", cause), c_error())?;
            ctx.renderer.write_line(
                "│ context is over the threshold — replies may start",
                c_error(),
            )?;
            ctx.renderer
                .write_line("│ hitting context-length errors. Try /compress", c_error())?;
            ctx.renderer.write_line(
                "│ manually, /clear to start fresh, or restart with",
                c_error(),
            )?;
            ctx.renderer
                .write_line("│ a larger context_window in config.", c_error())?;
            ctx.renderer.write_line(
                "╰─────────────────────────────────────────────────────╯",
                c_error(),
            )?;
        }
    }

    if !ctx.cli.no_session
        && let Err(e) = crate::session::storage::save_session(ctx.session)
    {
        ctx.renderer.write_line(
            &format!("warning: failed to save session: {}", e),
            c_error(),
        )?;
    }
    *is_running = false;
    if let Some(h) = agent_abort.take() {
        h.abort();
    }
    *agent_rx = None;
    *agent_interject = None;
    *agent_cancel = None;

    #[cfg(feature = "plugin")]
    let followup_for_decision = plugin_followup.clone();
    #[cfg(not(feature = "plugin"))]
    let followup_for_decision: Option<String> = None;

    #[cfg(feature = "loop")]
    let (loop_active, loop_should_stop) = loop_bits
        .state
        .as_ref()
        .map(|ls| (ls.active, ls.active && ls.should_stop()))
        .unwrap_or((false, false));
    #[cfg(not(feature = "loop"))]
    let (loop_active, loop_should_stop) = (false, false);

    let action = crate::plugin::decide_post_done_action(
        followup_for_decision,
        loop_active,
        loop_should_stop,
    );

    match action {
        crate::plugin::PostDoneAction::Followup(text) => {
            let followup_prompt = text + "\n\nContinue.";
            ctx.last_user_prompt.clone_from(&followup_prompt);
            let runner = agent.clone().spawn_runner(
                crate::agent::tools::background::prepend_pending_notifications(
                    &followup_prompt,
                    bg_store.as_ref(),
                ),
                crate::agent::runner::convert_history(ctx.session),
                Some(interjection_queue.clone()),
            );
            runner.install_into(
                agent_rx,
                agent_abort,
                agent_interject,
                agent_cancel,
                is_running,
            );
        }
        crate::plugin::PostDoneAction::LoopStop =>
        {
            #[cfg(feature = "loop")]
            if let Some(ls) = loop_bits.state.as_mut() {
                ctx.renderer.write_line(
                    &format!("[loop] max iterations ({}) reached, stopping", ls.iteration),
                    c_agent(),
                )?;
                ls.active = false;
                *loop_bits.label = None;
            }
        }
        crate::plugin::PostDoneAction::LoopIter =>
        {
            #[cfg(feature = "loop")]
            if let Some(ls) = loop_bits.state.as_mut() {
                let summary: String = response.chars().take(200).collect();
                ls.last_summary = Some(summary);
                ls.iteration += 1;
                let prompt = ls.build_prompt();
                ctx.last_user_prompt.clone_from(&prompt);
                let runner = agent.clone().spawn_runner(
                    crate::agent::tools::background::prepend_pending_notifications(
                        &prompt,
                        bg_store.as_ref(),
                    ),
                    Vec::new(),
                    Some(interjection_queue.clone()),
                );
                runner.install_into(
                    agent_rx,
                    agent_abort,
                    agent_interject,
                    agent_cancel,
                    is_running,
                );
                *loop_bits.label = Some(ls.iteration_label());
                ctx.renderer.write_line(
                    &format!("[loop] launching {}", ls.iteration_label()),
                    c_agent(),
                )?;
            }
        }
        crate::plugin::PostDoneAction::Idle => {}
    }

    // Phased `/plan` reviewer loop (P3e-b). If this `Done` closed a plan-driven
    // implement run and nothing else (plugin follow-up / loop iteration) claimed
    // the next turn, a write-disabled reviewer runs the code and either approves
    // or relaunches the implement run with the punch-list. See `plan_review`.
    super::plan_review::drive_plan_review(
        ctx,
        agent,
        bg_store,
        interjection_queue,
        agent_rx,
        agent_abort,
        agent_interject,
        agent_cancel,
        is_running,
    )
    .await?;

    // Phase 4: spawn background review when the
    // session is truly idle (no plugin followup,
    // loop iteration, or worktree cleanup claimed
    // the next turn). Fire-and-forget — the review
    // runs in a tokio task and never blocks the user.
    if !*is_running {
        let cwd = std::env::current_dir().unwrap_or_else(|_| ".".into());
        let paths = crate::extras::dirge_paths::ProjectPaths::new(&cwd);

        // Persist the completed turn to the SQLite
        // session DB for future search. Uses a
        // stable session id so messages from the
        // same interactive session are grouped.
        // Includes tool names + results for FTS5.
        persist_turn_to_db(
            ctx.session,
            ctx.last_user_prompt,
            &response,
            ctx.tool_calls_buf,
        );

        // dirge-a62g: prepend a deterministic, model-free ground-truth
        // digest (files touched, commands run, goal, where we stopped,
        // git diff --stat) so the review ranks/classifies KNOWN facts
        // instead of rediscovering them by re-reading the transcript.
        let base = crate::agent::review::build_transcript(ctx.session);
        let transcript =
            crate::agent::session_digest::review_transcript(ctx.session, Some(&paths.root), base);

        // dirge-ba0m: unified post-session learning orchestrator.
        // Replaces the three independent fire-and-forget spawns
        // (background review + skills curator + memory curator)
        // that used to race here. The orchestrator runs them
        // strictly in order inside ONE detached task so a skill
        // the review creates is flushed before the curator reads
        // it, and the three LLM runners never fire concurrently.
        // Still fire-and-forget — the user's turn never waits.
        crate::agent::post_session::spawn_post_session(agent.clone(), paths, transcript);
    }

    // (dirge-2qke / dirge-72ea) The post-merge cwd restore that used to
    // live here — fired unconditionally on the first Done after /wt-merge,
    // even if the merge had failed — is gone. /wt-merge now merges
    // programmatically and restores the cwd inline, only on a clean merge.

    // Drain the interjection queue once the run is fully
    // idle (no plugin follow-up, loop iteration, or worktree
    // cleanup grabbed the next turn). Concatenate all
    // queued messages as a single new user turn and kick
    // off another run against the now-stable agent/cwd.
    if !*is_running && !interjection_queue.lock().unwrap().is_empty() {
        let queued: Vec<String> = interjection_queue.lock().unwrap().drain(..).collect();
        let combined = queued.join("\n\n");
        // No write_user_lines here — the loop's
        // MessageStart{User} → AgentEvent::UserMessage
        // bridge will render the user's text once,
        // post-stripping the system-reminder block.
        // Calling write_user_lines here would
        // duplicate the render (see commit 7584bdf
        // for the original regular-input fix).

        ctx.last_user_prompt.clone_from(&combined);
        let history = crate::agent::runner::convert_history(ctx.session);
        ctx.session.add_message(MessageRole::User, &combined);

        let runner = agent.clone().spawn_runner(
            crate::agent::tools::background::prepend_pending_notifications(
                &combined,
                bg_store.as_ref(),
            ),
            history,
            Some(interjection_queue.clone()),
        );
        runner.install_into(
            agent_rx,
            agent_abort,
            agent_interject,
            agent_cancel,
            is_running,
        );
    }
    Ok(())
}