Skip to main content

kanade_shared/
manifest.rs

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