Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(agent-tui): monitor and background-shell rows, /monitors and /stop
Akarshan Biswas3423b27dd8fe
diff --git a/docs/src/pages/docs/agent/console.mdx b/docs/src/pages/docs/agent/console.mdxindex 698478762..1d25b964a 100644--- a/docs/src/pages/docs/agent/console.mdx+++ b/docs/src/pages/docs/agent/console.mdx@@ -130,6 +130,24 @@ generated, and it shows no elapsed time: that stays in the header. The footer reA subagent that finishes during the wait leaves its `• Finished NAME` row below the answer, and theagent picks up from there.+### Monitors and background shells++A monitor that matches leaves a `◔ Monitor mon-1: 'build' matched` row; one that times out leaves a+`⚠` warning instead. When a command the agent sent to the background ends, it leaves a tool row+(`• Background command finished after 12s: make build`), or a `⚠` warning if it failed. If the dock+has no room for every monitor, its last row reads `+N more · /monitors`. `/monitors` lists all of+them, and <kbd>x</kbd> stops the selected one.++While commands run in the background, a dim row sits above the input:++```+2 background shells running · /shells to view · /stop to close+```++During a turn the row is hidden so the input area keeps its height, and the working row ends with+`· 2 bg shells` instead. `/shells` lists the commands; <kbd>x</kbd> stops one, and `/stop` stops+all of them.+### MotionThe working row and the `[thinking]` badge animate: a brightness sweep crosses the label. Adiff --git a/docs/src/pages/docs/agent/slash-commands.mdx b/docs/src/pages/docs/agent/slash-commands.mdxindex f6847d4b4..cc2d68ba4 100644--- a/docs/src/pages/docs/agent/slash-commands.mdx+++ b/docs/src/pages/docs/agent/slash-commands.mdx@@ -56,7 +56,9 @@ See [Sessions](/docs/agent/sessions).| `/usage [session\|run\|account\|daily\|requests\|limits\|<execution-id>]` | Bare: an overview of both answers -- this session's estimate over your account's recorded spend, each labelled with its source and never added together. The session half draws immediately; the account half fills in when the read lands, or says why it could not. `session` shows the local estimate alone: total tokens and estimated cost, then the costliest models behind it, priced from what the provider publishes (a model it publishes no price for is listed as such rather than counted as free); `m` expands the model list past the top few. The other views read what the provider actually recorded, each leading with the figure it was opened for -- account spend, the newest day, whether spending is allowed -- with breakdowns folded behind the same `m`. Reported amounts print exactly as the provider sent them, so they never read as a local estimate. Same docked readout as `/context`, and only one is open at a time; the input field is inactive while it is up || `/cancel [N]` | Cancel queued messages. Bare: all, or an index || `/agents` | Inspect this session's subagents, running and finished. Enter opens one's run, drawn like the conversation (PgUp/PgDn to scroll); `m` messages it, `x` stops it |-| `/shells` (alias `/jobs`) | Inspect and stop background shell commands the agent left running |+| `/shells` (alias `/jobs`) | Inspect background shell commands the agent left running: one `• command` row each, with how long it has run; `x` stops the selected one |+| `/stop` | Stop every background shell command at once |+| `/monitors` | Inspect the monitors watching for a condition: id, name, polls so far and script; `x` stops the selected one || `/find <term>` | Search the whole transcript (case-insensitive) and jump to the nearest match, unfolding it if needed. <kbd>n</kbd> / <kbd>N</kbd> step to the next / previous match, wrapping at the ends, while the input is empty; <kbd>Esc</kbd> or the next message clears it. A miss is reported in the footer |See [Run modes](/docs/agent/run-modes), [Goals and todos](/docs/agent/goals-and-todos), anddiff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/monitor.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/monitor.rsindex 75134667b..3d6da1ef2 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/monitor.rs+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/monitor.rs@@ -445,7 +445,7 @@ async fn run_monitor(set.push_update(MonitorUpdate {monitor_id: monitor_id.to_string(),name: spec.name.clone(),- headline: format!("Monitor {monitor_id}: '{}' timed out", spec.name),+ headline: headline(monitor_id, &spec.name, false),text: format!("Monitor '{monitor_id}' ('{}') timed out after {}s and {} without a match. \It has stopped.",@@ -461,6 +461,24 @@ async fn run_monitor(}}+/// Last word of a matched monitor's headline; a display classifies the+/// headline by it, so it is shared rather than repeated as a literal.+pub const MATCHED_OUTCOME: &str = "matched";++/// Last words of a timed-out monitor's headline (see [`MATCHED_OUTCOME`]).+pub const TIMED_OUT_OUTCOME: &str = "timed out";++/// `Monitor mon-1: 'build' matched` (or `timed out`): the one producer of a+/// monitor headline, so the display that styles it by outcome cannot drift.+pub fn headline(monitor_id: &str, name: &str, matched: bool) -> String {+ let outcome = if matched {+ MATCHED_OUTCOME+ } else {+ TIMED_OUT_OUTCOME+ };+ format!("Monitor {monitor_id}: '{name}' {outcome}")+}+fn match_update(monitor_id: &str, name: &str, content: &str) -> MonitorUpdate {let body = if content.is_empty() {" (the script exited 0 with no output)".to_string()@@ -470,7 +488,7 @@ fn match_update(monitor_id: &str, name: &str, content: &str) -> MonitorUpdate {MonitorUpdate {monitor_id: monitor_id.to_string(),name: name.to_string(),- headline: format!("Monitor {monitor_id}: '{name}' matched"),+ headline: headline(monitor_id, name, true),text: format!("Monitor '{monitor_id}' ('{name}') matched{body}\n\nIt has stopped."),matched: true,}diff --git a/src-tauri/src/core/agent/bg_shell.rs b/src-tauri/src/core/agent/bg_shell.rsindex a58ffa511..058f7d87a 100644--- a/src-tauri/src/core/agent/bg_shell.rs+++ b/src-tauri/src/core/agent/bg_shell.rs@@ -159,10 +159,7 @@ impl BackgroundShells {state.pending = live;for p in expired {state.notices.push(ShellNotice {- headline: format!(- "Background command still running: {}",- short_command(&p.command)- ),+ headline: still_running_headline(&p.command),text: abandoned_text(&p),});}@@ -180,15 +177,31 @@ fn short_command(command: &str) -> String {format!("{head}...")}-fn headline(done: &ShellDone) -> String {- let verb = if done.failed { "failed" } else { "finished" };+/// Opening words of a failed command's headline. The TUI styles a headline by+/// how it opens, so these are shared with it rather than repeated as literals.+pub(crate) const FAILED_PREFIX: &str = "Background command failed after ";++/// Opening words of a finished command's headline (see [`FAILED_PREFIX`]).+pub(crate) const FINISHED_PREFIX: &str = "Background command finished after ";++pub(crate) fn headline(done: &ShellDone) -> String {+ let prefix = if done.failed {+ FAILED_PREFIX+ } else {+ FINISHED_PREFIX+ };format!(- "Background command {verb} after {}s: {}",+ "{prefix}{}s: {}",done.elapsed_secs,short_command(&done.command))}+/// The headline of a command the run stopped waiting on.+pub(crate) fn still_running_headline(command: &str) -> String {+ format!("Background command still running: {}", short_command(command))+}+fn text(done: &ShellDone) -> String {let verb = if done.failed {"finished with a nonzero exit"diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex d8b184c70..07e3e86ce 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -429,6 +429,51 @@ const SYSTEM_GLYPH: &str = "•";const WARN_GLYPH: &str = "⚠";const ERROR_GLYPH: &str = "■";+/// Gutter glyph for a monitor: the dock, `/monitors` and a match row share it.+const MONITOR_GLYPH: &str = "◔";++/// What a background ping's headline reports, so each kind gets the row its+/// news deserves rather than every ping reading as the same dim note.+///+/// Read off the headline because the headline is all `StreamEvent::Notice`+/// carries, and that event is published wire protocol (`protocol/schema.json`+/// and the generated ADK types): a display-only styling hint is not worth a+/// protocol change. The producers expose the exact words matched here, and a+/// test builds every headline through them, so a rewording fails a test+/// instead of quietly restyling the row.+#[derive(Clone, Copy, Debug, PartialEq)]+enum NoticeKind {+ MonitorMatched,+ MonitorTimedOut,+ ShellFinished,+ ShellFailed,+ /// Anything else (a hook notice, a still-running shell): a plain note.+ Other,+}++impl NoticeKind {+ fn of(headline: &str) -> Self {+ use tauri_plugin_agent_tools::tools::monitor::{MATCHED_OUTCOME, TIMED_OUT_OUTCOME};+ use crate::core::agent::bg_shell::{FAILED_PREFIX, FINISHED_PREFIX};+ let outcome = |word: &str| {+ headline+ .strip_suffix(word)+ .is_some_and(|rest| rest.ends_with("' "))+ };+ if headline.starts_with("Monitor ") && outcome(MATCHED_OUTCOME) {+ NoticeKind::MonitorMatched+ } else if headline.starts_with("Monitor ") && outcome(TIMED_OUT_OUTCOME) {+ NoticeKind::MonitorTimedOut+ } else if headline.starts_with(FAILED_PREFIX) {+ NoticeKind::ShellFailed+ } else if headline.starts_with(FINISHED_PREFIX) {+ NoticeKind::ShellFinished+ } else {+ NoticeKind::Other+ }+ }+}+/// Gutter glyph for goal-loop lines, matching the header's `◎ /goal` badge.const GOAL_GLYPH: &str = "◎";@@ -663,6 +708,8 @@ enum PickerKind {/// `/shells`: the background-shell inspector. Lists commands the `bash` tool/// detached (outran their timeout, still running), each stoppable with `x`.BackgroundShells,+ /// `/monitors`: the session's running monitors, each stoppable with `x`.+ Monitors,}/// Interactive list overlay (`/resume`, `/login`, `/mcp`, etc.): rows with a@@ -725,6 +772,7 @@ impl Picker {PickerKind::Agents => " subagents ",PickerKind::AgentDetail => " subagent ",PickerKind::BackgroundShells => " background shells ",+ PickerKind::Monitors => " monitors ",}}@@ -751,7 +799,9 @@ impl Picker {PickerKind::McpServer => " ↑/↓ select Enter run Esc back",PickerKind::Agents => " ↑/↓ select Enter view m message x stop Esc close",PickerKind::AgentDetail => " PgUp/PgDn scroll m message x stop Esc back",- PickerKind::BackgroundShells => " ↑/↓ select x stop Esc close",+ PickerKind::BackgroundShells | PickerKind::Monitors => {+ " ↑/↓ select x stop Esc close"+ }}}}@@ -5114,6 +5164,43 @@ impl App {self.system(Level::Info, text);}+ /// A background ping's headline, as the row its `NoticeKind` calls for: a+ /// monitor match under the monitor glyph, a timeout or a failed command as+ /// a warning, a finished command as a finished tool row. `who` names the+ /// subagent the ping came from, ahead of the headline.+ fn notice(&mut self, text: &str, who: Option<&str>) {+ let body = match who {+ Some(name) => format!("{name}: {text}"),+ None => text.to_string(),+ };+ match NoticeKind::of(text) {+ NoticeKind::MonitorMatched => {+ self.scrollback = 0;+ self.transcript.gap(Kind::Meta);+ self.transcript.push_row(RowKind::System {+ glyph: MONITOR_GLYPH,+ cont: " ",+ gutter: Style::new().fg(theme::accent()),+ body: vec![Span::raw(body)],+ bg: None,+ });+ }+ NoticeKind::MonitorTimedOut | NoticeKind::ShellFailed => {+ self.system(Level::Warn, &body)+ }+ NoticeKind::ShellFinished => {+ self.scrollback = 0;+ self.transcript.gap(Kind::Tool);+ self.transcript.push_row(RowKind::Tool {+ state: ToolState::Success,+ label: body,+ reserve: TOOL_ROW_RESERVE,+ });+ }+ NoticeKind::Other => self.note(&body),+ }+ }+/// A body row of a multi-line system block (`/help`, `/threads`, a goal/// status), under the header its `system*` call pushed. The gutter keeps/// running so the block reads as one unit instead of leaving its body@@ -5255,7 +5342,7 @@ impl App {return;}for notice in ¬ices {- self.note(¬ice.headline);+ self.notice(¬ice.headline, None);}if self.model.is_empty() {self.note("monitor update dropped: not signed in, run /login first");@@ -5283,7 +5370,7 @@ impl App {return;}for notice in &shells {- self.note(¬ice.headline);+ self.notice(¬ice.headline, None);}if self.model.is_empty() {self.note("background update dropped: not signed in, run /login first");@@ -7355,7 +7442,7 @@ impl App {StreamEvent::Notice { text } => {self.transcript.finalize_tool_group();self.transcript.flush_assistant();- self.note(&text);+ self.notice(&text, None);}StreamEvent::Monitors { monitors } => self.monitors = monitors,// The live countdown carries every attempt; the transcript notes@@ -7612,7 +7699,8 @@ impl App {// user's business as the parent's: dropping it made a child's// compaction invisible.StreamEvent::Notice { text } => {- self.note(&format!("{name}: {text}"));+ let who: &str = name;+ self.notice(&text, Some(who));}// The live countdown belongs to the parent's request; a child's// retry gets the same one-line note the parent's first retry does.@@ -14279,6 +14367,16 @@ async fn handle_key(None => {}}}+ // `/monitors`: `x` stops the selected monitor through the same+ // stop the model's `monitor` tool uses, and reports its answer.+ KeyCode::Char('x') if picker.kind == PickerKind::Monitors => {+ let id = picker.items[picker.selected].value.clone();+ if !id.is_empty() {+ let report = app.monitor_set.stop(&id);+ app.monitors = app.monitor_set.snapshot();+ app.note(&report);+ }+ }// Collection picker: Space toggles the selected plugin, Enter hands// the checked set to the loop (see `plugin_select_request`). Rows// already installed stay displayed but are not toggleable -- checking@@ -14389,7 +14487,8 @@ async fn handle_key(// acted on with `x` (stop), not Enter.PickerKind::Agents| PickerKind::AgentDetail- | PickerKind::BackgroundShells => {}+ | PickerKind::BackgroundShells+ | PickerKind::Monitors => {}PickerKind::PluginConnect => {}}}@@ -15108,6 +15207,18 @@ const SLASH_COMMANDS: &[SlashCommand] = &[description: "Alias of /shells: inspect and stop background shell commands",alias_of: Some("/shells"),},+ SlashCommand {+ name: "/stop",+ hint: "",+ description: "Stop every background shell command the agent left running",+ alias_of: None,+ },+ SlashCommand {+ name: "/monitors",+ hint: "",+ description: "Inspect and stop the monitors watching for a condition",+ alias_of: None,+ },SlashCommand {name: "/plugin",hint: "[list|install <spec>|remove <name>|search [query]|setup [name]]",@@ -15372,6 +15483,8 @@ async fn run_command("mcp" => open_mcp_picker(app, mcp_servers).await,"agents" => open_agents_picker(app),"shells" | "jobs" => open_background_shells_picker(app),+ "stop" => stop_background_shells(app),+ "monitors" => open_monitors_picker(app),"plugin" => plugin_command(app, arg).await,"reload" => reload_command(app, arg),"skills" => skills_command(app),@@ -18231,34 +18344,111 @@ fn open_background_shells_picker(app: &mut App) {});}-/// One row per detached background shell (`<elapsed> <command>`), whose `value`-/// is the real pid so `x` can stop it. A single watermark row when nothing is-/// backgrounded. `shells` is `App::bg_shells` (already filtered to backgrounded-/// and refreshed off the render path), so this stays a pure render function.+/// `/stop`: stop every background shell the user can see listed, where `x` in+/// `/shells` stops one. `kill` skips a pid that already finished, so the+/// frame `bg_shells` may lag the registry by cannot stop the wrong one.+fn stop_background_shells(app: &mut App) {+ let stopped = std::mem::take(&mut app.bg_shells)+ .into_iter()+ .filter(|s| tauri_plugin_agent_tools::tools::proc::kill(s.pid))+ .count();+ if stopped == 0 {+ app.note("no background shells running");+ } else {+ app.note(&format!("Stopping {}.", pluralize("background shell", stopped)));+ }+}++/// The lone dim row a live inspector shows when it has nothing to list. Its+/// empty `value` is what the stop keys read as "nothing selected".+fn watermark_item(label: &str) -> PickerItem {+ PickerItem {+ label: label.to_string(),+ value: String::new(),+ hint: None,+ checkbox: None,+ spans: Some(vec![Span::styled(+ label.to_string(),+ Style::new().fg(theme::muted()),+ )]),+ }+}++/// A live inspector row drawn from styled spans, whose plain text is what a+/// search matches against.+fn styled_item(value: String, spans: Vec<Span<'static>>) -> PickerItem {+ PickerItem {+ label: spans.iter().map(|s| s.content.as_ref()).collect(),+ value,+ hint: None,+ checkbox: None,+ spans: Some(spans),+ }+}++/// One row per detached background shell (`• <command> <elapsed>`), whose+/// `value` is the real pid so `x` can stop it. A single watermark row when+/// nothing is backgrounded. `shells` is `App::bg_shells` (already filtered to+/// backgrounded and refreshed off the render path), so this stays a pure+/// render function.fn background_shell_picker_items(shells: &[tauri_plugin_agent_tools::tools::proc::ShellInfo],) -> Vec<PickerItem> {if shells.is_empty() {- return vec![PickerItem {- label: "no background shells running".to_string(),- value: String::new(),- hint: None,- checkbox: None,- spans: None,- }];+ return vec![watermark_item("no background shells running")];}+ let accent = Style::new().fg(theme::accent());shells.iter()- .map(|s| PickerItem {- label: format!(- "{} {}",- format_elapsed(s.elapsed_secs),- single_line(&s.command)- ),- value: s.pid.to_string(),- hint: Some("running".to_string()),- checkbox: None,- spans: None,+ .map(|s| {+ styled_item(+ s.pid.to_string(),+ vec![+ Span::styled(TOOL_BULLET, accent),+ Span::styled(single_line(&s.command), accent),+ Span::styled(+ format!(" {}", format_elapsed(s.elapsed_secs)),+ Style::new().fg(theme::muted()),+ ),+ ],+ )+ })+ .collect()+}++/// Open the `/monitors` inspector. Rebuilt from `App::monitors` every frame+/// (see `draw`), so a monitor that matches or is stopped drops out in place.+fn open_monitors_picker(app: &mut App) {+ app.picker = Some(Picker {+ kind: PickerKind::Monitors,+ items: monitor_picker_items(&app.monitors),+ selected: 0,+ search: None,+ armed_delete: None,+ });+}++/// One row per running monitor (`◔ mon-1 build 12 polls <script>`), whose+/// `value` is the monitor id `x` stops; a watermark row when none are up.+fn monitor_picker_items(monitors: &[MonitorSnapshot]) -> Vec<PickerItem> {+ if monitors.is_empty() {+ return vec![watermark_item("no monitors running")];+ }+ let accent = Style::new().fg(theme::accent());+ let dim = Style::new().fg(theme::muted());+ monitors+ .iter()+ .map(|m| {+ styled_item(+ m.monitor_id.clone(),+ vec![+ Span::styled(format!("{MONITOR_GLYPH} "), accent),+ Span::styled(m.monitor_id.clone(), accent),+ Span::raw(format!(" {}", m.name)),+ Span::styled(format!(" {}", pluralize("poll", m.polls as usize)), dim),+ Span::styled(format!(" {}", single_line(&m.script)), dim),+ ],+ )}).collect()}@@ -18304,13 +18494,7 @@ fn agent_picker_items(spans.push(Span::raw(" "));}spans.extend(tail);- PickerItem {- label: spans.iter().map(|s| s.content.as_ref()).collect(),- value: p.run_id.clone(),- hint: None,- checkbox: None,- spans: Some(spans),- }+ styled_item(p.run_id.clone(), spans)};let mut items = Vec::new();for p in subagents.iter_mut() {@@ -20710,6 +20894,8 @@ fn draw(f: &mut Frame, app: &mut App) {// pulling content into view a row per frame.autoscroll_selection(app);let input_h = input_box_height(app, f.area().width);+ let shells_line = bg_shells_row(app, f.area().width);+ let shells_h = u16::from(shells_line.is_some());// Live state -- the plan and the running fan-out -- is docked, not woven// into the transcript: it describes *now*, and it is what the eye wants// right where the typing happens. Built at most once per frame, since its@@ -20720,7 +20906,7 @@ fn draw(f: &mut Frame, app: &mut App) {.is_none().then(|| {let width = f.area().width.max(1);- status_panel(app, width, panel_budget(f.area().height, input_h))+ status_panel(app, width, panel_budget(f.area().height, input_h + shells_h))}).filter(|l| !l.is_empty());let panel_h = panel_lines.as_ref().map_or(0, |l| l.len() as u16);@@ -20741,12 +20927,16 @@ fn draw(f: &mut Frame, app: &mut App) {Constraint::Length(TRANSCRIPT_BOTTOM_PAD), // 2: airConstraint::Length(panel_h), // 3: status panelConstraint::Length(1), // 4: separator rule- Constraint::Length(input_h), // 5: input- Constraint::Length(1), // 6: path + key hints+ Constraint::Length(shells_h), // 5: background shells+ Constraint::Length(input_h), // 6: input+ Constraint::Length(1), // 7: path + key hints]).split(f.area());let panel_area = raw[3];- let chunks = [raw[0], raw[1], raw[5], raw[6]];+ let chunks = [raw[0], raw[1], raw[6], raw[7]];+ if let Some(line) = shells_line {+ f.render_widget(Paragraph::new(line), raw[5]);+ }f.render_widget(header(app), chunks[0]);// Drawn for every path (picker included) so the dock always reads the same.@@ -20773,12 +20963,14 @@ fn draw(f: &mut Frame, app: &mut App) {// The /shells inspector is likewise live: rebuild from the refreshed// `bg_shells` each frame so a shell that finished or was just stopped drops// out. (`bg_shells` is refreshed off the render path; see the loop.)- if app.picker.as_ref().map(|p| p.kind) == Some(PickerKind::BackgroundShells) {- let items = background_shell_picker_items(&app.bg_shells);- if let Some(p) = app.picker.as_mut() {- p.selected = p.selected.min(items.len().saturating_sub(1));- p.items = items;- }+ let live_items = match app.picker.as_ref().map(|p| p.kind) {+ Some(PickerKind::BackgroundShells) => Some(background_shell_picker_items(&app.bg_shells)),+ Some(PickerKind::Monitors) => Some(monitor_picker_items(&app.monitors)),+ _ => None,+ };+ if let (Some(items), Some(p)) = (live_items, app.picker.as_mut()) {+ p.selected = p.selected.min(items.len().saturating_sub(1));+ p.items = items;}if let Some(kind) = app.picker.as_ref().map(|p| p.kind) {app.row_index.clear();@@ -22356,7 +22548,6 @@ fn agents_column(if panels.is_empty() || rows == 0 {return Vec::new();}- let dim = Style::new().fg(theme::muted());let max = width.max(8) as usize;let mut out = vec![Line::from(vec![Span::styled(AGENT_PANEL_GLYPH, Style::new().fg(theme::accent())),@@ -22431,25 +22622,29 @@ fn agents_column(if hidden > 0 {// The overflow row points at the `/agents` inspector, which lists the// whole live fan-out with per-agent detail -- the dock only has room for- // the newest few. Compact form when the (often half-width) column can't- // fit the hint.- let hinted = format!(" +{hidden} more · /agents");- if hinted.chars().count() <= max {- out.push(Line::from(vec![- Span::styled(format!(" +{hidden} more · "), dim),- Span::styled("/agents", Style::new().fg(theme::accent())),- ]));- } else {- out.push(Line::from(vec![Span::styled(- format!(" +{hidden} more running"),- dim,- )]));- }+ // the newest few.+ out.push(overflow_row(hidden, "/agents", "running", max));}out.truncate(rows);out}+/// A dock column's overflow row: `+N more · /command`, naming the inspector+/// that lists the rest, or the compact `+N more {fallback}` when the (often+/// half-width) column cannot fit the command.+fn overflow_row(hidden: usize, command: &str, fallback: &str, max: usize) -> Line<'static> {+ let dim = Style::new().fg(theme::muted());+ let lead = format!(" +{hidden} more · ");+ if lead.chars().count() + command.chars().count() <= max {+ Line::from(vec![+ Span::styled(lead, dim),+ Span::styled(command.to_string(), Style::new().fg(theme::accent())),+ ])+ } else {+ Line::from(Span::styled(format!(" +{hidden} more {fallback}"), dim))+ }+}+/// Token counts as `43K` / `1.1K` / `840` -- three significant characters, so a/// column of them stays the same width as the numbers grow.fn compact_tokens(n: u64) -> String {@@ -22984,7 +23179,7 @@ fn monitors_column(monitors: &[MonitorSnapshot], width: u16, rows: usize) -> Veclet dim = Style::new().fg(theme::muted());let max = width.max(8) as usize;let mut out = vec![Line::from(vec![- Span::styled("◔ ", Style::new().fg(theme::accent())),+ Span::styled(format!("{MONITOR_GLYPH} "), Style::new().fg(theme::accent())),Span::styled(pluralize("monitor", monitors.len()),Style::new().fg(theme::accent()).bold(),@@ -22999,12 +23194,15 @@ fn monitors_column(monitors: &[MonitorSnapshot], width: u16, rows: usize) -> Vecmonitors.len() - body.saturating_sub(1),)};- let detail = (body - shown).checked_div(shown).is_some_and(|n| n >= 1);+ // The overflow row takes its line first, or a script line would push it+ // past `rows` and the hidden monitors would go uncounted.+ let spare = (body - shown).saturating_sub(usize::from(hidden > 0));+ let detail = spare.checked_div(shown).is_some_and(|n| n >= 1);for monitor in monitors.iter().take(shown) {- let stats = format!(" {} polls", monitor.polls);+ let stats = format!(" {}", pluralize("poll", monitor.polls as usize));let label = format!("{} {}", monitor.monitor_id, monitor.name);out.push(Line::from(vec![- Span::styled("◔ ", Style::new().fg(theme::accent())),+ Span::styled(format!("{MONITOR_GLYPH} "), Style::new().fg(theme::accent())),Span::styled(truncate(&label, max.saturating_sub(2 + stats.len())),Style::new().fg(theme::accent()),@@ -23019,10 +23217,7 @@ fn monitors_column(monitors: &[MonitorSnapshot], width: u16, rows: usize) -> Vec}}if hidden > 0 {- out.push(Line::from(vec![Span::styled(- format!(" +{hidden} more watching"),- dim,- )]));+ out.push(overflow_row(hidden, "/monitors", "watching", max));}out.truncate(rows);out@@ -23031,6 +23226,50 @@ fn monitors_column(monitors: &[MonitorSnapshot], width: u16, rows: usize) -> Vec/// Max content rows the input box grows to before it scrolls internally.const MAX_INPUT_ROWS: u16 = 8;+/// `1 background shell running · /shells to view · /stop to close`: the dim+/// row over the input while the agent has commands running in the background,+/// cut to `width` (the commands go before the words, so a narrow terminal+/// still says how many are running).+///+/// `None` with nothing backgrounded, behind a picker, and while the working+/// row shows: a turn's input dock is one row tall by rule (see+/// `working_row`), so a shell backgrounded mid-turn must not make it jump.+/// The working row carries the count then instead (`bg_shells_count`).+fn bg_shells_row(app: &App, width: u16) -> Option<Line<'static>> {+ let n = app.bg_shells.len();+ if n == 0 || app.picker.is_some() || working_row_shown(app) {+ return None;+ }+ let dim = Style::new().fg(theme::muted());+ let accent = Style::new().fg(theme::accent());+ let spans = vec![+ Span::styled(format!(" {} running · ", pluralize("background shell", n)), dim),+ Span::styled("/shells", accent),+ Span::styled(" to view · ", dim),+ Span::styled("/stop", accent),+ Span::styled(" to close", dim),+ ];+ Some(Line::from(clip_spans(spans, width as usize)))+}++/// Whether the input row is the running turn's shimmering working row.+fn working_row_shown(app: &App) -> bool {+ app.status == Status::Running+ && app.input.is_empty()+ && app.message_queue.is_empty()+ && app.picker.is_none()+ && app.compacting.is_none()+ && app.run_compacting.is_none()+ && app.retrying.is_none()+}++/// The working row's ` · 1 bg shell` while background shells run, the+/// compact stand-in for `bg_shells_row`, which yields to the working row.+fn bg_shells_count(app: &App) -> Option<String> {+ let n = app.bg_shells.len();+ (n > 0).then(|| format!(" · {}", pluralize("bg shell", n)))+}+/// Rows the message box occupies: 1 content row for the idle/working/// placeholder, the parked row with its detail rows (`parked_rows`), or the/// wrapped input height clamped to `MAX_INPUT_ROWS` while editing, plus one@@ -23159,6 +23398,12 @@ fn working_row(app: &App, width: u16) -> Line<'static> {).remove(0).spans;+ if let Some(count) = bg_shells_count(app) {+ let room = (width as usize).saturating_sub(spans_width(&spans));+ if count.chars().count() <= room {+ spans.push(Span::styled(count, Style::new().fg(theme::muted())));+ }+ }push_fitting_hint(&mut spans, &WORKING_HINTS, width);Line::from(spans)}@@ -23499,19 +23744,8 @@ fn footer_spans(app: &App) -> Vec<Span<'static>> {// the other states.Status::Idle => vec![Span::raw(" ")],};- // Standing indicators lead the row in both states, newest concern leftmost:- // pending queue, then background shells still running unattended.- let bg_shells = app.bg_shells.len();- if bg_shells > 0 {- let plural = if bg_shells == 1 { "" } else { "s" };- spans.insert(- 0,- Span::styled(- format!("⚙ {bg_shells} bg shell{plural} · /shells "),- Style::new().magenta().bold(),- ),- );- }+ // A pending queue leads the row in every state. Background shells have+ // their own row over the input (`bg_shells_row`).let queue_count = app.message_queue.len();if queue_count > 0 {spans.insert(@@ -23578,8 +23812,9 @@ mod tests {SLASH_COMMANDS, SPINNER, SPINNER_ADVANCE_MS, THINKING_WORDS, WORKING_WORDS,};use super::{- agent_picker_items, agents_column, background_shell_picker_items, cache_summary_lines,- open_agents_picker, retry_wait_label, RetryWait, SubagentPanel, CHILD_RESULT_MAX,+ agent_picker_items, agents_column, background_shell_picker_items, bg_shells_row,+ cache_summary_lines, monitor_picker_items, monitors_column, open_agents_picker,+ open_background_shells_picker, open_monitors_picker, NoticeKind, retry_wait_label, RetryWait, SubagentPanel, CHILD_RESULT_MAX,CHILD_ROWS_MAX,};use crate::core::agent::events::{StreamEvent, Usage};@@ -24398,6 +24633,150 @@ mod tests {assert!(items[0].value.is_empty());}+ #[test]+ fn stop_and_monitors_commands_are_registered() {+ assert!(SLASH_COMMANDS.iter().any(|c| c.name == "/stop"));+ assert!(SLASH_COMMANDS.iter().any(|c| c.name == "/monitors"));+ }++ /// `/stop` stops every listed shell and says how many; a second `/stop`+ /// has nothing left to stop.+ #[tokio::test]+ async fn stop_command_stops_every_background_shell() {+ use tauri_plugin_agent_tools::tools::proc;+ // Pids no live process group can own, registered under a bucket of+ // their own so no other test's shells are listed or stopped.+ let thread = Some("stop-command-test");+ let pids = [i32::MAX as u32 - 7, i32::MAX as u32 - 8];+ let mut app = test_app();+ for pid in pids {+ proc::register(thread, pid, "sleep 9000");+ proc::mark_backgrounded(thread, pid);+ app.bg_shells.push(proc::ShellInfo {+ pid,+ command: "sleep 9000".into(),+ elapsed_secs: 1,+ backgrounded: true,+ });+ }+ run_command(&mut app, "stop", &no_mcp()).await;+ assert!(+ transcript_text(&app).contains("Stopping 2 background shells."),+ "{}",+ transcript_text(&app)+ );+ assert!(app.bg_shells.is_empty());+ assert!(proc::snapshot().iter().all(|s| !pids.contains(&s.pid)), "unregistered");+ run_command(&mut app, "stop", &no_mcp()).await;+ assert!(transcript_text(&app).contains("no background shells running"));+ }++ /// Every background headline, built by its real producer, maps to the row+ /// kind it is styled as: rewording a producer fails here rather than+ /// quietly turning a failure back into a dim note.+ #[test]+ fn notice_kinds_classify_the_real_headlines() {+ use crate::core::agent::bg_shell::{headline, still_running_headline};+ use tauri_plugin_agent_tools::tools::monitor::headline as monitor_headline;+ use tauri_plugin_agent_tools::tools::ShellDone;+ let done = |failed| ShellDone {+ id: 1,+ command: "make build".into(),+ elapsed_secs: 12,+ output_path: None,+ failed,+ };+ let cases = [+ (monitor_headline("mon-1", "build", true), NoticeKind::MonitorMatched),+ (monitor_headline("mon-1", "build", false), NoticeKind::MonitorTimedOut),+ // A name that itself ends in an outcome word must not fool it.+ (+ monitor_headline("mon-2", "wait until matched", false),+ NoticeKind::MonitorTimedOut,+ ),+ (headline(&done(false)), NoticeKind::ShellFinished),+ (headline(&done(true)), NoticeKind::ShellFailed),+ (still_running_headline("npm run dev"), NoticeKind::Other),+ ("hook 'fmt' failed: exit 2".to_string(), NoticeKind::Other),+ ];+ for (text, kind) in cases {+ assert_eq!(NoticeKind::of(&text), kind, "{text}");+ }+ }++ /// Each kind lands as its own row: a match under the monitor glyph, a+ /// timeout and a failure as warnings, a finished command as a tool row.+ #[test]+ fn background_notices_render_by_kind() {+ let mut app = test_app();+ app.submit_user("go".into());+ for text in [+ "Monitor mon-1: 'build' matched",+ "Monitor mon-2: 'boot' timed out",+ "Background command failed after 3s: make test",+ "Background command finished after 12s: make build",+ ] {+ app.apply(StreamEvent::Notice { text: text.into() });+ }+ let rows = render_rows(&mut app, 80, 20);+ let has = |needle: &str| rows.iter().any(|r| r.contains(needle));+ assert!(has("◔ Monitor mon-1: 'build' matched"), "{rows:#?}");+ assert!(has("⚠ Monitor mon-2: 'boot' timed out"), "{rows:#?}");+ assert!(has("⚠ Background command failed after 3s"), "{rows:#?}");+ assert!(has("• Background command finished after 12s"), "{rows:#?}");+ }++ /// `x` in `/monitors` stops the selected monitor through the monitor set.+ #[tokio::test]+ async fn monitors_picker_stops_the_selected_monitor() {+ let root = tempfile::tempdir().unwrap();+ let mut app = test_app();+ app.monitor_set+ .start(+ session_monitor_spec("false"),+ session_monitor_ctx(root.path()),+ )+ .unwrap();+ app.monitors = app.monitor_set.snapshot();+ run_command(&mut app, "monitors", &no_mcp()).await;+ let picker = app.picker.as_ref().expect("picker open");+ assert_eq!(picker.kind, PickerKind::Monitors);+ assert!(picker.items[0].label.contains("mon-1"), "{}", picker.items[0].label);+ press(&mut app, KeyCode::Char('x'), KeyModifiers::NONE).await;+ assert!(app.monitors.is_empty(), "stopped and undocked");+ assert!(+ transcript_text(&app).contains("Monitor mon-1"),+ "{}",+ transcript_text(&app)+ );+ let items = monitor_picker_items(&app.monitors);+ assert_eq!(items[0].label, "no monitors running");+ assert!(items[0].value.is_empty());+ }++ /// The shells row sits over an idle input, but a running turn keeps its+ /// one-row dock and folds the count into the working row instead.+ #[test]+ fn bg_shells_row_yields_to_the_working_row() {+ let mut app = test_app();+ app.bg_shells.push(tauri_plugin_agent_tools::tools::proc::ShellInfo {+ pid: 1,+ command: "sleep 9000".into(),+ elapsed_secs: 1,+ backgrounded: true,+ });+ let idle = render_rows(&mut app, 80, 12);+ assert!(+ idle.iter().any(|r| r.contains("1 background shell running · /shells to view · /stop to close")),+ "{idle:#?}"+ );+ app.submit_user("go".into());+ let running = render_rows(&mut app, 80, 12);+ assert!(!running.iter().any(|r| r.contains("background shell running")), "{running:#?}");+ assert!(running.iter().any(|r| r.contains("· 1 bg shell")), "{running:#?}");+ assert!(bg_shells_row(&app, 80).is_none());+ }+fn steering_request(messages: Vec<serde_json::Value>,) -> (diff --git a/src-tauri/src/core/cli/tui/tests/snapshot.rs b/src-tauri/src/core/cli/tui/tests/snapshot.rsindex 9f620426b..e6a9cbfdd 100644--- a/src-tauri/src/core/cli/tui/tests/snapshot.rs+++ b/src-tauri/src/core/cli/tui/tests/snapshot.rs@@ -512,6 +512,76 @@ fn agents_inspector_lists_the_fan_out() {assert_buffer("agents_inspector_list", &frame(&mut app, DARK, 90, 16));}+// ---- Monitors and background shells ----------------------------------------++fn shell(pid: u32, command: &str, elapsed_secs: u64) -> tauri_plugin_agent_tools::tools::proc::ShellInfo {+ tauri_plugin_agent_tools::tools::proc::ShellInfo {+ pid,+ command: command.into(),+ elapsed_secs,+ backgrounded: true,+ }+}++#[test]+fn monitors_inspector_lists_the_monitors() {+ let mut app = snapshot_app();+ app.monitors = vec![+ monitor("mon-1", "build", "grep -m1 OK build.log", 12),+ monitor("mon-2", "boot", "curl -sf localhost:1337/health", 1),+ ];+ open_monitors_picker(&mut app);+ assert_buffer("monitors_inspector_list", &frame(&mut app, DARK, 80, 10));+}++#[test]+fn monitors_inspector_empty() {+ let mut app = snapshot_app();+ open_monitors_picker(&mut app);+ assert_buffer("monitors_inspector_empty", &frame(&mut app, DARK, 60, 9));+}++#[test]+fn monitor_dock_overflow_points_at_monitors() {+ let monitors: Vec<MonitorSnapshot> = (1..=4)+ .map(|n| monitor(&format!("mon-{n}"), "build", "grep OK build.log", n))+ .collect();+ let buf = pinned(DARK, || lines_buffer(monitors_column(&monitors, 40, 3), 40));+ assert_buffer("monitor_dock_overflow", &buf);+}++#[test]+fn shells_inspector_lists_the_shells() {+ let mut app = snapshot_app();+ app.bg_shells = vec![shell(4242, "npm run dev", 65), shell(4343, "cargo build --release", 7)];+ open_background_shells_picker(&mut app);+ assert_buffer("shells_inspector_list", &frame(&mut app, DARK, 80, 10));+}++/// The idle dock with two background shells: the dim row over the input.+#[test]+fn bg_shells_row_over_the_input() {+ let mut app = snapshot_app();+ app.bg_shells = vec![shell(4242, "npm run dev", 65), shell(4343, "tail -f app.log", 7)];+ assert_buffer("bg_shells_row", &frame(&mut app, DARK, 80, 8));+}++/// Background pings, each as the row its kind calls for.+#[test]+fn transcript_background_notices() {+ let mut app = snapshot_app();+ start_turn(&mut app, "watch the build");+ for text in [+ "Monitor mon-1: 'build' matched",+ "Monitor mon-2: 'boot' timed out",+ "Background command failed after 3s: make test",+ "Background command finished after 12s: make build",+ ] {+ app.apply(StreamEvent::Notice { text: text.into() });+ }+ assert_buffer("transcript_background_notices", &body(&mut app));+}+/// Feed `event` to the running child `kv-review`, as its stream would.fn child(app: &mut App, event: StreamEvent) {app.apply(StreamEvent::Subagent {diff --git a/src-tauri/src/core/cli/tui/tests/snapshots/bg_shells_row.snap b/src-tauri/src/core/cli/tui/tests/snapshots/bg_shells_row.snapnew file mode 100644index 000000000..128262823--- /dev/null+++ b/src-tauri/src/core/cli/tui/tests/snapshots/bg_shells_row.snap@@ -0,0 +1,27 @@+---+source: src/core/cli/tui/tests/snapshot.rs+expression: render_snapshot(buf)+---+| 👋 gpt-5-mini effort medium ctx 0K/128K fallback ● [ready] |+|────────────────────────────────────────────────────────────────────────────────|+| |+|────────────────────────────────────────────────────────────────────────────────|+| 2 background shells running · /shells to view · /stop to close |+|> Type here to chat with agent |+| |+|📂 /snapshot/repo |++ 0 0..2 fg=Yellow+ 0 3..14 mod=BOLD+ 0 14..29 fg=#E5C07B+ 0 55..64 fg=#98C379+ 4 0..31 fg=#808080+ 4 31..38 fg=#56B6C2+ 4 38..49 fg=#808080+ 4 49..54 fg=#56B6C2+ 4 54..63 fg=#808080+ 5 0..2 fg=#56B6C2 mod=BOLD+ 5 2..3 mod=REVERSED+ 5 4..32 mod=DIM | ITALIC+ 7 0..1 fg=#808080+ 7 2..17 fg=#808080diff --git a/src-tauri/src/core/cli/tui/tests/snapshots/monitor_dock_overflow.snap b/src-tauri/src/core/cli/tui/tests/snapshots/monitor_dock_overflow.snapnew file mode 100644index 000000000..70e0fbaef--- /dev/null+++ b/src-tauri/src/core/cli/tui/tests/snapshots/monitor_dock_overflow.snap@@ -0,0 +1,14 @@+---+source: src/core/cli/tui/tests/snapshot.rs+expression: render_snapshot(buf)+---+|◔ 4 monitors |+|◔ mon-1 build 1 poll |+| +3 more · /monitors |++ 0 0..2 fg=#56B6C2+ 0 2..12 fg=#56B6C2 mod=BOLD+ 1 0..13 fg=#56B6C2+ 1 13..21 fg=#808080+ 2 0..12 fg=#808080+ 2 12..21 fg=#56B6C2diff --git a/src-tauri/src/core/cli/tui/tests/snapshots/monitors_inspector_empty.snap b/src-tauri/src/core/cli/tui/tests/snapshots/monitors_inspector_empty.snapnew file mode 100644index 000000000..4c323cc1d--- /dev/null+++ b/src-tauri/src/core/cli/tui/tests/snapshots/monitors_inspector_empty.snap@@ -0,0 +1,27 @@+---+source: src/core/cli/tui/tests/snapshot.rs+expression: render_snapshot(buf)+---+| 👋 gpt-5-mini effort medium ctx 0K/128K fallback ● [re|+|╭ monitors ────────────────────────────────────────────────╮|+|│ ▶ no monitors running │|+|╰──────────────────────────────────────────────────────────╯|+| |+|────────────────────────────────────────────────────────────|+|selecting… |+| |+|📂 /snapshot/repo ↑/↓ select x stop Esc close|++ 0 0..2 fg=Yellow+ 0 3..14 mod=BOLD+ 0 14..29 fg=#E5C07B+ 0 55..60 fg=#98C379+ 1 0..60 fg=#56B6C2+ 2 0..1 fg=#56B6C2+ 2 2..58 fg=Cyan mod=BOLD+ 2 59..60 fg=#56B6C2+ 3 0..60 fg=#56B6C2+ 6 0..10 mod=DIM | ITALIC+ 8 0..1 fg=#808080+ 8 2..17 fg=#808080+ 8 28..60 mod=DIMdiff --git a/src-tauri/src/core/cli/tui/tests/snapshots/monitors_inspector_list.snap b/src-tauri/src/core/cli/tui/tests/snapshots/monitors_inspector_list.snapnew file mode 100644index 000000000..aff5c8626--- /dev/null+++ b/src-tauri/src/core/cli/tui/tests/snapshots/monitors_inspector_list.snap@@ -0,0 +1,32 @@+---+source: src/core/cli/tui/tests/snapshot.rs+expression: render_snapshot(buf)+---+| 👋 gpt-5-mini effort medium ctx 0K/128K fallback ● [ready] |+|╭ monitors ────────────────────────────────────────────────────────────────────╮|+|│ ▶ ◔ mon-1 build 12 polls grep -m1 OK build.log │|+|│ ◔ mon-2 boot 1 poll curl -sf localhost:1337/health │|+|╰──────────────────────────────────────────────────────────────────────────────╯|+| |+|────────────────────────────────────────────────────────────────────────────────|+|selecting… |+| |+|📂 /snapshot/repo ↑/↓ select x stop Esc close|++ 0 0..2 fg=Yellow+ 0 3..14 mod=BOLD+ 0 14..29 fg=#E5C07B+ 0 55..64 fg=#98C379+ 1 0..80 fg=#56B6C2+ 2 0..1 fg=#56B6C2+ 2 2..78 fg=Cyan mod=BOLD+ 2 79..80 fg=#56B6C2+ 3 0..1 fg=#56B6C2+ 3 4..11 fg=#56B6C2+ 3 17..57 fg=#808080+ 3 79..80 fg=#56B6C2+ 4 0..80 fg=#56B6C2+ 7 0..10 mod=DIM | ITALIC+ 9 0..1 fg=#808080+ 9 2..17 fg=#808080+ 9 48..80 mod=DIMdiff --git a/src-tauri/src/core/cli/tui/tests/snapshots/shells_inspector_list.snap b/src-tauri/src/core/cli/tui/tests/snapshots/shells_inspector_list.snapnew file mode 100644index 000000000..0af02bb36--- /dev/null+++ b/src-tauri/src/core/cli/tui/tests/snapshots/shells_inspector_list.snap@@ -0,0 +1,32 @@+---+source: src/core/cli/tui/tests/snapshot.rs+expression: render_snapshot(buf)+---+| 👋 gpt-5-mini effort medium ctx 0K/128K fallback ● [ready] |+|╭ background shells ───────────────────────────────────────────────────────────╮|+|│ ▶ • npm run dev 1m 05s │|+|│ • cargo build --release 7s │|+|╰──────────────────────────────────────────────────────────────────────────────╯|+| |+|────────────────────────────────────────────────────────────────────────────────|+|selecting… |+| |+|📂 /snapshot/repo ↑/↓ select x stop Esc close|++ 0 0..2 fg=Yellow+ 0 3..14 mod=BOLD+ 0 14..29 fg=#E5C07B+ 0 55..64 fg=#98C379+ 1 0..80 fg=#56B6C2+ 2 0..1 fg=#56B6C2+ 2 2..78 fg=Cyan mod=BOLD+ 2 79..80 fg=#56B6C2+ 3 0..1 fg=#56B6C2+ 3 4..27 fg=#56B6C2+ 3 27..31 fg=#808080+ 3 79..80 fg=#56B6C2+ 4 0..80 fg=#56B6C2+ 7 0..10 mod=DIM | ITALIC+ 9 0..1 fg=#808080+ 9 2..17 fg=#808080+ 9 48..80 mod=DIMdiff --git a/src-tauri/src/core/cli/tui/tests/snapshots/transcript_background_notices.snap b/src-tauri/src/core/cli/tui/tests/snapshots/transcript_background_notices.snapnew file mode 100644index 000000000..12c518db3--- /dev/null+++ b/src-tauri/src/core/cli/tui/tests/snapshots/transcript_background_notices.snap@@ -0,0 +1,24 @@+---+source: src/core/cli/tui/tests/snapshot.rs+expression: render_snapshot(buf)+---+| |+|› watch the build |+| |+| |+|◔ Monitor mon-1: 'build' matched |+|⚠ Monitor mon-2: 'boot' timed out |+|⚠ Background command failed after 3s: make test |+| |+|• Background command finished after 12s: make build |++ 0 0..80 bg=#282C3A+ 1 0..2 fg=#56B6C2 bg=#282C3A mod=BOLD+ 1 2..17 fg=#E9ECF2 bg=#282C3A mod=BOLD+ 1 17..80 bg=#282C3A+ 2 0..80 bg=#282C3A+ 4 0..2 fg=#56B6C2+ 5 0..33 fg=#E5C07B+ 6 0..47 fg=#E5C07B+ 8 0..2 fg=#98C379 mod=BOLD+ 8 2..12 mod=BOLD