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 /// `Manifest.execute.timeout` lowered to whole seconds (#865). The
107 /// agent kills the run at this deadline, so the Client App's
108 /// stuck-run watchdog uses `timeout_secs + grace` instead of a fixed
109 /// 15 min — a near-silent job (e.g. Office repair) is no longer
110 /// falsely marked failed while it's still running. `None` from an
111 /// older agent that predates this field ⇒ the client falls back to
112 /// the fixed watchdog (#492 wire rule: `serde(default)` +
113 /// `skip_serializing_if`).
114 #[serde(default, skip_serializing_if = "Option::is_none")]
115 pub timeout_secs: Option<u64>,
116 /// Snapshot of the last KLP-driven run of this job FOR THIS
117 /// USER. `None` until they've executed it at least once.
118 /// Backend keeps the cross-user / cross-PC history separately
119 /// (operator-only `executions` table).
120 #[serde(default, skip_serializing_if = "Option::is_none")]
121 pub last_run: Option<JobRun>,
122}
123
124/// Compact summary of a past run — what the Client App shows next
125/// to the job's "Run again" button.
126#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
127pub struct JobRun {
128 pub run_id: String,
129 pub status: RunStatus,
130 pub started_at: chrono::DateTime<chrono::Utc>,
131 #[serde(default, skip_serializing_if = "Option::is_none")]
132 pub finished_at: Option<chrono::DateTime<chrono::Utc>>,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
134 pub exit_code: Option<i32>,
135}
136
137// ---------- jobs.list ----------
138
139/// `jobs.list` params — optional category filter (when the Client
140/// App is showing a single tab and doesn't want the full set).
141#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
142pub struct JobsListParams {
143 /// `None` ⇒ return every user-invokable job. `Some(key)` ⇒ filter
144 /// to that category key. The agent always strips
145 /// `user_invokable: false` manifests regardless of filter.
146 #[serde(default, skip_serializing_if = "Option::is_none")]
147 pub category: Option<String>,
148}
149
150#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
151pub struct JobsListResult {
152 pub items: Vec<UserInvokableJob>,
153}
154
155// ---------- jobs.execute ----------
156
157/// `jobs.execute` params — the manifest id to run. Agent looks up
158/// the manifest from KV at fire time, so a change to
159/// `user_invokable` takes effect on the next execute attempt (SPEC
160/// §2.1: "Agent 側で manifest を必ず再 lookup し、`user_invokable:
161/// false` への変更が即時反映される").
162#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
163pub struct JobsExecuteParams {
164 /// Manifest id from `jobs.list[].id`.
165 pub id: String,
166}
167
168#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
169pub struct JobsExecuteResult {
170 /// Agent-minted UUID for this specific run. Carried back to the
171 /// caller so they can correlate the `jobs.progress` pushes that
172 /// follow + later `jobs.kill` calls.
173 pub run_id: String,
174}
175
176// ---------- jobs.subscribe ----------
177
178#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
179pub struct JobsSubscribeParams {}
180
181#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
182pub struct JobsSubscribeResult {
183 pub subscription: String,
184}
185
186#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
187pub struct JobsUnsubscribeParams {
188 pub subscription: String,
189}
190
191// ---------- jobs.progress (push) ----------
192
193/// Push payload for `jobs.progress`. Sent on:
194/// - first move from Queued → Running
195/// - each stdout / stderr chunk (split to fit the 1 MiB framing
196/// cap — SPEC §2.12.2)
197/// - terminal state transition (Completed / Failed / Killed) with
198/// `exit_code` populated
199///
200/// The reference shape is SPEC §2.12.11's `JobProgress` struct.
201#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
202pub struct JobProgress {
203 /// The `run_id` minted by `jobs.execute`.
204 pub run_id: String,
205 pub status: RunStatus,
206 /// Newly-produced stdout, UTF-8 decoded (tolerant — see
207 /// `kanade-agent::process::capture_tolerant`). `None` when this
208 /// push is a pure status transition; `Some("")` would never be
209 /// emitted (the agent omits the field instead).
210 #[serde(default, skip_serializing_if = "Option::is_none")]
211 pub stdout_chunk: Option<String>,
212 /// Newly-produced stderr. Same conventions as `stdout_chunk`.
213 #[serde(default, skip_serializing_if = "Option::is_none")]
214 pub stderr_chunk: Option<String>,
215 /// Populated on the terminal push only. Agents stamp the actual
216 /// process exit code from the child. Synthetic non-process
217 /// outcomes (timeout, remote kill) are surfaced as `Some(-1)`
218 /// with the `status` field carrying the distinguishing
219 /// information (`Failed` / `Killed`), not via a reserved
220 /// exit-code number.
221 ///
222 /// Note: the sibling `ExecResult` wire (manifest exec →
223 /// backend, NOT this KLP flow) DOES partition synthetic skip
224 /// codes (124 / 125 / 126 / 127) for the agent's pre-exec
225 /// staleness gates. See the doc on
226 /// `kanade-agent::commands::publish_staleness_skipped` for
227 /// the table. JobProgress's exit_code does not share that
228 /// partition.
229 #[serde(default, skip_serializing_if = "Option::is_none")]
230 pub exit_code: Option<i32>,
231}
232
233// ---------- jobs.kill ----------
234
235/// `jobs.kill` params — `run_id` from this connection's earlier
236/// `jobs.execute` call. SPEC §2.12.4 forbids cross-connection kill
237/// (agent returns `Unauthorized`); a user wanting to stop another
238/// user's job goes through the operator SPA, not the Client App.
239#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
240pub struct JobsKillParams {
241 pub run_id: String,
242}
243
244#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
245pub struct JobsKillResult {
246 /// Wall-clock the agent dispatched the kill signal. The
247 /// terminal `jobs.progress` push (status = `Killed`) follows
248 /// asynchronously once the child process actually exits.
249 pub requested_at: chrono::DateTime<chrono::Utc>,
250}
251
252#[cfg(test)]
253mod tests {
254 use super::*;
255 use chrono::TimeZone;
256
257 #[test]
258 fn user_invokable_job_carries_free_form_category() {
259 // #792: category is a free-form string + optional tab metadata.
260 let wire = r#"{
261 "id":"wifi-tweak","display_name":"Wi-Fi 省電力を切る",
262 "category":"settings","category_label":"設定",
263 "category_icon":"settings","category_order":15,"version":"1.0.0"
264 }"#;
265 let j: UserInvokableJob = serde_json::from_str(wire).unwrap();
266 assert_eq!(j.category, "settings");
267 assert_eq!(j.category_label.as_deref(), Some("設定"));
268 assert_eq!(j.category_icon.as_deref(), Some("settings"));
269 assert_eq!(j.category_order, Some(15));
270 }
271
272 #[test]
273 fn run_status_serialises_snake_case() {
274 for (variant, expected) in [
275 (RunStatus::Queued, "\"queued\""),
276 (RunStatus::Running, "\"running\""),
277 (RunStatus::Completed, "\"completed\""),
278 (RunStatus::Failed, "\"failed\""),
279 (RunStatus::Killed, "\"killed\""),
280 ] {
281 let s = serde_json::to_string(&variant).unwrap();
282 assert_eq!(s, expected, "encode {variant:?}");
283 let back: RunStatus = serde_json::from_str(expected).unwrap();
284 assert_eq!(back, variant, "round-trip {expected}");
285 }
286 }
287
288 #[test]
289 fn user_invokable_job_minimum_shape_decodes() {
290 // Backend that hasn't fully populated `display_description`
291 // / `icon` / `last_run` must still produce decodable rows.
292 let wire = r#"{
293 "id":"chrome-update","display_name":"Chrome を更新",
294 "category":"software_update","version":"1.2.0"
295 }"#;
296 let j: UserInvokableJob = serde_json::from_str(wire).unwrap();
297 assert_eq!(j.id, "chrome-update");
298 assert!(j.display_description.is_none());
299 assert!(j.icon.is_none());
300 assert!(j.last_run.is_none());
301 // #865: an older agent omits timeout_secs ⇒ None (client falls
302 // back to its fixed watchdog).
303 assert!(j.timeout_secs.is_none());
304 }
305
306 #[test]
307 fn user_invokable_job_timeout_secs_round_trips() {
308 // #865: present on the wire ⇒ carried through; absent ⇒ omitted
309 // from the encoded form (not `null`), per the #492 wire rule.
310 let wire = r#"{
311 "id":"office-repair","display_name":"Office 修復",
312 "category":"troubleshoot","version":"1.0.0","timeout_secs":3600
313 }"#;
314 let j: UserInvokableJob = serde_json::from_str(wire).unwrap();
315 assert_eq!(j.timeout_secs, Some(3600));
316
317 let v = serde_json::to_value(UserInvokableJob {
318 id: "x".into(),
319 display_name: "X".into(),
320 display_description: None,
321 icon: None,
322 category: "catalog".into(),
323 category_label: None,
324 category_icon: None,
325 category_order: None,
326 version: "1.0.0".into(),
327 timeout_secs: None,
328 last_run: None,
329 })
330 .unwrap();
331 assert!(v.get("timeout_secs").is_none(), "wire: {v:?}");
332 }
333
334 #[test]
335 fn job_progress_status_transition_omits_chunks() {
336 // Status-only push (Queued → Running) has neither stdout
337 // nor stderr; both fields must be absent from the wire, not
338 // null. Strict JS clients reject `null` strings.
339 let p = JobProgress {
340 run_id: "run-1".into(),
341 status: RunStatus::Running,
342 stdout_chunk: None,
343 stderr_chunk: None,
344 exit_code: None,
345 };
346 let v = serde_json::to_value(&p).unwrap();
347 assert!(v.get("stdout_chunk").is_none(), "wire: {v:?}");
348 assert!(v.get("stderr_chunk").is_none(), "wire: {v:?}");
349 assert!(v.get("exit_code").is_none(), "wire: {v:?}");
350 }
351
352 #[test]
353 fn job_progress_terminal_push_carries_exit_code() {
354 let p = JobProgress {
355 run_id: "run-1".into(),
356 status: RunStatus::Completed,
357 stdout_chunk: None,
358 stderr_chunk: None,
359 exit_code: Some(0),
360 };
361 let v = serde_json::to_value(&p).unwrap();
362 assert_eq!(v["status"], "completed");
363 assert_eq!(v["exit_code"], 0);
364 }
365
366 #[test]
367 fn jobs_list_filter_optional() {
368 // No filter ⇒ all categories. Wire form has no `category`
369 // key, not `category: null`.
370 let p = JobsListParams::default();
371 let v = serde_json::to_value(&p).unwrap();
372 assert!(v.get("category").is_none(), "wire: {v:?}");
373 }
374
375 #[test]
376 fn jobs_execute_result_round_trips() {
377 let r = JobsExecuteResult {
378 run_id: "run-uuid-1".into(),
379 };
380 let json = serde_json::to_string(&r).unwrap();
381 let back: JobsExecuteResult = serde_json::from_str(&json).unwrap();
382 assert_eq!(back.run_id, "run-uuid-1");
383 }
384
385 #[test]
386 fn job_run_serialises_with_optional_finish() {
387 // In-flight run: started_at present, finished_at + exit_code
388 // absent. Critical because the Client App's "last run" chip
389 // uses `finished_at.is_some()` as the "row is terminal" flag.
390 let r = JobRun {
391 run_id: "run-1".into(),
392 status: RunStatus::Running,
393 started_at: chrono::Utc.with_ymd_and_hms(2026, 5, 24, 0, 0, 0).unwrap(),
394 finished_at: None,
395 exit_code: None,
396 };
397 let v = serde_json::to_value(&r).unwrap();
398 assert!(v.get("finished_at").is_none(), "wire: {v:?}");
399 assert!(v.get("exit_code").is_none(), "wire: {v:?}");
400 }
401
402 #[test]
403 fn unknown_enum_variants_decode_to_unknown() {
404 // #492: a newer peer's new variant must not make this build
405 // fail to decode the whole containing message — serde(other)
406 // catches it (non_exhaustive alone never protected the wire).
407 let s: RunStatus = serde_json::from_str("\"skipped\"").unwrap();
408 assert_eq!(s, RunStatus::Unknown);
409 // Known variants are untouched.
410 let r: RunStatus = serde_json::from_str("\"running\"").unwrap();
411 assert_eq!(r, RunStatus::Running);
412 }
413
414 #[test]
415 fn unknown_variant_round_trips() {
416 // PR #558 review (gemini): Unknown must SERIALIZE cleanly too
417 // — a node that decoded a newer peer's variant and re-emits
418 // the containing message (e.g. an agent forwarding a
419 // jobs.list entry) must not hit a runtime serialization
420 // error. It serialises as "unknown" and decodes back to
421 // Unknown on every #492-aware peer.
422 let s = serde_json::to_string(&RunStatus::Unknown).unwrap();
423 assert_eq!(s, "\"unknown\"");
424 let back: RunStatus = serde_json::from_str(&s).unwrap();
425 assert_eq!(back, RunStatus::Unknown);
426 }
427
428 #[test]
429 fn unknown_decodes_across_the_other_wire_enums() {
430 // PR #558 review (claude): cover the remaining #492 enums.
431 use crate::ipc::error::ErrorKind;
432 use crate::ipc::maintenance::DeferDuration;
433 use crate::ipc::notifications::NotificationPriority;
434
435 let k: ErrorKind = serde_json::from_str("\"FutureErrorKind\"").unwrap();
436 assert_eq!(k, ErrorKind::Unknown);
437 assert_eq!(k.code(), -32099);
438 let known: ErrorKind = serde_json::from_str("\"Unauthorized\"").unwrap();
439 assert_eq!(known, ErrorKind::Unauthorized);
440
441 let p: NotificationPriority = serde_json::from_str("\"critical\"").unwrap();
442 assert_eq!(p, NotificationPriority::Unknown);
443
444 let d: DeferDuration = serde_json::from_str("\"2h\"").unwrap();
445 assert_eq!(d, DeferDuration::Unknown);
446 assert_eq!(d.as_duration(), chrono::Duration::minutes(15));
447 }
448}