mod client; mod commands; mod daemon_ctrl; mod error; mod machine_target; mod output; mod phone; #[cfg(windows)] mod phone_windows; mod service; mod signal_helper; mod skill; use clap::{Parser, Subcommand}; #[derive(Parser)] #[command(name = "pu", about = "PurePoint workspace orchestrator")] struct Cli { /// Route a command to this project; overrides PU_PROJECT_ROOT and cwd #[arg(long, global = true, value_name = "ROOT")] project: Option, /// Connect to a specific daemon socket #[arg(long, global = true, value_name = "PATH")] socket: Option, /// Run on a registered machine's daemon over SSH (see `pu machine`) #[arg(long, global = true, value_name = "NAME", conflicts_with = "socket")] machine: Option, #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// Read and post to the shared project channel (does not wake agents) Channel { #[arg(long, global = true)] project_root: Option, #[command(subcommand)] action: commands::channel::ChannelAction, }, /// Initialize a PurePoint workspace Init { /// Output as JSON #[arg(long)] json: bool, }, /// Spawn an agent in a new worktree Spawn { /// The prompt for the agent (optional if --template or --file provided) prompt: Option, /// Agent type (default: claude) #[arg(short, long)] agent: Option, /// Worktree name #[arg(short, long)] name: Option, /// Base branch #[arg(short, long)] base: Option, /// Spawn in project root (no worktree) #[arg(long, conflicts_with = "worktree")] root: bool, /// Add to existing worktree #[arg(short, long)] worktree: Option, /// Use a saved prompt template by name #[arg(long, conflicts_with = "file")] template: Option, /// Read prompt from a file path #[arg(long, conflicts_with = "template")] file: Option, /// Command to run in the terminal (for terminal agents) #[arg(long)] command: Option, /// Variable substitution (KEY=VALUE), repeatable #[arg(long = "var", value_name = "KEY=VALUE")] vars: Vec, /// Launch the bare agent command, ignoring configured launch args (project and machine). --agent-args still apply #[arg(long)] no_auto: bool, /// Extra CLI flags passed directly to the agent (space-separated) #[arg(long)] agent_args: Option, /// Launch agent in plan/architect mode (read-only research) #[arg(long)] plan: bool, /// Disable event triggers for this agent #[arg(long)] no_trigger: bool, /// Bind an idle trigger to this agent (name of trigger in .pu/triggers/) #[arg(long, conflicts_with = "no_trigger")] trigger: Option, /// Output as JSON #[arg(long)] json: bool, }, /// Show workspace status Status { /// Summarize every registered project, independent of cwd #[arg(long, conflicts_with_all = ["agent", "project"])] global: bool, /// Show single agent status #[arg(long)] agent: Option, /// Output as JSON #[arg(long)] json: bool, }, /// List, add or remove the projects the daemon serves Projects { #[command(subcommand)] action: ProjectsAction, }, /// List agent instances (saved definitions use `pu agent`) Agents { #[command(subcommand)] action: InventoryAction, }, /// List worktrees known to the daemon Worktrees { #[command(subcommand)] action: InventoryAction, }, /// Bench (suspend) agents — pull them off the court #[command(long_about = "Bench (suspend) agents — pull them off the court.\n\n\ When using --all, the invoking agent may also be suspended.\n\ Use `pu play ` to resume a benched agent.")] Bench { /// Agent ID to bench #[arg(conflicts_with = "all")] agent_id: Option, /// Bench all active agents #[arg(long)] all: bool, /// Output as JSON #[arg(long)] json: bool, }, /// Put a benched agent back in play (resume) Play { /// Agent ID to resume agent_id: String, /// Output as JSON #[arg(long)] json: bool, }, /// Kill agents Kill { /// Kill specific agent #[arg(long, conflicts_with_all = ["worktree", "all"])] agent: Option, /// Kill all agents in worktree #[arg(short, long, conflicts_with = "all")] worktree: Option, /// Kill all agents #[arg(long)] all: bool, /// Also kill root-level agents (point guards). By default --all only kills worktree agents. #[arg(long, requires = "all")] include_root: bool, /// Output as JSON #[arg(long)] json: bool, }, /// Attach to an agent's terminal Attach { /// Agent ID agent_id: String, }, /// Read an agent's native conversation (or rendered terminal text) Read { agent_id: String, /// Maximum records; initial read returns the latest records #[arg(long, default_value = "50")] limit: usize, /// Continue forward from a cursor returned by --json #[arg(long)] since: Option, /// Bind an existing agent to its exact native session ID #[arg(long = "session")] session_id: Option, #[arg(long)] json: bool, }, /// Deprecated: raw terminal bytes; use read for conversations Logs { /// Agent ID agent_id: String, /// Number of bytes to read from tail #[arg(long, default_value = "500")] tail: usize, /// Output as JSON #[arg(long)] json: bool, }, /// Start, stop or supervise the daemon (systemd/launchd user service) Daemon { #[command(subcommand)] action: commands::daemon::DaemonAction, }, /// Pair phones directly with this Linux or Windows host and manage them Phone { #[command(subcommand)] action: commands::phone::PhoneAction, }, /// Check daemon health Health { /// Output as JSON #[arg(long)] json: bool, }, /// Manage saved prompt templates Prompt { #[command(subcommand)] action: PromptAction, }, /// Manage saved agent definitions Agent { #[command(subcommand)] action: AgentAction, }, /// Manage swarm compositions Swarm { #[command(subcommand)] action: SwarmAction, }, /// Send text or keys to an agent's terminal Send { /// Agent ID agent_id: String, /// Text to send text: Option, /// Don't append Enter after text #[arg(long)] no_enter: bool, /// Send a control key sequence (e.g., C-c, C-d) #[arg(long, conflicts_with = "text")] keys: Option, /// Output as JSON #[arg(long)] json: bool, }, /// Control the pane grid layout Grid { /// Workspace to act on (an id from `pu grid show`; default: the one on screen). /// Leaf and tab ids are only unique within a workspace. #[arg(long, global = true)] workspace: Option, #[command(subcommand)] action: GridAction, }, /// Manage scheduled tasks Schedule { #[command(subcommand)] action: ScheduleAction, }, /// Manage event-driven triggers Trigger { #[command(subcommand)] action: TriggerAction, }, /// Evaluate git hook gates Gate { /// Event type: pre-commit or pre-push event: String, /// Project root path #[arg(long)] project_root: Option, }, /// Workspace pulse — agents, runtimes, and git stats at a glance Pulse { /// Output as JSON #[arg(long)] json: bool, }, /// Show git diffs across agent worktrees Diff { /// Diff a specific worktree #[arg(long)] worktree: Option, /// Show file summary instead of full diff #[arg(long)] stat: bool, /// Output as JSON #[arg(long)] json: bool, }, /// Live dashboard showing all agents in real-time Watch { /// Refresh interval in milliseconds (default: 800) #[arg(long)] interval: Option, }, /// Check a machine (`--machine `) for settings that drop connections; changes nothing Doctor { /// Output as JSON #[arg(long)] json: bool, }, /// Manage machines reachable over SSH (use with --machine) Machine { #[command(subcommand)] action: commands::machine::MachineAction, }, /// Remove worktrees, their agents, and branches Clean { /// Remove a specific worktree #[arg(long, conflicts_with = "all")] worktree: Option, /// Remove all worktrees #[arg(long)] all: bool, /// Output as JSON #[arg(long)] json: bool, }, } #[derive(Subcommand)] enum PromptAction { /// List available prompt templates List { /// Output as JSON #[arg(long)] json: bool, }, /// Show a prompt template Show { /// Template name name: String, /// Output as JSON #[arg(long)] json: bool, }, /// Create a prompt template Create { /// Template name name: String, /// Template body #[arg(long)] body: String, /// Description #[arg(long, default_value = "")] description: String, /// Agent type #[arg(long, default_value = "claude")] agent: String, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, /// Delete a prompt template Delete { /// Template name name: String, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, } #[derive(Subcommand)] enum AgentAction { /// List agent definitions List { /// Output as JSON #[arg(long)] json: bool, }, /// Create an agent definition Create { /// Agent definition name name: String, /// Agent type #[arg(long, default_value = "claude")] agent_type: String, /// Prompt template name to use #[arg(long)] template: Option, /// Inline prompt text #[arg(long)] inline_prompt: Option, /// Command to run (for terminal agents) #[arg(long)] command: Option, /// Comma-separated tags #[arg(long, default_value = "")] tags: String, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, /// Show an agent definition Show { /// Agent definition name name: String, /// Output as JSON #[arg(long)] json: bool, }, /// Delete an agent definition Delete { /// Agent definition name name: String, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, } #[derive(Subcommand)] enum SwarmAction { /// List swarm definitions List { /// Output as JSON #[arg(long)] json: bool, }, /// Create a swarm definition Create { /// Swarm name name: String, /// Number of worktrees #[arg(long, default_value = "1")] worktrees: u32, /// Worktree template name #[arg(long, default_value = "")] worktree_template: String, /// Roster entry: "agent_def:role:quantity" (repeatable) #[arg(long = "roster", value_name = "AGENT:ROLE:QTY")] roster: Vec, /// Include terminal in swarm #[arg(long)] include_terminal: bool, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, /// Show a swarm definition Show { /// Swarm name name: String, /// Output as JSON #[arg(long)] json: bool, }, /// Delete a swarm definition Delete { /// Swarm name name: String, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, /// Run a swarm Run { /// Swarm name name: String, /// Variable substitution (KEY=VALUE), repeatable #[arg(long = "var", value_name = "KEY=VALUE")] vars: Vec, /// Output as JSON #[arg(long)] json: bool, }, } #[derive(Subcommand)] enum GridAction { /// Show current grid layout Show { /// Output as JSON #[arg(long)] json: bool, }, /// Split a pane Split { /// Axis: v (vertical/left-right) or h (horizontal/top-bottom) #[arg(long, default_value = "v")] axis: String, /// Leaf ID to split (default: focused pane) #[arg(long)] leaf: Option, }, /// Close a pane Close { /// Leaf ID to close (default: focused pane) #[arg(long)] leaf: Option, }, /// Move focus to another pane Focus { /// Direction: up, down, left, right #[arg(long)] direction: Option, /// Focus specific leaf ID #[arg(long)] leaf: Option, }, /// Show an agent in a pane's active tab Assign { /// Agent ID agent_id: String, /// Leaf ID (default: focused pane) #[arg(long)] leaf: Option, }, /// Manage the tabs inside a pane Tab { #[command(subcommand)] action: TabAction, }, } #[derive(Subcommand)] enum TabAction { /// Open a tab after the pane's active tab (empty unless --agent is given) New { /// Leaf ID (default: focused pane) #[arg(long)] leaf: Option, /// Agent ID to show in the new tab #[arg(long)] agent: Option, }, /// Select a tab by 1-based position, or the next/previous tab #[command(group( clap::ArgGroup::new("target") .required(true) .args(["index", "next", "prev"]) ))] Select { /// 1-based tab position in the pane #[arg(value_parser = clap::value_parser!(u32).range(1..))] index: Option, /// Select the next tab #[arg(long)] next: bool, /// Select the previous tab #[arg(long)] prev: bool, /// Leaf ID (default: focused pane) #[arg(long)] leaf: Option, }, /// Close a tab (default: the pane's active tab) Close { /// Leaf ID (default: focused pane) #[arg(long)] leaf: Option, /// Tab ID to close #[arg(long)] tab: Option, }, /// Move a tab to another pane Move { /// Tab ID to move tab_id: u32, /// Destination leaf ID #[arg(long)] to: u32, /// 1-based destination position (default: append) #[arg(long, value_parser = clap::value_parser!(u32).range(1..))] index: Option, }, /// Move a tab into a new pane split off its current pane Break { /// Tab ID (default: the focused pane's active tab) #[arg(long)] tab: Option, /// Axis: v (vertical/left-right) or h (horizontal/top-bottom) #[arg(long, default_value = "v", value_parser = ["v", "h"])] axis: String, }, } #[derive(Subcommand)] enum InventoryAction { /// List across all registered projects by default; --project narrows the scope List { #[arg(long, conflicts_with = "project")] global: bool, /// Filter agent instances by observed state (agents list only) #[arg(long, value_parser = ["running", "suspended", "broken", "unknown"])] state: Option, #[arg(long)] json: bool, }, } #[derive(Subcommand)] enum ProjectsAction { /// List all registered projects by default; --project narrows the scope List { #[arg(long, conflicts_with = "project")] global: bool, #[arg(long)] json: bool, }, /// Add a folder as a project, initializing it if needed (same as `pu init`) Add { /// Project folder (default: --project, then the current directory) path: Option, #[arg(long)] json: bool, }, /// Remove a project from this daemon on every device; its files are kept Remove { /// Project folder (default: --project, then the current directory) path: Option, /// Suspend the project's running agents instead of refusing #[arg(long)] suspend_agents: bool, #[arg(long)] json: bool, }, } #[derive(Subcommand)] enum ScheduleAction { /// List schedules List { /// Output as JSON #[arg(long)] json: bool, }, /// Create a schedule Create { /// Schedule name name: String, /// Recurrence: none, hourly, daily, weekdays, weekly, monthly #[arg(long, default_value = "none")] recurrence: String, /// Start time (RFC 3339 or YYYY-MM-DDTHH:MM:SS) #[arg(long)] start_at: String, /// Trigger type: agent-def, swarm-def, inline-prompt #[arg(long = "trigger")] trigger_type: String, /// Trigger name (for agent-def or swarm-def triggers) #[arg(long)] trigger_name: Option, /// Trigger prompt (for inline-prompt trigger) #[arg(long)] trigger_prompt: Option, /// Agent type for inline-prompt trigger #[arg(long, default_value = "claude")] agent: String, /// Variable substitution (KEY=VALUE), repeatable #[arg(long = "var", value_name = "KEY=VALUE")] vars: Vec, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Spawn as root agent (in project root, not a worktree) #[arg(long)] root: bool, /// Worktree/branch name (required when not --root) #[arg(long = "name")] agent_name: Option, /// Output as JSON #[arg(long)] json: bool, }, /// Show a schedule Show { /// Schedule name name: String, /// Output as JSON #[arg(long)] json: bool, }, /// Delete a schedule Delete { /// Schedule name name: String, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, /// Enable a schedule Enable { /// Schedule name name: String, /// Output as JSON #[arg(long)] json: bool, }, /// Disable a schedule Disable { /// Schedule name name: String, /// Output as JSON #[arg(long)] json: bool, }, } #[derive(Subcommand)] enum TriggerAction { /// List triggers List { /// Output as JSON #[arg(long)] json: bool, }, /// Show a trigger Show { /// Trigger name name: String, /// Output as JSON #[arg(long)] json: bool, }, /// Create a trigger Create { /// Trigger name name: String, /// Event type: agent_idle, pre_commit, pre_push #[arg(long = "on")] event: String, /// Description #[arg(long)] description: Option, /// Inject text (repeatable, creates sequence steps) #[arg(long)] inject: Vec, /// Gate command (repeatable, creates gate-only sequence steps) #[arg(long)] gate: Vec, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, /// Delete a trigger Delete { /// Trigger name name: String, /// Scope: local or global #[arg(long, default_value = "local")] scope: String, /// Output as JSON #[arg(long)] json: bool, }, /// Assign a trigger to an idle agent Assign { /// Agent ID agent_id: String, /// Trigger name trigger_name: String, /// Output as JSON #[arg(long)] json: bool, }, } /// Let legacy conhost render the ANSI colours `pu` prints. Windows Terminal /// already does; where the mode cannot be set (output is not a console, e.g. /// a pipe or file) nothing changes, as with piped output on Unix. #[cfg(windows)] fn enable_virtual_terminal() { use windows_sys::Win32::System::Console::{ CONSOLE_MODE, ENABLE_VIRTUAL_TERMINAL_PROCESSING, GetConsoleMode, GetStdHandle, STD_ERROR_HANDLE, STD_OUTPUT_HANDLE, SetConsoleMode, }; for id in [STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] { // SAFETY: GetStdHandle has no preconditions; the Get/SetConsoleMode // calls fail harmlessly on a handle that is not a console. unsafe { let handle = GetStdHandle(id); let mut mode: CONSOLE_MODE = 0; if GetConsoleMode(handle, &mut mode) != 0 { SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); } } } } fn main() { #[cfg(windows)] enable_virtual_terminal(); if std::env::args_os() .nth(1) .is_some_and(|arg| arg == "__agent-signal") { signal_helper::run(); return; } tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap() .block_on(async_main()); } async fn async_main() { let cli = Cli::parse(); if let Some(root) = &cli.project { commands::set_project_override(root.clone()); } // `pu doctor --machine` checks the ssh path itself, so it needs no daemon // endpoint: it must run (and explain) even when the forward cannot open. if let Commands::Doctor { json } = cli.command { if let Err(e) = commands::doctor::run(cli.machine.as_deref(), json).await { eprintln!("error: {e}"); std::process::exit(1); } return; } let custom_socket = cli.socket.is_some() || cli.machine.is_some(); // Held until the command finishes: dropping it closes a private ssh forward. let mut endpoint = None; let socket = if let Some(name) = cli.machine.as_deref() { match connect_machine(name, &cli.command).await { Ok(connected) => { #[cfg(unix)] if matches!(connected, machine_target::Endpoint::Private(_)) { machine_target::close_forwards_on_signal(); } let socket = connected.socket().to_path_buf(); endpoint = Some(connected); socket } Err(e) => { eprintln!("error: {e}"); std::process::exit(1); } } } else { match cli .socket .map(Ok) .unwrap_or_else(pu_core::paths::daemon_socket_path) { Ok(p) => p, Err(e) => { eprintln!("error: {e}"); std::process::exit(1); } } }; // The proxy's stdout is a daemon byte stream and its stdin read never // returns on its own; skip side work and exit as soon as it finishes. let proxy = matches!( cli.command, Commands::Daemon { action: commands::daemon::DaemonAction::Proxy { .. } } ); // Background plugin freshness check (non-blocking) if !custom_socket && !proxy { std::thread::spawn(skill::ensure_plugin_current); } let result = match cli.command { Commands::Init { json } => commands::init::run(&socket, json).await, Commands::Spawn { prompt, agent, name, base, root, worktree, template, file, command, vars, no_auto, agent_args, plan, no_trigger, trigger, json, } => { commands::spawn::run( &socket, prompt, agent, name, base, root, worktree, template, file, command, vars, no_auto, agent_args, plan, no_trigger, trigger, json, ) .await } Commands::Bench { agent_id, all, json, } => commands::bench::run_bench(&socket, agent_id, all, json).await, Commands::Play { agent_id, json } => { commands::bench::run_play(&socket, &agent_id, json).await } Commands::Status { agent, global, json, } => { if global { commands::inventory::run( &socket, pu_core::protocol::InventoryKind::Summary, None, json, ) .await } else { commands::status::run(&socket, agent, json).await } } Commands::Projects { action } => commands::projects::run(&socket, action).await, Commands::Agents { action } => { commands::inventory::run_list(&socket, pu_core::protocol::InventoryKind::Agents, action) .await } Commands::Worktrees { action } => { commands::inventory::run_list( &socket, pu_core::protocol::InventoryKind::Worktrees, action, ) .await } Commands::Kill { agent, worktree, all, include_root, json, } => commands::kill::run(&socket, agent, worktree, all, include_root, json).await, Commands::Attach { agent_id } => commands::attach::run(&socket, &agent_id).await, Commands::Read { agent_id, limit, since, session_id, json, } => commands::read::run(&socket, &agent_id, limit, since, session_id, json).await, Commands::Logs { agent_id, tail, json, } => commands::logs::run(&socket, &agent_id, tail, json).await, Commands::Health { json } => commands::health::run(&socket, json).await, Commands::Daemon { action } => commands::daemon::run(&socket, custom_socket, action).await, Commands::Phone { action } => commands::phone::run(action).await, Commands::Prompt { action } => match action { PromptAction::List { json } => commands::prompt::run_list(&socket, json).await, PromptAction::Show { name, json } => { commands::prompt::run_show(&socket, &name, json).await } PromptAction::Create { name, body, description, agent, scope, json, } => { commands::prompt::run_create( &socket, &name, &body, &description, &agent, &scope, json, ) .await } PromptAction::Delete { name, scope, json } => { commands::prompt::run_delete(&socket, &name, &scope, json).await } }, Commands::Agent { action } => match action { AgentAction::List { json } => commands::agent_def::run_list(&socket, json).await, AgentAction::Create { name, agent_type, template, inline_prompt, command, tags, scope, json, } => { commands::agent_def::run_create( &socket, &name, &agent_type, template, inline_prompt, command, &tags, &scope, json, ) .await } AgentAction::Show { name, json } => { commands::agent_def::run_show(&socket, &name, json).await } AgentAction::Delete { name, scope, json } => { commands::agent_def::run_delete(&socket, &name, &scope, json).await } }, Commands::Swarm { action } => match action { SwarmAction::List { json } => commands::swarm::run_list(&socket, json).await, SwarmAction::Create { name, worktrees, worktree_template, roster, include_terminal, scope, json, } => { commands::swarm::run_create( &socket, &name, worktrees, &worktree_template, roster, include_terminal, &scope, json, ) .await } SwarmAction::Show { name, json } => { commands::swarm::run_show(&socket, &name, json).await } SwarmAction::Delete { name, scope, json } => { commands::swarm::run_delete(&socket, &name, &scope, json).await } SwarmAction::Run { name, vars, json } => { commands::swarm::run_run(&socket, &name, vars, json).await } }, Commands::Send { agent_id, text, no_enter, keys, json, } => commands::send::run(&socket, &agent_id, text, no_enter, keys, json).await, Commands::Grid { workspace, action } => { commands::grid::run(&socket, workspace, action).await } Commands::Trigger { action } => match action { TriggerAction::List { json } => commands::trigger::run_list(&socket, json).await, TriggerAction::Show { name, json } => { commands::trigger::run_show(&socket, &name, json).await } TriggerAction::Create { name, event, description, inject, gate, scope, json, } => { commands::trigger::run_create( &socket, commands::trigger::CreateTriggerParams { name, event, description, injects: inject, gates: gate, scope, json, }, ) .await } TriggerAction::Delete { name, scope, json } => { commands::trigger::run_delete(&socket, &name, &scope, json).await } TriggerAction::Assign { agent_id, trigger_name, json, } => commands::trigger::run_assign(&socket, &agent_id, &trigger_name, json).await, }, Commands::Gate { event, project_root, } => commands::gate::run(&socket, &event, project_root).await, Commands::Channel { project_root, action, } => commands::channel::run(&socket, project_root, action).await, Commands::Pulse { json } => commands::pulse::run(&socket, json).await, Commands::Diff { worktree, stat, json, } => commands::diff::run(&socket, worktree, stat, json).await, Commands::Watch { interval } => commands::watch::run(&socket, interval).await, Commands::Machine { action } => commands::machine::run(action).await, Commands::Doctor { .. } => unreachable!("pu doctor returns before connecting"), Commands::Clean { worktree, all, json, } => commands::clean::run(&socket, worktree, all, json).await, Commands::Schedule { action } => match action { ScheduleAction::List { json } => commands::schedule::run_list(&socket, json).await, ScheduleAction::Create { name, recurrence, start_at, trigger_type, trigger_name, trigger_prompt, agent, vars, scope, root, agent_name, json, } => { commands::schedule::run_create( &socket, &name, &recurrence, &start_at, &trigger_type, trigger_name.as_deref(), trigger_prompt.as_deref(), &agent, vars, &scope, root, agent_name, json, ) .await } ScheduleAction::Show { name, json } => { commands::schedule::run_show(&socket, &name, json).await } ScheduleAction::Delete { name, scope, json } => { commands::schedule::run_delete(&socket, &name, &scope, json).await } ScheduleAction::Enable { name, json } => { commands::schedule::run_enable(&socket, &name, json).await } ScheduleAction::Disable { name, json } => { commands::schedule::run_disable(&socket, &name, json).await } }, }; // process::exit skips destructors, so close the forward first. drop(endpoint); if let Err(e) = result { eprintln!("error: {e}"); std::process::exit(1); } if proxy { // A pending blocking stdin read would hold up runtime shutdown. std::process::exit(0); } } /// Commands that read or write files on this computer cannot run on a machine. fn local_only_command(command: &Commands) -> Option<&'static str> { match command { Commands::Init { .. } => Some("init"), Commands::Channel { .. } => Some("channel"), Commands::Gate { .. } => Some("gate"), Commands::Machine { .. } => Some("machine"), _ => None, } } async fn connect_machine( name: &str, command: &Commands, ) -> Result { if let Some(command) = local_only_command(command) { return Err(error::CliError::Other(format!( "`pu {command}` works with files on this computer and cannot be used with --machine" ))); } let machine = machine_target::find_machine(name)?; machine_target::set_remote_machine(machine.name.clone()); machine_target::connect(&machine).await } #[cfg(test)] mod tests { use super::*; #[test] fn given_read_command_should_accept_native_session_and_incremental_cursor() { // given / when let cli = Cli::try_parse_from([ "pu", "read", "ag-one", "--session", "exact", "--since", "cursor", "--limit", "10", "--json", ]); // then assert!(cli.is_ok()); } #[test] fn given_global_queries_and_explicit_scope_should_parse_without_ambiguous_scope() { for args in [ vec!["pu", "status", "--global", "--json"], vec!["pu", "projects", "list", "--json"], vec!["pu", "projects", "add", "--json"], vec!["pu", "projects", "add", "/project"], vec![ "pu", "projects", "remove", "/project", "--suspend-agents", "--json", ], vec!["pu", "agents", "list", "--state", "running", "--json"], vec!["pu", "worktrees", "list", "--global", "--json"], ] { assert!(Cli::try_parse_from(args).is_ok()); } let cli = Cli::try_parse_from([ "pu", "agents", "list", "--project", "/project", "--socket", "/test.sock", ]) .unwrap(); assert_eq!(cli.project.as_deref(), Some("/project")); assert_eq!(cli.socket, Some(std::path::PathBuf::from("/test.sock"))); for args in [ vec!["pu", "status", "--global", "--agent", "ag-1"], vec!["pu", "status", "--global", "--project", "/project"], vec!["pu", "agents", "list", "--global", "--project", "/project"], vec!["pu", "agents", "list", "--state", "idle"], ] { assert!(Cli::try_parse_from(args).is_err()); } } #[test] fn given_channel_commands_should_parse_routing_and_reject_invalid_cursors() { let cli = Cli::try_parse_from([ "pu", "channel", "send", "hello", "--reply-to", "msg-1", "--commit", "abcdef0", "--pr", "42", "--project-root", "/project", "--json", ]) .unwrap(); assert!( matches!(cli.command, Commands::Channel { project_root: Some(root), action: commands::channel::ChannelAction::Send { json: true, .. } } if root == "/project") ); for args in [ vec!["pu", "channel", "read", "--since", "1", "--before", "5"], vec!["pu", "channel", "read", "--limit", "201"], vec!["pu", "channel", "read", "--limit", "0"], ] { assert!(Cli::try_parse_from(args).is_err()); } for args in [ vec![ "pu", "channel", "read", "--search", "hello", "--thread", "msg-1", ], vec!["pu", "channel", "edit", "msg-1", "changed"], vec!["pu", "channel", "react", "msg-1", "--remove"], ] { assert!(Cli::try_parse_from(args).is_ok()); } } #[test] fn given_machine_flag_should_parse_globally_and_conflict_with_socket() { let cli = Cli::try_parse_from(["pu", "agents", "list", "--machine", "studio"]).unwrap(); assert_eq!(cli.machine.as_deref(), Some("studio")); assert!(Cli::try_parse_from(["pu", "--machine", "a", "--socket", "/s", "health"]).is_err()); for args in [ vec!["pu", "machine", "list", "--json"], vec!["pu", "machine", "add", "studio", "me@host:2222"], vec!["pu", "machine", "remove", "studio"], vec!["pu", "machine", "status"], vec!["pu", "machine", "status", "studio", "--json"], vec!["pu", "machine", "start", "studio"], ] { assert!(Cli::try_parse_from(&args).is_ok(), "{args:?}"); } } #[test] fn given_doctor_should_take_the_global_machine_flag_and_json() { // given / when let cli = Cli::try_parse_from(["pu", "doctor", "--machine", "studio", "--json"]).unwrap(); // then assert_eq!(cli.machine.as_deref(), Some("studio")); assert!(matches!(cli.command, Commands::Doctor { json: true })); } #[test] fn given_local_file_commands_should_be_rejected_for_machines() { for (args, name) in [ (vec!["pu", "init"], "init"), (vec!["pu", "gate", "pre-commit"], "gate"), (vec!["pu", "channel", "read"], "channel"), (vec!["pu", "machine", "list"], "machine"), ] { let cli = Cli::try_parse_from(args).unwrap(); assert_eq!(local_only_command(&cli.command), Some(name)); } let cli = Cli::try_parse_from(["pu", "status"]).unwrap(); assert_eq!(local_only_command(&cli.command), None); } fn parse_tab(args: &[&str]) -> Result { let argv = ["pu", "grid", "tab"].iter().chain(args).copied(); match Cli::try_parse_from(argv)?.command { Commands::Grid { action: GridAction::Tab { action }, .. } => Ok(action), _ => panic!("expected grid tab"), } } #[test] fn given_select_with_position_should_parse_index() { let action = parse_tab(&["select", "2", "--leaf", "1"]).unwrap(); assert!(matches!( action, TabAction::Select { index: Some(2), next: false, prev: false, leaf: Some(1) } )); } #[test] fn given_select_without_target_should_be_rejected() { assert!(parse_tab(&["select"]).is_err()); } #[test] fn given_select_with_two_targets_should_be_rejected() { assert!(parse_tab(&["select", "2", "--next"]).is_err()); assert!(parse_tab(&["select", "--next", "--prev"]).is_err()); } #[test] fn given_select_position_zero_should_be_rejected() { assert!(parse_tab(&["select", "0"]).is_err()); } #[test] fn given_move_should_require_destination_leaf() { assert!(parse_tab(&["move", "4"]).is_err()); let action = parse_tab(&["move", "4", "--to", "1", "--index", "2"]).unwrap(); assert!(matches!( action, TabAction::Move { tab_id: 4, to: 1, index: Some(2) } )); } #[test] fn given_break_without_axis_should_default_to_vertical() { let action = parse_tab(&["break"]).unwrap(); match action { TabAction::Break { tab, axis } => { assert_eq!(tab, None); assert_eq!(axis, "v"); } _ => panic!("expected Break"), } assert!(parse_tab(&["break", "--axis", "x"]).is_err()); } #[test] fn given_assign_without_leaf_should_leave_leaf_unset() { let cli = Cli::try_parse_from(["pu", "grid", "assign", "ag-a"]).unwrap(); assert!(matches!( cli.command, Commands::Grid { action: GridAction::Assign { leaf: None, .. }, workspace: None, } )); } #[test] fn given_workspace_flag_after_subcommand_should_apply_to_grid() { let cli = Cli::try_parse_from([ "pu", "grid", "tab", "close", "--tab", "3", "--workspace", "ws-ag-b", ]) .unwrap(); match cli.command { Commands::Grid { workspace, .. } => assert_eq!(workspace.as_deref(), Some("ws-ag-b")), _ => panic!("expected grid"), } } }