|
| 1 | +//! `libra agent workspace …` — the read-only machine interface over the |
| 2 | +//! Part C W4 workspace registry (§C.8): keyset-paginated `list` and a |
| 3 | +//! by-id `show`. Lease mutation stays internal to the AgentRuntime |
| 4 | +//! services; this surface only OBSERVES `workspace_record`. |
| 5 | +
|
| 6 | +use clap::{Args, Subcommand}; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + internal::{ |
| 10 | + db::get_db_conn_instance, |
| 11 | + workspace::{WorkspaceListQuery, WorkspaceRecord, WorkspaceState, WorkspaceStore}, |
| 12 | + }, |
| 13 | + utils::{ |
| 14 | + error::{CliError, CliResult}, |
| 15 | + output::{OutputConfig, emit_json_data}, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +/// `schema_version` of the paged list / show JSON `data` payloads |
| 20 | +/// (additive evolution only — mirrors the AG-20 pagination precedent). |
| 21 | +const WORKSPACE_SCHEMA_VERSION: u32 = 1; |
| 22 | + |
| 23 | +#[derive(Subcommand, Debug)] |
| 24 | +pub enum WorkspaceSubcommand { |
| 25 | + /// List workspace records, keyset-paginated (`workspace_id` ASC). |
| 26 | + #[command(about = "List workspace records (keyset pagination)")] |
| 27 | + List(WorkspaceListArgs), |
| 28 | + /// Show one workspace record by id. |
| 29 | + #[command(about = "Show one workspace record")] |
| 30 | + Show(WorkspaceShowArgs), |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Args, Debug)] |
| 34 | +pub struct WorkspaceListArgs { |
| 35 | + /// Maximum rows to return (default 50, capped at 500). |
| 36 | + #[arg(long, value_name = "N")] |
| 37 | + pub limit: Option<u64>, |
| 38 | + /// Keyset cursor: the `next_cursor` value from the previous page, |
| 39 | + /// round-tripped verbatim. |
| 40 | + #[arg(long, value_name = "CURSOR")] |
| 41 | + pub cursor: Option<String>, |
| 42 | + /// Restrict to one or more states (repeatable): |
| 43 | + /// provisioning|active|releasing|released|orphaned. |
| 44 | + #[arg(long = "state", value_name = "STATE")] |
| 45 | + pub states: Vec<String>, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Args, Debug)] |
| 49 | +pub struct WorkspaceShowArgs { |
| 50 | + /// Workspace identifier from `agent workspace list`. |
| 51 | + #[arg(value_name = "WORKSPACE_ID")] |
| 52 | + pub workspace_id: String, |
| 53 | +} |
| 54 | + |
| 55 | +pub async fn execute_safe(cmd: WorkspaceSubcommand, output: &OutputConfig) -> CliResult<()> { |
| 56 | + match cmd { |
| 57 | + WorkspaceSubcommand::List(args) => list(args, output).await, |
| 58 | + WorkspaceSubcommand::Show(args) => show(args, output).await, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn parse_state(raw: &str) -> CliResult<WorkspaceState> { |
| 63 | + match raw.trim().to_ascii_lowercase().as_str() { |
| 64 | + "provisioning" => Ok(WorkspaceState::Provisioning), |
| 65 | + "active" => Ok(WorkspaceState::Active), |
| 66 | + "releasing" => Ok(WorkspaceState::Releasing), |
| 67 | + "released" => Ok(WorkspaceState::Released), |
| 68 | + "orphaned" => Ok(WorkspaceState::Orphaned), |
| 69 | + other => Err(CliError::command_usage(format!( |
| 70 | + "invalid --state '{other}': expected provisioning, active, releasing, \ |
| 71 | + released, or orphaned" |
| 72 | + ))), |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn record_json(record: &WorkspaceRecord) -> serde_json::Value { |
| 77 | + serde_json::json!({ |
| 78 | + "workspace_id": record.workspace_id, |
| 79 | + "kind": record.kind.as_db_value(), |
| 80 | + "worktree_id": record.worktree_id, |
| 81 | + "path": record.path, |
| 82 | + "owner_kind": record.owner_kind.as_db_value(), |
| 83 | + "owner_id": record.owner_id, |
| 84 | + "task_id": record.task_id, |
| 85 | + "session_id": record.session_id, |
| 86 | + "base_commit": record.base_commit, |
| 87 | + "branch": record.branch, |
| 88 | + "state": record.state.as_db_value(), |
| 89 | + "lease_owner": record.lease_owner, |
| 90 | + "lease_fence": record.lease_fence, |
| 91 | + "lease_expires_at": record.lease_expires_at, |
| 92 | + "created_at": record.created_at, |
| 93 | + "updated_at": record.updated_at, |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +async fn list(args: WorkspaceListArgs, output: &OutputConfig) -> CliResult<()> { |
| 98 | + let states = if args.states.is_empty() { |
| 99 | + None |
| 100 | + } else { |
| 101 | + Some( |
| 102 | + args.states |
| 103 | + .iter() |
| 104 | + .map(|raw| parse_state(raw)) |
| 105 | + .collect::<CliResult<Vec<_>>>()?, |
| 106 | + ) |
| 107 | + }; |
| 108 | + let query = WorkspaceListQuery { |
| 109 | + states, |
| 110 | + limit: args.limit, |
| 111 | + cursor: args.cursor, |
| 112 | + }; |
| 113 | + let conn = get_db_conn_instance().await; |
| 114 | + let page = WorkspaceStore::list_with_conn(&conn, &query) |
| 115 | + .await |
| 116 | + .map_err(|error| CliError::fatal(format!("failed to list workspace records: {error}")))?; |
| 117 | + |
| 118 | + if output.is_json() { |
| 119 | + let payload = serde_json::json!({ |
| 120 | + "schema_version": WORKSPACE_SCHEMA_VERSION, |
| 121 | + "workspaces": page.items.iter().map(record_json).collect::<Vec<_>>(), |
| 122 | + "next_cursor": page.next_cursor, |
| 123 | + }); |
| 124 | + return emit_json_data("agent_workspaces", &payload, output); |
| 125 | + } |
| 126 | + if output.quiet { |
| 127 | + return Ok(()); |
| 128 | + } |
| 129 | + if page.items.is_empty() { |
| 130 | + println!("(no workspace records)"); |
| 131 | + return Ok(()); |
| 132 | + } |
| 133 | + for record in &page.items { |
| 134 | + println!( |
| 135 | + "{} {:12} {:9} {}", |
| 136 | + record.workspace_id, |
| 137 | + record.state.as_db_value(), |
| 138 | + record.kind.as_db_value(), |
| 139 | + record.path |
| 140 | + ); |
| 141 | + } |
| 142 | + if let Some(cursor) = &page.next_cursor { |
| 143 | + println!("(more rows: --cursor {cursor})"); |
| 144 | + } |
| 145 | + Ok(()) |
| 146 | +} |
| 147 | + |
| 148 | +async fn show(args: WorkspaceShowArgs, output: &OutputConfig) -> CliResult<()> { |
| 149 | + let conn = get_db_conn_instance().await; |
| 150 | + let record = WorkspaceStore::get_with_conn(&conn, &args.workspace_id) |
| 151 | + .await |
| 152 | + .map_err(|error| CliError::fatal(format!("failed to read workspace record: {error}")))? |
| 153 | + .ok_or_else(|| { |
| 154 | + CliError::fatal(format!( |
| 155 | + "no workspace matches id '{}'; list workspaces with `libra agent workspace list`", |
| 156 | + args.workspace_id |
| 157 | + )) |
| 158 | + })?; |
| 159 | + |
| 160 | + if output.is_json() { |
| 161 | + let payload = serde_json::json!({ |
| 162 | + "schema_version": WORKSPACE_SCHEMA_VERSION, |
| 163 | + "workspace": record_json(&record), |
| 164 | + }); |
| 165 | + return emit_json_data("agent_workspace", &payload, output); |
| 166 | + } |
| 167 | + if output.quiet { |
| 168 | + return Ok(()); |
| 169 | + } |
| 170 | + println!("workspace_id: {}", record.workspace_id); |
| 171 | + println!("kind: {}", record.kind.as_db_value()); |
| 172 | + println!("state: {}", record.state.as_db_value()); |
| 173 | + println!("path: {}", record.path); |
| 174 | + if let Some(worktree_id) = &record.worktree_id { |
| 175 | + println!("worktree_id: {worktree_id}"); |
| 176 | + } |
| 177 | + println!("owner_kind: {}", record.owner_kind.as_db_value()); |
| 178 | + if let Some(owner_id) = &record.owner_id { |
| 179 | + println!("owner_id: {owner_id}"); |
| 180 | + } |
| 181 | + if let Some(task_id) = &record.task_id { |
| 182 | + println!("task_id: {task_id}"); |
| 183 | + } |
| 184 | + if let Some(session_id) = &record.session_id { |
| 185 | + println!("session_id: {session_id}"); |
| 186 | + } |
| 187 | + if let Some(branch) = &record.branch { |
| 188 | + println!("branch: {branch}"); |
| 189 | + } |
| 190 | + if let Some(base_commit) = &record.base_commit { |
| 191 | + println!("base_commit: {base_commit}"); |
| 192 | + } |
| 193 | + if let Some(lease_owner) = &record.lease_owner { |
| 194 | + println!("lease_owner: {lease_owner} (fence {})", record.lease_fence); |
| 195 | + } |
| 196 | + if let Some(expires) = record.lease_expires_at { |
| 197 | + println!("lease_expires_at: {expires}"); |
| 198 | + } |
| 199 | + println!("created_at: {}", record.created_at); |
| 200 | + println!("updated_at: {}", record.updated_at); |
| 201 | + Ok(()) |
| 202 | +} |
0 commit comments