arabica-cli 0.13.1

Arabica: a terminal coding agent with a fullscreen TUI, ACP support for Zed, and one-shot runs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
//! `structure -p`: run one task non-interactively and exit.
//!
//! Unlike `arabica acp` (`crates/arabica-cli/src/acp/`), there is no
//! client on the other end to ask for permission, so this binding never
//! wires a permission gate at all: what a tool policy admits, it runs
//! outright, and what it excludes is invisible to the model. `--allow-shell`
//! and `--read-only` are the only controls, decided once at startup, not
//! per call. See `docs/runtime_core_architecture.md` Appendix B.
//!
//! Every run persists to `$ARABICA_HOME` (`arabica_adapters::SqliteSessionStore`)
//! so a later `--continue`/`--resume` has something to pick back up --
//! `arabica sessions list` (`crates/arabica-cli/src/sessions.rs`) reads
//! the same database to show what is available.

use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[cfg(test)]
use arabica_adapters::SqliteSessionStore;
use arabica_protocol::{
    Command, CommandEnvelope, CommandId, Event, EventEnvelope, SessionStatus, WorkspaceId,
};
use arabica_provider::{ApiModelProvider, ApiProviderConfig};
use arabica_runner::LocalTool;
use arabica_runtime::{BlendRoutingPolicy, RunCancellation, RunControl};
use arabica_session::{
    DispatchControl, EventVisibility, FanOutObserver, SessionEventObserver, SessionManager,
};
use arabica_session::{NewSession, SessionStore, StoredSession};

use crate::host::{HostModel, HostRuntime, LocalRunnerPolicy, workspace_id_for};

/// stdout's shape in `-p` mode.
#[derive(clap::ValueEnum, Clone, Copy, Debug, Default, Eq, PartialEq)]
#[value(rename_all = "lower")]
pub enum OutputFormat {
    /// stdout carries only the final answer; progress goes to stderr.
    #[default]
    Text,
    /// stdout carries one JSON event per line, live; system prompts and raw
    /// reasoning are never included.
    // Each line is an `EventEnvelope` for a client-visible Event, the same
    // distinction `arabica_protocol::Event::is_client_visible` draws.
    Jsonl,
}

pub struct PrintOptions {
    /// The task text, already resolved: the `-p` value, or stdin's full
    /// content if the value was `-`.
    pub task: String,
    pub output_format: OutputFormat,
    pub allow_shell: bool,
    pub read_only: bool,
    pub resume: Resume,
    /// Run built-in tools on the remote runner `[runners.<name>]`.
    pub remote_runner: Option<String>,
}

/// Which session `-p` should send `task` to.
pub enum Resume {
    /// Start a fresh session, as `-p` always did before persistence existed.
    None,
    /// `--continue`: the most recently active session in this workspace.
    Continue,
    /// `--resume <ID>`: a specific session id, looked up in this workspace.
    Id(String),
}

/// Reads the task text for `-p`: the flag's value verbatim, or all of
/// stdin (trimmed of its trailing newline) when the value is exactly `-`.
pub fn resolve_task(value: &str) -> Result<String, std::io::Error> {
    if value != "-" {
        return Ok(value.to_owned());
    }
    let mut task = String::new();
    std::io::stdin().read_to_string(&mut task)?;
    Ok(task.trim_end_matches('\n').to_owned())
}

/// The tool policy `-p` builds from its flags. `--read-only` and
/// `--allow-shell` are mutually exclusive: shell has no path confinement,
/// so allowing it while also promising "nothing changes" would make the
/// promise false, not just permissive.
pub fn tool_policy(allow_shell: bool, read_only: bool) -> Result<LocalRunnerPolicy, String> {
    match (allow_shell, read_only) {
        (true, true) => Err("--allow-shell and --read-only are mutually exclusive".to_owned()),
        (true, false) => Ok(LocalRunnerPolicy::coding().with_tool(LocalTool::Shell)),
        (false, true) => Ok(LocalRunnerPolicy::read_only()),
        (false, false) => Ok(LocalRunnerPolicy::coding()),
    }
}

/// Exit codes, matching the plan: 0 completed, 1 failed, 2 configuration
/// error, 130 cancelled (128 + SIGINT, the usual shell convention).
const EXIT_COMPLETED: i32 = 0;
const EXIT_FAILED: i32 = 1;
const EXIT_CONFIG_ERROR: i32 = 2;
const EXIT_CANCELLED: i32 = 130;

enum Outcome {
    Completed(Option<String>),
    Cancelled,
    Failed(String),
}

/// Classify one `message.send` call's Events. Deliberately coarser than
/// `acp::stop_reason::classify`: `-p` has one generic failure exit code, not
/// ACP's five-way `StopReason`, so there is no typed-evidence distinction to
/// make here.
fn classify(events: &[EventEnvelope]) -> Outcome {
    if events
        .iter()
        .any(|envelope| matches!(envelope.event, Event::RunCancelled))
    {
        return Outcome::Cancelled;
    }
    for envelope in events {
        match &envelope.event {
            Event::RunCompleted { output } => return Outcome::Completed(output.clone()),
            Event::RunFailed { message } => return Outcome::Failed(message.clone()),
            _ => {}
        }
    }
    Outcome::Failed("run ended without a terminal event".to_owned())
}

/// Writes tool-call lifecycle lines to stderr as they happen. Deliberately
/// minimal: `-p` text mode's contract is "progress on stderr, the answer
/// alone on stdout", not a full transcript.
struct TextProgressObserver;

impl SessionEventObserver for TextProgressObserver {
    fn observe(&self, envelope: &EventEnvelope, _visibility: EventVisibility) {
        match &envelope.event {
            Event::ToolCallRequested {
                name, arguments, ..
            } => eprintln!("\u{2192} {name} {arguments}"),
            Event::ToolCallCompleted { name, is_error, .. } if *is_error => {
                eprintln!("\u{2717} {name} failed")
            }
            _ => {}
        }
    }
}

/// Streams every client-visible Event to stdout as one JSON line, live.
struct JsonlObserver;

impl SessionEventObserver for JsonlObserver {
    fn observe(&self, envelope: &EventEnvelope, _visibility: EventVisibility) {
        match jsonl_line(envelope) {
            Some(Ok(line)) => println!("{line}"),
            Some(Err(error)) => eprintln!("structure -p: failed to serialize an event: {error}"),
            None => {}
        }
    }
}

/// The decision `JsonlObserver` acts on, pulled out so it is testable
/// without capturing stdout: `None` for an Event this stream must never
/// carry (`Event::is_client_visible` is false for it -- system prompts,
/// raw provider reasoning), `Some` otherwise.
fn jsonl_line(envelope: &EventEnvelope) -> Option<Result<String, serde_json::Error>> {
    if !envelope.event.is_client_visible() {
        return None;
    }
    Some(serde_json::to_string(envelope))
}

/// Runs one task to completion and returns the process exit code. Never
/// panics on an ordinary failure (provider error, run failure, cancellation)
/// -- those are reported through the return code and, in text mode, stderr
/// -- but does call `std::process::exit(130)` directly from the Ctrl-C
/// listener on a *second* interrupt, since there is no value to return from
/// mid-run at that point.
pub async fn run(provider_config: ApiProviderConfig, options: PrintOptions) -> i32 {
    let model = match ApiModelProvider::new(provider_config.clone()) {
        Ok(model) => HostModel::Api(model),
        Err(error) => {
            eprintln!("error: {error}");
            return EXIT_CONFIG_ERROR;
        }
    };
    run_with_model(model, None, options).await
}

pub async fn run_with_model(
    model: HostModel,
    blend_policy: Option<BlendRoutingPolicy>,
    options: PrintOptions,
) -> i32 {
    let policy = match tool_policy(options.allow_shell, options.read_only) {
        Ok(policy) => policy,
        Err(message) => {
            eprintln!("error: {message}");
            return EXIT_CONFIG_ERROR;
        }
    };
    let runner_root = match std::env::current_dir() {
        Ok(root) => root,
        Err(error) => {
            eprintln!("error: {error}");
            return EXIT_CONFIG_ERROR;
        }
    };
    let arabica_home = match arabica_adapters::default_arabica_home() {
        Ok(home) => home,
        Err(error) => {
            eprintln!("error: {error}");
            return EXIT_CONFIG_ERROR;
        }
    };
    let workspace_id = workspace_id_for(&runner_root);
    // MCP servers come from the user config file; a broken one is reported
    // and skipped rather than failing the task, but a malformed config is
    // a configuration error.
    let mcp = match crate::mcp::connect_configured(&runner_root, &arabica_home).await {
        Ok((mcp, diagnostics)) => {
            for diagnostic in diagnostics {
                eprintln!("structure: {diagnostic}");
            }
            mcp
        }
        Err(message) => {
            eprintln!("error: {message}");
            return EXIT_CONFIG_ERROR;
        }
    };

    // Finding what to resume is a configuration question -- "does the
    // session the user named exist" -- not a run failure, so it is answered
    // here, before there is a runtime to fail, and reported as such
    // (EXIT_CONFIG_ERROR) rather than folded into run_task's own failure
    // handling (EXIT_FAILED, for a provider or runtime problem once a run
    // is actually under way).
    let session_store = crate::host::session_store(&arabica_home);
    let resumed = match resolve_resume(&options.resume, session_store.as_ref(), &workspace_id) {
        Ok(resumed) => resumed,
        Err(message) => {
            eprintln!("error: {message}");
            return EXIT_CONFIG_ERROR;
        }
    };

    let remote = match &options.remote_runner {
        Some(name) => match crate::runner::connect_configured(&arabica_home, name, &policy).await {
            Ok(remote) => Some(remote),
            Err(message) => {
                eprintln!("error: {message}");
                return EXIT_CONFIG_ERROR;
            }
        },
        None => None,
    };

    match run_task(
        model,
        blend_policy,
        &runner_root,
        policy,
        options,
        arabica_home,
        workspace_id,
        resumed,
        session_store,
        mcp,
        remote,
    )
    .await
    {
        Ok(outcome) => report(outcome),
        Err(error) => {
            eprintln!("error: {error}");
            EXIT_FAILED
        }
    }
}

/// Resolves `--continue`/`--resume <ID>` to the stored session (if any) that
/// `run_task` should append to, without touching the runtime: a pure lookup
/// against `$ARABICA_HOME` so the "does this session exist" question is
/// directly testable without spinning up a model or a `SessionManager`.
pub(crate) fn resolve_resume(
    resume: &Resume,
    session_store: &dyn SessionStore,
    workspace_id: &WorkspaceId,
) -> Result<Option<StoredSession>, String> {
    match resume {
        Resume::None => Ok(None),
        Resume::Continue => {
            let listings = session_store
                .list(Some(workspace_id))
                .map_err(|error| error.to_string())?;
            let Some(most_recent) = listings.first() else {
                return Err("no session to continue in this workspace".to_owned());
            };
            session_store
                .read(&most_recent.header.workspace_id, &most_recent.header.id)
                .map(Some)
                .map_err(|error| error.to_string())
        }
        Resume::Id(id) => session_store
            .read(workspace_id, &arabica_protocol::SessionId::new(id.clone()))
            .map(Some)
            .map_err(|error| format!("could not resume session {id}: {error}")),
    }
}

#[allow(clippy::too_many_arguments)]
async fn run_task(
    model: HostModel,
    blend_policy: Option<BlendRoutingPolicy>,
    runner_root: &Path,
    policy: LocalRunnerPolicy,
    options: PrintOptions,
    arabica_home: PathBuf,
    workspace_id: WorkspaceId,
    resumed: Option<StoredSession>,
    session_store: Arc<dyn SessionStore>,
    mcp: crate::mcp::McpTools,
    remote: Option<arabica_runner_remote::RemoteRunner>,
) -> Result<Outcome, Box<dyn std::error::Error>> {
    let runtime: HostRuntime = crate::host::build_host_runtime_with_blend_and_runner(
        model,
        runner_root,
        policy,
        &arabica_home,
        mcp,
        remote,
        blend_policy,
    )?;
    let mut manager = SessionManager::with_ids(runtime, Box::new(crate::host::UuidIds));
    let instructions_sha256 = crate::instructions::sha256(manager.runtime().system_instructions());

    let (session_id, store) = match resumed {
        Some(stored) => {
            let session_id = stored.header.id.clone();
            let store = session_store.open(&workspace_id, &session_id)?;
            manager.restore_session(
                stored.into_snapshot(),
                CommandId::new(uuid::Uuid::now_v7().to_string()),
                Some(store.as_ref()),
            )?;
            if manager
                .session(&session_id)
                .is_some_and(|session| session.status == SessionStatus::Suspended)
            {
                let resumed = manager
                    .dispatch(
                        CommandEnvelope::new(
                            CommandId::new(uuid::Uuid::now_v7().to_string()),
                            Some(session_id.clone()),
                            Command::SessionResume,
                        ),
                        DispatchControl::default(),
                    )
                    .await?;
                for event in &resumed {
                    store.observe(event, EventVisibility::Client);
                }
            }
            (session_id, store)
        }
        None => {
            let create = CommandEnvelope::new(
                CommandId::new(uuid::Uuid::now_v7().to_string()),
                None,
                Command::SessionCreate {
                    workspace_id: workspace_id.clone(),
                },
            );
            let created = manager.dispatch(create, DispatchControl::default()).await?;
            let session_created = created
                .into_iter()
                .next()
                .expect("session.create always produces at least one Event");
            let session_id = session_created.session_id.clone();
            let store = session_store.create(NewSession {
                session_id: &session_id,
                workspace_id: &workspace_id,
                cwd: runner_root,
                profile: None,
                instructions_sha256: Some(&instructions_sha256),
            })?;
            // dispatch's own return value already has this Event; nothing
            // observed it live because the store could not exist before its
            // own session_id -- allocated inside that same dispatch call --
            // was known. Persisting it here closes that one-event gap
            // rather than leaving session.created permanently missing from
            // the file restore_session later requires it to start with.
            store.observe(&session_created, EventVisibility::Client);
            (session_id, store)
        }
    };

    let progress_observer: Arc<dyn SessionEventObserver> = match options.output_format {
        OutputFormat::Text => Arc::new(TextProgressObserver),
        OutputFormat::Jsonl => Arc::new(JsonlObserver),
    };
    let mut observers: Vec<Arc<dyn SessionEventObserver>> =
        vec![store as Arc<dyn SessionEventObserver>, progress_observer];
    crate::evaluation::attach(&mut observers, &arabica_home);
    let observer: Arc<dyn SessionEventObserver> = Arc::new(FanOutObserver::new(observers));

    let cancellation = RunCancellation::new();
    let ctrl_c = tokio::spawn({
        let cancellation = cancellation.clone();
        async move {
            loop {
                if tokio::signal::ctrl_c().await.is_err() {
                    return;
                }
                if cancellation.is_cancelled() {
                    eprintln!("structure: second interrupt, exiting immediately");
                    std::process::exit(EXIT_CANCELLED);
                }
                eprintln!("structure: interrupted, cancelling the current run...");
                cancellation.cancel();
            }
        }
    });

    let prompt = CommandEnvelope::new(
        CommandId::new(uuid::Uuid::now_v7().to_string()),
        Some(session_id),
        Command::MessageSend {
            images: Vec::new(),
            content: options.task,
        },
    );
    let control = DispatchControl {
        run: RunControl {
            cancellation: Some(cancellation),
            permissions: None,
        },
        observer: Some(observer),
    };
    let events = manager.dispatch(prompt, control).await?;
    ctrl_c.abort();
    Ok(classify(&events))
}

fn report(outcome: Outcome) -> i32 {
    match outcome {
        Outcome::Completed(output) => {
            if let Some(output) = output {
                println!("{output}");
            }
            EXIT_COMPLETED
        }
        Outcome::Cancelled => {
            eprintln!("structure: cancelled");
            EXIT_CANCELLED
        }
        Outcome::Failed(message) => {
            eprintln!("error: {message}");
            EXIT_FAILED
        }
    }
}

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

    #[derive(Debug)]
    struct ReadOnlyStore(arabica_session::SessionHeader);

    impl SessionStore for ReadOnlyStore {
        fn create(
            &self,
            _: NewSession<'_>,
        ) -> Result<Arc<dyn arabica_session::SessionWriter>, arabica_session::StoreError> {
            panic!("resume lookup must not create a writer")
        }
        fn open(
            &self,
            _: &WorkspaceId,
            _: &arabica_protocol::SessionId,
        ) -> Result<Arc<dyn arabica_session::SessionWriter>, arabica_session::StoreError> {
            panic!("resume lookup must not acquire ownership")
        }
        fn read(
            &self,
            workspace: &WorkspaceId,
            session: &arabica_protocol::SessionId,
        ) -> Result<StoredSession, arabica_session::StoreError> {
            assert_eq!(workspace, &self.0.workspace_id);
            assert_eq!(session, &self.0.id);
            Ok(StoredSession {
                header: self.0.clone(),
                events: Vec::new(),
            })
        }
        fn list(
            &self,
            workspace: Option<&WorkspaceId>,
        ) -> Result<Vec<arabica_session::SessionListing>, arabica_session::StoreError> {
            assert_eq!(workspace, Some(&self.0.workspace_id));
            Ok(vec![arabica_session::SessionListing {
                header: self.0.clone(),
                updated_at_ms: Some(42),
            }])
        }
    }

    #[test]
    fn resume_lookup_uses_an_injected_store_without_backend_paths() {
        let store = ReadOnlyStore(arabica_session::SessionHeader {
            id: arabica_protocol::SessionId::new("stored-session"),
            workspace_id: WorkspaceId::new("workspace"),
            title: Some("Stored title".to_owned()),
            cwd: "/virtual-workspace".to_owned(),
            schema: "test".to_owned(),
            created_at_ms: 42,
            protocol_version: arabica_protocol::PROTOCOL_VERSION.to_owned(),
            profile: None,
            instructions_sha256: None,
        });
        let resumed = resolve_resume(&Resume::Continue, &store, &store.0.workspace_id)
            .unwrap()
            .unwrap();
        assert_eq!(resumed.header.title.as_deref(), Some("Stored title"));
        let resumed = resolve_resume(
            &Resume::Id("stored-session".to_owned()),
            &store,
            &store.0.workspace_id,
        )
        .unwrap()
        .unwrap();
        assert_eq!(resumed.header.id, store.0.id);
    }

    #[test]
    fn a_dash_reads_the_task_from_stdin_and_anything_else_passes_through() {
        assert_eq!(resolve_task("fix the bug").unwrap(), "fix the bug");
        // "-" is exercised via the CLI-level read path, not unit-tested
        // here: swapping process stdin out from under a test is not worth
        // the fragility it would add.
    }

    #[test]
    fn allow_shell_and_read_only_together_is_a_configuration_error() {
        assert!(tool_policy(true, true).is_err());
    }

    #[test]
    fn allow_shell_adds_shell_to_the_coding_policy() {
        let policy = tool_policy(true, false).expect("valid combination");
        assert_eq!(
            policy,
            LocalRunnerPolicy::coding().with_tool(LocalTool::Shell)
        );
    }

    #[test]
    fn read_only_never_includes_shell_or_any_mutating_tool() {
        let policy = tool_policy(false, true).expect("valid combination");
        assert_eq!(policy, LocalRunnerPolicy::read_only());
    }

    #[test]
    fn the_default_policy_is_coding_without_shell() {
        let policy = tool_policy(false, false).expect("valid combination");
        assert_eq!(policy, LocalRunnerPolicy::coding());
    }

    fn envelope(event: Event) -> EventEnvelope {
        EventEnvelope {
            protocol_version: arabica_protocol::PROTOCOL_VERSION.to_owned(),
            event_id: arabica_protocol::EventId::new("event-1"),
            command_id: arabica_protocol::CommandId::new("command-1"),
            workspace_id: arabica_protocol::WorkspaceId::new("ws-1"),
            session_id: arabica_protocol::SessionId::new("session-1"),
            run_id: Some(arabica_protocol::RunId::new("run-1")),
            sequence: 1,
            occurred_at_ms: 0,
            event,
        }
    }

    #[test]
    fn classify_prefers_cancelled_over_a_terminal_completion() {
        let events = vec![
            envelope(Event::RunCompleted {
                output: Some("done".to_owned()),
            }),
            envelope(Event::RunCancelled),
        ];
        assert!(matches!(classify(&events), Outcome::Cancelled));
    }

    #[test]
    fn classify_extracts_the_completed_output() {
        let events = vec![envelope(Event::RunCompleted {
            output: Some("the answer".to_owned()),
        })];
        match classify(&events) {
            Outcome::Completed(Some(output)) => assert_eq!(output, "the answer"),
            _ => panic!("expected Completed(Some(..))"),
        }
    }

    #[test]
    fn classify_extracts_the_failure_message() {
        let events = vec![envelope(Event::RunFailed {
            message: "boom".to_owned(),
        })];
        match classify(&events) {
            Outcome::Failed(message) => assert_eq!(message, "boom"),
            _ => panic!("expected Failed"),
        }
    }

    #[test]
    fn report_maps_each_outcome_to_its_documented_exit_code() {
        assert_eq!(report(Outcome::Completed(None)), EXIT_COMPLETED);
        assert_eq!(
            report(Outcome::Completed(Some("hi".to_owned()))),
            EXIT_COMPLETED
        );
        assert_eq!(report(Outcome::Cancelled), EXIT_CANCELLED);
        assert_eq!(report(Outcome::Failed("x".to_owned())), EXIT_FAILED);
    }

    #[test]
    fn jsonl_line_is_none_for_an_internal_event_and_some_for_a_client_visible_one() {
        // model.response.item is internal (Event::is_client_visible is
        // false for it). A leaked provider exchange -- system prompt, raw
        // reasoning -- in a stream built for scripting would be a real
        // information leak, not just noise, so this exercises the actual
        // decision `JsonlObserver::observe` acts on, not a stand-in
        // assertion about `is_client_visible` in isolation.
        let internal = envelope(Event::ModelResponseItem {
            model_step: 1,
            item_index: 0,
            item: arabica_model::RuntimeItem::Reasoning(arabica_model::ReasoningItem {
                id: None,
                summary: vec!["secret reasoning".to_owned()],
                provider_state: None,
            }),
        });
        assert!(jsonl_line(&internal).is_none());

        let client_visible = envelope(Event::RunStarted);
        let line = jsonl_line(&client_visible)
            .expect("a client-visible event must produce a line")
            .expect("serialization succeeds");
        assert!(line.contains("run.started"));
    }

    fn temp_home(label: &str) -> PathBuf {
        let unique = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock is valid")
            .as_nanos();
        std::env::temp_dir().join(format!("arabica-cli-print-{label}-{unique}"))
    }

    fn seed_session(home: &Path, workspace_id: &WorkspaceId) -> arabica_protocol::SessionId {
        let session_id = arabica_protocol::SessionId::new("session-1");
        SqliteSessionStore::create(
            home,
            NewSession {
                session_id: &session_id,
                workspace_id,
                cwd: Path::new("/repo"),
                profile: None,
                instructions_sha256: None,
            },
        )
        .expect("store creates");
        session_id
    }

    #[test]
    fn resume_none_resolves_to_nothing_without_touching_the_filesystem() {
        // A arabica_home that does not exist: if this ever touched the
        // filesystem, the lookup itself would error rather than returning
        // Ok(None).
        let home = PathBuf::from("/nonexistent/does-not-exist");
        let workspace_id = WorkspaceId::new("ws-1");
        assert!(
            resolve_resume(
                &Resume::None,
                crate::host::session_store(&home).as_ref(),
                &workspace_id
            )
            .expect("None never fails")
            .is_none()
        );
    }

    #[test]
    fn continue_with_no_sessions_in_the_workspace_is_a_configuration_error() {
        let home = temp_home("continue-empty");
        let workspace_id = WorkspaceId::new("ws-1");
        std::fs::create_dir_all(&home).expect("home creates");

        let error = resolve_resume(
            &Resume::Continue,
            crate::host::session_store(&home).as_ref(),
            &workspace_id,
        )
        .expect_err("no session exists to continue");
        assert!(
            error.contains("no session to continue"),
            "unexpected message: {error}"
        );

        std::fs::remove_dir_all(&home).ok();
    }

    #[test]
    fn continue_resolves_to_the_most_recently_active_session() {
        let home = temp_home("continue-hit");
        let workspace_id = WorkspaceId::new("ws-1");
        let session_id = seed_session(&home, &workspace_id);

        let resumed = resolve_resume(
            &Resume::Continue,
            crate::host::session_store(&home).as_ref(),
            &workspace_id,
        )
        .expect("a session exists to continue")
        .expect("Continue resolves to Some when a session exists");
        assert_eq!(resumed.header.id, session_id);

        std::fs::remove_dir_all(&home).ok();
    }

    #[test]
    fn resume_by_an_unknown_id_is_a_configuration_error() {
        let home = temp_home("resume-unknown");
        let workspace_id = WorkspaceId::new("ws-1");
        std::fs::create_dir_all(&home).expect("home creates");

        let error = resolve_resume(
            &Resume::Id("no-such-session".to_owned()),
            crate::host::session_store(&home).as_ref(),
            &workspace_id,
        )
        .expect_err("the named session does not exist");
        assert!(
            error.contains("could not resume session no-such-session"),
            "unexpected message: {error}"
        );

        std::fs::remove_dir_all(&home).ok();
    }

    #[test]
    fn resume_by_a_known_id_resolves_to_that_session() {
        let home = temp_home("resume-hit");
        let workspace_id = WorkspaceId::new("ws-1");
        let session_id = seed_session(&home, &workspace_id);

        let resumed = resolve_resume(
            &Resume::Id(session_id.0.clone()),
            crate::host::session_store(&home).as_ref(),
            &workspace_id,
        )
        .expect("the named session exists")
        .expect("Id resolves to Some when the session exists");
        assert_eq!(resumed.header.id, session_id);

        std::fs::remove_dir_all(&home).ok();
    }

    #[test]
    fn resume_by_id_does_not_see_a_session_in_a_different_workspace() {
        let home = temp_home("resume-cross-workspace");
        let other_workspace = WorkspaceId::new("ws-other");
        seed_session(&home, &other_workspace);

        let this_workspace = WorkspaceId::new("ws-1");
        let error = resolve_resume(
            &Resume::Id("session-1".to_owned()),
            crate::host::session_store(&home).as_ref(),
            &this_workspace,
        )
        .expect_err("session-1 belongs to a different workspace");
        assert!(
            error.contains("could not resume session session-1"),
            "unexpected message: {error}"
        );

        std::fs::remove_dir_all(&home).ok();
    }
}