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    pub created_at: i64,
135    /// Unix seconds of the most recent invocation — this row's newest
136    /// `tasks_runs` entry. `None` until the runner has fired this
137    /// version at least once (runs are tracked per-version).
138    #[serde(default, skip_serializing_if = "Option::is_none")]
139    #[schemars(extend("omitempty" = true))]
140    pub last_ran_at: Option<i64>,
141    /// `None` for a oneshot; `Some("30s" / "1h" / "1d12h" / …)`
142    /// for a recurring schedule, formatted as humantime so the
143    /// list output reads naturally without a unit-conversion
144    /// step at the consumer. The CLI parser accepts the same
145    /// shape on `agents tasks schedule --interval`.
146    #[serde(default, skip_serializing_if = "Option::is_none")]
147    #[schemars(extend("omitempty" = true))]
148    pub interval: Option<String>,
149    /// This row's version: `1` for a freshly scheduled task,
150    /// `max + 1` for each `tasks schedule --overwrite` (each version
151    /// is its own row; only the newest lists).
152    pub version: u64,
153    /// The plugin that registered this schedule (its `(owner,
154    /// repository, version)` coordinate), or `None` when it was not
155    /// scheduled by a plugin.
156    #[serde(default, skip_serializing_if = "Option::is_none")]
157    #[schemars(extend("omitempty" = true))]
158    pub plugin: Option<Plugin>,
159}
160
161#[derive(clap::Args)]
162#[command(group(
163    clap::ArgGroup::new("kind")
164        .multiple(false)
165        .args(["oneshot", "interval"])
166))]
167#[command(group(
168    clap::ArgGroup::new("readiness")
169        .multiple(false)
170        .args(["pending", "exhausted"])
171))]
172pub struct Args {
173    /// One or more `--target instance=L[,parent=P]` entries. Also
174    /// accepts `--target tag=T` and `--target me`. Lists schedules
175    /// whose `agent_instance_hierarchy` equals each resolved AIH.
176    #[arg(long = "target", required = true)]
177    pub targets: Vec<String>,
178    /// Filter to oneshot rows only. Mutually exclusive with `--interval`.
179    #[arg(long)]
180    pub oneshot: bool,
181    /// Filter to recurring rows only. Mutually exclusive with `--oneshot`.
182    #[arg(long)]
183    pub interval: bool,
184    /// Only schedules currently runnable. Mutually exclusive with `--exhausted`.
185    #[arg(long)]
186    pub pending: bool,
187    /// Only schedules NOT currently runnable. Mutually exclusive with `--pending`.
188    #[arg(long)]
189    pub exhausted: bool,
190    /// Skip rows with `schedules.id <= after_id`; use the highest
191    /// `id` from the previous page to paginate forward.
192    #[arg(long)]
193    pub after_id: Option<i64>,
194    #[arg(long)]
195    pub count: Option<u64>,
196    #[command(flatten)]
197    pub base: crate::cli::command::RequestBaseArgs,
198}
199
200#[derive(clap::Args)]
201#[command(args_conflicts_with_subcommands = true)]
202pub struct Command {
203    #[command(flatten)]
204    pub args: Args,
205    #[command(subcommand)]
206    pub schema: Option<Schema>,
207}
208
209#[derive(clap::Subcommand)]
210pub enum Schema {
211    /// Emit the JSON Schema for this leaf's `Request` type and exit.
212    RequestSchema(request_schema::Args),
213    /// Emit the JSON Schema for this leaf's `Response` type and exit.
214    ResponseSchema(response_schema::Args),
215}
216
217impl TryFrom<Args> for Request {
218    type Error = crate::cli::command::FromArgsError;
219    fn try_from(args: Args) -> Result<Self, Self::Error> {
220        let targets = args
221            .targets
222            .iter()
223            .map(|s| {
224                s.parse::<Target>().map_err(|msg| {
225                    crate::cli::command::FromArgsError::path_parse("target", msg)
226                })
227            })
228            .collect::<Result<Vec<_>, _>>()?;
229        Ok(Self {
230            path_type: Path::AgentsTasksList,
231            targets,
232            oneshot: args.oneshot,
233            interval: args.interval,
234            pending: args.pending,
235            exhausted: args.exhausted,
236            after_id: args.after_id,
237            count: args.count,
238            base: args.base.into(),
239        })
240    }
241}
242
243#[cfg(feature = "cli-executor")]
244pub async fn execute<E: crate::cli::command::CommandExecutor>(
245    executor: &E,
246    mut request: Request,
247    agent_arguments: Option<&crate::cli::command::AgentArguments>,
248) -> Result<E::Stream<ResponseItem>, E::Error> {
249    request.base.clear_transform();
250    executor.execute(request, agent_arguments).await
251}
252
253#[cfg(feature = "cli-executor")]
254pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
255    executor: &E,
256    mut request: Request,
257    transform: crate::cli::command::Transform,
258    agent_arguments: Option<&crate::cli::command::AgentArguments>,
259) -> Result<E::Stream<serde_json::Value>, E::Error> {
260    request.base.set_transform(transform);
261    executor.execute(request, agent_arguments).await
262}
263
264#[cfg(feature = "mcp")]
265impl crate::cli::command::CommandResponse for ResponseItem {
266    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
267        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
268    }
269}
270
271pub mod request_schema;
272
273pub mod response_schema;