Skip to main content

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