kanade_shared/ipc/jobs.rs
1//! `jobs.*` method types — user-invokable job catalog + execute +
2//! progress + kill.
3//!
4//! The Client App's three job-driven tabs (SPEC §2.1):
5//! - "アップデート" lists `category: software_update` manifests
6//! - "困ったとき" lists `category: troubleshoot` manifests
7//! - Software catalog lists `category: catalog` manifests
8//!
9//! All three flow through the same `jobs.list` / `jobs.execute` /
10//! `jobs.progress` pipeline — only the filter differs. Manifests
11//! with `user_invokable: false` are invisible from KLP (the agent
12//! filters before answering `jobs.list`, and rejects
13//! `jobs.execute` with `Unauthorized` if a client tries to call
14//! one directly).
15
16use serde::{Deserialize, Serialize};
17
18// ---------- shared types ----------
19
20/// Category key for a user-invokable job (the manifest's
21/// `client.category`). #792: this is now a **free-form string**, not a
22/// closed enum — an operator names a tab from the manifest alone and the
23/// Client App renders one tab per distinct key it sees (label / icon /
24/// order supplied by `client.category_label` / `_icon` / `_order`, with
25/// built-in defaults for the well-known keys below).
26///
27/// The agent's maintenance / auto-reboot logic special-cases the
28/// `software_update` key, so it's named here as a constant rather than a
29/// bare literal; the other well-known keys only carry Client App display
30/// defaults, so they live there.
31pub const CATEGORY_SOFTWARE_UPDATE: &str = "software_update";
32
33/// Run-state machine for one `jobs.execute` invocation.
34///
35/// State transitions:
36/// `Queued` → `Running` → `Completed` | `Failed` | `Killed`.
37/// `Queued` ⇒ accepted but not started yet (waiting on the
38/// concurrent-run cap or staleness check); the very first
39/// `jobs.progress` push usually moves straight to `Running`.
40/// `#[non_exhaustive]` so a future SPEC can add states like
41/// `Skipped` (staleness gate) without a wire bump.
42#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Hash)]
43#[serde(rename_all = "snake_case")]
44#[non_exhaustive]
45pub enum RunStatus {
46 /// Accepted, not yet spawned.
47 Queued,
48 /// `tokio::process::Command::spawn()` returned, script is
49 /// running.
50 Running,
51 /// Exited with code 0 (or whatever the manifest declares as
52 /// success).
53 Completed,
54 /// Exited non-zero, or a Layer 2 skipped-result was published.
55 Failed,
56 /// User-initiated kill via `jobs.kill`. Distinct from `Failed`
57 /// so the SPA can show "stopped by you" instead of "errored".
58 Killed,
59 /// #492: serde-level forward-compat catch-all. `#[non_exhaustive]`
60 /// only affects Rust match exhaustiveness — serde still hard-fails
61 /// on an unknown variant STRING, so a newer peer's new variant
62 /// used to make older readers reject the whole containing message.
63 /// Unknown decodes any unrecognised value; UIs render it neutrally.
64 #[serde(other)]
65 Unknown,
66}
67
68/// One entry in `jobs.list` — the SPEC §2.12.11 reference shape,
69/// extended with the `description` field used by the manifest's
70/// existing `display_description`.
71#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
72pub struct UserInvokableJob {
73 /// Manifest id (matches everywhere else — `Command.id`,
74 /// `ExecResult.manifest_id`).
75 pub id: String,
76 /// `display_name` from the manifest.
77 pub display_name: String,
78 /// `display_description` from the manifest. Renders as the row's
79 /// subtitle in the Client App.
80 #[serde(default, skip_serializing_if = "Option::is_none")]
81 pub display_description: Option<String>,
82 /// Optional icon hint (lucide-react name or a `data:` URL).
83 /// `None` means the SPA falls back to the category's default
84 /// icon.
85 #[serde(default, skip_serializing_if = "Option::is_none")]
86 pub icon: Option<String>,
87 /// Free-form category key (#792). The Client App groups jobs into one
88 /// tab per distinct key.
89 pub category: String,
90 /// Operator-supplied display name for the category's tab (from
91 /// `client.category_label`). `None` ⇒ the Client App uses a built-in
92 /// default for a well-known key, else the key itself.
93 #[serde(default, skip_serializing_if = "Option::is_none")]
94 pub category_label: Option<String>,
95 /// Operator-supplied tab icon (lucide name or `data:` URL) for the
96 /// category (from `client.category_icon`). `None` ⇒ Client App default.
97 #[serde(default, skip_serializing_if = "Option::is_none")]
98 pub category_icon: Option<String>,
99 /// Operator-supplied sort order for the tab (from
100 /// `client.category_order`); lower sorts first. `None` ⇒ default.
101 #[serde(default, skip_serializing_if = "Option::is_none")]
102 pub category_order: Option<i64>,
103 /// Pinned version string from the manifest. Same field as
104 /// `Manifest.version`.
105 pub version: String,
106 /// Snapshot of the last KLP-driven run of this job FOR THIS
107 /// USER. `None` until they've executed it at least once.
108 /// Backend keeps the cross-user / cross-PC history separately
109 /// (operator-only `executions` table).
110 #[serde(default, skip_serializing_if = "Option::is_none")]
111 pub last_run: Option<JobRun>,
112}
113
114/// Compact summary of a past run — what the Client App shows next
115/// to the job's "Run again" button.
116#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
117pub struct JobRun {
118 pub run_id: String,
119 pub status: RunStatus,
120 pub started_at: chrono::DateTime<chrono::Utc>,
121 #[serde(default, skip_serializing_if = "Option::is_none")]
122 pub finished_at: Option<chrono::DateTime<chrono::Utc>>,
123 #[serde(default, skip_serializing_if = "Option::is_none")]
124 pub exit_code: Option<i32>,
125}
126
127// ---------- jobs.list ----------
128
129/// `jobs.list` params — optional category filter (when the Client
130/// App is showing a single tab and doesn't want the full set).
131#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
132pub struct JobsListParams {
133 /// `None` ⇒ return every user-invokable job. `Some(key)` ⇒ filter
134 /// to that category key. The agent always strips
135 /// `user_invokable: false` manifests regardless of filter.
136 #[serde(default, skip_serializing_if = "Option::is_none")]
137 pub category: Option<String>,
138}
139
140#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
141pub struct JobsListResult {
142 pub items: Vec<UserInvokableJob>,
143}
144
145// ---------- jobs.execute ----------
146
147/// `jobs.execute` params — the manifest id to run. Agent looks up
148/// the manifest from KV at fire time, so a change to
149/// `user_invokable` takes effect on the next execute attempt (SPEC
150/// §2.1: "Agent 側で manifest を必ず再 lookup し、`user_invokable:
151/// false` への変更が即時反映される").
152#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
153pub struct JobsExecuteParams {
154 /// Manifest id from `jobs.list[].id`.
155 pub id: String,
156}
157
158#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
159pub struct JobsExecuteResult {
160 /// Agent-minted UUID for this specific run. Carried back to the
161 /// caller so they can correlate the `jobs.progress` pushes that
162 /// follow + later `jobs.kill` calls.
163 pub run_id: String,
164}
165
166// ---------- jobs.subscribe ----------
167
168#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
169pub struct JobsSubscribeParams {}
170
171#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
172pub struct JobsSubscribeResult {
173 pub subscription: String,
174}
175
176#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
177pub struct JobsUnsubscribeParams {
178 pub subscription: String,
179}
180
181// ---------- jobs.progress (push) ----------
182
183/// Push payload for `jobs.progress`. Sent on:
184/// - first move from Queued → Running
185/// - each stdout / stderr chunk (split to fit the 1 MiB framing
186/// cap — SPEC §2.12.2)
187/// - terminal state transition (Completed / Failed / Killed) with
188/// `exit_code` populated
189///
190/// The reference shape is SPEC §2.12.11's `JobProgress` struct.
191#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
192pub struct JobProgress {
193 /// The `run_id` minted by `jobs.execute`.
194 pub run_id: String,
195 pub status: RunStatus,
196 /// Newly-produced stdout, UTF-8 decoded (tolerant — see
197 /// `kanade-agent::process::capture_tolerant`). `None` when this
198 /// push is a pure status transition; `Some("")` would never be
199 /// emitted (the agent omits the field instead).
200 #[serde(default, skip_serializing_if = "Option::is_none")]
201 pub stdout_chunk: Option<String>,
202 /// Newly-produced stderr. Same conventions as `stdout_chunk`.
203 #[serde(default, skip_serializing_if = "Option::is_none")]
204 pub stderr_chunk: Option<String>,
205 /// Populated on the terminal push only. Agents stamp the actual
206 /// process exit code from the child. Synthetic non-process
207 /// outcomes (timeout, remote kill) are surfaced as `Some(-1)`
208 /// with the `status` field carrying the distinguishing
209 /// information (`Failed` / `Killed`), not via a reserved
210 /// exit-code number.
211 ///
212 /// Note: the sibling `ExecResult` wire (manifest exec →
213 /// backend, NOT this KLP flow) DOES partition synthetic skip
214 /// codes (124 / 125 / 126 / 127) for the agent's pre-exec
215 /// staleness gates. See the doc on
216 /// `kanade-agent::commands::publish_staleness_skipped` for
217 /// the table. JobProgress's exit_code does not share that
218 /// partition.
219 #[serde(default, skip_serializing_if = "Option::is_none")]
220 pub exit_code: Option<i32>,
221}
222
223// ---------- jobs.kill ----------
224
225/// `jobs.kill` params — `run_id` from this connection's earlier
226/// `jobs.execute` call. SPEC §2.12.4 forbids cross-connection kill
227/// (agent returns `Unauthorized`); a user wanting to stop another
228/// user's job goes through the operator SPA, not the Client App.
229#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
230pub struct JobsKillParams {
231 pub run_id: String,
232}
233
234#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
235pub struct JobsKillResult {
236 /// Wall-clock the agent dispatched the kill signal. The
237 /// terminal `jobs.progress` push (status = `Killed`) follows
238 /// asynchronously once the child process actually exits.
239 pub requested_at: chrono::DateTime<chrono::Utc>,
240}
241
242#[cfg(test)]
243mod tests {
244 use super::*;
245 use chrono::TimeZone;
246
247 #[test]
248 fn user_invokable_job_carries_free_form_category() {
249 // #792: category is a free-form string + optional tab metadata.
250 let wire = r#"{
251 "id":"wifi-tweak","display_name":"Wi-Fi 省電力を切る",
252 "category":"settings","category_label":"設定",
253 "category_icon":"settings","category_order":15,"version":"1.0.0"
254 }"#;
255 let j: UserInvokableJob = serde_json::from_str(wire).unwrap();
256 assert_eq!(j.category, "settings");
257 assert_eq!(j.category_label.as_deref(), Some("設定"));
258 assert_eq!(j.category_icon.as_deref(), Some("settings"));
259 assert_eq!(j.category_order, Some(15));
260 }
261
262 #[test]
263 fn run_status_serialises_snake_case() {
264 for (variant, expected) in [
265 (RunStatus::Queued, "\"queued\""),
266 (RunStatus::Running, "\"running\""),
267 (RunStatus::Completed, "\"completed\""),
268 (RunStatus::Failed, "\"failed\""),
269 (RunStatus::Killed, "\"killed\""),
270 ] {
271 let s = serde_json::to_string(&variant).unwrap();
272 assert_eq!(s, expected, "encode {variant:?}");
273 let back: RunStatus = serde_json::from_str(expected).unwrap();
274 assert_eq!(back, variant, "round-trip {expected}");
275 }
276 }
277
278 #[test]
279 fn user_invokable_job_minimum_shape_decodes() {
280 // Backend that hasn't fully populated `display_description`
281 // / `icon` / `last_run` must still produce decodable rows.
282 let wire = r#"{
283 "id":"chrome-update","display_name":"Chrome を更新",
284 "category":"software_update","version":"1.2.0"
285 }"#;
286 let j: UserInvokableJob = serde_json::from_str(wire).unwrap();
287 assert_eq!(j.id, "chrome-update");
288 assert!(j.display_description.is_none());
289 assert!(j.icon.is_none());
290 assert!(j.last_run.is_none());
291 }
292
293 #[test]
294 fn job_progress_status_transition_omits_chunks() {
295 // Status-only push (Queued → Running) has neither stdout
296 // nor stderr; both fields must be absent from the wire, not
297 // null. Strict JS clients reject `null` strings.
298 let p = JobProgress {
299 run_id: "run-1".into(),
300 status: RunStatus::Running,
301 stdout_chunk: None,
302 stderr_chunk: None,
303 exit_code: None,
304 };
305 let v = serde_json::to_value(&p).unwrap();
306 assert!(v.get("stdout_chunk").is_none(), "wire: {v:?}");
307 assert!(v.get("stderr_chunk").is_none(), "wire: {v:?}");
308 assert!(v.get("exit_code").is_none(), "wire: {v:?}");
309 }
310
311 #[test]
312 fn job_progress_terminal_push_carries_exit_code() {
313 let p = JobProgress {
314 run_id: "run-1".into(),
315 status: RunStatus::Completed,
316 stdout_chunk: None,
317 stderr_chunk: None,
318 exit_code: Some(0),
319 };
320 let v = serde_json::to_value(&p).unwrap();
321 assert_eq!(v["status"], "completed");
322 assert_eq!(v["exit_code"], 0);
323 }
324
325 #[test]
326 fn jobs_list_filter_optional() {
327 // No filter ⇒ all categories. Wire form has no `category`
328 // key, not `category: null`.
329 let p = JobsListParams::default();
330 let v = serde_json::to_value(&p).unwrap();
331 assert!(v.get("category").is_none(), "wire: {v:?}");
332 }
333
334 #[test]
335 fn jobs_execute_result_round_trips() {
336 let r = JobsExecuteResult {
337 run_id: "run-uuid-1".into(),
338 };
339 let json = serde_json::to_string(&r).unwrap();
340 let back: JobsExecuteResult = serde_json::from_str(&json).unwrap();
341 assert_eq!(back.run_id, "run-uuid-1");
342 }
343
344 #[test]
345 fn job_run_serialises_with_optional_finish() {
346 // In-flight run: started_at present, finished_at + exit_code
347 // absent. Critical because the Client App's "last run" chip
348 // uses `finished_at.is_some()` as the "row is terminal" flag.
349 let r = JobRun {
350 run_id: "run-1".into(),
351 status: RunStatus::Running,
352 started_at: chrono::Utc.with_ymd_and_hms(2026, 5, 24, 0, 0, 0).unwrap(),
353 finished_at: None,
354 exit_code: None,
355 };
356 let v = serde_json::to_value(&r).unwrap();
357 assert!(v.get("finished_at").is_none(), "wire: {v:?}");
358 assert!(v.get("exit_code").is_none(), "wire: {v:?}");
359 }
360
361 #[test]
362 fn unknown_enum_variants_decode_to_unknown() {
363 // #492: a newer peer's new variant must not make this build
364 // fail to decode the whole containing message — serde(other)
365 // catches it (non_exhaustive alone never protected the wire).
366 let s: RunStatus = serde_json::from_str("\"skipped\"").unwrap();
367 assert_eq!(s, RunStatus::Unknown);
368 // Known variants are untouched.
369 let r: RunStatus = serde_json::from_str("\"running\"").unwrap();
370 assert_eq!(r, RunStatus::Running);
371 }
372
373 #[test]
374 fn unknown_variant_round_trips() {
375 // PR #558 review (gemini): Unknown must SERIALIZE cleanly too
376 // — a node that decoded a newer peer's variant and re-emits
377 // the containing message (e.g. an agent forwarding a
378 // jobs.list entry) must not hit a runtime serialization
379 // error. It serialises as "unknown" and decodes back to
380 // Unknown on every #492-aware peer.
381 let s = serde_json::to_string(&RunStatus::Unknown).unwrap();
382 assert_eq!(s, "\"unknown\"");
383 let back: RunStatus = serde_json::from_str(&s).unwrap();
384 assert_eq!(back, RunStatus::Unknown);
385 }
386
387 #[test]
388 fn unknown_decodes_across_the_other_wire_enums() {
389 // PR #558 review (claude): cover the remaining #492 enums.
390 use crate::ipc::error::ErrorKind;
391 use crate::ipc::maintenance::DeferDuration;
392 use crate::ipc::notifications::NotificationPriority;
393
394 let k: ErrorKind = serde_json::from_str("\"FutureErrorKind\"").unwrap();
395 assert_eq!(k, ErrorKind::Unknown);
396 assert_eq!(k.code(), -32099);
397 let known: ErrorKind = serde_json::from_str("\"Unauthorized\"").unwrap();
398 assert_eq!(known, ErrorKind::Unauthorized);
399
400 let p: NotificationPriority = serde_json::from_str("\"critical\"").unwrap();
401 assert_eq!(p, NotificationPriority::Unknown);
402
403 let d: DeferDuration = serde_json::from_str("\"2h\"").unwrap();
404 assert_eq!(d, DeferDuration::Unknown);
405 assert_eq!(d.as_duration(), chrono::Duration::minutes(15));
406 }
407}