diff --git a/crates/gaviero-tui/CLAUDE.md b/crates/gaviero-tui/CLAUDE.md index 3c5eea68..80e08fba 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). 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 does not mirror the panel. +**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 does not mirror the panel. **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. diff --git a/crates/gaviero-tui/src/app/swarm_run.rs b/crates/gaviero-tui/src/app/swarm_run.rs index c9d7f87d..b81ab307 100644 --- a/crates/gaviero-tui/src/app/swarm_run.rs +++ b/crates/gaviero-tui/src/app/swarm_run.rs @@ -243,8 +243,12 @@ fn open_form(app: &mut App, script: ScriptRow) { let profiles = ProfileCatalog::scan(&app.workspace, &ScriptCatalog::global_scripts_dir()) .names(); - app.swarm_dashboard.form = - Some(SwarmForm::new(script, inspection, folders, selected).with_profiles(profiles)); + let mut form = + SwarmForm::new(script, inspection, folders, selected).with_profiles(profiles); + if let Some(previous) = app.swarm_dashboard.last_forms.get(&form.script.path) { + form.restore_from(previous); + } + app.swarm_dashboard.form = Some(form); app.swarm_dashboard.view = SwarmView::Form; reload_profile_info(app); refresh_form_warnings(app); @@ -1156,6 +1160,17 @@ fn handle_run(app: &mut App, action: Action) { show_selected_diff(app); 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()); + if let Some(script) = script { + open_form(app, script); + } + return; + } Action::InsertChar('u') if !dash.running => { undo_pressed(app); return; @@ -1753,6 +1768,69 @@ tier spare other assert!(notes.contains("Tier local runs on client other"), "{notes}"); } + #[tokio::test(flavor = "multi_thread")] + async fn running_a_script_again_starts_from_the_values_of_its_last_run() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (url, _asked) = fake_ollama("custom_out/report.md"); + let (mut app, mut rx) = app_with_script(&root, &url); + + open_filled_form(&mut app); + handle_action(&mut app, Action::Tab); + for _ in 0.."default_out".len() { + handle_action(&mut app, Action::Backspace); + } + type_text(&mut app, "custom_out"); + press_run(&mut app); + assert_eq!(app.swarm_dashboard.view, SwarmView::Run); + pump_until(&mut app, &mut rx, |a| !a.swarm_dashboard.running).await; + + // `r` on the finished run: the same values, ready to run again. + handle_action(&mut app, Action::InsertChar('r')); + assert_eq!(app.swarm_dashboard.view, SwarmView::Form); + assert_eq!(field_text(&app, "Prompt"), "the moon"); + assert_eq!(field_text(&app, "OUT_DIR"), "custom_out"); + assert!(app.swarm_dashboard.form.as_ref().unwrap().on_run_button()); + + // Picking the script from the list restores them too. + handle_escape(&mut app); + assert_eq!(app.swarm_dashboard.view, SwarmView::Picker); + handle_action(&mut app, Action::Enter); + assert_eq!(field_text(&app, "Prompt"), "the moon"); + assert_eq!(field_text(&app, "OUT_DIR"), "custom_out"); + } + + #[tokio::test(flavor = "multi_thread")] + async fn a_stopped_run_keeps_its_values_for_the_next_one() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().canonicalize().unwrap(); + let (mut app, mut rx) = app_with_script(&root, &silent_ollama()); + + open_filled_form(&mut app); + press_run(&mut app); + pump_until(&mut app, &mut rx, |a| { + matches!(a.swarm_dashboard.agents[0].status, AgentStatus::Running) + }) + .await; + assert!( + handle_action_is_ignored_while_running(&mut app), + "`r` does nothing while the run is going" + ); + handle_action(&mut app, Action::InsertChar('x')); + handle_action(&mut app, Action::InsertChar('x')); + pump_until(&mut app, &mut rx, |a| !a.swarm_dashboard.running).await; + + handle_action(&mut app, Action::InsertChar('r')); + assert_eq!(app.swarm_dashboard.view, SwarmView::Form); + assert_eq!(field_text(&app, "Prompt"), "the moon"); + } + + /// Press `r` during a run; true when the run view stayed on screen. + fn handle_action_is_ignored_while_running(app: &mut App) -> bool { + handle_action(app, Action::InsertChar('r')); + app.swarm_dashboard.view == SwarmView::Run + } + #[tokio::test(flavor = "multi_thread")] async fn stopping_ends_a_run_that_would_otherwise_never_finish() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/gaviero-tui/src/panels/swarm_dashboard.rs b/crates/gaviero-tui/src/panels/swarm_dashboard.rs index 962023df..78722285 100644 --- a/crates/gaviero-tui/src/panels/swarm_dashboard.rs +++ b/crates/gaviero-tui/src/panels/swarm_dashboard.rs @@ -5,6 +5,7 @@ //! [`crate::app::swarm_run`]; every change arrives through the event loop as a //! [`SwarmUpdate`]. +use std::collections::HashMap; use std::path::PathBuf; use std::time::{Duration, Instant}; @@ -684,9 +685,54 @@ impl SwarmForm { } } + /// Take every value `previous` (this script's last run) was launched + /// with: the Folder / Workflow / Profile choices, the agent and tier rows, + /// and every typed input still on the form. Values for inputs the script + /// no longer has are dropped. Focus lands on Run. + pub fn restore_from(&mut self, previous: &SwarmForm) { + for role in [FieldRole::Folder, FieldRole::Workflow, FieldRole::Profile] { + let Some(name) = previous + .fields + .iter() + .find(|f| f.role == role) + .and_then(FormField::choice) + else { + continue; + }; + if let Some(field) = self.fields.iter_mut().find(|f| f.role == role) + && let FieldValue::Choice { options, selected } = &mut field.value + && let Some(index) = options.iter().position(|o| o == name) + { + *selected = index; + } + } + self.profile_info = previous.profile_info.clone(); + let (folder, workflow, profile) = ( + self.selected_folder(), + self.selected_workflow(), + self.choice_of(FieldRole::Profile), + ); + let mut fresh = self.build_fields(folder, workflow, profile, &previous.fields); + for field in &mut fresh { + if matches!(field.value, FieldValue::Choice { .. }) { + continue; + } + if let Some(old) = previous + .fields + .iter() + .find(|o| o.name == field.name && o.role == field.role && o.is_edited()) + { + field.value = old.value.clone(); + } + } + self.fields = fresh; + self.focus = self.fields.len(); + } + /// Rebuild every field from the current choices, keeping what the user /// typed or chose under the same name. fn rebuild(&mut self) { + let on_run = self.on_run_button(); let focused = self.focus; let folder = self.selected_folder(); let workflow = self.selected_workflow(); @@ -705,7 +751,11 @@ impl SwarmForm { } } self.fields = fresh; - self.focus = focused.min(self.fields.len()); + self.focus = if on_run { + self.fields.len() + } else { + focused.min(self.fields.len()) + }; } /// The first field to type into. The Folder / Workflow / Profile @@ -1441,6 +1491,11 @@ pub struct SwarmDashboardState { pub table_rect: Rect, pub detail_rect: Rect, wrapped: WrappedOutput, + /// The form each script was last launched with, by script path, so the + /// next run of that script starts from the same values (this session). + pub last_forms: HashMap, + /// Script of the run on screen, for `r`. + pub last_script: Option, } impl SwarmDashboardState { @@ -1474,6 +1529,8 @@ impl SwarmDashboardState { table_rect: Rect::default(), detail_rect: Rect::default(), wrapped: WrappedOutput::default(), + last_forms: HashMap::new(), + last_script: None, } } @@ -1494,7 +1551,13 @@ impl SwarmDashboardState { self.running = true; self.abort = None; self.view = SwarmView::Run; - self.form = None; + // Kept, so running the script again starts from these values. + if let Some(mut form) = self.form.take() { + form.completion.reset(); + form.error = None; + self.last_script = Some(form.script.path.clone()); + self.last_forms.insert(form.script.path.clone(), form); + } self.script_label = script_label; self.run_root = Some(run_root); self.agents = planned @@ -2285,7 +2348,7 @@ impl SwarmDashboardState { } else if self.running { "↑↓ agent Enter expand Tab scroll output x stop" } else { - "Esc scripts ↑↓ agent Enter expand d diff u undo" + "Esc scripts r run again ↑↓ agent Enter expand d diff u undo" } }