diff --git a/codex-rs/backend-client/src/client.rs b/codex-rs/backend-client/src/client.rs index fdd4504bb6..ea3585956b 100644 --- a/codex-rs/backend-client/src/client.rs +++ b/codex-rs/backend-client/src/client.rs @@ -174,6 +174,7 @@ impl Client { limit: Option, task_filter: Option<&str>, environment_id: Option<&str>, + cursor: Option<&str>, ) -> Result { let url = match self.path_style { PathStyle::CodexApi => format!("{}/api/codex/tasks/list", self.base_url), @@ -190,6 +191,11 @@ impl Client { } else { req }; + let req = if let Some(c) = cursor { + req.query(&[("cursor", c)]) + } else { + req + }; let req = if let Some(id) = environment_id { req.query(&[("environment_id", id)]) } else { diff --git a/codex-rs/cloud-tasks-client/src/api.rs b/codex-rs/cloud-tasks-client/src/api.rs index cd8228bc28..7059bdb39f 100644 --- a/codex-rs/cloud-tasks-client/src/api.rs +++ b/codex-rs/cloud-tasks-client/src/api.rs @@ -94,6 +94,12 @@ pub struct CreatedTask { pub id: TaskId, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct TaskListPage { + pub tasks: Vec, + pub cursor: Option, +} + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] pub struct DiffSummary { pub files_changed: usize, @@ -126,7 +132,12 @@ impl Default for TaskText { #[async_trait::async_trait] pub trait CloudBackend: Send + Sync { - async fn list_tasks(&self, env: Option<&str>) -> Result>; + async fn list_tasks( + &self, + env: Option<&str>, + limit: Option, + cursor: Option<&str>, + ) -> Result; async fn get_task_summary(&self, id: TaskId) -> Result; async fn get_task_diff(&self, id: TaskId) -> Result>; /// Return assistant output messages (no diff) when available. diff --git a/codex-rs/cloud-tasks-client/src/http.rs b/codex-rs/cloud-tasks-client/src/http.rs index f55d0fe797..e6990b7ebd 100644 --- a/codex-rs/cloud-tasks-client/src/http.rs +++ b/codex-rs/cloud-tasks-client/src/http.rs @@ -6,6 +6,7 @@ use crate::CloudTaskError; use crate::DiffSummary; use crate::Result; use crate::TaskId; +use crate::TaskListPage; use crate::TaskStatus; use crate::TaskSummary; use crate::TurnAttempt; @@ -59,8 +60,13 @@ impl HttpClient { #[async_trait::async_trait] impl CloudBackend for HttpClient { - async fn list_tasks(&self, env: Option<&str>) -> Result> { - self.tasks_api().list(env).await + async fn list_tasks( + &self, + env: Option<&str>, + limit: Option, + cursor: Option<&str>, + ) -> Result { + self.tasks_api().list(env, limit, cursor).await } async fn get_task_summary(&self, id: TaskId) -> Result { @@ -132,10 +138,16 @@ mod api { } } - pub(crate) async fn list(&self, env: Option<&str>) -> Result> { + pub(crate) async fn list( + &self, + env: Option<&str>, + limit: Option, + cursor: Option<&str>, + ) -> Result { + let limit_i32 = limit.and_then(|lim| i32::try_from(lim).ok()); let resp = self .backend - .list_tasks(Some(20), Some("current"), env) + .list_tasks(limit_i32, Some("current"), env, cursor) .await .map_err(|e| CloudTaskError::Http(format!("list_tasks failed: {e}")))?; @@ -146,11 +158,19 @@ mod api { .collect(); append_error_log(&format!( - "http.list_tasks: env={} items={}", + "http.list_tasks: env={} limit={} cursor_in={} cursor_out={} items={}", env.unwrap_or(""), + limit_i32 + .map(|v| v.to_string()) + .unwrap_or_else(|| "".to_string()), + cursor.unwrap_or(""), + resp.cursor.as_deref().unwrap_or(""), tasks.len() )); - Ok(tasks) + Ok(TaskListPage { + tasks, + cursor: resp.cursor, + }) } pub(crate) async fn summary(&self, id: TaskId) -> Result { diff --git a/codex-rs/cloud-tasks-client/src/lib.rs b/codex-rs/cloud-tasks-client/src/lib.rs index a723512f8a..b28b356f2a 100644 --- a/codex-rs/cloud-tasks-client/src/lib.rs +++ b/codex-rs/cloud-tasks-client/src/lib.rs @@ -9,6 +9,7 @@ pub use api::CreatedTask; pub use api::DiffSummary; pub use api::Result; pub use api::TaskId; +pub use api::TaskListPage; pub use api::TaskStatus; pub use api::TaskSummary; pub use api::TaskText; diff --git a/codex-rs/cloud-tasks-client/src/mock.rs b/codex-rs/cloud-tasks-client/src/mock.rs index 2d03cea029..f6e14e61a2 100644 --- a/codex-rs/cloud-tasks-client/src/mock.rs +++ b/codex-rs/cloud-tasks-client/src/mock.rs @@ -16,7 +16,12 @@ pub struct MockClient; #[async_trait::async_trait] impl CloudBackend for MockClient { - async fn list_tasks(&self, _env: Option<&str>) -> Result> { + async fn list_tasks( + &self, + _env: Option<&str>, + _limit: Option, + _cursor: Option<&str>, + ) -> Result { // Slightly vary content by env to aid tests that rely on the mock let rows = match _env { Some("env-A") => vec![("T-2000", "A: First", TaskStatus::Ready)], @@ -58,11 +63,14 @@ impl CloudBackend for MockClient { attempt_total: Some(if id_str == "T-1000" { 2 } else { 1 }), }); } - Ok(out) + Ok(crate::TaskListPage { + tasks: out, + cursor: None, + }) } async fn get_task_summary(&self, id: TaskId) -> Result { - let tasks = self.list_tasks(None).await?; + let tasks = self.list_tasks(None, None, None).await?.tasks; tasks .into_iter() .find(|t| t.id == id) diff --git a/codex-rs/cloud-tasks/src/app.rs b/codex-rs/cloud-tasks/src/app.rs index ce12128a3e..ac3dd9e8df 100644 --- a/codex-rs/cloud-tasks/src/app.rs +++ b/codex-rs/cloud-tasks/src/app.rs @@ -123,9 +123,13 @@ pub async fn load_tasks( env: Option<&str>, ) -> anyhow::Result> { // In later milestones, add a small debounce, spinner, and error display. - let tasks = tokio::time::timeout(Duration::from_secs(5), backend.list_tasks(env)).await??; + let tasks = tokio::time::timeout( + Duration::from_secs(5), + backend.list_tasks(env, Some(20), None), + ) + .await??; // Hide review-only tasks from the main list. - let filtered: Vec = tasks.into_iter().filter(|t| !t.is_review).collect(); + let filtered: Vec = tasks.tasks.into_iter().filter(|t| !t.is_review).collect(); Ok(filtered) } @@ -362,7 +366,9 @@ mod tests { async fn list_tasks( &self, env: Option<&str>, - ) -> codex_cloud_tasks_client::Result> { + limit: Option, + cursor: Option<&str>, + ) -> codex_cloud_tasks_client::Result { let key = env.map(str::to_string); let titles = self .by_env @@ -383,15 +389,28 @@ mod tests { attempt_total: Some(1), }); } - Ok(out) + let max = limit.unwrap_or(i64::MAX); + let max = max.min(20); + let mut limited = Vec::new(); + for task in out { + if (limited.len() as i64) >= max { + break; + } + limited.push(task); + } + Ok(codex_cloud_tasks_client::TaskListPage { + tasks: limited, + cursor: cursor.map(str::to_string), + }) } async fn get_task_summary( &self, id: TaskId, ) -> codex_cloud_tasks_client::Result { - self.list_tasks(None) + self.list_tasks(None, None, None) .await? + .tasks .into_iter() .find(|t| t.id == id) .ok_or_else(|| CloudTaskError::Msg(format!("Task {} not found", id.0))) diff --git a/codex-rs/cloud-tasks/src/cli.rs b/codex-rs/cloud-tasks/src/cli.rs index 6b36509639..595c649b38 100644 --- a/codex-rs/cloud-tasks/src/cli.rs +++ b/codex-rs/cloud-tasks/src/cli.rs @@ -18,6 +18,8 @@ pub enum Command { Exec(ExecCommand), /// Show the status of a Codex Cloud task. Status(StatusCommand), + /// List Codex Cloud tasks. + List(ListCommand), /// Apply the diff for a Codex Cloud task locally. Apply(ApplyCommand), /// Show the unified diff for a Codex Cloud task. @@ -58,6 +60,17 @@ fn parse_attempts(input: &str) -> Result { } } +fn parse_limit(input: &str) -> Result { + let value: i64 = input + .parse() + .map_err(|_| "limit must be an integer between 1 and 20".to_string())?; + if (1..=20).contains(&value) { + Ok(value) + } else { + Err("limit must be between 1 and 20".to_string()) + } +} + #[derive(Debug, Args)] pub struct StatusCommand { /// Codex Cloud task identifier to inspect. @@ -65,6 +78,25 @@ pub struct StatusCommand { pub task_id: String, } +#[derive(Debug, Args)] +pub struct ListCommand { + /// Filter tasks by environment identifier. + #[arg(long = "env", value_name = "ENV_ID")] + pub environment: Option, + + /// Maximum number of tasks to return (1-20). + #[arg(long = "limit", default_value_t = 20, value_parser = parse_limit, value_name = "N")] + pub limit: i64, + + /// Pagination cursor returned by a previous call. + #[arg(long = "cursor", value_name = "CURSOR")] + pub cursor: Option, + + /// Emit JSON instead of plain text. + #[arg(long = "json", default_value_t = false)] + pub json: bool, +} + #[derive(Debug, Args)] pub struct ApplyCommand { /// Codex Cloud task identifier to apply. diff --git a/codex-rs/cloud-tasks/src/lib.rs b/codex-rs/cloud-tasks/src/lib.rs index e1bedbc1ce..b1d42fb86f 100644 --- a/codex-rs/cloud-tasks/src/lib.rs +++ b/codex-rs/cloud-tasks/src/lib.rs @@ -393,11 +393,10 @@ fn summary_line(summary: &codex_cloud_tasks_client::DiffSummary, colorize: bool) let bullet = "•" .if_supports_color(Stream::Stdout, |t| t.dimmed()) .to_string(); - let file_label = "file" + let file_label = format!("file{}", if files == 1 { "" } else { "s" }) .if_supports_color(Stream::Stdout, |t| t.dimmed()) .to_string(); - let plural = if files == 1 { "" } else { "s" }; - format!("{adds_str}/{dels_str} {bullet} {files} {file_label}{plural}") + format!("{adds_str}/{dels_str} {bullet} {files} {file_label}") } else { format!( "+{adds}/-{dels} • {files} file{}", @@ -473,6 +472,25 @@ fn format_task_status_lines( lines } +fn format_task_list_lines( + tasks: &[codex_cloud_tasks_client::TaskSummary], + base_url: &str, + now: chrono::DateTime, + colorize: bool, +) -> Vec { + let mut lines = Vec::new(); + for (idx, task) in tasks.iter().enumerate() { + lines.push(util::task_url(base_url, &task.id.0)); + for line in format_task_status_lines(task, now, colorize) { + lines.push(format!(" {line}")); + } + if idx + 1 < tasks.len() { + lines.push(String::new()); + } + } + lines +} + async fn run_status_command(args: crate::cli::StatusCommand) -> anyhow::Result<()> { let ctx = init_backend("codex_cloud_tasks_status").await?; let task_id = parse_task_id(&args.task_id)?; @@ -489,6 +507,73 @@ async fn run_status_command(args: crate::cli::StatusCommand) -> anyhow::Result<( Ok(()) } +async fn run_list_command(args: crate::cli::ListCommand) -> anyhow::Result<()> { + let ctx = init_backend("codex_cloud_tasks_list").await?; + let env_filter = if let Some(env) = args.environment { + Some(resolve_environment_id(&ctx, &env).await?) + } else { + None + }; + let page = codex_cloud_tasks_client::CloudBackend::list_tasks( + &*ctx.backend, + env_filter.as_deref(), + Some(args.limit), + args.cursor.as_deref(), + ) + .await?; + if args.json { + let tasks: Vec<_> = page + .tasks + .iter() + .map(|task| { + serde_json::json!({ + "id": task.id.0, + "url": util::task_url(&ctx.base_url, &task.id.0), + "title": task.title, + "status": task.status, + "updated_at": task.updated_at, + "environment_id": task.environment_id, + "environment_label": task.environment_label, + "summary": { + "files_changed": task.summary.files_changed, + "lines_added": task.summary.lines_added, + "lines_removed": task.summary.lines_removed, + }, + "is_review": task.is_review, + "attempt_total": task.attempt_total, + }) + }) + .collect(); + let payload = serde_json::json!({ + "tasks": tasks, + "cursor": page.cursor, + }); + println!("{}", serde_json::to_string_pretty(&payload)?); + return Ok(()); + } + if page.tasks.is_empty() { + println!("No tasks found."); + return Ok(()); + } + let now = Utc::now(); + let colorize = supports_color::on(SupportStream::Stdout).is_some(); + for line in format_task_list_lines(&page.tasks, &ctx.base_url, now, colorize) { + println!("{line}"); + } + if let Some(cursor) = page.cursor { + let command = format!("codex cloud list --cursor='{cursor}'"); + if colorize { + println!( + "\nTo fetch the next page, run {}", + command.if_supports_color(Stream::Stdout, |text| text.cyan()) + ); + } else { + println!("\nTo fetch the next page, run {command}"); + } + } + Ok(()) +} + async fn run_diff_command(args: crate::cli::DiffCommand) -> anyhow::Result<()> { let ctx = init_backend("codex_cloud_tasks_diff").await?; let task_id = parse_task_id(&args.task_id)?; @@ -649,6 +734,7 @@ pub async fn run_main(cli: Cli, _codex_linux_sandbox_exe: Option) -> an return match command { crate::cli::Command::Exec(args) => run_exec_command(args).await, crate::cli::Command::Status(args) => run_status_command(args).await, + crate::cli::Command::List(args) => run_list_command(args).await, crate::cli::Command::Apply(args) => run_apply_command(args).await, crate::cli::Command::Diff(args) => run_diff_command(args).await, }; @@ -2181,6 +2267,54 @@ mod tests { ); } + #[test] + fn format_task_list_lines_formats_urls() { + let now = Utc::now(); + let tasks = vec![ + TaskSummary { + id: TaskId("task_1".to_string()), + title: "Example task".to_string(), + status: TaskStatus::Ready, + updated_at: now, + environment_id: Some("env-1".to_string()), + environment_label: Some("Env".to_string()), + summary: DiffSummary { + files_changed: 3, + lines_added: 5, + lines_removed: 2, + }, + is_review: false, + attempt_total: None, + }, + TaskSummary { + id: TaskId("task_2".to_string()), + title: "No diff task".to_string(), + status: TaskStatus::Pending, + updated_at: now, + environment_id: Some("env-2".to_string()), + environment_label: None, + summary: DiffSummary::default(), + is_review: false, + attempt_total: Some(1), + }, + ]; + let lines = format_task_list_lines(&tasks, "https://chatgpt.com/backend-api", now, false); + assert_eq!( + lines, + vec![ + "https://chatgpt.com/codex/tasks/task_1".to_string(), + " [READY] Example task".to_string(), + " Env • 0s ago".to_string(), + " +5/-2 • 3 files".to_string(), + String::new(), + "https://chatgpt.com/codex/tasks/task_2".to_string(), + " [PENDING] No diff task".to_string(), + " env-2 • 0s ago".to_string(), + " no diff".to_string(), + ] + ); + } + #[tokio::test] async fn collect_attempt_diffs_includes_sibling_attempts() { let backend = MockClient; diff --git a/codex-rs/cloud-tasks/tests/env_filter.rs b/codex-rs/cloud-tasks/tests/env_filter.rs index 8c737c6c28..688ccd29bd 100644 --- a/codex-rs/cloud-tasks/tests/env_filter.rs +++ b/codex-rs/cloud-tasks/tests/env_filter.rs @@ -5,18 +5,23 @@ use codex_cloud_tasks_client::MockClient; async fn mock_backend_varies_by_env() { let client = MockClient; - let root = CloudBackend::list_tasks(&client, None).await.unwrap(); + let root = CloudBackend::list_tasks(&client, None, None, None) + .await + .unwrap() + .tasks; assert!(root.iter().any(|t| t.title.contains("Update README"))); - let a = CloudBackend::list_tasks(&client, Some("env-A")) + let a = CloudBackend::list_tasks(&client, Some("env-A"), None, None) .await - .unwrap(); + .unwrap() + .tasks; assert_eq!(a.len(), 1); assert_eq!(a[0].title, "A: First"); - let b = CloudBackend::list_tasks(&client, Some("env-B")) + let b = CloudBackend::list_tasks(&client, Some("env-B"), None, None) .await - .unwrap(); + .unwrap() + .tasks; assert_eq!(b.len(), 2); assert!(b[0].title.starts_with("B: ")); }