Skip to main content

harness/
harness.rs

1//! The neutral harness contract: the [`Harness`] trait, the run-control
2//! handle, the neutral request/metadata types, and the shared
3//! interactive-login helper.
4//!
5//! A *harness* is whatever actually answers the user's prompt — a CLI
6//! agent (bob / Claude Code / Codex today), a direct LLM API tomorrow,
7//! some other runner after that. A consumer only needs to: probe whether
8//! a harness is ready, run a one-time install if required, stream a run,
9//! and know which credential to ask for. This module is that seam.
10//!
11//! ## Design rules
12//!
13//! - **Object-safe trait.** Consumers hold `Box<dyn Harness>`; no
14//!   generics leak across the seam.
15//! - **Arc callbacks, not generic closures.** Streaming methods take
16//!   `Arc<dyn Fn(..) + Send + Sync>` so they stay object-safe and can be
17//!   cloned onto the reader threads the subprocess engine uses.
18//! - **Normalize at the adapter, not the UI.** The event enums in
19//!   [`crate::events`] are harness-neutral by intent; each adapter
20//!   translates its CLI's wire format into them so the front-end consumes
21//!   one shape regardless of which harness produced it.
22
23use std::path::PathBuf;
24use std::sync::{mpsc, Arc, Condvar, Mutex};
25
26use serde::{Deserialize, Serialize};
27
28use crate::events::RunEvent;
29use cli_stream::{spawn_streaming, InstallEvent, ProcessEvent, ProcessHandle};
30
31// --- Streaming callbacks --------------------------------------------
32
33/// Callback a harness invokes for each run event. `Arc<dyn Fn>` is
34/// `Clone + Send + Sync`, so it can be handed to the multiple reader
35/// threads a process-backed harness uses without the trait method
36/// needing to be generic.
37pub type RunCallback = Arc<dyn Fn(RunEvent) + Send + Sync>;
38
39/// Callback a harness invokes for each install event.
40pub type InstallCallback = Arc<dyn Fn(InstallEvent) + Send + Sync>;
41
42// --- Errors ---------------------------------------------------------
43
44/// A boxed, type-erased error source. The [`HarnessError`] variants carry one
45/// of these instead of `#[from]`-ing a single concrete type, because each
46/// *category* can be produced by more than one underlying error: a `Spawn`
47/// failure is a [`cli_stream::StreamError`] for the claude/codex adapters but a
48/// `bob_rs::BobError` for bob. The real error stays reachable through
49/// [`std::error::Error::source`] (and `downcast_ref`); the category is the
50/// variant.
51pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
52
53/// Why a [`Harness`] operation failed. Returned by `install` / `run` /
54/// `login` / [`RunControl::cancel`] so a consumer can branch on the *kind* of
55/// failure — offer install vs sign-in vs surface the message — instead of
56/// string-matching.
57///
58/// Each category carries the real underlying error as a [`source`] (via the
59/// [`BoxError`] field), so a consumer that wants more than the category can
60/// walk `.source()` or `downcast_ref::<cli_stream::StreamError>()` /
61/// `::<bob_rs::BobError>()`. The `Display` still flattens the source into the
62/// message (`"failed to start the agent: <source>"`), so a consumer that just
63/// stringifies at a boundary (e.g. a Tauri command's `.to_string()`) gets the
64/// same full message as before. `#[non_exhaustive]` so adding a variant later
65/// isn't a breaking change.
66///
67/// ```
68/// use harness::{HarnessError, StreamError};
69/// use std::error::Error;
70///
71/// // Box any typed source under a category constructor:
72/// let err = HarnessError::spawn(StreamError::PipeNotCaptured { stream: "stdout" });
73///
74/// // Stringifying at a boundary flattens the source into the message
75/// // (so a Tauri command's `.to_string()` keeps its full text)…
76/// assert!(err.to_string().starts_with("failed to start the agent: "));
77///
78/// // …while the real typed cause stays reachable for a consumer that wants
79/// // to branch on it rather than parse a string.
80/// let source = err.source().expect("Spawn carries a source");
81/// assert!(source.downcast_ref::<StreamError>().is_some());
82/// ```
83///
84/// [`source`]: std::error::Error::source
85#[derive(Debug, thiserror::Error)]
86#[non_exhaustive]
87pub enum HarnessError {
88    /// The harness's CLI couldn't be started — not installed, not on `PATH`,
89    /// or an OS-level spawn failure.
90    #[error("failed to start the agent: {0}")]
91    Spawn(#[source] BoxError),
92    /// A one-time install step failed.
93    #[error("install failed: {0}")]
94    Install(#[source] BoxError),
95    /// Interactive sign-in failed.
96    #[error("sign-in failed: {0}")]
97    Login(#[source] BoxError),
98    /// Cancelling an in-flight run failed.
99    #[error("cancel failed: {0}")]
100    Cancel(#[source] BoxError),
101    /// Any other adapter/runtime failure (e.g. a backend SDK error that
102    /// doesn't map onto the cases above). Carries a message rather than a
103    /// source — it's the catch-all when there's nothing typed to preserve.
104    #[error("{0}")]
105    Other(String),
106}
107
108impl HarnessError {
109    /// Categorize a source error as a [`Spawn`](HarnessError::Spawn) failure.
110    /// Accepts anything boxable — a typed `StreamError`/`BobError`, or a
111    /// `String`/`&str` for adapters with nothing typed to carry.
112    pub fn spawn(source: impl Into<BoxError>) -> Self {
113        Self::Spawn(source.into())
114    }
115    /// Categorize a source error as an [`Install`](HarnessError::Install) failure.
116    pub fn install(source: impl Into<BoxError>) -> Self {
117        Self::Install(source.into())
118    }
119    /// Categorize a source error as a [`Login`](HarnessError::Login) failure.
120    pub fn login(source: impl Into<BoxError>) -> Self {
121        Self::Login(source.into())
122    }
123    /// Categorize a source error as a [`Cancel`](HarnessError::Cancel) failure.
124    pub fn cancel(source: impl Into<BoxError>) -> Self {
125        Self::Cancel(source.into())
126    }
127}
128
129// --- Run control (cancellation) -------------------------------------
130
131/// Object-safe handle to an in-flight run. A process-backed harness
132/// cancels by signalling its child; a request-backed harness (a hosted
133/// LLM API) cancels by aborting its HTTP stream. The consumer only needs
134/// these two operations, so the concrete mechanism stays behind the trait.
135pub trait RunControl: Send + Sync {
136    /// Stop the run. Best-effort; idempotent.
137    fn cancel(&self) -> Result<(), HarnessError>;
138    /// Whether [`cancel`](RunControl::cancel) was called.
139    fn was_cancelled(&self) -> bool;
140}
141
142/// Boxed [`RunControl`] returned by [`Harness::run`].
143pub type RunHandle = Box<dyn RunControl>;
144
145// The engine's run handle is the canonical process-backed `RunControl`.
146// Both the trait and the handle live in this crate, so this impl is here
147// (orphan rule) rather than in any adapter crate.
148impl RunControl for ProcessHandle {
149    fn cancel(&self) -> Result<(), HarnessError> {
150        ProcessHandle::cancel(self).map_err(HarnessError::cancel)
151    }
152    fn was_cancelled(&self) -> bool {
153        ProcessHandle::was_cancelled(self)
154    }
155}
156
157// --- Neutral request / metadata shapes ------------------------------
158
159/// What the user wants the harness to do with the prompt. Mirrors
160/// the Ask / Edit split the comment bubble already exposes; adapters
161/// map it onto their own mode vocabulary.
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
163#[serde(rename_all = "snake_case")]
164pub enum RunMode {
165    /// Answer / discuss. No file edits expected.
166    Ask,
167    /// Propose edits to the workspace.
168    Edit,
169}
170
171/// How hard the model should think, in harness-neutral terms. Codex
172/// maps this onto `model_reasoning_effort`; Claude Code has no
173/// equivalent `-p` flag today and ignores it. Kept neutral so a future
174/// harness that exposes effort can honor the same field.
175#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
176#[serde(rename_all = "snake_case")]
177pub enum ReasoningEffort {
178    Minimal,
179    Low,
180    Medium,
181    High,
182}
183
184impl ReasoningEffort {
185    /// The CLI/config token for this level (e.g. codex's
186    /// `model_reasoning_effort="high"`).
187    pub fn as_cli_value(self) -> &'static str {
188        match self {
189            ReasoningEffort::Minimal => "minimal",
190            ReasoningEffort::Low => "low",
191            ReasoningEffort::Medium => "medium",
192            ReasoningEffort::High => "high",
193        }
194    }
195}
196
197/// User-chosen, harness-neutral run-shaping knobs. Every field is
198/// optional; each adapter maps the ones its CLI supports and ignores
199/// the rest (Claude has no reasoning-effort flag; Codex has no
200/// max-turns flag). Grouped into one struct so the neutral
201/// [`RunRequest`] stays open for extension — a new knob is a field
202/// here, not a new positional parameter threaded through every caller.
203#[derive(Debug, Clone, Default)]
204pub struct RunTuning {
205    /// Model id or alias passed verbatim to the CLI (`--model` /
206    /// `-m`). `None` → let the CLI use its configured default.
207    pub model: Option<String>,
208    /// Reasoning effort (Codex: `-c model_reasoning_effort`).
209    pub effort: Option<ReasoningEffort>,
210    /// Cap on agentic turns (Claude: `--max-turns`).
211    pub max_turns: Option<u32>,
212}
213
214/// A harness-neutral run request. Adapter-specific knobs (bob's
215/// approval mode, coin budget, executable override) are filled in by
216/// the adapter from its own defaults; the user-facing tuning the
217/// picker exposes (model, effort, turn cap) rides on `tuning`.
218#[derive(Debug, Clone)]
219pub struct RunRequest {
220    /// Caller-chosen id used to correlate events with the handle.
221    pub run_id: String,
222    pub prompt: String,
223    /// Working directory for the run — the workspace path, so the
224    /// harness's tool calls land inside the user's vault.
225    pub cwd: Option<PathBuf>,
226    pub mode: RunMode,
227    /// Optional, harness-neutral run-shaping knobs (model, effort,
228    /// turn cap). Adapters honor the subset their CLI supports.
229    pub tuning: RunTuning,
230}
231
232/// Where a harness's secret lives in the OS keychain, and how to
233/// label it in the UI. Lets the front-end ask for the right
234/// credential per harness without hard-coding any one harness's slot.
235#[derive(Debug, Clone, Serialize)]
236#[serde(rename_all = "camelCase")]
237pub struct CredentialSpec {
238    /// Human label, e.g. "Bob API key" / "Anthropic API key".
239    pub label: String,
240    pub keychain_service: String,
241    pub keychain_account: String,
242    /// Whether the harness can run at all without this credential.
243    pub required: bool,
244}
245
246/// Harness-neutral readiness snapshot for the UI. `details` carries
247/// adapter-specific probes (bob's Node/npm) as free-form JSON so the
248/// trait stays generic.
249#[derive(Debug, Clone, Serialize)]
250#[serde(rename_all = "camelCase")]
251pub struct HarnessReadiness {
252    pub harness_id: String,
253    /// Installed *and* authenticated *and* able to run.
254    pub ready: bool,
255    pub installed: bool,
256    pub version: Option<String>,
257    pub auth_configured: bool,
258    pub error: Option<String>,
259    /// Adapter-specific extra fields (serialized harness snapshot).
260    pub details: serde_json::Value,
261}
262
263/// A model the harness can be pointed at, for the picker's model
264/// selector. `value` is passed verbatim to the CLI (`--model` / `-m`)
265/// via [`RunTuning::model`]; `label` is the human-facing name.
266#[derive(Debug, Clone, Serialize)]
267#[serde(rename_all = "camelCase")]
268pub struct HarnessModel {
269    pub value: String,
270    pub label: String,
271}
272
273/// What a harness supports, so every consumer (the picker, the options
274/// panel, the credential preflight, the chat availability gate) adapts
275/// to it *declaratively* instead of branching on the harness id. A new
276/// adapter that, say, needs a stored key just sets `credential_required:
277/// true` here — no `id == "bob"` checks to hunt down.
278#[derive(Debug, Clone, Serialize)]
279#[serde(rename_all = "camelCase")]
280pub struct HarnessCapabilities {
281    /// Compose stores this harness's credential (bob). When `false`,
282    /// the CLI owns its own login (claude/codex) and Compose runs no
283    /// credential/install preflight — a missing login surfaces as the
284    /// harness's own run error rather than a Compose prompt.
285    pub credential_required: bool,
286    /// Emits previewable suggested edits the user approves before they
287    /// apply (bob). When `false`, edits land on disk directly and the
288    /// file watcher reflects them (claude/codex).
289    pub previews_edits: bool,
290    /// Curated model choices for the picker's selector. Empty → no
291    /// curated list (rely on `allows_custom_model`).
292    pub models: Vec<HarnessModel>,
293    /// Whether a free-text model id is accepted beyond `models` (codex,
294    /// whose model names change frequently). Drives a text field vs a
295    /// fixed dropdown in the picker.
296    pub allows_custom_model: bool,
297    /// Honors [`RunTuning::effort`] (codex reasoning effort).
298    pub supports_effort: bool,
299    /// Honors [`RunTuning::max_turns`] (claude turn cap).
300    pub supports_max_turns: bool,
301    /// Supports an interactive [`Harness::login`] flow (the CLI's own
302    /// OAuth, e.g. `claude auth login` / `codex login`). Drives the
303    /// picker's "Sign in" affordance when installed-but-not-signed-in.
304    /// `false` for harnesses Compose authenticates itself (bob).
305    pub supports_login: bool,
306}
307
308/// Static metadata for the harness picker.
309#[derive(Debug, Clone, Serialize)]
310#[serde(rename_all = "camelCase")]
311pub struct HarnessInfo {
312    pub id: String,
313    pub display_name: String,
314    pub description: String,
315    /// True if the harness needs a one-time [`Harness::install`].
316    pub requires_install: bool,
317    /// Declarative capabilities — what the harness supports, so the UI
318    /// and run-gating never special-case its id.
319    pub capabilities: HarnessCapabilities,
320}
321
322// --- The trait ------------------------------------------------------
323
324/// A pluggable agent backend. Implementors are cheap to construct
325/// (they hold config, not connections) so a registry can hand out
326/// fresh boxes on demand.
327pub trait Harness: Send + Sync {
328    /// Static metadata for the UI.
329    fn info(&self) -> HarnessInfo;
330
331    /// Probe availability / version / auth. May shell out; callers
332    /// should treat it as blocking and run it off the UI thread.
333    fn readiness(&self) -> HarnessReadiness;
334
335    /// Stream a one-time install. Harnesses that need no install
336    /// (e.g. a hosted-API adapter) return `Ok(())` immediately.
337    fn install(&self, on_event: InstallCallback) -> Result<(), HarnessError>;
338
339    /// Start a run, streaming events through `on_event`. Returns a
340    /// handle immediately; work continues on background threads.
341    fn run(&self, request: RunRequest, on_event: RunCallback) -> Result<RunHandle, HarnessError>;
342
343    /// The credential this harness needs.
344    fn credential(&self) -> CredentialSpec;
345
346    /// Trigger the harness's own interactive sign-in (its CLI's OAuth),
347    /// streaming progress as [`InstallEvent`]s — the same subprocess
348    /// stream shape as [`install`](Harness::install). The flow opens the
349    /// user's browser; this blocks until the login process exits, then
350    /// `Done { ok }` reports success. Default: unsupported — harnesses
351    /// that Compose authenticates itself (bob, via its API key) keep it.
352    fn login(&self, _on_event: InstallCallback) -> Result<(), HarnessError> {
353        Err(HarnessError::login(
354            "This harness does not support interactive sign-in.",
355        ))
356    }
357
358    /// Convenience over [`run`](Harness::run) for callers that want to
359    /// *pull* events off a channel instead of supplying a push callback.
360    /// Forwards each [`RunEvent`] into an `mpsc` channel and hands the
361    /// receiver back alongside the run handle, so the caller can simply
362    /// `for event in rx { … }` rather than re-write the
363    /// `Arc::new(move |ev| tx.send(ev))` plumbing at every call site.
364    ///
365    /// The receiver hangs up when the run ends — and on its own, without
366    /// the caller dropping the [`RunHandle`] first. The forwarding callback
367    /// (and the `Sender` it owns) lives only on the engine's reader
368    /// threads; once the process exits and those threads finish, every
369    /// clone of the callback drops, the `Sender` drops, and the `for` loop
370    /// over `rx` terminates. (Dropping the handle never cancels a run — see
371    /// [`RunControl`] — so it is safe to drain `rx` to completion while
372    /// still holding the handle for a possible [`cancel`](RunControl::cancel).)
373    ///
374    /// Prefer [`run`](Harness::run) directly when you need push semantics —
375    /// e.g. forwarding straight onto a Tauri `Channel` or an SSE sink from
376    /// inside the callback — where an intermediate channel is just an extra
377    /// hop. This is a provided method (not overridable surface): adapters
378    /// implement only `run`, and every harness — built-in or third-party —
379    /// gets `run_channel` for free.
380    ///
381    /// ```no_run
382    /// use harness::{Claude, Harness, RunEvent, RunMode, RunRequest, RunTuning};
383    ///
384    /// # fn main() -> Result<(), harness::HarnessError> {
385    /// let (_handle, rx) = Claude::new().run_channel(RunRequest {
386    ///     run_id: "demo".into(),
387    ///     prompt: "Explain Markdown headings in one sentence.".into(),
388    ///     cwd: None,
389    ///     mode: RunMode::Ask,
390    ///     tuning: RunTuning::default(),
391    /// })?;
392    /// for event in rx {
393    ///     match event {
394    ///         RunEvent::Text { delta, .. } => print!("{delta}"),
395    ///         RunEvent::Exited { .. } => break,
396    ///         _ => {}
397    ///     }
398    /// }
399    /// # Ok(())
400    /// # }
401    /// ```
402    fn run_channel(
403        &self,
404        request: RunRequest,
405    ) -> Result<(RunHandle, mpsc::Receiver<RunEvent>), HarnessError> {
406        let (tx, rx) = mpsc::channel();
407        let handle = self.run(
408            request,
409            Arc::new(move |event| {
410                // A hung-up receiver (consumer stopped early) is not an
411                // error: the run keeps streaming; we just drop the event
412                // nobody is waiting for.
413                let _ = tx.send(event);
414            }),
415        )?;
416        Ok((handle, rx))
417    }
418}
419
420/// Run a harness's interactive sign-in command, streaming its output as
421/// [`InstallEvent`]s and blocking until it exits. Reuses
422/// [`spawn_streaming`] (PATH augmentation + reader threads, so a packaged
423/// `.app` finds the CLI), mapping its process events onto the
424/// install-stream shape (Step / Stdout / Stderr / Done). The login CLI
425/// opens the user's browser for OAuth; we surface its output (incl. any
426/// device-code URL) so the UI can show progress. Blocks on a condvar
427/// until the process exits — the caller is a Tauri `(async)` command on
428/// a worker thread, so the UI never blocks.
429pub fn run_login_command(
430    program: &str,
431    args: &[&str],
432    on_event: InstallCallback,
433) -> Result<(), HarnessError> {
434    (*on_event)(InstallEvent::Step {
435        text: "Opening your browser to sign in…".to_owned(),
436    });
437    let done = Arc::new((Mutex::new(false), Condvar::new()));
438    let done_cb = Arc::clone(&done);
439    let events_cb = Arc::clone(&on_event);
440    // Bound, not `_`, so the handle outlives the wait (dropping it could
441    // signal the child); by the time we return, the process has exited.
442    let _handle = spawn_streaming(
443        PathBuf::from(program),
444        args.iter().map(|s| (*s).to_owned()).collect(),
445        Vec::new(),
446        std::env::current_dir().unwrap_or_default(),
447        format!("login-{program}"),
448        move |event| match event {
449            ProcessEvent::Started { .. } => {}
450            ProcessEvent::Stdout { line, .. } => {
451                (*events_cb)(InstallEvent::Stdout { text: line });
452            }
453            ProcessEvent::Stderr { line, .. } => {
454                (*events_cb)(InstallEvent::Stderr { text: line });
455            }
456            ProcessEvent::Error { message, .. } => {
457                (*events_cb)(InstallEvent::Stderr { text: message });
458            }
459            ProcessEvent::Exited { exit_code, .. } => {
460                (*events_cb)(InstallEvent::Done {
461                    exit_code,
462                    ok: exit_code == Some(0),
463                });
464                let (lock, cvar) = &*done_cb;
465                // Recover from a poisoned lock instead of panicking on a
466                // reader thread: the guarded value is a plain bool, never in a
467                // half-updated state worth bailing on.
468                *lock.lock().unwrap_or_else(|p| p.into_inner()) = true;
469                cvar.notify_all();
470            }
471            // `ProcessEvent` is #[non_exhaustive]; ignore any future variant.
472            _ => {}
473        },
474    )
475    .map_err(HarnessError::login)?;
476    let (lock, cvar) = &*done;
477    let mut finished = lock.lock().unwrap_or_else(|p| p.into_inner());
478    while !*finished {
479        finished = cvar.wait(finished).unwrap_or_else(|p| p.into_inner());
480    }
481    Ok(())
482}
483
484/// Whether an API-key value an adapter pulled from the environment counts as
485/// authenticated — i.e. present and non-blank. Adapters OR this into their
486/// [`Harness::readiness`] so a key in the env (headless / CI / container)
487/// reports authenticated, not only the CLI's own interactive OAuth login —
488/// which can't complete where there's no browser. Pure (the env read stays at
489/// the call site) so it's unit-tested directly.
490///
491/// Only the claude/codex adapters OR this into readiness — bob reports auth via
492/// `bob-rs`'s own keychain source — so it's gated to those features. Without
493/// them (`--no-default-features`) it would be dead code, hence the `cfg`.
494#[cfg(any(feature = "claude", feature = "codex"))]
495pub(crate) fn api_key_value_usable(value: Option<String>) -> bool {
496    matches!(value, Some(v) if !v.trim().is_empty())
497}
498
499#[cfg(test)]
500mod tests {
501    use super::*;
502
503    // Gated like the fn it tests — `api_key_value_usable` only exists when a
504    // claude/codex adapter is compiled in.
505    #[cfg(any(feature = "claude", feature = "codex"))]
506    #[test]
507    fn api_key_value_usable_requires_a_nonblank_value() {
508        assert!(api_key_value_usable(Some("sk-abc".to_owned())));
509        assert!(!api_key_value_usable(Some(String::new())));
510        assert!(!api_key_value_usable(Some("   ".to_owned())));
511        assert!(!api_key_value_usable(None));
512    }
513
514    /// A no-op [`RunControl`] so the mock harness below can hand back a
515    /// [`RunHandle`] without a real process behind it.
516    struct NoopControl;
517    impl RunControl for NoopControl {
518        fn cancel(&self) -> Result<(), HarnessError> {
519            Ok(())
520        }
521        fn was_cancelled(&self) -> bool {
522            false
523        }
524    }
525
526    /// A minimal in-memory harness whose `run()` pushes a fixed event
527    /// sequence straight to the callback, synchronously, then returns —
528    /// dropping its only `RunCallback` clone. That's exactly the ownership
529    /// shape `run_channel` relies on, with no subprocess to spawn, so it
530    /// pins down the contract: events are forwarded, and the receiver hangs
531    /// up on its own once the run's callback ownership ends.
532    struct MockHarness {
533        events: Vec<RunEvent>,
534    }
535    impl Harness for MockHarness {
536        fn info(&self) -> HarnessInfo {
537            unreachable!("not exercised by run_channel")
538        }
539        fn readiness(&self) -> HarnessReadiness {
540            unreachable!("not exercised by run_channel")
541        }
542        fn install(&self, _on_event: InstallCallback) -> Result<(), HarnessError> {
543            Ok(())
544        }
545        fn run(
546            &self,
547            _request: RunRequest,
548            on_event: RunCallback,
549        ) -> Result<RunHandle, HarnessError> {
550            for event in &self.events {
551                on_event(event.clone());
552            }
553            // `on_event` (the lone RunCallback clone, owning the channel's
554            // Sender) drops as this returns → the receiver closes.
555            Ok(Box::new(NoopControl))
556        }
557        fn credential(&self) -> CredentialSpec {
558            unreachable!("not exercised by run_channel")
559        }
560    }
561
562    fn demo_request() -> RunRequest {
563        RunRequest {
564            run_id: "t".to_owned(),
565            prompt: "hi".to_owned(),
566            cwd: None,
567            mode: RunMode::Ask,
568            tuning: RunTuning::default(),
569        }
570    }
571
572    #[test]
573    fn run_channel_forwards_every_event_then_closes() {
574        let harness = MockHarness {
575            events: vec![
576                RunEvent::Text {
577                    run_id: "t".to_owned(),
578                    delta: "hello".to_owned(),
579                },
580                RunEvent::Exited {
581                    run_id: "t".to_owned(),
582                    exit_code: Some(0),
583                    cancelled: false,
584                },
585            ],
586        };
587        let (_handle, rx) = harness.run_channel(demo_request()).expect("run_channel ok");
588        // Draining to completion *terminates* — proof the channel closed
589        // without us dropping the handle.
590        let collected: Vec<RunEvent> = rx.into_iter().collect();
591        assert_eq!(
592            collected,
593            vec![
594                RunEvent::Text {
595                    run_id: "t".to_owned(),
596                    delta: "hello".to_owned(),
597                },
598                RunEvent::Exited {
599                    run_id: "t".to_owned(),
600                    exit_code: Some(0),
601                    cancelled: false,
602                },
603            ]
604        );
605    }
606
607    #[test]
608    fn run_channel_receiver_closes_even_with_no_events() {
609        let harness = MockHarness { events: Vec::new() };
610        let (_handle, rx) = harness.run_channel(demo_request()).expect("run_channel ok");
611        assert_eq!(rx.into_iter().count(), 0); // closes immediately, doesn't hang
612    }
613
614    #[test]
615    fn harness_error_preserves_typed_source_and_flattened_message() {
616        use std::error::Error;
617
618        // Categorize a real typed engine error as a Spawn failure.
619        let err = HarnessError::spawn(cli_stream::StreamError::PipeNotCaptured { stream: "stdout" });
620
621        // Display still flattens the source into the message, so a consumer
622        // that just `.to_string()`s at a boundary (a Tauri command) gets the
623        // category prefix *and* the full underlying detail — unchanged from
624        // when the variant held a String.
625        let message = err.to_string();
626        assert!(message.starts_with("failed to start the agent: "), "got {message:?}");
627        assert!(message.contains("stdout pipe was not captured"), "got {message:?}");
628
629        // And the real typed error is reachable via the source chain — the
630        // whole point of carrying a source instead of a flattened string.
631        let source = err.source().expect("HarnessError::Spawn has a source");
632        assert!(
633            source.downcast_ref::<cli_stream::StreamError>().is_some(),
634            "source should downcast back to the typed StreamError"
635        );
636    }
637}