arabica-cli 0.9.0

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
//! Host composition for detached evaluation workers, shared per ARABICA_HOME.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};

use arabica_adapters::EvaluationWorker;
use arabica_session::SessionEventObserver;

static WORKERS: OnceLock<Mutex<BTreeMap<PathBuf, Arc<EvaluationWorker>>>> = OnceLock::new();

pub(crate) fn worker(home: &Path) -> Option<Arc<EvaluationWorker>> {
    let mut workers = WORKERS.get_or_init(Default::default).lock().ok()?;
    if let Some(worker) = workers.get(home) {
        return Some(worker.clone());
    }
    let config_home = home.to_path_buf();
    let worker = Arc::new(
        EvaluationWorker::start(home.to_path_buf(), 64, move || {
            crate::config::user_config_evaluation(&config_home)
                .map_err(|_| "invalid evaluation configuration".into())
        })
        .ok()?,
    );
    workers.insert(home.to_path_buf(), worker.clone());
    Some(worker)
}

pub(crate) fn attach(observers: &mut Vec<Arc<dyn SessionEventObserver>>, home: &Path) {
    if let Some(worker) = worker(home) {
        observers.push(worker);
    }
}

#[derive(clap::Subcommand, Debug)]
pub enum EvaluationAction {
    /// Freeze and inspect per-run timing and acceptance evidence for a session.
    Runs {
        session_id: String,
        #[arg(long)]
        workspace_id: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// Score immutable terminal evidence using an explicit JSON acceptance spec.
    Score {
        session_id: String,
        run_id: String,
        #[arg(long)]
        specification: PathBuf,
        #[arg(long)]
        workspace_id: Option<String>,
        #[arg(long, default_value = "evidence-checks")]
        scorer: String,
        #[arg(long)]
        json: bool,
    },
    /// Compare actual scored trials listed in a JSON manifest; never replays
    /// one policy's history as if a different policy had executed it.
    Compare {
        #[arg(long)]
        manifest: PathBuf,
        #[arg(long)]
        json: bool,
    },
    /// Read the most recently persisted report without starting an Agent.
    Show {
        session_id: String,
        /// Workspace defaults to the current directory's workspace.
        #[arg(long)]
        workspace_id: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// Inspect persisted policy versions and generated candidates.
    Policies {
        #[arg(long)]
        policy_id: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// Evaluate a session offline and generate an evolved candidate policy version if warranted.
    Evolve {
        session_id: String,
        #[arg(long)]
        workspace_id: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// Run the decoupled evaluation worker as an independent background daemon process.
    Daemon {
        #[arg(long, default_value_t = 64)]
        capacity: usize,
    },
}

pub async fn run(action: EvaluationAction) -> i32 {
    match action {
        EvaluationAction::Runs {
            session_id,
            workspace_id,
            json,
        } => run_quality(session_id, workspace_id, None, json),
        EvaluationAction::Score {
            session_id,
            run_id,
            specification,
            workspace_id,
            scorer,
            json,
        } => run_quality(
            session_id,
            workspace_id,
            Some((run_id, specification, scorer)),
            json,
        ),
        EvaluationAction::Compare { manifest, json } => run_compare(manifest, json),
        EvaluationAction::Show {
            session_id,
            workspace_id,
            json,
        } => run_show(session_id, workspace_id, json),
        EvaluationAction::Policies { policy_id, json } => run_policies(policy_id, json),
        EvaluationAction::Evolve {
            session_id,
            workspace_id,
            json,
        } => run_evolve(session_id, workspace_id, json),
        EvaluationAction::Daemon { capacity } => run_daemon(capacity).await,
    }
}

type QualityResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync>>;

fn bounded_json<T: serde::de::DeserializeOwned>(path: &Path) -> QualityResult<T> {
    use std::io::Read;
    let file = std::fs::File::open(path)?;
    let mut bytes = Vec::new();
    file.take(1024 * 1024 + 1).read_to_end(&mut bytes)?;
    if bytes.len() > 1024 * 1024 {
        return Err("evaluation input exceeds one MiB".into());
    }
    Ok(serde_json::from_slice(&bytes)?)
}

fn workspace_identity(id: Option<String>) -> QualityResult<arabica_protocol::WorkspaceId> {
    Ok(match id {
        Some(id) => arabica_protocol::WorkspaceId::new(id),
        None => crate::host::workspace_id_for(&std::env::current_dir()?),
    })
}

fn session_runs(
    home: &Path,
    workspace: &arabica_protocol::WorkspaceId,
    session: &arabica_protocol::SessionId,
    db: &mut arabica_adapters::SqliteEvaluationStore,
) -> QualityResult<Vec<arabica_runtime::RunEvaluation>> {
    use arabica_session::SessionStore;
    let stored = arabica_adapters::SqliteSessionRepository::new(home).read(workspace, session)?;
    let mut runs = Vec::new();
    for snapshot in arabica_runtime::run_evidence_snapshots(&stored.events) {
        let snapshot = db.resolve_run_snapshot(snapshot)?;
        let acceptance = db.latest_acceptance(&snapshot)?;
        runs.push(arabica_runtime::RunEvaluation {
            snapshot,
            acceptance,
        });
    }
    Ok(runs)
}

fn print_quality<T: serde::Serialize>(result: QualityResult<T>, json: bool) -> i32 {
    match result {
        Ok(value) => {
            let encoded = if json {
                serde_json::to_string(&value)
            } else {
                serde_json::to_string_pretty(&value)
            };
            match encoded {
                Ok(encoded) => {
                    println!("{encoded}");
                    0
                }
                Err(_) => {
                    eprintln!("error: could not serialize evaluation report");
                    1
                }
            }
        }
        Err(error) => {
            eprintln!("error: run evaluation failed: {error}");
            1
        }
    }
}

fn run_quality(
    session: String,
    workspace: Option<String>,
    score: Option<(String, PathBuf, String)>,
    json: bool,
) -> i32 {
    let result = (|| -> QualityResult<Vec<arabica_runtime::RunEvaluation>> {
        let home = arabica_adapters::default_arabica_home()?;
        let workspace = workspace_identity(workspace)?;
        let session = arabica_protocol::SessionId::new(session);
        let mut db =
            arabica_adapters::SqliteEvaluationStore::open(&home.join("evaluation.sqlite3"))?;
        let mut runs = session_runs(&home, &workspace, &session, &mut db)?;
        if let Some((run_id, path, scorer)) = score {
            let specification: arabica_runtime::AcceptanceSpec = bounded_json(&path)?;
            let run = runs
                .iter_mut()
                .find(|run| run.snapshot.run_id.0 == run_id)
                .ok_or("terminal run not found in this session")?;
            let registry = arabica_runtime::EvaluationRegistry::builtins(
                &arabica_runtime::EvaluationConfig::default(),
            )?;
            let acceptance = registry.acceptance(&scorer, &run.snapshot, &specification)?;
            db.save_acceptance(&run.snapshot, &specification, &acceptance)?;
            run.acceptance = Some(acceptance);
            runs.retain(|run| run.snapshot.run_id.0 == run_id);
        }
        Ok(runs)
    })();
    print_quality(result, json)
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct ComparisonManifest {
    trials: Vec<ComparisonReference>,
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct ComparisonReference {
    candidate: String,
    repetition: u32,
    workspace_id: String,
    session_id: String,
    run_id: String,
}

fn run_compare(manifest: PathBuf, json: bool) -> i32 {
    let result = (|| -> QualityResult<Vec<arabica_runtime::PolicyComparisonSummary>> {
        let manifest: ComparisonManifest = bounded_json(&manifest)?;
        let home = arabica_adapters::default_arabica_home()?;
        let mut db =
            arabica_adapters::SqliteEvaluationStore::open(&home.join("evaluation.sqlite3"))?;
        let mut trials = Vec::new();
        for reference in manifest.trials {
            let workspace = arabica_protocol::WorkspaceId::new(reference.workspace_id);
            let session = arabica_protocol::SessionId::new(reference.session_id);
            let evaluation = session_runs(&home, &workspace, &session, &mut db)?
                .into_iter()
                .find(|run| run.snapshot.run_id.0 == reference.run_id)
                .ok_or("comparison terminal run not found")?;
            trials.push(arabica_runtime::PolicyComparisonTrial {
                candidate: reference.candidate,
                repetition: reference.repetition,
                evaluation,
            });
        }
        db.save_policy_comparison(&trials)
    })();
    print_quality(result, json)
}

fn run_show(session_id: String, workspace_id: Option<String>, json: bool) -> i32 {
    let result = (|| -> Result<arabica_adapters::EvaluationView, Box<dyn std::error::Error + Send + Sync>> {
        let home = arabica_adapters::default_arabica_home()?;
        let workspace = match workspace_id {
            Some(id) => arabica_protocol::WorkspaceId::new(id),
            None => crate::host::workspace_id_for(&std::env::current_dir()?),
        };
        let session = arabica_protocol::SessionId::new(session_id);
        let report = arabica_adapters::SqliteEvaluationStore::read_latest(&home.join("evaluation.sqlite3"), &workspace, &session)?;
        let policies = arabica_adapters::SqliteEvaluationStore::read_policy_versions(&home.join("evaluation.sqlite3"), None).unwrap_or_default();
        Ok(arabica_adapters::EvaluationView { schema_version: 1, workspace_id: workspace, session_id: session, worker_status: None, refresh_accepted: None, report, policies })
    })();
    match result {
        Ok(view) => {
            if json {
                println!("{}", serde_json::to_string(&view).expect("evaluation view"));
            } else if let Some(report) = view.report {
                println!(
                    "Evaluation checkpoint: {} · context: {} v{} · model: {} v{}",
                    report.checkpoint.sequence,
                    report.context.plugin.id,
                    report.context.plugin.version,
                    report.model.plugin.id,
                    report.model.plugin.version
                );
                println!(
                    "{}",
                    serde_json::to_string_pretty(&report).expect("evaluation report")
                );
            } else {
                println!(
                    "No persisted evaluation for session {}. Agent execution is unaffected.",
                    view.session_id
                );
            }
            0
        }
        Err(_) => {
            eprintln!("error: evaluation database unavailable or incompatible");
            1
        }
    }
}

fn run_policies(policy_id: Option<String>, json: bool) -> i32 {
    let result = (|| -> Result<Vec<arabica_runtime::GeneratedPolicyRecord>, Box<dyn std::error::Error + Send + Sync>> {
        let home = arabica_adapters::default_arabica_home()?;
        let path = home.join("evaluation.sqlite3");
        arabica_adapters::SqliteEvaluationStore::read_policy_versions(&path, policy_id.as_deref())
    })();
    match result {
        Ok(policies) => {
            if json {
                println!(
                    "{}",
                    serde_json::to_string(&policies).expect("policies json")
                );
            } else if policies.is_empty() {
                println!("No policy versions recorded.");
            } else {
                for p in policies {
                    println!(
                        "Policy: {} v{} [{}] (default model: {})\n  Reason: {}\n",
                        p.policy_id, p.version, p.status, p.policy.default_model, p.reason
                    );
                }
            }
            0
        }
        Err(_) => {
            eprintln!("error: evaluation database unavailable or incompatible");
            1
        }
    }
}

fn run_evolve(session_id: String, workspace_id: Option<String>, json: bool) -> i32 {
    let result = (|| -> Result<Vec<arabica_runtime::GeneratedPolicyRecord>, Box<dyn std::error::Error + Send + Sync>> {
        let home = arabica_adapters::default_arabica_home()?;
        let workspace = match workspace_id {
            Some(id) => arabica_protocol::WorkspaceId::new(id),
            None => crate::host::workspace_id_for(&std::env::current_dir()?),
        };
        let session = arabica_protocol::SessionId::new(session_id.clone());
        let sessions = arabica_adapters::SqliteSessionRepository::new(&home);
        use arabica_session::SessionStore;
        let stored = sessions.read(&workspace, &session)?;
        let config = crate::config::user_config_evaluation(&home).unwrap_or_default();
        let evidence = arabica_runtime::EvaluationEvidence::from_history(&stored.events);
        let registry = arabica_runtime::EvaluationRegistry::builtins(&config)?;
        let model_eval = registry.model(&config.model_strategy, &evidence)?;

        let mut db = arabica_adapters::SqliteEvaluationStore::open(&home.join("evaluation.sqlite3"))?;
        let mut candidate_bases = db.list_policy_versions(None)?;
        if candidate_bases.is_empty()
            && let Ok(policies) =
                crate::config::user_config_blend_policies(&home, &std::env::current_dir()?)
        {
            for (_id, policy) in policies {
                let seed = arabica_runtime::GeneratedPolicyRecord {
                    policy_id: policy.policy_id.clone(),
                    version: policy.version,
                    status: "active".into(),
                    policy: policy.clone(),
                    basis_fingerprint: None,
                    reason: "configured base policy".into(),
                };
                let _ = db.save_policy_version(&seed);
                candidate_bases.push(seed);
            }
        }

        let mut evolved_records = Vec::new();
        for group in &model_eval.data.groups {
            for base in &candidate_bases {
                if let Some(evolved) = arabica_runtime::evolve_blend_policy(
                    &base.policy,
                    &group.report,
                    Some(evidence.fingerprint.clone()),
                ) {
                    db.save_policy_version(&evolved)?;
                    evolved_records.push(evolved);
                }
            }
        }
        Ok(evolved_records)
    })();
    match result {
        Ok(evolved_records) => {
            if json {
                println!(
                    "{}",
                    serde_json::to_string(&evolved_records).expect("evolved json")
                );
            } else if evolved_records.is_empty() {
                println!("Session evaluation complete. No policy evolution candidate generated.");
            } else {
                for rec in evolved_records {
                    println!(
                        "Generated policy candidate: {} v{} [{}]",
                        rec.policy_id, rec.version, rec.status
                    );
                    println!("  Reason: {}", rec.reason);
                }
            }
            0
        }
        Err(e) => {
            eprintln!("error: offline policy evolution failed: {e}");
            1
        }
    }
}

async fn run_daemon(capacity: usize) -> i32 {
    let home = match arabica_adapters::default_arabica_home() {
        Ok(h) => h,
        Err(_) => {
            eprintln!("error: could not resolve ARABICA_HOME");
            return 1;
        }
    };
    println!(
        "Arabica evaluation daemon running (capacity: {}, home: {})...",
        capacity,
        home.display()
    );
    if worker(&home).is_none() {
        eprintln!("error: failed to start background evaluation worker");
        return 1;
    }
    println!("Evaluation daemon active. Press Ctrl+C to stop.");
    let _ = tokio::signal::ctrl_c().await;
    println!("Evaluation daemon stopped.");
    0
}

pub(crate) fn view(
    home: &Path,
    workspace: arabica_protocol::WorkspaceId,
    session: arabica_protocol::SessionId,
    refresh: bool,
) -> arabica_adapters::EvaluationView {
    let worker = worker(home);
    let refresh_accepted = refresh.then(|| {
        worker
            .as_ref()
            .is_some_and(|worker| worker.refresh(workspace.clone(), session.clone()))
    });
    let report = worker
        .as_ref()
        .and_then(|worker| worker.latest(&workspace, &session));
    let worker_status = worker.as_ref().map(|worker| worker.status());
    let policies = arabica_adapters::SqliteEvaluationStore::read_policy_versions(
        &home.join("evaluation.sqlite3"),
        None,
    )
    .unwrap_or_default();
    arabica_adapters::EvaluationView {
        schema_version: 1,
        workspace_id: workspace,
        session_id: session,
        worker_status,
        refresh_accepted,
        report,
        policies,
    }
}