Skip to main content

kanade_shared/
manifest.rs

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