Skip to main content

objectiveai_sdk/cli/command/tasks/list/
mod.rs

1//! `agents tasks list` — inspection leaf over `schedules`.
2//!
3//! Scope is one or more `--target` entries (the shared `Target`:
4//! `me` / `instance=L[,parent=P]` / `tag=T`). Each resolves to an AIH
5//! and the listing returns the schedule rows whose
6//! `agent_instance_hierarchy` equals it — exact match, no subtree
7//! descent. Only the NEWEST version of each `(name, aih)` lists;
8//! `--overwrite`-shadowed versions stay on disk (per-version run
9//! history) but never surface here. `--oneshot` / `--interval` filter
10//! by kind; `--pending` / `--exhausted` filter by readiness (derived
11//! from the schedule's newest `tasks_runs` entry); `--after-id` /
12//! `--count` paginate forward by ascending row id.
13
14use crate::cli::command::CommandRequest;
15
16/// The same `Target` every hierarchy-scoped read command uses
17/// (`agents instances list`, `agents logs read all`, …).
18pub use crate::cli::command::agents::logs::read::all::Target;
19
20#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
21#[schemars(rename = "cli.command.tasks.list.Request")]
22pub struct Request {
23    pub path_type: Path,
24    /// One or more targets to list schedules for. Each resolves to a
25    /// single AIH (`me` → the cli's own; `instance=L[,parent=P]`;
26    /// `tag=T` BOUND-only — PENDING / ABSENT error), and rows whose
27    /// `agent_instance_hierarchy` equals any resolved AIH are returned.
28    pub targets: Vec<Target>,
29    /// Filter to oneshot rows only. Mutually exclusive with `interval`.
30    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
31    pub oneshot: bool,
32    /// Filter to recurring rows only. Mutually exclusive with `oneshot`.
33    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
34    pub interval: bool,
35    /// Show only schedules currently runnable — oneshots that
36    /// have never fired, and interval rows whose interval has
37    /// elapsed. Mutually exclusive with `exhausted`.
38    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
39    pub pending: bool,
40    /// Show only schedules NOT currently runnable — fired
41    /// oneshots (visible briefly before the runner deletes
42    /// them) and interval rows that are cooling down. Mutually
43    /// exclusive with `pending`.
44    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
45    pub exhausted: bool,
46    /// Skip rows with `schedules.id <= after_id`. Use the highest
47    /// `id` from a previous page to paginate forward.
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    #[schemars(extend("omitempty" = true))]
50    pub after_id: Option<i64>,
51    /// Per-target row cap — each target's query returns at most this
52    /// many rows (ascending id, after `after_id`). `None` = unlimited.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    #[schemars(extend("omitempty" = true))]
55    pub count: Option<u64>,
56    #[serde(flatten)]
57    pub base: crate::cli::command::RequestBase,
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
61#[schemars(rename = "cli.command.tasks.list.Path")]
62pub enum Path {
63    #[serde(rename = "tasks/list")]
64    AgentsTasksList,
65}
66
67impl CommandRequest for Request {
68    fn into_command(&self) -> Vec<String> {
69        let mut argv = vec![
70            "tasks".to_string(),
71            "list".to_string(),
72        ];
73        for target in &self.targets {
74            argv.push("--target".to_string());
75            argv.push(target.into_arg_string());
76        }
77        if self.oneshot {
78            argv.push("--oneshot".to_string());
79        }
80        if self.interval {
81            argv.push("--interval".to_string());
82        }
83        if self.pending {
84            argv.push("--pending".to_string());
85        }
86        if self.exhausted {
87            argv.push("--exhausted".to_string());
88        }
89        if let Some(after_id) = self.after_id {
90            argv.push("--after-id".to_string());
91            argv.push(after_id.to_string());
92        }
93        if let Some(c) = self.count {
94            argv.push("--count".to_string());
95            argv.push(c.to_string());
96        }
97        self.base.push_flags(&mut argv);
98        argv
99    }
100
101    fn request_base(&self) -> &crate::cli::command::RequestBase {
102        &self.base
103    }
104
105    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
106        Some(&mut self.base)
107    }
108}
109
110/// The plugin that registered a schedule. All three fields are present
111/// together or the whole object is absent (the `schedules` table
112/// enforces all-or-nothing on its `plugin_*` columns).
113#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
114#[schemars(rename = "cli.command.tasks.list.Plugin")]
115pub struct Plugin {
116    pub owner: String,
117    pub repository: String,
118    pub version: String,
119}
120
121/// One schedule row.
122#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
123#[schemars(rename = "cli.command.tasks.list.ResponseItem")]
124pub struct ResponseItem {
125    /// The `schedules` row id. Monotonic; pass the highest `id` from a
126    /// page as the next request's `after_id` to paginate forward.
127    pub id: i64,
128    /// The `--name` passed to `agents tasks schedule`. Unique per
129    /// `agent_instance_hierarchy`.
130    pub name: String,
131    pub agent_instance_hierarchy: String,
132    pub command: Vec<String>,
133    pub description: String,
134    /// RFC3339 timestamp this schedule row was created.
135    pub created_at: String,
136    /// RFC3339 timestamp of the most recent invocation — this row's
137    /// newest `tasks_runs` entry. `None` until the runner has fired
138    /// this version at least once (runs are tracked per-version).
139    #[serde(default, skip_serializing_if = "Option::is_none")]
140    #[schemars(extend("omitempty" = true))]
141    pub last_ran_at: Option<String>,
142    /// `None` for a oneshot; `Some("30s" / "1h" / "1d12h" / …)`
143    /// for a recurring schedule, formatted as humantime so the
144    /// list output reads naturally without a unit-conversion
145    /// step at the consumer. The CLI parser accepts the same
146    /// shape on `agents tasks schedule --interval`.
147    #[serde(default, skip_serializing_if = "Option::is_none")]
148    #[schemars(extend("omitempty" = true))]
149    pub interval: Option<String>,
150    /// This row's version: `1` for a freshly scheduled task,
151    /// `max + 1` for each `tasks schedule --overwrite` (each version
152    /// is its own row; only the newest lists).
153    pub version: u64,
154    /// The plugin that registered this schedule (its `(owner,
155    /// repository, version)` coordinate), or `None` when it was not
156    /// scheduled by a plugin.
157    #[serde(default, skip_serializing_if = "Option::is_none")]
158    #[schemars(extend("omitempty" = true))]
159    pub plugin: Option<Plugin>,
160}
161
162#[derive(clap::Args)]
163#[command(group(
164    clap::ArgGroup::new("kind")
165        .multiple(false)
166        .args(["oneshot", "interval"])
167))]
168#[command(group(
169    clap::ArgGroup::new("readiness")
170        .multiple(false)
171        .args(["pending", "exhausted"])
172))]
173pub struct Args {
174    /// One or more `--target instance=L[,parent=P]` entries. Also
175    /// accepts `--target tag=T` and `--target me`. Lists schedules
176    /// whose `agent_instance_hierarchy` equals each resolved AIH.
177    #[arg(long = "target", required = true)]
178    pub targets: Vec<String>,
179    /// Filter to oneshot rows only. Mutually exclusive with `--interval`.
180    #[arg(long)]
181    pub oneshot: bool,
182    /// Filter to recurring rows only. Mutually exclusive with `--oneshot`.
183    #[arg(long)]
184    pub interval: bool,
185    /// Only schedules currently runnable. Mutually exclusive with `--exhausted`.
186    #[arg(long)]
187    pub pending: bool,
188    /// Only schedules NOT currently runnable. Mutually exclusive with `--pending`.
189    #[arg(long)]
190    pub exhausted: bool,
191    /// Skip rows with `schedules.id <= after_id`; use the highest
192    /// `id` from the previous page to paginate forward.
193    #[arg(long)]
194    pub after_id: Option<i64>,
195    #[arg(long)]
196    pub count: Option<u64>,
197    #[command(flatten)]
198    pub base: crate::cli::command::RequestBaseArgs,
199}
200
201#[derive(clap::Args)]
202#[command(args_conflicts_with_subcommands = true)]
203pub struct Command {
204    #[command(flatten)]
205    pub args: Args,
206    #[command(subcommand)]
207    pub schema: Option<Schema>,
208}
209
210#[derive(clap::Subcommand)]
211pub enum Schema {
212    /// Emit the JSON Schema for this leaf's `Request` type and exit.
213    RequestSchema(request_schema::Args),
214    /// Emit the JSON Schema for this leaf's `Response` type and exit.
215    ResponseSchema(response_schema::Args),
216}
217
218impl TryFrom<Args> for Request {
219    type Error = crate::cli::command::FromArgsError;
220    fn try_from(args: Args) -> Result<Self, Self::Error> {
221        let targets = args
222            .targets
223            .iter()
224            .map(|s| {
225                s.parse::<Target>().map_err(|msg| {
226                    crate::cli::command::FromArgsError::path_parse("target", msg)
227                })
228            })
229            .collect::<Result<Vec<_>, _>>()?;
230        Ok(Self {
231            path_type: Path::AgentsTasksList,
232            targets,
233            oneshot: args.oneshot,
234            interval: args.interval,
235            pending: args.pending,
236            exhausted: args.exhausted,
237            after_id: args.after_id,
238            count: args.count,
239            base: args.base.into(),
240        })
241    }
242}
243
244#[cfg(feature = "cli-executor")]
245pub async fn execute<E: crate::cli::command::CommandExecutor>(
246    executor: &E,
247    mut request: Request,
248    agent_arguments: Option<&crate::cli::command::AgentArguments>,
249) -> Result<E::Stream<ResponseItem>, E::Error> {
250    request.base.clear_transform();
251    executor.execute(request, agent_arguments).await
252}
253
254#[cfg(feature = "cli-executor")]
255pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
256    executor: &E,
257    mut request: Request,
258    transform: crate::cli::command::Transform,
259    agent_arguments: Option<&crate::cli::command::AgentArguments>,
260) -> Result<E::Stream<serde_json::Value>, E::Error> {
261    request.base.set_transform(transform);
262    executor.execute(request, agent_arguments).await
263}
264
265#[cfg(feature = "mcp")]
266impl crate::cli::command::CommandResponse for ResponseItem {
267    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
268        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
269    }
270}
271
272pub mod request_schema;
273
274pub mod response_schema;