diff --git a/crates/gaviero-cli/CLAUDE.md b/crates/gaviero-cli/CLAUDE.md index 67a5966e..2d939766 100644 --- a/crates/gaviero-cli/CLAUDE.md +++ b/crates/gaviero-cli/CLAUDE.md @@ -37,6 +37,7 @@ Full user-facing flag tables: [README.md](README.md). Do not duplicate every fie ## Conventions - **stdout = results, stderr = telemetry.** Observers always log to stderr. +- **Every `execute` run is published for the phone app.** `start_live_run` creates a core [`live_run`](../gaviero-core/src/swarm/live_run.rs) record under `~/.gaviero/remote/cli-runs//`, and both observers feed it (`LiveRunHandle`, flushed at most once a second, heartbeat every 30 s, finished on `execute`'s result). `--coordinated` planning is not published. `GAVIERO_CLI_RUNS_DIR` moves the folder; integration tests must set it so they never write into the user's list. - **Model spec:** `provider:model` required. Accepted prefixes include `claude:`, `codex:`, `cursor:`, `ollama:`, `local:`, `deepseek:`, `dsh:` ([`validate_model_spec`](../gaviero-core/src/swarm/backend/shared.rs)). Default: workspace `agent.model`, then `claude:sonnet`. - **`--repo` vs `--workspace`:** `execution repo` vs `execution document` ([`workflow_execution_mode`](../gaviero-dsl/src/lib.rs)). Conflicts with each other; `--workspace` defaults to the plan file's directory when `--var PLAN_FILE=...` is set. - **DSL precedence.** Models: `--profile` (else the profile named `default`) says what each tier means; an agent's tier is `--tier AGENT=TIER` > the workflow's `tiers { }` > its own `tier`; `--tier-client TIER=CLIENT` points a tier at another client of the same profile and `--tier-effort TIER=EFFORT` changes its effort, for one run (merged per tier by `gaviero_dsl::TierOverride::from_pairs`). Vars: agent-level > `--var` > script-level. Params: `--param` (see [`workflow_params`](../gaviero-dsl/src/workflow_params.rs)). diff --git a/crates/gaviero-cli/src/main.rs b/crates/gaviero-cli/src/main.rs index 1a5cdb8d..c7e3cf13 100644 --- a/crates/gaviero-cli/src/main.rs +++ b/crates/gaviero-cli/src/main.rs @@ -8,6 +8,7 @@ use anyhow::{Context, Result}; use clap::Parser; use gaviero_core::observer::{AcpObserver, SwarmObserver}; +use gaviero_core::swarm::live_run::{CliRunStore, LiveRun, LiveRunHandle, LiveUpdate, PlannedAgent}; use gaviero_core::script_profiles::ProfileCatalog; use gaviero_core::scripts::ScriptCatalog; use gaviero_core::swarm::models::{AgentStatus, SwarmResult, WorkUnit}; @@ -696,6 +697,8 @@ struct CliAcpObserver { agent_id: String, verbose: bool, stream_buffer: Mutex, + /// The run's published record (`swarm::live_run`), for the phone app. + live: Option, } impl CliAcpObserver { @@ -704,6 +707,18 @@ impl CliAcpObserver { agent_id: agent_id.into(), verbose, stream_buffer: Mutex::new(String::new()), + live: None, + } + } + + fn publishing(mut self, live: Option) -> Self { + self.live = live; + self + } + + fn publish(&self, update: impl FnOnce(String) -> LiveUpdate) { + if let Some(live) = &self.live { + live.update(update(self.agent_id.clone())); } } @@ -725,6 +740,10 @@ impl CliAcpObserver { impl AcpObserver for CliAcpObserver { fn on_stream_chunk(&self, text: &str) { + self.publish(|id| LiveUpdate::Text { + id, + text: text.to_string(), + }); if !self.verbose { return; } @@ -736,6 +755,10 @@ impl AcpObserver for CliAcpObserver { } fn on_tool_call_started(&self, tool_summary: &str) { + self.publish(|id| LiveUpdate::Tool { + id, + summary: tool_summary.to_string(), + }); self.flush_stream_lines(true); eprintln!(" [{}] ⚙ {}", self.agent_id, tool_summary); } @@ -744,6 +767,10 @@ impl AcpObserver for CliAcpObserver { if role != "assistant" { return; } + self.publish(|id| LiveUpdate::Message { + id, + content: content.to_string(), + }); self.flush_stream_lines(true); if self.verbose { return; @@ -760,14 +787,26 @@ impl AcpObserver for CliAcpObserver { fn on_proposal_deferred( &self, path: &std::path::Path, - _old_content: Option<&str>, - _new_content: &str, + old_content: Option<&str>, + new_content: &str, ) { + let old_lines = old_content.map(|s| s.lines().count()).unwrap_or(0); + let new_lines = new_content.lines().count(); + self.publish(|id| LiveUpdate::File { + id, + path: path.display().to_string(), + additions: new_lines.saturating_sub(old_lines), + deletions: old_lines.saturating_sub(new_lines), + }); self.flush_stream_lines(true); eprintln!(" [{}] ✎ {}", self.agent_id, path.display()); } fn on_streaming_status(&self, status: &str) { + self.publish(|id| LiveUpdate::Status { + id, + status: status.to_string(), + }); self.flush_stream_lines(true); eprintln!(" [{}] … {}", self.agent_id, status); } @@ -792,15 +831,33 @@ impl AcpObserver for CliAcpObserver { } } -/// CLI observer for swarm events. -struct CliSwarmObserver; +/// CLI observer for swarm events. With `live`, every event also goes into +/// the run's published record (`swarm::live_run`) the phone app reads. +#[derive(Default)] +struct CliSwarmObserver { + live: Option, +} + +impl CliSwarmObserver { + fn publish(&self, update: LiveUpdate) { + if let Some(live) = &self.live { + live.update(update); + } + } +} impl SwarmObserver for CliSwarmObserver { fn on_phase_changed(&self, phase: &str) { + self.publish(LiveUpdate::Phase(phase.to_string())); eprintln!("[phase] {}", phase); } fn on_agent_state_changed(&self, work_unit_id: &str, status: &AgentStatus, detail: &str) { + self.publish(LiveUpdate::Agent { + id: work_unit_id.to_string(), + status: status.clone(), + detail: detail.to_string(), + }); match status { AgentStatus::Running => { if detail.is_empty() { @@ -827,10 +884,15 @@ impl SwarmObserver for CliSwarmObserver { } fn on_tier_started(&self, current: usize, total: usize) { + self.publish(LiveUpdate::Tier { current, total }); eprintln!("[tier] {}/{}", current, total); } fn on_merge_conflict(&self, branch: &str, files: &[String]) { + self.publish(LiveUpdate::Notice(format!( + "Merge conflict in {branch}: {}", + files.join(", ") + ))); eprintln!("[conflict] branch={} files={}", branch, files.join(", ")); } @@ -864,6 +926,11 @@ impl SwarmObserver for CliSwarmObserver { } fn on_tier_dispatch(&self, unit_id: &str, tier: gaviero_core::types::ModelTier, backend: &str) { + self.publish(LiveUpdate::Dispatch { + id: unit_id.to_string(), + tier, + backend: backend.to_string(), + }); eprintln!( "[dispatch] {} → {} ({})", unit_id, @@ -896,6 +963,7 @@ impl SwarmObserver for CliSwarmObserver { } fn on_loop_iteration_started(&self, current: u32, max: u32, agents: &[String]) { + self.publish(LiveUpdate::Notice(format!("Loop pass {current}/{max}"))); eprintln!( "[loop] iteration {}/{} agents=[{}]", current, @@ -923,6 +991,12 @@ impl SwarmObserver for CliSwarmObserver { } fn on_loop_gate_failed(&self, probe: &str, status: &str, output: &str) { + let diagnostic = output.lines().next().unwrap_or_default().trim(); + self.publish(LiveUpdate::Notice(if diagnostic.is_empty() { + format!("Loop gate failed: {probe} ({status})") + } else { + format!("Loop gate failed: {probe} ({status}) - {diagnostic}") + })); eprintln!("[loop] gate FAIL - {} ({})", probe, status); if !output.trim().is_empty() { eprintln!("{}", output.trim_end()); @@ -930,6 +1004,7 @@ impl SwarmObserver for CliSwarmObserver { } fn on_cost_update(&self, estimate: &gaviero_core::swarm::verify::CostEstimate) { + self.publish(LiveUpdate::Cost(estimate.estimated_usd)); eprintln!("[cost] ~${:.4}", estimate.estimated_usd); } } @@ -4735,7 +4810,7 @@ async fn main() -> Result<()> { } // Execute via swarm pipeline - let swarm_observer = CliSwarmObserver; + let swarm_observer = CliSwarmObserver::default(); let specificity = workspace.resolve_specificity_config(Some(&repo)); let (swarm_extra_tools, _) = workspace.resolve_agent_tools(Some(&repo)); // Shell policy from the workspace cascade; agents run in worktrees @@ -4908,6 +4983,11 @@ async fn main() -> Result<()> { None }; + // Publish the run for the phone app (any TUI with remote on this machine + // serves `~/.gaviero/remote/cli-runs`). + let live = start_live_run(&cli, &plan, &repo); + let flusher = live.as_ref().map(LiveRunHandle::spawn_flusher); + let swarm_observer = CliSwarmObserver { live: live.clone() }; let result = gaviero_core::swarm::pipeline::execute( &plan, &config, @@ -4916,11 +4996,22 @@ async fn main() -> Result<()> { &swarm_observer, |agent_id| { let verbose = cli.script.is_none(); - Box::new(CliAcpObserver::new(agent_id, verbose)) + Box::new(CliAcpObserver::new(agent_id, verbose).publishing(live.clone())) as Box }, ) - .await?; + .await; + if let Some(flusher) = flusher { + flusher.stop().await; + } + if let Some(live) = live { + let outcome = match &result { + Ok(result) => LiveUpdate::Completed(Box::new(result.clone())), + Err(e) => LiveUpdate::Failed(format!("{e:#}")), + }; + let _ = tokio::task::spawn_blocking(move || live.finish(outcome)).await; + } + let result = result?; // Drain the extractor writer before the runtime shuts down, so in-flight // swarm-finding extraction (slow LLM calls enqueued right up to the end @@ -4988,6 +5079,51 @@ async fn main() -> Result<()> { } } +/// Start the run's published record (`swarm::live_run`): every planned agent +/// queued, labelled like the SWARM panel labels a run. None when there is no +/// machine folder; a write failure only logs. +fn start_live_run( + cli: &Cli, + plan: &gaviero_core::swarm::plan::CompiledPlan, + run_root: &std::path::Path, +) -> Option { + let store = CliRunStore::machine_default()?; + let stem = |p: &std::path::Path| { + p.file_stem() + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or_else(|| p.display().to_string()) + }; + let mut script = if let Some(path) = &cli.script { + stem(path) + } else if let Some(path) = &cli.plan { + stem(path) + } else if cli.work_units.is_some() { + "work units".to_string() + } else { + "task".to_string() + }; + if let Some(workflow) = &cli.workflow { + script = format!("{script} · {workflow}"); + } + let folder = run_root + .file_name() + .map(|n| n.to_string_lossy().to_string()) + .unwrap_or_default(); + let planned = plan + .work_units_ordered() + .unwrap_or_default() + .iter() + .chain(plan.loop_judge_units.iter()) + .map(|unit| PlannedAgent { + id: unit.id.clone(), + tier: unit.tier, + model: unit.model.clone(), + }) + .collect(); + let run = LiveRun::new(CliRunStore::new_run_id(), script, folder, planned); + Some(LiveRunHandle::start(store, run)) +} + /// Build an extraction-LLM-equipped memory writer for the headless swarm /// (PR-6-replacement, scope-B follow-up). Mirrors the TUI: resolve /// `memory.extractor.model` (falling back to the agent model), build the diff --git a/crates/gaviero-cli/tests/state_modes_cli.rs b/crates/gaviero-cli/tests/state_modes_cli.rs index 3a954f22..27d27773 100644 --- a/crates/gaviero-cli/tests/state_modes_cli.rs +++ b/crates/gaviero-cli/tests/state_modes_cli.rs @@ -131,6 +131,7 @@ fn run(script: &Path, workspace: &Path, extra: &[&str]) -> Output { .args(extra) // Covers the isolated run with no settings to read. .env("GAVIERO_EMBEDDER_MODEL", "null") + .env("GAVIERO_CLI_RUNS_DIR", shared_cli_runs_dir()) .output() .expect("spawn gaviero-cli") } @@ -159,8 +160,18 @@ fn enclosed_by_a_workspace(dir: &Path) -> bool { .any(|d| Some(d) != home.as_deref() && has_workspace_marker(d)) } +/// Where test runs publish their live record (`swarm::live_run`) — never +/// the user's `~/.gaviero/remote/cli-runs`, which the phone lists. +fn shared_cli_runs_dir() -> PathBuf { + std::env::temp_dir().join("gaviero-cli-runs-tests") +} + /// `gaviero-cli --script` started from `cwd`, with no root flags. fn run_from(cwd: &Path, script: &Path) -> Output { + run_from_publishing(cwd, script, &shared_cli_runs_dir()) +} + +fn run_from_publishing(cwd: &Path, script: &Path, cli_runs: &Path) -> Output { Command::new(gaviero_cli()) .current_dir(cwd) .arg("--script") @@ -172,6 +183,7 @@ fn run_from(cwd: &Path, script: &Path) -> Output { .args(["--model", "ollama:qwen", "--no-mcp"]) .args(["--skip-mcp-preflight", "--run-timeout", "120"]) .env("GAVIERO_EMBEDDER_MODEL", "null") + .env("GAVIERO_CLI_RUNS_DIR", cli_runs) .output() .expect("spawn gaviero-cli") } @@ -224,6 +236,39 @@ fn a_run_from_the_workspace_folder_uses_its_memory_and_graph() { assert!(!root.join(".gaviero/graphs").exists(), "{stderr}"); } +/// Every run is published for the phone app: one record under the machine +/// folder, finished, with the agent's row and its output. +#[test] +fn a_run_is_published_for_the_phone() { + let tmp = tempfile::tempdir().unwrap(); + let root = std::fs::canonicalize(tmp.path()).unwrap().join("proj"); + std::fs::create_dir_all(&root).unwrap(); + configure(&root); + init_repo(&root); + let script = write_repo_script(&root); + let runs = tmp.path().join("cli-runs"); + + let stderr = stderr_of(&run_from_publishing(&root, &script, &runs)); + + let records: Vec<_> = std::fs::read_dir(&runs) + .unwrap_or_else(|e| panic!("no cli-runs folder ({e}): {stderr}")) + .flatten() + .map(|e| e.path()) + .collect(); + assert_eq!(records.len(), 1, "{records:?}"); + let run: serde_json::Value = + serde_json::from_str(&std::fs::read_to_string(records[0].join("run.json")).unwrap()) + .unwrap(); + assert_eq!(run["script"], "wf", "{run}"); + assert_eq!(run["folder"], "proj", "{run}"); + assert_eq!(run["running"], false, "{run}"); + assert_eq!(run["phase"], "completed", "{run}"); + assert_eq!(run["agents"][0]["id"], "writer", "{run}"); + assert_eq!(run["agents"][0]["status"], "completed", "{run}"); + let output = std::fs::read_to_string(records[0].join("agents/writer.json")).unwrap(); + assert!(output.contains("Running"), "{output}"); +} + /// Each folder of a multi-folder workspace has its own graph in the TUI, so /// a run from the second folder must use that one too. #[test] diff --git a/crates/gaviero-core/CLAUDE.md b/crates/gaviero-core/CLAUDE.md index 99a0ab51..4e8b083f 100644 --- a/crates/gaviero-core/CLAUDE.md +++ b/crates/gaviero-core/CLAUDE.md @@ -29,7 +29,7 @@ Network/model tests (Ollama, embedder downloads, Cursor/Codex/Claude CLI presenc | Turn capture | [`turn_capture/`](src/turn_capture) | Host-side, git-free record of everything a chat turn changed: baseline manifest + SHA-256 blob store (`/.gaviero/turns`, blobs ≤ 8 MiB), `begin`/`end` diff, host-write ledger, overlap marking, sensitive auto-revert, byte-exact file/hunk revert, persisted pending reviews, retention (last 5 reviewed turns + pending). Sessions honour it via `AgentOptions::host_capture`. | | Repo map | [`repo_map/`](src/repo_map) | Graph + [`topology.rs`](src/repo_map/topology.rs) + symbol enrichment/search. Live per-folder graph: [`repo_map/service.rs`](src/repo_map/service.rs) (`GraphService`, single writer thread). | | Skills | [`skills/`](src/skills) | Frontmatter, catalog, planner `ResolvedSkill` seam, opt-in constitution emit ([`skills/emit.rs`](src/skills/emit.rs)). | -| Scripts | [`scripts.rs`](src/scripts.rs), [`script_profiles.rs`](src/script_profiles.rs) | Named `.gaviero/dsl//.gaviero` workflows and `.gaviero/dsl/profiles/.gaviero` model profiles (located here, parsed by `gaviero-dsl`; `profiles` is therefore not a valid script name). Scopes and name resolution are shared with skills in [`scoped_folders.rs`](src/scoped_folders.rs); run preparation shared by CLI and TUI is in [`swarm/script_run.rs`](src/swarm/script_run.rs). | +| Scripts | [`scripts.rs`](src/scripts.rs), [`script_profiles.rs`](src/script_profiles.rs) | Named `.gaviero/dsl//.gaviero` workflows and `.gaviero/dsl/profiles/.gaviero` model profiles (located here, parsed by `gaviero-dsl`; `profiles` is therefore not a valid script name). Scopes and name resolution are shared with skills in [`scoped_folders.rs`](src/scoped_folders.rs); run preparation shared by CLI and TUI is in [`swarm/script_run.rs`](src/swarm/script_run.rs). Run records for the TUI SWARM panel's History and continue (`/.gaviero/swarm-runs/`, `resume_checkpoint` over the pipeline's `.gaviero/state/.json`) are in [`swarm/run_history.rs`](src/swarm/run_history.rs). `gaviero-cli` runs publish a live record for the phone in [`swarm/live_run.rs`](src/swarm/live_run.rs) (machine folder `~/.gaviero/remote/cli-runs/`, `GAVIERO_CLI_RUNS_DIR` overrides; keeps 20; a running record not written for 90 s reads `interrupted`). `CliRunStore::list_json` / `detail_json` build the `GET /v1/cli-runs[/]` bodies the TUI serves. | | History | [`history/`](src/history) | Per-turn NDJSON log (`.gaviero/history/turns.ndjson`): record model, the single `HistoryRecorder` (keys every capture by `turn_id`), tolerant reader shared by the TUI panel and `gaviero-cli --history`, the one token estimator. Write-only from host capture points; nothing under `memory/`, `context_planner/`, `mcp/`, `agent_session/`, `swarm/` imports it. Size-rotated through [`util::ndjson`](src/util/ndjson.rs), shared with the MCP telemetry sink. | | Session persistence | [`session_state.rs`](src/session_state.rs), [`session_journal.rs`](src/session_journal.rs) | State + conversations save only on a clean quit; the journal appends each prompt at dispatch (fsync) so a crash can't lose it. All saves are tmp-file + rename. | | Other | `acp`, `context_planner`, `validation_gate`, `git`, `git_conflict`, `terminal`, `util`, `workspace`, … | See `lib.rs`. | diff --git a/crates/gaviero-core/src/swarm/live_run.rs b/crates/gaviero-core/src/swarm/live_run.rs new file mode 100644 index 00000000..51384418 --- /dev/null +++ b/crates/gaviero-core/src/swarm/live_run.rs @@ -0,0 +1,1174 @@ +//! A `gaviero-cli` run, published while it runs so the phone app can show it +//! the way it shows the TUI SWARM panel's run. +//! +//! One folder per run under the machine folder +//! `~/.gaviero/remote/cli-runs//` ([`CliRunStore::machine_default`]; +//! `GAVIERO_CLI_RUNS_DIR` overrides it): `run.json` ([`LiveRun`] — the header +//! and one row per agent) and `agents/.json` (that agent's output +//! lines). The CLI writes it through a [`LiveRunHandle`]: at most once a +//! second when something changed, and at least every [`HEARTBEAT`] while +//! running. A record still `running` whose last write is older than +//! [`STALE_AFTER_SECS`] belongs to a process that went away, and is reported +//! as `interrupted`. +//! +//! Any TUI running the remote listener on the machine serves the folder as +//! `GET /v1/cli-runs` ([`CliRunStore::list_json`]) and +//! `GET /v1/cli-runs/` ([`CliRunStore::detail_json`], the same shape as the +//! `request_swarm` reply). Neither body carries an absolute path. + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use anyhow::{Context, Result}; +use serde::{Deserialize, Serialize}; + +use super::models::{AgentStatus, SwarmResult}; +use crate::types::ModelTier; + +/// Records kept on the machine; older finished ones go when a run starts. +pub const KEEP_CLI_RUNS: usize = 20; +/// A `running` record not written for this long is `interrupted`. +pub const STALE_AFTER_SECS: i64 = 90; +/// A running record is rewritten at least this often, changed or not. +pub const HEARTBEAT: Duration = Duration::from_secs(30); +/// How often the flusher looks for changes. +pub const FLUSH_INTERVAL: Duration = Duration::from_secs(1); +/// Overrides the machine folder (tests, and users who want it elsewhere). +pub const DIR_ENV: &str = "GAVIERO_CLI_RUNS_DIR"; + +/// Output kept per agent, as the TUI SWARM panel keeps it. +const MAX_ACTIVITY_LINES: usize = 500; +const MAX_ACTIVITY_BYTES: usize = 256 * 1024; +/// Most output bytes one detail reply carries: the newest ones. +const OUTPUT_TAIL_BYTES: usize = 64 * 1024; + +const RECORD_VERSION: u32 = 1; +const RECORD_FILE: &str = "run.json"; +const AGENTS_DIR: &str = "agents"; + +// ── Model ─────────────────────────────────────────────────────── + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum LineKind { + Text, + ToolCall, + Status, + FileChange, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct LiveLine { + pub kind: LineKind, + pub text: String, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct LiveAgent { + pub id: String, + pub status: AgentStatus, + pub detail: String, + pub tier: Option, + pub backend: Option, + /// Unix milliseconds the current pass started; None before it ran. + pub started_at_ms: Option, + /// Run time, frozen once the agent finished. + pub elapsed_ms: Option, + pub branch: Option, + #[serde(default)] + pub modified_files: Vec, + /// Bumped on every output change; names the saved output. + pub revision: u64, + /// Kept in `agents/.json`, not in `run.json`. + #[serde(skip)] + pub activity: Vec, + #[serde(skip)] + saw_text: bool, +} + +impl LiveAgent { + fn new(id: String, status: AgentStatus, detail: String) -> Self { + let started_at_ms = matches!(status, AgentStatus::Running).then(now_ms); + Self { + id, + status, + detail, + tier: None, + backend: None, + started_at_ms, + elapsed_ms: None, + branch: None, + modified_files: Vec::new(), + revision: 0, + activity: Vec::new(), + saw_text: false, + } + } + + fn is_finished(&self) -> bool { + matches!(self.status, AgentStatus::Completed | AgentStatus::Failed(_)) + } + + fn freeze_elapsed(&mut self) { + if self.elapsed_ms.is_none() { + self.elapsed_ms = self + .started_at_ms + .map(|start| (now_ms() - start).max(0) as u64); + } + } + + fn push_activity(&mut self, kind: LineKind, text: String) { + self.activity.push(LiveLine { kind, text }); + self.trim_activity(); + } + + fn trim_activity(&mut self) { + self.revision += 1; + if self.activity.len() > MAX_ACTIVITY_LINES { + let excess = self.activity.len() - MAX_ACTIVITY_LINES; + self.activity.drain(..excess); + } + let mut total: usize = self.activity.iter().map(|l| l.text.len()).sum(); + while total > MAX_ACTIVITY_BYTES { + let over = total - MAX_ACTIVITY_BYTES; + if self.activity.len() > 1 && self.activity[0].text.len() <= over { + total -= self.activity.remove(0).text.len(); + } else { + let first = &mut self.activity[0].text; + let mut cut = over.min(first.len()); + while !first.is_char_boundary(cut) { + cut += 1; + } + first.drain(..cut); + total -= cut; + } + } + } +} + +/// An agent known from the compiled plan before anything has run. +#[derive(Debug, Clone)] +pub struct PlannedAgent { + pub id: String, + pub tier: ModelTier, + pub model: Option, +} + +/// One CLI run: what `run.json` holds. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct LiveRun { + pub version: u32, + /// Folder name and the id the phone addresses the run by. + pub id: String, + /// Numeric id for the reply's `run.run_id` and output tokens: the start + /// time in Unix milliseconds. + pub run_id: u64, + pub script: String, + /// Name of the run root's folder — never a path. + pub folder: String, + pub pid: u32, + /// Unix seconds. + pub started_at: i64, + /// Unix seconds of the last write; the heartbeat. + pub updated_at: i64, + pub phase: String, + pub running: bool, + pub tier_current: usize, + pub tier_total: usize, + pub cost_usd: f64, + /// Run-wide note: the failure, gate results, loop passes. + pub message: String, + /// The agent the output follows: whichever started last, unless the one + /// followed is still running. + pub selected: Option, + pub agents: Vec, +} + +/// One change to a run, from the pipeline's observers. +#[derive(Debug)] +pub enum LiveUpdate { + Phase(String), + Agent { + id: String, + status: AgentStatus, + detail: String, + }, + Tier { + current: usize, + total: usize, + }, + Dispatch { + id: String, + tier: ModelTier, + backend: String, + }, + Text { + id: String, + text: String, + }, + Tool { + id: String, + summary: String, + }, + Status { + id: String, + status: String, + }, + File { + id: String, + path: String, + additions: usize, + deletions: usize, + }, + /// An agent's whole answer; kept only if it never streamed any text. + Message { + id: String, + content: String, + }, + Notice(String), + Cost(f64), + Completed(Box), + Failed(String), +} + +impl LiveRun { + /// A run that has just started, every planned agent queued. + pub fn new(id: String, script: String, folder: String, planned: Vec) -> Self { + let now = now_ms(); + Self { + version: RECORD_VERSION, + id, + run_id: now.max(0) as u64, + script, + folder, + pid: std::process::id(), + started_at: now / 1000, + updated_at: now / 1000, + phase: "starting".to_string(), + running: true, + tier_current: 0, + tier_total: 0, + cost_usd: 0.0, + message: String::new(), + selected: None, + agents: planned + .into_iter() + .map(|p| { + let mut agent = LiveAgent::new(p.id, AgentStatus::Pending, "queued".into()); + agent.tier = Some(p.tier); + agent.backend = p.model; + agent + }) + .collect(), + } + } + + pub fn apply(&mut self, update: LiveUpdate) { + match update { + LiveUpdate::Phase(phase) => self.phase = phase, + LiveUpdate::Agent { id, status, detail } => self.update_agent(&id, status, detail), + LiveUpdate::Tier { current, total } => { + self.tier_current = current; + self.tier_total = total; + } + LiveUpdate::Dispatch { id, tier, backend } => { + if let Some(agent) = self.agents.iter_mut().find(|a| a.id == id) { + agent.tier = Some(tier); + agent.backend = Some(backend); + } + } + LiveUpdate::Text { id, text } => { + let clean = strip_ansi(&text); + let agent = self.agent_or_insert(&id); + agent.saw_text = true; + match agent.activity.last_mut() { + Some(last) if last.kind == LineKind::Text => { + last.text.push_str(&clean); + agent.trim_activity(); + } + _ => agent.push_activity(LineKind::Text, clean), + } + } + LiveUpdate::Tool { id, summary } => { + let text = if summary.starts_with('[') { + summary + } else { + format!("[{summary}]") + }; + let agent = self.agent_or_insert(&id); + agent.detail = text.clone(); + agent.push_activity(LineKind::ToolCall, text); + } + LiveUpdate::Status { id, status } => self.agent_or_insert(&id).detail = status, + LiveUpdate::File { + id, + path, + additions, + deletions, + } => { + let line = format!("[wrote {path} +{additions} -{deletions}]"); + let agent = self.agent_or_insert(&id); + agent.detail = line.clone(); + agent.push_activity(LineKind::FileChange, line); + } + LiveUpdate::Message { id, content } => { + let agent = self.agent_or_insert(&id); + if !agent.saw_text && !content.trim().is_empty() { + agent.saw_text = true; + agent.push_activity(LineKind::Text, strip_ansi(&content)); + } + } + LiveUpdate::Notice(message) => self.message = message, + LiveUpdate::Cost(usd) => self.cost_usd = usd, + LiveUpdate::Completed(result) => self.finish_with_result(&result), + LiveUpdate::Failed(message) => { + self.end_run("failed"); + self.message = message; + } + } + } + + fn agent_or_insert(&mut self, id: &str) -> &mut LiveAgent { + let index = match self.agents.iter().position(|a| a.id == id) { + Some(index) => index, + None => { + self.agents + .push(LiveAgent::new(id.to_string(), AgentStatus::Running, String::new())); + self.agents.len() - 1 + } + }; + &mut self.agents[index] + } + + fn update_agent(&mut self, id: &str, status: AgentStatus, detail: String) { + let agent = self.agent_or_insert(id); + let changed = std::mem::discriminant(&agent.status) != std::mem::discriminant(&status); + let now_running = matches!(status, AgentStatus::Running); + if now_running && (changed || agent.started_at_ms.is_none()) { + // A loop runs the same agent again: the clock restarts each pass. + agent.started_at_ms = Some(now_ms()); + agent.elapsed_ms = None; + agent.saw_text = false; + } + if matches!(status, AgentStatus::Completed | AgentStatus::Failed(_)) { + agent.freeze_elapsed(); + } + if changed { + let line = match &status { + AgentStatus::Pending => format!("Queued: {detail}"), + AgentStatus::Running => format!("Running: {detail}"), + AgentStatus::Completed => format!("Completed: {detail}"), + AgentStatus::Failed(e) => format!("Failed: {e}"), + }; + agent.push_activity(LineKind::Status, line); + } + agent.status = status; + agent.detail = detail; + if now_running && changed { + self.follow(id); + } + } + + fn follow(&mut self, id: &str) { + let followed_running = self.selected.as_deref().is_some_and(|sel| { + sel != id + && self + .agents + .iter() + .any(|a| a.id == sel && matches!(a.status, AgentStatus::Running)) + }); + if !followed_running { + self.selected = Some(id.to_string()); + } + } + + /// Close out a run that ended without a result. + fn end_run(&mut self, phase: &str) { + self.running = false; + self.phase = phase.to_string(); + for agent in &mut self.agents { + if !agent.is_finished() { + agent.freeze_elapsed(); + agent.status = AgentStatus::Failed(phase.to_string()); + agent.detail = phase.to_string(); + } + } + } + + fn finish_with_result(&mut self, result: &SwarmResult) { + self.running = false; + self.phase = if result.success { + "completed".to_string() + } else { + "completed with failures".to_string() + }; + for manifest in &result.manifests { + let Some(agent) = self + .agents + .iter_mut() + .find(|a| a.id == manifest.work_unit_id) + else { + continue; + }; + agent.status = manifest.status.clone(); + agent.freeze_elapsed(); + agent.branch = manifest.branch.clone(); + agent.modified_files = manifest + .modified_files + .iter() + .map(|p| p.to_string_lossy().to_string()) + .collect(); + let count = agent.modified_files.len(); + agent.detail = if count > 0 { + format!("Modified {count} file{}", if count == 1 { "" } else { "s" }) + } else { + manifest + .summary + .clone() + .unwrap_or_else(|| agent.detail.clone()) + }; + } + // An agent the run never reached has no manifest. + for agent in &mut self.agents { + if matches!(agent.status, AgentStatus::Pending | AgentStatus::Running) { + agent.freeze_elapsed(); + agent.status = AgentStatus::Failed("not run".to_string()); + agent.detail = "not run".to_string(); + } + } + } + + /// Still `running`, but its process stopped writing. + pub fn is_interrupted(&self, now_secs: i64) -> bool { + self.running && now_secs - self.updated_at > STALE_AFTER_SECS + } + + /// `running`, `completed`, `completed_with_failures`, `failed` or + /// `interrupted`. + pub fn state(&self, now_secs: i64) -> &'static str { + if self.is_interrupted(now_secs) { + "interrupted" + } else if self.running { + "running" + } else { + match self.phase.as_str() { + "completed" => "completed", + "completed with failures" => "completed_with_failures", + _ => "failed", + } + } + } +} + +// ── Store ─────────────────────────────────────────────────────── + +/// The machine folder of CLI run records. +#[derive(Debug, Clone)] +pub struct CliRunStore { + dir: PathBuf, +} + +impl CliRunStore { + pub fn new(dir: impl Into) -> Self { + Self { dir: dir.into() } + } + + /// `$GAVIERO_CLI_RUNS_DIR` when set and non-empty, else + /// `~/.gaviero/remote/cli-runs`. None without a home directory. + pub fn machine_default() -> Option { + if let Some(dir) = std::env::var_os(DIR_ENV).filter(|v| !v.is_empty()) { + return Some(Self::new(PathBuf::from(dir))); + } + crate::workspace::remote_machine_state_dir().map(|dir| Self::new(dir.join("cli-runs"))) + } + + pub fn dir(&self) -> &Path { + &self.dir + } + + /// A new run id: start time then pid, so ids sort in start order and two + /// runs started in the same millisecond still differ. + pub fn new_run_id() -> String { + format!( + "{}-{}", + chrono::Utc::now().format("%Y%m%d-%H%M%S-%3f"), + std::process::id() + ) + } + + /// Ids are only ever ours: letters, digits, `-`, `_`. Anything else (a + /// path separator, `..`) names no run. + fn run_dir(&self, id: &str) -> Option { + let valid = !id.is_empty() + && id.len() <= 128 + && id + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'); + valid.then(|| self.dir.join(id)) + } + + fn agent_path(run_dir: &Path, agent: &str) -> PathBuf { + let name: String = agent + .chars() + .map(|c| { + if c.is_ascii_alphanumeric() || c == '-' || c == '_' { + c + } else { + '_' + } + }) + .collect(); + run_dir.join(AGENTS_DIR).join(format!("{name}.json")) + } + + /// Write `run.json` and the given agents' output (tmp file + rename). + pub fn save(&self, run: &LiveRun, outputs: &[(String, Vec)]) -> Result<()> { + let dir = self + .run_dir(&run.id) + .with_context(|| format!("invalid run id {:?}", run.id))?; + std::fs::create_dir_all(dir.join(AGENTS_DIR)) + .with_context(|| format!("creating {}", dir.display()))?; + for (agent, lines) in outputs { + write_atomic(&Self::agent_path(&dir, agent), &serde_json::to_vec(lines)?)?; + } + write_atomic(&dir.join(RECORD_FILE), &serde_json::to_vec_pretty(run)?) + } + + pub fn load(&self, id: &str) -> Option { + let dir = self.run_dir(id)?; + let text = std::fs::read_to_string(dir.join(RECORD_FILE)).ok()?; + serde_json::from_str(&text).ok() + } + + pub fn read_output(&self, id: &str, agent: &str) -> Vec { + self.run_dir(id) + .and_then(|dir| std::fs::read_to_string(Self::agent_path(&dir, agent)).ok()) + .and_then(|text| serde_json::from_str(&text).ok()) + .unwrap_or_default() + } + + /// Every readable record, newest first. + pub fn list(&self) -> Vec { + let Ok(entries) = std::fs::read_dir(&self.dir) else { + return Vec::new(); + }; + let mut runs: Vec = entries + .flatten() + .filter_map(|e| std::fs::read_to_string(e.path().join(RECORD_FILE)).ok()) + .filter_map(|json| serde_json::from_str(&json).ok()) + .collect(); + runs.sort_by(|a, b| { + b.started_at + .cmp(&a.started_at) + .then_with(|| b.id.cmp(&a.id)) + }); + runs + } + + /// Delete all but the newest `keep` records. A run still going (running + /// and not stale) is never deleted. Returns how many went. + pub fn prune(&self, keep: usize) -> usize { + let now = now_secs(); + self.list() + .into_iter() + .skip(keep) + .filter(|run| !run.running || run.is_interrupted(now)) + .filter_map(|run| self.run_dir(&run.id)) + .filter(|dir| std::fs::remove_dir_all(dir).is_ok()) + .count() + } + + /// `GET /v1/cli-runs`: every record, running ones first, then newest. + pub fn list_json(&self, host: &str) -> serde_json::Value { + let now = now_secs(); + let mut runs = self.list(); + // Stable: newest first within each group. + runs.sort_by_key(|run| run.state(now) != "running"); + let runs: Vec<_> = runs + .iter() + .map(|run| { + let state = run.state(now); + serde_json::json!({ + "id": run.id, + "script": run.script, + "folder": run.folder, + "state": state, + "phase": if state == "interrupted" { "interrupted" } else { run.phase.as_str() }, + "running": state == "running", + "started_at": rfc3339(run.started_at), + "updated_at": rfc3339(run.updated_at), + "cost_usd": run.cost_usd, + "counts": counts_json(run, state == "interrupted"), + "agents": run.agents.len(), + }) + }) + .collect(); + serde_json::json!({ + "host": host, + "generated_at": rfc3339(now), + "runs": runs, + }) + } + + /// `GET /v1/cli-runs/`: the `request_swarm` reply shape for one run, + /// plus `id`, `folder`, `state`, `started_at` in `run`. None for an + /// unknown id. + pub fn detail_json( + &self, + id: &str, + agent_id: Option<&str>, + output_token: Option<&str>, + ) -> Option { + let run = self.load(id)?; + let now = now_secs(); + let state = run.state(now); + let interrupted = state == "interrupted"; + let now_ms = now_ms(); + let agents: Vec<_> = run + .agents + .iter() + .map(|agent| { + let (status, error) = shown_status(agent, interrupted); + let elapsed = agent.elapsed_ms.or_else(|| { + let start = agent.started_at_ms?; + // An interrupted run stopped at its last write. + let end = if interrupted { run.updated_at * 1000 } else { now_ms }; + (matches!(agent.status, AgentStatus::Running)).then(|| (end - start).max(0) as u64) + }); + serde_json::json!({ + "id": agent.id, + "status": status, + "error": error, + "detail": if interrupted && status == "failed" && error.as_deref() == Some("interrupted") { + "interrupted".to_string() + } else { + agent.detail.replace(['\n', '\r'], " ") + }, + "tier": agent.tier.map(|t| match t { + ModelTier::Cheap => "cheap", + ModelTier::Expensive => "expensive", + }), + "backend": agent.backend, + "elapsed_ms": elapsed, + "branch": agent.branch, + "modified_files": agent.modified_files, + }) + }) + .collect(); + let run_json = serde_json::json!({ + "id": run.id, + "run_id": run.run_id, + "script": run.script, + "folder": run.folder, + "state": state, + "started_at": rfc3339(run.started_at), + "phase": if interrupted { "interrupted" } else { run.phase.as_str() }, + "running": state == "running", + "tier_current": run.tier_current, + "tier_total": run.tier_total, + "cost_usd": run.cost_usd, + "message": if interrupted && run.message.is_empty() { + "gaviero-cli stopped without finishing this run.".to_string() + } else { + run.message.clone() + }, + "counts": counts_json(&run, interrupted), + "agents": agents, + }); + + let selected = agent_id + .and_then(|id| run.agents.iter().find(|a| a.id == id)) + .or_else(|| { + run.selected + .as_deref() + .and_then(|id| run.agents.iter().find(|a| a.id == id)) + }) + .or_else(|| run.agents.first()); + let output = selected.map(|agent| { + let token = format!("{}:{}", run.run_id, agent.revision); + if output_token == Some(token.as_str()) { + return serde_json::json!({ + "agent_id": agent.id, + "token": token, + "unchanged": true, + }); + } + let lines = self.read_output(&run.id, &agent.id); + let (lines, truncated) = output_tail(&lines, OUTPUT_TAIL_BYTES); + serde_json::json!({ + "agent_id": agent.id, + "token": token, + "unchanged": false, + "lines": lines, + "truncated": truncated, + }) + }); + Some(serde_json::json!({ + "run": run_json, + "selected_id": selected.map(|a| a.id.clone()), + "output": output, + })) + } +} + +/// Wire status and error of an agent; an interrupted run's unfinished +/// agents read as failed, `interrupted`. +fn shown_status(agent: &LiveAgent, interrupted: bool) -> (&'static str, Option) { + match &agent.status { + AgentStatus::Pending | AgentStatus::Running if interrupted => { + ("failed", Some("interrupted".to_string())) + } + AgentStatus::Pending => ("queued", None), + AgentStatus::Running => ("running", None), + AgentStatus::Completed => ("completed", None), + AgentStatus::Failed(e) => ("failed", Some(e.clone())), + } +} + +fn counts_json(run: &LiveRun, interrupted: bool) -> serde_json::Value { + let (mut running, mut queued, mut done, mut failed) = (0, 0, 0, 0); + for agent in &run.agents { + match shown_status(agent, interrupted).0 { + "running" => running += 1, + "queued" => queued += 1, + "completed" => done += 1, + _ => failed += 1, + } + } + serde_json::json!({ "running": running, "queued": queued, "done": done, "failed": failed }) +} + +/// The newest lines that fit in `budget` bytes, oldest first. The oldest +/// kept line loses its head when it does not fit whole. +fn output_tail(lines: &[LiveLine], budget: usize) -> (Vec, bool) { + let mut out = Vec::new(); + let mut left = budget; + let mut truncated = false; + for line in lines.iter().rev() { + if left == 0 { + truncated = true; + break; + } + let text = if line.text.len() <= left { + line.text.as_str() + } else { + truncated = true; + let mut cut = line.text.len() - left; + while !line.text.is_char_boundary(cut) { + cut += 1; + } + &line.text[cut..] + }; + left -= text.len(); + out.push(serde_json::json!({ "kind": line.kind, "text": text })); + } + out.reverse(); + (out, truncated) +} + +// ── Publishing ────────────────────────────────────────────────── + +struct Published { + run: LiveRun, + dirty: bool, + /// Output revision last written, per agent. + saved: HashMap, + last_write: Instant, +} + +/// The publishing side, shared by a run's observers. Updates only touch +/// memory under the lock; files are written outside it. +#[derive(Clone)] +pub struct LiveRunHandle { + store: CliRunStore, + state: Arc>, +} + +impl LiveRunHandle { + /// Prune old records and write the first one. A failed write is logged + /// and later writes are still tried. + pub fn start(store: CliRunStore, run: LiveRun) -> Self { + store.prune(KEEP_CLI_RUNS.saturating_sub(1)); + let handle = Self { + store, + state: Arc::new(Mutex::new(Published { + run, + dirty: true, + saved: HashMap::new(), + last_write: Instant::now(), + })), + }; + handle.flush(true); + handle + } + + pub fn id(&self) -> String { + self.state.lock().map(|s| s.run.id.clone()).unwrap_or_default() + } + + pub fn update(&self, update: LiveUpdate) { + if let Ok(mut state) = self.state.lock() { + state.run.apply(update); + state.dirty = true; + } + } + + /// Write what changed; with `force`, rewrite the header even when + /// nothing did (the heartbeat). + pub fn flush(&self, force: bool) { + let (run, outputs) = { + let Ok(mut state) = self.state.lock() else { + return; + }; + if !state.dirty && !force { + return; + } + state.dirty = false; + state.last_write = Instant::now(); + state.run.updated_at = now_secs(); + let mut outputs = Vec::new(); + let mut header = state.run.clone(); + for agent in &mut header.agents { + let activity = std::mem::take(&mut agent.activity); + if state.saved.get(&agent.id) != Some(&agent.revision) { + outputs.push((agent.id.clone(), activity)); + } + } + for (id, _) in &outputs { + let revision = header + .agents + .iter() + .find(|a| &a.id == id) + .map(|a| a.revision) + .unwrap_or_default(); + state.saved.insert(id.clone(), revision); + } + (header, outputs) + }; + if let Err(e) = self.store.save(&run, &outputs) { + tracing::warn!("cli run record {}: {e:#}", run.id); + if let Ok(mut state) = self.state.lock() { + // Write everything again next time. + state.saved.clear(); + state.dirty = true; + } + } + } + + fn heartbeat_due(&self) -> bool { + self.state + .lock() + .map(|s| s.last_write.elapsed() >= HEARTBEAT) + .unwrap_or(false) + } + + /// Flush every [`FLUSH_INTERVAL`] until [`LiveRunFlusher::stop`]; the + /// heartbeat keeps a quiet run from reading as interrupted. + pub fn spawn_flusher(&self) -> LiveRunFlusher { + let handle = self.clone(); + let stop = Arc::new(tokio::sync::Notify::new()); + let stopped = stop.clone(); + let task = tokio::spawn(async move { + loop { + tokio::select! { + _ = tokio::time::sleep(FLUSH_INTERVAL) => {} + _ = stopped.notified() => break, + } + // Awaited outside the select: a stop waits for this write. + let h = handle.clone(); + let force = h.heartbeat_due(); + let _ = tokio::task::spawn_blocking(move || h.flush(force)).await; + } + }); + LiveRunFlusher { stop, task } + } + + /// End the run (`Completed` or `Failed`) and write it at once. Stop the + /// flusher first, or an older snapshot it is still writing can land + /// after this one. + pub fn finish(&self, outcome: LiveUpdate) { + self.update(outcome); + self.flush(true); + } +} + +/// The background writer of one run ([`LiveRunHandle::spawn_flusher`]). +pub struct LiveRunFlusher { + stop: Arc, + task: tokio::task::JoinHandle<()>, +} + +impl LiveRunFlusher { + /// Stop, waiting for a write in progress, so nothing it holds can + /// overwrite the final record. + pub async fn stop(self) { + // `notify_one` keeps the permit when the task is mid-write. + self.stop.notify_one(); + let _ = self.task.await; + } +} + +// ── Helpers ───────────────────────────────────────────────────── + +fn now_ms() -> i64 { + chrono::Utc::now().timestamp_millis() +} + +fn now_secs() -> i64 { + chrono::Utc::now().timestamp() +} + +fn rfc3339(secs: i64) -> String { + chrono::DateTime::from_timestamp(secs, 0) + .unwrap_or_default() + .to_rfc3339_opts(chrono::SecondsFormat::Secs, true) +} + +/// Agent output may carry terminal escapes; the phone shows plain text. +fn strip_ansi(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + let mut chars = text.chars().peekable(); + while let Some(ch) = chars.next() { + if ch != '\x1b' { + out.push(ch); + continue; + } + match chars.peek() { + Some('[') => { + chars.next(); + for c in chars.by_ref() { + if ('@'..='~').contains(&c) { + break; + } + } + } + Some(']') => { + chars.next(); + for c in chars.by_ref() { + if c == '\x07' || c == '\x1b' { + break; + } + } + } + _ => { + chars.next(); + } + } + } + out +} + +fn write_atomic(path: &Path, bytes: &[u8]) -> Result<()> { + let tmp = path.with_extension("json.tmp"); + std::fs::write(&tmp, bytes).with_context(|| format!("writing {}", tmp.display()))?; + std::fs::rename(&tmp, path).with_context(|| format!("replacing {}", path.display()))?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::swarm::models::AgentManifest; + + fn planned(ids: &[&str]) -> Vec { + ids.iter() + .map(|id| PlannedAgent { + id: id.to_string(), + tier: ModelTier::Cheap, + model: Some("claude:haiku".into()), + }) + .collect() + } + + fn running(id: &str) -> LiveUpdate { + LiveUpdate::Agent { + id: id.into(), + status: AgentStatus::Running, + detail: "go".into(), + } + } + + fn sample_run(id: &str) -> LiveRun { + LiveRun::new(id.into(), "deep_explore".into(), "proj".into(), planned(&["a", "b"])) + } + + #[test] + fn updates_follow_the_dashboard_rules() { + let mut run = sample_run("r1"); + run.apply(running("a")); + run.apply(LiveUpdate::Text { id: "a".into(), text: "hel".into() }); + run.apply(LiveUpdate::Text { id: "a".into(), text: "\x1b[1mlo\x1b[0m".into() }); + run.apply(LiveUpdate::Tool { id: "a".into(), summary: "Read x".into() }); + // A whole message after streamed text is not repeated. + run.apply(LiveUpdate::Message { id: "a".into(), content: "hello".into() }); + let a = &run.agents[0]; + assert_eq!( + a.activity.iter().map(|l| (l.kind, l.text.as_str())).collect::>(), + vec![ + (LineKind::Status, "Running: go"), + (LineKind::Text, "hello"), + (LineKind::ToolCall, "[Read x]"), + ] + ); + assert_eq!(a.detail, "[Read x]"); + assert_eq!(run.selected.as_deref(), Some("a")); + + // b starting while a still runs does not move the selection. + run.apply(running("b")); + assert_eq!(run.selected.as_deref(), Some("a")); + + run.apply(LiveUpdate::Completed(Box::new(SwarmResult { + manifests: vec![AgentManifest { + work_unit_id: "a".into(), + status: AgentStatus::Completed, + modified_files: vec!["x.rs".into()], + branch: Some("gaviero/a".into()), + summary: None, + output: None, + cost_usd: 0.0, + }], + merge_results: vec![], + success: false, + pre_swarm_sha: String::new(), + }))); + assert!(!run.running); + assert_eq!(run.phase, "completed with failures"); + assert_eq!(run.agents[0].detail, "Modified 1 file"); + assert_eq!(run.agents[1].status, AgentStatus::Failed("not run".into())); + assert!(run.agents[0].elapsed_ms.is_some()); + } + + #[test] + fn a_published_run_round_trips_with_its_output() { + let dir = tempfile::tempdir().unwrap(); + let store = CliRunStore::new(dir.path()); + let handle = LiveRunHandle::start(store.clone(), sample_run("r1")); + handle.update(running("a")); + handle.update(LiveUpdate::Text { id: "a".into(), text: "working".into() }); + handle.flush(false); + + let detail = store.detail_json("r1", None, None).unwrap(); + assert_eq!(detail["run"]["script"], "deep_explore"); + assert_eq!(detail["run"]["folder"], "proj"); + assert_eq!(detail["run"]["state"], "running"); + assert_eq!(detail["run"]["counts"]["running"], 1); + assert_eq!(detail["selected_id"], "a"); + assert_eq!(detail["output"]["lines"][1]["text"], "working"); + assert_eq!(detail["output"]["lines"][1]["kind"], "text"); + + // The same token again: nothing new to send. + let token = detail["output"]["token"].as_str().unwrap().to_string(); + let again = store.detail_json("r1", None, Some(&token)).unwrap(); + assert_eq!(again["output"]["unchanged"], true); + assert!(again["output"].get("lines").is_none()); + + // Another agent's output on request; an unknown run is None. + let b = store.detail_json("r1", Some("b"), Some(&token)).unwrap(); + assert_eq!(b["output"]["agent_id"], "b"); + assert_eq!(b["output"]["unchanged"], false); + assert!(store.detail_json("nope", None, None).is_none()); + assert!(store.detail_json("../r1", None, None).is_none()); + + handle.finish(LiveUpdate::Failed("boom".into())); + let list = store.list_json("desk"); + assert_eq!(list["host"], "desk"); + assert_eq!(list["runs"][0]["id"], "r1"); + assert_eq!(list["runs"][0]["state"], "failed"); + assert_eq!(list["runs"][0]["running"], false); + let detail = store.detail_json("r1", None, None).unwrap(); + assert_eq!(detail["run"]["message"], "boom"); + } + + #[test] + fn a_running_record_that_stopped_writing_is_interrupted() { + let dir = tempfile::tempdir().unwrap(); + let store = CliRunStore::new(dir.path()); + let mut run = sample_run("r1"); + run.apply(running("a")); + run.updated_at = now_secs() - STALE_AFTER_SECS - 5; + store.save(&run, &[]).unwrap(); + + let detail = store.detail_json("r1", None, None).unwrap(); + assert_eq!(detail["run"]["state"], "interrupted"); + assert_eq!(detail["run"]["phase"], "interrupted"); + assert_eq!(detail["run"]["running"], false); + assert_eq!(detail["run"]["agents"][0]["status"], "failed"); + assert_eq!(detail["run"]["agents"][0]["error"], "interrupted"); + assert_eq!(detail["run"]["counts"]["failed"], 2); + assert_eq!(store.list_json("h")["runs"][0]["state"], "interrupted"); + } + + #[test] + fn running_records_list_first_then_newest() { + let dir = tempfile::tempdir().unwrap(); + let store = CliRunStore::new(dir.path()); + let mut old_running = sample_run("a-old"); + old_running.started_at -= 100; + let mut new_done = sample_run("b-new"); + new_done.apply(LiveUpdate::Failed("x".into())); + store.save(&old_running, &[]).unwrap(); + store.save(&new_done, &[]).unwrap(); + let ids: Vec<_> = store.list_json("h")["runs"] + .as_array() + .unwrap() + .iter() + .map(|r| r["id"].as_str().unwrap().to_string()) + .collect(); + assert_eq!(ids, vec!["a-old", "b-new"]); + } + + #[test] + fn prune_keeps_the_newest_and_every_live_run() { + let dir = tempfile::tempdir().unwrap(); + let store = CliRunStore::new(dir.path()); + for i in 0..5 { + let mut run = sample_run(&format!("r{i}")); + run.started_at += i; + if i != 0 { + run.apply(LiveUpdate::Failed("x".into())); + } + store.save(&run, &[]).unwrap(); + } + // r0 is the oldest but still running: kept. + assert_eq!(store.prune(2), 2); + let mut ids: Vec<_> = store.list().into_iter().map(|r| r.id).collect(); + ids.sort(); + assert_eq!(ids, vec!["r0", "r3", "r4"]); + } + + #[tokio::test(flavor = "multi_thread")] + async fn the_flusher_writes_changes_and_stops_before_the_final_write() { + let dir = tempfile::tempdir().unwrap(); + let store = CliRunStore::new(dir.path()); + let handle = LiveRunHandle::start(store.clone(), sample_run("r1")); + let flusher = handle.spawn_flusher(); + handle.update(running("a")); + handle.update(LiveUpdate::Text { id: "a".into(), text: "tick".into() }); + let deadline = Instant::now() + Duration::from_secs(5); + while store.read_output("r1", "a").len() < 2 { + assert!(Instant::now() < deadline, "the flusher never wrote"); + tokio::time::sleep(Duration::from_millis(50)).await; + } + flusher.stop().await; + handle.finish(LiveUpdate::Failed("boom".into())); + // Nothing is left running that could write an older snapshot. + tokio::time::sleep(FLUSH_INTERVAL + Duration::from_millis(200)).await; + let run = store.load("r1").unwrap(); + assert!(!run.running); + assert_eq!(run.phase, "failed"); + } + + #[test] + fn output_tail_keeps_the_newest_bytes() { + let lines = vec![ + LiveLine { kind: LineKind::Text, text: "aaaa".into() }, + LiveLine { kind: LineKind::Status, text: "bbbb".into() }, + ]; + let (tail, truncated) = output_tail(&lines, 6); + assert!(truncated); + assert_eq!(tail[0]["text"], "aa"); + assert_eq!(tail[1]["kind"], "status"); + } +} diff --git a/crates/gaviero-core/src/swarm/loop_resume.rs b/crates/gaviero-core/src/swarm/loop_resume.rs index 838a244b..67a2dd84 100644 --- a/crates/gaviero-core/src/swarm/loop_resume.rs +++ b/crates/gaviero-core/src/swarm/loop_resume.rs @@ -156,6 +156,9 @@ pub fn detect( // version → agent id → coverage let mut by_version: BTreeMap> = BTreeMap::new(); + // Per agent, the owned globs that hold versioned artefacts in some pass. + let mut versioned_globs: BTreeMap<&str, std::collections::BTreeSet> = + owned.keys().map(|id| (*id, Default::default())).collect(); for (rel, len) in &files { let Some(version) = parse_version(rel) else { @@ -168,6 +171,7 @@ pub fn detect( else { continue; }; + versioned_globs.entry(agent_id).or_default().insert(idx); let entry = by_version .entry(version) .or_default() @@ -214,19 +218,41 @@ pub fn detect( notes.push(format!("v{version} incomplete — a runtime `for` wrote nothing")); continue; } - let mut vectors = per_agent + // Roster clones share one glob layout and must produce the same + // artefact set, so no reviewer reads peer input a peer never wrote. + // Agents with a layout of their own (a curator and a verifier in one + // panel) cannot be compared that way; each must instead cover every + // glob it writes versioned files to in any pass. Unversioned globs + // (`archive.md`) never count. + let fixed: Vec<(&str, &Coverage)> = per_agent .iter() .filter(|(id, _)| !dynamic.contains(*id)) - .map(|(_, c)| &c.counts); - let Some(first) = vectors.next() else { + .map(|(id, c)| (*id, c)) + .collect(); + if fixed.is_empty() { // Only runtime `for` members, each with at least one artefact. last_complete = Some(*version); continue; - }; - if first.iter().sum::() == 0 { + } + if fixed.iter().any(|(_, c)| c.total() == 0) { continue; } - if vectors.all(|v| v == first) { + let layout = |id: &str| owned.get(id).map_or(0, Vec::len); + let consistent = fixed.iter().all(|(id, c)| { + let peers: Vec<&&Coverage> = fixed + .iter() + .filter(|(other, _)| other != id && layout(other) == layout(id)) + .map(|(_, c)| c) + .collect(); + if peers.is_empty() { + versioned_globs[*id] + .iter() + .all(|&glob| c.counts.get(glob).is_some_and(|&n| n > 0)) + } else { + peers.iter().all(|p| p.counts == c.counts) + } + }); + if consistent { last_complete = Some(*version); } else { let detail: Vec = per_agent @@ -234,7 +260,8 @@ pub fn detect( .map(|(id, c)| format!("{id}={}", c.total())) .collect(); notes.push(format!( - "v{version} incomplete — reviewers produced different artefact sets ({})", + "v{version} incomplete — an agent's artefact set is short or differs from its \ + peers' ({})", detail.join(", ") )); } @@ -704,6 +731,62 @@ mod tests { assert_eq!(resume.resume_iter_start, 3); } + /// The `deep_explore.gaviero` shape: a curator (four owned globs, two of + /// them unversioned), a verifier (one) and a runtime `for` investigator + /// in one loop. Their artefact sets differ by design; each pass is + /// complete once each agent covered its own versioned globs. + fn deep_explore_units() -> Vec { + vec![ + unit( + "curator", + &[ + "out/frontier-v*.json", + "out/assignments-v*.json", + "out/archive.md", + "out/journal.md", + ], + ), + unit("verifier", &["out/audit-v*.json"]), + unit("investigator", &["out/{{q.id}}-finding-v*.json"]), + ] + } + + fn deep_explore_pass(root: &Path, v: u32, audit: bool) { + write(root, &format!("out/frontier-v{v}.json"), "x"); + write(root, &format!("out/assignments-v{v}.json"), "x"); + write(root, &format!("out/q{v}-p{v}-finding-v{v}.json"), "x"); + if audit { + write(root, &format!("out/audit-v{v}.json"), "x"); + } + } + + #[test] + fn a_panel_of_different_agents_resumes_after_its_last_audited_pass() { + let tmp = tempfile::tempdir().unwrap(); + let root = tmp.path(); + write(root, "out/landscape.json", "x"); + write(root, "out/journal.md", "x"); + write(root, "out/archive.md", "x"); + for v in 1..=3 { + deep_explore_pass(root, v, true); + } + // Pass 4: the lanes ran, the verifier never wrote its audit. + deep_explore_pass(root, 4, false); + let units = deep_explore_units(); + let lc = loop_config(&["curator", "verifier", "investigator"], "out", 1); + + let resume = detect(root, &lc, &map(&units)).expect("passes 1-3 are complete"); + assert_eq!(resume.last_complete_iter, 3); + assert_eq!(resume.resume_iter_start, 4); + assert!(resume.discarded.iter().all(|p| p.contains("-v4."))); + assert_eq!(resume.discarded.len(), 3); + + // The curator wrote its frontier but not its assignments: not done. + std::fs::remove_file(root.join("out/assignments-v3.json")).unwrap(); + let resume = detect(root, &lc, &map(&units)).expect("passes 1-2 are complete"); + assert_eq!(resume.last_complete_iter, 2); + } + #[test] fn placeholders_become_wildcards() { assert_eq!(wildcard_placeholders("out/{{q.id}}-v*.md"), "out/*-v*.md"); diff --git a/crates/gaviero-core/src/swarm/merge.rs b/crates/gaviero-core/src/swarm/merge.rs index 6a6430c9..0fa981b2 100644 --- a/crates/gaviero-core/src/swarm/merge.rs +++ b/crates/gaviero-core/src/swarm/merge.rs @@ -76,6 +76,24 @@ fn collect_conflict_files(repo_dir: &Path) -> Result> { Ok(conflicts) } +/// True when `branch` exists and HEAD does not contain it yet: an agent +/// branch a previous run finished but never merged (the run was interrupted +/// before its merge phase). A missing branch, or one already merged, is false. +pub fn branch_pending_merge(repo_dir: &Path, branch: &str) -> bool { + let git = |args: &[&str]| { + Command::new("git") + .args(args) + .current_dir(repo_dir) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .is_ok_and(|s| s.success()) + }; + let reference = format!("refs/heads/{branch}"); + git(&["rev-parse", "--verify", "--quiet", &reference]) + && !git(&["merge-base", "--is-ancestor", &reference, "HEAD"]) +} + /// Abort an in-progress merge. pub fn abort_merge(repo_dir: &Path) -> Result<()> { Command::new("git") @@ -238,3 +256,47 @@ async fn resolve_single_file( Ok(cleaned.to_string()) } + +#[cfg(test)] +mod tests { + use super::*; + + fn git(dir: &Path, args: &[&str]) { + let status = Command::new("git") + .args(args) + .current_dir(dir) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .unwrap(); + assert!(status.success(), "git {args:?}"); + } + + fn commit(dir: &Path, file: &str) { + std::fs::write(dir.join(file), file).unwrap(); + git(dir, &["add", file]); + git(dir, &["commit", "-qm", file]); + } + + #[test] + fn only_an_existing_unmerged_branch_is_pending() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + git(root, &["init", "-q", "-b", "main"]); + git(root, &["config", "user.name", "t"]); + git(root, &["config", "user.email", "t@t"]); + commit(root, "base.txt"); + + git(root, &["branch", "gaviero/done"]); + git(root, &["checkout", "-q", "-b", "gaviero/agent"]); + commit(root, "agent.txt"); + git(root, &["checkout", "-q", "main"]); + + assert!(branch_pending_merge(root, "gaviero/agent"), "unmerged work"); + assert!(!branch_pending_merge(root, "gaviero/done"), "already in HEAD"); + assert!(!branch_pending_merge(root, "gaviero/missing"), "no such branch"); + + merge_branch(root, "gaviero/agent").unwrap(); + assert!(!branch_pending_merge(root, "gaviero/agent"), "merged now"); + } +} diff --git a/crates/gaviero-core/src/swarm/mod.rs b/crates/gaviero-core/src/swarm/mod.rs index 7073333c..9b09d860 100644 --- a/crates/gaviero-core/src/swarm/mod.rs +++ b/crates/gaviero-core/src/swarm/mod.rs @@ -6,6 +6,7 @@ pub mod context; pub mod context_bundle; pub mod coordinator; pub mod execution_state; +pub mod live_run; pub(crate) mod loop_gate; pub mod loop_resume; pub mod merge; @@ -18,6 +19,7 @@ pub mod planner; pub mod privacy; pub mod replanner; pub mod router; +pub mod run_history; pub mod runtime_for; pub mod script_run; pub mod spawn; diff --git a/crates/gaviero-core/src/swarm/pipeline.rs b/crates/gaviero-core/src/swarm/pipeline.rs index 6f6ac61e..51f457d3 100644 --- a/crates/gaviero-core/src/swarm/pipeline.rs +++ b/crates/gaviero-core/src/swarm/pipeline.rs @@ -560,6 +560,17 @@ pub async fn execute( // Execution state tracks per-node progress (populated as nodes complete) let mut exec_state = initial_state.unwrap_or_else(|| ExecutionState::new_from_plan(plan)); let plan_hash = plan.hash(); + // On resume, agents the checkpoint shows as completed are skipped, so + // their branches never reach `all_manifests`. If the earlier run was + // interrupted before its merge phase, those branches are still unmerged; + // the merge phase below picks them up (only if still pending). + let resumed_manifests: Vec = exec_state + .node_states + .values() + .filter(|node| node.status == NodeStatus::Completed) + .filter_map(|node| node.result.clone()) + .filter(|manifest| manifest.branch.is_some()) + .collect(); // Filter out already-completed nodes if resuming let work_units: Vec = work_units @@ -862,6 +873,16 @@ pub async fn execute( for init_id in &resume.satisfied_init_units { exec_state.set_status(init_id, NodeStatus::Completed); } + // A checkpoint (`--resume`) marks a loop body agent Completed for + // whatever pass it ran last, not for the pass resumed here. The + // tier dispatch runs the loop's first pass and skips Completed + // agents, so left as is it would skip the resumed pass and the + // loop would judge and continue past a pass that never ran. + for agent_id in &lc.agent_ids { + if exec_state.status(agent_id) == NodeStatus::Completed { + exec_state.set_status(agent_id, NodeStatus::Pending); + } + } lc.iter_start = resume.resume_iter_start; observer.on_loop_resumed(&resume); } @@ -2597,7 +2618,17 @@ pub async fn execute( } false }; - for manifest in &all_manifests { + // `true` marks a branch left by an earlier, interrupted run. + let stranded = resumed_manifests + .iter() + .filter(|m| { + m.branch + .as_deref() + .is_some_and(|b| merge::branch_pending_merge(&config.workspace_root, b)) + }) + .map(|m| (true, m)); + for (from_earlier_run, manifest) in stranded.chain(all_manifests.iter().map(|m| (false, m))) + { if let Some(ref branch) = manifest.branch && matches!(manifest.status, AgentStatus::Completed) { @@ -2605,7 +2636,26 @@ pub async fn execute( tracing::debug!("skipping merge of stacked iteration branch '{}'", branch); continue; } - let mut result = merge::merge_branch(&config.workspace_root, branch)?; + let mut result = match merge::merge_branch(&config.workspace_root, branch) { + Ok(result) => result, + // This run's work still merges; a leftover branch that + // will not is reported (the run ends with failures) and + // left for the user to merge. + Err(e) if from_earlier_run => { + tracing::warn!("could not merge {branch} from the earlier run: {e:#}"); + observer.on_merge_conflict( + branch, + &[format!("merge failed, branch kept for a manual merge: {e}")], + ); + all_merges.push(MergeResult { + work_unit_id: manifest.work_unit_id.clone(), + success: false, + conflicts: Vec::new(), + }); + continue; + } + Err(e) => return Err(e), + }; if !result.success && !result.conflicts.is_empty() { let files: Vec = result .conflicts diff --git a/crates/gaviero-core/src/swarm/run_history.rs b/crates/gaviero-core/src/swarm/run_history.rs new file mode 100644 index 00000000..e106da4e --- /dev/null +++ b/crates/gaviero-core/src/swarm/run_history.rs @@ -0,0 +1,453 @@ +//! A record of every script run started from the TUI's SWARM panel. +//! +//! One folder per run under `/.gaviero/swarm-runs//`: +//! `run.json` ([`SwarmRunRecord`]: what was run, with which inputs, how it +//! ended, every agent's final state) and `agents/.json` (each agent's +//! saved output, in whatever form the host writes it). +//! +//! A run is continued from the pipeline's own checkpoint +//! (`/.gaviero/state/.json`, [`ExecutionState`]). Every run +//! of the same plan writes that one file, so only the newest record of a plan +//! can be continued — see [`resume_checkpoint`]. + +use std::path::{Path, PathBuf}; + +use anyhow::{Context, Result}; +use serde::{Deserialize, Serialize}; + +use super::execution_state::{ExecutionState, NodeStatus}; +use super::models::AgentStatus; +use crate::types::ModelTier; + +/// Runs kept per folder; older ones are pruned when a run starts. +pub const KEEP_RUNS: usize = 50; + +const RECORD_VERSION: u32 = 1; +const RECORD_FILE: &str = "run.json"; +const AGENTS_DIR: &str = "agents"; + +/// How a run ended. `Running` on a record no process is executing means the +/// run was interrupted (the editor closed, the machine slept or crashed). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum RunOutcome { + Running, + Completed, + CompletedWithFailures, + Failed, + Stopped, +} + +impl RunOutcome { + pub fn label(self) -> &'static str { + match self { + Self::Running => "running", + Self::Completed => "completed", + Self::CompletedWithFailures => "completed with failures", + Self::Failed => "failed", + Self::Stopped => "stopped", + } + } +} + +/// `--tier-client` / `--tier-effort` for one tier, as the run was given them. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct RunTierOverride { + pub tier: String, + pub client: Option, + pub effort: Option, +} + +/// Everything the script was compiled with, after the host resolved it +/// (`$skill` expanded into the prompt, file vars located). Compiling the same +/// script with these again gives the same plan. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct RunInputs { + /// The folder chosen for the run, before the script anchored it. + pub form_root: PathBuf, + pub workflow: Option, + pub profile: Option, + pub prompt: Option, + pub vars: Vec<(String, String)>, + pub params: Vec<(String, String)>, + pub agent_tiers: Vec<(String, String)>, + pub tier_overrides: Vec, +} + +/// One agent's final state in a run. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct RunAgentRecord { + pub id: String, + pub status: AgentStatus, + pub detail: String, + pub branch: Option, + #[serde(default)] + pub modified_files: Vec, + pub tier: Option, + pub backend: Option, + pub elapsed_secs: Option, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct SwarmRunRecord { + pub version: u32, + pub id: String, + pub script_path: PathBuf, + pub script_label: String, + /// The run root: where the pipeline ran and wrote its checkpoint. + pub root: PathBuf, + pub plan_hash: String, + /// Unix seconds. + pub started_at: i64, + pub ended_at: Option, + pub outcome: RunOutcome, + #[serde(default)] + pub message: String, + #[serde(default)] + pub cost_usd: f64, + /// Times the run was continued after it stopped. + #[serde(default)] + pub resumes: u32, + /// The run's merges were undone: its checkpoint no longer describes the + /// tree, so it cannot be continued. + #[serde(default)] + pub reverted: bool, + pub inputs: RunInputs, + #[serde(default)] + pub agents: Vec, +} + +impl SwarmRunRecord { + pub fn new( + id: String, + script_path: PathBuf, + script_label: String, + root: PathBuf, + plan_hash: String, + inputs: RunInputs, + ) -> Self { + Self { + version: RECORD_VERSION, + id, + script_path, + script_label, + root, + plan_hash, + started_at: chrono::Utc::now().timestamp(), + ended_at: None, + outcome: RunOutcome::Running, + message: String::new(), + cost_usd: 0.0, + resumes: 0, + reverted: false, + inputs, + agents: Vec::new(), + } + } + + /// Agents that completed, out of all recorded. + pub fn completed_count(&self) -> usize { + self.agents + .iter() + .filter(|a| matches!(a.status, AgentStatus::Completed)) + .count() + } +} + +/// The run records of one folder. +#[derive(Debug, Clone)] +pub struct RunHistory { + dir: PathBuf, +} + +impl RunHistory { + /// `/.gaviero/swarm-runs`. + pub fn for_folder(folder: &Path) -> Self { + Self { + dir: folder.join(".gaviero").join("swarm-runs"), + } + } + + pub fn dir(&self) -> &Path { + &self.dir + } + + /// A new run id; ids sort in start order. + pub fn new_run_id() -> String { + chrono::Utc::now().format("%Y%m%d-%H%M%S-%3f").to_string() + } + + fn run_dir(&self, run_id: &str) -> PathBuf { + self.dir.join(safe_name(run_id)) + } + + fn agent_log_path(&self, run_id: &str, agent: &str) -> PathBuf { + self.run_dir(run_id) + .join(AGENTS_DIR) + .join(format!("{}.json", safe_name(agent))) + } + + /// Write the record (tmp file + rename, so a crash never leaves half). + pub fn save(&self, record: &SwarmRunRecord) -> Result<()> { + let dir = self.run_dir(&record.id); + std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?; + let json = serde_json::to_string_pretty(record)?; + write_atomic(&dir.join(RECORD_FILE), json.as_bytes()) + } + + /// Every readable record, newest first. + pub fn list(&self) -> Vec { + let Ok(entries) = std::fs::read_dir(&self.dir) else { + return Vec::new(); + }; + let mut records: Vec = entries + .flatten() + .filter_map(|e| std::fs::read_to_string(e.path().join(RECORD_FILE)).ok()) + .filter_map(|json| serde_json::from_str(&json).ok()) + .collect(); + records.sort_by(|a, b| { + b.started_at + .cmp(&a.started_at) + .then_with(|| b.id.cmp(&a.id)) + }); + records + } + + pub fn write_agent_log(&self, run_id: &str, agent: &str, content: &str) -> Result<()> { + let path = self.agent_log_path(run_id, agent); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + write_atomic(&path, content.as_bytes()) + } + + pub fn read_agent_log(&self, run_id: &str, agent: &str) -> Option { + std::fs::read_to_string(self.agent_log_path(run_id, agent)).ok() + } + + /// Delete all but the newest `keep` runs. Returns how many went. + pub fn prune(&self, keep: usize) -> usize { + let Ok(entries) = std::fs::read_dir(&self.dir) else { + return 0; + }; + let mut dirs: Vec = entries + .flatten() + .map(|e| e.path()) + .filter(|p| p.join(RECORD_FILE).is_file()) + .collect(); + // Ids start with the UTC start time, so name order is start order. + dirs.sort(); + let excess = dirs.len().saturating_sub(keep); + dirs.into_iter() + .take(excess) + .filter(|dir| std::fs::remove_dir_all(dir).is_ok()) + .count() + } +} + +/// The checkpoint `record` can be continued from, if any. +/// +/// None when the run completed or was undone, when a newer record of the +/// same plan in the same root exists (its run wrote over the shared +/// checkpoint), or when the checkpoint is gone or has nothing left to run. +/// `records` is the folder's history; `record` may be one of them. +pub fn resume_checkpoint( + record: &SwarmRunRecord, + records: &[SwarmRunRecord], +) -> Option { + if record.outcome == RunOutcome::Completed || record.reverted { + return None; + } + let superseded = records.iter().any(|other| { + other.id != record.id + && other.root == record.root + && other.plan_hash == record.plan_hash + && (other.started_at, &other.id) > (record.started_at, &record.id) + }); + if superseded { + return None; + } + let state = ExecutionState::load(&record.root, &record.plan_hash).ok()??; + state + .node_states + .values() + .any(|node| node.status != NodeStatus::Completed) + .then_some(state) +} + +fn safe_name(name: &str) -> String { + name.chars() + .map(|c| { + if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-') { + c + } else { + '_' + } + }) + .collect() +} + +fn write_atomic(path: &Path, bytes: &[u8]) -> Result<()> { + let tmp = path.with_extension("json.tmp"); + std::fs::write(&tmp, bytes).with_context(|| format!("writing {}", tmp.display()))?; + std::fs::rename(&tmp, path).with_context(|| format!("replacing {}", path.display()))?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::swarm::execution_state::NodeState; + + fn record(id: &str, root: &Path, hash: &str, started_at: i64) -> SwarmRunRecord { + let mut record = SwarmRunRecord::new( + id.to_string(), + root.join("s.gaviero"), + "s".to_string(), + root.to_path_buf(), + hash.to_string(), + RunInputs { + form_root: root.to_path_buf(), + prompt: Some("the moon".into()), + vars: vec![("OUT_DIR".into(), "out".into())], + ..Default::default() + }, + ); + record.started_at = started_at; + record + } + + fn checkpoint(root: &Path, hash: &str, nodes: &[(&str, NodeStatus)]) { + let mut state = ExecutionState::default(); + for (id, status) in nodes { + state.node_states.insert( + id.to_string(), + NodeState { + status: status.clone(), + ..Default::default() + }, + ); + } + state.save(root, hash).unwrap(); + } + + #[test] + fn records_round_trip_newest_first() { + let dir = tempfile::tempdir().unwrap(); + let history = RunHistory::for_folder(dir.path()); + let mut old = record("20260101-000000-000", dir.path(), "h", 10); + old.agents.push(RunAgentRecord { + id: "writer".into(), + status: AgentStatus::Failed("limit".into()), + detail: "limit".into(), + branch: Some("gaviero/writer".into()), + modified_files: vec!["a.md".into()], + tier: Some(ModelTier::Expensive), + backend: Some("claude:opus".into()), + elapsed_secs: Some(3), + }); + let new = record("20260101-000001-000", dir.path(), "h", 20); + history.save(&old).unwrap(); + history.save(&new).unwrap(); + + let listed = history.list(); + assert_eq!(listed.len(), 2); + assert_eq!(listed[0].id, new.id); + assert_eq!(listed[1], old); + assert!( + !history.run_dir(&old.id).join("run.json.tmp").exists(), + "no tmp file left behind" + ); + } + + #[test] + fn agent_logs_round_trip_under_a_safe_name() { + let dir = tempfile::tempdir().unwrap(); + let history = RunHistory::for_folder(dir.path()); + history.write_agent_log("r1", "map[0]/x", "[1,2]").unwrap(); + assert_eq!(history.read_agent_log("r1", "map[0]/x").as_deref(), Some("[1,2]")); + assert!(history.dir().join("r1/agents/map_0__x.json").is_file()); + assert_eq!(history.read_agent_log("r1", "other"), None); + } + + #[test] + fn unreadable_records_are_skipped() { + let dir = tempfile::tempdir().unwrap(); + let history = RunHistory::for_folder(dir.path()); + history.save(&record("good", dir.path(), "h", 1)).unwrap(); + let bad = history.dir().join("bad"); + std::fs::create_dir_all(&bad).unwrap(); + std::fs::write(bad.join("run.json"), "{ not json").unwrap(); + let ids: Vec = history.list().into_iter().map(|r| r.id).collect(); + assert_eq!(ids, vec!["good".to_string()]); + } + + #[test] + fn prune_keeps_the_newest() { + let dir = tempfile::tempdir().unwrap(); + let history = RunHistory::for_folder(dir.path()); + for i in 0..5 { + history + .save(&record(&format!("2026010{i}-000000-000"), dir.path(), "h", i)) + .unwrap(); + } + assert_eq!(history.prune(3), 2); + let ids: Vec = history.list().into_iter().map(|r| r.id).collect(); + assert_eq!( + ids, + vec!["20260104-000000-000", "20260103-000000-000", "20260102-000000-000"] + ); + assert_eq!(history.prune(3), 0); + } + + #[test] + fn an_unfinished_run_resumes_from_its_checkpoint() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + checkpoint(root, "h", &[("a", NodeStatus::Completed), ("b", NodeStatus::Running)]); + let run = record("r1", root, "h", 1); + let state = resume_checkpoint(&run, std::slice::from_ref(&run)).expect("resumable"); + assert_eq!(state.status("a"), NodeStatus::Completed); + assert_eq!(state.status("b"), NodeStatus::Running); + } + + #[test] + fn nothing_to_resume_in_these_cases() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + + // No checkpoint. + let run = record("r1", root, "h", 1); + assert!(resume_checkpoint(&run, &[]).is_none()); + + // Every node completed. + checkpoint(root, "done", &[("a", NodeStatus::Completed)]); + let run = record("r2", root, "done", 1); + assert!(resume_checkpoint(&run, &[]).is_none()); + + // The run completed. + checkpoint(root, "h", &[("a", NodeStatus::HardFailure)]); + let mut run = record("r3", root, "h", 1); + run.outcome = RunOutcome::Completed; + assert!(resume_checkpoint(&run, &[]).is_none()); + + // The run was undone. + let mut run = record("r3b", root, "h", 1); + run.outcome = RunOutcome::CompletedWithFailures; + assert!(resume_checkpoint(&run, &[]).is_some()); + run.reverted = true; + assert!(resume_checkpoint(&run, &[]).is_none()); + + // A newer run of the same plan in the same root wrote over the + // checkpoint; a newer one elsewhere or of another plan does not count. + let older = record("r4", root, "h", 1); + let newer = record("r5", root, "h", 2); + let elsewhere = record("r6", &root.join("sub"), "h", 3); + let other_plan = record("r7", root, "other", 4); + assert!(resume_checkpoint(&older, &[older.clone(), newer.clone()]).is_none()); + assert!(resume_checkpoint(&newer, &[older.clone(), newer.clone()]).is_some()); + assert!( + resume_checkpoint(&older, &[older.clone(), elsewhere, other_plan]).is_some() + ); + } +} diff --git a/crates/gaviero-core/tests/swarm_loop_resume_checkpoint.rs b/crates/gaviero-core/tests/swarm_loop_resume_checkpoint.rs new file mode 100644 index 00000000..1276b90e --- /dev/null +++ b/crates/gaviero-core/tests/swarm_loop_resume_checkpoint.rs @@ -0,0 +1,199 @@ +//! A loop resumed from its artefacts runs the resumed pass, even when the +//! run is also resumed from a node checkpoint (`gaviero-cli --resume`). +//! +//! The checkpoint marks a loop body agent Completed for the last pass it +//! ran. The tier dispatch runs a loop's first pass and skips Completed +//! agents, so before the fix a `--resume` skipped the pass the artefacts said +//! to resume at and the loop moved past it: a run restarted after pass 4 +//! re-ran from pass 2 (no artefact resume) or would skip pass 5. + +use std::collections::HashMap; +use std::path::Path; + +use gaviero_core::observer::{AcpObserver, SwarmObserver}; +use gaviero_core::repo_map::SpecificityConfig; +use gaviero_core::swarm::execution_state::{ExecutionState, NodeStatus}; +use gaviero_core::swarm::models::{AgentStatus, WorkUnit}; +use gaviero_core::swarm::pipeline::{SwarmConfig, execute}; +use gaviero_core::swarm::plan::{CompiledPlan, ExecutionMode, LoopConfig, LoopUntilCondition}; +use gaviero_core::types::{FileScope, ModelTier, PrivacyLevel}; +use wiremock::matchers::{method, path}; +use wiremock::{Mock, MockServer, ResponseTemplate}; + +#[allow(deprecated)] +fn writer() -> WorkUnit { + WorkUnit { + id: "writer".to_string(), + description: "write a pass".to_string(), + scope: FileScope { + owned_paths: vec!["out/writer-v*.md".to_string()], + read_only_paths: vec![], + interface_contracts: HashMap::new(), + }, + produces_json: vec![], + produces: vec![], + depends_on: vec![], + backend: Default::default(), + model: Some("ollama:qwen".to_string()), + effort: None, + extra: vec![], + tier: ModelTier::Cheap, + privacy: PrivacyLevel::Public, + coordinator_instructions: "Write pass {{ITER}}.".to_string(), + estimated_tokens: 0, + max_retries: 1, + timeout_secs: 3600, + escalation_tier: None, + read_namespaces: None, + write_namespace: None, + memory_importance: None, + staleness_sources: vec![], + memory_read_query: None, + memory_read_limit: None, + memory_write_content: None, + impact_scope: false, + context_callers_of: vec![], + context_tests_for: vec![], + context_depth: 2, + extra_allowed_tools: vec![], + } +} + +/// One pass only: the tier dispatch runs it and the loop block adds none. +fn one_pass_loop() -> LoopConfig { + LoopConfig { + agent_ids: vec!["writer".to_string()], + until: LoopUntilCondition::Command("exit 1".to_string()), + max_iterations: 1, + iter_start: 1, + strict_judge: true, + stability: 1, + judge_timeout_secs: 120, + branch_chain: Default::default(), + consensus_mode: Default::default(), + verdict_output_dir: Some("out".to_string()), + irreconcilable_after: 2, + baseline_units: Vec::new(), + } +} + +struct Quiet; +impl SwarmObserver for Quiet { + fn on_phase_changed(&self, _phase: &str) {} + fn on_agent_state_changed(&self, _id: &str, _status: &AgentStatus, _detail: &str) {} + fn on_tier_started(&self, _current: usize, _total: usize) {} + fn on_merge_conflict(&self, _branch: &str, _files: &[String]) {} + fn on_completed(&self, _result: &gaviero_core::swarm::models::SwarmResult) {} +} +impl AcpObserver for Quiet { + fn on_stream_chunk(&self, _text: &str) {} + fn on_tool_call_started(&self, _tool_name: &str) {} + fn on_streaming_status(&self, _status: &str) {} + fn on_message_complete(&self, _role: &str, _content: &str) {} + fn on_proposal_deferred(&self, _path: &Path, _old: Option<&str>, _new: &str) {} +} + +fn config_for(workspace: &Path, ollama_url: &str) -> SwarmConfig { + SwarmConfig { + execution_mode: ExecutionMode::Document, + max_parallel: 1, + workspace_root: workspace.to_path_buf(), + model: "ollama:qwen".to_string(), + ollama_base_url: Some(ollama_url.to_string()), + use_worktrees: false, + read_namespaces: vec!["default".to_string()], + write_namespace: "default".to_string(), + context_files: vec![], + worktree_context_paths: vec![], + excludes: vec![], + memory_writer: None, + mcp_config: None, + specificity: SpecificityConfig::default(), + swarm_extra_tools: vec![], + tool_policy: None, + exposed_tools: None, + extract_agent_findings: false, + resume_from_artifacts: true, + knowledge_invalidation: None, + run_timeout_secs: 120, + chat_injection: Default::default(), + skills_emit: Default::default(), + skill_catalog: None, + memory_root: None, + graph_db_path: None, + } +} + +fn ndjson_turn(file: &str) -> String { + let body = format!("\npass body\n"); + let chunk = serde_json::json!({ + "model": "qwen", + "message": { "role": "assistant", "content": body }, + "done": false + }); + let done = serde_json::json!({ + "model": "qwen", "done": true, "total_duration": 1_000_000, + "eval_count": 2, "prompt_eval_count": 5 + }); + format!("{chunk}\n{done}") +} + +#[tokio::test] +async fn a_checkpoint_resume_still_runs_the_pass_the_artefacts_resume_at() { + let tmp = tempfile::tempdir().expect("tempdir"); + let workspace = tmp.path(); + std::fs::create_dir_all(workspace.join("out")).unwrap(); + // Passes 1 and 2 are on disk. + std::fs::write(workspace.join("out/writer-v1.md"), "pass 1\n").unwrap(); + std::fs::write(workspace.join("out/writer-v2.md"), "pass 2\n").unwrap(); + + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/api/chat")) + .respond_with( + ResponseTemplate::new(200) + .set_body_string(ndjson_turn("out/writer-v3.md")) + .insert_header("content-type", "application/x-ndjson"), + ) + .mount(&server) + .await; + + let mut plan = CompiledPlan::from_work_units(vec![writer()], Some(1)); + plan.execution_mode = ExecutionMode::Document; + plan.loop_configs = vec![one_pass_loop()]; + + // The node checkpoint of the earlier run: the loop agent completed + // (its last pass), as `gaviero-cli --resume` loads it. + let mut checkpoint = ExecutionState::new_from_plan(&plan); + checkpoint.set_status("writer", NodeStatus::Completed); + + let make_obs = |_id: &str| -> Box { Box::new(Quiet) }; + let result = execute( + &plan, + &config_for(workspace, &server.uri()), + Some(checkpoint), + None, + &Quiet, + make_obs, + ) + .await; + assert!(result.is_ok(), "{:?}", result.err().map(|e| format!("{e:#}"))); + + let prompts: Vec = server + .received_requests() + .await + .unwrap_or_default() + .iter() + .map(|r| String::from_utf8_lossy(&r.body).into_owned()) + .collect(); + assert_eq!(prompts.len(), 1, "the resumed pass ran exactly once"); + assert!( + prompts[0].contains("Write pass 3."), + "it ran as pass 3, after the two on disk" + ); + assert_eq!( + std::fs::read_to_string(workspace.join("out/writer-v1.md")).unwrap(), + "pass 1\n", + "earlier passes are untouched" + ); +} diff --git a/crates/gaviero-dsl/examples/deep_explore.gaviero b/crates/gaviero-dsl/examples/deep_explore.gaviero index 70923f98..9976147c 100644 --- a/crates/gaviero-dsl/examples/deep_explore.gaviero +++ b/crates/gaviero-dsl/examples/deep_explore.gaviero @@ -291,7 +291,29 @@ prompt curate-body #" when little is open. `[]` when nothing open is worth a lane. Never assign a question already in archive.md. - Write {{OUT_DIR}}/frontier-v{{ITER}}.json: + E. Keep the frontier small. It is read and written every pass, and + re-typing a large file by hand is slow and loses detail. + - From pass 2 on, never re-type the whole frontier. Copy + {{OUT_DIR}}/frontier-v{{PREV_ITER}}.json to + {{OUT_DIR}}/frontier-v{{ITER}}.json with a command or a short + script, then change only what this pass changed: edit those + entries, or load the JSON in a short Python script, apply the + changes and write it back. Then load the result as JSON to check + it parses and that no question id was lost. + - `question`: at most 250 characters. Narrowing a question replaces + its wording; the old wording goes to journal.md, not the field. + - `why`: at most 300 characters, the current evidence hook only. + Replace it, never append to it; its history belongs in + journal.md. + - closed and dead questions keep only id, question, status, + operator, parent, depth and supported_claims. A parked question + also keeps `why`: the experiment or data that would answer it. + - `leading_claims`: at most 5, each at most 200 characters. + - No top-level keys beyond the ones below. + A frontier already over these limits is brought within them this + pass, once, by script; say so in journal.md. + + Write {{OUT_DIR}}/frontier-v{{ITER}}.json (kept small as in E): {"pass": {{ITER}}, "collection": "", "questions": [{"id": "q7", "question": "…", "kind": "core|mechanism|method|contrarian|adjacent|gap|bridge|blind_spot", "operator": "seed|decompose|falsify|bridge|analogy|recency|gap|method_transfer|child", @@ -452,10 +474,20 @@ prompt verify-body #" even if it said "exact". Flag claims that two lanes state in contradiction. + Failures. A timeout or 429: wait, retry once, then move on. If the + `{{ACADEMIC_SERVER}}` server is unreachable or its tools are not in + your toolset, do not stop and do not ask what to do: this run is + unattended and nobody will answer. Check what you can on the web + (WebSearch/WebFetch, if you have them): existence, title and year, + with "checked_via": "web". Mark every claim you could not check + against the source's own text `cannot_verify` with reason "academic + server unavailable", and say so in "saturation". Never give a + `supported` verdict you did not check. Always write the file below. + Write {{OUT_DIR}}/audit-v{{ITER}}.json: {"pass": {{ITER}}, "claims": [{"finding": "", "index": 0, "verdict": "supported|partial|not_supported|cannot_verify", - "reason": "…", "passage": "", "checked_via": "corpus|fulltext|abstract|citations", + "reason": "…", "passage": "", "checked_via": "corpus|fulltext|abstract|citations|web", "retracted": false, "resolved": true, "sampled": false}], "fidelity": [{"finding": "", "verdict": "exact|narrowed|substituted", "note": "…"}], "contradictions": [{"a": "#", "b": "#", "note": "…"}], @@ -595,7 +627,9 @@ agent curator { "{{OUT_DIR}}/frontier-v{{ITER}}.json" "{{OUT_DIR}}/assignments-v{{ITER}}.json" ] - timeout 1800 + // Reading every pass's findings and the frontier takes longer as the + // run grows; 30 minutes was not enough by pass 5 of a real run. + timeout 3600 prompt curate-body } diff --git a/crates/gaviero-relay/tests/e2e.rs b/crates/gaviero-relay/tests/e2e.rs index a3149607..c0cb78fb 100644 --- a/crates/gaviero-relay/tests/e2e.rs +++ b/crates/gaviero-relay/tests/e2e.rs @@ -54,6 +54,7 @@ async fn sidecar(bundle: Vec, token: String) -> SpawnedServer { registry: None, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: None, confirm_required: Vec::new(), allowed_slash_commands: Vec::new(), limits: Limits { diff --git a/crates/gaviero-remote/PROTOCOL.md b/crates/gaviero-remote/PROTOCOL.md index bdf3c876..844705b4 100644 --- a/crates/gaviero-remote/PROTOCOL.md +++ b/crates/gaviero-remote/PROTOCOL.md @@ -1,4 +1,4 @@ -# Gaviero Remote Protocol — v1.10 (1.0 frozen; 1.1 through 1.10 additive) +# Gaviero Remote Protocol — v1.11 (1.0 frozen; 1.1 through 1.11 additive) Normative wire contract between the gaviero TUI sidecar (server) and the mobile client. 1.0 was frozen by Plan A V3 unit A0 on 2026-08-05; 1.1 was added by Plan C V1 unit C0 on @@ -8,10 +8,11 @@ truth); one example fixture per frame lives in [`fixtures/`](fixtures/). Where p schema disagree, the schema wins. ``` -PROTOCOL_VERSION = { major: 1, minor: 10 } +PROTOCOL_VERSION = { major: 1, minor: 11 } WebSocket path = /v1/ws Subprotocol = gaviero.v1 Instances path = /v1/instances (HTTPS GET, 1.1, on every instance) +CLI runs path = /v1/cli-runs (HTTPS GET, 1.11, on every instance) ``` The document version of the plan that produced this (V3 / Plan C V1) is unrelated to the wire @@ -221,7 +222,7 @@ share a folder name, each gains the nearest parent folder that tells them apart `hello.capabilities` is an array of strings, **empty in 1.0**; the shape is frozen so minor versions can advertise features. 1.1 advertises `latest_page` and `instances`; 1.2 adds -`turn_review`; 1.3 adds `file_browser`; 1.4 adds `permission_turn_grant`; 1.6 adds `permission_grants`; 1.7 adds `shell_history`; 1.8 adds `skill_completions`; 1.10 adds `swarm_activity`. Clients +`turn_review`; 1.3 adds `file_browser`; 1.4 adds `permission_turn_grant`; 1.6 adds `permission_grants`; 1.7 adds `shell_history`; 1.8 adds `skill_completions`; 1.10 adds `swarm_activity`; 1.11 adds `cli_runs`. Clients ignore unknown entries. ## DTOs @@ -538,6 +539,50 @@ No run since the desktop started ⇒ `{ "run": null, "selected_id": null, "outpu The Flutter client polls it about once a second while its Swarm section is on screen. Older servers receive no swarm requests. +## CLI runs (`cli_runs` capability, 1.11) + +`gaviero-cli` runs a script through the same pipeline as the desktop SWARM panel, but has no +listener of its own. Each run publishes a record on its machine (`~/.gaviero/remote/cli-runs/`, +written at most once a second and at least every 30 s while it runs); every instance listener of +that machine serves those records over HTTPS, like `GET /v1/instances` — so the list works from the +instance picker without a WebSocket, and a CLI run is visible while at least one instance with +remote runs on the machine. Read-only: no start, stop or continue. + +Both resources need `Authorization: Bearer` (`401` otherwise), answer `Cache-Control: no-store`, +and share the listener's 10 requests/s budget with `GET /v1/instances` (`429` above it). A server +without `cli_runs` answers `404`. + +`GET /v1/cli-runs` — every kept record (the CLI keeps the newest 20), running ones first, then +newest first: + +```json +{ "host": "desk.gaviero.example.com", "generated_at": "2026-11-17T10:00:05Z", + "runs": [ { "id": "20261117-095800-123-4242", "script": "deep_explore", "folder": "gaviero", + "state": "running", "phase": "running", "running": true, + "started_at": "2026-11-17T09:58:00Z", "updated_at": "2026-11-17T10:00:04Z", + "cost_usd": 0.042, "counts": { "running": 1, "queued": 2, "done": 0, "failed": 0 }, + "agents": 3 } ] } +``` + +- `state ∈ running | completed | completed_with_failures | failed | interrupted`. `interrupted` is a + record still marked running whose last write is older than 90 s: the CLI process went away. + `phase` is the pipeline's display string while running, `interrupted` when interrupted. +- `folder` is the name of the folder the run ran in — never a path. `script` is the script's file + name without extension (`task` / `work units` for those modes), plus ` · ` when one was + chosen. + +`GET /v1/cli-runs/?agent_id=&output_token=` (both query parameters optional) — +one run, in exactly the `request_swarm` result shape (§Swarm activity), with four more `run` keys: +`id`, `folder`, `state`, `started_at`. `run.run_id` is a number (the start time in Unix ms) and +`output.token` is `:`, with the same `unchanged` rule. Without `agent_id` the +output follows the agent that started last (unless the one followed is still running). In an +`interrupted` run, agents that never finished read `status: failed`, `error: "interrupted"`. +Unknown `` ⇒ `404`. + +The Flutter client lists CLI runs in a tab of its instance picker (polling the list about every +3 s while shown) and opens one in a screen like the Swarm section, polling the run about once a +second. Older servers answer `404`; the client says the computer needs a newer Gaviero. + ## File browser (`file_browser` capability) Servers advertising `hello.capabilities: ["file_browser", ...]` implement the read-only workspace diff --git a/crates/gaviero-remote/protocol.schema.json b/crates/gaviero-remote/protocol.schema.json index 32ba4850..da710e1a 100644 --- a/crates/gaviero-remote/protocol.schema.json +++ b/crates/gaviero-remote/protocol.schema.json @@ -1,5 +1,6 @@ { "$comment": "Generated from gaviero-remote DTOs — do not edit by hand.", + "cli_runs_path": "/v1/cli-runs", "client_envelope": { "$defs": { "ClientHello": { @@ -124,7 +125,7 @@ ] }, "ProtocolVersion": { - "description": "Wire protocol version. Deliberately unrelated to any plan-document\nversion: this is the first protocol ever on the wire, so it starts at\nmajor 1 (Plan A V3 §0.1 item 1). Minor 1 (Plan C V1 §4) is additive:\n`hello.machine`, capability strings, optional\n`request_messages.before_seq`, and the `GET /v1/instances` resource.\nMinor 2 is additive: the turn-review frames, `snapshot.open_turn_reviews`,\nand two error codes. Minor 3 is additive: the `file_browser` capability\nwith the `list_directory` / `read_file` client frames. Minor 4 is\nadditive: the `permission_turn_grant` capability, optional\n`permission_request.mcp_server` and `permission_decision.grant`. Minor 5\nis additive: optional `TurnReviewFile.folder` and\n`turn_review_action.folder` (no capability — a client groups by `folder`\nwhen files carry it, and echoes it only then). Minor 6 is additive: the\n`permission_grants` capability, optional `permission_request.grants`, and\nthree more `PermissionGrant` values (`tool_session`,\n`mcp_server_session`, `turn_all`) — the desktop's `a` / `A` / `p` keys.\nMinor 7 is additive: the `shell_history` capability and optional\n`request_terminals.history`. Minor 8 is additive: the `skill_completions`\ncapability and the `request_skill_completions` client frame. Minor 9 is\nadditive: optional `InstanceInfo.last_activity_at` in `GET /v1/instances`.\nMinor 10 is additive: the `swarm_activity` capability and the\n`request_swarm` client frame.", + "description": "Wire protocol version. Deliberately unrelated to any plan-document\nversion: this is the first protocol ever on the wire, so it starts at\nmajor 1 (Plan A V3 §0.1 item 1). Minor 1 (Plan C V1 §4) is additive:\n`hello.machine`, capability strings, optional\n`request_messages.before_seq`, and the `GET /v1/instances` resource.\nMinor 2 is additive: the turn-review frames, `snapshot.open_turn_reviews`,\nand two error codes. Minor 3 is additive: the `file_browser` capability\nwith the `list_directory` / `read_file` client frames. Minor 4 is\nadditive: the `permission_turn_grant` capability, optional\n`permission_request.mcp_server` and `permission_decision.grant`. Minor 5\nis additive: optional `TurnReviewFile.folder` and\n`turn_review_action.folder` (no capability — a client groups by `folder`\nwhen files carry it, and echoes it only then). Minor 6 is additive: the\n`permission_grants` capability, optional `permission_request.grants`, and\nthree more `PermissionGrant` values (`tool_session`,\n`mcp_server_session`, `turn_all`) — the desktop's `a` / `A` / `p` keys.\nMinor 7 is additive: the `shell_history` capability and optional\n`request_terminals.history`. Minor 8 is additive: the `skill_completions`\ncapability and the `request_skill_completions` client frame. Minor 9 is\nadditive: optional `InstanceInfo.last_activity_at` in `GET /v1/instances`.\nMinor 10 is additive: the `swarm_activity` capability and the\n`request_swarm` client frame. Minor 11 is additive: the `cli_runs`\ncapability and the `GET /v1/cli-runs` / `GET /v1/cli-runs/`\nresources.", "properties": { "major": { "format": "uint16", @@ -885,7 +886,7 @@ "type": "object" }, "ProtocolVersion": { - "description": "Wire protocol version. Deliberately unrelated to any plan-document\nversion: this is the first protocol ever on the wire, so it starts at\nmajor 1 (Plan A V3 §0.1 item 1). Minor 1 (Plan C V1 §4) is additive:\n`hello.machine`, capability strings, optional\n`request_messages.before_seq`, and the `GET /v1/instances` resource.\nMinor 2 is additive: the turn-review frames, `snapshot.open_turn_reviews`,\nand two error codes. Minor 3 is additive: the `file_browser` capability\nwith the `list_directory` / `read_file` client frames. Minor 4 is\nadditive: the `permission_turn_grant` capability, optional\n`permission_request.mcp_server` and `permission_decision.grant`. Minor 5\nis additive: optional `TurnReviewFile.folder` and\n`turn_review_action.folder` (no capability — a client groups by `folder`\nwhen files carry it, and echoes it only then). Minor 6 is additive: the\n`permission_grants` capability, optional `permission_request.grants`, and\nthree more `PermissionGrant` values (`tool_session`,\n`mcp_server_session`, `turn_all`) — the desktop's `a` / `A` / `p` keys.\nMinor 7 is additive: the `shell_history` capability and optional\n`request_terminals.history`. Minor 8 is additive: the `skill_completions`\ncapability and the `request_skill_completions` client frame. Minor 9 is\nadditive: optional `InstanceInfo.last_activity_at` in `GET /v1/instances`.\nMinor 10 is additive: the `swarm_activity` capability and the\n`request_swarm` client frame.", + "description": "Wire protocol version. Deliberately unrelated to any plan-document\nversion: this is the first protocol ever on the wire, so it starts at\nmajor 1 (Plan A V3 §0.1 item 1). Minor 1 (Plan C V1 §4) is additive:\n`hello.machine`, capability strings, optional\n`request_messages.before_seq`, and the `GET /v1/instances` resource.\nMinor 2 is additive: the turn-review frames, `snapshot.open_turn_reviews`,\nand two error codes. Minor 3 is additive: the `file_browser` capability\nwith the `list_directory` / `read_file` client frames. Minor 4 is\nadditive: the `permission_turn_grant` capability, optional\n`permission_request.mcp_server` and `permission_decision.grant`. Minor 5\nis additive: optional `TurnReviewFile.folder` and\n`turn_review_action.folder` (no capability — a client groups by `folder`\nwhen files carry it, and echoes it only then). Minor 6 is additive: the\n`permission_grants` capability, optional `permission_request.grants`, and\nthree more `PermissionGrant` values (`tool_session`,\n`mcp_server_session`, `turn_all`) — the desktop's `a` / `A` / `p` keys.\nMinor 7 is additive: the `shell_history` capability and optional\n`request_terminals.history`. Minor 8 is additive: the `skill_completions`\ncapability and the `request_skill_completions` client frame. Minor 9 is\nadditive: optional `InstanceInfo.last_activity_at` in `GET /v1/instances`.\nMinor 10 is additive: the `swarm_activity` capability and the\n`request_swarm` client frame. Minor 11 is additive: the `cli_runs`\ncapability and the `GET /v1/cli-runs` / `GET /v1/cli-runs/`\nresources.", "properties": { "major": { "format": "uint16", @@ -956,7 +957,7 @@ "instances_path": "/v1/instances", "protocol_version": { "major": 1, - "minor": 10 + "minor": 11 }, "server_envelope": { "$defs": { @@ -1902,7 +1903,7 @@ "type": "object" }, "ProtocolVersion": { - "description": "Wire protocol version. Deliberately unrelated to any plan-document\nversion: this is the first protocol ever on the wire, so it starts at\nmajor 1 (Plan A V3 §0.1 item 1). Minor 1 (Plan C V1 §4) is additive:\n`hello.machine`, capability strings, optional\n`request_messages.before_seq`, and the `GET /v1/instances` resource.\nMinor 2 is additive: the turn-review frames, `snapshot.open_turn_reviews`,\nand two error codes. Minor 3 is additive: the `file_browser` capability\nwith the `list_directory` / `read_file` client frames. Minor 4 is\nadditive: the `permission_turn_grant` capability, optional\n`permission_request.mcp_server` and `permission_decision.grant`. Minor 5\nis additive: optional `TurnReviewFile.folder` and\n`turn_review_action.folder` (no capability — a client groups by `folder`\nwhen files carry it, and echoes it only then). Minor 6 is additive: the\n`permission_grants` capability, optional `permission_request.grants`, and\nthree more `PermissionGrant` values (`tool_session`,\n`mcp_server_session`, `turn_all`) — the desktop's `a` / `A` / `p` keys.\nMinor 7 is additive: the `shell_history` capability and optional\n`request_terminals.history`. Minor 8 is additive: the `skill_completions`\ncapability and the `request_skill_completions` client frame. Minor 9 is\nadditive: optional `InstanceInfo.last_activity_at` in `GET /v1/instances`.\nMinor 10 is additive: the `swarm_activity` capability and the\n`request_swarm` client frame.", + "description": "Wire protocol version. Deliberately unrelated to any plan-document\nversion: this is the first protocol ever on the wire, so it starts at\nmajor 1 (Plan A V3 §0.1 item 1). Minor 1 (Plan C V1 §4) is additive:\n`hello.machine`, capability strings, optional\n`request_messages.before_seq`, and the `GET /v1/instances` resource.\nMinor 2 is additive: the turn-review frames, `snapshot.open_turn_reviews`,\nand two error codes. Minor 3 is additive: the `file_browser` capability\nwith the `list_directory` / `read_file` client frames. Minor 4 is\nadditive: the `permission_turn_grant` capability, optional\n`permission_request.mcp_server` and `permission_decision.grant`. Minor 5\nis additive: optional `TurnReviewFile.folder` and\n`turn_review_action.folder` (no capability — a client groups by `folder`\nwhen files carry it, and echoes it only then). Minor 6 is additive: the\n`permission_grants` capability, optional `permission_request.grants`, and\nthree more `PermissionGrant` values (`tool_session`,\n`mcp_server_session`, `turn_all`) — the desktop's `a` / `A` / `p` keys.\nMinor 7 is additive: the `shell_history` capability and optional\n`request_terminals.history`. Minor 8 is additive: the `skill_completions`\ncapability and the `request_skill_completions` client frame. Minor 9 is\nadditive: optional `InstanceInfo.last_activity_at` in `GET /v1/instances`.\nMinor 10 is additive: the `swarm_activity` capability and the\n`request_swarm` client frame. Minor 11 is additive: the `cli_runs`\ncapability and the `GET /v1/cli-runs` / `GET /v1/cli-runs/`\nresources.", "properties": { "major": { "format": "uint16", diff --git a/crates/gaviero-remote/src/lib.rs b/crates/gaviero-remote/src/lib.rs index ae3bd3af..88f88b12 100644 --- a/crates/gaviero-remote/src/lib.rs +++ b/crates/gaviero-remote/src/lib.rs @@ -29,6 +29,10 @@ pub const SUBPROTOCOL: &str = "gaviero.v1"; /// HTTPS path of the instance directory (1.1): served on every instance /// listener, so any instance's relay route lists the whole machine. pub const INSTANCES_PATH: &str = "/v1/instances"; +/// HTTPS path of the machine's `gaviero-cli` runs (1.11, `cli_runs`): the +/// list here, one run at `/`. Served on every instance +/// listener, like [`INSTANCES_PATH`]. +pub const CLI_RUNS_PATH: &str = "/v1/cli-runs"; /// Application close codes (4000–4999). Committed in PROTOCOL.md. pub mod close_code { diff --git a/crates/gaviero-remote/src/schema.rs b/crates/gaviero-remote/src/schema.rs index 671368c3..8d597975 100644 --- a/crates/gaviero-remote/src/schema.rs +++ b/crates/gaviero-remote/src/schema.rs @@ -18,6 +18,7 @@ pub fn protocol_schema() -> Value { "ws_path": crate::WS_PATH, "subprotocol": crate::SUBPROTOCOL, "instances_path": crate::INSTANCES_PATH, + "cli_runs_path": crate::CLI_RUNS_PATH, "client_envelope": client, "server_envelope": server, "instance_directory": directory, diff --git a/crates/gaviero-remote/src/server/mod.rs b/crates/gaviero-remote/src/server/mod.rs index c9029327..485dabc1 100644 --- a/crates/gaviero-remote/src/server/mod.rs +++ b/crates/gaviero-remote/src/server/mod.rs @@ -20,7 +20,7 @@ use std::time::{Duration, Instant}; use axum::Json; use axum::Router; -use axum::extract::State; +use axum::extract::{Path, Query, State}; use axum::extract::ws::WebSocketUpgrade; use axum::http::{HeaderMap, StatusCode, header}; use axum::response::{IntoResponse, Response}; @@ -30,6 +30,22 @@ use tokio::sync::mpsc; use crate::dto::{Limits, MachineInfo, WorkspaceInfo}; +/// The machine's `gaviero-cli` runs (1.11 `cli_runs`). The host supplies +/// it — the record format belongs to `gaviero-core`, which this crate never +/// depends on — and the server only authenticates, rate-limits and routes. +/// Both calls read files: they run on the blocking pool. +pub trait CliRunSource: Send + Sync { + /// `GET /v1/cli-runs` body. + fn list(&self, host: &str) -> serde_json::Value; + /// `GET /v1/cli-runs/` body; `None` ⇒ 404. + fn detail( + &self, + id: &str, + agent_id: Option<&str>, + output_token: Option<&str>, + ) -> Option; +} + pub struct RemoteServerConfig { pub bind_addr: SocketAddr, /// Additional listeners sharing the same hub and TLS material — the @@ -60,6 +76,8 @@ pub struct RemoteServerConfig { pub registry: Option, pub heartbeat_interval: Duration, pub stale_after: Duration, + /// 1.11: serves `GET /v1/cli-runs[/]`. `None` ⇒ both answer 404. + pub cli_runs: Option>, /// Wire defaults: ping 20 s, idle 60 s. Configurable for tests only. pub ping_interval: Duration, pub idle_timeout: Duration, @@ -132,12 +150,14 @@ pub(crate) struct SharedState { registry_dir: Option, host: String, stale_after: Duration, + cli_runs: Option>, } #[derive(Clone)] pub(crate) struct AppState { shared: SharedState, - /// Per-listener token bucket for `GET /v1/instances` (10/s). + /// Per-listener token bucket for `GET /v1/instances` and + /// `GET /v1/cli-runs[/]` together (10/s). http_rate: Arc>, } @@ -188,6 +208,8 @@ fn instance_router(shared: SharedState) -> Router { Router::new() .route(crate::WS_PATH, any(ws_handler)) .route(crate::INSTANCES_PATH, get(instances_handler)) + .route(crate::CLI_RUNS_PATH, get(cli_runs_handler)) + .route(&format!("{}/{{id}}", crate::CLI_RUNS_PATH), get(cli_run_handler)) .with_state(state) } @@ -222,6 +244,7 @@ pub async fn spawn(config: RemoteServerConfig) -> Result, headers: HeaderMap) -> ); response } + +/// Bearer, then the shared rate bucket — the order `instances_handler` uses. +fn http_gate(state: &AppState, headers: &HeaderMap) -> Result<(), Response> { + if !bearer_ok(state, headers) { + return Err(StatusCode::UNAUTHORIZED.into_response()); + } + if !state.http_rate.lock().expect("http rate").take() { + return Err(StatusCode::TOO_MANY_REQUESTS.into_response()); + } + Ok(()) +} + +fn no_store(body: serde_json::Value) -> Response { + let mut response = Json(body).into_response(); + response.headers_mut().insert( + header::CACHE_CONTROL, + header::HeaderValue::from_static("no-store"), + ); + response +} + +/// `GET /v1/cli-runs` (1.11). +async fn cli_runs_handler(State(state): State, headers: HeaderMap) -> Response { + if let Err(response) = http_gate(&state, &headers) { + return response; + } + let Some(source) = state.shared.cli_runs.clone() else { + return StatusCode::NOT_FOUND.into_response(); + }; + let host = state.shared.host.clone(); + match tokio::task::spawn_blocking(move || source.list(&host)).await { + Ok(body) => no_store(body), + Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), + } +} + +#[derive(serde::Deserialize)] +struct CliRunQuery { + agent_id: Option, + output_token: Option, +} + +/// `GET /v1/cli-runs/?agent_id=&output_token=` (1.11). +async fn cli_run_handler( + State(state): State, + headers: HeaderMap, + Path(id): Path, + Query(query): Query, +) -> Response { + if let Err(response) = http_gate(&state, &headers) { + return response; + } + let Some(source) = state.shared.cli_runs.clone() else { + return StatusCode::NOT_FOUND.into_response(); + }; + let read = tokio::task::spawn_blocking(move || { + source.detail( + &id, + query.agent_id.as_deref().filter(|s| !s.is_empty()), + query.output_token.as_deref().filter(|s| !s.is_empty()), + ) + }); + match read.await { + Ok(Some(body)) => no_store(body), + Ok(None) => StatusCode::NOT_FOUND.into_response(), + Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), + } +} diff --git a/crates/gaviero-remote/src/version.rs b/crates/gaviero-remote/src/version.rs index b2ef5a9c..105e047a 100644 --- a/crates/gaviero-remote/src/version.rs +++ b/crates/gaviero-remote/src/version.rs @@ -22,14 +22,16 @@ use serde::{Deserialize, Serialize}; /// capability and the `request_skill_completions` client frame. Minor 9 is /// additive: optional `InstanceInfo.last_activity_at` in `GET /v1/instances`. /// Minor 10 is additive: the `swarm_activity` capability and the -/// `request_swarm` client frame. +/// `request_swarm` client frame. Minor 11 is additive: the `cli_runs` +/// capability and the `GET /v1/cli-runs` / `GET /v1/cli-runs/` +/// resources. #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] pub struct ProtocolVersion { pub major: u16, pub minor: u16, } -pub const PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion { major: 1, minor: 10 }; +pub const PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion { major: 1, minor: 11 }; /// `hello.capabilities` entries this server version advertises (Plan C §4.1). pub mod capability { @@ -58,6 +60,10 @@ pub mod capability { /// 1.10: `request_swarm` reads the SWARM panel's run — its agents, their /// state, and one agent's output. Read-only. pub const SWARM_ACTIVITY: &str = "swarm_activity"; + /// 1.11: `GET /v1/cli-runs` lists the machine's `gaviero-cli` runs and + /// `GET /v1/cli-runs/` reads one, in the `request_swarm` shape. + /// Read-only. + pub const CLI_RUNS: &str = "cli_runs"; } /// Why a peer's version is unacceptable. @@ -95,7 +101,7 @@ mod tests { .is_ok() ); assert!( - ours.check_compatible(&ProtocolVersion { major: 1, minor: 10 }) + ours.check_compatible(&ProtocolVersion { major: 1, minor: 11 }) .is_ok() ); } diff --git a/crates/gaviero-remote/tests/pairing_e2e.rs b/crates/gaviero-remote/tests/pairing_e2e.rs index 7574ae5c..a97f8b4c 100644 --- a/crates/gaviero-remote/tests/pairing_e2e.rs +++ b/crates/gaviero-remote/tests/pairing_e2e.rs @@ -91,6 +91,7 @@ async fn qr_payload_pairs_a_client_end_to_end() { registry: None, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: None, confirm_required: ["/autoapprove", "/yolo", "/reset", "/clear"] .iter() .map(|s| s.to_string()) @@ -301,6 +302,7 @@ async fn spawn_with( registry: None, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: None, confirm_required: Vec::new(), allowed_slash_commands: Vec::new(), limits: Limits { @@ -339,6 +341,7 @@ async fn a_stale_qr_token_cannot_pair() { registry: None, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: None, confirm_required: Vec::new(), allowed_slash_commands: Vec::new(), limits: Limits { @@ -408,6 +411,7 @@ async fn a_token_in_the_query_string_is_not_accepted() { registry: None, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: None, confirm_required: Vec::new(), allowed_slash_commands: Vec::new(), limits: Limits { diff --git a/crates/gaviero-remote/tests/relay_sni_prototype.rs b/crates/gaviero-remote/tests/relay_sni_prototype.rs index ac995170..8af9da90 100644 --- a/crates/gaviero-remote/tests/relay_sni_prototype.rs +++ b/crates/gaviero-remote/tests/relay_sni_prototype.rs @@ -168,6 +168,7 @@ async fn sidecar(bundle: Vec, token: String) -> SpawnedServer { registry: None, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: None, confirm_required: Vec::new(), allowed_slash_commands: Vec::new(), limits: Limits { diff --git a/crates/gaviero-remote/tests/server.rs b/crates/gaviero-remote/tests/server.rs index 16cf5c73..77f5fb30 100644 --- a/crates/gaviero-remote/tests/server.rs +++ b/crates/gaviero-remote/tests/server.rs @@ -65,6 +65,7 @@ fn test_config(tls: &TestTls) -> RemoteServerConfig { registry: None, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: None, confirm_required: vec![ "/autoapprove".into(), "/yolo".into(), @@ -1142,3 +1143,97 @@ async fn instances_get_flood_returns_429() { } assert!(saw_429, "flooding GET /v1/instances must hit the 10/s cap"); } + +// ── 1.11: GET /v1/cli-runs ─────────────────────────────────────── + +use gaviero_remote::CLI_RUNS_PATH; +use gaviero_remote::server::CliRunSource; + +/// One run, `run-1`; records what each detail call asked for. +#[derive(Default)] +struct FakeCliRuns { + asked: std::sync::Mutex, Option)>>, +} + +impl CliRunSource for FakeCliRuns { + fn list(&self, host: &str) -> serde_json::Value { + serde_json::json!({ "host": host, "runs": [{ "id": "run-1", "state": "running" }] }) + } + + fn detail( + &self, + id: &str, + agent_id: Option<&str>, + output_token: Option<&str>, + ) -> Option { + self.asked.lock().unwrap().push(( + id.to_string(), + agent_id.map(str::to_string), + output_token.map(str::to_string), + )); + (id == "run-1").then(|| serde_json::json!({ "run": { "id": id }, "selected_id": agent_id })) + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn cli_runs_are_served_behind_the_bearer() { + let tls = make_tls(); + let source = Arc::new(FakeCliRuns::default()); + let mut config = test_config(&tls); + config.machine = Some(MachineInfo { + host: TEST_HOST.into(), + directory_url: None, + }); + config.cli_runs = Some(source.clone()); + let server = spawn(config).await.unwrap(); + let addr = server.local_addr; + + let (status, _, _) = https_get(&tls, addr, TEST_HOST, None, CLI_RUNS_PATH).await; + assert_eq!(status, 401); + let (status, _, _) = + https_get(&tls, addr, TEST_HOST, None, &format!("{CLI_RUNS_PATH}/run-1")).await; + assert_eq!(status, 401); + assert!(source.asked.lock().unwrap().is_empty(), "no read without a token"); + + let (status, head, body) = https_get(&tls, addr, TEST_HOST, Some(TOKEN), CLI_RUNS_PATH).await; + assert_eq!(status, 200, "{body}"); + assert!(head.to_ascii_lowercase().contains("cache-control: no-store"), "{head}"); + let list: serde_json::Value = serde_json::from_str(&body).unwrap(); + assert_eq!(list["host"], TEST_HOST); + assert_eq!(list["runs"][0]["id"], "run-1"); + + let path = format!("{CLI_RUNS_PATH}/run-1?agent_id=scout&output_token=1%3A2"); + let (status, _, body) = https_get(&tls, addr, TEST_HOST, Some(TOKEN), &path).await; + assert_eq!(status, 200, "{body}"); + let detail: serde_json::Value = serde_json::from_str(&body).unwrap(); + assert_eq!(detail["selected_id"], "scout"); + assert_eq!( + source.asked.lock().unwrap().last().cloned(), + Some(("run-1".into(), Some("scout".into()), Some("1:2".into()))) + ); + + // Empty query values read as absent. + let path = format!("{CLI_RUNS_PATH}/run-1?agent_id=&output_token="); + let (status, _, _) = https_get(&tls, addr, TEST_HOST, Some(TOKEN), &path).await; + assert_eq!(status, 200); + assert_eq!( + source.asked.lock().unwrap().last().cloned(), + Some(("run-1".into(), None, None)) + ); + + let (status, _, _) = + https_get(&tls, addr, TEST_HOST, Some(TOKEN), &format!("{CLI_RUNS_PATH}/gone")).await; + assert_eq!(status, 404); +} + +#[tokio::test(flavor = "multi_thread")] +async fn without_a_source_cli_runs_are_not_found() { + let tls = make_tls(); + let server = spawn(test_config(&tls)).await.unwrap(); + let addr = server.local_addr; + let (status, _, _) = https_get(&tls, addr, TEST_HOST, Some(TOKEN), CLI_RUNS_PATH).await; + assert_eq!(status, 404); + let (status, _, _) = + https_get(&tls, addr, TEST_HOST, Some(TOKEN), &format!("{CLI_RUNS_PATH}/x")).await; + assert_eq!(status, 404); +} diff --git a/crates/gaviero-tui/CLAUDE.md b/crates/gaviero-tui/CLAUDE.md index 3f6c84c4..60e63f63 100644 --- a/crates/gaviero-tui/CLAUDE.md +++ b/crates/gaviero-tui/CLAUDE.md @@ -38,7 +38,7 @@ Workspace dispatch: directory → `Workspace::single_folder`; `*.gaviero-workspa **Observer bridge:** implements `WriteGateObserver` and `AcpObserver` from [`gaviero_core::observer`](../gaviero-core/src/observer.rs), plus the memory / manifest / MCP observers. Each holds an event-channel sender. **No background task mutates `App` directly.** -**SWARM panel** (`Alt+w`): [`panels/swarm_dashboard.rs`](src/panels/swarm_dashboard.rs) holds state and rendering, [`app/swarm_run.rs`](src/app/swarm_run.rs) launching and input. Three views: script picker (`ScriptCatalog`, plus a path typed in the filter), input form (`gaviero_dsl::inspect_file`, plus a Profile selector when any model profile exists; its Prompt field is the chat's box, [`widgets/prompt_input.rs`](src/widgets/prompt_input.rs), and `$skill` is expanded at launch; `file` and `dir` vars complete like `@`, and list params complete the tier after `id=` from the selected profile; below the vars, when the resolved profile reads, an **Agents** section (one `FieldRole::AgentTier` choice per agent the workflow runs — its tier, from the profile's tiers → `--tier`) and a **Tiers** section (per tier those agents now use, a `FieldRole::TierModel` choice — its client, from the profile's clients → `--tier-client` — and a `FieldRole::TierEffort` choice — the chosen model's levels from core `effort_completions` (`SwarmForm::effort_options`, stubbed in tests) plus the client's own effort or `(provider default)` → `--tier-effort`); on these rows Enter opens the options as a list in the form's popup (`AutocompleteMode::Choice`, `open_choice_list`), ↑/↓ move, Enter picks and moves to the next field, ←/→/Space still cycle; only changed rows are passed, the profile is re-read (`reload_profile_info`) when Folder or Profile changes, and choices survive where the new profile still offers them; the form opens on its first input, below the Folder / Workflow / Profile selectors; a list param is `FieldValue::List`, one input per item in `--param` entry syntax, split / rejoined by `split_list_value` / `join_list_items` — short form unless an item is a `{…}` literal, so free fields survive), and the run (one row per planned agent; the selected agent's output below). Starting a run moves the form into `SwarmDashboardState::last_forms` (keyed by script path, this session only): opening that script again — `r` on a finished or stopped run, or the picker — restores every value (`SwarmForm::restore_from`: selectors by option name, agent/tier/effort rows, typed inputs the script still has) with focus on Run. A run is `pipeline::execute` in-process, prepared by the same [`script_run`](../gaviero-core/src/swarm/script_run.rs) helpers as `gaviero-cli --script`; keep the two in step. It uses the pipeline's Write Gate, not TURN REVIEW. Every update carries a run id so a stopped run's stragglers are dropped. This panel is the only way the TUI runs a script: chat has no `/run`, and `/swarm`, `/cswarm` and `/undo-swarm` stay gone. Remote mirrors the run read-only: `request_swarm` (protocol 1.10 `swarm_activity`, `app/remote.rs` `swarm_snapshot`) returns the header, agent rows and one agent's output tail; the phone's agent choice never moves the desktop selection, and the phone can neither start nor stop a run. +**SWARM panel** (`Alt+w`): [`panels/swarm_dashboard.rs`](src/panels/swarm_dashboard.rs) holds state and rendering, [`app/swarm_run.rs`](src/app/swarm_run.rs) launching and input. Three views: script picker (`ScriptCatalog`, plus a path typed in the filter), input form (`gaviero_dsl::inspect_file`, plus a Profile selector when any model profile exists; its Prompt field is the chat's box, [`widgets/prompt_input.rs`](src/widgets/prompt_input.rs), and `$skill` is expanded at launch; `file` and `dir` vars complete like `@`, and list params complete the tier after `id=` from the selected profile; below the vars, when the resolved profile reads, an **Agents** section (one `FieldRole::AgentTier` choice per agent the workflow runs — its tier, from the profile's tiers → `--tier`) and a **Tiers** section (per tier those agents now use, a `FieldRole::TierModel` choice — its client, from the profile's clients → `--tier-client` — and a `FieldRole::TierEffort` choice — the chosen model's levels from core `effort_completions` (`SwarmForm::effort_options`, stubbed in tests) plus the client's own effort or `(provider default)` → `--tier-effort`); on these rows Enter opens the options as a list in the form's popup (`AutocompleteMode::Choice`, `open_choice_list`), ↑/↓ move, Enter picks and moves to the next field, ←/→/Space still cycle; only changed rows are passed, the profile is re-read (`reload_profile_info`) when Folder or Profile changes, and choices survive where the new profile still offers them; the form opens on its first input, below the Folder / Workflow / Profile selectors; a list param is `FieldValue::List`, one input per item in `--param` entry syntax, split / rejoined by `split_list_value` / `join_list_items` — short form unless an item is a `{…}` literal, so free fields survive), and the run (one row per planned agent; the selected agent's output below). Starting a run moves the form into `SwarmDashboardState::last_forms` (keyed by script path, this session only): opening that script again — `r` on a finished or stopped run, or the picker — restores every value (`SwarmForm::restore_from`: selectors by option name, agent/tier/effort rows, typed inputs the script still has) with focus on Run. A run is `pipeline::execute` in-process, prepared by the same [`script_run`](../gaviero-core/src/swarm/script_run.rs) helpers as `gaviero-cli --script`; keep the two in step. **Run history:** every run is recorded through core [`run_history`](../gaviero-core/src/swarm/run_history.rs) under the owning workspace folder's `.gaviero/swarm-runs//` (`run.json` + `agents/.json` = serialized `ActivityLine`s; last `KEEP_RUNS` kept). The record holds the final `RunInputs`: prompt after `$skill` expansion, vars after `locate_path_vars`. Every `Event::Swarm` goes through `swarm_run::apply_update`, which saves the record on agent state changes and at run end. Never call `SwarmDashboardState::apply` directly outside tests of the panel itself. `Tab` in the picker opens `SwarmView::History`. A record still `running` that this TUI is not executing is shown as `interrupted`. `Enter` opens it read-only (`show_record`, `viewing_history`; `u`/`r` refused). `c` twice (`pending_continue_confirm` → `continue_run`) recompiles from the record through `prepare_run`, refuses when `plan.hash()` differs (agent ids + descriptions), and calls `execute` with the checkpoint `run_history::resume_checkpoint` returns (None when the run completed, was undone — `Reverted(Ok)` sets `record.reverted` — nothing is left, or a newer record of the same root + plan replaced the shared `.gaviero/state/.json`). Completed agents are seeded from the earlier pass, and the run continues on the same record (`resumes += 1`). On resume, core merges `gaviero/` branches of checkpoint-completed agents that are still unmerged (`merge::branch_pending_merge`); one that fails to merge is reported as a failed merge, never aborts the run. It uses the pipeline's Write Gate, not TURN REVIEW. Every update carries a run id so a stopped run's stragglers are dropped. This panel is the only way the TUI runs a script: chat has no `/run`, and `/swarm`, `/cswarm` and `/undo-swarm` stay gone. Remote mirrors the run read-only: `request_swarm` (protocol 1.10 `swarm_activity`, `app/remote.rs` `swarm_snapshot`) returns the header, agent rows and one agent's output tail; the phone's agent choice never moves the desktop selection, and the phone can neither start nor stop a run. **Turn review (mandatory):** [`app/turn_review.rs`](src/app/turn_review.rs) + the dispatch task in [`app/side_panel.rs`](src/app/side_panel.rs). Each chat turn is bracketed by `app.turn_capture.begin/end` (core `turn_capture`); the session runs with `AgentOptions::host_capture` and the conversation's gate mode `AutoAccept`. A non-empty change set opens the TURN REVIEW left panel (non-modal) and blocks *that* conversation's next prompt (desktop keeps the typed prompt; remote gets `turn_review_pending`). Pending reviews and their decisions persist under `.gaviero/turns/pending/` and reopen on startup. Any host write made while a turn may be running — editor saves, review reverts, MCP config synthesis — must go through `turn_review::note_host_write` or it is attributed to the agent. @@ -50,7 +50,7 @@ Workspace dispatch: directory → `Workspace::single_folder`; `*.gaviero-workspa **SSH tabs** (session host): a tab runs a [`Target`](src/session_host/target.rs) — `Local(path)` or `Ssh { host, path: Option }` — saved in `session-host.json` and the restart record as the plain path (old files load unchanged) or `ssh://` (`ssh:///~/…`). The Alt+N picker switches Local / SSH with Ctrl+T (`PickerKey::ToggleMode`; `Picker::new_with_ssh`); SSH mode takes `host` or `host:/path`, listing `~/.ssh/config` `Host` aliases (`picker::ssh_config_hosts`, no `Include`) and recent SSH targets. An SSH tab runs `target::ssh_program()` — `GAVIERO_SSH` if set (tests), else the inbox `%SystemRoot%\System32\OpenSSH\ssh.exe` (Git's `ssh`, often first on PATH, is a different console implementation), else `ssh` — with `-t -o ServerAliveInterval=15 -o ServerAliveCountMax=3 "exec $SHELL -l -c 'exec env GAVIERO_SESSION_HOST=1 ""|--pick'"` in the same pseudo-terminal as a local tab (measured: ConPTY hands `ssh.exe` correct xterm VT for the host's keys, SGR mouse, bracketed paste and resize; OSC 0/2/7/52 come back). Hosts, paths and the gaviero override refuse `' " \ $` and backticks, so the quoting holds in sh/bash/zsh/fish (csh and Windows remotes are out of scope). `sshHosts..gaviero` in `session-host.json` overrides where gaviero is on that machine; the host re-reads `sshHosts` from disk before every save and before every connection, so hand edits survive. No session-host env vars or control file reach an SSH tab: the remote gaviero supervises itself (remote builds restart there). `gaviero --pick [START]` runs the folder picker (`picker::run_standalone`) **before** `supervisor::supervise(&args)`, rewriting the args to the chosen path, so a restart never asks again. Every child under the host (`GAVIERO_SESSION_HOST`) writes its opened folder once as OSC 7 (`main.rs::write_session_path`, `osc7::encode_file_url`); the host decodes it in `TitleCb::unhandled_osc` and, for SSH tabs only, adopts it (`Host::adopt_reported_path`: label `host:folder`, `HostState::replace_open`). That report also marks a local tab ready for focus reports: after every host event `Host::sync_focus` sends `ESC[I` to the shown local tab and `ESC[O` to the others, once per change (`Session::sync_focus`; ConPTY turns them into focus events), so a hidden tab's `terminal_has_focus` is false — otherwise every local tab acted on a desktop-wide Ctrl+V (`platform::take_ctrl_v_press_edge`) and attached the clipboard image in the wrong tab. SSH tabs get none (the remote end may be a shell); Ctrl+V there with no clipboard text says so on the tab bar. Closing or quitting kills an SSH tab's `ssh` at once (`close_grace` = 0) — the remote gaviero gets SIGHUP, and `platform::install_hangup_forwarder` (Unix) turns SIGHUP/SIGTERM into `Event::HangUp`, which leaves through the normal save path (`platform::hung_up` stops drawing to the dead terminal; log line `exit: session state saved`). `ssh` exiting 255 (`SSH_CONNECTION_LOST`) keeps the tab `disconnected` with a ` Disconnected ` box: Enter reconnects (`Host::reconnect`), other input is dropped. OSC 52 copies from any tab reach the local clipboard (`TitleCb::copy_to_clipboard` → `session_host::clipboard`, ≤ 1 MiB UTF-8), and Ctrl+V in an SSH tab sends the local clipboard text as a (bracketed) paste. Shift+Enter in an SSH tab is sent as Alt+Enter (`encode::ssh_key_record`; every tab of a Unix host via `keys::vt_override`): `ssh.exe` reads VT, where conhost writes Shift+Enter as a bare `\r`, while Alt+Enter crosses as `ESC \r` and the remote gaviero binds both to the chat newline. Test hooks: `GAVIERO_SSH` (point it at the gaviero binary: with `GAVIERO_INPUT_PROBE` it becomes the probe), `GAVIERO_PROBE_ARGS` (the probe records its argv; Ctrl+K exits 255, Ctrl+O sends OSC 7, Ctrl+Y sends OSC 52), `GAVIERO_SESSION_HOST_CLIPBOARD` (a file instead of the system clipboard). `tests/session_host_ssh.rs` covers the Windows side; `tests/remote_side_unix.rs` (`--pick`, OSC 7, SIGHUP save) runs only on Unix — on this Windows PC run it in WSL (`wsl.exe -d Ubuntu-26.04`, `CARGO_TARGET_DIR=$HOME/.cache/gaviero-wsl-target`); cross-compiling from Windows fails on openssl-sys. -**Remote sidecar:** every TUI launch with `remote.relay.host` set starts a background WSS sidecar (`gaviero-remote`, Plan C) unless `remote.enabled` is false. gaviero-relay is the only transport: the listener binds loopback, the pinned-CA leaf, the bind, the relay client and the machine registry are all set up off the event loop (`Event::RemoteStarted` / `Event::RemoteUnavailable`). The phone mirrors every conversation tab and can decide turn reviews per file (protocol 1.2 `turn_review`; per-hunk and the drift confirmation stay desktop-only). Pairing is `/remote` (QR + machine token). See [`crates/gaviero-remote/PROTOCOL.md`](../gaviero-remote/PROTOCOL.md). +**Remote sidecar:** every TUI launch with `remote.relay.host` set starts a background WSS sidecar (`gaviero-remote`, Plan C) unless `remote.enabled` is false. gaviero-relay is the only transport: the listener binds loopback, the pinned-CA leaf, the bind, the relay client and the machine registry are all set up off the event loop (`Event::RemoteStarted` / `Event::RemoteUnavailable`). The phone mirrors every conversation tab and can decide turn reviews per file (protocol 1.2 `turn_review`; per-hunk and the drift confirmation stay desktop-only). Every listener also serves the machine's `gaviero-cli` runs, read-only (protocol 1.11 `cli_runs`: `GET /v1/cli-runs[/]`). `remote_setup::CliRuns` passes core `live_run::CliRunStore` to `gaviero_remote::server::CliRunSource`. Pairing is `/remote` (QR + machine token). See [`crates/gaviero-remote/PROTOCOL.md`](../gaviero-remote/PROTOCOL.md). **Authoritative slash list:** [`app/commands.rs`](src/app/commands.rs) (and chat helpers in [`panels/agent_chat.rs`](src/panels/agent_chat.rs)). Groups: session (`/model`, `/effort`, `/autoapprove`/`/yolo`, …), context (`/lite`, `/inject`, `/context mode …`), memory, MCP (`/mcp`, `/mcp probe` — probe runs from `gaviero-cli --mcp-reach-probe`), skills (`/skills`, `$skill`). Do not maintain a second inventory in ARCHITECTURE.md — point here. diff --git a/crates/gaviero-tui/README.md b/crates/gaviero-tui/README.md index ad2f7c5c..f1144a0a 100644 --- a/crates/gaviero-tui/README.md +++ b/crates/gaviero-tui/README.md @@ -55,6 +55,8 @@ review src/auth/session.rs for race conditions **Run a DSL workflow** (SWARM panel, Alt+W): pick a script from `.gaviero/dsl//.gaviero` or type a path, fill in its inputs, and move down to **Run**. A list input (a roster or a `for` list) shows one editable row per item: click the `x` before an item or press `Backspace` on an empty one to remove it, and click the `+` after `(list)` or press `Alt+Enter` to add one. While it runs, `↑`/`↓` choose an agent and its output shows below; `Enter` expands the output, `Tab` scrolls it, and `x` twice stops the run. Afterwards, `d` shows an agent's diff, `u` twice undoes the run's merges, and `n` starts another. The same scripts run headless with `gaviero-cli --script`. +**Past runs** (SWARM panel, `Tab` from the script list): every run started here is recorded under `/.gaviero/swarm-runs/` (the last 50 per folder) with its inputs, each agent's final state and its output, saved as the run goes. A run cut short — the editor closed, the machine slept — shows as `interrupted`. `Enter` opens a run as it ended; `↑`/`↓` and `Enter` inspect each agent's output there. A run marked `⟳ can continue` (it stopped, failed, or ended with failed agents, for example when a provider's usage limit ran out) continues with `c` pressed twice: the agents it completed are not run again, and the rest run with the inputs the run was started with. `c` is also offered on a run that just ended that way. Branches of agents that finished before an interruption and were never merged are merged when the continued run ends. Only the newest run of a script can continue: a later run whose agents have the same names and descriptions replaces its checkpoint. A run that was undone with `u` cannot continue. Continuing is refused when the script's agents (names and descriptions) changed since; an edited agent prompt is simply used as edited. + **Turn review** — agents edit files directly while they work (edit tools, shell commands, formatters — whatever they need to test). When a turn ends, gaviero compares the workspace with a snapshot taken before the prompt (no git needed; `.gitignore` and `files.exclude` paths are skipped, except the gitignored folders you list in `turnReview.includeIgnored`, e.g. `"turnReview": { "includeIgnored": ["plans", "docs/private"] }` — paths relative to each workspace folder; gaviero's own `.gaviero/` folder is skipped too, except `settings.json`, `dsl/` and `skills/`, and any other folder in it you list there, such as `".gaviero/plans"`) and, if anything changed, opens the TURN REVIEW panel. That conversation cannot send its next prompt until every changed file is accepted or rejected; other conversations keep working. Accepted edits stay as the agent left them; rejected ones go back to the pre-prompt version. Sensitive files (`.env`, keys) an agent changed are restored automatically. Cancelled and failed turns are reviewed too. Four decisions, each applied immediately — the review ends when every file has one: diff --git a/crates/gaviero-tui/src/app/controller.rs b/crates/gaviero-tui/src/app/controller.rs index f7131e2a..6668918c 100644 --- a/crates/gaviero-tui/src/app/controller.rs +++ b/crates/gaviero-tui/src/app/controller.rs @@ -1670,7 +1670,7 @@ pub(super) fn handle_event(app: &mut App, event: Event) { app.remote.status = crate::app::remote::RemoteStatus::Unavailable(reason); } Event::Swarm { run, update } => { - app.swarm_dashboard.apply(run, update); + crate::app::swarm_run::apply_update(app, run, update); } Event::Tick => { app.terminal_manager.tick(); diff --git a/crates/gaviero-tui/src/app/remote_setup.rs b/crates/gaviero-tui/src/app/remote_setup.rs index af7b9a92..397e8d2a 100644 --- a/crates/gaviero-tui/src/app/remote_setup.rs +++ b/crates/gaviero-tui/src/app/remote_setup.rs @@ -554,6 +554,25 @@ pub fn qr_payload_for(config: &RemoteConfig, token: &str, tls_ca: &str) -> Strin // ── Server startup ────────────────────────────────────────────────── +/// The machine's `gaviero-cli` runs (protocol 1.11 `cli_runs`), read from +/// the records the CLI publishes (core `swarm::live_run`). +struct CliRuns(gaviero_core::swarm::live_run::CliRunStore); + +impl gaviero_remote::server::CliRunSource for CliRuns { + fn list(&self, host: &str) -> serde_json::Value { + self.0.list_json(host) + } + + fn detail( + &self, + id: &str, + agent_id: Option<&str>, + output_token: Option<&str>, + ) -> Option { + self.0.detail_json(id, agent_id, output_token) + } +} + /// Everything the bootstrap learned, delivered to the event loop as /// `Event::RemoteStarted` and rendered by `/remote`. #[derive(Clone)] @@ -601,6 +620,10 @@ pub async fn start( .collect() }; let last = *candidates.last().expect("at least one candidate"); + // Served (and advertised) only when the machine folder resolves. + let cli_runs: Option> = + gaviero_core::swarm::live_run::CliRunStore::machine_default() + .map(|store| std::sync::Arc::new(CliRuns(store)) as _); let mut last_bind_error = String::new(); for port in candidates { @@ -648,7 +671,14 @@ pub async fn start( gaviero_remote::version::capability::SHELL_HISTORY.to_string(), gaviero_remote::version::capability::SKILL_COMPLETIONS.to_string(), gaviero_remote::version::capability::SWARM_ACTIVITY.to_string(), - ], + ] + .into_iter() + .chain( + cli_runs + .is_some() + .then(|| gaviero_remote::version::capability::CLI_RUNS.to_string()), + ) + .collect(), machine: Some(gaviero_remote::dto::MachineInfo { host: config.machine_host.clone(), directory_url: Some(directory_url(config)), @@ -658,6 +688,7 @@ pub async fn start( registry, heartbeat_interval: gaviero_remote::server::registry::HEARTBEAT_INTERVAL, stale_after: gaviero_remote::server::registry::STALE_AFTER, + cli_runs: cli_runs.clone(), confirm_required: crate::app::remote::REMOTE_CONFIRM_REQUIRED .iter() .map(|s| s.to_string()) diff --git a/crates/gaviero-tui/src/app/swarm_run.rs b/crates/gaviero-tui/src/app/swarm_run.rs index b81ab307..5723597e 100644 --- a/crates/gaviero-tui/src/app/swarm_run.rs +++ b/crates/gaviero-tui/src/app/swarm_run.rs @@ -15,16 +15,21 @@ use super::state::{Focus, SidePanelMode}; use crate::event::Event; use crate::keymap::Action; use crate::panels::swarm_dashboard::{ - DashboardFocus, FieldRole, FormValues, ListTarget, PlannedAgent, ScriptRow, SwarmForm, - SwarmUpdate, SwarmView, + ActivityLine, DashboardFocus, FieldRole, FormValues, HistoryRow, ListTarget, PlannedAgent, + RunRecordState, ScriptRow, SwarmForm, SwarmUpdate, SwarmView, }; use crate::widgets::prompt_input::{self, AutocompleteMode}; use crate::widgets::render_utils::strip_ansi; use crate::widgets::text_input::TextInput; use gaviero_core::script_profiles::ProfileCatalog; use gaviero_core::scripts::{ScriptCatalog, is_repo_scoped}; +use gaviero_core::swarm::execution_state::{ExecutionState, NodeStatus}; use gaviero_core::swarm::models::{AgentStatus, SwarmResult}; use gaviero_core::swarm::plan::ExecutionMode; +use gaviero_core::swarm::run_history::{ + KEEP_RUNS, RunAgentRecord, RunHistory, RunInputs, RunOutcome, RunTierOverride, + SwarmRunRecord, resume_checkpoint, +}; use gaviero_core::swarm::{pipeline, script_run}; use gaviero_core::workspace::settings as S; @@ -164,8 +169,10 @@ pub(super) fn open_panel(app: &mut App) { app.panel_visible.side_panel = true; app.side_panel = SidePanelMode::SwarmDashboard; app.focus = Focus::SidePanel; - if app.swarm_dashboard.view == SwarmView::Picker { - refresh_picker(app); + match app.swarm_dashboard.view { + SwarmView::Picker => refresh_picker(app), + SwarmView::History => refresh_history(app), + _ => {} } } @@ -290,6 +297,34 @@ fn script_from_typed_path(app: &App, typed: &str) -> Option { }) } +/// A picker row for a script known only by its path (a run from History). +/// Named as the catalog names it: its folder, or the file for a loose script. +fn script_row_from_path( + workspace: &gaviero_core::workspace::Workspace, + path: &Path, +) -> Option { + if !path.is_file() { + return None; + } + let stem = path.file_stem()?.to_string_lossy().to_string(); + let in_own_folder = path + .parent() + .and_then(|dir| dir.file_name()) + .is_some_and(|dir| dir.to_string_lossy() == stem); + let name = if in_own_folder { + stem + } else { + path.file_name()?.to_string_lossy().to_string() + }; + Some(ScriptRow { + name, + source: "history".to_string(), + description: None, + path: path.to_path_buf(), + owner: workspace.folder_for_path(path).map(Path::to_path_buf), + }) +} + /// What the user should know before starting: writes land in this checkout. fn refresh_form_warnings(app: &mut App) { let chat_turn_running = app @@ -361,13 +396,81 @@ fn prepare_and_start(app: &mut App) -> Result<(), String> { let script = form.script.clone(); let execution_mode = form.execution_mode(); + // `$skill` is expanded once, here; the record keeps the result, so a + // continued run compiles the same prompt whatever the catalog holds then. + let mut skill_notes = Vec::new(); + let prompt = values.prompt.as_deref().map(|prompt| { + let repo = gaviero_core::memory::scope::hash_path(&values.root); + let (expanded, notes) = expand_prompt_skills(prompt, &app.skill_catalog, &repo); + skill_notes = notes; + expanded + }); + let label = match &values.workflow { + Some(workflow) if workflow != &script.name => format!("{} / {workflow}", script.name), + _ => script.name.clone(), + }; + let inputs = RunInputs { + form_root: values.root, + workflow: values.workflow, + profile: values.profile, + prompt, + vars: values.vars, + params: values.params, + agent_tiers: values.agent_tiers, + tier_overrides: values + .tier_overrides + .into_iter() + .map(|o| RunTierOverride { + tier: o.tier, + client: o.client, + effort: o.effort, + }) + .collect(), + }; + let prepared = prepare_run(app, &script.path, label, execution_mode, &inputs, skill_notes)?; + + let history = history_for(app, &prepared.root); + history.prune(KEEP_RUNS.saturating_sub(1)); + let record = SwarmRunRecord::new( + RunHistory::new_run_id(), + script.path.clone(), + prepared.label.clone(), + prepared.root.clone(), + prepared.plan.hash(), + inputs, + ); + start_run(app, prepared, RunRecordState::new(history, record), None); + Ok(()) +} + +/// A compiled run, ready to start. +struct PreparedRun { + label: String, + root: PathBuf, + plan: gaviero_core::swarm::plan::CompiledPlan, + config: pipeline::SwarmConfig, + planned: Vec, + notes: Vec, +} + +/// Compile `script` with `inputs` and set up its run, as `gaviero-cli +/// --script` does. `inputs` are final: skills already expanded, file vars +/// already located. Nothing runs yet. +fn prepare_run( + app: &App, + script: &Path, + label: String, + execution_mode: ExecutionMode, + inputs: &RunInputs, + extra_notes: Vec, +) -> Result { let anchor = script_run::anchor_script_run( - &values.root, - &values.root, + &inputs.form_root, + &inputs.form_root, None, true, execution_mode, - values.vars, + inputs.vars.clone(), ) .map_err(|e| format!("{e:#}"))?; let root = std::fs::canonicalize(&anchor.root) @@ -381,25 +484,28 @@ fn prepare_and_start(app: &mut App) -> Result<(), String> { let profile = ProfileCatalog::scan(&app.workspace, &ScriptCatalog::global_scripts_dir()) .select( - values.profile.as_deref(), + inputs.profile.as_deref(), Some(&gaviero_core::memory::scope::hash_path(&root)), &root, )?; if let Some(profile) = &profile { notes.push(format!("Profile: {}", profile.name)); } + notes.extend(extra_notes); - let prompt = values.prompt.as_deref().map(|prompt| { - let repo = gaviero_core::memory::scope::hash_path(&values.root); - let (expanded, skill_notes) = expand_prompt_skills(prompt, &app.skill_catalog, &repo); - notes.extend(skill_notes); - expanded - }); - - for (agent, tier) in &values.agent_tiers { + let tier_overrides: Vec = inputs + .tier_overrides + .iter() + .map(|o| gaviero_dsl::TierOverride { + tier: o.tier.clone(), + client: o.client.clone(), + effort: o.effort.clone(), + }) + .collect(); + for (agent, tier) in &inputs.agent_tiers { notes.push(format!("Agent {agent} runs on tier {tier}")); } - for o in &values.tier_overrides { + for o in &tier_overrides { notes.push(match (&o.client, &o.effort) { (Some(client), Some(effort)) => { format!("Tier {} runs on client {client} at effort {effort}", o.tier) @@ -410,14 +516,14 @@ fn prepare_and_start(app: &mut App) -> Result<(), String> { }); } let plan = gaviero_dsl::compile_file_with_models( - &script.path, + script, profile.as_ref(), - &values.tier_overrides, - values.workflow.as_deref(), - prompt.as_deref(), + &tier_overrides, + inputs.workflow.as_deref(), + inputs.prompt.as_deref(), &vars, - &values.agent_tiers, - &values.params, + &inputs.agent_tiers, + &inputs.params, ) .map_err(|report| report_text(&report))?; @@ -448,14 +554,73 @@ fn prepare_and_start(app: &mut App) -> Result<(), String> { mcp_config, worktree_context_paths, ); - let memory = app.memory.clone(); + Ok(PreparedRun { + label, + root, + plan, + config, + planned, + notes, + }) +} - let label = match &values.workflow { - Some(workflow) if workflow != &script.name => format!("{} / {workflow}", script.name), - _ => script.name.clone(), - }; +/// Where a run's record is kept: the workspace folder it ran in, else its root. +fn history_for(app: &App, root: &Path) -> RunHistory { + let folder = app.workspace.folder_for_path(root).unwrap_or(root); + RunHistory::for_folder(folder) +} + +/// Show the run, save its record and start it. `initial_state` continues a +/// run from its checkpoint: the agents it completed are not run again. +fn start_run( + app: &mut App, + prepared: PreparedRun, + mut state: RunRecordState, + initial_state: Option, +) { + let PreparedRun { + label, + root, + plan, + config, + planned, + notes, + } = prepared; + let memory = app.memory.clone(); let run = app.swarm_dashboard.begin_run(label, root, planned, notes); + if let Some(checkpoint) = &initial_state { + let earlier: Vec = state.record.agents.clone(); + let mut done: Vec<&String> = checkpoint + .node_states + .iter() + .filter(|(_, node)| node.status == NodeStatus::Completed) + .map(|(id, _)| id) + .collect(); + done.sort(); + for id in done { + let detail = earlier + .iter() + .find(|a| &a.id == id) + .map(|a| a.detail.clone()) + .filter(|d| !d.is_empty()) + .unwrap_or_else(|| "done in an earlier pass".to_string()); + let activity = load_activity(&state.history, &state.record.id, id); + app.swarm_dashboard.seed_completed(id, detail, activity); + } + } + + state.record.outcome = RunOutcome::Running; + state.record.ended_at = None; + state.record.agents = app.swarm_dashboard.record_agents(); + if let Err(e) = state.history.save(&state.record) { + tracing::warn!("swarm run record not saved: {e:#}"); + app.swarm_dashboard.status_message.push_str(&format!( + "\nThis run is not recorded in History: {e:#}" + )); + } + app.swarm_dashboard.record = Some(state); + let tx = app.event_tx.clone(); let task = tokio::spawn({ let tx = tx.clone(); @@ -473,13 +638,19 @@ fn prepare_and_start(app: &mut App) -> Result<(), String> { agent: agent.to_string(), }) }; - let update = - match pipeline::execute(&plan, &config, None, memory, &observer, make_observer) - .await - { - Ok(result) => SwarmUpdate::Completed(Box::new(result)), - Err(e) => SwarmUpdate::Failed(format!("{e:#}")), - }; + let update = match pipeline::execute( + &plan, + &config, + initial_state, + memory, + &observer, + make_observer, + ) + .await + { + Ok(result) => SwarmUpdate::Completed(Box::new(result)), + Err(e) => SwarmUpdate::Failed(format!("{e:#}")), + }; let _ = tx.send(Event::Swarm { run, update }); } }); @@ -494,7 +665,235 @@ fn prepare_and_start(app: &mut App) -> Result<(), String> { let _ = tx.send(Event::Swarm { run, update }); } }); - Ok(()) +} + +// ── Run history ───────────────────────────────────────────────── + +/// Every run update goes through here: the panel shows it, and the run's +/// record is saved whenever an agent changes state and when the run ends, +/// so a run cut short (editor closed, machine asleep) is still in History. +pub(super) fn apply_update(app: &mut App, run: u64, update: SwarmUpdate) { + let current = run == app.swarm_dashboard.run_id; + let end = match &update { + SwarmUpdate::Completed(result) if result.success => Some(RunOutcome::Completed), + SwarmUpdate::Completed(_) => Some(RunOutcome::CompletedWithFailures), + SwarmUpdate::Failed(_) => Some(RunOutcome::Failed), + SwarmUpdate::Stopped => Some(RunOutcome::Stopped), + _ => None, + }; + let progress = end.is_some() || matches!(update, SwarmUpdate::Agent { .. }); + let reverted = matches!(update, SwarmUpdate::Reverted(Ok(()))); + app.swarm_dashboard.apply(run, update); + if current && progress && !app.swarm_dashboard.viewing_history { + save_progress(&mut app.swarm_dashboard, end); + } + // An undone run's checkpoint no longer describes the tree: continuing it + // would skip agents whose work is gone. + if current + && reverted + && let Some(state) = app.swarm_dashboard.record.as_mut() + { + state.record.reverted = true; + state.resumable = false; + if let Err(e) = state.history.save(&state.record) { + tracing::warn!("swarm run record not saved: {e:#}"); + } + } +} + +fn save_progress( + dash: &mut crate::panels::swarm_dashboard::SwarmDashboardState, + end: Option, +) { + let agents = dash.record_agents(); + let cost = dash.estimated_cost_usd; + let message = dash.status_message.clone(); + let Some(state) = dash.record.as_mut() else { + return; + }; + state.record.agents = agents; + state.record.cost_usd = cost; + // Output is saved as it changes, so an interrupted run keeps it too. + for agent in &dash.agents { + if agent.activity.is_empty() + || state.saved_revisions.get(&agent.id) == Some(&agent.revision()) + { + continue; + } + let json = match serde_json::to_string(&agent.activity) { + Ok(json) => json, + Err(e) => { + tracing::warn!("swarm output of {} not saved: {e}", agent.id); + continue; + } + }; + match state.history.write_agent_log(&state.record.id, &agent.id, &json) { + Ok(()) => { + state.saved_revisions.insert(agent.id.clone(), agent.revision()); + } + Err(e) => tracing::warn!("swarm output of {} not saved: {e:#}", agent.id), + } + } + if let Some(outcome) = end { + state.record.outcome = outcome; + state.record.ended_at = Some(chrono::Utc::now().timestamp()); + state.record.message = message; + } + if let Err(e) = state.history.save(&state.record) { + tracing::warn!("swarm run record not saved: {e:#}"); + } + if end.is_some() { + state.resumable = resume_checkpoint(&state.record, &state.history.list()).is_some(); + } +} + +/// An agent's saved output; empty when there is none or it does not parse. +fn load_activity(history: &RunHistory, run_id: &str, agent: &str) -> Vec { + history + .read_agent_log(run_id, agent) + .and_then(|json| serde_json::from_str(&json).ok()) + .unwrap_or_default() +} + +/// Reload History: the runs of every workspace folder, newest first. +pub(super) fn refresh_history(app: &mut App) { + let dash = &app.swarm_dashboard; + let live = dash + .record + .as_ref() + .filter(|_| dash.running) + .map(|state| state.record.id.clone()); + let mut folders: Vec = workspace_folders(app).into_iter().map(|(_, p)| p).collect(); + folders.dedup(); + let mut rows = Vec::new(); + for folder in folders { + let history = RunHistory::for_folder(&folder); + let records = history.list(); + for record in &records { + let is_live = live.as_ref() == Some(&record.id); + rows.push(HistoryRow { + history: history.clone(), + record: record.clone(), + resumable: !is_live && resume_checkpoint(record, &records).is_some(), + interrupted: !is_live && record.outcome == RunOutcome::Running, + }); + } + } + rows.sort_by(|a, b| { + b.record + .started_at + .cmp(&a.record.started_at) + .then_with(|| b.record.id.cmp(&a.record.id)) + }); + let list = &mut app.swarm_dashboard.history; + list.rows = rows; + list.scroll.selected = list.scroll.selected.min(list.rows.len().saturating_sub(1)); +} + +fn open_history(app: &mut App) { + app.swarm_dashboard.view = SwarmView::History; + refresh_history(app); +} + +/// History `Enter`: show the run as it ended. The live run is shown live. +fn open_history_row(app: &mut App) { + let Some(row) = app.swarm_dashboard.history.selected().cloned() else { + return; + }; + let dash = &mut app.swarm_dashboard; + let is_live = dash.running + && dash + .record + .as_ref() + .is_some_and(|state| state.record.id == row.record.id); + if is_live { + dash.view = SwarmView::Run; + return; + } + let activity = row + .record + .agents + .iter() + .map(|a| (a.id.clone(), load_activity(&row.history, &row.record.id, &a.id))) + .collect(); + dash.show_record(row, activity); +} + +/// `c` twice: continue the run on screen from its checkpoint, running only +/// the agents it did not complete, with the inputs it was started with. +/// Refused when the script's agents (ids and descriptions) changed since. +fn continue_run(app: &mut App) { + let dash = &mut app.swarm_dashboard; + if !dash.can_continue() { + return; + } + let Some(state) = dash.record.as_ref() else { + return; + }; + let history = state.history.clone(); + let mut record = state.record.clone(); + let Some(checkpoint) = resume_checkpoint(&record, &history.list()) else { + if let Some(state) = dash.record.as_mut() { + state.resumable = false; + } + dash.status_message = + "This run can no longer be continued: its checkpoint is gone or a later run of \ + the same script replaced it." + .to_string(); + return; + }; + let execution_mode = match gaviero_dsl::inspect_file(&record.script_path) { + Ok(inspection) => record + .inputs + .workflow + .as_deref() + .and_then(|name| inspection.workflows.iter().find(|w| w.name == name)) + .map_or(inspection.default_execution, |w| w.execution), + Err(report) => { + dash.status_message = format!( + "Cannot continue: {}: {}", + record.script_path.display(), + report_text(&report) + ); + return; + } + }; + let done = checkpoint + .node_states + .values() + .filter(|n| n.status == NodeStatus::Completed) + .count(); + let note = format!( + "Continuing: {done} of {} agents already done.", + checkpoint.node_states.len() + ); + let prepared = match prepare_run( + app, + &record.script_path, + record.script_label.clone(), + execution_mode, + &record.inputs, + vec![note], + ) { + Ok(prepared) => prepared, + Err(message) => { + app.swarm_dashboard.status_message = format!("Cannot continue: {message}"); + return; + } + }; + // The checkpoint is keyed by the plan's agents (ids and descriptions, + // `CompiledPlan::hash`). An edit that keeps them — a reworded prompt — + // is continued with the edited script. + if prepared.plan.hash() != record.plan_hash { + app.swarm_dashboard.status_message = "Cannot continue: the script's agents changed \ + since this run, so its checkpoint no longer matches them. Run it again from the form." + .to_string(); + return; + } + record.resumes += 1; + let script_path = record.script_path.clone(); + start_run(app, prepared, RunRecordState::new(history, record), Some(checkpoint)); + app.swarm_dashboard.last_script = Some(script_path); } /// Turn each `file` and `dir` var into the path agents should see. Values @@ -712,11 +1111,37 @@ fn swarm_config( pub(super) fn handle_action(app: &mut App, action: Action) { match app.swarm_dashboard.view { SwarmView::Picker => handle_picker(app, action), + SwarmView::History => handle_history(app, action), SwarmView::Form => handle_form(app, action), SwarmView::Run => handle_run(app, action), } } +fn handle_history(app: &mut App, action: Action) { + let list = &mut app.swarm_dashboard.history; + let count = list.rows.len(); + match action { + Action::CursorUp | Action::InsertChar('k') => list.scroll.move_up(), + Action::CursorDown | Action::InsertChar('j') => list.scroll.move_down(count), + Action::PageUp => { + for _ in 0..10 { + list.scroll.move_up(); + } + } + Action::PageDown => { + for _ in 0..10 { + list.scroll.move_down(count); + } + } + Action::Enter => open_history_row(app), + Action::Tab => { + app.swarm_dashboard.view = SwarmView::Picker; + refresh_picker(app); + } + _ => {} + } +} + /// `Esc` steps back one level. Returns whether it did anything. pub(super) fn handle_escape(app: &mut App) -> bool { let dash = &mut app.swarm_dashboard; @@ -745,17 +1170,29 @@ pub(super) fn handle_escape(app: &mut App) -> bool { true } SwarmView::Picker => false, + SwarmView::History => { + dash.view = SwarmView::Picker; + refresh_picker(app); + true + } SwarmView::Run => { if dash.diff_agent.is_some() { dash.diff_agent = None; true - } else if dash.pending_stop_confirm || dash.pending_undo_confirm { + } else if dash.pending_stop_confirm + || dash.pending_undo_confirm + || dash.pending_continue_confirm + { dash.pending_stop_confirm = false; dash.pending_undo_confirm = false; + dash.pending_continue_confirm = false; true } else if dash.output_maximized { dash.output_maximized = false; true + } else if !dash.running && dash.viewing_history { + open_history(app); + true } else if !dash.running { dash.view = SwarmView::Picker; refresh_picker(app); @@ -789,7 +1226,7 @@ pub(super) fn handle_paste(app: &mut App, text: &str) { } } } - SwarmView::Run => {} + SwarmView::History | SwarmView::Run => {} } } @@ -816,6 +1253,7 @@ fn edit_input(input: &mut TextInput, action: &Action) -> bool { fn handle_picker(app: &mut App, action: Action) { let visible = app.swarm_dashboard.picker.visible().len(); match action { + Action::Tab => open_history(app), Action::CursorUp => app.swarm_dashboard.picker.scroll.move_up(), Action::CursorDown => app.swarm_dashboard.picker.scroll.move_down(visible), Action::Enter => { @@ -1160,12 +1598,31 @@ fn handle_run(app: &mut App, action: Action) { show_selected_diff(app); return; } + // Continuing starts paid agent work: like `x` and `u`, it asks first. + Action::InsertChar('c') if dash.can_continue() => { + if dash.pending_continue_confirm { + dash.pending_continue_confirm = false; + continue_run(app); + } else { + dash.pending_continue_confirm = true; + } + return; + } + Action::InsertChar('r') if !dash.running && dash.viewing_history => return, + Action::InsertChar('u') if !dash.running && dash.viewing_history => { + dash.status_message = + "Undo is offered only right after a run, not for one opened from History." + .to_string(); + return; + } Action::InsertChar('r') if !dash.running => { - let script = dash - .last_script - .as_ref() - .and_then(|path| dash.last_forms.get(path)) - .map(|form| form.script.clone()); + let script = dash.last_script.as_ref().and_then(|path| { + dash.last_forms + .get(path) + .map(|form| form.script.clone()) + // A run continued from History has no form this session. + .or_else(|| script_row_from_path(&app.workspace, path)) + }); if let Some(script) = script { open_form(app, script); } @@ -1321,6 +1778,14 @@ pub(super) fn scroll_wheel(app: &mut App, up: bool, col: u16, row: u16) { dash.picker.scroll.scroll_down(delta, visible); } } + SwarmView::History => { + let count = dash.history.rows.len(); + if up { + dash.history.scroll.scroll_up(delta); + } else { + dash.history.scroll.scroll_down(delta, count); + } + } SwarmView::Form => { if let Some(form) = dash.form.as_mut() && form.completion.is_open() @@ -1365,6 +1830,11 @@ pub(super) fn click(app: &mut App, col: u16, row: u16) { dash.picker.scroll.selected = index; } } + SwarmView::History => { + if let Some(index) = dash.history.index_at(col, row) { + dash.history.scroll.selected = index; + } + } SwarmView::Form => { if let Some(form) = dash.form.as_mut() { match form.list_target_at(col, row) { @@ -1595,9 +2065,7 @@ workflow run { tokio::time::timeout(Duration::from_secs(30), async { while !done(app) { match rx.recv().await { - Some(Event::Swarm { run, update }) => { - app.swarm_dashboard.apply(run, update); - } + Some(Event::Swarm { run, update }) => apply_update(app, run, update), Some(_) => {} None => panic!("event channel closed"), } @@ -2476,4 +2944,392 @@ workflow run { scroll_output(&mut dash, 1); assert!(dash.detail_auto_scroll, "back at the end, following resumes"); } + + // ── Run history ───────────────────────────────────────────── + + const TWO_STEP_SCRIPT: &str = r#"// description: Two parts +agent first { + description "write part one" + tier local + scope { owned ["out/first.md"] } + produces ["out/first.md"] + prompt "Write the FIRST part about {{PROMPT}}." + max_retries 0 +} + +agent second { + description "write part two" + tier local + depends_on [first] + scope { owned ["out/second.md"] } + produces ["out/second.md"] + prompt "Write the SECOND part." + max_retries 0 +} + +workflow run { + execution_mode document + steps [first second] + max_parallel 1 +} +"#; + + /// An Ollama that writes the part the prompt asks for, and answers + /// request number `fail_on` (1-based) with an error, as a provider whose + /// usage limit ran out would. + fn flaky_ollama(fail_on: usize) -> (String, std::sync::mpsc::Receiver) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let (seen_tx, seen_rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + for (index, stream) in listener.incoming().enumerate() { + let Ok(mut stream) = stream else { continue }; + let request = read_request(&stream); + let _ = seen_tx.send(request.clone()); + if index + 1 == fail_on { + let _ = write!( + stream, + "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 5\r\n\ + Connection: close\r\n\r\nlimit" + ); + continue; + } + let path = if request.contains("SECOND") { + "out/second.md" + } else { + "out/first.md" + }; + let content = format!("\npart body\n"); + let chunk = serde_json::json!({ + "model": "qwen", + "message": {"role": "assistant", "content": content}, + "done": false + }); + let done = serde_json::json!({"model": "qwen", "done": true}); + let body = format!("{chunk}\n{done}\n"); + let _ = write!( + stream, + "HTTP/1.1 200 OK\r\nContent-Type: application/x-ndjson\r\n\ + Content-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + } + }); + (format!("http://{addr}"), seen_rx) + } + + fn drain(rx: &std::sync::mpsc::Receiver) -> Vec { + std::iter::from_fn(|| rx.recv_timeout(Duration::from_millis(200)).ok()).collect() + } + + /// Open the only script, type `prompt` and run it to the end. + async fn run_to_end(app: &mut App, rx: &mut mpsc::UnboundedReceiver, prompt: &str) { + open_panel(app); + handle_action(app, Action::Enter); + assert_eq!( + app.swarm_dashboard.view, + SwarmView::Form, + "{:?}", + app.swarm_dashboard.picker.error + ); + type_text(app, prompt); + press_run(app); + assert_eq!( + app.swarm_dashboard.view, + SwarmView::Run, + "error: {:?}", + app.swarm_dashboard.form.as_ref().and_then(|f| f.error.clone()) + ); + pump_until(app, rx, |a| !a.swarm_dashboard.running).await; + } + + fn records(root: &Path) -> Vec { + RunHistory::for_folder(root).list() + } + + fn render_text(app: &mut App) -> String { + let area = ratatui::layout::Rect::new(0, 0, 120, 30); + let mut buf = ratatui::buffer::Buffer::empty(area); + app.swarm_dashboard.render(area, &mut buf, true); + (0..area.height) + .map(|y| { + (0..area.width) + .map(|x| buf[(x, y)].symbol().to_string()) + .collect::() + }) + .collect::>() + .join("\n") + } + + #[tokio::test(flavor = "multi_thread")] + async fn a_panel_run_is_recorded_with_its_inputs_and_output() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (url, _asked) = fake_ollama("default_out/report.md"); + let (mut app, mut rx) = app_with_script(&root, &url); + + run_to_end(&mut app, &mut rx, "the moon").await; + assert_eq!(app.swarm_dashboard.phase, "completed"); + + let saved = records(&root); + assert_eq!(saved.len(), 1); + let record = &saved[0]; + assert_eq!(record.outcome, RunOutcome::Completed); + assert_eq!(record.inputs.prompt.as_deref(), Some("the moon")); + assert_eq!(record.script_label, "report / run"); + assert!(record.ended_at.is_some()); + assert_eq!(record.agents.len(), 1); + assert_eq!(record.agents[0].id, "writer"); + assert_eq!(record.agents[0].status, AgentStatus::Completed); + let output = load_activity(&RunHistory::for_folder(&root), &record.id, "writer"); + assert!(!output.is_empty(), "the agent's output was saved"); + assert!( + !app.swarm_dashboard.can_continue(), + "a completed run has nothing to continue" + ); + } + + #[tokio::test(flavor = "multi_thread")] + async fn history_lists_runs_newest_first_and_marks_interrupted_ones() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (mut app, _rx) = app_with_script(&root, "http://127.0.0.1:9"); + let history = RunHistory::for_folder(&root); + let inputs = RunInputs { + form_root: root.clone(), + ..Default::default() + }; + + let mut done = SwarmRunRecord::new( + "20260101-000000-000".into(), + root.join("s.gaviero"), + "older".into(), + root.clone(), + "plan-a".into(), + inputs.clone(), + ); + done.started_at = 100; + done.outcome = RunOutcome::Completed; + history.save(&done).unwrap(); + + // Cut short: the record still says running and the checkpoint has an + // agent that never finished. + let mut cut = SwarmRunRecord::new( + "20260102-000000-000".into(), + root.join("s.gaviero"), + "newer".into(), + root.clone(), + "plan-b".into(), + inputs, + ); + cut.started_at = 200; + history.save(&cut).unwrap(); + let mut checkpoint = ExecutionState::default(); + checkpoint.set_status("writer", NodeStatus::Running); + checkpoint.save(&root, "plan-b").unwrap(); + + open_panel(&mut app); + handle_action(&mut app, Action::Tab); + assert_eq!(app.swarm_dashboard.view, SwarmView::History); + let rows = &app.swarm_dashboard.history.rows; + assert_eq!(rows.len(), 2); + assert_eq!(rows[0].record.script_label, "newer"); + assert!(rows[0].interrupted && rows[0].resumable); + assert_eq!(rows[1].record.script_label, "older"); + assert!(!rows[1].interrupted && !rows[1].resumable); + + let text = render_text(&mut app); + let newer = text.lines().find(|l| l.contains("newer")).unwrap(); + assert!(newer.contains("interrupted") && newer.contains("can continue"), "{newer}"); + let older = text.lines().find(|l| l.contains("older")).unwrap(); + assert!(older.contains("completed") && !older.contains("can continue"), "{older}"); + + // Tab and Esc both go back to the scripts. + handle_action(&mut app, Action::Tab); + assert_eq!(app.swarm_dashboard.view, SwarmView::Picker); + handle_action(&mut app, Action::Tab); + assert!(handle_escape(&mut app)); + assert_eq!(app.swarm_dashboard.view, SwarmView::Picker); + } + + #[tokio::test(flavor = "multi_thread")] + async fn opening_a_past_run_shows_its_agents_and_saved_output() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (url, _asked) = fake_ollama("default_out/report.md"); + let (mut app, mut rx) = app_with_script(&root, &url); + run_to_end(&mut app, &mut rx, "the moon").await; + let live_output = app.swarm_dashboard.agents[0].activity.len(); + + // A later session: nothing in memory but what was saved. + let (mut app, _rx) = app_with_script(&root, &url); + open_panel(&mut app); + handle_action(&mut app, Action::Tab); + handle_action(&mut app, Action::Enter); + + let dash = &app.swarm_dashboard; + assert_eq!(dash.view, SwarmView::Run); + assert!(dash.viewing_history && !dash.running); + assert_eq!(dash.phase, "completed"); + assert_eq!(dash.agents.len(), 1); + assert_eq!(dash.agents[0].id, "writer"); + assert!(matches!(dash.agents[0].status, AgentStatus::Completed)); + assert_eq!(dash.agents[0].activity.len(), live_output, "saved output restored"); + + handle_action(&mut app, Action::InsertChar('u')); + assert!(app.swarm_dashboard.status_message.contains("Undo is offered only")); + assert!(handle_escape(&mut app)); + assert_eq!(app.swarm_dashboard.view, SwarmView::History); + } + + #[tokio::test(flavor = "multi_thread")] + async fn an_interrupted_run_continues_with_only_the_unfinished_agents() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + // The second agent's request fails, as when a usage limit runs out. + let (url, asked) = flaky_ollama(2); + let (mut app, mut rx) = app_with_source(&root, &url, TWO_STEP_SCRIPT); + + run_to_end(&mut app, &mut rx, "the moon").await; + assert_eq!( + app.swarm_dashboard.phase, + "completed with failures", + "{}", + app.swarm_dashboard.status_message + ); + assert_eq!(drain(&asked).len(), 2); + assert!(app.swarm_dashboard.can_continue()); + assert_eq!(records(&root)[0].outcome, RunOutcome::CompletedWithFailures); + + // Later, from History. + let (mut app, mut rx) = app_with_source(&root, &url, TWO_STEP_SCRIPT); + open_panel(&mut app); + handle_action(&mut app, Action::Tab); + assert!(app.swarm_dashboard.history.rows[0].resumable); + handle_action(&mut app, Action::Enter); + assert!(render_text(&mut app).contains("c continue")); + handle_action(&mut app, Action::InsertChar('c')); + assert!(!app.swarm_dashboard.running, "one `c` only asks"); + assert!(render_text(&mut app).contains("Press c again")); + handle_action(&mut app, Action::InsertChar('c')); + assert!( + app.swarm_dashboard.running, + "continue started: {}", + app.swarm_dashboard.status_message + ); + pump_until(&mut app, &mut rx, |a| !a.swarm_dashboard.running).await; + + let dash = &app.swarm_dashboard; + assert_eq!(dash.phase, "completed", "{}", dash.status_message); + let asked_now = drain(&asked); + assert_eq!(asked_now.len(), 1, "only the unfinished agent ran"); + assert!(asked_now[0].contains("SECOND"), "{}", asked_now[0]); + assert!(matches!(dash.agents[0].status, AgentStatus::Completed)); + assert!(matches!(dash.agents[1].status, AgentStatus::Completed)); + assert!(!dash.agents[0].activity.is_empty(), "the first pass's output is shown"); + assert!(root.join("out/second.md").is_file()); + + let saved = records(&root); + assert_eq!(saved.len(), 1, "continuing keeps the same record"); + assert_eq!(saved[0].outcome, RunOutcome::Completed); + assert_eq!(saved[0].resumes, 1); + assert!(!app.swarm_dashboard.can_continue()); + } + + /// The checkpoint is keyed by the agents: a script whose agents changed + /// cannot be continued from it. + #[tokio::test(flavor = "multi_thread")] + async fn a_run_whose_script_agents_changed_is_not_continued() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (url, asked) = flaky_ollama(2); + let (mut app, mut rx) = app_with_source(&root, &url, TWO_STEP_SCRIPT); + run_to_end(&mut app, &mut rx, "the moon").await; + assert!(app.swarm_dashboard.can_continue()); + drain(&asked); + + let script = root.join(".gaviero/dsl/report/report.gaviero"); + std::fs::write( + &script, + TWO_STEP_SCRIPT.replace("write part two", "write part two and three"), + ) + .unwrap(); + handle_action(&mut app, Action::InsertChar('c')); + handle_action(&mut app, Action::InsertChar('c')); + + let dash = &app.swarm_dashboard; + assert!(!dash.running); + assert!(dash.status_message.contains("changed"), "{}", dash.status_message); + assert!(drain(&asked).is_empty(), "nothing ran"); + assert_eq!(records(&root)[0].resumes, 0); + } + + /// The editor went away mid-run: the record still says running and the + /// checkpoint has the second agent unfinished. A later session lists it as + /// interrupted and continues it. + #[tokio::test(flavor = "multi_thread")] + async fn a_run_cut_short_by_closing_the_editor_is_continued_later() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (url, asked) = flaky_ollama(2); + let (mut app, mut rx) = app_with_source(&root, &url, TWO_STEP_SCRIPT); + run_to_end(&mut app, &mut rx, "the moon").await; + drain(&asked); + + let history = RunHistory::for_folder(&root); + let mut record = history.list().remove(0); + record.outcome = RunOutcome::Running; + record.ended_at = None; + history.save(&record).unwrap(); + let mut checkpoint = ExecutionState::load(&root, &record.plan_hash) + .unwrap() + .unwrap(); + assert_eq!(checkpoint.status("first"), NodeStatus::Completed); + checkpoint.set_status("second", NodeStatus::Running); + checkpoint.save(&root, &record.plan_hash).unwrap(); + + let (mut app, mut rx) = app_with_source(&root, &url, TWO_STEP_SCRIPT); + open_panel(&mut app); + handle_action(&mut app, Action::Tab); + let row = &app.swarm_dashboard.history.rows[0]; + assert!(row.interrupted && row.resumable); + handle_action(&mut app, Action::Enter); + assert_eq!(app.swarm_dashboard.phase, "interrupted"); + handle_action(&mut app, Action::InsertChar('c')); + handle_action(&mut app, Action::InsertChar('c')); + assert!(app.swarm_dashboard.running, "{}", app.swarm_dashboard.status_message); + pump_until(&mut app, &mut rx, |a| !a.swarm_dashboard.running).await; + + assert_eq!(app.swarm_dashboard.phase, "completed", "{}", app.swarm_dashboard.status_message); + let asked_now = drain(&asked); + assert_eq!(asked_now.len(), 1); + assert!(asked_now[0].contains("SECOND")); + assert_eq!(records(&root)[0].outcome, RunOutcome::Completed); + + // `r` now opens the script's form, though this session never had one. + handle_action(&mut app, Action::InsertChar('r')); + assert_eq!(app.swarm_dashboard.view, SwarmView::Form); + } + + #[tokio::test(flavor = "multi_thread")] + async fn an_undone_run_cannot_be_continued() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (url, _asked) = flaky_ollama(2); + let (mut app, mut rx) = app_with_source(&root, &url, TWO_STEP_SCRIPT); + run_to_end(&mut app, &mut rx, "the moon").await; + assert!(app.swarm_dashboard.can_continue()); + + let run = app.swarm_dashboard.run_id; + apply_update(&mut app, run, SwarmUpdate::Reverted(Ok(()))); + assert!(!app.swarm_dashboard.can_continue()); + handle_action(&mut app, Action::InsertChar('c')); + assert!(!app.swarm_dashboard.pending_continue_confirm); + assert!(records(&root)[0].reverted); + + open_panel(&mut app); + handle_escape(&mut app); + handle_action(&mut app, Action::Tab); + assert!(!app.swarm_dashboard.history.rows[0].resumable); + } } diff --git a/crates/gaviero-tui/src/panels/swarm_dashboard.rs b/crates/gaviero-tui/src/panels/swarm_dashboard.rs index 0b6d1f22..48118cee 100644 --- a/crates/gaviero-tui/src/panels/swarm_dashboard.rs +++ b/crates/gaviero-tui/src/panels/swarm_dashboard.rs @@ -23,6 +23,8 @@ use crate::widgets::scrollbar::render_scrollbar; use crate::widgets::text_input::TextInput; use gaviero_core::swarm::models::{AgentStatus, SwarmResult}; use gaviero_core::swarm::plan::ExecutionMode; +use gaviero_core::swarm::run_history::{RunAgentRecord, RunHistory, RunOutcome, SwarmRunRecord}; +use serde::{Deserialize, Serialize}; use gaviero_core::types::ModelTier; use gaviero_dsl::ast::ReviewerEntry; use gaviero_dsl::workflow_params::{list_literal_entry, parse_reviewers_override}; @@ -86,7 +88,9 @@ pub enum SwarmUpdate { // ── Activity ──────────────────────────────────────────────────── -#[derive(Debug, Clone, PartialEq)] +/// Serialized into each agent's saved output (`run_history` agent logs). +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] pub enum ActivityKind { Text, ToolCall, @@ -94,12 +98,84 @@ pub enum ActivityKind { FileChange, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct ActivityLine { pub kind: ActivityKind, pub text: String, } +// ── Run history ───────────────────────────────────────────────── + +/// The record of the run on screen: the live one, or one opened from history. +pub struct RunRecordState { + pub history: RunHistory, + pub record: SwarmRunRecord, + /// The run can be continued from its checkpoint (`c`). + pub resumable: bool, + /// Activity revision last saved, per agent. + pub saved_revisions: HashMap, +} + +impl RunRecordState { + pub fn new(history: RunHistory, record: SwarmRunRecord) -> Self { + Self { + history, + record, + resumable: false, + saved_revisions: HashMap::new(), + } + } +} + +/// One past run in the History list. +#[derive(Debug, Clone)] +pub struct HistoryRow { + pub history: RunHistory, + pub record: SwarmRunRecord, + pub resumable: bool, + /// Its record says running, but nothing is running it. + pub interrupted: bool, +} + +impl HistoryRow { + /// What became of the run, as the list and the run header say it. + pub fn outcome_label(&self) -> &'static str { + if self.interrupted { + "interrupted" + } else { + self.record.outcome.label() + } + } +} + +pub struct HistoryList { + pub rows: Vec, + pub scroll: ScrollState, + list_rect: Rect, +} + +impl HistoryList { + fn new() -> Self { + Self { + rows: Vec::new(), + scroll: ScrollState::new(), + list_rect: Rect::default(), + } + } + + pub fn selected(&self) -> Option<&HistoryRow> { + self.rows.get(self.scroll.selected) + } + + pub fn index_at(&self, column: u16, row: u16) -> Option { + if !self.list_rect.contains((column, row).into()) { + return None; + } + let index = self.scroll.offset + (row - self.list_rect.y) as usize; + (index < self.rows.len()).then_some(index) + } +} + // ── Picker ────────────────────────────────────────────────────── /// One script the picker can offer. @@ -1421,6 +1497,8 @@ impl AgentEntry { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SwarmView { Picker, + /// Past runs, newest first (`Tab` from the picker). + History, Form, Run, } @@ -1494,6 +1572,8 @@ pub struct SwarmDashboardState { pub follow_running: bool, pub diff_agent: Option, pub pending_undo_confirm: bool, + /// `c` was pressed once; a second `c` continues the run. + pub pending_continue_confirm: bool, pub pending_stop_confirm: bool, pub table_rect: Rect, pub detail_rect: Rect, @@ -1503,6 +1583,11 @@ pub struct SwarmDashboardState { pub last_forms: HashMap, /// Script of the run on screen, for `r`. pub last_script: Option, + pub history: HistoryList, + /// Record of the run on screen, saved as it goes. + pub record: Option, + /// The run on screen was opened from History, not run in this session. + pub viewing_history: bool, } impl SwarmDashboardState { @@ -1532,12 +1617,16 @@ impl SwarmDashboardState { follow_running: true, diff_agent: None, pending_undo_confirm: false, + pending_continue_confirm: false, pending_stop_confirm: false, table_rect: Rect::default(), detail_rect: Rect::default(), wrapped: WrappedOutput::default(), last_forms: HashMap::new(), last_script: None, + history: HistoryList::new(), + record: None, + viewing_history: false, } } @@ -1590,11 +1679,99 @@ impl SwarmDashboardState { self.follow_running = true; self.diff_agent = None; self.pending_undo_confirm = false; + self.pending_continue_confirm = false; self.pending_stop_confirm = false; self.wrapped = WrappedOutput::default(); + self.record = None; + self.viewing_history = false; self.run_id } + /// An agent a continued run will not run again: done in an earlier pass, + /// with the output that pass saved. + pub fn seed_completed(&mut self, id: &str, detail: String, activity: Vec) { + let entry = self.agent_or_insert(id); + entry.status = AgentStatus::Completed; + entry.detail = detail; + entry.started_at = None; + entry.finished_after = None; + entry.saw_text = activity.iter().any(|l| l.kind == ActivityKind::Text); + entry.activity = activity; + entry.trim_activity(); + } + + /// Every agent's state, as the run record keeps it. + pub fn record_agents(&self) -> Vec { + self.agents + .iter() + .map(|a| RunAgentRecord { + id: a.id.clone(), + status: a.status.clone(), + detail: a.detail.clone(), + branch: a.branch.clone(), + modified_files: a.modified_files.clone(), + tier: a.model_tier, + backend: a.backend.clone(), + elapsed_secs: a.elapsed().map(|d| d.as_secs()), + }) + .collect() + } + + /// Show a past run: its agents as they ended, with the output each saved. + /// Nothing runs; `c` continues it when `row.resumable`. + pub fn show_record(&mut self, row: HistoryRow, mut activity: HashMap>) { + self.run_id += 1; + self.running = false; + self.abort = None; + self.view = SwarmView::Run; + self.script_label = row.record.script_label.clone(); + self.run_root = Some(row.record.root.clone()); + self.agents = row + .record + .agents + .iter() + .map(|a| { + let mut entry = AgentEntry::new(a.id.clone(), a.status.clone(), a.detail.clone()); + entry.started_at = None; + entry.branch = a.branch.clone(); + entry.modified_files = a.modified_files.clone(); + entry.model_tier = a.tier; + entry.backend = a.backend.clone(); + entry.finished_after = a.elapsed_secs.map(Duration::from_secs); + entry.activity = activity.remove(&a.id).unwrap_or_default(); + entry.saw_text = entry.activity.iter().any(|l| l.kind == ActivityKind::Text); + entry.trim_activity(); + entry + }) + .collect(); + self.scroll.reset(); + self.phase = row.outcome_label().to_string(); + self.tier_current = 0; + self.tier_total = 0; + self.result = None; + self.estimated_cost_usd = row.record.cost_usd; + self.status_message = row.record.message.clone(); + self.detail_scroll = 0; + self.detail_auto_scroll = true; + self.focus = DashboardFocus::Table; + self.output_maximized = false; + self.follow_running = false; + self.diff_agent = None; + self.pending_undo_confirm = false; + self.pending_continue_confirm = false; + self.pending_stop_confirm = false; + self.wrapped = WrappedOutput::default(); + let mut state = RunRecordState::new(row.history, row.record); + state.resumable = row.resumable; + self.record = Some(state); + self.viewing_history = true; + } + + /// The run on screen can be continued now. + pub fn can_continue(&self) -> bool { + !self.running && self.record.as_ref().is_some_and(|r| r.resumable) + } + pub fn counts(&self) -> AgentCounts { let mut counts = AgentCounts::default(); for agent in &self.agents { @@ -1892,6 +2069,7 @@ impl SwarmDashboardState { } match self.view { SwarmView::Picker => self.render_picker(area, buf, focused), + SwarmView::History => self.render_history(area, buf, focused), SwarmView::Form => self.render_form(area, buf, focused), SwarmView::Run => self.render_run(area, buf, focused), } @@ -1965,13 +2143,114 @@ impl SwarmDashboardState { put(buf, area.x + 1, footer_y, right, warning, fg(theme::WARNING)); } let hint = if self.has_run() { - " ↑↓ select Enter open Esc back to run" + " ↑↓ select Enter open Tab history Esc back to run" } else { - " ↑↓ select Enter open type to filter or a path" + " ↑↓ select Enter open Tab history type to filter or a path" }; put(buf, area.x, area.bottom() - 1, right, hint, fg(theme::TEXT_DIM)); } + fn render_history(&mut self, area: Rect, buf: &mut Buffer, focused: bool) { + let bg = theme::PANEL_BG; + let right = area.right(); + put(buf, area.x, area.y, right, " SWARM run history", bold(theme::WARNING)); + + let footer_rows: u16 = 1; + let list = Rect { + x: area.x, + y: area.y + 1, + width: area.width, + height: area.height.saturating_sub(1 + footer_rows), + }; + self.history.list_rect = list; + self.history.scroll.set_viewport(list.height as usize); + self.history.scroll.ensure_visible_on_render(); + + if self.history.rows.is_empty() && list.height > 0 { + put( + buf, + list.x, + list.y, + right, + " No runs yet. Runs started here are kept in .gaviero/swarm-runs.", + fg(theme::TEXT_DIM), + ); + } + let selected_bg = if focused { + theme::FOCUSED_SELECTION_BG + } else { + theme::DARK_BG + }; + for (index, row) in self + .history + .rows + .iter() + .enumerate() + .skip(self.history.scroll.offset) + .take(list.height as usize) + { + let y = list.y + (index - self.history.scroll.offset) as u16; + let row_bg = if index == self.history.scroll.selected { + selected_bg + } else { + bg + }; + for x in area.x..right { + buf[(x, y)].set_style(Style::default().bg(row_bg)); + } + let x = put( + buf, + area.x + 1, + y, + right, + &format!("{} ", format_started(row.record.started_at)), + fg(theme::TEXT_DIM).bg(row_bg), + ); + let x = put(buf, x, y, right, &row.record.script_label, bold(theme::TEXT_FG).bg(row_bg)); + let outcome_color = if row.interrupted { + theme::WARNING + } else { + match row.record.outcome { + RunOutcome::Completed => theme::SUCCESS, + RunOutcome::Running => theme::FOCUS_BORDER, + _ => theme::ERROR, + } + }; + let x = put( + buf, + x, + y, + right, + &format!(" {}", row.outcome_label()), + fg(outcome_color).bg(row_bg), + ); + let mut tail = format!( + " {}/{} done", + row.record.completed_count(), + row.record.agents.len() + ); + if row.record.cost_usd > 0.0 { + tail.push_str(&format!(" ~${:.3}", row.record.cost_usd)); + } + if row.record.resumes > 0 { + tail.push_str(&format!(" continued ×{}", row.record.resumes)); + } + let x = put(buf, x, y, right, &tail, fg(theme::TEXT_DIM).bg(row_bg)); + if row.resumable { + put(buf, x, y, right, " ⟳ can continue", bold(theme::INFO_CYAN).bg(row_bg)); + } + } + + put( + buf, + area.x, + area.bottom() - 1, + right, + " ↑↓ select Enter open Tab/Esc scripts", + fg(theme::TEXT_DIM), + ); + } + fn render_picker_empty(&self, list: Rect, buf: &mut Buffer) { let right = list.right(); let mut y = list.y; @@ -2337,25 +2616,33 @@ impl SwarmDashboardState { } let hint = self.run_hint(); - let style = if self.pending_stop_confirm || self.pending_undo_confirm { + let style = if self.pending_stop_confirm + || self.pending_undo_confirm + || self.pending_continue_confirm + { bold(theme::WARNING) } else { fg(theme::TEXT_DIM) }; - put(buf, area.x + 1, footer_y, right, hint, style); + put(buf, area.x + 1, footer_y, right, &hint, style); } - fn run_hint(&self) -> &'static str { + fn run_hint(&self) -> String { + let continue_hint = if self.can_continue() { " c continue" } else { "" }; if self.pending_stop_confirm { - "Press x again to stop every agent. Esc cancels." + "Press x again to stop every agent. Esc cancels.".to_string() } else if self.pending_undo_confirm { - "Press u again to undo the whole run. Esc cancels." + "Press u again to undo the whole run. Esc cancels.".to_string() + } else if self.pending_continue_confirm { + "Press c again to run the agents this run did not complete. Esc cancels.".to_string() } else if self.diff_agent.is_some() { - "↑↓ scroll Esc back to output" + "↑↓ scroll Esc back to output".to_string() } else if self.running { - "↑↓ agent Enter expand Tab scroll output x stop" + "↑↓ agent Enter expand Tab scroll output x stop".to_string() + } else if self.viewing_history { + format!("Esc history{continue_hint} ↑↓ agent Enter expand d diff") } else { - "Esc scripts r run again ↑↓ agent Enter expand d diff u undo" + format!("Esc scripts{continue_hint} r run again ↑↓ agent Enter expand d diff u undo") } } @@ -2731,6 +3018,13 @@ fn spinner() -> &'static str { ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"][(tick % 10) as usize] } +/// A run's start time in local time, for the History list. +fn format_started(unix_secs: i64) -> String { + chrono::DateTime::from_timestamp(unix_secs, 0) + .map(|t| t.with_timezone(&chrono::Local).format("%Y-%m-%d %H:%M").to_string()) + .unwrap_or_else(|| "?".to_string()) +} + /// First row to draw so the focused field is on screen, preferring to show /// its label over its tail. fn form_offset(first: usize, last: usize, viewport: usize, total: usize) -> usize {