kanade_shared/manifest.rs
1use serde::{Deserialize, Serialize};
2
3use crate::ipc::jobs::JobCategory;
4use crate::wire::{RunAs, Shell, Staleness};
5
6/// YAML job manifest (= registered "what to run", v0.18.0+).
7///
8/// Owns only script-intrinsic fields. **Who** (`target`), **how to
9/// phase fanout** (`rollout`), and **when to stagger start**
10/// (`jitter`) all moved to the Schedule / exec request side — same
11/// script can now be fired against different targets / rollouts
12/// without copying the script body.
13///
14/// #492: these types are READ fleet-wide (agents decode them from
15/// BUCKET_JOBS / BUCKET_SCHEDULES and inside live Commands), so they
16/// must tolerate unknown fields — `deny_unknown_fields` here made a
17/// gradually-upgrading fleet's OLD agents reject the whole object
18/// the moment a newer backend added any field. Operator typo
19/// protection (the old reason for the attribute) lives at the WRITE
20/// boundaries instead: `kanade job/schedule create` and the backend
21/// POST extractor parse via [`crate::strict`], which rejects unknown
22/// keys with their full paths. The wire rule: new fields always get
23/// `#[serde(default)]` (+ `skip_serializing_if` while old readers
24/// may still be strict).
25#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
26pub struct Manifest {
27 pub id: String,
28 pub version: String,
29 #[serde(default)]
30 pub description: Option<String>,
31 pub execute: Execute,
32 #[serde(default)]
33 pub require_approval: bool,
34 /// Opt-in marker that this job produces a JSON inventory fact
35 /// payload on stdout. When present, the backend's results
36 /// projector parses `ExecResult.stdout` as JSON and upserts an
37 /// `inventory_facts` row keyed by `(pc_id, manifest.id)`. The
38 /// `display` sub-config drives the SPA's Inventory page render.
39 #[serde(default)]
40 pub inventory: Option<InventoryHint>,
41 /// Issue #246: opt-in marker that this job emits per-line
42 /// observability events on stdout (one JSON `ObsEvent` per
43 /// newline). When present, the agent — after the script exits
44 /// successfully — parses each non-empty stdout line as an
45 /// `ObsEvent`, publishes it on `obs.<pc_id>` via the
46 /// `obs_outbox`, and (intentionally) **omits the stdout from
47 /// the `ExecResult`** so the timeline data doesn't double up
48 /// in `execution_results.stdout` (which would multiply rows
49 /// by ~50/day/PC of noise).
50 ///
51 /// Distinct from `inventory:` (single JSON object → projector
52 /// upsert) — events are append-only timeline points consumed
53 /// by the dedicated `obs_events` table.
54 #[serde(default)]
55 pub emit: Option<EmitConfig>,
56 /// #290: opt-in marker that this job is an operator-defined
57 /// **health check** whose result feeds the Client App's Health
58 /// tab over KLP (`StateSnapshot.checks`). The script prints a
59 /// free-form JSON object on stdout (like any inventory job); the
60 /// agent reads the [`CheckHint::status_field`] value dynamically
61 /// into a [`crate::ipc::state::Check`] named `check.name`.
62 /// Cadence / windows / conditions come from
63 /// the job's Schedule (exactly like inventory) — there is
64 /// deliberately no interval here. **Composes with `inventory:`**:
65 /// the script's stdout is one JSON object, so a check can also
66 /// carry an `inventory:` block to project the rest of that object
67 /// (incl. `explode` sub-tables) for SPA fleet-querying. Only
68 /// `emit:` (NDJSON stdout) is incompatible.
69 #[serde(default)]
70 pub check: Option<CheckHint>,
71 /// #219: opt-in marker that this job COLLECTS files into a bundle.
72 /// The script does the collection work and prints a single JSON
73 /// object on stdout carrying a `files` array of paths (the field
74 /// name is [`CollectHint::files_field`], default `"files"`); the
75 /// agent — after the script exits successfully — zips those files,
76 /// uploads the archive to the `OBJECT_COLLECTIONS` Object Store
77 /// bucket (key `<pc_id>/<job_id>/<timestamp>.zip`), and records the
78 /// key in [`crate::wire::ExecResult::collect_object`]. The operator
79 /// downloads bundles from the SPA Collect page.
80 ///
81 /// Like `inventory:` / `check:` this reads a JSON object from stdout,
82 /// but it consumes that stdout for its OWN contract (a `files`
83 /// list), so it is mutually exclusive with `inventory:` / `check:` /
84 /// `emit:` (enforced in [`Manifest::validate`]). It composes with
85 /// `client:` — a `collect:` + `client:` job lets an end user trigger
86 /// a collection from the Client App (the same-host agent runs it).
87 #[serde(default, skip_serializing_if = "Option::is_none")]
88 pub collect: Option<CollectHint>,
89 /// #720: opt-in declarative aggregation over `obs_events` that drives
90 /// the SPA **Analytics** page. Unlike the other hints this one never
91 /// touches stdout and is never delivered to the agent — it's a pure
92 /// *read spec* the backend reads from `BUCKET_JOBS` at query time and
93 /// turns into `json_extract` aggregation SQL. Each entry is one widget
94 /// (a `dashboard:` tab groups them); `scope:` selects per-PC vs
95 /// fleet-wide rollup. Because it consumes nothing at run time it
96 /// composes with every other hint (typically paired with `emit:`,
97 /// which produces the events it reads). See [`AggregateWidget`].
98 ///
99 /// New field ⇒ #492 wire rule (`default` + `skip_serializing_if`).
100 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub aggregate: Option<Vec<AggregateWidget>>,
102 /// v0.26: Layer 2 staleness policy (SPEC.md §2.6.2). Controls
103 /// what the agent does at fire time when it can't verify the
104 /// `script_current` / `script_status` KV values are fresh —
105 /// especially relevant for `runs_on: agent` schedules where
106 /// the agent may fire from cache while offline. Defaults to
107 /// `Staleness::Cached` (silently use cached values), which
108 /// matches every pre-v0.26 Manifest.
109 #[serde(default)]
110 pub staleness: Staleness,
111 /// #291: opt-in marker that this job is offered to **end users**
112 /// in the Client App's job tabs over KLP (`jobs.list` →
113 /// `jobs.execute`). Parallel to [`inventory`] / [`check`] /
114 /// [`emit`]: the block's mere presence is the opt-in, and it
115 /// groups the end-user presentation fields (name / category /
116 /// icon) that only make sense for a user-facing job. `None`
117 /// (the default) ⇒ an operator-only job — inventory, checks,
118 /// scheduled maintenance — that never surfaces in the catalog.
119 ///
120 /// The agent re-reads this at every `jobs.list` / `jobs.execute`
121 /// (SPEC §2.1), so removing the block takes a job out of a
122 /// running client on its next action.
123 ///
124 /// [`inventory`]: Manifest::inventory
125 /// [`check`]: Manifest::check
126 /// [`emit`]: Manifest::emit
127 #[serde(default, skip_serializing_if = "Option::is_none")]
128 pub client: Option<ClientHint>,
129 /// Free-form operator taxonomy for the Jobs catalog. Purely a
130 /// SPA-side organisational aid — agents / scheduler / projector
131 /// never read it — so it carries no runtime semantics and any
132 /// string is allowed (`security`, `weekly`, `windows`, …). Jobs
133 /// cross-cut (a `check-bitlocker` is at once a health-check, a
134 /// security control, and Windows-specific), which is why this is
135 /// a multi-valued list rather than the single closed-enum
136 /// [`ClientHint::category`] (whose values are the end-user Client
137 /// App's tabs, a different concern). The operator Jobs page groups
138 /// rows by id-prefix for free; tags add the orthogonal filter axis
139 /// prefixes can't express.
140 ///
141 /// Empty by default (the overwhelming majority of jobs), and a
142 /// new field, so it follows the #492 wire rule: `serde(default)`
143 /// plus `skip_serializing_if` keep gradually-upgrading old readers
144 /// from tripping over its absence / presence.
145 #[serde(default, skip_serializing_if = "Vec::is_empty")]
146 pub tags: Vec<String>,
147 /// GitOps provenance (#678) — see [`RepoOrigin`]. Stamped by
148 /// `kanade job create` when the source YAML lives inside a Git work
149 /// tree, so the SPA can render the job read-only and point edits
150 /// back at the repo instead of letting a ClickOps edit silently
151 /// diverge from Git (SPEC design principle #3: 設定駆動 YAML + Git).
152 /// `None` for SPA-born jobs and for manifests applied from outside
153 /// any Git repo. Purely informational: agents / scheduler /
154 /// projector never read it, and it survives `script_file:` inlining
155 /// (it's orthogonal to the exactly-one-of script-source rule). New
156 /// field ⇒ #492 wire rule (`default` + `skip_serializing_if`).
157 #[serde(default, skip_serializing_if = "Option::is_none")]
158 pub origin: Option<RepoOrigin>,
159}
160
161/// GitOps provenance for a repo-managed YAML artifact — a [`Manifest`]
162/// (#678) or a [`Schedule`] (#695). Populated by `kanade job create` /
163/// `kanade schedule create` from the Git context of the source YAML;
164/// the SPA reads it to render Git-managed entries read-only and link
165/// the operator back at the repo. Never consulted by the runtime.
166#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
167pub struct RepoOrigin {
168 /// Repo-relative path of the source YAML — the primary edit target
169 /// the SPA surfaces (e.g. `configs/jobs/foo.yaml`). Forward slashes
170 /// regardless of the authoring OS.
171 pub path: String,
172 /// `origin` remote URL, when the repo has one. Lets the SPA turn
173 /// `path` into a clickable link; `None` for remote-less repos.
174 #[serde(default, skip_serializing_if = "Option::is_none")]
175 pub repo: Option<String>,
176 /// Repo-relative path of the `script_file:` a job manifest inlined,
177 /// when it used one — a secondary pointer shown beneath `path`.
178 /// Always `None` for schedules (they carry no script).
179 #[serde(default, skip_serializing_if = "Option::is_none")]
180 pub script_file: Option<String>,
181}
182
183/// "Who + how + when-to-stagger" — the fanout-plan side of an exec.
184/// Used both as the POST `/api/exec/{job_id}` body and as the embedded
185/// `target` / `rollout` / `jitter` slot on [`Schedule`]. Centralising
186/// here keeps the validation + serialisation logic in one place.
187#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
188pub struct FanoutPlan {
189 #[serde(default)]
190 pub target: Target,
191 /// Optional wave rollout — when present, the backend publishes
192 /// each wave's group subject on its own delay schedule instead
193 /// of fanning out the `target` block in one go. `target` then
194 /// only labels the deploy for the audit log.
195 #[serde(default, skip_serializing_if = "Option::is_none")]
196 pub rollout: Option<Rollout>,
197 /// Optional humantime jitter; agent uses it to randomise
198 /// execution start. Lives here (not on the script) so different
199 /// schedules / ad-hoc fires of the same job can pick different
200 /// stagger windows.
201 #[serde(default, skip_serializing_if = "Option::is_none")]
202 pub jitter: Option<String>,
203 /// Absolute time the scheduler stamps on each emitted Command
204 /// when this exec was driven by a [`Schedule`] with
205 /// `starting_deadline`. Agents receiving a Command after this
206 /// instant publish a synthetic skipped-result instead of
207 /// running the script. `None` (default) = no deadline / catch
208 /// up whenever delivered. Operators don't usually set this
209 /// directly — the scheduler computes it from `tick_at +
210 /// starting_deadline`.
211 #[serde(default, skip_serializing_if = "Option::is_none")]
212 pub deadline_at: Option<chrono::DateTime<chrono::Utc>>,
213}
214
215/// Manifest sub-section: how the SPA should render the inventory
216/// facts this job produces. Each field name (`field`) is a top-level
217/// key in the stdout JSON, e.g. `hostname`, `ram_gb`.
218///
219/// Two render modes:
220/// * `display` — vertical "field / value" per PC, used by the
221/// `/inventory?pc=<id>` detail view. ALL columns the operator
222/// wants visible on the detail page.
223/// * `summary` — horizontal table across the fleet (row = PC,
224/// column = field) on `/inventory`. Optional; when omitted the
225/// SPA falls back to `display`, but operators usually want a
226/// trimmer "hostname / OS / CPU / RAM" set for the fleet view.
227#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
228pub struct InventoryHint {
229 /// Detail-view columns, in order.
230 pub display: Vec<DisplayField>,
231 /// Optional fleet-list columns (row = PC). Defaults to `display`
232 /// when omitted, but operators usually pick a 3-5 column subset.
233 #[serde(default, skip_serializing_if = "Option::is_none")]
234 pub summary: Option<Vec<DisplayField>>,
235 /// v0.31 / #40: payload arrays that should be exploded into
236 /// per-element rows of a derived SQLite table. Lets operators
237 /// answer cross-PC questions ("which PCs still have Chrome <
238 /// 120?", "C: >90% full") with normal SQL filters + indexes
239 /// instead of grepping JSON. The projector creates the derived
240 /// table on register and replaces this PC's rows on each result
241 /// (DELETE WHERE pc_id=? AND job_id=? + bulk INSERT). See
242 /// [`ExplodeSpec`] for the per-spec schema.
243 #[serde(default, skip_serializing_if = "Option::is_none")]
244 pub explode: Option<Vec<ExplodeSpec>>,
245 /// v0.35 / #93: top-level scalar fields whose changes the
246 /// projector logs to `inventory_history` (one event per
247 /// changed field per scan). Pairs with `explode[].track_history`
248 /// — that covers array elements; this covers single-valued
249 /// fields like `ram_bytes` / `os_version` / `cpu_model` /
250 /// `os_build` that operators want to track for "did the RAM
251 /// get upgraded?" / "when did Win 11 land on this PC?" /
252 /// "BIOS / firmware bumped?" questions. Field name = `field_path`
253 /// in the history row, `identity_json` is NULL, `before_json`
254 /// / `after_json` each carry `{"value": <prior or new value>}`.
255 /// First-ever observation of a scalar (no prior facts row)
256 /// emits `added`; subsequent value changes emit `changed`. No
257 /// `removed` events — a scalar disappearing from the payload
258 /// is rare and the operator can still see the last value via
259 /// the `before_json` of the most recent change.
260 #[serde(default, skip_serializing_if = "Option::is_none")]
261 pub history_scalars: Option<Vec<String>>,
262}
263
264/// Manifest sub-section (#290): marks a job as an operator-defined
265/// **health check**. Parallel to [`InventoryHint`] / `EmitConfig`.
266/// The stdout contract is a free-form JSON object (same as any
267/// inventory job) from which the agent reads `status_field` /
268/// `detail_field` to build the KLP [`crate::ipc::state::Check`] shown
269/// on the Client App's Health tab.
270///
271/// There is deliberately **no timing field** — when / how often /
272/// in which window a check runs is driven by the job's Schedule,
273/// exactly like inventory jobs, so operators get the full `when:` /
274/// rollout / `runs_on` expressiveness for free.
275///
276/// A check's stdout is a **free-form inventory object** (arbitrary
277/// key/value pairs + arrays) — same as any inventory job — that also
278/// carries a status field. `check:` adds only the health semantics on
279/// top: which field is the ok/warn/fail/unknown status, an optional
280/// one-line summary field, and a remediation job. Everything else
281/// (rich per-PC detail, `explode` sub-tables like a software list) is
282/// driven by a co-present [`InventoryHint`] and rendered with the
283/// SAME display logic the SPA Inventory page uses — on the Client App
284/// too. This keeps checks maximally expressive without a bespoke
285/// payload type.
286#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
287pub struct CheckHint {
288 /// Stable check id → [`Check.name`](crate::ipc::state::Check),
289 /// the SPA/Client React key + analytics label. Unique within the
290 /// fleet's check set. Machine-friendly slug (`disk_space`,
291 /// `defender_rtp`); for the human-facing row title see [`label`].
292 ///
293 /// [`label`]: CheckHint::label
294 pub name: String,
295 /// Optional human-facing display title →
296 /// [`Check.label`](crate::ipc::state::Check). The Client App's
297 /// Health tab and the operator SPA's Compliance page render this
298 /// instead of the [`name`](CheckHint::name) slug when set
299 /// (`"ウイルス対策のリアルタイム保護"` reads better than
300 /// `defender_rtp`). Falls back to the slug when absent, so it's
301 /// purely additive. Author it in the check's language — there's no
302 /// per-locale variant; checks are operator-defined per fleet.
303 #[serde(default, skip_serializing_if = "Option::is_none")]
304 pub label: Option<String>,
305 /// Top-level stdout field whose string value
306 /// (`ok`/`warn`/`fail`/`unknown`) becomes the Health-tab light
307 /// ([`CheckStatus`](crate::ipc::state::CheckStatus)). Defaults to
308 /// `"status"`; a missing / unparseable value → `unknown`.
309 #[serde(default = "default_status_field")]
310 pub status_field: String,
311 /// Top-level stdout field used as the Health-tab row's one-line
312 /// summary. Defaults to `"detail"`; absent in the payload → no
313 /// detail line (the rich breakdown lives in the inventory view).
314 #[serde(default = "default_detail_field")]
315 pub detail_field: String,
316 /// Optional remediation job id →
317 /// [`Check.troubleshoot`](crate::ipc::state::Check). The Client
318 /// App shows a "修復する" button when present; that job must be
319 /// `user_invokable`.
320 #[serde(default, skip_serializing_if = "Option::is_none")]
321 pub troubleshoot: Option<String>,
322 /// #290 PR-E: when `true` (default), the backend also projects this
323 /// check's `status` / `detail` into the `check_status` table so the
324 /// operator SPA gets a fleet-wide compliance view for free — no
325 /// `inventory:` block needed. Set `fleet: false` for a client-only
326 /// check the operator doesn't want surfaced across the fleet.
327 #[serde(default = "default_fleet")]
328 pub fleet: bool,
329}
330
331fn default_status_field() -> String {
332 "status".to_string()
333}
334
335fn default_detail_field() -> String {
336 "detail".to_string()
337}
338
339fn default_fleet() -> bool {
340 true
341}
342
343fn default_files_field() -> String {
344 "files".to_string()
345}
346
347/// Fallback cap on a collect bundle's total input size when the
348/// manifest's `collect.max_size` is unset. 50 MB (decimal).
349pub const DEFAULT_COLLECT_MAX_SIZE: u64 = 50 * 1_000_000;
350
351/// Manifest sub-section (#219): marks a job as a **file collector** and
352/// carries how the collected bundle presents in the SPA. Parallel to
353/// [`InventoryHint`] / [`CheckHint`] — the block's presence is the
354/// opt-in. The script prints a single JSON object on stdout whose
355/// [`files_field`](CollectHint::files_field) key holds an array of file
356/// paths to bundle (env vars are expanded); the agent zips them and
357/// uploads to `OBJECT_COLLECTIONS`. See [`Manifest::collect`].
358#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
359pub struct CollectHint {
360 /// Operator/end-user-facing title for the collection, shown as the
361 /// bundle's heading on the SPA Collect page (and the Client App row
362 /// when paired with `client:`). Required; validated non-empty.
363 pub name: String,
364 /// Optional one-line description of what the bundle contains.
365 #[serde(default, skip_serializing_if = "Option::is_none")]
366 pub description: Option<String>,
367 /// Human-readable cap on the bundle's total input size
368 /// (`"50MB"`, `"500KB"`, `"1GiB"`). The agent refuses to build a
369 /// bundle whose listed files exceed this. `None` ⇒
370 /// [`DEFAULT_COLLECT_MAX_SIZE`]. Parsed by [`parse_size_bytes`];
371 /// [`Manifest::validate`] rejects an unparseable value at create
372 /// time.
373 ///
374 /// Note: this bounds the **uncompressed** bytes the agent reads off
375 /// disk, not the resulting zip. Text logs compress well, so the
376 /// download is usually much smaller; many tiny files add a little
377 /// per-entry zip overhead. Read it as "how much the agent reads +
378 /// packs", not "the exact download size".
379 #[serde(default, skip_serializing_if = "Option::is_none")]
380 pub max_size: Option<String>,
381 /// Top-level stdout JSON key holding the array of file paths to
382 /// bundle. Defaults to `"files"`.
383 #[serde(default = "default_files_field")]
384 pub files_field: String,
385}
386
387impl CollectHint {
388 /// The effective size cap in bytes — the parsed `max_size` or
389 /// [`DEFAULT_COLLECT_MAX_SIZE`] when unset. Assumes `max_size` (if
390 /// present) already passed [`Manifest::validate`]; falls back to the
391 /// default on a parse error rather than panicking on the fire path.
392 pub fn max_size_bytes(&self) -> u64 {
393 match &self.max_size {
394 Some(s) => parse_size_bytes(s).unwrap_or(DEFAULT_COLLECT_MAX_SIZE),
395 None => DEFAULT_COLLECT_MAX_SIZE,
396 }
397 }
398}
399
400/// Parse a human-readable byte size (`"50MB"`, `"500 KB"`, `"1GiB"`,
401/// `"1024"`). Decimal units (KB/MB/GB) are 1000-based; binary units
402/// (KiB/MiB/GiB) are 1024-based; a bare number (or `B`) is bytes.
403/// Case-insensitive. Shared by `collect.max_size` validation and the
404/// agent's bundle-size enforcement.
405pub fn parse_size_bytes(s: &str) -> Result<u64, String> {
406 let t = s.trim();
407 if t.is_empty() {
408 return Err("size must not be empty".to_string());
409 }
410 let split = t.find(|c: char| !c.is_ascii_digit()).unwrap_or(t.len());
411 let (num_str, unit_raw) = t.split_at(split);
412 if num_str.is_empty() {
413 return Err(format!("size '{s}': missing leading number"));
414 }
415 let num: u64 = num_str
416 .parse()
417 .map_err(|_| format!("size '{s}': bad number '{num_str}'"))?;
418 let mult: u64 = match unit_raw.trim().to_ascii_lowercase().as_str() {
419 "" | "b" => 1,
420 "kb" => 1_000,
421 "mb" => 1_000_000,
422 "gb" => 1_000_000_000,
423 "kib" => 1024,
424 "mib" => 1024 * 1024,
425 "gib" => 1024 * 1024 * 1024,
426 other => {
427 return Err(format!(
428 "size '{s}': unknown unit '{other}' (use B/KB/MB/GB/KiB/MiB/GiB)"
429 ));
430 }
431 };
432 num.checked_mul(mult)
433 .ok_or_else(|| format!("size '{s}': overflow"))
434}
435
436/// Manifest sub-section (#291): marks a job as **user-invokable**
437/// from the Client App and carries how it presents to the end user.
438/// Parallel to [`InventoryHint`] / [`CheckHint`] / `EmitConfig` —
439/// the block's presence is the opt-in (no separate boolean), and its
440/// required fields (`name`, `category`) are enforced by serde at
441/// parse time, so a half-filled catalog entry fails
442/// `kanade job create` instead of rendering a nameless / tab-less row.
443///
444/// The agent maps this 1:1 into the KLP
445/// [`UserInvokableJob`](crate::ipc::jobs::UserInvokableJob) wire shape
446/// that `jobs.list` returns; the Client App renders one row per job in
447/// the tab named by `category`.
448#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
449pub struct ClientHint {
450 /// End-user-facing title for the job row. The operator-internal
451 /// `Manifest::id` slug is rarely what an end user should read, so
452 /// this is required (and validated non-empty by
453 /// [`Manifest::validate`]). Maps to `UserInvokableJob::display_name`.
454 pub name: String,
455 /// Optional one-line subtitle under `name` in the Client App.
456 /// Distinct from the operator-facing top-level
457 /// [`Manifest::description`] — this one is written for the end
458 /// user. Maps to `UserInvokableJob::display_description`.
459 #[serde(default, skip_serializing_if = "Option::is_none")]
460 pub description: Option<String>,
461 /// Which Client App tab the job lives in (`software_update` →
462 /// アップデート, `troubleshoot` → 困ったとき, `catalog` → software
463 /// catalog). Required — without it the agent can't place the job
464 /// in a tab.
465 pub category: JobCategory,
466 /// Optional icon hint for the job row — a lucide-react icon name
467 /// or a `data:` URL. `None` ⇒ the Client App falls back to the
468 /// category's default icon. Surfaced verbatim in
469 /// `jobs.list[].icon`.
470 #[serde(default, skip_serializing_if = "Option::is_none")]
471 pub icon: Option<String>,
472}
473
474/// #720 — one widget on the SPA **Analytics** page: a declarative
475/// aggregation over the `obs_events` table. The backend reads these off
476/// `Manifest::aggregate` (from `BUCKET_JOBS`) at query time and builds
477/// the `json_extract` GROUP BY / time-bucket SQL from these generic
478/// primitives, so an operator can chart any emitted event without a Rust
479/// change. The reference shapes are the attendance dashboards
480/// (presence / app_sample / web_visit), but the same DSL covers logon /
481/// reboot / agent-health trends, etc.
482#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
483pub struct AggregateWidget {
484 /// Tab this widget lives under on the Analytics page. Widgets from
485 /// every job are collected and grouped by this label, so the same
486 /// string across jobs builds one multi-source dashboard. Required.
487 pub dashboard: String,
488 /// Widget heading. Required, validated non-empty.
489 pub title: String,
490 /// Optional sort weight (#743). Once the order-aware sort lands (PR2)
491 /// widgets render in `(order, dashboard, title)` order, so a lower
492 /// `order` pulls a widget — and its tab — earlier; equal/absent `order`
493 /// falls back to the alphabetical `(dashboard, title)` ordering. Treated
494 /// as `0` when unset, so a fleet with no `order` anywhere stays purely
495 /// alphabetical (today's behaviour); negatives are allowed to pin
496 /// something first. (This field only carries the value; the backend
497 /// applies it.)
498 #[serde(default, skip_serializing_if = "Option::is_none")]
499 pub order: Option<i32>,
500 /// `pc` rolls up a single selected PC; `fleet` rolls up all PCs
501 /// (and unlocks `group_by: pc_id` to rank PCs against each other).
502 /// Defaults to `pc`.
503 #[serde(default)]
504 pub scope: AggregateScope,
505 /// `obs_events.kind` this widget reads (e.g. `app_sample`,
506 /// `presence`, `unexpected_shutdown`). Required.
507 pub kind: String,
508 /// Optional `obs_events.source` filter, when one `kind` is emitted by
509 /// more than one collector.
510 #[serde(default, skip_serializing_if = "Option::is_none")]
511 pub source: Option<String>,
512 /// How to roll the matching events up. See [`AggregateAgg`].
513 pub agg: AggregateAgg,
514 /// Dotted JSON path (no `$.` prefix) to group by for `agg: count` /
515 /// `sum` — e.g. `foreground.app`. The literal `pc_id` is special:
516 /// it groups by the `pc_id` column (fleet ranking), not a payload
517 /// field. Omit for a single total. Required when `agg: sum` needs a
518 /// breakdown; for `agg: count` omitting it yields the grand total.
519 #[serde(default, skip_serializing_if = "Option::is_none")]
520 pub group_by: Option<String>,
521 /// Dotted JSON path to a boolean for `agg: ratio` (e.g. `active`):
522 /// the widget reports `true_count / total`. Required when `agg: ratio`.
523 #[serde(default, skip_serializing_if = "Option::is_none")]
524 pub bool_path: Option<String>,
525 /// Dotted JSON path to a number for `agg: sum`. Required when `agg: sum`.
526 #[serde(default, skip_serializing_if = "Option::is_none")]
527 pub value_path: Option<String>,
528 /// Optional value transform applied before grouping. Currently only
529 /// `host` (parse a URL down to its host) — used by the top-sites
530 /// widget, where SQLite can't parse a URL so the backend does it in
531 /// Rust. See [`AggregateTransform`].
532 #[serde(default, skip_serializing_if = "Option::is_none")]
533 pub transform: Option<AggregateTransform>,
534 /// Optional sampling cadence in minutes. When set, a `count` is also
535 /// reported as estimated time (`count × sample_minutes`) — e.g. a
536 /// 2-minute app sampler turns 11 samples into ~22 minutes. Must be ≥ 1.
537 #[serde(default, skip_serializing_if = "Option::is_none")]
538 #[schemars(range(min = 1))]
539 pub sample_minutes: Option<u32>,
540 /// Grouped values to drop from the rollup (e.g. `["LockApp"]` so the
541 /// lock screen doesn't top the app ranking). Empty by default.
542 #[serde(default, skip_serializing_if = "Vec::is_empty")]
543 pub exclude: Vec<String>,
544 /// Optional time bucketing — `hour` buckets events by local
545 /// hour-of-day for a `timeline` render. See [`AggregateTimeBucket`].
546 #[serde(default, skip_serializing_if = "Option::is_none")]
547 pub time_bucket: Option<AggregateTimeBucket>,
548 /// Top-N cap for grouped renders (`bar`). Defaults to 10 when unset.
549 #[serde(default, skip_serializing_if = "Option::is_none")]
550 #[schemars(range(min = 1))]
551 pub limit: Option<u32>,
552 /// Which widget the SPA draws. See [`AggregateRender`].
553 pub render: AggregateRender,
554}
555
556/// Per-PC vs fleet-wide rollup for an [`AggregateWidget`].
557#[derive(
558 Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
559)]
560#[serde(rename_all = "lowercase")]
561#[non_exhaustive]
562pub enum AggregateScope {
563 /// Roll up the single PC the operator selected. The default.
564 #[default]
565 Pc,
566 /// Roll up across every PC. Unlocks `group_by: pc_id`.
567 Fleet,
568 /// #492 forward-compat catch-all — a Manifest is read fleet-wide, so
569 /// an older reader must tolerate a future variant rather than failing
570 /// to decode the whole job. The backend skips an `Unknown` widget.
571 #[serde(other)]
572 Unknown,
573}
574
575/// The rollup function for an [`AggregateWidget`].
576#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
577#[serde(rename_all = "lowercase")]
578#[non_exhaustive]
579pub enum AggregateAgg {
580 /// Row count, optionally grouped (`group_by`) and time-estimated
581 /// (`sample_minutes`).
582 Count,
583 /// `true_count / total` over `bool_path`.
584 Ratio,
585 /// Sum of `value_path`, optionally grouped.
586 Sum,
587 /// #492 forward-compat catch-all (see [`AggregateScope::Unknown`]).
588 #[serde(other)]
589 Unknown,
590}
591
592/// Optional pre-grouping value transform for an [`AggregateWidget`].
593#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
594#[serde(rename_all = "lowercase")]
595#[non_exhaustive]
596pub enum AggregateTransform {
597 /// Parse the grouped value as a URL and keep only its host.
598 Host,
599 /// #492 forward-compat catch-all (see [`AggregateScope::Unknown`]).
600 #[serde(other)]
601 Unknown,
602}
603
604/// Time bucketing for an [`AggregateWidget`] (drives a `timeline`).
605#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
606#[serde(rename_all = "lowercase")]
607#[non_exhaustive]
608pub enum AggregateTimeBucket {
609 /// Bucket by local hour-of-day (0–23), summed over the window.
610 Hour,
611 /// #492 forward-compat catch-all (see [`AggregateScope::Unknown`]).
612 #[serde(other)]
613 Unknown,
614}
615
616/// Which visual the SPA renders an [`AggregateWidget`] as.
617#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
618#[serde(rename_all = "lowercase")]
619#[non_exhaustive]
620pub enum AggregateRender {
621 /// Ranked horizontal bars (a grouped `count` / `sum`).
622 Bar,
623 /// A single ratio dial (`agg: ratio`).
624 Gauge,
625 /// 24-hour activity strip (`time_bucket: hour`).
626 Timeline,
627 /// A single headline number (an ungrouped total).
628 Stat,
629 /// #492 forward-compat catch-all (see [`AggregateScope::Unknown`]).
630 #[serde(other)]
631 Unknown,
632}
633
634/// True if `p` is a well-formed dotted JSON path of `[A-Za-z0-9_]`
635/// segments joined by single dots — the shape safe to bind into
636/// `json_extract(payload, '$.' || ?)`. The charset blocks injection; the
637/// segment check additionally rejects `"."`, `".foo"`, `"foo."`,
638/// `"foo..bar"`, which would pass the charset but produce a malformed
639/// `$.` path that errors at query time. Accepts `pc_id`, `foreground.app`,
640/// `active`, etc.
641fn is_valid_json_path(p: &str) -> bool {
642 !p.is_empty()
643 && p.split('.').all(|seg| {
644 !seg.is_empty() && seg.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
645 })
646}
647
648/// Per-widget validation for a list of [`AggregateWidget`]s — shared by
649/// the `aggregate:` job hint ([`Manifest::validate`]) and the standalone
650/// [`View`] resource (#743) so the two can't diverge. `field` names the
651/// containing key for error messages (`"aggregate"` or `"widgets"`).
652///
653/// Enforces: non-empty list; non-empty dashboard/title/kind; a
654/// blank-when-set `source`; rejection of any #492 `Unknown` enum
655/// (an operator typo at create time); safe dotted JSON paths; the value
656/// path each `agg` needs (and rejection of mis-paired ones); `pc_id`
657/// grouping only in `fleet` scope; `transform`/`limit`/`exclude` only with
658/// a `group_by`; positive `limit`/`sample_minutes`; `gauge`⇔`ratio`; and
659/// `timeline`⇔`time_bucket`.
660pub fn validate_aggregate_widgets(widgets: &[AggregateWidget], field: &str) -> Result<(), String> {
661 if widgets.is_empty() {
662 return Err(format!(
663 "`{field}:` must list at least one widget when present"
664 ));
665 }
666 for (i, w) in widgets.iter().enumerate() {
667 let at = format!("{field}[{i}]");
668 for (label, value) in [
669 ("dashboard", &w.dashboard),
670 ("title", &w.title),
671 ("kind", &w.kind),
672 ] {
673 if value.trim().is_empty() {
674 return Err(format!("{at}.{label} must not be empty"));
675 }
676 }
677 // A present-but-blank `source` is a no-op filter — reject like the
678 // other blank-when-set guards.
679 if let Some(source) = &w.source {
680 if source.trim().is_empty() {
681 return Err(format!("{at}.source must not be empty when set"));
682 }
683 }
684 // Reject values that fell through to the #492 `Unknown` catch-all:
685 // at create time on the current version that's an operator typo. (A
686 // genuinely-future variant only reaches an older reader via a stored
687 // resource, which is never re-validated, so forward-compat holds.)
688 if w.scope == AggregateScope::Unknown {
689 return Err(format!("{at}.scope is not a known value (pc | fleet)"));
690 }
691 if w.agg == AggregateAgg::Unknown {
692 return Err(format!(
693 "{at}.agg is not a known value (count | ratio | sum)"
694 ));
695 }
696 if w.render == AggregateRender::Unknown {
697 return Err(format!(
698 "{at}.render is not a known value (bar | gauge | timeline | stat)"
699 ));
700 }
701 if w.transform == Some(AggregateTransform::Unknown) {
702 return Err(format!("{at}.transform is not a known value (host)"));
703 }
704 if w.time_bucket == Some(AggregateTimeBucket::Unknown) {
705 return Err(format!("{at}.time_bucket is not a known value (hour)"));
706 }
707 for (label, path) in [
708 ("group_by", &w.group_by),
709 ("bool_path", &w.bool_path),
710 ("value_path", &w.value_path),
711 ] {
712 if let Some(p) = path {
713 if !is_valid_json_path(p) {
714 return Err(format!(
715 "{at}.{label} '{p}' must be a dotted JSON path of [A-Za-z0-9_] segments"
716 ));
717 }
718 }
719 }
720 // Each agg uses exactly one value path; reject a mis-paired path so
721 // a typo fails at create rather than being ignored.
722 match w.agg {
723 // count: grouped → ranking, ungrouped → grand total.
724 AggregateAgg::Count => {
725 for (label, path) in [("bool_path", &w.bool_path), ("value_path", &w.value_path)] {
726 if path.is_some() {
727 return Err(format!("{at}.agg=count does not use `{label}`"));
728 }
729 }
730 }
731 AggregateAgg::Ratio => {
732 if w.bool_path.is_none() {
733 return Err(format!("{at}.agg=ratio requires `bool_path`"));
734 }
735 if w.value_path.is_some() {
736 return Err(format!("{at}.agg=ratio does not use `value_path`"));
737 }
738 }
739 AggregateAgg::Sum => {
740 if w.value_path.is_none() {
741 return Err(format!("{at}.agg=sum requires `value_path`"));
742 }
743 if w.bool_path.is_some() {
744 return Err(format!("{at}.agg=sum does not use `bool_path`"));
745 }
746 }
747 // Rejected above; arm exists only for exhaustiveness.
748 AggregateAgg::Unknown => {}
749 }
750 // Ranking PCs against each other only means something across the
751 // fleet — within one PC it's a single bar.
752 if w.group_by.as_deref() == Some("pc_id") && w.scope != AggregateScope::Fleet {
753 return Err(format!(
754 "{at}.group_by: pc_id is only valid with scope: fleet"
755 ));
756 }
757 // `transform` rewrites the grouped PAYLOAD value (URL→host); it's
758 // meaningless on a `pc_id` grouping (the pc_id column, not a payload
759 // field), so reject the combo at create time.
760 if w.transform.is_some() && w.group_by.as_deref() == Some("pc_id") {
761 return Err(format!("{at}.transform is not valid with group_by: pc_id"));
762 }
763 // limit / transform / exclude all operate on grouped values, so
764 // without a `group_by` they're silent no-ops — reject.
765 if w.group_by.is_none() {
766 if w.limit.is_some() {
767 return Err(format!("{at}.limit requires `group_by`"));
768 }
769 if w.transform.is_some() {
770 return Err(format!("{at}.transform requires `group_by`"));
771 }
772 if !w.exclude.is_empty() {
773 return Err(format!("{at}.exclude requires `group_by`"));
774 }
775 }
776 if w.limit == Some(0) {
777 return Err(format!("{at}.limit must be > 0"));
778 }
779 if w.sample_minutes == Some(0) {
780 return Err(format!("{at}.sample_minutes must be > 0"));
781 }
782 for ex in &w.exclude {
783 if ex.trim().is_empty() {
784 return Err(format!("{at}.exclude must not contain empty entries"));
785 }
786 }
787 // A gauge draws a single ratio dial — only meaningful for agg: ratio.
788 if w.render == AggregateRender::Gauge && w.agg != AggregateAgg::Ratio {
789 return Err(format!("{at}.render=gauge is only valid with agg: ratio"));
790 }
791 // A timeline needs a bucket; a bucket on any other render is a no-op
792 // that signals operator confusion — reject both.
793 match (w.render, &w.time_bucket) {
794 (AggregateRender::Timeline, None) => {
795 return Err(format!("{at}.render=timeline requires `time_bucket`"));
796 }
797 (r, Some(_)) if r != AggregateRender::Timeline => {
798 return Err(format!(
799 "{at}.time_bucket is only valid with render: timeline"
800 ));
801 }
802 _ => {}
803 }
804 }
805 Ok(())
806}
807
808/// A standalone declarative read/aggregation for the Analytics page (#743).
809///
810/// A **view** aggregates stored fleet data (`obs_events`, …) without an
811/// `execute` or a schedule — unlike a [`Manifest`] it only declares
812/// [`AggregateWidget`]s. (The first line is concise on purpose: `schemars`
813/// uses it as the generated schema's `title`.) The backend reads views from
814/// `BUCKET_VIEWS` at
815/// query time and merges their widgets with the co-located `aggregate:`
816/// hints on jobs, so a cross-cutting dashboard (one that charts events
817/// emitted by several other jobs / the agent) has a home that doesn't need
818/// a noop job carrier. Stored JSON in `BUCKET_VIEWS`, keyed by `id`.
819#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
820pub struct View {
821 /// Stable identifier (the KV key). Required, validated non-empty.
822 pub id: String,
823 /// Optional human description shown on the Views admin page.
824 #[serde(default, skip_serializing_if = "Option::is_none")]
825 pub description: Option<String>,
826 /// The widgets this view contributes to the Analytics page.
827 pub widgets: Vec<AggregateWidget>,
828 /// Free-form operator taxonomy (same role as [`Manifest::tags`]).
829 #[serde(default, skip_serializing_if = "Vec::is_empty")]
830 pub tags: Vec<String>,
831 /// GitOps provenance (#678), stamped by `kanade view create` from the
832 /// source YAML's Git context — same as [`Manifest::origin`].
833 #[serde(default, skip_serializing_if = "Option::is_none")]
834 pub origin: Option<RepoOrigin>,
835}
836
837/// True if `id` is a safe resource identifier — non-empty and only
838/// `[A-Za-z0-9._-]`. A view `id` becomes a NATS KV key *and* a URL path
839/// segment (`/api/views/{id}`), so this blocks `/`, `..`, whitespace and
840/// other characters that would break the KV key or let a CLI arg wander
841/// the URL space. (#743 / #744 follow-up — a deliberately small charset
842/// rather than the looser set NATS technically allows.)
843pub fn is_valid_resource_id(id: &str) -> bool {
844 !id.is_empty()
845 && id
846 .chars()
847 .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '_' || c == '-')
848}
849
850impl View {
851 pub fn validate(&self) -> Result<(), String> {
852 if !is_valid_resource_id(self.id.trim()) {
853 return Err(
854 "view.id must be non-empty and only [A-Za-z0-9._-] (it's a KV key + URL segment)"
855 .to_string(),
856 );
857 }
858 validate_aggregate_widgets(&self.widgets, "widgets")?;
859 for tag in &self.tags {
860 if tag.trim().is_empty() {
861 return Err("tags must not contain empty entries".to_string());
862 }
863 }
864 Ok(())
865 }
866}
867
868/// Issue #246 — `emit:` manifest block for jobs whose stdout is
869/// NDJSON observability events (one `ObsEvent` per line). Parallel
870/// to `inventory:` but for the append-only timeline pipeline; see
871/// `Manifest::emit` for the full contract.
872#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
873pub struct EmitConfig {
874 /// What kind of payload the agent should expect on stdout. Only
875 /// `events` is defined today (parses each non-empty line as
876 /// `ObsEvent` and publishes on `obs.<pc_id>`); future variants
877 /// (e.g. metrics streams, structured trace events) plug in here.
878 #[serde(rename = "type")]
879 pub kind: EmitKind,
880 /// Operator hint for where the script keeps its own state — the
881 /// watermark file the PowerShell / sh body reads + writes
882 /// between runs so it only emits NEW events since the last
883 /// poll. The agent doesn't read this; it's documentation that
884 /// the SPA (and `kanade job edit`) can surface to operators
885 /// reviewing the manifest. Optional; the script is allowed to
886 /// keep state anywhere (registry, env, etc.) — the field's
887 /// presence makes the convention discoverable.
888 #[serde(default, skip_serializing_if = "Option::is_none")]
889 pub watermark_path: Option<String>,
890}
891
892/// `emit.type` enum. Lowercase serde so manifests read
893/// `type: events` rather than `Events`.
894#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
895#[serde(rename_all = "lowercase")]
896pub enum EmitKind {
897 /// Per-line `ObsEvent` JSON. Agent parses + publishes on
898 /// `obs.<pc_id>`, drops the stdout from the resulting
899 /// `ExecResult`.
900 Events,
901}
902
903/// v0.31 / #40: declarative "flatten this JSON array into a real
904/// SQLite table" spec on an inventory manifest. The projector
905/// creates the table on first registration (CREATE TABLE IF NOT
906/// EXISTS + indexes) and writes a row per element of
907/// `payload[field]` on every result, scoped by (pc_id, job_id) so
908/// each PC's rows replace cleanly without a per-PC schema.
909#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
910pub struct ExplodeSpec {
911 /// JSON array key under the payload to explode. E.g. `"apps"`
912 /// for `payload: { apps: [{...}, {...}] }`.
913 pub field: String,
914 /// Derived SQLite table name. Operators choose this — pick
915 /// something namespaced + stable (`inventory_sw_apps`, not
916 /// `apps`) so multiple inventory manifests don't collide on a
917 /// generic name.
918 pub table: String,
919 /// Element-level fields that uniquely identify a row inside one
920 /// PC's payload. The full PK is `(pc_id, job_id) + these
921 /// columns`. Required — operators must think about uniqueness
922 /// (e.g. `["name", "source"]` for installed apps because the
923 /// same name appears in multiple uninstall hives).
924 ///
925 /// v0.31 / #41: same tuple drives history identity. When
926 /// `track_history` is on, the projector serialises these
927 /// fields' values into `inventory_history.identity_json` for
928 /// every change event, so queries like "every PC that ever
929 /// installed Chrome (any source)" filter on identity_json
930 /// content without a per-manifest schema.
931 pub primary_key: Vec<String>,
932 /// Per-element fields that become columns in the derived table.
933 pub columns: Vec<ExplodeColumn>,
934 /// v0.31 / #41: when true (default false), the projector
935 /// diffs each PC's incoming payload against the prior rows
936 /// for the same (pc_id, job_id) BEFORE the DELETE-then-INSERT
937 /// replace, and writes added / removed / changed events into
938 /// `inventory_history`. Lets operators answer time-dimension
939 /// questions ("when did Chrome 120 first appear on PC X?",
940 /// "what's the Win 11 23H2 rollout curve") without storing
941 /// per-scan snapshots. Off by default so operators opt in
942 /// per-spec — history has a real storage cost on long-lived
943 /// deployments (mitigated by the 90-day default retention
944 /// sweeper, see `cleanup` module).
945 #[serde(default)]
946 pub track_history: bool,
947}
948
949/// One column in an [`ExplodeSpec`]'s derived table.
950#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
951pub struct ExplodeColumn {
952 /// JSON key under each array element. Becomes the column name
953 /// in the derived SQLite table — we don't rename.
954 pub field: String,
955 /// SQLite affinity: `"text"` (default), `"integer"`, `"real"`.
956 /// Storage maps directly via `sqlx::query.bind(...)`; type
957 /// mismatches at INSERT-time fail loudly rather than silently
958 /// dropping the row.
959 #[serde(default, skip_serializing_if = "Option::is_none")]
960 #[serde(rename = "type")]
961 pub kind: Option<String>,
962 /// When true, the projector creates a `CREATE INDEX` on this
963 /// column at table-creation time. Boost for the common-filter
964 /// columns (`name`, `version`) — operators mark them
965 /// explicitly, the projector won't guess.
966 #[serde(default)]
967 pub index: bool,
968}
969
970#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
971pub struct DisplayField {
972 /// Top-level key in the stdout JSON.
973 pub field: String,
974 /// Human-readable column header.
975 pub label: String,
976 /// Optional render hint — `"number"`, `"bytes"`, `"timestamp"`,
977 /// or `"table"` (#39). Defaults to plain text rendering on the
978 /// SPA side. `"table"` expects the field's value to be a JSON
979 /// array of objects and renders a nested sub-table on the
980 /// per-PC detail page using `columns` as the schema; the fleet
981 /// summary view falls back to showing the row count for
982 /// `"table"` cells so the wide list stays compact.
983 #[serde(default, skip_serializing_if = "Option::is_none")]
984 #[serde(rename = "type")]
985 pub kind: Option<String>,
986 /// v0.30 / #39: when `kind == "table"`, the SPA renders the
987 /// field's value (an array of objects like
988 /// `disks: [{ device_id, size_bytes, ... }]`) as a nested
989 /// sub-table using these columns. Each column is itself a
990 /// `DisplayField`, so the nested cells reuse the same render
991 /// hints (`bytes`, `number`, `timestamp`) — no parallel format
992 /// pipeline. Ignored for any other `kind`.
993 #[serde(default, skip_serializing_if = "Option::is_none")]
994 pub columns: Option<Vec<DisplayField>>,
995}
996
997#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
998pub struct Rollout {
999 #[serde(default)]
1000 pub strategy: RolloutStrategy,
1001 pub waves: Vec<Wave>,
1002}
1003
1004#[derive(
1005 Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
1006)]
1007#[serde(rename_all = "lowercase")]
1008pub enum RolloutStrategy {
1009 #[default]
1010 Wave,
1011}
1012
1013#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
1014pub struct Wave {
1015 pub group: String,
1016 /// humantime delay measured from the deploy's publish time. wave[0]
1017 /// typically has "0s"; subsequent waves use minutes / hours.
1018 pub delay: String,
1019}
1020
1021#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
1022pub struct Target {
1023 #[serde(default)]
1024 pub groups: Vec<String>,
1025 #[serde(default)]
1026 pub pcs: Vec<String>,
1027 #[serde(default)]
1028 pub all: bool,
1029}
1030
1031impl Target {
1032 /// At least one of all / groups / pcs is set.
1033 pub fn is_specified(&self) -> bool {
1034 self.all || !self.groups.is_empty() || !self.pcs.is_empty()
1035 }
1036}
1037
1038#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
1039pub struct Execute {
1040 pub shell: ExecuteShell,
1041 /// Inline script body. Mutually exclusive with [`script_file`]
1042 /// and [`script_object`]; exactly one of the three must be set
1043 /// (enforced by [`Execute::validate_script_source`] at the
1044 /// write-side parse boundaries — `kanade job create` and
1045 /// `POST /api/jobs`).
1046 ///
1047 /// Empty string is treated as **unset** so operators can swap
1048 /// to a `script_file:` / `script_object:` alternative just by
1049 /// commenting out the body, without having to also drop the
1050 /// `script:` key entirely.
1051 ///
1052 /// [`script_file`]: Self::script_file
1053 /// [`script_object`]: Self::script_object
1054 #[serde(default, skip_serializing_if = "Option::is_none")]
1055 pub script: Option<String>,
1056 /// Repo-local file path resolved by the operator-side CLI at
1057 /// `kanade job create` time. The CLI reads the file, slots its
1058 /// contents into `script`, and clears this field before
1059 /// POSTing — so the backend / agents never see `script_file`
1060 /// in stored manifests. SPEC §2.4.1.
1061 ///
1062 /// Resolver lands in a follow-up PR
1063 /// (yukimemi/kanade#210); today this field passes parse-time
1064 /// validation but the operator-side CLI bails with "not yet
1065 /// implemented" until the resolver ships, so manifests that
1066 /// reach the backend with `script_file` set are treated as a
1067 /// schema-bug.
1068 #[serde(default, skip_serializing_if = "Option::is_none")]
1069 pub script_file: Option<String>,
1070 /// Object Store reference (`<name>/<version>`) into the
1071 /// `scripts` bucket (`OBJECT_SCRIPTS`). Agents fetch the body
1072 /// at Execute time via `/api/script-objects/{name}/{version}`
1073 /// and cache it locally. SPEC §2.4.1.
1074 ///
1075 /// Fully wired (#210/#211): the backend resolves the digest at
1076 /// exec submission (`api::exec::resolve_script_source`), the agent
1077 /// fetches + sha-verifies + caches the body (`script_cache`), and
1078 /// `kanade script` CRUDs the store. Unlike `script_file:` (inlined
1079 /// CLI-side, git-managed), this keeps the body in versioned,
1080 /// digest-pinned object storage — the ops-managed counterpart.
1081 #[serde(default, skip_serializing_if = "Option::is_none")]
1082 pub script_object: Option<String>,
1083 /// humantime duration string (e.g. "30s", "10m"). Script-intrinsic
1084 /// — represents how long this script reasonably takes to run.
1085 pub timeout: String,
1086 /// Token + session combination the agent uses to launch the
1087 /// script (v0.21). Default = [`RunAs::System`] (Session 0,
1088 /// LocalSystem privileges, no GUI) — matches pre-v0.21 behavior.
1089 #[serde(default)]
1090 pub run_as: RunAs,
1091 /// Working directory for the spawned child (v0.21.1). When
1092 /// unset, the child inherits the agent's cwd — on Windows that
1093 /// means `%SystemRoot%\System32` for the prod service, which is
1094 /// almost never what operators actually want. Use an absolute
1095 /// path; relative paths are passed through to the OS verbatim.
1096 /// `%PROGRAMDATA%` works for `run_as: system`; for `run_as: user`
1097 /// you'd want `%USERPROFILE%` (but expansion happens in the
1098 /// shell, so write `$env:USERPROFILE` for PowerShell, or set
1099 /// it via teravars before `kanade job create`).
1100 #[serde(default, skip_serializing_if = "Option::is_none")]
1101 pub cwd: Option<String>,
1102}
1103
1104impl Execute {
1105 /// Treat an empty `script:` body as "intentionally unset". Operators
1106 /// commenting out a block-scalar tend to leave the key behind, and
1107 /// failing the validator on `script: ""` would surprise them.
1108 fn has_inline_script(&self) -> bool {
1109 matches!(&self.script, Some(s) if !s.is_empty())
1110 }
1111
1112 /// Enforce that exactly one of `script` / `script_file` /
1113 /// `script_object` is set. Called at the write-side parse
1114 /// boundaries (CLI `kanade job create` + backend
1115 /// `POST /api/jobs`) so ambiguous YAML is rejected before it
1116 /// reaches the JOBS KV. Read paths (projector, agent
1117 /// scheduler, list endpoints) skip this check — they only ever
1118 /// see what the write path already validated.
1119 pub fn validate_script_source(&self) -> Result<(), String> {
1120 let inline = self.has_inline_script();
1121 let file = self.script_file.is_some();
1122 let obj = self.script_object.is_some();
1123 let set = [inline, file, obj].into_iter().filter(|b| *b).count();
1124 match set {
1125 1 => Ok(()),
1126 0 => Err("execute: one of `script`, `script_file`, `script_object` must be set".into()),
1127 _ => Err(format!(
1128 "execute: only one of `script` / `script_file` / `script_object` may be set \
1129 (got script={inline}, script_file={file}, script_object={obj})"
1130 )),
1131 }
1132 }
1133}
1134
1135impl Manifest {
1136 /// Cross-field semantic checks that don't fit into pure serde
1137 /// derive. Currently delegates to
1138 /// [`Execute::validate_script_source`] — see that method's
1139 /// docs for the rationale on which call sites should run this.
1140 pub fn validate(&self) -> Result<(), String> {
1141 self.execute.validate_script_source()?;
1142 // Stdout-format compatibility. `inventory:` and `check:` both
1143 // consume the SAME single JSON object — they COMPOSE: a check
1144 // can extract `status`/`detail` for the Health tab while the
1145 // projector explodes the rest into SPA sub-tables. `emit:` is
1146 // different — its stdout is NDJSON and the agent omits it from
1147 // the result entirely — so it can't be paired with either.
1148 if self.emit.is_some() && (self.inventory.is_some() || self.check.is_some()) {
1149 return Err(
1150 "`emit:` is incompatible with `inventory:` / `check:` — emit's stdout is NDJSON \
1151 timeline events (and omitted from the result), while inventory/check read a \
1152 single JSON object from stdout"
1153 .to_string(),
1154 );
1155 }
1156 // `collect:` consumes stdout for its OWN contract (a JSON object
1157 // carrying a `files` array), so unlike the inventory+check pair it
1158 // can't share stdout with another stdout-reading hint. It composes
1159 // only with `client:` (which doesn't touch stdout).
1160 if self.collect.is_some()
1161 && (self.inventory.is_some() || self.check.is_some() || self.emit.is_some())
1162 {
1163 return Err(
1164 "`collect:` is incompatible with `inventory:` / `check:` / `emit:` — collect \
1165 reads its own `files` JSON object from stdout. (It composes with `client:`.)"
1166 .to_string(),
1167 );
1168 }
1169 // A check's `name` is the Health-tab row id (React key); the
1170 // field names tell the agent where to read status/detail.
1171 // An empty value is an invisible runtime bug, and the serde
1172 // defaults don't guard an operator who writes `status_field:
1173 // ""` explicitly — reject all three here.
1174 if let Some(check) = &self.check {
1175 for (label, value) in [
1176 ("check.name", &check.name),
1177 ("check.status_field", &check.status_field),
1178 ("check.detail_field", &check.detail_field),
1179 ] {
1180 if value.trim().is_empty() {
1181 return Err(format!("{label} must not be empty"));
1182 }
1183 }
1184 // A present-but-blank `troubleshoot` is a broken
1185 // remediation job id (the "修復する" button would target
1186 // an empty manifest id) — reject it too.
1187 if let Some(troubleshoot) = &check.troubleshoot {
1188 if troubleshoot.trim().is_empty() {
1189 return Err("check.troubleshoot must not be empty when set".to_string());
1190 }
1191 }
1192 // A present-but-blank `label` would render an empty row
1193 // title on the Health tab / Compliance page — reject it so
1194 // the slug fallback only ever kicks in when label is absent.
1195 if let Some(label) = &check.label {
1196 if label.trim().is_empty() {
1197 return Err("check.label must not be empty when set".to_string());
1198 }
1199 }
1200 }
1201 // #291: a `client:` job is rendered in the Client App's
1202 // catalog (`jobs.list` → `jobs.execute`). serde already makes
1203 // `name` + `category` required at parse time; the only gap is
1204 // a present-but-blank `name`, which would render an empty row
1205 // title — reject it like the other display-id fields.
1206 if let Some(client) = &self.client {
1207 if client.name.trim().is_empty() {
1208 return Err("client.name must not be empty".to_string());
1209 }
1210 // Optional display fields, when present, must be
1211 // meaningful: a blank `description` renders an empty
1212 // subtitle and a blank `icon` is a dangling lucide name.
1213 // Same present-but-blank guard the `check:` block applies
1214 // to its optional `troubleshoot` id.
1215 for (label, value) in [
1216 ("client.description", &client.description),
1217 ("client.icon", &client.icon),
1218 ] {
1219 if let Some(v) = value {
1220 if v.trim().is_empty() {
1221 return Err(format!("{label} must not be empty when set"));
1222 }
1223 }
1224 }
1225 }
1226 // #219: a `collect:` job's `name` heads the bundle on the SPA
1227 // Collect page (and the Client App row when paired with
1228 // `client:`), `files_field` tells the agent where to read the
1229 // path list, and `max_size` must be a parseable size so a typo
1230 // is caught at create time rather than silently capping the
1231 // bundle at the default on the fire path.
1232 if let Some(collect) = &self.collect {
1233 if collect.name.trim().is_empty() {
1234 return Err("collect.name must not be empty".to_string());
1235 }
1236 if collect.files_field.trim().is_empty() {
1237 return Err("collect.files_field must not be empty".to_string());
1238 }
1239 if let Some(description) = &collect.description {
1240 if description.trim().is_empty() {
1241 return Err("collect.description must not be empty when set".to_string());
1242 }
1243 }
1244 if let Some(max_size) = &collect.max_size {
1245 parse_size_bytes(max_size).map_err(|e| format!("collect.max_size: {e}"))?;
1246 }
1247 }
1248 // #720/#743: `aggregate:` is a pure read-spec (it never touches
1249 // stdout and is never sent to an agent), so it composes with every
1250 // other hint. The per-widget rules are shared with the standalone
1251 // `view` resource — see [`validate_aggregate_widgets`].
1252 if let Some(widgets) = &self.aggregate {
1253 validate_aggregate_widgets(widgets, "aggregate")?;
1254 }
1255 // A blank / whitespace-only tag is an invisible operator typo
1256 // that would render an empty filter chip on the Jobs page —
1257 // reject it like the other present-but-blank display fields.
1258 for tag in &self.tags {
1259 if tag.trim().is_empty() {
1260 return Err("tags must not contain empty entries".to_string());
1261 }
1262 }
1263 Ok(())
1264 }
1265}
1266
1267#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
1268#[serde(rename_all = "lowercase")]
1269pub enum ExecuteShell {
1270 Powershell,
1271 Cmd,
1272}
1273
1274impl From<ExecuteShell> for Shell {
1275 fn from(s: ExecuteShell) -> Self {
1276 match s {
1277 ExecuteShell::Powershell => Shell::Powershell,
1278 ExecuteShell::Cmd => Shell::Cmd,
1279 }
1280 }
1281}
1282
1283#[cfg(test)]
1284mod tests {
1285 use super::*;
1286
1287 /// The example check-job + schedule YAMLs shipped under `configs/`
1288 /// must stay valid as the schema evolves (#290 PR-C). `include_str!`
1289 /// pins them at compile time so a breaking edit fails `cargo test`
1290 /// rather than only `kanade job create` at deploy time.
1291 #[test]
1292 fn example_check_job_yamls_parse_and_validate() {
1293 let jobs = [
1294 (
1295 "check-bitlocker",
1296 include_str!("../../../configs/jobs/check-bitlocker.yaml"),
1297 ),
1298 (
1299 "check-av-signature",
1300 include_str!("../../../configs/jobs/check-av-signature.yaml"),
1301 ),
1302 (
1303 "check-cert-expiry",
1304 include_str!("../../../configs/jobs/check-cert-expiry.yaml"),
1305 ),
1306 (
1307 "check-disk-space",
1308 include_str!("../../../configs/jobs/check-disk-space.yaml"),
1309 ),
1310 (
1311 "check-pending-reboot",
1312 include_str!("../../../configs/jobs/check-pending-reboot.yaml"),
1313 ),
1314 (
1315 "check-defender-rtp",
1316 include_str!("../../../configs/jobs/check-defender-rtp.yaml"),
1317 ),
1318 (
1319 "check-firewall",
1320 include_str!("../../../configs/jobs/check-firewall.yaml"),
1321 ),
1322 ];
1323 for (name, yaml) in jobs {
1324 let m: Manifest =
1325 serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{name} parse: {e}"));
1326 m.validate()
1327 .unwrap_or_else(|e| panic!("{name} validate: {e}"));
1328 let check = m
1329 .check
1330 .as_ref()
1331 .unwrap_or_else(|| panic!("{name} must carry a check: hint"));
1332 assert!(!check.name.trim().is_empty(), "{name} check.name empty");
1333 // These examples all read admin-only WMI / registry / netsh
1334 // state, so they run_as system. NOTE: that's a property of
1335 // these particular checks, NOT of the `check:` contract — a
1336 // check probing user-session state could run_as user.
1337 assert_eq!(
1338 m.execute.run_as,
1339 RunAs::System,
1340 "{name} should run_as system"
1341 );
1342 }
1343 }
1344
1345 /// The example user-invokable job YAMLs (#291) shipped under
1346 /// `configs/jobs/` must stay valid as the `client:` schema
1347 /// evolves. `include_str!` pins them at compile time so a breaking
1348 /// edit fails `cargo test`, not `kanade job create` at deploy.
1349 #[test]
1350 fn example_client_job_yamls_parse_and_validate() {
1351 let jobs = [
1352 (
1353 "fix-teams-cache",
1354 JobCategory::Troubleshoot,
1355 include_str!("../../../configs/jobs/fix-teams-cache.yaml"),
1356 ),
1357 (
1358 "chrome-update",
1359 JobCategory::SoftwareUpdate,
1360 include_str!("../../../configs/jobs/chrome-update.yaml"),
1361 ),
1362 (
1363 "install-slack",
1364 JobCategory::Catalog,
1365 include_str!("../../../configs/jobs/install-slack.yaml"),
1366 ),
1367 (
1368 "fix-defender-rtp",
1369 JobCategory::Troubleshoot,
1370 include_str!("../../../configs/jobs/fix-defender-rtp.yaml"),
1371 ),
1372 ];
1373 for (id, category, yaml) in jobs {
1374 let m: Manifest =
1375 serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{id} parse: {e}"));
1376 m.validate()
1377 .unwrap_or_else(|e| panic!("{id} validate: {e}"));
1378 assert_eq!(m.id, id, "{id} id mismatch");
1379 let client = m
1380 .client
1381 .as_ref()
1382 .unwrap_or_else(|| panic!("{id} must carry a client: block"));
1383 assert!(!client.name.trim().is_empty(), "{id} client.name empty");
1384 assert_eq!(client.category, category, "{id} category");
1385 }
1386 }
1387
1388 /// #219: the shipped `collect:` example must stay valid as the
1389 /// schema evolves. `include_str!` pins it at compile time so a
1390 /// breaking edit (or a YAML typo in the PowerShell block) fails
1391 /// `cargo test` rather than `kanade job create` at deploy. It carries
1392 /// both `collect:` and `client:` (end-user-triggerable), which must
1393 /// compose.
1394 #[test]
1395 fn example_collect_job_yaml_parses_and_validates() {
1396 let yaml = include_str!("../../../configs/jobs/collect-diagnostics.yaml");
1397 let m: Manifest = serde_yaml::from_str(yaml).expect("collect-diagnostics parse");
1398 m.validate().expect("collect-diagnostics validate");
1399 assert_eq!(m.id, "collect-diagnostics");
1400 let collect = m.collect.as_ref().expect("collect: block present");
1401 assert!(!collect.name.trim().is_empty());
1402 assert_eq!(collect.files_field, "files");
1403 assert_eq!(collect.max_size_bytes(), 50_000_000);
1404 // collect + client compose — the Client App can trigger it.
1405 assert!(
1406 m.client.is_some(),
1407 "collect-diagnostics also carries client:"
1408 );
1409 }
1410
1411 /// The `emit: { type: events }` collector jobs under
1412 /// `configs/jobs/` feed the obs_events timeline. `include_str!`
1413 /// pins them at compile time so a breaking edit (e.g. an `emit:`
1414 /// paired with `check:`/`inventory:`, a bad watermark field, or a
1415 /// YAML typo in the PowerShell block) fails `cargo test` rather
1416 /// than `kanade job create` at deploy. Every one must carry an
1417 /// `emit.type=events` block and NO check/inventory (validate()
1418 /// rejects the pairing).
1419 #[test]
1420 fn example_event_collector_job_yamls_parse_and_validate() {
1421 let jobs = [
1422 (
1423 "collect-winlog-events",
1424 include_str!("../../../configs/jobs/collect-winlog-events.yaml"),
1425 ),
1426 (
1427 "collect-winlog-logons-all",
1428 include_str!("../../../configs/jobs/collect-winlog-logons-all.yaml"),
1429 ),
1430 (
1431 "collect-wlan-events",
1432 include_str!("../../../configs/jobs/collect-wlan-events.yaml"),
1433 ),
1434 ];
1435 for (id, yaml) in jobs {
1436 // Strict parse so an unknown-key typo in these fixtures fails
1437 // here (not silently at deploy) — the runtime Manifest is
1438 // unknown-key-tolerant, so the lenient serde_yaml::from_str
1439 // wouldn't catch fixture drift (CodeRabbit #689).
1440 let m: Manifest =
1441 crate::strict::from_yaml_str(yaml).unwrap_or_else(|e| panic!("{id} parse: {e}"));
1442 m.validate()
1443 .unwrap_or_else(|e| panic!("{id} validate: {e}"));
1444 assert_eq!(m.id, id, "{id} id mismatch");
1445 let emit = m
1446 .emit
1447 .as_ref()
1448 .unwrap_or_else(|| panic!("{id} must carry an emit: block"));
1449 assert_eq!(emit.kind, EmitKind::Events, "{id} emit.type");
1450 assert!(
1451 m.check.is_none() && m.inventory.is_none(),
1452 "{id}: emit jobs must not pair with check/inventory"
1453 );
1454 }
1455 }
1456
1457 /// The `inventory:` snapshot jobs under `configs/jobs/` project
1458 /// facts into `inventory_facts` + exploded tables. `include_str!`
1459 /// pins them at compile time so a breaking edit (bad explode
1460 /// schema, a YAML typo in the PowerShell block, an `inventory:`
1461 /// accidentally paired with `emit:`) fails `cargo test` rather
1462 /// than the projector at deploy. Each must carry an `inventory:`
1463 /// block and NO emit (validate() rejects the pairing).
1464 #[test]
1465 fn example_inventory_job_yamls_parse_and_validate() {
1466 let jobs = [
1467 (
1468 "inventory-hw",
1469 include_str!("../../../configs/jobs/inventory-hw.yaml"),
1470 ),
1471 (
1472 "inventory-sw",
1473 include_str!("../../../configs/jobs/inventory-sw.yaml"),
1474 ),
1475 (
1476 "inventory-driver",
1477 include_str!("../../../configs/jobs/inventory-driver.yaml"),
1478 ),
1479 ];
1480 for (id, yaml) in jobs {
1481 let m: Manifest =
1482 serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{id} parse: {e}"));
1483 m.validate()
1484 .unwrap_or_else(|e| panic!("{id} validate: {e}"));
1485 assert_eq!(m.id, id, "{id} id mismatch");
1486 assert!(m.inventory.is_some(), "{id} must carry an inventory: block");
1487 assert!(m.emit.is_none(), "{id}: inventory jobs must not set emit:");
1488 }
1489 }
1490
1491 #[test]
1492 fn example_check_schedule_yamls_parse_and_validate() {
1493 let schedules = [
1494 (
1495 "check-bitlocker",
1496 include_str!("../../../configs/schedules/check-bitlocker.yaml"),
1497 ),
1498 (
1499 "check-av-signature",
1500 include_str!("../../../configs/schedules/check-av-signature.yaml"),
1501 ),
1502 (
1503 "check-cert-expiry",
1504 include_str!("../../../configs/schedules/check-cert-expiry.yaml"),
1505 ),
1506 (
1507 "check-disk-space",
1508 include_str!("../../../configs/schedules/check-disk-space.yaml"),
1509 ),
1510 (
1511 "check-pending-reboot",
1512 include_str!("../../../configs/schedules/check-pending-reboot.yaml"),
1513 ),
1514 (
1515 "check-defender-rtp",
1516 include_str!("../../../configs/schedules/check-defender-rtp.yaml"),
1517 ),
1518 (
1519 "check-firewall",
1520 include_str!("../../../configs/schedules/check-firewall.yaml"),
1521 ),
1522 ];
1523 for (name, yaml) in schedules {
1524 let s: Schedule =
1525 serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{name} schedule parse: {e}"));
1526 s.validate()
1527 .unwrap_or_else(|e| panic!("{name} schedule validate: {e}"));
1528 assert_eq!(s.job_id, name, "{name} schedule must reference its job");
1529 }
1530 }
1531
1532 /// Inventory schedule wrappers (`per_pc` cadence) must stay valid
1533 /// alongside the schedule schema. `include_str!` pins them so a
1534 /// breaking edit fails `cargo test`, not `kanade schedule create`.
1535 #[test]
1536 fn example_inventory_schedule_yamls_parse_and_validate() {
1537 let schedules = [
1538 (
1539 "inventory-hw",
1540 include_str!("../../../configs/schedules/inventory-hw.yaml"),
1541 ),
1542 (
1543 "inventory-sw",
1544 include_str!("../../../configs/schedules/inventory-sw.yaml"),
1545 ),
1546 (
1547 "inventory-driver",
1548 include_str!("../../../configs/schedules/inventory-driver.yaml"),
1549 ),
1550 ];
1551 for (name, yaml) in schedules {
1552 let s: Schedule =
1553 serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{name} schedule parse: {e}"));
1554 s.validate()
1555 .unwrap_or_else(|e| panic!("{name} schedule validate: {e}"));
1556 assert_eq!(s.job_id, name, "{name} schedule must reference its job");
1557 }
1558 }
1559
1560 #[test]
1561 fn target_is_specified_requires_at_least_one_field() {
1562 let empty = Target::default();
1563 assert!(!empty.is_specified());
1564
1565 let with_all = Target {
1566 all: true,
1567 ..Target::default()
1568 };
1569 assert!(with_all.is_specified());
1570
1571 let with_groups = Target {
1572 groups: vec!["canary".into()],
1573 ..Target::default()
1574 };
1575 assert!(with_groups.is_specified());
1576
1577 let with_pcs = Target {
1578 pcs: vec!["pc-01".into()],
1579 ..Target::default()
1580 };
1581 assert!(with_pcs.is_specified());
1582 }
1583
1584 #[test]
1585 fn manifest_deserialises_minimal_yaml() {
1586 // Matches jobs/echo-test.yaml. v0.18: no target/rollout/jitter
1587 // — those live on the schedule / exec request now.
1588 let yaml = r#"
1589id: echo-test
1590version: 0.0.1
1591execute:
1592 shell: powershell
1593 script: "echo 'kanade'"
1594 timeout: 30s
1595"#;
1596 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1597 assert_eq!(m.id, "echo-test");
1598 assert_eq!(m.version, "0.0.1");
1599 assert!(matches!(m.execute.shell, ExecuteShell::Powershell));
1600 assert_eq!(
1601 m.execute.script.as_deref().map(str::trim),
1602 Some("echo 'kanade'")
1603 );
1604 assert!(m.execute.script_file.is_none());
1605 assert!(m.execute.script_object.is_none());
1606 assert_eq!(m.execute.timeout, "30s");
1607 assert!(!m.require_approval);
1608 m.validate()
1609 .expect("inline-script manifest passes validation");
1610 }
1611
1612 #[test]
1613 fn manifest_parses_check_job_and_validates() {
1614 // An operator-defined health check (#290): a `check:` hint +
1615 // a PowerShell script that prints {status, detail}.
1616 let yaml = r#"
1617id: check-bitlocker
1618version: 0.1.0
1619execute:
1620 shell: powershell
1621 run_as: system
1622 timeout: 15s
1623 script: |
1624 [pscustomobject]@{ status = 'ok'; detail = 'all volumes protected' } | ConvertTo-Json -Compress
1625check:
1626 name: bitlocker
1627 troubleshoot: fix-bitlocker
1628"#;
1629 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1630 let check = m.check.as_ref().expect("check hint present");
1631 assert_eq!(check.name, "bitlocker");
1632 assert_eq!(check.troubleshoot.as_deref(), Some("fix-bitlocker"));
1633 // Field names default to the conventional "status" / "detail".
1634 assert_eq!(check.status_field, "status");
1635 assert_eq!(check.detail_field, "detail");
1636 assert!(m.inventory.is_none() && m.emit.is_none());
1637 m.validate().expect("check-only manifest passes validation");
1638 }
1639
1640 #[test]
1641 fn manifest_check_defaults_and_custom_fields() {
1642 // Minimal: only `name`; status/detail fields default.
1643 let m: Manifest = serde_yaml::from_str(
1644 r#"
1645id: check-disk
1646version: 0.1.0
1647execute:
1648 shell: powershell
1649 script: "[pscustomobject]@{ status = 'ok' } | ConvertTo-Json -Compress"
1650 timeout: 10s
1651check:
1652 name: disk_free
1653"#,
1654 )
1655 .expect("parse");
1656 let c = m.check.as_ref().unwrap();
1657 assert_eq!(c.name, "disk_free");
1658 assert_eq!(c.status_field, "status");
1659 assert_eq!(c.detail_field, "detail");
1660 assert!(c.troubleshoot.is_none());
1661 m.validate().expect("validates");
1662
1663 // The operator can point status/detail at any field of their
1664 // free-form inventory object.
1665 let m2: Manifest = serde_yaml::from_str(
1666 r#"
1667id: check-custom
1668version: 0.1.0
1669execute:
1670 shell: powershell
1671 script: "echo x"
1672 timeout: 10s
1673check:
1674 name: patch_level
1675 status_field: compliance
1676 detail_field: summary
1677"#,
1678 )
1679 .expect("parse");
1680 let c2 = m2.check.as_ref().unwrap();
1681 assert_eq!(c2.status_field, "compliance");
1682 assert_eq!(c2.detail_field, "summary");
1683 }
1684
1685 #[test]
1686 fn manifest_allows_check_composed_with_inventory() {
1687 // `check:` + `inventory:` COMPOSE on the same stdout object:
1688 // status/detail → Health tab, the rest → SPA projection +
1689 // explode sub-tables. Must pass validation.
1690 let yaml = r#"
1691id: check-bitlocker-detailed
1692version: 0.1.0
1693execute:
1694 shell: powershell
1695 script: "echo x"
1696 timeout: 10s
1697check:
1698 name: bitlocker
1699inventory:
1700 display:
1701 - { field: status, label: Status }
1702"#;
1703 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1704 assert!(m.check.is_some() && m.inventory.is_some());
1705 m.validate().expect("check + inventory compose");
1706 }
1707
1708 #[test]
1709 fn manifest_parses_collect_job_and_validates() {
1710 // #219: a `collect:` hint + a script that lists files on stdout.
1711 let yaml = r#"
1712id: collect-diagnostics
1713version: 0.1.0
1714execute:
1715 shell: powershell
1716 run_as: system
1717 timeout: 120s
1718 script: |
1719 @{ files = @("$env:KANADE_COLLECT_DIR/system.csv") } | ConvertTo-Json
1720collect:
1721 name: "Full diagnostics"
1722 description: "Event logs + process"
1723 max_size: 50MB
1724"#;
1725 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1726 let c = m.collect.as_ref().expect("collect hint present");
1727 assert_eq!(c.name, "Full diagnostics");
1728 assert_eq!(c.files_field, "files"); // default
1729 assert_eq!(c.max_size_bytes(), 50_000_000);
1730 m.validate().expect("collect-only manifest validates");
1731 }
1732
1733 #[test]
1734 fn manifest_collect_max_size_defaults_when_unset() {
1735 let m: Manifest = serde_yaml::from_str(
1736 r#"
1737id: collect-min
1738version: 0.1.0
1739execute:
1740 shell: powershell
1741 script: "echo x"
1742 timeout: 10s
1743collect:
1744 name: minimal
1745"#,
1746 )
1747 .expect("parse");
1748 let c = m.collect.as_ref().unwrap();
1749 assert!(c.max_size.is_none());
1750 assert_eq!(c.max_size_bytes(), DEFAULT_COLLECT_MAX_SIZE);
1751 m.validate().expect("validates");
1752 }
1753
1754 #[test]
1755 fn manifest_allows_collect_with_client() {
1756 // collect composes with client (client doesn't touch stdout):
1757 // an end user can trigger a collection from the Client App.
1758 let yaml = r#"
1759id: collect-diag-client
1760version: 0.1.0
1761execute:
1762 shell: powershell
1763 script: "echo x"
1764 timeout: 10s
1765collect:
1766 name: diagnostics
1767client:
1768 name: "Send diagnostics"
1769 category: troubleshoot
1770"#;
1771 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1772 assert!(m.collect.is_some() && m.client.is_some());
1773 m.validate().expect("collect + client compose");
1774 }
1775
1776 #[test]
1777 fn manifest_rejects_collect_combined_with_inventory() {
1778 // collect consumes stdout for its own `files` contract → can't
1779 // share with inventory/check/emit.
1780 let yaml = r#"
1781id: bad-collect-mix
1782version: 0.1.0
1783execute:
1784 shell: powershell
1785 script: "echo x"
1786 timeout: 10s
1787collect:
1788 name: diag
1789inventory:
1790 display:
1791 - { field: status, label: Status }
1792"#;
1793 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1794 let err = m
1795 .validate()
1796 .expect_err("collect + inventory must be rejected");
1797 assert!(err.contains("collect"), "error mentions collect: {err}");
1798 }
1799
1800 #[test]
1801 fn manifest_rejects_collect_combined_with_check_or_emit() {
1802 // collect is exclusive with every stdout-consuming hint, not
1803 // just inventory — guard the check + emit branches too.
1804 for extra in ["check:\n name: health\n", "emit:\n type: events\n"] {
1805 let yaml = format!(
1806 "id: bad-collect-mix\nversion: 0.1.0\nexecute:\n shell: powershell\n \
1807 script: \"echo x\"\n timeout: 10s\ncollect:\n name: diag\n{extra}"
1808 );
1809 let m: Manifest = serde_yaml::from_str(&yaml).expect("parse");
1810 let err = m
1811 .validate()
1812 .expect_err("collect + stdout-consuming hint must fail");
1813 assert!(err.contains("collect"), "error mentions collect: {err}");
1814 }
1815 }
1816
1817 #[test]
1818 fn manifest_rejects_collect_empty_name_and_bad_size() {
1819 let empty_name: Manifest = serde_yaml::from_str(
1820 r#"
1821id: c
1822version: 0.1.0
1823execute: { shell: powershell, script: "echo x", timeout: 10s }
1824collect: { name: " " }
1825"#,
1826 )
1827 .expect("parse");
1828 assert!(
1829 empty_name.validate().is_err(),
1830 "blank collect.name rejected"
1831 );
1832
1833 let bad_size: Manifest = serde_yaml::from_str(
1834 r#"
1835id: c
1836version: 0.1.0
1837execute: { shell: powershell, script: "echo x", timeout: 10s }
1838collect: { name: diag, max_size: "50 quux" }
1839"#,
1840 )
1841 .expect("parse");
1842 let err = bad_size.validate().expect_err("bad max_size rejected");
1843 assert!(err.contains("max_size"), "error mentions max_size: {err}");
1844 }
1845
1846 #[test]
1847 fn parse_size_bytes_units() {
1848 assert_eq!(parse_size_bytes("1024").unwrap(), 1024);
1849 assert_eq!(parse_size_bytes("1B").unwrap(), 1);
1850 assert_eq!(parse_size_bytes("50MB").unwrap(), 50_000_000);
1851 assert_eq!(parse_size_bytes("500 KB").unwrap(), 500_000);
1852 assert_eq!(parse_size_bytes("1GiB").unwrap(), 1024 * 1024 * 1024);
1853 assert_eq!(parse_size_bytes("2mib").unwrap(), 2 * 1024 * 1024);
1854 assert!(parse_size_bytes("").is_err());
1855 assert!(parse_size_bytes("MB").is_err());
1856 assert!(parse_size_bytes("12 zonks").is_err());
1857 }
1858
1859 #[test]
1860 fn manifest_rejects_check_combined_with_emit() {
1861 // `emit:` stdout is NDJSON (and omitted from the result), so
1862 // it can't pair with `check:` (which needs a single JSON
1863 // object on stdout).
1864 let yaml = r#"
1865id: bad-mix
1866version: 0.1.0
1867execute:
1868 shell: powershell
1869 script: "echo x"
1870 timeout: 10s
1871check:
1872 name: bitlocker
1873emit:
1874 type: events
1875"#;
1876 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1877 let err = m.validate().expect_err("emit + check must fail");
1878 assert!(err.contains("incompatible"), "err: {err}");
1879 }
1880
1881 #[test]
1882 fn manifest_rejects_emit_combined_with_inventory() {
1883 // The other half of the emit-incompatibility condition.
1884 let yaml = r#"
1885id: bad-mix-2
1886version: 0.1.0
1887execute:
1888 shell: powershell
1889 script: "echo x"
1890 timeout: 10s
1891emit:
1892 type: events
1893inventory:
1894 display:
1895 - { field: status, label: Status }
1896"#;
1897 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1898 let err = m.validate().expect_err("emit + inventory must fail");
1899 assert!(err.contains("incompatible"), "err: {err}");
1900 }
1901
1902 #[test]
1903 fn manifest_rejects_empty_check_field_names() {
1904 // Empty name / status_field / detail_field are invisible
1905 // runtime bugs (empty React key, agent reads the wrong field)
1906 // — reject them even though serde supplies non-empty defaults.
1907 let base = |inner: &str| {
1908 format!(
1909 "id: c\nversion: 0.1.0\nexecute:\n shell: powershell\n script: \"echo x\"\n timeout: 10s\ncheck:\n{inner}"
1910 )
1911 };
1912 for inner in [
1913 " name: \"\"\n",
1914 " name: ok\n status_field: \"\"\n",
1915 " name: ok\n detail_field: \" \"\n",
1916 // present-but-blank troubleshoot → broken remediation id.
1917 " name: ok\n troubleshoot: \" \"\n",
1918 ] {
1919 let m: Manifest = serde_yaml::from_str(&base(inner)).expect("parse");
1920 let err = m.validate().expect_err("empty field must fail");
1921 assert!(err.contains("must not be empty"), "err: {err}");
1922 }
1923 }
1924
1925 #[test]
1926 fn manifest_client_absent_by_default() {
1927 // A plain operator job (the overwhelming majority) carries no
1928 // `client:` block, so it never surfaces in the end-user
1929 // catalog.
1930 let yaml = r#"
1931id: echo-test
1932version: 0.0.1
1933execute:
1934 shell: powershell
1935 script: "echo 'kanade'"
1936 timeout: 30s
1937"#;
1938 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1939 assert!(m.client.is_none());
1940 m.validate().expect("operator-only job validates");
1941 }
1942
1943 #[test]
1944 fn manifest_client_parses_and_validates() {
1945 // The Client App "困ったとき" remediation job shape: a
1946 // user-invokable troubleshoot job with the end-user fields the
1947 // KLP `jobs.list` wire needs, grouped under `client:`.
1948 let yaml = r#"
1949id: fix-teams-cache
1950version: 1.0.0
1951execute:
1952 shell: powershell
1953 script: "echo clearing"
1954 timeout: 60s
1955client:
1956 name: "Teams のキャッシュをクリア"
1957 description: "Teams が重いときに試してください"
1958 category: troubleshoot
1959 icon: brush-cleaning
1960"#;
1961 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1962 let c = m.client.as_ref().expect("client block present");
1963 assert_eq!(c.name, "Teams のキャッシュをクリア");
1964 assert_eq!(
1965 c.description.as_deref(),
1966 Some("Teams が重いときに試してください")
1967 );
1968 assert_eq!(c.category, JobCategory::Troubleshoot);
1969 assert_eq!(c.icon.as_deref(), Some("brush-cleaning"));
1970 m.validate().expect("user-invokable job validates");
1971 }
1972
1973 #[test]
1974 fn manifest_client_minimal_only_name_and_category() {
1975 // description + icon are optional; name + category are the
1976 // serde-required minimum.
1977 let yaml = r#"
1978id: install-slack
1979version: 1.0.0
1980execute:
1981 shell: powershell
1982 script: "echo install"
1983 timeout: 600s
1984client:
1985 name: Slack
1986 category: catalog
1987"#;
1988 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1989 let c = m.client.as_ref().expect("client present");
1990 assert_eq!(c.category, JobCategory::Catalog);
1991 assert!(c.description.is_none() && c.icon.is_none());
1992 m.validate().expect("minimal client validates");
1993 }
1994
1995 #[test]
1996 fn manifest_client_rejects_blank_name() {
1997 // serde guarantees `name`/`category` are present; the one gap
1998 // is a present-but-blank name → empty catalog row title.
1999 let yaml = r#"
2000id: j
2001version: 1.0.0
2002execute:
2003 shell: powershell
2004 script: "echo x"
2005 timeout: 30s
2006client:
2007 name: " "
2008 category: catalog
2009"#;
2010 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2011 let err = m.validate().expect_err("blank name must fail");
2012 assert!(err.contains("client.name"), "err: {err}");
2013 }
2014
2015 #[test]
2016 fn manifest_client_rejects_blank_optional_fields() {
2017 // description / icon are optional, but a present-but-blank
2018 // value is a bug (empty subtitle / dangling icon name) — reject
2019 // it, mirroring the check: block's troubleshoot guard.
2020 for (field, line) in [
2021 ("client.description", " description: \" \"\n"),
2022 ("client.icon", " icon: \"\"\n"),
2023 ] {
2024 let yaml = format!(
2025 "id: j\nversion: 1.0.0\nexecute:\n shell: powershell\n script: \"echo x\"\n timeout: 30s\nclient:\n name: A\n category: catalog\n{line}"
2026 );
2027 let m: Manifest = serde_yaml::from_str(&yaml).expect("parse");
2028 let err = m.validate().expect_err("blank optional field must fail");
2029 assert!(err.contains(field), "expected {field} in err: {err}");
2030 }
2031 }
2032
2033 #[test]
2034 fn manifest_client_requires_category_at_parse() {
2035 // A `client:` block missing `category` is a hard parse error
2036 // (serde required field) — no manual validate() needed.
2037 let yaml = r#"
2038id: j
2039version: 1.0.0
2040execute:
2041 shell: powershell
2042 script: "echo x"
2043 timeout: 30s
2044client:
2045 name: "A job"
2046"#;
2047 let r: Result<Manifest, _> = serde_yaml::from_str(yaml);
2048 assert!(
2049 r.is_err(),
2050 "missing category must be a parse error, got {r:?}"
2051 );
2052 }
2053
2054 #[test]
2055 fn manifest_client_rejects_unknown_field() {
2056 // #492: the strict create boundary catches a fat-fingered
2057 // `displayname:` (with its path) instead of silently
2058 // dropping it; the tolerant read path accepts it.
2059 let yaml = r#"
2060id: j
2061version: 1.0.0
2062execute:
2063 shell: powershell
2064 script: "echo x"
2065 timeout: 30s
2066client:
2067 name: "A job"
2068 category: catalog
2069 displayname: oops
2070"#;
2071 let r = crate::strict::from_yaml_str::<Manifest>(yaml);
2072 let err = r.expect_err("unknown client field must be rejected at the write boundary");
2073 // serde_ignored renders the Option layer as `?`:
2074 // `client.?.displayname`. Assert on the leaf key.
2075 assert!(err.contains("displayname"), "{err}");
2076 // The READ path tolerates the same payload (gradual-upgrade
2077 // contract: an old agent must accept a newer writer's field).
2078 let m: Manifest = serde_yaml::from_str(yaml).expect("tolerant read");
2079 assert_eq!(m.client.as_ref().map(|c| c.name.as_str()), Some("A job"));
2080 }
2081
2082 #[test]
2083 fn manifest_tags_default_empty() {
2084 // The overwhelming majority of jobs carry no tags; the field
2085 // must default to an empty Vec (not fail to parse) and skip
2086 // serialisation so old readers never see the key.
2087 let yaml = r#"
2088id: echo-test
2089version: 0.0.1
2090execute:
2091 shell: powershell
2092 script: "echo 'kanade'"
2093 timeout: 30s
2094"#;
2095 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2096 assert!(m.tags.is_empty());
2097 m.validate().expect("tag-less job validates");
2098 // skip_serializing_if = empty ⇒ the key is absent from JSON.
2099 let json = serde_json::to_string(&m).expect("serialize");
2100 assert!(
2101 !json.contains("tags"),
2102 "empty tags must not serialise: {json}"
2103 );
2104 }
2105
2106 #[test]
2107 fn manifest_parses_and_validates_tags() {
2108 let yaml = r#"
2109id: check-bitlocker
2110version: 0.1.0
2111execute:
2112 shell: powershell
2113 script: "echo x"
2114 timeout: 30s
2115tags: [security, windows, health-check]
2116"#;
2117 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2118 assert_eq!(m.tags, vec!["security", "windows", "health-check"]);
2119 m.validate().expect("tagged job validates");
2120 // Round-trips through JSON (the wire format the SPA reads).
2121 let json = serde_json::to_string(&m).expect("serialize");
2122 assert!(json.contains("\"tags\""), "non-empty tags must serialise");
2123 }
2124
2125 #[test]
2126 fn manifest_rejects_blank_tag() {
2127 // A whitespace-only tag renders an empty filter chip — reject
2128 // it at the write boundary like the other blank display fields.
2129 let yaml = r#"
2130id: j
2131version: 0.1.0
2132execute:
2133 shell: powershell
2134 script: "echo x"
2135 timeout: 30s
2136tags: [ok, " "]
2137"#;
2138 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2139 let err = m.validate().expect_err("blank tag must fail");
2140 assert!(err.contains("tags must not contain empty"), "err: {err}");
2141 }
2142
2143 // #720 — wrap an `aggregate:` YAML block (already indented as a
2144 // top-level key body) into an otherwise-minimal valid manifest.
2145 fn manifest_with_aggregate(aggregate_block: &str) -> Manifest {
2146 let yaml = format!(
2147 "id: t\nversion: 0.0.1\nexecute:\n shell: powershell\n script: echo hi\n timeout: 30s\n{aggregate_block}"
2148 );
2149 serde_yaml::from_str(&yaml).expect("parse aggregate manifest")
2150 }
2151
2152 #[test]
2153 fn aggregate_accepts_full_valid_spec() {
2154 // count+group_by+exclude+sample_minutes, ratio+bool_path,
2155 // timeline+time_bucket, fleet ranking via group_by: pc_id, and a
2156 // bare total stat — alongside emit (composes with every hint).
2157 let m = manifest_with_aggregate(
2158 "emit:\n type: events\naggregate:\n\
2159 - { dashboard: Utilization, title: Top apps, kind: app_sample, agg: count, group_by: foreground.app, sample_minutes: 2, exclude: [LockApp], render: bar }\n\
2160 - { dashboard: Utilization, title: Active ratio, kind: presence, agg: ratio, bool_path: active, sample_minutes: 5, render: gauge }\n\
2161 - { dashboard: Utilization, title: By hour, kind: presence, agg: ratio, bool_path: active, time_bucket: hour, render: timeline }\n\
2162 - { dashboard: Reliability, title: Crashes by PC, scope: fleet, kind: unexpected_shutdown, agg: count, group_by: pc_id, render: bar }\n\
2163 - { dashboard: Reliability, title: Total crashes, scope: fleet, kind: unexpected_shutdown, agg: count, render: stat }\n",
2164 );
2165 m.validate().expect("valid aggregate spec");
2166 }
2167
2168 #[test]
2169 fn aggregate_rejects_empty_list() {
2170 let m = manifest_with_aggregate("aggregate: []\n");
2171 let err = m.validate().expect_err("empty list must fail");
2172 assert!(err.contains("at least one widget"), "err: {err}");
2173 }
2174
2175 #[test]
2176 fn aggregate_rejects_ratio_without_bool_path() {
2177 let m = manifest_with_aggregate(
2178 "aggregate:\n- { dashboard: D, title: T, kind: presence, agg: ratio, render: gauge }\n",
2179 );
2180 let err = m.validate().expect_err("ratio needs bool_path");
2181 assert!(err.contains("agg=ratio requires `bool_path`"), "err: {err}");
2182 }
2183
2184 #[test]
2185 fn aggregate_rejects_sum_without_value_path() {
2186 let m = manifest_with_aggregate(
2187 "aggregate:\n- { dashboard: D, title: T, kind: io, agg: sum, render: bar }\n",
2188 );
2189 let err = m.validate().expect_err("sum needs value_path");
2190 assert!(err.contains("agg=sum requires `value_path`"), "err: {err}");
2191 }
2192
2193 #[test]
2194 fn aggregate_rejects_pc_id_group_without_fleet() {
2195 let m = manifest_with_aggregate(
2196 "aggregate:\n- { dashboard: D, title: T, kind: presence, agg: count, group_by: pc_id, render: bar }\n",
2197 );
2198 let err = m.validate().expect_err("pc_id grouping needs fleet");
2199 assert!(
2200 err.contains("pc_id is only valid with scope: fleet"),
2201 "err: {err}"
2202 );
2203 }
2204
2205 #[test]
2206 fn aggregate_rejects_transform_with_pc_id_group() {
2207 let m = manifest_with_aggregate(
2208 "aggregate:\n- { dashboard: D, title: T, scope: fleet, kind: web_visit, agg: count, group_by: pc_id, transform: host, render: bar }\n",
2209 );
2210 let err = m
2211 .validate()
2212 .expect_err("transform on pc_id grouping must fail");
2213 assert!(
2214 err.contains("transform is not valid with group_by: pc_id"),
2215 "err: {err}"
2216 );
2217 }
2218
2219 #[test]
2220 fn aggregate_rejects_timeline_without_bucket() {
2221 let m = manifest_with_aggregate(
2222 "aggregate:\n- { dashboard: D, title: T, kind: presence, agg: ratio, bool_path: active, render: timeline }\n",
2223 );
2224 let err = m.validate().expect_err("timeline needs a bucket");
2225 assert!(
2226 err.contains("render=timeline requires `time_bucket`"),
2227 "err: {err}"
2228 );
2229 }
2230
2231 #[test]
2232 fn aggregate_rejects_bucket_on_non_timeline() {
2233 let m = manifest_with_aggregate(
2234 "aggregate:\n- { dashboard: D, title: T, kind: presence, agg: ratio, bool_path: active, time_bucket: hour, render: gauge }\n",
2235 );
2236 let err = m.validate().expect_err("bucket only on timeline");
2237 assert!(
2238 err.contains("time_bucket is only valid with render: timeline"),
2239 "err: {err}"
2240 );
2241 }
2242
2243 #[test]
2244 fn aggregate_rejects_unsafe_json_path() {
2245 // A path with characters outside [A-Za-z0-9_.] could break out of
2246 // the `'$.' || ?` bind — reject at create time.
2247 let m = manifest_with_aggregate(
2248 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, group_by: \"foo'; DROP\", render: bar }\n",
2249 );
2250 let err = m.validate().expect_err("unsafe path must fail");
2251 assert!(err.contains("dotted JSON path"), "err: {err}");
2252 }
2253
2254 #[test]
2255 fn aggregate_rejects_blank_title() {
2256 let m = manifest_with_aggregate(
2257 "aggregate:\n- { dashboard: D, title: \" \", kind: k, agg: count, render: stat }\n",
2258 );
2259 let err = m.validate().expect_err("blank title must fail");
2260 assert!(err.contains("title must not be empty"), "err: {err}");
2261 }
2262
2263 #[test]
2264 fn aggregate_rejects_blank_kind() {
2265 let m = manifest_with_aggregate(
2266 "aggregate:\n- { dashboard: D, title: T, kind: \" \", agg: count, render: stat }\n",
2267 );
2268 let err = m.validate().expect_err("blank kind must fail");
2269 assert!(err.contains("kind must not be empty"), "err: {err}");
2270 }
2271
2272 #[test]
2273 fn aggregate_rejects_blank_source_when_set() {
2274 let m = manifest_with_aggregate(
2275 "aggregate:\n- { dashboard: D, title: T, kind: k, source: \"\", agg: count, render: stat }\n",
2276 );
2277 let err = m.validate().expect_err("blank source must fail");
2278 assert!(
2279 err.contains("source must not be empty when set"),
2280 "err: {err}"
2281 );
2282 }
2283
2284 #[test]
2285 fn aggregate_rejects_count_with_value_path() {
2286 let m = manifest_with_aggregate(
2287 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, value_path: bytes, render: stat }\n",
2288 );
2289 let err = m.validate().expect_err("count must not use value_path");
2290 assert!(
2291 err.contains("agg=count does not use `value_path`"),
2292 "err: {err}"
2293 );
2294 }
2295
2296 #[test]
2297 fn aggregate_rejects_ratio_with_value_path() {
2298 let m = manifest_with_aggregate(
2299 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: ratio, bool_path: active, value_path: bytes, render: gauge }\n",
2300 );
2301 let err = m.validate().expect_err("ratio must not use value_path");
2302 assert!(
2303 err.contains("agg=ratio does not use `value_path`"),
2304 "err: {err}"
2305 );
2306 }
2307
2308 #[test]
2309 fn aggregate_rejects_gauge_without_ratio() {
2310 let m = manifest_with_aggregate(
2311 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, group_by: app, render: gauge }\n",
2312 );
2313 let err = m.validate().expect_err("gauge needs ratio");
2314 assert!(
2315 err.contains("render=gauge is only valid with agg: ratio"),
2316 "err: {err}"
2317 );
2318 }
2319
2320 #[test]
2321 fn aggregate_rejects_limit_without_group_by() {
2322 let m = manifest_with_aggregate(
2323 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, limit: 5, render: stat }\n",
2324 );
2325 let err = m.validate().expect_err("limit needs group_by");
2326 assert!(err.contains("limit requires `group_by`"), "err: {err}");
2327 }
2328
2329 #[test]
2330 fn aggregate_rejects_exclude_without_group_by() {
2331 let m = manifest_with_aggregate(
2332 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, exclude: [x], render: stat }\n",
2333 );
2334 let err = m.validate().expect_err("exclude needs group_by");
2335 assert!(err.contains("exclude requires `group_by`"), "err: {err}");
2336 }
2337
2338 #[test]
2339 fn aggregate_rejects_zero_limit_and_zero_sample_minutes() {
2340 let m = manifest_with_aggregate(
2341 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, group_by: app, limit: 0, render: bar }\n",
2342 );
2343 assert!(m.validate().unwrap_err().contains("limit must be > 0"));
2344 let m = manifest_with_aggregate(
2345 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, group_by: app, sample_minutes: 0, render: bar }\n",
2346 );
2347 assert!(
2348 m.validate()
2349 .unwrap_err()
2350 .contains("sample_minutes must be > 0")
2351 );
2352 }
2353
2354 #[test]
2355 fn aggregate_rejects_empty_exclude_entry() {
2356 let m = manifest_with_aggregate(
2357 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, group_by: app, exclude: [\" \"], render: bar }\n",
2358 );
2359 let err = m.validate().expect_err("blank exclude entry must fail");
2360 assert!(
2361 err.contains("exclude must not contain empty entries"),
2362 "err: {err}"
2363 );
2364 }
2365
2366 #[test]
2367 fn aggregate_rejects_malformed_dotted_paths() {
2368 for bad in [".foo", "foo.", "foo..bar", "."] {
2369 let m = manifest_with_aggregate(&format!(
2370 "aggregate:\n- {{ dashboard: D, title: T, kind: k, agg: count, group_by: \"{bad}\", render: bar }}\n"
2371 ));
2372 let err = m.validate().expect_err("malformed path must fail");
2373 assert!(err.contains("dotted JSON path"), "path {bad}: {err}");
2374 }
2375 }
2376
2377 #[test]
2378 fn aggregate_rejects_unknown_enum_value() {
2379 // An unrecognised render string deserialises to the #492 Unknown
2380 // catch-all (so old readers don't choke); validate() rejects it as
2381 // a typo at create time.
2382 let m = manifest_with_aggregate(
2383 "aggregate:\n- { dashboard: D, title: T, kind: k, agg: count, render: heatmap }\n",
2384 );
2385 let err = m.validate().expect_err("unknown render must fail");
2386 assert!(err.contains("render is not a known value"), "err: {err}");
2387 }
2388
2389 #[test]
2390 fn aggregate_accepts_order_field() {
2391 let m = manifest_with_aggregate(
2392 "aggregate:\n- { dashboard: D, title: T, order: -5, kind: k, agg: count, render: stat }\n",
2393 );
2394 m.validate().expect("order is a valid optional field");
2395 let w = &m.aggregate.as_ref().unwrap()[0];
2396 assert_eq!(w.order, Some(-5));
2397 }
2398
2399 // ── #743 View resource ───────────────────────────────────────────
2400 fn view_from(yaml_body: &str) -> View {
2401 serde_yaml::from_str(&format!("id: v1\n{yaml_body}")).expect("parse view")
2402 }
2403
2404 #[test]
2405 fn view_accepts_valid_widgets() {
2406 let v = view_from(
2407 "widgets:\n\
2408 - { dashboard: Reliability, title: Crashes by PC, scope: fleet, kind: unexpected_shutdown, agg: count, group_by: pc_id, render: bar }\n\
2409 - { dashboard: Reliability, title: Total, scope: fleet, kind: unexpected_shutdown, agg: count, render: stat }\n",
2410 );
2411 v.validate().expect("valid view");
2412 }
2413
2414 #[test]
2415 fn view_rejects_empty_widgets() {
2416 let v = view_from("widgets: []\n");
2417 let err = v.validate().expect_err("empty widgets must fail");
2418 assert!(err.contains("at least one widget"), "err: {err}");
2419 }
2420
2421 #[test]
2422 fn view_rejects_blank_id() {
2423 let v: View = serde_yaml::from_str(
2424 "id: \" \"\nwidgets:\n- { dashboard: D, title: T, kind: k, agg: count, render: stat }\n",
2425 )
2426 .expect("parse");
2427 let err = v.validate().expect_err("blank id must fail");
2428 assert!(err.contains("view.id must"), "err: {err}");
2429 }
2430
2431 #[test]
2432 fn view_rejects_unsafe_id() {
2433 // A `/` or `..` in the id would break the KV key and the
2434 // `/api/views/{id}` URL segment — reject at create time.
2435 for bad in ["../etc", "a/b", "has space", "x;y"] {
2436 let v: View = serde_yaml::from_str(&format!(
2437 "id: \"{bad}\"\nwidgets:\n- {{ dashboard: D, title: T, kind: k, agg: count, render: stat }}\n",
2438 ))
2439 .expect("parse");
2440 let err = v.validate().expect_err("unsafe id must fail");
2441 assert!(err.contains("[A-Za-z0-9._-]"), "id {bad}: {err}");
2442 }
2443 assert!(is_valid_resource_id("dashboards-fleet.v1_2"));
2444 }
2445
2446 #[test]
2447 fn view_reuses_shared_widget_validation() {
2448 // The same per-widget rule the job hint enforces (ratio needs
2449 // bool_path), reported under the `widgets[..]` field.
2450 let v = view_from(
2451 "widgets:\n- { dashboard: D, title: T, kind: presence, agg: ratio, render: gauge }\n",
2452 );
2453 let err = v.validate().expect_err("ratio without bool_path must fail");
2454 assert!(
2455 err.contains("widgets[0].agg=ratio requires `bool_path`"),
2456 "err: {err}"
2457 );
2458 }
2459
2460 fn execute_with(
2461 script: Option<&str>,
2462 script_file: Option<&str>,
2463 script_object: Option<&str>,
2464 ) -> Execute {
2465 Execute {
2466 shell: ExecuteShell::Powershell,
2467 script: script.map(str::to_owned),
2468 script_file: script_file.map(str::to_owned),
2469 script_object: script_object.map(str::to_owned),
2470 timeout: "30s".into(),
2471 run_as: RunAs::default(),
2472 cwd: None,
2473 }
2474 }
2475
2476 #[test]
2477 fn validate_accepts_inline_script() {
2478 let e = execute_with(Some("echo hi"), None, None);
2479 assert!(e.validate_script_source().is_ok());
2480 }
2481
2482 #[test]
2483 fn validate_accepts_script_file_alone() {
2484 let e = execute_with(None, Some("scripts/cleanup.ps1"), None);
2485 assert!(e.validate_script_source().is_ok());
2486 }
2487
2488 #[test]
2489 fn validate_accepts_script_object_alone() {
2490 let e = execute_with(None, None, Some("cleanup/1.0.0"));
2491 assert!(e.validate_script_source().is_ok());
2492 }
2493
2494 #[test]
2495 fn validate_treats_empty_inline_script_as_unset() {
2496 // `script: ""` + `script_object` set is the natural shape
2497 // when an operator comments out the YAML block-scalar body
2498 // but leaves the key. Should pass.
2499 let e = execute_with(Some(""), None, Some("cleanup/1.0.0"));
2500 assert!(e.validate_script_source().is_ok());
2501 }
2502
2503 #[test]
2504 fn validate_rejects_zero_sources() {
2505 let e = execute_with(None, None, None);
2506 let err = e.validate_script_source().unwrap_err();
2507 assert!(err.contains("must be set"), "got: {err}");
2508 }
2509
2510 #[test]
2511 fn validate_rejects_empty_inline_only() {
2512 let e = execute_with(Some(""), None, None);
2513 let err = e.validate_script_source().unwrap_err();
2514 assert!(err.contains("must be set"), "got: {err}");
2515 }
2516
2517 #[test]
2518 fn validate_rejects_inline_plus_file() {
2519 let e = execute_with(Some("echo hi"), Some("scripts/cleanup.ps1"), None);
2520 let err = e.validate_script_source().unwrap_err();
2521 assert!(err.contains("only one of"), "got: {err}");
2522 }
2523
2524 #[test]
2525 fn validate_rejects_inline_plus_object() {
2526 let e = execute_with(Some("echo hi"), None, Some("cleanup/1.0.0"));
2527 let err = e.validate_script_source().unwrap_err();
2528 assert!(err.contains("only one of"), "got: {err}");
2529 }
2530
2531 #[test]
2532 fn validate_rejects_file_plus_object() {
2533 let e = execute_with(None, Some("scripts/cleanup.ps1"), Some("cleanup/1.0.0"));
2534 let err = e.validate_script_source().unwrap_err();
2535 assert!(err.contains("only one of"), "got: {err}");
2536 }
2537
2538 #[test]
2539 fn validate_rejects_all_three() {
2540 let e = execute_with(
2541 Some("echo hi"),
2542 Some("scripts/cleanup.ps1"),
2543 Some("cleanup/1.0.0"),
2544 );
2545 let err = e.validate_script_source().unwrap_err();
2546 assert!(err.contains("only one of"), "got: {err}");
2547 }
2548
2549 #[test]
2550 fn manifest_deserialises_script_object_yaml() {
2551 // SPEC §2.4.1 example shape with the Object Store
2552 // reference picked over inline.
2553 let yaml = r#"
2554id: cleanup-disk-temp
2555version: 1.0.1
2556execute:
2557 shell: powershell
2558 script_object: cleanup-disk-temp/1.0.1
2559 timeout: 600s
2560"#;
2561 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2562 assert_eq!(
2563 m.execute.script_object.as_deref(),
2564 Some("cleanup-disk-temp/1.0.1")
2565 );
2566 assert!(m.execute.script.is_none());
2567 m.validate()
2568 .expect("script_object-only manifest passes validation");
2569 }
2570
2571 #[test]
2572 fn manifest_rejects_typo_in_script_field_name() {
2573 // #492: the strict create boundary catches `script_objectt`
2574 // and similar fat-fingers (with the full path) instead of
2575 // letting them silently fall through to "all three unset".
2576 let yaml = r#"
2577id: typo
2578version: 1.0.0
2579execute:
2580 shell: powershell
2581 script_objectt: oops
2582 timeout: 30s
2583"#;
2584 let err = crate::strict::from_yaml_str::<Manifest>(yaml)
2585 .expect_err("typo'd execute field must be rejected at the write boundary");
2586 assert!(err.contains("execute.script_objectt"), "{err}");
2587 }
2588
2589 #[test]
2590 fn schedule_carries_target_and_rollout() {
2591 let yaml = r#"
2592id: hourly-cleanup-canary
2593when:
2594 per_pc: { every: 1h }
2595job_id: cleanup
2596enabled: true
2597target:
2598 groups: [canary, wave1]
2599jitter: 30s
2600rollout:
2601 strategy: wave
2602 waves:
2603 - { group: canary, delay: 0s }
2604 - { group: wave1, delay: 5s }
2605"#;
2606 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2607 assert_eq!(s.id, "hourly-cleanup-canary");
2608 assert_eq!(s.job_id, "cleanup");
2609 assert_eq!(s.plan.target.groups, vec!["canary", "wave1"]);
2610 assert_eq!(s.plan.jitter.as_deref(), Some("30s"));
2611 let rollout = s.plan.rollout.expect("rollout present");
2612 assert_eq!(rollout.waves.len(), 2);
2613 assert_eq!(rollout.waves[0].group, "canary");
2614 assert_eq!(rollout.waves[1].delay, "5s");
2615 assert_eq!(rollout.strategy, RolloutStrategy::Wave);
2616 }
2617
2618 #[test]
2619 fn schedule_minimal_target_all() {
2620 let yaml = r#"
2621id: kitting
2622when:
2623 per_pc: once
2624enabled: true
2625job_id: scheduled-echo
2626target: { all: true }
2627"#;
2628 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2629 assert_eq!(s.id, "kitting");
2630 assert_eq!(s.when, When::PerPc(PerPolicy::Once(OnceLiteral::Once)));
2631 assert!(s.enabled);
2632 assert_eq!(s.job_id, "scheduled-echo");
2633 assert!(s.plan.target.all);
2634 assert!(s.plan.rollout.is_none());
2635 assert!(s.plan.jitter.is_none());
2636 assert!(s.active.is_empty());
2637 }
2638
2639 #[test]
2640 fn schedule_enabled_defaults_to_true() {
2641 let yaml = r#"
2642id: x
2643when:
2644 per_pc: once
2645job_id: y
2646target: { all: true }
2647"#;
2648 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2649 assert!(s.enabled);
2650 }
2651
2652 #[test]
2653 fn schedule_tags_default_empty_and_skip_serialise() {
2654 let yaml = r#"
2655id: x
2656when:
2657 per_pc: once
2658job_id: y
2659target: { all: true }
2660"#;
2661 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2662 assert!(s.tags.is_empty());
2663 s.validate().expect("tag-less schedule validates");
2664 let json = serde_json::to_string(&s).expect("serialize");
2665 assert!(
2666 !json.contains("tags"),
2667 "empty tags must not serialise: {json}"
2668 );
2669 }
2670
2671 #[test]
2672 fn schedule_parses_and_validates_tags() {
2673 let yaml = r#"
2674id: weekly-cleanup
2675when:
2676 per_pc: { every: 1h }
2677job_id: cleanup
2678target: { all: true }
2679tags: [weekly, maintenance]
2680"#;
2681 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2682 assert_eq!(s.tags, vec!["weekly", "maintenance"]);
2683 s.validate().expect("tagged schedule validates");
2684 }
2685
2686 #[test]
2687 fn schedule_rejects_blank_tag() {
2688 let yaml = r#"
2689id: x
2690when:
2691 per_pc: once
2692job_id: y
2693target: { all: true }
2694tags: [ok, " "]
2695"#;
2696 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2697 let err = s.validate().expect_err("blank tag must fail");
2698 assert!(err.contains("tags must not contain empty"), "err: {err}");
2699 }
2700
2701 // ---- `when` parsing (#418 Phase 1) ----
2702
2703 fn schedule_yaml_with(when_block: &str) -> String {
2704 format!(
2705 r#"
2706id: x
2707when:
2708{when_block}
2709job_id: y
2710target: {{ all: true }}
2711"#
2712 )
2713 }
2714
2715 #[test]
2716 fn when_per_pc_every_parses_unquoted_humantime() {
2717 // `6h` is digit-led but non-numeric → YAML string, same as
2718 // the old `cooldown: 6h` convention. No quotes needed.
2719 let s: Schedule =
2720 serde_yaml::from_str(&schedule_yaml_with(" per_pc: { every: 6h }")).expect("parse");
2721 assert_eq!(
2722 s.when,
2723 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() }))
2724 );
2725 }
2726
2727 #[test]
2728 fn when_per_target_every_parses() {
2729 let s: Schedule = serde_yaml::from_str(&schedule_yaml_with(" per_target: { every: 24h }"))
2730 .expect("parse");
2731 assert_eq!(
2732 s.when,
2733 When::PerTarget(PerPolicy::Every(EverySpec {
2734 every: "24h".into()
2735 }))
2736 );
2737 }
2738
2739 #[test]
2740 fn when_per_target_once_parses() {
2741 // Falls out of the shared PerPolicy shape and decide_fire
2742 // already implements it ("any one pc succeeds → skip the
2743 // target forever"), so it is allowed, not rejected.
2744 let s: Schedule =
2745 serde_yaml::from_str(&schedule_yaml_with(" per_target: once")).expect("parse");
2746 assert_eq!(s.when, When::PerTarget(PerPolicy::Once(OnceLiteral::Once)));
2747 }
2748
2749 #[test]
2750 fn when_calendar_time_parses() {
2751 let s: Schedule = serde_yaml::from_str(&schedule_yaml_with(
2752 " calendar:\n at: \"09:00\"\n days: [mon-fri]",
2753 ))
2754 .expect("parse");
2755 match &s.when {
2756 When::Calendar(c) => {
2757 assert_eq!(c.at, "09:00");
2758 assert_eq!(c.days, vec!["mon-fri"]);
2759 }
2760 other => panic!("expected calendar, got {other:?}"),
2761 }
2762 }
2763
2764 #[test]
2765 fn when_calendar_days_default_empty() {
2766 let s: Schedule =
2767 serde_yaml::from_str(&schedule_yaml_with(" calendar:\n at: \"09:00\""))
2768 .expect("parse");
2769 match &s.when {
2770 When::Calendar(c) => assert!(c.days.is_empty(), "days defaults to empty (= daily)"),
2771 other => panic!("expected calendar, got {other:?}"),
2772 }
2773 }
2774
2775 #[test]
2776 fn when_calendar_datetime_parses_all_separators() {
2777 // one-shot: date+time in hyphen / ISO-T / slash forms
2778 for at in ["2026-06-10 09:00", "2026-06-10T09:00", "2026/06/10 09:00"] {
2779 let block = format!(" calendar:\n at: \"{at}\"");
2780 let s: Schedule = serde_yaml::from_str(&schedule_yaml_with(&block))
2781 .unwrap_or_else(|e| panic!("parse '{at}': {e}"));
2782 match &s.when {
2783 When::Calendar(c) => {
2784 use chrono::Datelike;
2785 let p = c.parse_at().expect("parse_at");
2786 let d = p.date.expect("datetime at carries a date");
2787 assert_eq!((d.year(), d.month(), d.day()), (2026, 6, 10), "for '{at}'");
2788 }
2789 other => panic!("expected calendar, got {other:?}"),
2790 }
2791 }
2792 }
2793
2794 #[test]
2795 fn when_rejects_bad_once_keyword() {
2796 // `onec` must be a parse error, not a silently-absorbed
2797 // string (OnceLiteral is a single-variant enum for exactly
2798 // this reason).
2799 let r: Result<Schedule, _> = serde_yaml::from_str(&schedule_yaml_with(" per_pc: onec"));
2800 assert!(r.is_err(), "expected parse error, got {r:?}");
2801 }
2802
2803 #[test]
2804 fn when_rejects_unknown_key_in_every() {
2805 // `{ evry: 6h }` still fails on the tolerant read path: the
2806 // required `every` key is missing, so no PerPolicy variant
2807 // matches (#492 removed deny_unknown_fields, but required
2808 // keys keep the untagged disambiguation honest).
2809 let r: Result<Schedule, _> =
2810 serde_yaml::from_str(&schedule_yaml_with(" per_pc: { evry: 6h }"));
2811 assert!(r.is_err(), "expected parse error, got {r:?}");
2812 }
2813
2814 #[test]
2815 fn when_rejects_unknown_variant() {
2816 let r: Result<Schedule, _> =
2817 serde_yaml::from_str(&schedule_yaml_with(" per_galaxy: once"));
2818 assert!(r.is_err(), "expected parse error, got {r:?}");
2819 }
2820
2821 #[test]
2822 fn when_rejects_old_top_level_cron_field() {
2823 // Pre-#418 shape: top-level `cron:` + no `when:`. Must fail
2824 // loudly (missing `when`), which is what turns stale KV
2825 // blobs into warn-skips after the upgrade.
2826 let yaml = r#"
2827id: x
2828cron: "* * * * * *"
2829job_id: y
2830target: { all: true }
2831"#;
2832 let r: Result<Schedule, _> = serde_yaml::from_str(yaml);
2833 assert!(r.is_err(), "expected parse error, got {r:?}");
2834 }
2835
2836 #[test]
2837 fn when_rejects_retired_cron_escape_hatch() {
2838 // #418 Phase 2 retired `when: { cron: "..." }`. A raw cron
2839 // is now an unknown variant → parse error (operators use the
2840 // calendar form instead).
2841 let r: Result<Schedule, _> =
2842 serde_yaml::from_str(&schedule_yaml_with(" cron: \"0 0 9 * * mon-fri\""));
2843 assert!(
2844 r.is_err(),
2845 "expected parse error for retired cron, got {r:?}"
2846 );
2847 }
2848
2849 #[test]
2850 fn when_round_trips_json_and_yaml() {
2851 // Round-trip through the full Schedule: that is the wire
2852 // unit for both stores (JSON catalog KV + YAML mirror), and
2853 // it exercises the singleton_map field attribute that keeps
2854 // serde_yaml on the map shape instead of `!per_pc` tags.
2855 for when in [
2856 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
2857 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
2858 When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
2859 When::PerTarget(PerPolicy::Every(EverySpec {
2860 every: "24h".into(),
2861 })),
2862 calendar("09:00", &["mon-fri"]),
2863 calendar("2026-06-10 09:00", &[]),
2864 When::On(vec![OnTrigger::Startup]),
2865 When::On(vec![OnTrigger::Startup, OnTrigger::Logon]),
2866 When::On(vec![OnTrigger::Lock, OnTrigger::Unlock]),
2867 When::On(vec![OnTrigger::NetworkChange]),
2868 ] {
2869 // Event triggers are agent-only; the rest validate on backend.
2870 let runs_on = if matches!(when, When::On(_)) {
2871 RunsOn::Agent
2872 } else {
2873 RunsOn::Backend
2874 };
2875 let s = schedule_with(when.clone(), runs_on);
2876
2877 let json = serde_json::to_string(&s).expect("json serialise");
2878 let back: Schedule = serde_json::from_str(&json).expect("json deserialise");
2879 assert_eq!(back.when, when, "json round-trip for {when}");
2880
2881 let yaml = serde_yaml::to_string(&s).expect("yaml serialise");
2882 assert!(
2883 !yaml.contains('!'),
2884 "yaml must use the map shape, not tags: {yaml}"
2885 );
2886 let back: Schedule = serde_yaml::from_str(&yaml).expect("yaml deserialise");
2887 assert_eq!(back.when, when, "yaml round-trip for {when}");
2888 }
2889 }
2890
2891 #[test]
2892 fn when_once_serialises_as_bare_keyword() {
2893 // The wire shape operators see in the YAML mirror must stay
2894 // the ergonomic `per_pc: once`, not a one-variant map.
2895 let json = serde_json::to_value(When::PerPc(PerPolicy::Once(OnceLiteral::Once)))
2896 .expect("serialise");
2897 assert_eq!(json, serde_json::json!({ "per_pc": "once" }));
2898 }
2899
2900 #[test]
2901 fn when_displays_operator_summary() {
2902 for (when, expected) in [
2903 (
2904 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
2905 "per_pc once",
2906 ),
2907 (
2908 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
2909 "per_pc every 6h",
2910 ),
2911 (
2912 When::PerTarget(PerPolicy::Every(EverySpec {
2913 every: "24h".into(),
2914 })),
2915 "per_target every 24h",
2916 ),
2917 (calendar("09:00", &["mon-fri"]), "at 09:00 [mon-fri]"),
2918 (calendar("2026-06-10 09:00", &[]), "at 2026-06-10 09:00"),
2919 (When::On(vec![OnTrigger::Startup]), "on [startup]"),
2920 (
2921 When::On(vec![OnTrigger::Startup, OnTrigger::Logon]),
2922 "on [startup,logon]",
2923 ),
2924 (
2925 When::On(vec![OnTrigger::Lock, OnTrigger::Unlock]),
2926 "on [lock,unlock]",
2927 ),
2928 (
2929 When::On(vec![OnTrigger::NetworkChange]),
2930 "on [network_change]",
2931 ),
2932 ] {
2933 assert_eq!(when.to_string(), expected);
2934 }
2935 }
2936
2937 // ---- lowering (#418: when → engine vocabulary) ----
2938
2939 fn schedule_with(when: When, runs_on: RunsOn) -> Schedule {
2940 Schedule {
2941 id: "x".into(),
2942 when,
2943 job_id: "y".into(),
2944 plan: FanoutPlan::default(),
2945 active: Active::default(),
2946 constraints: Constraints::default(),
2947 on_failure: OnFailure::default(),
2948 tz: ScheduleTz::default(),
2949 starting_deadline: None,
2950 runs_on,
2951 enabled: true,
2952 tags: Vec::new(),
2953 origin: None,
2954 }
2955 }
2956
2957 fn calendar(at: &str, days: &[&str]) -> When {
2958 When::Calendar(CalendarSpec {
2959 at: at.into(),
2960 days: days.iter().map(|d| (*d).to_string()).collect(),
2961 })
2962 }
2963
2964 #[test]
2965 fn next_calendar_fire_returns_next_utc_occurrence() {
2966 use chrono::TimeZone;
2967 // Daily 09:00, evaluated in UTC. From 08:00 the same day, the
2968 // next strict occurrence is 09:00 that day.
2969 let mut s = schedule_with(calendar("09:00", &[]), RunsOn::Backend);
2970 s.tz = ScheduleTz::Utc;
2971 let now = chrono::Utc.with_ymd_and_hms(2026, 6, 9, 8, 0, 0).unwrap();
2972 let next = s.next_calendar_fire(now).expect("calendar has a next fire");
2973 assert_eq!(
2974 next,
2975 chrono::Utc.with_ymd_and_hms(2026, 6, 9, 9, 0, 0).unwrap()
2976 );
2977 }
2978
2979 #[test]
2980 fn next_calendar_fire_is_strictly_after_now() {
2981 use chrono::TimeZone;
2982 // Standing exactly on a fire instant must preview the *next*
2983 // one (inclusive = false), not the one firing right now.
2984 let mut s = schedule_with(calendar("09:00", &[]), RunsOn::Backend);
2985 s.tz = ScheduleTz::Utc;
2986 let on_fire = chrono::Utc.with_ymd_and_hms(2026, 6, 9, 9, 0, 0).unwrap();
2987 let next = s
2988 .next_calendar_fire(on_fire)
2989 .expect("calendar has a next fire");
2990 assert_eq!(
2991 next,
2992 chrono::Utc.with_ymd_and_hms(2026, 6, 10, 9, 0, 0).unwrap()
2993 );
2994 }
2995
2996 #[test]
2997 fn next_calendar_fire_none_for_reconcile_shapes() {
2998 // `per_pc` / `per_target` lower to the every-minute poll cron —
2999 // no discrete upcoming event to preview, so `None`.
3000 let now = chrono::Utc::now();
3001 for when in [
3002 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3003 When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
3004 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
3005 When::PerTarget(PerPolicy::Every(EverySpec {
3006 every: "24h".into(),
3007 })),
3008 ] {
3009 let s = schedule_with(when, RunsOn::Backend);
3010 assert!(
3011 s.next_calendar_fire(now).is_none(),
3012 "reconcile shapes have no calendar fire",
3013 );
3014 }
3015 }
3016
3017 // ---- preview_fires (#418 dry-run / preview) ----
3018
3019 fn cal_utc(at: &str, days: &[&str]) -> Schedule {
3020 let mut s = schedule_with(calendar(at, days), RunsOn::Backend);
3021 s.tz = ScheduleTz::Utc; // host-independent assertions
3022 s
3023 }
3024
3025 #[test]
3026 fn preview_lists_next_calendar_occurrences() {
3027 use chrono::TimeZone;
3028 // Weekday 09:00, from Wed 2026-06-10 00:00 UTC: the next five
3029 // fires skip the weekend (Sat 13 / Sun 14).
3030 let s = cal_utc("09:00", &["mon-fri"]);
3031 let now = chrono::Utc.with_ymd_and_hms(2026, 6, 10, 0, 0, 0).unwrap();
3032 let got = s.preview_fires(now, 5);
3033 let want: Vec<_> = [
3034 (2026, 6, 10), // Wed
3035 (2026, 6, 11), // Thu
3036 (2026, 6, 12), // Fri
3037 (2026, 6, 15), // Mon (skips Sat 13 / Sun 14)
3038 (2026, 6, 16), // Tue
3039 ]
3040 .iter()
3041 .map(|(y, m, d)| chrono::Utc.with_ymd_and_hms(*y, *m, *d, 9, 0, 0).unwrap())
3042 .collect();
3043 assert_eq!(got, want);
3044 }
3045
3046 #[test]
3047 fn preview_handles_nth_and_last_weekday() {
3048 use chrono::TimeZone;
3049 let now = chrono::Utc.with_ymd_and_hms(2026, 6, 1, 0, 0, 0).unwrap();
3050 // 2nd Tuesday (Patch Tuesday): Jun 9, Jul 14 2026.
3051 let nth = cal_utc("09:00", &["tue#2"]).preview_fires(now, 2);
3052 assert_eq!(
3053 nth,
3054 vec![
3055 chrono::Utc.with_ymd_and_hms(2026, 6, 9, 9, 0, 0).unwrap(),
3056 chrono::Utc.with_ymd_and_hms(2026, 7, 14, 9, 0, 0).unwrap(),
3057 ]
3058 );
3059 // Last Friday of the month: Jun 26, Jul 31 2026.
3060 let last = cal_utc("22:00", &["friL"]).preview_fires(now, 2);
3061 assert_eq!(
3062 last,
3063 vec![
3064 chrono::Utc.with_ymd_and_hms(2026, 6, 26, 22, 0, 0).unwrap(),
3065 chrono::Utc.with_ymd_and_hms(2026, 7, 31, 22, 0, 0).unwrap(),
3066 ]
3067 );
3068 }
3069
3070 #[test]
3071 fn preview_is_empty_for_reconcile_and_zero_count() {
3072 let now = chrono::Utc::now();
3073 // reconcile shapes have no discrete fire times
3074 let recon = schedule_with(
3075 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
3076 RunsOn::Backend,
3077 );
3078 assert!(recon.preview_fires(now, 5).is_empty());
3079 // count == 0 yields nothing even for a calendar
3080 assert!(cal_utc("09:00", &[]).preview_fires(now, 0).is_empty());
3081 }
3082
3083 #[test]
3084 fn preview_skips_outside_active_window() {
3085 use chrono::TimeZone;
3086 // Daily 09:00, active only [2026-06-15, 2026-06-17). Occurrences
3087 // before `from` are skipped; `until` is exclusive, so 06-17's
3088 // fire is out — leaving exactly the 15th and 16th.
3089 let mut s = cal_utc("09:00", &[]);
3090 s.active = Active {
3091 from: Some("2026-06-15".into()),
3092 until: Some("2026-06-17".into()),
3093 };
3094 let now = chrono::Utc.with_ymd_and_hms(2026, 6, 10, 0, 0, 0).unwrap();
3095 let got = s.preview_fires(now, 5);
3096 assert_eq!(
3097 got,
3098 vec![
3099 chrono::Utc.with_ymd_and_hms(2026, 6, 15, 9, 0, 0).unwrap(),
3100 chrono::Utc.with_ymd_and_hms(2026, 6, 16, 9, 0, 0).unwrap(),
3101 ]
3102 );
3103 }
3104
3105 #[test]
3106 fn preview_empty_when_calendar_time_outside_window() {
3107 use chrono::TimeZone;
3108 // Fires at 09:00 but the maintenance window is overnight — it can
3109 // never run, so the preview is empty (matches
3110 // `calendar_outside_window`), and the scan still terminates.
3111 let mut s = cal_utc("09:00", &[]);
3112 s.constraints = Constraints {
3113 window: Some("22:00-05:00".into()),
3114 ..Constraints::default()
3115 };
3116 let now = chrono::Utc.with_ymd_and_hms(2026, 6, 10, 0, 0, 0).unwrap();
3117 assert!(s.preview_fires(now, 5).is_empty());
3118 // Every candidate tick is rejected, so this also exercises the
3119 // SCAN_CAP bound: a large `count` must still terminate (and
3120 // return empty) rather than spin (claude #578 review).
3121 assert!(s.preview_fires(now, 50).is_empty());
3122 }
3123
3124 #[test]
3125 fn preview_past_one_shot_is_empty() {
3126 use chrono::TimeZone;
3127 // A dated one-shot whose instant has passed never fires again.
3128 let s = cal_utc("2026-06-10 09:00", &[]);
3129 let now = chrono::Utc.with_ymd_and_hms(2026, 6, 11, 0, 0, 0).unwrap();
3130 assert!(s.preview_fires(now, 5).is_empty());
3131 // …but from before it, the single future fire shows up.
3132 let before = chrono::Utc.with_ymd_and_hms(2026, 6, 1, 0, 0, 0).unwrap();
3133 assert_eq!(
3134 s.preview_fires(before, 5),
3135 vec![chrono::Utc.with_ymd_and_hms(2026, 6, 10, 9, 0, 0).unwrap()]
3136 );
3137 }
3138
3139 #[test]
3140 fn lowering_matches_the_418_table() {
3141 let cases = [
3142 (
3143 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3144 (POLL_CRON, ExecMode::OncePerPc, None),
3145 ),
3146 (
3147 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
3148 (POLL_CRON, ExecMode::OncePerPc, Some("6h")),
3149 ),
3150 (
3151 When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
3152 (POLL_CRON, ExecMode::OncePerTarget, None),
3153 ),
3154 (
3155 When::PerTarget(PerPolicy::Every(EverySpec {
3156 every: "24h".into(),
3157 })),
3158 (POLL_CRON, ExecMode::OncePerTarget, Some("24h")),
3159 ),
3160 // calendar repeating → 6-field cron
3161 (
3162 calendar("09:00", &["mon-fri"]),
3163 ("0 0 9 * * mon-fri", ExecMode::EveryTick, None),
3164 ),
3165 // calendar daily (no days) → DOW *
3166 (
3167 calendar("18:30", &[]),
3168 ("0 30 18 * * *", ExecMode::EveryTick, None),
3169 ),
3170 // calendar one-shot → 7-field year cron
3171 (
3172 calendar("2026-06-10 09:00", &[]),
3173 ("0 0 9 10 6 * 2026", ExecMode::EveryTick, None),
3174 ),
3175 ];
3176 for (when, (cron, mode, cooldown)) in cases {
3177 let l = schedule_with(when.clone(), RunsOn::Backend).lowered();
3178 assert_eq!(l.cron, cron, "cron for {when}");
3179 assert_eq!(l.mode, mode, "mode for {when}");
3180 assert_eq!(l.cooldown.as_deref(), cooldown, "cooldown for {when}");
3181 }
3182 }
3183
3184 #[test]
3185 fn lowered_carries_schedule_tz() {
3186 for (tz, want) in [
3187 (ScheduleTz::Local, ScheduleTz::Local),
3188 (ScheduleTz::Utc, ScheduleTz::Utc),
3189 ] {
3190 let mut s = schedule_with(calendar("09:00", &["mon-fri"]), RunsOn::Backend);
3191 s.tz = tz;
3192 assert_eq!(s.lowered().tz, want, "calendar carries tz");
3193 // reconcile shapes carry tz too (for the active-window check)
3194 let mut s = schedule_with(
3195 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3196 RunsOn::Backend,
3197 );
3198 s.tz = tz;
3199 assert_eq!(s.lowered().tz, want, "reconcile carries tz");
3200 }
3201 }
3202
3203 #[test]
3204 fn poll_cron_is_accepted_by_the_engine_parser() {
3205 // POLL_CRON is system-generated — if the engine's parser
3206 // ever rejected it every reconcile schedule would die at
3207 // register time. Validate it with the same croner config
3208 // (Seconds::Required, dom_and_dow, year optional).
3209 croner::parser::CronParser::builder()
3210 .seconds(croner::parser::Seconds::Required)
3211 .dom_and_dow(true)
3212 .build()
3213 .parse(POLL_CRON)
3214 .expect("POLL_CRON must parse");
3215 }
3216
3217 // ---- Schedule::validate() (#418 decision F) ----
3218
3219 #[test]
3220 fn validate_accepts_reconcile_shapes() {
3221 for when in [
3222 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3223 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
3224 When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
3225 When::PerTarget(PerPolicy::Every(EverySpec {
3226 every: "24h".into(),
3227 })),
3228 ] {
3229 schedule_with(when.clone(), RunsOn::Backend)
3230 .validate()
3231 .unwrap_or_else(|e| panic!("{when} should validate: {e}"));
3232 }
3233 }
3234
3235 #[test]
3236 fn validate_accepts_per_pc_on_agent() {
3237 schedule_with(
3238 When::PerPc(PerPolicy::Every(EverySpec { every: "1h".into() })),
3239 RunsOn::Agent,
3240 )
3241 .validate()
3242 .expect("per_pc + agent is the offline-inventory shape");
3243 }
3244
3245 // ---- #418 event triggers (when: { on }) ----
3246
3247 #[test]
3248 fn validate_accepts_event_on_agent() {
3249 for triggers in [
3250 vec![OnTrigger::Startup],
3251 vec![OnTrigger::Logon],
3252 vec![OnTrigger::Lock],
3253 vec![OnTrigger::Unlock],
3254 vec![OnTrigger::NetworkChange],
3255 vec![
3256 OnTrigger::Startup,
3257 OnTrigger::Logon,
3258 OnTrigger::Lock,
3259 OnTrigger::Unlock,
3260 OnTrigger::NetworkChange,
3261 ],
3262 ] {
3263 schedule_with(When::On(triggers), RunsOn::Agent)
3264 .validate()
3265 .expect("when.on is valid on runs_on: agent");
3266 }
3267 }
3268
3269 #[test]
3270 fn validate_rejects_event_on_backend() {
3271 let err = schedule_with(When::On(vec![OnTrigger::Startup]), RunsOn::Backend)
3272 .validate()
3273 .unwrap_err();
3274 assert!(err.contains("when.on"), "got: {err}");
3275 assert!(err.contains("runs_on: agent"), "got: {err}");
3276 }
3277
3278 #[test]
3279 fn validate_rejects_empty_event_list() {
3280 let err = schedule_with(When::On(vec![]), RunsOn::Agent)
3281 .validate()
3282 .unwrap_err();
3283 assert!(err.contains("when.on"), "got: {err}");
3284 assert!(err.contains("at least one"), "got: {err}");
3285 }
3286
3287 #[test]
3288 fn event_schedule_lowers_to_event_mode_and_is_event() {
3289 let s = schedule_with(When::On(vec![OnTrigger::Startup]), RunsOn::Agent);
3290 assert!(s.is_event());
3291 assert_eq!(s.lowered().mode, ExecMode::Event);
3292 assert_eq!(s.event_triggers(), &[OnTrigger::Startup]);
3293 // non-event schedules report no triggers.
3294 let cal = schedule_with(calendar("09:00", &[]), RunsOn::Backend);
3295 assert!(!cal.is_event());
3296 assert!(cal.event_triggers().is_empty());
3297 }
3298
3299 // ---- #418 constraints.require (env gates) ----
3300
3301 fn require_schedule(req: Require, runs_on: RunsOn) -> Schedule {
3302 let mut s = schedule_with(
3303 When::PerPc(PerPolicy::Every(EverySpec { every: "1m".into() })),
3304 runs_on,
3305 );
3306 s.constraints.require = Some(req);
3307 s
3308 }
3309
3310 #[test]
3311 fn require_met_combinations() {
3312 use std::time::Duration;
3313 let idle = |m: u64| Some(Duration::from_secs(m * 60));
3314 // Builder for the sensed state: (ac, idle, cpu, network).
3315 let env = |ac, idle, cpu, net| EnvState {
3316 ac_online: ac,
3317 idle,
3318 cpu_pct: cpu,
3319 network_up: net,
3320 };
3321 // Empty require — always met regardless of sensed state.
3322 assert!(require_met(
3323 &Require::default(),
3324 &env(false, None, None, false)
3325 ));
3326 // ac_power: only on AC.
3327 let ac = Require {
3328 ac_power: true,
3329 ..Default::default()
3330 };
3331 assert!(!require_met(&ac, &env(false, None, None, true)));
3332 assert!(require_met(&ac, &env(true, None, None, false)));
3333 // idle: needs >= the configured min; None idle never satisfies.
3334 let idle10 = Require {
3335 idle: Some("10m".into()),
3336 ..Default::default()
3337 };
3338 assert!(!require_met(&idle10, &env(true, None, None, true)));
3339 assert!(!require_met(&idle10, &env(true, idle(5), None, true)));
3340 assert!(require_met(&idle10, &env(true, idle(15), None, true)));
3341 assert!(require_met(&idle10, &env(true, idle(10), None, true))); // boundary inclusive
3342 // cpu_below: needs CPU strictly < threshold; None cpu never satisfies.
3343 let cpu20 = Require {
3344 cpu_below: Some(20.0),
3345 ..Default::default()
3346 };
3347 assert!(!require_met(&cpu20, &env(true, None, None, true))); // no sample → fail-closed
3348 assert!(!require_met(&cpu20, &env(true, None, Some(20.0), true))); // == threshold
3349 assert!(!require_met(&cpu20, &env(true, None, Some(55.0), true))); // busy
3350 assert!(require_met(&cpu20, &env(true, None, Some(5.0), true))); // quiet
3351 // network: only when online.
3352 let net = Require {
3353 network: true,
3354 ..Default::default()
3355 };
3356 assert!(!require_met(&net, &env(true, None, None, false))); // offline
3357 assert!(require_met(&net, &env(true, None, None, true))); // online
3358 // all four: AND.
3359 let all = Require {
3360 ac_power: true,
3361 idle: Some("10m".into()),
3362 cpu_below: Some(20.0),
3363 network: true,
3364 };
3365 assert!(!require_met(&all, &env(false, idle(20), Some(5.0), true))); // on battery
3366 assert!(!require_met(&all, &env(true, idle(1), Some(5.0), true))); // not idle enough
3367 assert!(!require_met(&all, &env(true, idle(20), Some(50.0), true))); // busy
3368 assert!(!require_met(&all, &env(true, idle(20), Some(5.0), false))); // offline
3369 assert!(require_met(&all, &env(true, idle(20), Some(5.0), true)));
3370 // An unparseable idle is treated as no-requirement by require_met
3371 // (validate rejects it at create time, so this only guards a
3372 // hand-edited blob): ac still gates.
3373 let bad = Require {
3374 ac_power: true,
3375 idle: Some("garbage".into()),
3376 ..Default::default()
3377 };
3378 assert!(require_met(&bad, &env(true, None, None, true)));
3379 assert!(!require_met(&bad, &env(false, None, None, true)));
3380 }
3381
3382 #[test]
3383 fn validate_accepts_and_rejects_cpu_below() {
3384 // In-range accepted.
3385 require_schedule(
3386 Require {
3387 cpu_below: Some(20.0),
3388 ..Default::default()
3389 },
3390 RunsOn::Agent,
3391 )
3392 .validate()
3393 .expect("cpu_below 20 is valid");
3394 // Upper boundary: 100.0 is accepted (fires unless CPU is exactly
3395 // 100%). Pins the inclusive upper bound against a future c < 100.0.
3396 require_schedule(
3397 Require {
3398 cpu_below: Some(100.0),
3399 ..Default::default()
3400 },
3401 RunsOn::Agent,
3402 )
3403 .validate()
3404 .expect("cpu_below 100 is valid");
3405 // Out of range rejected (0 and >100).
3406 for bad in [0.0, -5.0, 100.1] {
3407 let err = require_schedule(
3408 Require {
3409 cpu_below: Some(bad),
3410 ..Default::default()
3411 },
3412 RunsOn::Agent,
3413 )
3414 .validate()
3415 .unwrap_err();
3416 assert!(
3417 err.contains("constraints.require.cpu_below"),
3418 "cpu_below {bad}: {err}"
3419 );
3420 }
3421 }
3422
3423 #[test]
3424 fn validate_accepts_require_on_agent() {
3425 require_schedule(
3426 Require {
3427 ac_power: true,
3428 idle: Some("10m".into()),
3429 cpu_below: Some(20.0),
3430 network: true,
3431 },
3432 RunsOn::Agent,
3433 )
3434 .validate()
3435 .expect("constraints.require is valid on runs_on: agent");
3436 }
3437
3438 #[test]
3439 fn validate_rejects_require_on_backend() {
3440 let err = require_schedule(
3441 Require {
3442 ac_power: true,
3443 ..Default::default()
3444 },
3445 RunsOn::Backend,
3446 )
3447 .validate()
3448 .unwrap_err();
3449 assert!(err.contains("constraints.require"), "got: {err}");
3450 assert!(err.contains("runs_on: agent"), "got: {err}");
3451
3452 // An idle-only require (ac_power: false) is also non-empty
3453 // (is_empty folds the fields) and must reject on backend too —
3454 // guards against a regression in Require::is_empty.
3455 let err = require_schedule(
3456 Require {
3457 idle: Some("10m".into()),
3458 ..Default::default()
3459 },
3460 RunsOn::Backend,
3461 )
3462 .validate()
3463 .unwrap_err();
3464 assert!(
3465 err.contains("constraints.require"),
3466 "idle-only on backend: {err}"
3467 );
3468 }
3469
3470 #[test]
3471 fn validate_rejects_bad_require_idle() {
3472 let err = require_schedule(
3473 Require {
3474 idle: Some("not-a-duration".into()),
3475 ..Default::default()
3476 },
3477 RunsOn::Agent,
3478 )
3479 .validate()
3480 .unwrap_err();
3481 assert!(err.contains("constraints.require.idle"), "got: {err}");
3482 }
3483
3484 #[test]
3485 fn require_round_trips_and_skips_empty() {
3486 // ac_power: false is skipped; an all-default require nested in
3487 // constraints is omitted (is_empty folds it in).
3488 let yaml = "id: s\nwhen: { per_pc: { every: 1m } }\njob_id: j\nruns_on: agent\n\
3489 constraints: { require: { ac_power: true, idle: 10m, cpu_below: 20, \
3490 network: true } }\n";
3491 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
3492 let req = s.constraints.require.as_ref().expect("require present");
3493 assert!(req.ac_power);
3494 assert_eq!(req.idle.as_deref(), Some("10m"));
3495 assert_eq!(req.cpu_below, Some(20.0));
3496 assert!(req.network);
3497 // Re-serialize: idle + cpu_below + network present, ac_power true.
3498 let back = serde_json::to_string(&s.constraints).unwrap();
3499 assert!(back.contains("\"idle\":\"10m\""), "got: {back}");
3500 assert!(back.contains("\"cpu_below\":20"), "got: {back}");
3501 assert!(back.contains("\"network\":true"), "got: {back}");
3502 // An empty require is omitted entirely by is_empty.
3503 let mut empty = s.clone();
3504 empty.constraints.require = Some(Require::default());
3505 assert!(empty.constraints.is_empty());
3506 }
3507
3508 #[test]
3509 fn validate_rejects_per_target_on_agent() {
3510 let err = schedule_with(
3511 When::PerTarget(PerPolicy::Every(EverySpec {
3512 every: "24h".into(),
3513 })),
3514 RunsOn::Agent,
3515 )
3516 .validate()
3517 .unwrap_err();
3518 assert!(err.contains("per_target"), "got: {err}");
3519 assert!(err.contains("runs_on: agent"), "got: {err}");
3520
3521 // per_target: once is also backend-only.
3522 let err = schedule_with(
3523 When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
3524 RunsOn::Agent,
3525 )
3526 .validate()
3527 .unwrap_err();
3528 assert!(err.contains("per_target"), "got (once): {err}");
3529 assert!(err.contains("runs_on: agent"), "got (once): {err}");
3530 }
3531
3532 #[test]
3533 fn validate_rejects_bad_every_duration() {
3534 let err = schedule_with(
3535 When::PerPc(PerPolicy::Every(EverySpec { every: "6x".into() })),
3536 RunsOn::Backend,
3537 )
3538 .validate()
3539 .unwrap_err();
3540 assert!(err.contains("when.every"), "got: {err}");
3541 }
3542
3543 #[test]
3544 fn validate_rejects_bad_jitter_and_starting_deadline() {
3545 let mut s = schedule_with(
3546 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3547 RunsOn::Backend,
3548 );
3549 s.plan.jitter = Some("5x".into());
3550 let err = s.validate().unwrap_err();
3551 assert!(err.contains("jitter"), "got: {err}");
3552
3553 let mut s = schedule_with(
3554 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3555 RunsOn::Backend,
3556 );
3557 s.starting_deadline = Some("soon".into());
3558 let err = s.validate().unwrap_err();
3559 assert!(err.contains("starting_deadline"), "got: {err}");
3560 }
3561
3562 #[test]
3563 fn validate_accepts_calendar_shapes() {
3564 for when in [
3565 calendar("09:00", &["mon-fri"]), // weekday morning
3566 calendar("00:00", &["sun"]), // weekly
3567 calendar("18:30", &[]), // daily
3568 calendar("2026-06-10 09:00", &[]), // one-shot
3569 calendar("2026/12/25 00:00", &[]), // one-shot, slash form
3570 ] {
3571 schedule_with(when.clone(), RunsOn::Backend)
3572 .validate()
3573 .unwrap_or_else(|e| panic!("{when} should validate: {e}"));
3574 }
3575 }
3576
3577 #[test]
3578 fn validate_rejects_bad_at() {
3579 for bad in ["25:00", "09:60", "9", "noon", "2026-13-01 09:00"] {
3580 let err = schedule_with(calendar(bad, &[]), RunsOn::Backend)
3581 .validate()
3582 .unwrap_err();
3583 assert!(err.contains("when.at"), "for '{bad}', got: {err}");
3584 }
3585 }
3586
3587 #[test]
3588 fn validate_rejects_datetime_at_with_days() {
3589 // A dated `at` is a one-shot — pairing it with days is a
3590 // contradiction (the date already pins the day).
3591 let err = schedule_with(calendar("2026-06-10 09:00", &["mon"]), RunsOn::Backend)
3592 .validate()
3593 .unwrap_err();
3594 assert!(
3595 err.contains("one-shot") && err.contains("days"),
3596 "got: {err}"
3597 );
3598 }
3599
3600 #[test]
3601 fn validate_rejects_bad_day_name() {
3602 // A garbage DOW token is caught by the days pre-flight and
3603 // reported against `when.days`, not the confusing
3604 // "when.at lowered to invalid cron" (claude #432 review).
3605 let err = schedule_with(calendar("09:00", &["funday"]), RunsOn::Backend)
3606 .validate()
3607 .unwrap_err();
3608 assert!(err.contains("when.days"), "got: {err}");
3609 assert!(err.contains("funday"), "names the bad token: {err}");
3610 // a degenerate range like `mon-` reports the whole token, not
3611 // a cryptic empty part (claude #432 follow-up)
3612 let err = schedule_with(calendar("09:00", &["mon-"]), RunsOn::Backend)
3613 .validate()
3614 .unwrap_err();
3615 assert!(err.contains("'mon-'"), "names the whole token: {err}");
3616 // valid names / ranges / numeric / * all pass
3617 for ok in [
3618 calendar("09:00", &["mon-fri"]),
3619 calendar("09:00", &["mon", "wed", "sun"]),
3620 calendar("09:00", &["1-5"]),
3621 ] {
3622 schedule_with(ok.clone(), RunsOn::Backend)
3623 .validate()
3624 .unwrap_or_else(|e| panic!("{ok} should validate: {e}"));
3625 }
3626 }
3627
3628 #[test]
3629 fn validate_accepts_nth_weekday() {
3630 // #418: nth-weekday (Patch Tuesday). validate() also lowers to
3631 // a cron and parses it with croner, so passing here proves the
3632 // whole chain — token → DOW field → engine-acceptable cron.
3633 for ok in [
3634 calendar("09:00", &["tue#2"]), // 2nd Tuesday
3635 calendar("09:00", &["fri#1"]), // 1st Friday
3636 calendar("03:00", &["sun#5"]), // 5th Sunday
3637 calendar("09:00", &["tue#2", "thu#2"]), // a list of nths
3638 calendar("09:00", &["2#2"]), // numeric DOW + ordinal
3639 // Case-insensitive both sides: validate lowercases, croner
3640 // upper-cases the whole pattern before aliasing (claude #547).
3641 calendar("09:00", &["TUE#2"]),
3642 ] {
3643 schedule_with(ok.clone(), RunsOn::Backend)
3644 .validate()
3645 .unwrap_or_else(|e| panic!("{ok} should validate: {e}"));
3646 }
3647 }
3648
3649 #[test]
3650 fn validate_rejects_bad_nth_weekday() {
3651 // ordinal out of 1..5, a range with #, and a bad day before #.
3652 for bad in ["tue#0", "tue#6", "tue#x", "mon-fri#2", "funday#2"] {
3653 let err = schedule_with(calendar("09:00", &[bad]), RunsOn::Backend)
3654 .validate()
3655 .unwrap_err();
3656 assert!(err.contains("when.days"), "for '{bad}', got: {err}");
3657 }
3658 }
3659
3660 #[test]
3661 fn validate_accepts_last_weekday() {
3662 // #418: last-weekday (`friL` = last Friday). Like the nth case,
3663 // validate() lowers to a cron and round-trips it through croner,
3664 // so passing proves token → DOW field → engine-acceptable cron
3665 // with the verified last-<dow>-of-month semantics.
3666 for ok in [
3667 calendar("09:00", &["friL"]), // last Friday
3668 calendar("03:00", &["sunL"]), // last Sunday
3669 calendar("22:00", &["5L"]), // numeric DOW + last
3670 calendar("00:00", &["0L"]), // numeric Sunday (0…
3671 calendar("00:00", &["7L"]), // …and its 7 alias)
3672 calendar("09:00", &["monL", "friL"]), // a list of last-weekdays
3673 // Case-insensitive both the weekday and the `L` suffix:
3674 // validate lowercases the day, croner upper-cases the whole
3675 // pattern before aliasing (claude #547).
3676 calendar("09:00", &["FRIL"]),
3677 calendar("09:00", &["fril"]),
3678 ] {
3679 schedule_with(ok.clone(), RunsOn::Backend)
3680 .validate()
3681 .unwrap_or_else(|e| panic!("{ok} should validate: {e}"));
3682 }
3683 }
3684
3685 #[test]
3686 fn validate_rejects_bad_last_weekday() {
3687 // bare `L` (no weekday — a footgun croner reads as Saturday), a
3688 // range with L, a bad day before L, and an internal space that
3689 // would otherwise leak a malformed cron downstream (gemini #560).
3690 for bad in ["L", "l", "mon-friL", "fundayL", "8L", "*L", "fri L"] {
3691 let err = schedule_with(calendar("09:00", &[bad]), RunsOn::Backend)
3692 .validate()
3693 .unwrap_err();
3694 assert!(err.contains("when.days"), "for '{bad}', got: {err}");
3695 }
3696 }
3697
3698 #[test]
3699 fn calendar_oneshot_instant_detects_past() {
3700 use chrono::TimeZone;
3701 // a dated `at` resolves to an absolute instant…
3702 let c = CalendarSpec {
3703 at: "2024-01-01 09:00".into(),
3704 days: vec![],
3705 };
3706 let t = c
3707 .oneshot_instant(ScheduleTz::Utc)
3708 .expect("one-shot instant");
3709 assert_eq!(
3710 t,
3711 chrono::Utc.with_ymd_and_hms(2024, 1, 1, 9, 0, 0).unwrap()
3712 );
3713 assert!(t < chrono::Utc::now(), "2024 is in the past");
3714 // …while a repeating (time-only) calendar has no instant
3715 let rep = CalendarSpec {
3716 at: "09:00".into(),
3717 days: vec!["mon-fri".into()],
3718 };
3719 assert!(rep.oneshot_instant(ScheduleTz::Utc).is_none());
3720 }
3721
3722 fn schedule_with_active(from: Option<&str>, until: Option<&str>) -> Schedule {
3723 let mut s = schedule_with(
3724 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3725 RunsOn::Backend,
3726 );
3727 s.active = Active {
3728 from: from.map(str::to_owned),
3729 until: until.map(str::to_owned),
3730 };
3731 s
3732 }
3733
3734 #[test]
3735 fn validate_accepts_active_window() {
3736 schedule_with_active(Some("2026-07-01"), Some("2026-08-01T12:00:00+09:00"))
3737 .validate()
3738 .expect("date + rfc3339 bounds should validate");
3739 }
3740
3741 #[test]
3742 fn validate_rejects_unparseable_active_bound() {
3743 let err = schedule_with_active(Some("July 1st"), None)
3744 .validate()
3745 .unwrap_err();
3746 assert!(err.contains("active"), "got: {err}");
3747 }
3748
3749 #[test]
3750 fn validate_rejects_from_not_before_until() {
3751 let err = schedule_with_active(Some("2026-08-01"), Some("2026-07-01"))
3752 .validate()
3753 .unwrap_err();
3754 assert!(err.contains("strictly before"), "got: {err}");
3755
3756 let err = schedule_with_active(Some("2026-07-01"), Some("2026-07-01"))
3757 .validate()
3758 .unwrap_err();
3759 assert!(err.contains("strictly before"), "got: {err}");
3760 }
3761
3762 // ---- Active window semantics ----
3763
3764 #[test]
3765 fn active_window_is_half_open() {
3766 use chrono::TimeZone;
3767 let active = Active {
3768 from: Some("2026-07-01".into()),
3769 until: Some("2026-08-01".into()),
3770 };
3771 // UTC tz so the date bounds are UTC midnight.
3772 let at = |y, m, d, h| chrono::Utc.with_ymd_and_hms(y, m, d, h, 0, 0).unwrap();
3773 let c = |t| active.contains(t, ScheduleTz::Utc);
3774 assert!(!c(at(2026, 6, 30, 23)), "before from");
3775 assert!(c(at(2026, 7, 1, 0)), "at from (inclusive)");
3776 assert!(c(at(2026, 7, 15, 12)), "inside");
3777 assert!(!c(at(2026, 8, 1, 0)), "at until (exclusive)");
3778 assert!(!c(at(2026, 8, 2, 0)), "after until");
3779 }
3780
3781 #[test]
3782 fn active_empty_window_is_always_active() {
3783 assert!(Active::default().contains(chrono::Utc::now(), ScheduleTz::Local));
3784 }
3785
3786 #[test]
3787 fn active_rfc3339_bound_honours_offset_regardless_of_tz() {
3788 use chrono::TimeZone;
3789 let active = Active {
3790 from: Some("2026-07-01T09:00:00+09:00".into()),
3791 until: None,
3792 };
3793 // RFC3339 carries its own offset → tz arg is ignored.
3794 // 09:00 JST = 00:00 UTC.
3795 for tz in [ScheduleTz::Utc, ScheduleTz::Local] {
3796 assert!(
3797 !active.contains(
3798 chrono::Utc
3799 .with_ymd_and_hms(2026, 6, 30, 23, 59, 0)
3800 .unwrap(),
3801 tz
3802 )
3803 );
3804 assert!(active.contains(
3805 chrono::Utc.with_ymd_and_hms(2026, 7, 1, 0, 0, 0).unwrap(),
3806 tz
3807 ));
3808 }
3809 }
3810
3811 #[test]
3812 fn active_date_bound_respects_tz() {
3813 // A bare `YYYY-MM-DD` bound is midnight *in the schedule's
3814 // tz* (#418 Phase 2). The UTC interpretation is exact and
3815 // host-independent; assert that precisely.
3816 use chrono::TimeZone;
3817 let utc = Active::parse_bound("2026-07-01", ScheduleTz::Utc).expect("utc");
3818 assert_eq!(
3819 utc,
3820 chrono::Utc.with_ymd_and_hms(2026, 7, 1, 0, 0, 0).unwrap()
3821 );
3822
3823 // The local interpretation must equal what chrono::Local
3824 // computes for the same wall-clock midnight — proves the tz
3825 // path is wired to the host zone (the magnitude vs UTC is
3826 // host-dependent, so we compare against Local directly rather
3827 // than hard-coding the JST offset, keeping CI green on UTC
3828 // runners).
3829 let local = Active::parse_bound("2026-07-01", ScheduleTz::Local).expect("local");
3830 let want = chrono::Local
3831 .with_ymd_and_hms(2026, 7, 1, 0, 0, 0)
3832 .single()
3833 .expect("local midnight is unambiguous")
3834 .with_timezone(&chrono::Utc);
3835 assert_eq!(local, want, "date bound resolved in host-local tz");
3836 }
3837
3838 #[test]
3839 fn active_empty_is_skipped_when_serialising() {
3840 let s = schedule_with(
3841 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3842 RunsOn::Backend,
3843 );
3844 let json = serde_json::to_value(&s).expect("serialise");
3845 assert!(
3846 json.get("active").is_none(),
3847 "empty active must not appear on the wire: {json}"
3848 );
3849 }
3850
3851 // ---- constraints.window (#418 Phase 3) ----
3852
3853 fn with_window(win: &str) -> Schedule {
3854 let mut s = schedule_with(
3855 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
3856 RunsOn::Backend,
3857 );
3858 s.constraints.window = Some(win.into());
3859 s
3860 }
3861
3862 #[test]
3863 fn constraints_window_parses_and_round_trips() {
3864 let yaml = r#"
3865id: x
3866when:
3867 per_pc: { every: 6h }
3868job_id: y
3869target: { all: true }
3870constraints:
3871 window: "22:00-05:00"
3872"#;
3873 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
3874 assert_eq!(s.constraints.window.as_deref(), Some("22:00-05:00"));
3875 let back: Schedule =
3876 serde_json::from_str(&serde_json::to_string(&s).expect("ser")).expect("de");
3877 assert_eq!(back.constraints.window.as_deref(), Some("22:00-05:00"));
3878 }
3879
3880 #[test]
3881 fn constraints_empty_is_skipped_when_serialising() {
3882 let s = schedule_with(
3883 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
3884 RunsOn::Backend,
3885 );
3886 let json = serde_json::to_value(&s).expect("serialise");
3887 assert!(
3888 json.get("constraints").is_none(),
3889 "empty constraints must not appear on the wire: {json}"
3890 );
3891 }
3892
3893 #[test]
3894 fn window_no_constraint_always_allows() {
3895 let c = Constraints::default();
3896 assert!(c.allows(chrono::Utc::now(), ScheduleTz::Local));
3897 }
3898
3899 #[test]
3900 fn window_same_day_is_half_open() {
3901 use chrono::TimeZone;
3902 let s = with_window("09:00-17:00");
3903 let at = |h, m| chrono::Utc.with_ymd_and_hms(2026, 6, 9, h, m, 0).unwrap();
3904 let a = |t| s.constraints.allows(t, ScheduleTz::Utc);
3905 assert!(!a(at(8, 59)), "before start");
3906 assert!(a(at(9, 0)), "at start (inclusive)");
3907 assert!(a(at(16, 59)), "inside");
3908 assert!(!a(at(17, 0)), "at end (exclusive)");
3909 assert!(!a(at(23, 0)), "after end");
3910 }
3911
3912 #[test]
3913 fn window_crossing_midnight() {
3914 use chrono::TimeZone;
3915 let s = with_window("22:00-05:00");
3916 let at = |h, m| chrono::Utc.with_ymd_and_hms(2026, 6, 9, h, m, 0).unwrap();
3917 let a = |t| s.constraints.allows(t, ScheduleTz::Utc);
3918 assert!(a(at(22, 0)), "at start tonight");
3919 assert!(a(at(23, 30)), "late tonight");
3920 assert!(a(at(3, 0)), "early tomorrow");
3921 assert!(!a(at(5, 0)), "at end (exclusive)");
3922 assert!(!a(at(12, 0)), "midday outside");
3923 assert!(!a(at(21, 59)), "just before start");
3924 }
3925
3926 #[test]
3927 fn window_respects_tz() {
3928 // The same instant is inside the window under one tz and may
3929 // be outside under another. Compare UTC vs Local via the
3930 // host's own offset (kept CI-green on UTC runners like the
3931 // active tz test does).
3932 use chrono::TimeZone;
3933 let s = with_window("09:00-17:00");
3934 let noon_utc = chrono::Utc.with_ymd_and_hms(2026, 6, 9, 12, 0, 0).unwrap();
3935 // Under UTC, 12:00 is inside 09:00-17:00.
3936 assert!(s.constraints.allows(noon_utc, ScheduleTz::Utc));
3937 // Under Local, the verdict tracks the host wall-clock time;
3938 // assert it matches a direct wall_time membership check.
3939 let local_t = noon_utc.with_timezone(&chrono::Local).time();
3940 let in_local = local_t >= chrono::NaiveTime::from_hms_opt(9, 0, 0).unwrap()
3941 && local_t < chrono::NaiveTime::from_hms_opt(17, 0, 0).unwrap();
3942 assert_eq!(s.constraints.allows(noon_utc, ScheduleTz::Local), in_local);
3943 }
3944
3945 #[test]
3946 fn validate_accepts_good_window() {
3947 for w in ["09:00-17:00", "22:00-05:00", "00:00-23:59"] {
3948 with_window(w)
3949 .validate()
3950 .unwrap_or_else(|e| panic!("'{w}' should validate: {e}"));
3951 }
3952 }
3953
3954 #[test]
3955 fn validate_rejects_bad_window() {
3956 for bad in ["9-5", "22:00", "22:00-22:00", "25:00-05:00", "09:00_17:00"] {
3957 let err = with_window(bad).validate().unwrap_err();
3958 assert!(
3959 err.contains("constraints.window"),
3960 "for '{bad}', got: {err}"
3961 );
3962 }
3963 }
3964
3965 // ---- constraints.skip_dates (#418 holiday exclusion) ----
3966
3967 fn with_skip_dates(dates: &[&str]) -> Schedule {
3968 let mut s = schedule_with(calendar("09:00", &[]), RunsOn::Backend);
3969 s.tz = ScheduleTz::Utc; // host-independent date assertions
3970 s.constraints.skip_dates = dates.iter().map(|d| (*d).to_string()).collect();
3971 s
3972 }
3973
3974 #[test]
3975 fn allows_blocks_listed_skip_date() {
3976 use chrono::TimeZone;
3977 let s = with_skip_dates(&["2026-06-10", "2026-12-25"]);
3978 // Any time on a listed date is blocked (whole day).
3979 let on = chrono::Utc.with_ymd_and_hms(2026, 6, 10, 9, 0, 0).unwrap();
3980 assert!(!s.constraints.allows(on, ScheduleTz::Utc));
3981 let on_midnight = chrono::Utc.with_ymd_and_hms(2026, 12, 25, 0, 0, 0).unwrap();
3982 assert!(!s.constraints.allows(on_midnight, ScheduleTz::Utc));
3983 // A date not in the list fires normally.
3984 let off = chrono::Utc.with_ymd_and_hms(2026, 6, 11, 9, 0, 0).unwrap();
3985 assert!(s.constraints.allows(off, ScheduleTz::Utc));
3986 }
3987
3988 #[test]
3989 fn allows_corrupt_skip_date_fails_closed() {
3990 use chrono::TimeZone;
3991 // A garbled entry (only reachable via hand-edited KV) blocks
3992 // rather than silently re-enabling fires — same posture as a
3993 // corrupt window.
3994 let s = with_skip_dates(&["not-a-date"]);
3995 let any = chrono::Utc.with_ymd_and_hms(2026, 6, 11, 9, 0, 0).unwrap();
3996 assert!(!s.constraints.allows(any, ScheduleTz::Utc));
3997 }
3998
3999 #[test]
4000 fn validate_accepts_good_skip_dates() {
4001 with_skip_dates(&["2026-01-01", "2026-12-25", "2027-05-03"])
4002 .validate()
4003 .expect("well-formed skip dates should validate");
4004 }
4005
4006 #[test]
4007 fn validate_rejects_bad_skip_date() {
4008 for bad in ["2026-13-01", "01-01-2026", "nope", "2026/01/01"] {
4009 let err = with_skip_dates(&[bad]).validate().unwrap_err();
4010 assert!(
4011 err.contains("constraints.skip_dates"),
4012 "for '{bad}', got: {err}"
4013 );
4014 }
4015 }
4016
4017 #[test]
4018 fn preview_skips_holidays() {
4019 use chrono::TimeZone;
4020 // Daily 09:00 with two of the next five days marked as holidays
4021 // — preview drops exactly those, since it gates on `allows`.
4022 let mut s = cal_utc("09:00", &[]);
4023 s.constraints.skip_dates = vec!["2026-06-11".into(), "2026-06-13".into()];
4024 let now = chrono::Utc.with_ymd_and_hms(2026, 6, 10, 0, 0, 0).unwrap();
4025 let got = s.preview_fires(now, 4);
4026 let want: Vec<_> = [
4027 (2026, 6, 10),
4028 (2026, 6, 12), // skips 06-11
4029 (2026, 6, 14), // skips 06-13
4030 (2026, 6, 15),
4031 ]
4032 .iter()
4033 .map(|(y, m, d)| chrono::Utc.with_ymd_and_hms(*y, *m, *d, 9, 0, 0).unwrap())
4034 .collect();
4035 assert_eq!(got, want);
4036 }
4037
4038 // ---- constraints.max_concurrent (#418) ----
4039
4040 fn with_max_concurrent(max: u32, runs_on: RunsOn) -> Schedule {
4041 let mut s = schedule_with(
4042 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
4043 runs_on,
4044 );
4045 s.constraints.max_concurrent = Some(max);
4046 s
4047 }
4048
4049 #[test]
4050 fn validate_accepts_backend_max_concurrent() {
4051 with_max_concurrent(5, RunsOn::Backend)
4052 .validate()
4053 .expect("backend max_concurrent should validate");
4054 }
4055
4056 #[test]
4057 fn validate_rejects_max_concurrent_on_agent() {
4058 // Decision E: a central running-instance cap needs a central
4059 // counter, which agents don't have.
4060 let err = with_max_concurrent(5, RunsOn::Agent)
4061 .validate()
4062 .unwrap_err();
4063 assert!(err.contains("constraints.max_concurrent"), "got: {err}");
4064 assert!(err.contains("runs_on: agent"), "got: {err}");
4065 }
4066
4067 #[test]
4068 fn validate_rejects_zero_max_concurrent() {
4069 let err = with_max_concurrent(0, RunsOn::Backend)
4070 .validate()
4071 .unwrap_err();
4072 assert!(err.contains("max_concurrent must be >= 1"), "got: {err}");
4073 }
4074
4075 #[test]
4076 fn max_concurrent_round_trips_and_skips_when_absent() {
4077 let s = with_max_concurrent(3, RunsOn::Backend);
4078 let json = serde_json::to_value(&s.constraints).expect("ser");
4079 assert_eq!(json.get("max_concurrent").and_then(|v| v.as_u64()), Some(3));
4080 // A schedule with no constraints omits the whole block.
4081 let bare = schedule_with(
4082 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
4083 RunsOn::Backend,
4084 );
4085 assert!(bare.constraints.is_empty());
4086 }
4087
4088 #[test]
4089 fn window_fail_closed_on_corrupt_blob() {
4090 // A malformed window (only reachable via a hand-edited KV
4091 // blob — validate() rejects it at create) must BLOCK, not
4092 // silently allow fires during a change-freeze (gemini #452).
4093 let s = with_window("22:00_05:00");
4094 assert!(
4095 !s.constraints.allows(chrono::Utc::now(), ScheduleTz::Utc),
4096 "corrupt window fails closed"
4097 );
4098 // …and the scheduler can surface why it's stuck.
4099 assert!(
4100 s.bad_window().is_some(),
4101 "bad_window reports the parse error"
4102 );
4103 assert!(with_window("22:00-05:00").bad_window().is_none());
4104 }
4105
4106 #[test]
4107 fn calendar_outside_window_is_flagged() {
4108 // at 09:00 can never fall in 22:00-05:00 → never fires.
4109 let mut s = schedule_with(calendar("09:00", &["mon-fri"]), RunsOn::Backend);
4110 s.constraints.window = Some("22:00-05:00".into());
4111 assert!(s.calendar_outside_window(), "09:00 is not in 22:00-05:00");
4112
4113 // at 23:00 IS inside the overnight window → fine.
4114 let mut s = schedule_with(calendar("23:00", &[]), RunsOn::Backend);
4115 s.constraints.window = Some("22:00-05:00".into());
4116 assert!(!s.calendar_outside_window(), "23:00 is in 22:00-05:00");
4117
4118 // reconcile shapes are never flagged (they poll every minute).
4119 let mut s = schedule_with(
4120 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
4121 RunsOn::Backend,
4122 );
4123 s.constraints.window = Some("22:00-05:00".into());
4124 assert!(!s.calendar_outside_window(), "reconcile is unaffected");
4125
4126 // no window → never flagged.
4127 let s = schedule_with(calendar("09:00", &[]), RunsOn::Backend);
4128 assert!(!s.calendar_outside_window());
4129 }
4130
4131 // ---- on_failure.retry (#418 Phase 4) ----
4132
4133 fn with_retry(max: u32, backoff: &str) -> Schedule {
4134 let mut s = schedule_with(
4135 When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
4136 RunsOn::Backend,
4137 );
4138 s.on_failure.retry = Some(Retry {
4139 max,
4140 backoff: backoff.into(),
4141 });
4142 s
4143 }
4144
4145 #[test]
4146 fn on_failure_parses_and_round_trips() {
4147 let yaml = r#"
4148id: x
4149when:
4150 per_pc: { every: 6h }
4151job_id: y
4152target: { all: true }
4153on_failure:
4154 retry: { max: 3, backoff: 10m }
4155"#;
4156 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
4157 let r = s.on_failure.retry.as_ref().expect("retry present");
4158 assert_eq!(r.max, 3);
4159 assert_eq!(r.backoff, "10m");
4160 let back: Schedule =
4161 serde_json::from_str(&serde_json::to_string(&s).expect("ser")).expect("de");
4162 assert_eq!(back.on_failure, s.on_failure);
4163 }
4164
4165 #[test]
4166 fn on_failure_empty_is_skipped_when_serialising() {
4167 let s = schedule_with(
4168 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
4169 RunsOn::Backend,
4170 );
4171 let json = serde_json::to_value(&s).expect("serialise");
4172 assert!(
4173 json.get("on_failure").is_none(),
4174 "empty on_failure must not appear on the wire: {json}"
4175 );
4176 }
4177
4178 #[test]
4179 fn validate_accepts_good_retry() {
4180 for (max, backoff) in [(1, "30s"), (3, "10m"), (10, "1h")] {
4181 with_retry(max, backoff)
4182 .validate()
4183 .unwrap_or_else(|e| panic!("retry {{max:{max}, backoff:{backoff}}}: {e}"));
4184 }
4185 }
4186
4187 #[test]
4188 fn validate_rejects_bad_backoff() {
4189 let err = with_retry(3, "soon").validate().unwrap_err();
4190 assert!(err.contains("on_failure.retry.backoff"), "got: {err}");
4191 }
4192
4193 #[test]
4194 fn validate_rejects_sub_second_backoff() {
4195 // "500ms" parses as humantime but lowers to 0s on the wire —
4196 // reject it so the operator doesn't get a silent no-wait
4197 // (coderabbit #466).
4198 for bad in ["500ms", "0s", "999ms"] {
4199 let err = with_retry(3, bad).validate().unwrap_err();
4200 assert!(
4201 err.contains("on_failure.retry.backoff must be >= 1s"),
4202 "for '{bad}', got: {err}"
4203 );
4204 }
4205 }
4206
4207 #[test]
4208 fn validate_rejects_out_of_range_max() {
4209 for bad in [0u32, 11, 1000] {
4210 let err = with_retry(bad, "10m").validate().unwrap_err();
4211 assert!(
4212 err.contains("on_failure.retry.max"),
4213 "for max={bad}, got: {err}"
4214 );
4215 }
4216 }
4217
4218 #[test]
4219 fn lowered_retry_reduces_backoff_to_seconds() {
4220 let s = with_retry(3, "10m");
4221 let spec = s.on_failure.lowered_retry().expect("a retry policy");
4222 assert_eq!(spec.max, 3);
4223 assert_eq!(spec.backoff_secs, 600);
4224 }
4225
4226 #[test]
4227 fn lowered_retry_is_none_without_policy() {
4228 let s = schedule_with(
4229 When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
4230 RunsOn::Backend,
4231 );
4232 assert!(s.on_failure.lowered_retry().is_none());
4233 }
4234
4235 // ---- global change-freeze (#418 Phase 5) ----
4236
4237 #[test]
4238 fn freeze_empty_window_is_always_active() {
4239 // The big-red-button shape: no bounds = frozen until cleared.
4240 let f = Freeze::default();
4241 assert!(f.is_active(chrono::Utc::now()));
4242 }
4243
4244 #[test]
4245 fn freeze_window_is_half_open() {
4246 use chrono::TimeZone;
4247 let f = Freeze {
4248 from: Some("2026-12-20T00:00:00+00:00".into()),
4249 until: Some("2027-01-05T00:00:00+00:00".into()),
4250 reason: Some("year-end".into()),
4251 tz: ScheduleTz::Utc,
4252 };
4253 let at = |y, mo, d| chrono::Utc.with_ymd_and_hms(y, mo, d, 0, 0, 0).unwrap();
4254 assert!(!f.is_active(at(2026, 12, 19)), "before from = not frozen");
4255 assert!(f.is_active(at(2026, 12, 20)), "from is inclusive");
4256 assert!(f.is_active(at(2026, 12, 31)), "inside window");
4257 assert!(!f.is_active(at(2027, 1, 5)), "until is exclusive");
4258 assert!(!f.is_active(at(2027, 1, 6)), "after until = not frozen");
4259 }
4260
4261 #[test]
4262 fn freeze_fails_closed_on_corrupt_bound() {
4263 // A freeze is a safety switch: an unparseable bound (only
4264 // reachable via a hand-edited KV blob) must read as FROZEN, not
4265 // "fire normally" (coderabbit #472) — the opposite of `active`,
4266 // which fail-opens.
4267 let f = Freeze {
4268 from: Some("not-a-date".into()),
4269 until: None,
4270 reason: None,
4271 tz: ScheduleTz::Utc,
4272 };
4273 assert!(f.is_active(chrono::Utc::now()), "corrupt bound → frozen");
4274 }
4275
4276 #[test]
4277 fn freeze_validate_accepts_good_bounds() {
4278 Freeze {
4279 from: Some("2026-12-20".into()),
4280 until: Some("2027-01-05T12:00:00+09:00".into()),
4281 reason: None,
4282 tz: ScheduleTz::Local,
4283 }
4284 .validate()
4285 .expect("date + rfc3339 bounds should validate");
4286 // Empty (indefinite) freeze is valid.
4287 Freeze::default().validate().expect("empty freeze is valid");
4288 }
4289
4290 #[test]
4291 fn freeze_validate_rejects_bad_bound_and_inverted_window() {
4292 let err = Freeze {
4293 from: Some("never".into()),
4294 ..Default::default()
4295 }
4296 .validate()
4297 .unwrap_err();
4298 assert!(err.contains("freeze:"), "got: {err}");
4299
4300 let inverted = Freeze {
4301 from: Some("2027-01-05".into()),
4302 until: Some("2026-12-20".into()),
4303 ..Default::default()
4304 }
4305 .validate()
4306 .unwrap_err();
4307 assert!(inverted.contains("freeze.from"), "got: {inverted}");
4308 }
4309
4310 #[test]
4311 fn freeze_round_trips_and_skips_empty_fields() {
4312 let f = Freeze {
4313 from: None,
4314 until: Some("2027-01-05".into()),
4315 reason: Some("INC-1234".into()),
4316 tz: ScheduleTz::Utc,
4317 };
4318 let json = serde_json::to_value(&f).expect("serialise");
4319 assert!(json.get("from").is_none(), "empty from omitted: {json}");
4320 let back: Freeze = serde_json::from_value(json).expect("round-trip");
4321 assert_eq!(back, f);
4322 }
4323
4324 #[test]
4325 fn shipped_schedule_configs_parse_and_validate() {
4326 // Every YAML under configs/schedules/ must parse with the
4327 // current Schedule serde AND pass validate() — keeps the
4328 // shipped examples from drifting out of sync with the model
4329 // (#418 removed back-compat, so drift = broken at create).
4330 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../configs/schedules");
4331 let mut seen = 0;
4332 for entry in std::fs::read_dir(&dir).expect("read configs/schedules") {
4333 let path = entry.expect("dir entry").path();
4334 if path.extension().and_then(|e| e.to_str()) != Some("yaml") {
4335 continue;
4336 }
4337 let body = std::fs::read_to_string(&path).expect("read yaml");
4338 let s: Schedule = serde_yaml::from_str(&body)
4339 .unwrap_or_else(|e| panic!("{} failed to parse: {e}", path.display()));
4340 s.validate()
4341 .unwrap_or_else(|e| panic!("{} failed validate(): {e}", path.display()));
4342 seen += 1;
4343 }
4344 assert!(seen > 0, "no schedule YAMLs found in {}", dir.display());
4345 }
4346
4347 // ---- pre-existing enum wire formats (unchanged by #418) ----
4348
4349 #[test]
4350 fn exec_mode_serialises_snake_case() {
4351 for (mode, expected) in [
4352 (ExecMode::EveryTick, "every_tick"),
4353 (ExecMode::OncePerPc, "once_per_pc"),
4354 (ExecMode::OncePerTarget, "once_per_target"),
4355 ] {
4356 let s = serde_json::to_value(mode).expect("serialise");
4357 assert_eq!(s, serde_json::Value::String(expected.into()));
4358 let back: ExecMode = serde_json::from_value(serde_json::Value::String(expected.into()))
4359 .expect("deserialise");
4360 assert_eq!(back, mode, "round-trip for {expected}");
4361 }
4362 }
4363
4364 #[test]
4365 fn schedule_runs_on_defaults_to_backend() {
4366 let yaml = r#"
4367id: x
4368when:
4369 per_pc: once
4370job_id: y
4371target: { all: true }
4372"#;
4373 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
4374 assert_eq!(s.runs_on, RunsOn::Backend);
4375 }
4376
4377 #[test]
4378 fn schedule_runs_on_agent_parses() {
4379 let yaml = r#"
4380id: offline-inv
4381when:
4382 per_pc: { every: 1h }
4383job_id: inventory-hw
4384target: { all: true }
4385runs_on: agent
4386"#;
4387 let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
4388 assert_eq!(s.runs_on, RunsOn::Agent);
4389 assert_eq!(s.lowered().mode, ExecMode::OncePerPc);
4390 }
4391
4392 #[test]
4393 fn runs_on_serialises_snake_case() {
4394 for (mode, expected) in [(RunsOn::Backend, "backend"), (RunsOn::Agent, "agent")] {
4395 let s = serde_json::to_value(mode).expect("serialise");
4396 assert_eq!(s, serde_json::Value::String(expected.into()));
4397 let back: RunsOn = serde_json::from_value(serde_json::Value::String(expected.into()))
4398 .expect("deserialise");
4399 assert_eq!(back, mode);
4400 }
4401 }
4402
4403 #[test]
4404 fn execute_shell_into_wire_shell() {
4405 assert_eq!(Shell::from(ExecuteShell::Powershell), Shell::Powershell);
4406 assert_eq!(Shell::from(ExecuteShell::Cmd), Shell::Cmd);
4407 }
4408
4409 #[test]
4410 fn manifest_staleness_defaults_to_cached() {
4411 let yaml = r#"
4412id: x
4413version: 1.0.0
4414execute:
4415 shell: powershell
4416 script: "echo"
4417 timeout: 1s
4418"#;
4419 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
4420 assert_eq!(m.staleness, Staleness::Cached);
4421 }
4422
4423 #[test]
4424 fn manifest_strict_staleness_parses() {
4425 let yaml = r#"
4426id: urgent-patch
4427version: 2.5.1
4428execute:
4429 shell: powershell
4430 script: Install-Hotfix
4431 timeout: 5m
4432staleness:
4433 mode: strict
4434 max_cache_age: 0s
4435"#;
4436 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
4437 match m.staleness {
4438 Staleness::Strict { max_cache_age } => assert_eq!(max_cache_age, "0s"),
4439 other => panic!("expected strict, got {other:?}"),
4440 }
4441 }
4442
4443 #[test]
4444 fn manifest_unchecked_staleness_parses() {
4445 let yaml = r#"
4446id: legacy
4447version: 0.1.0
4448execute:
4449 shell: cmd
4450 script: "echo"
4451 timeout: 1s
4452staleness:
4453 mode: unchecked
4454"#;
4455 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
4456 assert_eq!(m.staleness, Staleness::Unchecked);
4457 }
4458
4459 #[test]
4460 fn missing_required_field_errors() {
4461 // `id` missing.
4462 let yaml = r#"
4463version: 1.0.0
4464target: { all: true }
4465execute:
4466 shell: powershell
4467 script: "echo"
4468 timeout: 1s
4469"#;
4470 let r: Result<Manifest, _> = serde_yaml::from_str(yaml);
4471 assert!(r.is_err(), "expected error, got {:?}", r);
4472 }
4473
4474 #[test]
4475 fn display_field_table_kind_round_trips_with_nested_columns() {
4476 // #39: `type: table` + `columns:` on a DisplayField gets
4477 // round-tripped through serde so the SPA receives the
4478 // nested schema verbatim. Nested columns themselves are
4479 // DisplayFields so they can carry `type: bytes` /
4480 // `type: number` for cell formatting.
4481 let yaml = r#"
4482id: inv-hw
4483version: 1.0.0
4484execute:
4485 shell: powershell
4486 script: "echo"
4487 timeout: 60s
4488inventory:
4489 display:
4490 - field: hostname
4491 label: Hostname
4492 - field: disks
4493 label: Disks
4494 type: table
4495 columns:
4496 - field: device_id
4497 label: Drive
4498 - field: size_bytes
4499 label: Size
4500 type: bytes
4501 - field: free_bytes
4502 label: Free
4503 type: bytes
4504 - field: file_system
4505 label: FS
4506"#;
4507 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
4508 let inv = m.inventory.as_ref().expect("inventory hint");
4509 let disks = inv
4510 .display
4511 .iter()
4512 .find(|d| d.field == "disks")
4513 .expect("disks display row");
4514 assert_eq!(disks.kind.as_deref(), Some("table"));
4515 let cols = disks.columns.as_ref().expect("table needs columns");
4516 assert_eq!(cols.len(), 4);
4517 assert_eq!(cols[1].field, "size_bytes");
4518 assert_eq!(cols[1].kind.as_deref(), Some("bytes"));
4519 }
4520
4521 #[test]
4522 fn display_field_scalar_kind_keeps_columns_none() {
4523 // Defensive: when type is a scalar (`bytes` / `number` /
4524 // `timestamp`) the `columns` field stays None — the SPA
4525 // uses its presence as the "render nested table" signal,
4526 // so it must not leak in via serde defaults.
4527 let yaml = r#"
4528id: x
4529version: 1.0.0
4530execute:
4531 shell: powershell
4532 script: "echo"
4533 timeout: 5s
4534inventory:
4535 display:
4536 - { field: ram_bytes, label: RAM, type: bytes }
4537"#;
4538 let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
4539 let inv = m.inventory.as_ref().unwrap();
4540 assert!(inv.display[0].columns.is_none());
4541 }
4542
4543 // ---- checked-in JSON Schema freshness (docs/schemas/) ----
4544
4545 /// The JSON Schemas under `docs/schemas/` must match what
4546 /// `schema_for!` produces today — a Cargo.lock-style freshness guard
4547 /// so a `Schedule` / `Manifest` field change can't silently drift
4548 /// the operator-facing schema. The SPA editor, the backend
4549 /// `/api/schemas/*` endpoints, and these files all read the same
4550 /// derived shape; this test fails CI if the checked-in copy lags.
4551 /// Regenerate with:
4552 /// `UPDATE_SCHEMAS=1 cargo test -p kanade-shared schema_files_are_current`
4553 #[test]
4554 fn schema_files_are_current() {
4555 assert_schema_file("schedule.schema.json", &schemars::schema_for!(Schedule));
4556 assert_schema_file("job.schema.json", &schemars::schema_for!(Manifest));
4557 assert_schema_file("view.schema.json", &schemars::schema_for!(View));
4558 }
4559
4560 fn assert_schema_file(name: &str, schema: &schemars::Schema) {
4561 let generated = serde_json::to_string_pretty(schema).expect("serialize schema") + "\n";
4562 let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
4563 .join("../../docs/schemas")
4564 .join(name);
4565 if std::env::var_os("UPDATE_SCHEMAS").is_some() {
4566 std::fs::create_dir_all(path.parent().unwrap()).expect("mkdir docs/schemas");
4567 std::fs::write(&path, &generated).unwrap_or_else(|e| panic!("write {path:?}: {e}"));
4568 return;
4569 }
4570 // Normalize CRLF→LF before comparing: `.gitattributes` already
4571 // pins these files to `eol=lf`, but a stray CRLF working-tree
4572 // copy (autocrlf, a tool rewrite) shouldn't turn a *content*-
4573 // freshness check into a confusing line-ending failure — that's
4574 // .gitattributes' job, not this test's (gemini #588).
4575 let on_disk = std::fs::read_to_string(&path)
4576 .unwrap_or_else(|e| {
4577 panic!(
4578 "read {path:?}: {e}\n\
4579 generate it with: UPDATE_SCHEMAS=1 cargo test -p kanade-shared schema_files_are_current"
4580 )
4581 })
4582 .replace("\r\n", "\n");
4583 assert_eq!(
4584 on_disk, generated,
4585 "{name} is stale — a Schedule/Manifest schema change isn't reflected in docs/schemas/. \
4586 Refresh with: UPDATE_SCHEMAS=1 cargo test -p kanade-shared schema_files_are_current"
4587 );
4588 }
4589}
4590
4591/// Periodic schedule (spec §2.4.3). v0.18.0 carries the fanout plan
4592/// (target + optional rollout + optional jitter) inline; the
4593/// referenced job (`job_id` → [`BUCKET_JOBS`]) supplies only the
4594/// script body. Two schedules of the same job can target different
4595/// groups on different cadences without copying the manifest.
4596///
4597/// #418 Phase 1: the cadence is the single [`When`] field. The old
4598/// `cron` × `mode` × `cooldown` × `auto_disable_when_done` quartet
4599/// is gone (no back-compat — pre-Phase-1 KV blobs fail to parse and
4600/// are warn-skipped; re-`schedule create` to upgrade them). The
4601/// engine underneath is unchanged: [`Schedule::lowered`] maps `when`
4602/// onto the same (cron, ExecMode, cooldown) trio the scheduler and
4603/// `decide_fire` always ran on.
4604#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
4605pub struct Schedule {
4606 pub id: String,
4607 /// When to fire — a reconcile cadence (`per_pc` / `per_target`)
4608 /// or a calendar time trigger (`at` / `days`). See [`When`].
4609 ///
4610 /// `singleton_map`: serde_yaml 0.9 renders externally-tagged
4611 /// enums as `!per_pc` YAML tags by default; this keeps the
4612 /// operator-facing map shape (`when: { per_pc: once }`). JSON
4613 /// output is identical either way, and the schemars schema
4614 /// (external tagging = oneOf of single-key objects) already
4615 /// matches the singleton-map wire shape.
4616 #[serde(with = "serde_yaml::with::singleton_map")]
4617 #[schemars(with = "When")]
4618 pub when: When,
4619 /// Key into [`crate::kv::BUCKET_JOBS`]. Must equal a registered
4620 /// Manifest's `id`.
4621 pub job_id: String,
4622 /// Who + how-to-phase + when-to-stagger. The Manifest doesn't
4623 /// carry these any more — same job + different fanout = different
4624 /// schedule.
4625 #[serde(flatten)]
4626 pub plan: FanoutPlan,
4627 /// Optional validity window. Outside `[from, until)` the
4628 /// schedule is dormant — still registered, still visible, but
4629 /// every tick is skipped (deleted ≠ dormant: a campaign that
4630 /// ended stays inspectable and can be re-armed by editing the
4631 /// window). Checked at tick time on both the backend scheduler
4632 /// and the agent's local scheduler.
4633 #[serde(default, skip_serializing_if = "Active::is_empty")]
4634 pub active: Active,
4635 /// #418 operational constraints gating *when within an active
4636 /// period* a fire may happen: a maintenance `window`, a fleet
4637 /// `max_concurrent` cap, and `skip_dates` (holiday exclusion). The
4638 /// wall-clock ones are evaluated in the schedule's `tz`; future
4639 /// `require` (env gates) lands in the same namespace. Checked at
4640 /// tick time on both schedulers (and surfaced by `preview`).
4641 #[serde(default, skip_serializing_if = "Constraints::is_empty")]
4642 pub constraints: Constraints,
4643 /// #418 Phase 4: what to do after a fire's script comes back
4644 /// failed. Currently just `retry` (fixed-backoff in-process
4645 /// re-run); future `notify` / `disable` join the same namespace.
4646 /// Applied fire-side in `handle_command` (the retry policy is
4647 /// lowered onto every Command this schedule produces), so it
4648 /// covers both `runs_on` locations.
4649 #[serde(default, skip_serializing_if = "OnFailure::is_empty")]
4650 pub on_failure: OnFailure,
4651 /// #418 Phase 2: the timezone this schedule's wall-clock fields
4652 /// are evaluated in — both the calendar `at` firing time AND the
4653 /// `active.{from,until}` window bounds. `local` (default) = the
4654 /// running host's TZ (the agent's for `runs_on: agent`, the
4655 /// backend server's otherwise); `utc` for TZ-independent
4656 /// schedules. Reconcile shapes (`per_pc`/`per_target`) ignore it
4657 /// for firing (poll cron runs every minute regardless) but still
4658 /// honor it for the `active` window.
4659 #[serde(default)]
4660 pub tz: ScheduleTz,
4661 /// v0.22: optional humantime window after a cron tick during
4662 /// which the Command is still considered "live". The scheduler
4663 /// computes `tick_at + starting_deadline` and stamps it onto
4664 /// each Command as `deadline_at`; agents skip Commands they
4665 /// receive after that absolute time. `None` (default) = no
4666 /// deadline, meaning a Command queued in the broker / stream
4667 /// during agent downtime runs whenever the agent reconnects —
4668 /// good for kitting / inventory / cleanup. Set this for
4669 /// time-of-day notifications, lunch reminders, etc., where
4670 /// "fire 3 hours late" would be wrong.
4671 #[serde(default, skip_serializing_if = "Option::is_none")]
4672 pub starting_deadline: Option<String>,
4673 /// v0.23: where does the cron tick happen? `Backend` (default,
4674 /// historical) = backend's scheduler fires Commands via NATS;
4675 /// agents passively receive. `Agent` = each targeted agent runs
4676 /// its own internal cron and fires locally, so the schedule
4677 /// keeps ticking even when the broker is unreachable (laptop on
4678 /// the train, broker maintenance window, full WAN outage). The
4679 /// two locations are mutually exclusive — when `Agent`, the
4680 /// backend scheduler stays out and just keeps the definition in
4681 /// KV for agents to read.
4682 #[serde(default)]
4683 pub runs_on: RunsOn,
4684 #[serde(default = "default_true")]
4685 pub enabled: bool,
4686 /// Free-form operator taxonomy for the Schedules page — the
4687 /// schedule-side mirror of `Manifest.tags` (added in #640; a plain
4688 /// code ref rather than an intra-doc link, since that field isn't
4689 /// on this branch until #640 merges). Purely a SPA-side
4690 /// organisational aid (search / filter chips alongside the
4691 /// id-prefix grouping); the scheduler never reads it, so any
4692 /// string is allowed and it carries no firing semantics. A
4693 /// schedule's own tags are independent of its job's: the same job
4694 /// may back a `weekly` maintenance schedule and a `canary` rollout
4695 /// schedule. Empty by default and `skip_serializing_if`-elided per
4696 /// the #492 gradual-upgrade wire rule.
4697 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4698 pub tags: Vec<String>,
4699 /// GitOps provenance (#695) — see [`RepoOrigin`]. Stamped by
4700 /// `kanade schedule create` when the source YAML lives inside a Git
4701 /// work tree, so the SPA renders the schedule read-only and points
4702 /// edits back at the repo (SPEC design principle #3: 設定駆動 YAML +
4703 /// Git), parity with a job's [`Manifest::origin`]. `None` for
4704 /// SPA-born schedules and ones applied from outside any repo. Purely
4705 /// informational — the scheduler never reads it. New field ⇒ #492
4706 /// wire rule (`default` + `skip_serializing_if`).
4707 #[serde(default, skip_serializing_if = "Option::is_none")]
4708 pub origin: Option<RepoOrigin>,
4709}
4710
4711/// v0.23 — where the cron tick fires from.
4712#[derive(
4713 Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
4714)]
4715#[serde(rename_all = "snake_case")]
4716pub enum RunsOn {
4717 /// Backend's central scheduler ticks and publishes Commands to
4718 /// NATS. Historical default, what every pre-v0.23 schedule
4719 /// uses. Agent offline ⇒ Command queued in STREAM_EXEC; agent
4720 /// reconnects ⇒ catch-up via [`command_replay`](crate)
4721 /// (see kanade-agent's command_replay module).
4722 #[default]
4723 Backend,
4724 /// Each targeted agent runs the cron tick locally. Survives
4725 /// broker / WAN outages. Best for laptops / mobile devices that
4726 /// roam off the corporate network. Agent must be online for the
4727 /// initial schedule + job-catalog pull, but once cached the
4728 /// agent fires the script standalone.
4729 Agent,
4730}
4731
4732/// Per-pc/per-target dedup semantics for a [`Schedule`] (v0.19).
4733#[derive(
4734 Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
4735)]
4736#[serde(rename_all = "snake_case")]
4737pub enum ExecMode {
4738 /// Fire on every cron tick at the whole target. Historical
4739 /// (pre-v0.19) behavior; no dedup.
4740 #[default]
4741 EveryTick,
4742 /// Fire at each pc until that pc succeeds; then skip it until
4743 /// the optional cooldown elapses (or forever if no cooldown).
4744 /// Use for kitting / first-boot / per-pc compliance checks.
4745 OncePerPc,
4746 /// Fire at the whole target until **any** pc succeeds; then
4747 /// skip the whole target until the optional cooldown elapses
4748 /// (or forever if no cooldown). Use for "one delegate is
4749 /// enough" tasks like license check-in.
4750 OncePerTarget,
4751 /// #418 OS-native event trigger (`when: { on: [...] }`). There is
4752 /// no cron — the agent fires it from an OS event source (boot /
4753 /// session-change), not a tick — so the scheduler skips
4754 /// `tokio-cron` registration for it. Each event occurrence fires
4755 /// once, gated by the standard freeze / active / window /
4756 /// skip_dates checks.
4757 Event,
4758}
4759
4760/// #418 Phase 1 — the single "when does this fire" axis.
4761///
4762/// Replaces the old `cron` + `mode` + `cooldown` trio whose
4763/// interactions were implicit (cron doubled as both a real
4764/// time-of-day trigger and a reconcile poll period; contradictory
4765/// combinations silently no-opped). Two shapes:
4766///
4767/// * **reconcile** (`per_pc` / `per_target`) — desired-state: "each
4768/// pc (or one delegate) should have run this within `every`".
4769/// The poll period is system-generated ([`POLL_CRON`], every
4770/// minute) and no longer the operator's concern.
4771/// * **calendar** (`{ at, days }`) — a wall-clock time trigger
4772/// (#418 Phase 2, replacing the old raw-cron escape hatch). Fires
4773/// the whole target at the given time, no dedup. `at: "09:00"` +
4774/// `days` repeats; `at: "2026-06-10 09:00"` (a date+time) fires
4775/// exactly once. Evaluated in the schedule's top-level `tz`.
4776#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
4777#[serde(rename_all = "snake_case")]
4778pub enum When {
4779 /// Fire at each targeted pc: `once` (kitting — succeed once,
4780 /// skip forever, forever catching brand-new / re-imaged pcs)
4781 /// or `{ every: <humantime> }` (patrol — re-arm per pc after
4782 /// the interval).
4783 PerPc(PerPolicy),
4784 /// Fire until **any** one pc of the target succeeds, then skip
4785 /// the whole target (`once`) or re-arm after `every`. Needs
4786 /// fleet-wide completion data, so it is backend-only —
4787 /// `runs_on: agent` + `per_target` is rejected by
4788 /// [`Schedule::validate`].
4789 PerTarget(PerPolicy),
4790 /// Calendar time trigger: `{ at: "09:00", days: [mon-fri] }`
4791 /// (repeating) or `{ at: "2026-06-10 09:00" }` (one-shot). Fires
4792 /// the whole target at that wall-clock time in the schedule's
4793 /// `tz` — no dedup, no cooldown.
4794 Calendar(CalendarSpec),
4795 /// #418 OS-native event trigger: `when: { on: [startup, logon] }`.
4796 /// Fires when the agent observes the listed OS event(s) rather than
4797 /// on a clock — there is no cron. `runs_on: agent` only (the agent
4798 /// owns the event source); [`Schedule::validate`] rejects it on
4799 /// `backend` and rejects an empty list. Each event occurrence fires
4800 /// once, gated by the same freeze / active / `constraints.window` /
4801 /// `skip_dates` checks as the cron path. `startup` fires once per OS
4802 /// boot (deduped via the host boot time); a `starting_deadline`, if
4803 /// set, limits it to "agent came up within that long after boot".
4804 On(Vec<OnTrigger>),
4805}
4806
4807/// An OS event the agent can fire a schedule on (#418 `when: { on }`).
4808#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Hash)]
4809#[serde(rename_all = "snake_case")]
4810pub enum OnTrigger {
4811 /// Once per OS boot (the agent's first run for that boot). Catches
4812 /// freshly-imaged / reinstalled hosts at their next startup.
4813 Startup,
4814 /// On an interactive-session user logon — console, RDP, or
4815 /// auto-logon (Windows `WTS_SESSION_LOGON`). Does not fire for
4816 /// service / network / batch logons (no interactive session).
4817 Logon,
4818 /// When the workstation is locked (Win+L / idle lock; Windows
4819 /// `WTS_SESSION_LOCK`). Use for step-away compliance / cleanup.
4820 Lock,
4821 /// When the workstation is unlocked — the user returns to a locked
4822 /// session (Windows `WTS_SESSION_UNLOCK`). Use to re-check
4823 /// compliance / refresh state when work resumes.
4824 Unlock,
4825 /// When the host's network changes — IP address table change on
4826 /// connect / disconnect / DHCP renew / VPN / Wi-Fi roam (Windows
4827 /// `NotifyAddrChange`). Debounced agent-side (a burst of changes
4828 /// from one transition fires once after the network settles), so
4829 /// use it for "re-check connectivity / re-register on network move"
4830 /// rather than expecting one fire per raw adapter event.
4831 ///
4832 /// IPv4 only: `NotifyAddrChange` watches the IPv4 address table, so a
4833 /// transition that touches only IPv6 addresses won't fire. In practice
4834 /// dual-stack networks change both tables together, but a pure-IPv6
4835 /// move (e.g. an IPv6-only Wi-Fi roam) is not detected.
4836 NetworkChange,
4837}
4838
4839/// Calendar time trigger (#418 Phase 2). `at` is either a time of
4840/// day (`"HH:MM"`, repeating — combine with `days`) or a full
4841/// date+time (`"YYYY-MM-DD HH:MM"`, a one-shot that fires once and
4842/// never again). Evaluated in the schedule's top-level `tz`.
4843#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
4844pub struct CalendarSpec {
4845 /// `"HH:MM"` (24h) for a repeating trigger, or
4846 /// `"YYYY-MM-DD HH:MM"` (hyphen / slash / `T` separators all
4847 /// accepted) for a one-shot. Parsed lazily —
4848 /// [`Schedule::validate`] rejects garbage at create time.
4849 pub at: String,
4850 /// Day-of-week filter for a time-of-day `at`: `["mon-fri"]`,
4851 /// `["mon","wed","fri"]`, … (passed verbatim to the cron DOW
4852 /// field, so ranges and names both work). An **nth-weekday**
4853 /// `["tue#2"]` fires only on the 2nd Tuesday of each month
4854 /// ("Patch Tuesday"); the ordinal is `1..5`. A **last-weekday**
4855 /// `["friL"]` fires only on the last Friday of each month (handy
4856 /// for monthly maintenance). Empty = every day. Must be empty
4857 /// when `at` carries a date (the date already pins the day).
4858 #[serde(default, skip_serializing_if = "Vec::is_empty")]
4859 pub days: Vec<String>,
4860}
4861
4862/// Parsed `CalendarSpec.at`: the wall-clock minute/hour, plus the
4863/// date for a one-shot (`None` = repeating time-of-day).
4864struct ParsedAt {
4865 minute: u32,
4866 hour: u32,
4867 date: Option<chrono::NaiveDate>,
4868}
4869
4870impl CalendarSpec {
4871 /// Parse `at`: a date+time (`YYYY-MM-DD HH:MM`, hyphen / slash /
4872 /// `T` separators) is a one-shot; a bare `HH:MM` is repeating.
4873 fn parse_at(&self) -> Result<ParsedAt, String> {
4874 use chrono::Timelike;
4875 let s = self.at.trim();
4876 for fmt in ["%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M", "%Y/%m/%d %H:%M"] {
4877 if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s, fmt) {
4878 return Ok(ParsedAt {
4879 minute: dt.minute(),
4880 hour: dt.hour(),
4881 date: Some(dt.date()),
4882 });
4883 }
4884 }
4885 if let Ok(t) = chrono::NaiveTime::parse_from_str(s, "%H:%M") {
4886 return Ok(ParsedAt {
4887 minute: t.minute(),
4888 hour: t.hour(),
4889 date: None,
4890 });
4891 }
4892 Err(format!(
4893 "when.at: unparseable '{}' (want HH:MM or YYYY-MM-DD HH:MM)",
4894 self.at
4895 ))
4896 }
4897
4898 /// Pre-flight check on the `days` tokens so a bad day name gives
4899 /// a `when.days:`-scoped error instead of croner's confusing
4900 /// "when.at lowered to invalid cron" (claude #432 review). Each
4901 /// token is a day name (`mon`..`sun`), a numeric DOW (`0`..`7`),
4902 /// `*`, a `-` range of those, an **nth-weekday** like `tue#2`
4903 /// (2nd Tuesday of the month — "Patch Tuesday"), or a
4904 /// **last-weekday** like `friL` (last Friday of the month).
4905 fn validate_days(&self) -> Result<(), String> {
4906 const NAMES: [&str; 7] = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
4907 let is_day = |p: &str| NAMES.contains(&p) || p.parse::<u8>().is_ok_and(|n| n <= 7);
4908 for tok in &self.days {
4909 // Report the whole token on a malformed range like `mon-`
4910 // (which would otherwise split to a cryptic empty part —
4911 // claude #432 follow-up).
4912 let invalid = |reason: &str| {
4913 Err(format!(
4914 "when.days: invalid day token '{tok}' ({reason}; \
4915 want mon..sun, 0-7, a range like mon-fri, an nth-weekday \
4916 like tue#2, a last-weekday like friL, or *)"
4917 ))
4918 };
4919 // #418: nth-weekday suffix (`tue#2` = 2nd Tuesday). Croner
4920 // accepts `<dow>#<n>` (n = 1..5) in the DOW field, and
4921 // `to_cron` passes the token through verbatim, so the
4922 // engine fires only on that occurrence. It's a single
4923 // weekday + ordinal — not combinable with a range.
4924 if let Some((day_part, nth_part)) = tok.split_once('#') {
4925 // Normalize once and use `d` consistently (gemini #547);
4926 // the outer `invalid` already echoes the raw `tok`.
4927 let d = day_part.trim().to_ascii_lowercase();
4928 if d.contains('-') || !is_day(&d) {
4929 return invalid("the part before # must be a single weekday");
4930 }
4931 match nth_part.trim().parse::<u8>() {
4932 Ok(n) if (1..=5).contains(&n) => {}
4933 _ => return invalid("the # ordinal must be 1..5 (e.g. tue#2 = 2nd Tuesday)"),
4934 }
4935 continue;
4936 }
4937 // #418: last-weekday suffix (`friL` = last Friday of the
4938 // month — the monthly-maintenance sibling of Patch Tuesday).
4939 // Croner accepts `<dow>L` in the DOW field with verified
4940 // last-<dow>-of-month semantics, and `to_cron` passes it
4941 // through verbatim. A single weekday + `L` — bare `L` and
4942 // ranges are rejected (croner would read bare `L` as
4943 // Saturday, which is a confusing footgun).
4944 if let Some(day_part) = tok.strip_suffix(['L', 'l']) {
4945 // No `.trim()`: a cron DOW token can't carry internal
4946 // whitespace, so `"fri L"` must be *rejected* here (its
4947 // strip leaves `"fri "`, and `is_day` catches the space)
4948 // rather than trimmed into a clean `"fri"` that then
4949 // produces a malformed `fri L` cron downstream and a
4950 // confusing croner error (gemini #560).
4951 let d = day_part.to_ascii_lowercase();
4952 if d.is_empty() {
4953 return invalid("`L` (last-weekday) needs a weekday before it, e.g. friL");
4954 }
4955 if d.contains('-') || !is_day(&d) {
4956 return invalid(
4957 "the part before L must be a single weekday (e.g. friL = last Friday)",
4958 );
4959 }
4960 continue;
4961 }
4962 for part in tok.split('-') {
4963 let p = part.trim().to_ascii_lowercase();
4964 if p.is_empty() {
4965 return invalid("empty range bound");
4966 }
4967 if p != "*" && !is_day(&p) {
4968 return invalid(&format!("'{part}' is not a day"));
4969 }
4970 }
4971 }
4972 Ok(())
4973 }
4974
4975 /// For a one-shot (`at` carries a date), the absolute instant it
4976 /// fires in `tz`. `None` for a repeating calendar. Used to warn
4977 /// about a one-shot whose date is already in the past (it would
4978 /// never fire).
4979 pub fn oneshot_instant(&self, tz: ScheduleTz) -> Option<chrono::DateTime<chrono::Utc>> {
4980 let p = self.parse_at().ok()?;
4981 let date = p.date?;
4982 let naive = date.and_hms_opt(p.hour, p.minute, 0)?;
4983 tz.naive_to_utc(naive)
4984 }
4985
4986 /// The wall-clock time-of-day this calendar fires at (`None` if
4987 /// `at` is unparseable — validate() guards that). Used to detect
4988 /// a calendar whose fire time can never fall inside its
4989 /// `constraints.window` (claude #452 review).
4990 pub fn fire_time(&self) -> Option<chrono::NaiveTime> {
4991 let p = self.parse_at().ok()?;
4992 chrono::NaiveTime::from_hms_opt(p.hour, p.minute, 0)
4993 }
4994
4995 /// Lower to the cron string the scheduler engine runs. Repeating
4996 /// → 6-field `0 {min} {hour} * * {dow}`; one-shot → 7-field
4997 /// `0 {min} {hour} {day} {month} * {year}` (a past year never
4998 /// fires — that's what makes it one-shot).
4999 fn to_cron(&self) -> Result<String, String> {
5000 use chrono::Datelike;
5001 let ParsedAt { minute, hour, date } = self.parse_at()?;
5002 match date {
5003 Some(d) => {
5004 if !self.days.is_empty() {
5005 return Err(
5006 "when.at with a date is a one-shot and cannot be combined with days".into(),
5007 );
5008 }
5009 Ok(format!(
5010 "0 {minute} {hour} {} {} * {}",
5011 d.day(),
5012 d.month(),
5013 d.year()
5014 ))
5015 }
5016 None => {
5017 let dow = if self.days.is_empty() {
5018 "*".to_string()
5019 } else {
5020 self.validate_days()?;
5021 self.days.join(",")
5022 };
5023 Ok(format!("0 {minute} {hour} * * {dow}"))
5024 }
5025 }
5026 }
5027}
5028
5029/// The timezone a schedule's wall-clock fields (`when.at`,
5030/// `active.{from,until}`) are evaluated in (#418 Phase 2).
5031#[derive(
5032 Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
5033)]
5034#[serde(rename_all = "snake_case")]
5035pub enum ScheduleTz {
5036 /// The running host's local timezone — the agent's for
5037 /// `runs_on: agent`, the backend server's otherwise. Default.
5038 #[default]
5039 Local,
5040 /// UTC — for timezone-independent schedules.
5041 Utc,
5042}
5043
5044impl ScheduleTz {
5045 /// Interpret a naive (zoneless) datetime as being in this tz and
5046 /// convert to UTC. On a DST *fold* (the local time occurs twice
5047 /// when clocks go back) we pick `.earliest()` rather than
5048 /// rejecting it; `None` is reserved for a true DST *gap* (a local
5049 /// time that never exists). `Utc` is fixed-offset so neither ever
5050 /// happens; `Local` is whatever timezone the running host is set
5051 /// to and *can* hit a gap/fold on any DST-observing host — not
5052 /// just the JST we run today (gemini + claude #432 review).
5053 fn naive_to_utc(self, naive: chrono::NaiveDateTime) -> Option<chrono::DateTime<chrono::Utc>> {
5054 use chrono::TimeZone;
5055 match self {
5056 ScheduleTz::Utc => Some(chrono::DateTime::from_naive_utc_and_offset(
5057 naive,
5058 chrono::Utc,
5059 )),
5060 ScheduleTz::Local => chrono::Local
5061 .from_local_datetime(&naive)
5062 .earliest()
5063 .map(|dt| dt.with_timezone(&chrono::Utc)),
5064 }
5065 }
5066
5067 /// The wall-clock time-of-day `now` reads as in this tz — used by
5068 /// [`Constraints::allows`] to test a maintenance window
5069 /// (#418 Phase 3). `Utc` is the naive UTC time; `Local` is the
5070 /// running host's local time.
5071 fn wall_time(self, now: chrono::DateTime<chrono::Utc>) -> chrono::NaiveTime {
5072 match self {
5073 ScheduleTz::Utc => now.time(),
5074 ScheduleTz::Local => now.with_timezone(&chrono::Local).time(),
5075 }
5076 }
5077
5078 /// The wall-clock *date* `now` reads as in this tz — used by
5079 /// [`Constraints::allows`] to test `skip_dates` (#418 holiday
5080 /// exclusion). Same tz semantics as [`Self::wall_time`].
5081 fn wall_date(self, now: chrono::DateTime<chrono::Utc>) -> chrono::NaiveDate {
5082 match self {
5083 ScheduleTz::Utc => now.date_naive(),
5084 ScheduleTz::Local => now.with_timezone(&chrono::Local).date_naive(),
5085 }
5086 }
5087
5088 /// Stable lowercase wire/display label (`local` / `utc`) — matches
5089 /// the serde `snake_case` representation. Used for the preview
5090 /// response's `tz` field so the JSON shape isn't coupled to the
5091 /// `Debug` repr (claude #578 review).
5092 pub fn as_str(self) -> &'static str {
5093 match self {
5094 ScheduleTz::Local => "local",
5095 ScheduleTz::Utc => "utc",
5096 }
5097 }
5098}
5099
5100impl std::fmt::Display for ScheduleTz {
5101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5102 f.write_str(self.as_str())
5103 }
5104}
5105
5106/// `once` vs `{ every: <humantime> }` — shared by `per_pc` /
5107/// `per_target`. Untagged so the YAML stays the bare keyword or a
5108/// one-key map, nothing more ceremonial.
5109#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
5110#[serde(untagged)]
5111pub enum PerPolicy {
5112 /// The bare string `once`: succeed once, then skip permanently
5113 /// (cooldown = infinity).
5114 Once(OnceLiteral),
5115 /// Re-arm after the humantime interval, e.g. `{ every: 6h }`.
5116 Every(EverySpec),
5117}
5118
5119/// Single-variant enum so serde accepts exactly the string `once`
5120/// (a free-form `String` would swallow typos like `onec`).
5121#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
5122#[serde(rename_all = "snake_case")]
5123pub enum OnceLiteral {
5124 Once,
5125}
5126
5127/// `{ every: <humantime> }`. Standalone struct (not an inline
5128/// struct variant). `{ evry: 6h }` still fails to parse (the
5129/// required `every` key is missing), and the create boundaries
5130/// reject the unknown `evry` via [`crate::strict`] with its path —
5131/// while agents reading a future writer's extra fields tolerate
5132/// them (#492).
5133#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
5134pub struct EverySpec {
5135 /// Humantime interval (`10m`, `6h`, `1d`...). Parsed lazily —
5136 /// [`Schedule::validate`] rejects garbage at create time.
5137 pub every: String,
5138}
5139
5140impl PerPolicy {
5141 /// The cooldown this policy lowers to: `once` = `None`
5142 /// (permanent skip), `every` = the interval.
5143 fn cooldown(&self) -> Option<String> {
5144 match self {
5145 PerPolicy::Once(_) => None,
5146 PerPolicy::Every(EverySpec { every }) => Some(every.clone()),
5147 }
5148 }
5149}
5150
5151impl std::fmt::Display for When {
5152 /// Operator-facing one-liner (`per_pc once` / `per_pc every 6h`
5153 /// / `at 09:00 [mon-fri]` / `at 2026-06-10 09:00`) for log
5154 /// lines, audit payloads and the API's `ScheduleSummary`.
5155 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5156 let policy = |p: &PerPolicy| match p {
5157 PerPolicy::Once(_) => "once".to_string(),
5158 PerPolicy::Every(EverySpec { every }) => format!("every {every}"),
5159 };
5160 match self {
5161 When::PerPc(p) => write!(f, "per_pc {}", policy(p)),
5162 When::PerTarget(p) => write!(f, "per_target {}", policy(p)),
5163 When::Calendar(c) if c.days.is_empty() => write!(f, "at {}", c.at),
5164 When::Calendar(c) => write!(f, "at {} [{}]", c.at, c.days.join(",")),
5165 When::On(triggers) => {
5166 let names: Vec<&str> = triggers.iter().map(|t| t.as_str()).collect();
5167 write!(f, "on [{}]", names.join(","))
5168 }
5169 }
5170 }
5171}
5172
5173impl OnTrigger {
5174 /// Lowercase wire/display label (matches the serde `snake_case`).
5175 pub fn as_str(self) -> &'static str {
5176 match self {
5177 OnTrigger::Startup => "startup",
5178 OnTrigger::Logon => "logon",
5179 OnTrigger::Lock => "lock",
5180 OnTrigger::Unlock => "unlock",
5181 OnTrigger::NetworkChange => "network_change",
5182 }
5183 }
5184}
5185
5186/// Optional validity window for a [`Schedule`] (#418 decision G).
5187/// Half-open `[from, until)`; either bound may be omitted. Bounds
5188/// are `YYYY-MM-DD` (= that day's 00:00 in the schedule's `tz`) or
5189/// full RFC3339 (offset is honored as-is, `tz` ignored). Kept as
5190/// strings so the JSON Schema the SPA editor consumes stays two
5191/// plain string fields, mirroring `jitter` / `starting_deadline`.
5192///
5193/// #418 Phase 2: bounds are evaluated in the schedule's top-level
5194/// `tz` (was UTC-only in Phase 1) so `tz: local` makes both the
5195/// calendar `at` AND the `active` window local — one consistent
5196/// timezone per schedule.
5197#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default, PartialEq, Eq)]
5198pub struct Active {
5199 /// Dormant before this instant.
5200 #[serde(default, skip_serializing_if = "Option::is_none")]
5201 pub from: Option<String>,
5202 /// Dormant from this instant on (exclusive).
5203 #[serde(default, skip_serializing_if = "Option::is_none")]
5204 pub until: Option<String>,
5205}
5206
5207impl Active {
5208 /// `skip_serializing_if` helper — an empty window means "always
5209 /// active" and is omitted from the wire format entirely.
5210 pub fn is_empty(&self) -> bool {
5211 self.from.is_none() && self.until.is_none()
5212 }
5213
5214 /// Parse one bound: RFC3339 first (offset honored, `tz`
5215 /// ignored), then bare `YYYY-MM-DD` (00:00 in `tz`).
5216 pub fn parse_bound(s: &str, tz: ScheduleTz) -> Result<chrono::DateTime<chrono::Utc>, String> {
5217 if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(s) {
5218 return Ok(dt.with_timezone(&chrono::Utc));
5219 }
5220 if let Ok(d) = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") {
5221 let midnight = d.and_hms_opt(0, 0, 0).expect("00:00:00 is always valid");
5222 return tz.naive_to_utc(midnight).ok_or_else(|| {
5223 format!("active: bound '{s}' falls in a DST gap for the schedule's tz")
5224 });
5225 }
5226 Err(format!(
5227 "active: unparseable bound '{s}' (want YYYY-MM-DD or RFC3339)"
5228 ))
5229 }
5230
5231 /// Is `now` inside the window? Unparseable bounds are treated
5232 /// as absent here (fail-open) — [`Schedule::validate`] is the
5233 /// place that rejects them loudly; this runs on every tick and
5234 /// must never panic on a stale KV blob.
5235 pub fn contains(&self, now: chrono::DateTime<chrono::Utc>, tz: ScheduleTz) -> bool {
5236 let bound = |s: &Option<String>| s.as_deref().and_then(|s| Self::parse_bound(s, tz).ok());
5237 if bound(&self.from).is_some_and(|from| now < from) {
5238 return false;
5239 }
5240 if bound(&self.until).is_some_and(|until| now >= until) {
5241 return false;
5242 }
5243 true
5244 }
5245}
5246
5247/// Host-environment gate (#418 `constraints.require`). Fire only when
5248/// the target host is in the required state. Sensed **in-process by the
5249/// agent** (Win32), so it is `runs_on: agent` only — the backend cannot
5250/// read a target host's power/idle state ([`Schedule::validate`]
5251/// rejects it on `runs_on: backend`, symmetric with `when: { on }`).
5252///
5253/// Evaluated at fire time as a skip-this-tick gate (NOT in
5254/// [`Constraints::allows`], which stays pure for `preview`): a reconcile
5255/// cadence re-checks every minute (so it effectively defers until the
5256/// state is met — the intended pairing); a `calendar` fire that lands
5257/// while the state is unmet is simply missed, same as `window`. It is
5258/// therefore a *runtime* gate and does not appear in `preview`.
5259// No `Eq`: `cpu_below: Option<f64>` is only `PartialEq` (f64 is not Eq).
5260#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default, PartialEq)]
5261pub struct Require {
5262 /// Fire only while on **AC power** (skip on battery). Reads
5263 /// `GetSystemPowerStatus`; an unknown/unreadable status is treated
5264 /// as not-on-AC (fail-closed — a restrictive gate must not fire
5265 /// when it can't confirm the condition). `false` (default) = no
5266 /// power requirement.
5267 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
5268 pub ac_power: bool,
5269 /// Fire only when the active console session has had **no keyboard /
5270 /// mouse input for at least this long** (humantime, e.g. `"10m"`) —
5271 /// "don't run while the user is actively working". Input-based
5272 /// (simpler than Task Scheduler's CPU/disk-aware idle). A
5273 /// headless / disconnected console (no interactive user) trivially
5274 /// satisfies it. `None` (default) = no idle requirement. Parsed
5275 /// lazily; [`Schedule::validate`] rejects garbage at create time.
5276 #[serde(default, skip_serializing_if = "Option::is_none")]
5277 pub idle: Option<String>,
5278 /// Fire only when the **whole-machine CPU usage is below this
5279 /// percent** (0–100; e.g. `20.0` = "system CPU < 20%") — "don't run
5280 /// while the box is busy". Reuses the agent's `host_perf` system CPU%
5281 /// sample (`sysinfo` mean over cores), so the reading is up to one
5282 /// `host_perf` cadence old (default 60s) — fine as a "generally
5283 /// busy?" proxy, and more accurate than a fresh one-shot read (CPU%
5284 /// needs two samples). An unavailable sample (host_perf not warmed
5285 /// up yet, or stale) is treated as "not below" (fail-closed — a
5286 /// restrictive gate must not fire when it can't confirm). `None`
5287 /// (default) = no CPU requirement. [`Schedule::validate`] rejects an
5288 /// out-of-range value at create time.
5289 #[serde(default, skip_serializing_if = "Option::is_none")]
5290 pub cpu_below: Option<f64>,
5291 /// Fire only when the host has **internet connectivity** (Windows
5292 /// `GetNetworkConnectivityHint` reports InternetAccess) — "don't run
5293 /// until online" for jobs that download / phone home. A captive
5294 /// portal (ConstrainedInternetAccess), LAN-only (LocalAccess), or
5295 /// unknown/unreadable state is treated as offline (fail-closed) — a
5296 /// portal would just fail a download, so we hold the run. For VPN /
5297 /// SASE / app-specific conditions, use a custom script gate (separate
5298 /// slice). `false` (default) = no network requirement.
5299 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
5300 pub network: bool,
5301}
5302
5303impl Require {
5304 /// `skip_serializing_if` helper for an embedded empty `require`.
5305 pub fn is_empty(&self) -> bool {
5306 !self.ac_power && self.idle.is_none() && self.cpu_below.is_none() && !self.network
5307 }
5308
5309 /// Parsed minimum-idle duration (`None` = no idle requirement, or an
5310 /// unparseable value — `validate` rejects the latter at create time).
5311 pub fn min_idle(&self) -> Option<std::time::Duration> {
5312 self.idle
5313 .as_deref()
5314 .and_then(|s| humantime::parse_duration(s.trim()).ok())
5315 }
5316
5317 /// First unparseable field for create-time rejection (mirrors
5318 /// [`Constraints::bad_skip_date`]).
5319 pub fn bad_idle(&self) -> Option<String> {
5320 self.idle.as_deref().and_then(|s| {
5321 humantime::parse_duration(s.trim())
5322 .err()
5323 .map(|e| format!("constraints.require.idle: invalid duration '{s}': {e}"))
5324 })
5325 }
5326}
5327
5328/// Host-environment state sensed by the agent, fed to [`require_met`].
5329/// A named struct (not positional args) so the growing set of sensed
5330/// signals — several of them `bool` — can't be transposed at a call
5331/// site. The Win32 sensing lives in `kanade-agent::env_gate`.
5332#[derive(Debug, Clone, Copy, Default)]
5333pub struct EnvState {
5334 /// Is the host on AC power (`false` if on battery or unreadable).
5335 pub ac_online: bool,
5336 /// How long the console has been idle (`None` = couldn't determine).
5337 pub idle: Option<std::time::Duration>,
5338 /// Whole-machine CPU usage 0–100 (`None` = no sample yet).
5339 pub cpu_pct: Option<f64>,
5340 /// Does the host have internet connectivity (`false` if offline /
5341 /// LAN-only / unreadable).
5342 pub network_up: bool,
5343}
5344
5345/// Pure env-gate decision (#418 `constraints.require`). The Win32
5346/// sensing lives in the agent (`kanade-agent::env_gate`); this is the
5347/// testable core, fed the already-sensed [`EnvState`]. Deliberately a
5348/// free fn (not folded into [`Constraints::allows`]) so `allows` stays
5349/// pure and `preview` never evaluates a runtime gate. Each set
5350/// requirement is a restrictive AND: any unmet (or unknown) gate skips.
5351pub fn require_met(req: &Require, env: &EnvState) -> bool {
5352 if req.ac_power && !env.ac_online {
5353 return false;
5354 }
5355 if let Some(min) = req.min_idle() {
5356 match env.idle {
5357 Some(d) if d >= min => {}
5358 _ => return false,
5359 }
5360 }
5361 if let Some(max) = req.cpu_below {
5362 match env.cpu_pct {
5363 Some(p) if p < max => {}
5364 _ => return false,
5365 }
5366 }
5367 if req.network && !env.network_up {
5368 return false;
5369 }
5370 true
5371}
5372
5373/// [`Active`] decides *over what date range* a schedule is live,
5374/// `Constraints` decides *when, within an active period,* a fire is
5375/// allowed: `window` (a maintenance time-of-day window),
5376/// `max_concurrent` (a fleet-wide running-instance cap), `skip_dates`
5377/// (holiday exclusion) and `require` (host-environment gates, agent-only
5378/// — see [`Require`]).
5379// No `Eq`: contains `require: Option<Require>` which holds an f64.
5380#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default, PartialEq)]
5381pub struct Constraints {
5382 /// `"HH:MM-HH:MM"` wall-clock window (evaluated in the schedule's
5383 /// `tz`). Fires outside it are skipped — mainly for reconcile
5384 /// cadences ("patrol every 6h, but only fire overnight") and
5385 /// daytime change-freezes. `start > end` crosses midnight
5386 /// (`"22:00-05:00"` = 22:00 through 05:00 next morning). Parsed
5387 /// lazily; [`Schedule::validate`] rejects garbage at create time.
5388 #[serde(default, skip_serializing_if = "Option::is_none")]
5389 pub window: Option<String>,
5390 /// Fleet-wide cap on how many instances of this schedule's job may
5391 /// run **at the same time** (#418 "同時実行ハード上限"). The
5392 /// backend scheduler counts the job's still-in-flight runs
5393 /// (`execution_results.finished_at IS NULL`) each tick and only
5394 /// dispatches to as many remaining pcs as there are free slots —
5395 /// a rolling window that refills as runs complete. Useful for
5396 /// disk/CPU/network-heavy jobs you don't want hammering the whole
5397 /// fleet at once.
5398 ///
5399 /// **Backend-only** (it needs a central counter): combining it
5400 /// with `runs_on: agent` is rejected by [`Schedule::validate`]
5401 /// (#418 decision E — "中央上限には中央が要る"). Most meaningful
5402 /// for `per_pc` reconcile cadences, where the poll re-ticks and
5403 /// refills slots. `None` (default) = no cap.
5404 #[serde(default, skip_serializing_if = "Option::is_none")]
5405 pub max_concurrent: Option<u32>,
5406 /// Calendar dates the schedule must **not** fire on — holidays,
5407 /// blackout days, one-off freeze dates (#418 "祝日除外"). Each is
5408 /// `YYYY-MM-DD`, evaluated as a wall-clock date in the schedule's
5409 /// `tz`. Applies to every `when` shape (a reconcile cadence skips
5410 /// the whole day; a calendar fire landing on the date is
5411 /// suppressed) and is honored by both the live scheduler and
5412 /// `preview`, since both gate on [`Constraints::allows`]. Empty
5413 /// (default) = no skips. Operator-supplied: there is no built-in
5414 /// holiday calendar — list the dates you care about. Parsed lazily;
5415 /// [`Schedule::validate`] rejects a malformed date at create time.
5416 #[serde(default, skip_serializing_if = "Vec::is_empty")]
5417 pub skip_dates: Vec<String>,
5418 /// Host-environment gate (#418): fire only when the target host is
5419 /// in the required state (on AC power, idle). Agent-sensed at fire
5420 /// time, `runs_on: agent` only. See [`Require`]. `None` (default) =
5421 /// no environment requirement.
5422 #[serde(default, skip_serializing_if = "Option::is_none")]
5423 pub require: Option<Require>,
5424}
5425
5426impl Constraints {
5427 /// `skip_serializing_if` helper — empty constraints are omitted
5428 /// from the wire format entirely.
5429 pub fn is_empty(&self) -> bool {
5430 self.window.is_none()
5431 && self.max_concurrent.is_none()
5432 && self.skip_dates.is_empty()
5433 && self.require.as_ref().is_none_or(Require::is_empty)
5434 }
5435
5436 /// The first unparseable `skip_dates` entry, if any — the
5437 /// scheduler logs it at register time so a fail-closed
5438 /// (never-firing) schedule from a hand-edited KV blob is
5439 /// diagnosable, mirroring [`Schedule::bad_window`].
5440 pub fn bad_skip_date(&self) -> Option<String> {
5441 self.skip_dates.iter().find_map(|s| {
5442 chrono::NaiveDate::parse_from_str(s.trim(), "%Y-%m-%d")
5443 .err()
5444 .map(|e| format!("constraints.skip_dates: invalid date '{s}': {e}"))
5445 })
5446 }
5447
5448 /// Parse `"HH:MM-HH:MM"` into `(start, end)`. Equal bounds are an
5449 /// error (a zero-width or all-day window is ambiguous — write no
5450 /// window for "always").
5451 pub fn parse_window(s: &str) -> Result<(chrono::NaiveTime, chrono::NaiveTime), String> {
5452 let (a, b) = s
5453 .split_once('-')
5454 .ok_or_else(|| format!("constraints.window: '{s}' must be 'HH:MM-HH:MM'"))?;
5455 let parse = |part: &str| {
5456 chrono::NaiveTime::parse_from_str(part.trim(), "%H:%M")
5457 .map_err(|e| format!("constraints.window: invalid time '{}': {e}", part.trim()))
5458 };
5459 let (start, end) = (parse(a)?, parse(b)?);
5460 if start == end {
5461 return Err(format!(
5462 "constraints.window: start and end are equal ('{s}'); omit window for 'always'"
5463 ));
5464 }
5465 Ok((start, end))
5466 }
5467
5468 /// Is a fire allowed at `now` (evaluated in `tz`)? No window =
5469 /// always allowed. Half-open `[start, end)`; `start > end`
5470 /// crosses midnight.
5471 ///
5472 /// **Fail-closed** on an unparseable window (returns `false`,
5473 /// gemini #452 review): a window is a *restrictive* constraint
5474 /// (change-freeze / overnight-only), so a corrupt one must NOT
5475 /// silently allow fires during the restricted hours. Bad windows
5476 /// are rejected at create time by [`Schedule::validate`]; this
5477 /// only bites a hand-edited KV blob, where blocking is the safe
5478 /// direction. The scheduler warns at register time
5479 /// ([`Schedule::bad_window`]) so a stuck schedule is diagnosable.
5480 /// The tick path never panics regardless.
5481 pub fn allows(&self, now: chrono::DateTime<chrono::Utc>, tz: ScheduleTz) -> bool {
5482 // #418 holiday / blackout dates: never fire on a listed wall
5483 // date (in `tz`). Checked before the window since a skipped day
5484 // overrides any within-window allowance. Fail-closed on a
5485 // corrupt entry (same posture as `window`): a skip date is a
5486 // *restrictive* constraint, so a garbled one must not silently
5487 // re-enable fires — it blocks until fixed (`validate` rejects it
5488 // at create time; `bad_skip_date` lets the scheduler warn).
5489 if !self.skip_dates.is_empty() {
5490 let today = tz.wall_date(now);
5491 let blocked = self.skip_dates.iter().any(|s| {
5492 match chrono::NaiveDate::parse_from_str(s.trim(), "%Y-%m-%d") {
5493 Ok(d) => d == today,
5494 Err(_) => true, // corrupt entry → fail-closed (block)
5495 }
5496 });
5497 if blocked {
5498 return false;
5499 }
5500 }
5501 match self.window.as_deref() {
5502 // No window → always allowed.
5503 None => true,
5504 // Window set: membership, or fail-closed if unparseable
5505 // (`window_contains` returns None for a corrupt window).
5506 Some(_) => self.window_contains(tz.wall_time(now)).unwrap_or(false),
5507 }
5508 }
5509
5510 /// Membership of a wall-clock time-of-day in the window. `None`
5511 /// when there is no window or it's unparseable (callers decide
5512 /// the failure direction). `start > end` crosses midnight.
5513 fn window_contains(&self, t: chrono::NaiveTime) -> Option<bool> {
5514 let (start, end) = Self::parse_window(self.window.as_deref()?).ok()?;
5515 Some(if start <= end {
5516 start <= t && t < end
5517 } else {
5518 t >= start || t < end
5519 })
5520 }
5521}
5522
5523/// What to do when a fire's script fails (#418 Phase 4 — the "高"
5524/// retry/backoff gap). Where [`Constraints`] gates *whether* a fire
5525/// happens, `OnFailure` decides what happens *after* one ran and
5526/// came back bad. Only `retry` so far; future `notify` / `disable`
5527/// would join the same namespace.
5528#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default, PartialEq, Eq)]
5529pub struct OnFailure {
5530 /// Re-run the script in-process when it exits non-zero (or times
5531 /// out), up to a cap, with a fixed backoff between attempts.
5532 /// `None` (default) = no retry: a failed run is published as-is
5533 /// and (for reconcile cadences) simply re-fires on the next poll
5534 /// tick. See [`Retry`].
5535 #[serde(default, skip_serializing_if = "Option::is_none")]
5536 pub retry: Option<Retry>,
5537}
5538
5539impl OnFailure {
5540 /// `skip_serializing_if` helper — an empty policy is omitted from
5541 /// the wire format entirely.
5542 pub fn is_empty(&self) -> bool {
5543 self.retry.is_none()
5544 }
5545
5546 /// Lower the operator-facing `retry` (humantime backoff) onto the
5547 /// engine vocabulary the agent's executor runs on (backoff in
5548 /// whole seconds). Single seam shared by the backend command
5549 /// builder and the agent's local scheduler so the two stamp the
5550 /// same [`crate::wire::RetrySpec`] onto every Command. Returns
5551 /// `None` when there is no retry policy or the backoff is
5552 /// unparseable (validate() rejects the latter at create time;
5553 /// this stays fail-safe = "no retry" for a hand-edited KV blob
5554 /// rather than panicking on the fire path).
5555 pub fn lowered_retry(&self) -> Option<crate::wire::RetrySpec> {
5556 let r = self.retry.as_ref()?;
5557 let backoff_secs = humantime::parse_duration(&r.backoff).ok()?.as_secs();
5558 Some(crate::wire::RetrySpec {
5559 max: r.max,
5560 backoff_secs,
5561 })
5562 }
5563}
5564
5565/// Fixed-backoff retry policy (#418 Phase 4). `max` is the number of
5566/// *additional* attempts after the first run (so `max: 3` = up to 4
5567/// total executions); `backoff` is the humantime delay slept between
5568/// attempts. The retry happens fire-side (inside `kanade fire` /
5569/// `handle_command`) on every OS for the PoC — the Windows-native
5570/// "restart on failure" Task Scheduler path is deferred to the
5571/// native-delegation phase (#418 decision H).
5572#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
5573pub struct Retry {
5574 /// Max additional attempts after the first failure. Bounded
5575 /// `1..=10` by [`Schedule::validate`] — a typo'd `max: 1000`
5576 /// with a short backoff would otherwise pin a flapping script in
5577 /// a tight loop for the whole window.
5578 pub max: u32,
5579 /// Humantime delay slept between attempts (`"10m"`, `"30s"`).
5580 pub backoff: String,
5581}
5582
5583/// Fleet-wide change-freeze (#418 Phase 5 — the "メンテナンス窓 /
5584/// 変更凍結" gap's global half). Where [`Constraints::window`] is a
5585/// *per-schedule* time-of-day gate, a `Freeze` is a *single, fleet-
5586/// global* "stop all automated change" switch the operator flips
5587/// during an incident or a year-end change-freeze. It lives in its
5588/// own KV singleton ([`crate::kv::KEY_FREEZE`]); when present and
5589/// active, both the backend scheduler and every agent's local
5590/// scheduler skip *every* fire.
5591///
5592/// Shapes:
5593/// * `{}` (no bounds) — frozen indefinitely until the operator
5594/// clears it (incident "big red button").
5595/// * `{ from, until }` — frozen only within `[from, until)`,
5596/// evaluated in `tz` (planned change-freeze; auto-thaws).
5597///
5598/// The KV key being *absent* means "not frozen" — so clearing the
5599/// freeze is a KV delete, and `is_active` only ever runs on a freeze
5600/// the operator actually set.
5601#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default, PartialEq, Eq)]
5602pub struct Freeze {
5603 /// Frozen from this instant (RFC3339 or bare `YYYY-MM-DD` in
5604 /// `tz`). `None` ⇒ frozen from the beginning of time.
5605 #[serde(default, skip_serializing_if = "Option::is_none")]
5606 pub from: Option<String>,
5607 /// Thawed from this instant on, exclusive. `None` ⇒ frozen with
5608 /// no scheduled end (manual clear required).
5609 #[serde(default, skip_serializing_if = "Option::is_none")]
5610 pub until: Option<String>,
5611 /// Operator-supplied note surfaced on the freeze-skip log and the
5612 /// SPA banner ("year-end change freeze", "INC-1234"). Advisory.
5613 #[serde(default, skip_serializing_if = "Option::is_none")]
5614 pub reason: Option<String>,
5615 /// Timezone the bare-date bounds are evaluated in (RFC3339 bounds
5616 /// carry their own offset). Defaults to host-local like a
5617 /// schedule's `tz`.
5618 #[serde(default)]
5619 pub tz: ScheduleTz,
5620}
5621
5622impl Freeze {
5623 /// Is the fleet frozen at `now`? An empty window (`from`/`until`
5624 /// both absent) is frozen unconditionally; otherwise membership of
5625 /// `[from, until)` in `tz`. Half-open like [`Active::contains`],
5626 /// but **fails CLOSED** on an unparseable bound — a freeze is a
5627 /// safety switch, so a corrupt window (only reachable via a
5628 /// hand-edited KV blob; `validate` rejects it at set time) must
5629 /// mean "frozen", not "fire normally" (coderabbit #472). This is
5630 /// the one deliberate divergence from `active`'s fail-OPEN
5631 /// behaviour, where an unparseable bound dormant-skips a schedule.
5632 pub fn is_active(&self, now: chrono::DateTime<chrono::Utc>) -> bool {
5633 // Parse a bound; an unparseable one short-circuits the whole
5634 // check to `true` (frozen) via the closure's `None` sentinel
5635 // handled below.
5636 let bound = |s: &Option<String>| -> Result<Option<chrono::DateTime<chrono::Utc>>, ()> {
5637 match s.as_deref() {
5638 None => Ok(None),
5639 Some(raw) => Active::parse_bound(raw, self.tz).map(Some).map_err(|_| ()),
5640 }
5641 };
5642 let (from, until) = match (bound(&self.from), bound(&self.until)) {
5643 (Ok(f), Ok(u)) => (f, u),
5644 // Any corrupt bound → fail closed (frozen).
5645 _ => return true,
5646 };
5647 if from.is_some_and(|f| now < f) {
5648 return false;
5649 }
5650 if until.is_some_and(|u| now >= u) {
5651 return false;
5652 }
5653 true
5654 }
5655
5656 /// Reject unparseable bounds / `from >= until` at set time (the
5657 /// API + CLI counterpart to [`Schedule::validate`]).
5658 pub fn validate(&self) -> Result<(), String> {
5659 let from = self
5660 .from
5661 .as_deref()
5662 .map(|s| Active::parse_bound(s, self.tz))
5663 .transpose()
5664 .map_err(|e| e.replace("active:", "freeze:"))?;
5665 let until = self
5666 .until
5667 .as_deref()
5668 .map(|s| Active::parse_bound(s, self.tz))
5669 .transpose()
5670 .map_err(|e| e.replace("active:", "freeze:"))?;
5671 if let (Some(f), Some(u)) = (from, until) {
5672 if f >= u {
5673 return Err(format!(
5674 "freeze.from ({}) must be strictly before freeze.until ({})",
5675 self.from.as_deref().unwrap_or_default(),
5676 self.until.as_deref().unwrap_or_default(),
5677 ));
5678 }
5679 }
5680 Ok(())
5681 }
5682}
5683
5684/// The system-generated poll cadence every reconcile-shaped `when`
5685/// lowers to. Operators never write this: the real inter-run
5686/// spacing is the `every` cooldown; this only bounds "how soon do
5687/// we notice somebody is due" (#418 decision B took the poll
5688/// period away from the operator).
5689pub const POLL_CRON: &str = "0 * * * * *";
5690
5691/// What a [`When`] lowers to — the exact (cron, mode, cooldown)
5692/// trio the pre-#418 engine ran on. Keeping the engine vocabulary
5693/// unchanged is what lets Phase 1 swap the operator surface without
5694/// touching the tick / dedup machinery.
5695pub struct Lowered {
5696 /// Cron handed to `tokio-cron-scheduler` — [`POLL_CRON`] for
5697 /// reconcile shapes, a 6/7-field cron for calendar shapes.
5698 pub cron: String,
5699 /// Dedup semantics for `decide_fire`.
5700 pub mode: ExecMode,
5701 /// Humantime re-arm interval (`None` = succeed once, skip
5702 /// forever).
5703 pub cooldown: Option<String>,
5704 /// Timezone to evaluate `cron` in (#418 Phase 2). The scheduler
5705 /// passes this to `Job::new_async_tz`. Reconcile shapes carry
5706 /// the schedule's tz too even though POLL_CRON is tz-agnostic,
5707 /// so the same value drives the `active`-window check.
5708 pub tz: ScheduleTz,
5709}
5710
5711impl Schedule {
5712 /// The error message if this schedule's `constraints.window` is
5713 /// set but unparseable, else `None`. The scheduler logs this at
5714 /// register time so a fail-closed (never-firing) schedule from a
5715 /// hand-edited KV blob is diagnosable (gemini #452 review).
5716 pub fn bad_window(&self) -> Option<String> {
5717 let w = self.constraints.window.as_deref()?;
5718 Constraints::parse_window(w).err()
5719 }
5720
5721 /// True when this is a `calendar` schedule whose fire time can
5722 /// never fall inside its `constraints.window` — the cron fires,
5723 /// the window check rejects it, and (firing only at that
5724 /// time-of-day) it effectively never runs. An easy misconfig to
5725 /// set up by accident; the scheduler warns at register time
5726 /// (claude #452 review). Reconcile shapes poll every minute, so
5727 /// they always catch the window opening and aren't affected.
5728 pub fn calendar_outside_window(&self) -> bool {
5729 let When::Calendar(c) = &self.when else {
5730 return false;
5731 };
5732 let Some(t) = c.fire_time() else {
5733 return false;
5734 };
5735 matches!(self.constraints.window_contains(t), Some(false))
5736 }
5737
5738 /// Up to `count` future instants this schedule will fire, as
5739 /// absolute UTC, strictly after `now` — the dry-run / preview
5740 /// surface (#418 "ドライラン / プレビュー"). Only **calendar**
5741 /// schedules have discrete fire times; reconcile shapes
5742 /// (`per_pc`/`per_target`) poll every minute gated by cooldown, so
5743 /// they return an empty vec and the caller describes the cadence
5744 /// instead. Occurrences outside the `active.{from,until}` window or
5745 /// the `constraints.window` are **skipped**, so the list reflects
5746 /// when the schedule will ACTUALLY run, not the raw cron ticks.
5747 /// Evaluated in the schedule's `tz`, exactly like the scheduler's
5748 /// `Job::new_async_tz`, and with the same croner config the
5749 /// scheduler / [`Schedule::validate`] use, so a preview can never
5750 /// disagree with a real fire. A schedule that can never fire (a
5751 /// calendar time wholly outside its window, a past one-shot,
5752 /// `enabled: false` is *not* considered here — callers gate on
5753 /// `enabled` separately) yields an empty vec.
5754 pub fn preview_fires(
5755 &self,
5756 now: chrono::DateTime<chrono::Utc>,
5757 count: usize,
5758 ) -> Vec<chrono::DateTime<chrono::Utc>> {
5759 use croner::parser::{CronParser, Seconds};
5760 if !matches!(self.when, When::Calendar(_)) {
5761 return Vec::new();
5762 }
5763 // Same lowering + croner config as `next_calendar_fire` and the
5764 // live scheduler, so a preview can never disagree with a real
5765 // fire. `preview_fires` adds the N-occurrence walk and the
5766 // active / window filtering on top of that single seam.
5767 let lowered = self.lowered();
5768 let Ok(cron) = CronParser::builder()
5769 .seconds(Seconds::Required)
5770 .dom_and_dow(true)
5771 .build()
5772 .parse(&lowered.cron)
5773 else {
5774 return Vec::new();
5775 };
5776 let accept = |utc: chrono::DateTime<chrono::Utc>| {
5777 self.active.contains(utc, self.tz) && self.constraints.allows(utc, self.tz)
5778 };
5779 match self.tz {
5780 ScheduleTz::Utc => Self::next_occurrences(&cron, now, count, accept),
5781 ScheduleTz::Local => {
5782 Self::next_occurrences(&cron, now.with_timezone(&chrono::Local), count, accept)
5783 }
5784 }
5785 }
5786
5787 /// Walk croner forward from `after` collecting up to `count`
5788 /// accepted occurrences (converted to UTC). Generic over the tz the
5789 /// cron is evaluated in so `preview_fires` can run it in either
5790 /// `Utc` or `Local` without duplicating the loop.
5791 fn next_occurrences<Tz>(
5792 cron: &croner::Cron,
5793 after: chrono::DateTime<Tz>,
5794 count: usize,
5795 accept: impl Fn(chrono::DateTime<chrono::Utc>) -> bool,
5796 ) -> Vec<chrono::DateTime<chrono::Utc>>
5797 where
5798 Tz: chrono::TimeZone,
5799 {
5800 // Bound the scan so an `active`/window dead-end (every future
5801 // tick rejected) can't spin forever: ~4096 raw ticks covers
5802 // >10y of a daily calendar while staying instant for croner.
5803 const SCAN_CAP: usize = 4096;
5804 let mut out = Vec::with_capacity(count.min(SCAN_CAP));
5805 let mut cursor = after;
5806 let mut scanned = 0usize;
5807 while out.len() < count && scanned < SCAN_CAP {
5808 scanned += 1;
5809 let Ok(next) = cron.find_next_occurrence(&cursor, false) else {
5810 break;
5811 };
5812 let utc = next.with_timezone(&chrono::Utc);
5813 if accept(utc) {
5814 out.push(utc);
5815 }
5816 // `find_next_occurrence(.., inclusive = false)` already
5817 // advances strictly past `cursor`, so handing it `next`
5818 // verbatim gets the following occurrence — no manual +1s
5819 // nudge (and `DateTime<Tz>` is `Copy`, so no clone).
5820 cursor = next;
5821 }
5822 out
5823 }
5824
5825 /// Lower the operator-facing `when` onto the engine vocabulary.
5826 /// Single seam shared by the backend scheduler and the agent's
5827 /// local scheduler so the two can never drift.
5828 pub fn lowered(&self) -> Lowered {
5829 let tz = self.tz;
5830 match &self.when {
5831 When::PerPc(p) => Lowered {
5832 cron: POLL_CRON.into(),
5833 mode: ExecMode::OncePerPc,
5834 cooldown: p.cooldown(),
5835 tz,
5836 },
5837 When::PerTarget(p) => Lowered {
5838 cron: POLL_CRON.into(),
5839 mode: ExecMode::OncePerTarget,
5840 cooldown: p.cooldown(),
5841 tz,
5842 },
5843 // `to_cron` only fails on a malformed `at` (rejected by
5844 // validate() at create time). For a hand-edited KV blob
5845 // that slipped past, emit a deliberately-invalid cron so
5846 // register()'s Job::new_async_tz fails → warn+skip,
5847 // rather than firing at the wrong time.
5848 When::Calendar(c) => Lowered {
5849 cron: c
5850 .to_cron()
5851 .unwrap_or_else(|_| "# invalid calendar at".into()),
5852 mode: ExecMode::EveryTick,
5853 cooldown: None,
5854 tz,
5855 },
5856 // Event triggers have no cron — the agent fires them from an
5857 // OS event source. The `# event-trigger` cron is never
5858 // registered (the scheduler branches on `is_event()` first),
5859 // but keep it deliberately-invalid as a belt-and-suspenders
5860 // so a stray registration would fail rather than misfire.
5861 When::On(_) => Lowered {
5862 cron: "# event-trigger (no cron)".into(),
5863 mode: ExecMode::Event,
5864 cooldown: None,
5865 tz,
5866 },
5867 }
5868 }
5869
5870 /// True when this schedule fires from an OS event (`when: { on }`)
5871 /// rather than a clock — the agent skips `tokio-cron` registration
5872 /// for these and drives them from boot / session-change instead.
5873 pub fn is_event(&self) -> bool {
5874 matches!(self.when, When::On(_))
5875 }
5876
5877 /// The OS event triggers this schedule listens for, or `&[]` when it
5878 /// is not an event schedule.
5879 pub fn event_triggers(&self) -> &[OnTrigger] {
5880 match &self.when {
5881 When::On(t) => t,
5882 _ => &[],
5883 }
5884 }
5885
5886 /// The next absolute (UTC) time this schedule fires, or `None` when
5887 /// it has no discrete upcoming fire to preview.
5888 ///
5889 /// Used by the KLP `maintenance.list` preview ("what's about to
5890 /// happen on my PC", SPEC §2.1). Returns `None` for:
5891 ///
5892 /// - reconcile shapes (`per_pc` / `per_target`) — they lower to the
5893 /// every-minute [`POLL_CRON`] and re-converge state continuously,
5894 /// so "next fire" is always ~60s away and means nothing to a user
5895 /// previewing upcoming maintenance;
5896 /// - a calendar schedule whose lowered cron won't parse (a
5897 /// hand-edited KV blob that slipped past [`Schedule::validate`]);
5898 /// - a cron with no future occurrence.
5899 ///
5900 /// The wall-clock fire is evaluated in the schedule's own `tz`
5901 /// (matching the live tick's `Job::new_async_tz`) then normalised
5902 /// to UTC for the wire. `inclusive = false`: strictly the *next*
5903 /// fire after `now`, never one matching the current instant.
5904 pub fn next_calendar_fire(
5905 &self,
5906 now: chrono::DateTime<chrono::Utc>,
5907 ) -> Option<chrono::DateTime<chrono::Utc>> {
5908 if !matches!(self.when, When::Calendar(_)) {
5909 return None;
5910 }
5911 let lowered = self.lowered();
5912 // Same parser configuration tokio-cron-scheduler 0.15 uses
5913 // internally, so this can never compute a fire the live
5914 // scheduler wouldn't (seconds required, DOM-and-DOW honored).
5915 let cron = croner::parser::CronParser::builder()
5916 .seconds(croner::parser::Seconds::Required)
5917 .dom_and_dow(true)
5918 .build()
5919 .parse(&lowered.cron)
5920 .ok()?;
5921 match lowered.tz {
5922 ScheduleTz::Utc => cron.find_next_occurrence(&now, false).ok(),
5923 ScheduleTz::Local => {
5924 let now_local = now.with_timezone(&chrono::Local);
5925 cron.find_next_occurrence(&now_local, false)
5926 .ok()
5927 .map(|t| t.with_timezone(&chrono::Utc))
5928 }
5929 }
5930 }
5931
5932 /// Cross-field semantic checks that don't fit pure serde derive
5933 /// — the [`Manifest::validate`] counterpart (#418 decision F;
5934 /// pre-Phase-1 a broken schedule was accepted at create time
5935 /// and silently warn-skipped at tick time). Run at every create
5936 /// site: `kanade schedule create` (client-side) and
5937 /// `POST /api/schedules`. The job_id-exists check lives in the
5938 /// API handler instead — it needs the JOBS KV.
5939 pub fn validate(&self) -> Result<(), String> {
5940 if matches!(self.runs_on, RunsOn::Agent) && matches!(self.when, When::PerTarget(_)) {
5941 return Err(
5942 "when.per_target needs fleet-wide completion data and is backend-only; \
5943 it cannot be combined with runs_on: agent (each agent self-schedules, \
5944 so per-target dedup would be deduping across a target of 1)"
5945 .into(),
5946 );
5947 }
5948 // #418 event triggers: the agent owns the OS event source
5949 // (boot / session-change), so `when: { on }` is agent-only and
5950 // needs at least one trigger.
5951 if let When::On(triggers) = &self.when {
5952 if !matches!(self.runs_on, RunsOn::Agent) {
5953 return Err(
5954 "when.on (OS event trigger) is fired by the agent's own event \
5955 source, so it requires runs_on: agent"
5956 .into(),
5957 );
5958 }
5959 if triggers.is_empty() {
5960 return Err(
5961 "when.on must list at least one trigger (e.g. [startup, logon])".into(),
5962 );
5963 }
5964 }
5965 if let Some(cd) = self.lowered().cooldown.as_deref() {
5966 humantime::parse_duration(cd)
5967 .map_err(|e| format!("when.every: invalid duration '{cd}': {e}"))?;
5968 }
5969 if let When::Calendar(c) = &self.when {
5970 // Lower the calendar form to its cron (catches a bad `at`
5971 // and the date+days conflict), then validate that cron
5972 // with the same parser configuration tokio-cron-scheduler
5973 // 0.15 uses internally (croner, seconds required,
5974 // DOM-and-DOW both honored, year optional) — create-time
5975 // validation can never accept what register() rejects.
5976 let cron = c.to_cron()?;
5977 croner::parser::CronParser::builder()
5978 .seconds(croner::parser::Seconds::Required)
5979 .dom_and_dow(true)
5980 .build()
5981 .parse(&cron)
5982 .map_err(|e| format!("when.at lowered to invalid cron '{cron}': {e}"))?;
5983 }
5984 // The other humantime strings on the schedule (claude #419
5985 // review): runtime degrades gracefully on both (bad jitter →
5986 // silent no-op, bad starting_deadline → warn + skipped tick),
5987 // but "rejected at create time" should cover every field the
5988 // operator can typo, not just `when`.
5989 if let Some(j) = &self.plan.jitter {
5990 humantime::parse_duration(j)
5991 .map_err(|e| format!("jitter: invalid duration '{j}': {e}"))?;
5992 }
5993 if let Some(sd) = &self.starting_deadline {
5994 humantime::parse_duration(sd)
5995 .map_err(|e| format!("starting_deadline: invalid duration '{sd}': {e}"))?;
5996 }
5997 let from = self
5998 .active
5999 .from
6000 .as_deref()
6001 .map(|s| Active::parse_bound(s, self.tz))
6002 .transpose()?;
6003 let until = self
6004 .active
6005 .until
6006 .as_deref()
6007 .map(|s| Active::parse_bound(s, self.tz))
6008 .transpose()?;
6009 if let (Some(f), Some(u)) = (from, until) {
6010 if f >= u {
6011 return Err(format!(
6012 "active.from ({}) must be strictly before active.until ({})",
6013 self.active.from.as_deref().unwrap_or_default(),
6014 self.active.until.as_deref().unwrap_or_default(),
6015 ));
6016 }
6017 }
6018 // #418 Phase 3: a bad maintenance window is rejected at create
6019 // time (parse_window also catches equal bounds).
6020 if let Some(w) = self.constraints.window.as_deref() {
6021 Constraints::parse_window(w)?;
6022 }
6023 // #418 holiday exclusion: reject a malformed skip date at create
6024 // time so the fail-closed `allows` path only ever bites a
6025 // hand-edited KV blob, not a fresh `kanade schedule create`.
6026 if let Some(err) = self.constraints.bad_skip_date() {
6027 return Err(err);
6028 }
6029 // #418: constraints.max_concurrent is a central running-instance
6030 // cap, so it needs the backend's counter — reject it on
6031 // runs_on: agent (decision E), and reject a meaningless 0.
6032 if let Some(mc) = self.constraints.max_concurrent {
6033 // Check the structural incompatibility (agent has no central
6034 // counter) before the value range, so a `max_concurrent: 0`
6035 // + `runs_on: agent` combo reports the more fundamental
6036 // problem first (claude #542).
6037 if matches!(self.runs_on, RunsOn::Agent) {
6038 return Err(
6039 "constraints.max_concurrent needs a central counter and is backend-only; \
6040 it cannot be combined with runs_on: agent (each agent self-schedules, \
6041 so there is no fleet-wide count to cap against)"
6042 .into(),
6043 );
6044 }
6045 if mc == 0 {
6046 return Err(
6047 "constraints.max_concurrent must be >= 1 (0 would never fire; \
6048 omit it for no cap)"
6049 .into(),
6050 );
6051 }
6052 }
6053 // #418: constraints.require (host-state env gates: ac_power /
6054 // idle / cpu_below / network) is sensed in-process by the agent,
6055 // so it needs runs_on: agent — the backend can't read a target
6056 // host's power / idle / cpu / connectivity state. Symmetric with
6057 // `when: { on }` (also agent-only); inverse of max_concurrent
6058 // (backend-only).
6059 if let Some(req) = &self.constraints.require {
6060 if !req.is_empty() && matches!(self.runs_on, RunsOn::Backend) {
6061 return Err(
6062 "constraints.require (host-state env gates: ac_power / idle / cpu_below / \
6063 network) is sensed in-process by the agent and needs runs_on: agent; the \
6064 backend cannot read a target host's power / idle / cpu / connectivity state"
6065 .into(),
6066 );
6067 }
6068 // Reject a malformed idle duration at create time so the
6069 // fail-closed runtime path only ever bites a hand-edited
6070 // KV blob (mirror skip_dates / on_failure.retry).
6071 if let Some(err) = req.bad_idle() {
6072 return Err(err);
6073 }
6074 // cpu_below is a percent — reject out-of-range so a typo
6075 // can't make a schedule that never (>=100 is always-busy?
6076 // no — <0 never matches) or trivially fires.
6077 if let Some(c) = req.cpu_below
6078 && !(c > 0.0 && c <= 100.0)
6079 {
6080 return Err(format!(
6081 "constraints.require.cpu_below must be in (0, 100] percent (got {c}); \
6082 omit it for no CPU requirement"
6083 ));
6084 }
6085 }
6086 // #418 Phase 4: a bad on_failure.retry is rejected at create
6087 // time — backoff must be valid humantime, and max is bounded
6088 // so a typo can't pin a flapping script in a tight loop.
6089 if let Some(r) = &self.on_failure.retry {
6090 let backoff = humantime::parse_duration(&r.backoff).map_err(|e| {
6091 format!(
6092 "on_failure.retry.backoff: invalid duration '{}': {e}",
6093 r.backoff
6094 )
6095 })?;
6096 // The wire form lowers backoff to whole seconds, so a
6097 // sub-second value would silently become a 0s no-wait
6098 // (coderabbit #466). Reject it rather than honour a backoff
6099 // the operator can't actually get.
6100 if backoff.as_secs() < 1 {
6101 return Err(format!(
6102 "on_failure.retry.backoff must be >= 1s (got '{}'); sub-second backoffs \
6103 round to 0 on the wire",
6104 r.backoff
6105 ));
6106 }
6107 if !(1..=10).contains(&r.max) {
6108 return Err(format!(
6109 "on_failure.retry.max must be 1..=10 (got {}); it counts additional \
6110 attempts after the first run",
6111 r.max
6112 ));
6113 }
6114 }
6115 // A blank / whitespace-only tag renders an empty filter chip on
6116 // the Schedules page — reject it at create time, mirroring the
6117 // Manifest::validate tag guard.
6118 for tag in &self.tags {
6119 if tag.trim().is_empty() {
6120 return Err("tags must not contain empty entries".to_string());
6121 }
6122 }
6123 Ok(())
6124 }
6125}
6126
6127fn default_true() -> bool {
6128 true
6129}