Skip to main content

kanade_shared/
manifest.rs

1use serde::{Deserialize, Serialize};
2
3use crate::ipc::jobs::JobCategory;
4use crate::wire::{RunAs, Shell, Staleness};
5
6/// YAML job manifest (= registered "what to run", v0.18.0+).
7///
8/// Owns only script-intrinsic fields. **Who** (`target`), **how to
9/// phase fanout** (`rollout`), and **when to stagger start**
10/// (`jitter`) all moved to the Schedule / exec request side — same
11/// script can now be fired against different targets / rollouts
12/// without copying the script body.
13///
14/// `deny_unknown_fields` makes operators copy-pasting an older yaml
15/// that still has `target:` / `rollout:` see a clear parse error at
16/// `kanade job create` time instead of mysteriously losing it.
17#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
18#[serde(deny_unknown_fields)]
19pub struct Manifest {
20    pub id: String,
21    pub version: String,
22    #[serde(default)]
23    pub description: Option<String>,
24    pub execute: Execute,
25    #[serde(default)]
26    pub require_approval: bool,
27    /// Opt-in marker that this job produces a JSON inventory fact
28    /// payload on stdout. When present, the backend's results
29    /// projector parses `ExecResult.stdout` as JSON and upserts an
30    /// `inventory_facts` row keyed by `(pc_id, manifest.id)`. The
31    /// `display` sub-config drives the SPA's Inventory page render.
32    #[serde(default)]
33    pub inventory: Option<InventoryHint>,
34    /// Issue #246: opt-in marker that this job emits per-line
35    /// observability events on stdout (one JSON `ObsEvent` per
36    /// newline). When present, the agent — after the script exits
37    /// successfully — parses each non-empty stdout line as an
38    /// `ObsEvent`, publishes it on `obs.<pc_id>` via the
39    /// `obs_outbox`, and (intentionally) **omits the stdout from
40    /// the `ExecResult`** so the timeline data doesn't double up
41    /// in `execution_results.stdout` (which would multiply rows
42    /// by ~50/day/PC of noise).
43    ///
44    /// Distinct from `inventory:` (single JSON object → projector
45    /// upsert) — events are append-only timeline points consumed
46    /// by the dedicated `obs_events` table.
47    #[serde(default)]
48    pub emit: Option<EmitConfig>,
49    /// #290: opt-in marker that this job is an operator-defined
50    /// **health check** whose result feeds the Client App's Health
51    /// tab over KLP (`StateSnapshot.checks`). The script prints a
52    /// free-form JSON object on stdout (like any inventory job); the
53    /// agent reads the [`CheckHint::status_field`] value dynamically
54    /// into a [`crate::ipc::state::Check`] named `check.name`.
55    /// Cadence / windows / conditions come from
56    /// the job's Schedule (exactly like inventory) — there is
57    /// deliberately no interval here. **Composes with `inventory:`**:
58    /// the script's stdout is one JSON object, so a check can also
59    /// carry an `inventory:` block to project the rest of that object
60    /// (incl. `explode` sub-tables) for SPA fleet-querying. Only
61    /// `emit:` (NDJSON stdout) is incompatible.
62    #[serde(default)]
63    pub check: Option<CheckHint>,
64    /// v0.26: Layer 2 staleness policy (SPEC.md §2.6.2). Controls
65    /// what the agent does at fire time when it can't verify the
66    /// `script_current` / `script_status` KV values are fresh —
67    /// especially relevant for `runs_on: agent` schedules where
68    /// the agent may fire from cache while offline. Defaults to
69    /// `Staleness::Cached` (silently use cached values), which
70    /// matches every pre-v0.26 Manifest.
71    #[serde(default)]
72    pub staleness: Staleness,
73    /// #291: opt-in marker that this job is offered to **end users**
74    /// in the Client App's job tabs over KLP (`jobs.list` →
75    /// `jobs.execute`). Parallel to [`inventory`] / [`check`] /
76    /// [`emit`]: the block's mere presence is the opt-in, and it
77    /// groups the end-user presentation fields (name / category /
78    /// icon) that only make sense for a user-facing job. `None`
79    /// (the default) ⇒ an operator-only job — inventory, checks,
80    /// scheduled maintenance — that never surfaces in the catalog.
81    ///
82    /// The agent re-reads this at every `jobs.list` / `jobs.execute`
83    /// (SPEC §2.1), so removing the block takes a job out of a
84    /// running client on its next action.
85    ///
86    /// [`inventory`]: Manifest::inventory
87    /// [`check`]: Manifest::check
88    /// [`emit`]: Manifest::emit
89    #[serde(default, skip_serializing_if = "Option::is_none")]
90    pub client: Option<ClientHint>,
91}
92
93/// "Who + how + when-to-stagger" — the fanout-plan side of an exec.
94/// Used both as the POST `/api/exec/{job_id}` body and as the embedded
95/// `target` / `rollout` / `jitter` slot on [`Schedule`]. Centralising
96/// here keeps the validation + serialisation logic in one place.
97#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
98pub struct FanoutPlan {
99    #[serde(default)]
100    pub target: Target,
101    /// Optional wave rollout — when present, the backend publishes
102    /// each wave's group subject on its own delay schedule instead
103    /// of fanning out the `target` block in one go. `target` then
104    /// only labels the deploy for the audit log.
105    #[serde(default, skip_serializing_if = "Option::is_none")]
106    pub rollout: Option<Rollout>,
107    /// Optional humantime jitter; agent uses it to randomise
108    /// execution start. Lives here (not on the script) so different
109    /// schedules / ad-hoc fires of the same job can pick different
110    /// stagger windows.
111    #[serde(default, skip_serializing_if = "Option::is_none")]
112    pub jitter: Option<String>,
113    /// Absolute time the scheduler stamps on each emitted Command
114    /// when this exec was driven by a [`Schedule`] with
115    /// `starting_deadline`. Agents receiving a Command after this
116    /// instant publish a synthetic skipped-result instead of
117    /// running the script. `None` (default) = no deadline / catch
118    /// up whenever delivered. Operators don't usually set this
119    /// directly — the scheduler computes it from `tick_at +
120    /// starting_deadline`.
121    #[serde(default, skip_serializing_if = "Option::is_none")]
122    pub deadline_at: Option<chrono::DateTime<chrono::Utc>>,
123}
124
125/// Manifest sub-section: how the SPA should render the inventory
126/// facts this job produces. Each field name (`field`) is a top-level
127/// key in the stdout JSON, e.g. `hostname`, `ram_gb`.
128///
129/// Two render modes:
130///   * `display` — vertical "field / value" per PC, used by the
131///     `/inventory?pc=<id>` detail view. ALL columns the operator
132///     wants visible on the detail page.
133///   * `summary` — horizontal table across the fleet (row = PC,
134///     column = field) on `/inventory`. Optional; when omitted the
135///     SPA falls back to `display`, but operators usually want a
136///     trimmer "hostname / OS / CPU / RAM" set for the fleet view.
137#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
138pub struct InventoryHint {
139    /// Detail-view columns, in order.
140    pub display: Vec<DisplayField>,
141    /// Optional fleet-list columns (row = PC). Defaults to `display`
142    /// when omitted, but operators usually pick a 3-5 column subset.
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    pub summary: Option<Vec<DisplayField>>,
145    /// v0.31 / #40: payload arrays that should be exploded into
146    /// per-element rows of a derived SQLite table. Lets operators
147    /// answer cross-PC questions ("which PCs still have Chrome <
148    /// 120?", "C: >90% full") with normal SQL filters + indexes
149    /// instead of grepping JSON. The projector creates the derived
150    /// table on register and replaces this PC's rows on each result
151    /// (DELETE WHERE pc_id=? AND job_id=? + bulk INSERT). See
152    /// [`ExplodeSpec`] for the per-spec schema.
153    #[serde(default, skip_serializing_if = "Option::is_none")]
154    pub explode: Option<Vec<ExplodeSpec>>,
155    /// v0.35 / #93: top-level scalar fields whose changes the
156    /// projector logs to `inventory_history` (one event per
157    /// changed field per scan). Pairs with `explode[].track_history`
158    /// — that covers array elements; this covers single-valued
159    /// fields like `ram_bytes` / `os_version` / `cpu_model` /
160    /// `os_build` that operators want to track for "did the RAM
161    /// get upgraded?" / "when did Win 11 land on this PC?" /
162    /// "BIOS / firmware bumped?" questions. Field name = `field_path`
163    /// in the history row, `identity_json` is NULL, `before_json`
164    /// / `after_json` each carry `{"value": <prior or new value>}`.
165    /// First-ever observation of a scalar (no prior facts row)
166    /// emits `added`; subsequent value changes emit `changed`. No
167    /// `removed` events — a scalar disappearing from the payload
168    /// is rare and the operator can still see the last value via
169    /// the `before_json` of the most recent change.
170    #[serde(default, skip_serializing_if = "Option::is_none")]
171    pub history_scalars: Option<Vec<String>>,
172}
173
174/// Manifest sub-section (#290): marks a job as an operator-defined
175/// **health check**. Parallel to [`InventoryHint`] / `EmitConfig`.
176/// The stdout contract is a free-form JSON object (same as any
177/// inventory job) from which the agent reads `status_field` /
178/// `detail_field` to build the KLP [`crate::ipc::state::Check`] shown
179/// on the Client App's Health tab.
180///
181/// There is deliberately **no timing field** — when / how often /
182/// in which window a check runs is driven by the job's Schedule,
183/// exactly like inventory jobs, so operators get the full `when:` /
184/// rollout / `runs_on` expressiveness for free.
185///
186/// A check's stdout is a **free-form inventory object** (arbitrary
187/// key/value pairs + arrays) — same as any inventory job — that also
188/// carries a status field. `check:` adds only the health semantics on
189/// top: which field is the ok/warn/fail/unknown status, an optional
190/// one-line summary field, and a remediation job. Everything else
191/// (rich per-PC detail, `explode` sub-tables like a software list) is
192/// driven by a co-present [`InventoryHint`] and rendered with the
193/// SAME display logic the SPA Inventory page uses — on the Client App
194/// too. This keeps checks maximally expressive without a bespoke
195/// payload type.
196#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
197#[serde(deny_unknown_fields)]
198pub struct CheckHint {
199    /// Stable check id → [`Check.name`](crate::ipc::state::Check),
200    /// the SPA/Client React key + analytics label. Unique within the
201    /// fleet's check set.
202    pub name: String,
203    /// Top-level stdout field whose string value
204    /// (`ok`/`warn`/`fail`/`unknown`) becomes the Health-tab light
205    /// ([`CheckStatus`](crate::ipc::state::CheckStatus)). Defaults to
206    /// `"status"`; a missing / unparseable value → `unknown`.
207    #[serde(default = "default_status_field")]
208    pub status_field: String,
209    /// Top-level stdout field used as the Health-tab row's one-line
210    /// summary. Defaults to `"detail"`; absent in the payload → no
211    /// detail line (the rich breakdown lives in the inventory view).
212    #[serde(default = "default_detail_field")]
213    pub detail_field: String,
214    /// Optional remediation job id →
215    /// [`Check.troubleshoot`](crate::ipc::state::Check). The Client
216    /// App shows a "修復する" button when present; that job must be
217    /// `user_invokable`.
218    #[serde(default, skip_serializing_if = "Option::is_none")]
219    pub troubleshoot: Option<String>,
220    /// #290 PR-E: when `true` (default), the backend also projects this
221    /// check's `status` / `detail` into the `check_status` table so the
222    /// operator SPA gets a fleet-wide compliance view for free — no
223    /// `inventory:` block needed. Set `fleet: false` for a client-only
224    /// check the operator doesn't want surfaced across the fleet.
225    #[serde(default = "default_fleet")]
226    pub fleet: bool,
227}
228
229fn default_status_field() -> String {
230    "status".to_string()
231}
232
233fn default_detail_field() -> String {
234    "detail".to_string()
235}
236
237fn default_fleet() -> bool {
238    true
239}
240
241/// Manifest sub-section (#291): marks a job as **user-invokable**
242/// from the Client App and carries how it presents to the end user.
243/// Parallel to [`InventoryHint`] / [`CheckHint`] / `EmitConfig` —
244/// the block's presence is the opt-in (no separate boolean), and its
245/// required fields (`name`, `category`) are enforced by serde at
246/// parse time, so a half-filled catalog entry fails
247/// `kanade job create` instead of rendering a nameless / tab-less row.
248///
249/// The agent maps this 1:1 into the KLP
250/// [`UserInvokableJob`](crate::ipc::jobs::UserInvokableJob) wire shape
251/// that `jobs.list` returns; the Client App renders one row per job in
252/// the tab named by `category`.
253#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
254#[serde(deny_unknown_fields)]
255pub struct ClientHint {
256    /// End-user-facing title for the job row. The operator-internal
257    /// `Manifest::id` slug is rarely what an end user should read, so
258    /// this is required (and validated non-empty by
259    /// [`Manifest::validate`]). Maps to `UserInvokableJob::display_name`.
260    pub name: String,
261    /// Optional one-line subtitle under `name` in the Client App.
262    /// Distinct from the operator-facing top-level
263    /// [`Manifest::description`] — this one is written for the end
264    /// user. Maps to `UserInvokableJob::display_description`.
265    #[serde(default, skip_serializing_if = "Option::is_none")]
266    pub description: Option<String>,
267    /// Which Client App tab the job lives in (`software_update` →
268    /// アップデート, `troubleshoot` → 困ったとき, `catalog` → software
269    /// catalog). Required — without it the agent can't place the job
270    /// in a tab.
271    pub category: JobCategory,
272    /// Optional icon hint for the job row — a lucide-react icon name
273    /// or a `data:` URL. `None` ⇒ the Client App falls back to the
274    /// category's default icon. Surfaced verbatim in
275    /// `jobs.list[].icon`.
276    #[serde(default, skip_serializing_if = "Option::is_none")]
277    pub icon: Option<String>,
278}
279
280/// Issue #246 — `emit:` manifest block for jobs whose stdout is
281/// NDJSON observability events (one `ObsEvent` per line). Parallel
282/// to `inventory:` but for the append-only timeline pipeline; see
283/// `Manifest::emit` for the full contract.
284#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
285#[serde(deny_unknown_fields)]
286pub struct EmitConfig {
287    /// What kind of payload the agent should expect on stdout. Only
288    /// `events` is defined today (parses each non-empty line as
289    /// `ObsEvent` and publishes on `obs.<pc_id>`); future variants
290    /// (e.g. metrics streams, structured trace events) plug in here.
291    #[serde(rename = "type")]
292    pub kind: EmitKind,
293    /// Operator hint for where the script keeps its own state — the
294    /// watermark file the PowerShell / sh body reads + writes
295    /// between runs so it only emits NEW events since the last
296    /// poll. The agent doesn't read this; it's documentation that
297    /// the SPA (and `kanade job edit`) can surface to operators
298    /// reviewing the manifest. Optional; the script is allowed to
299    /// keep state anywhere (registry, env, etc.) — the field's
300    /// presence makes the convention discoverable.
301    #[serde(default, skip_serializing_if = "Option::is_none")]
302    pub watermark_path: Option<String>,
303}
304
305/// `emit.type` enum. Lowercase serde so manifests read
306/// `type: events` rather than `Events`.
307#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
308#[serde(rename_all = "lowercase")]
309pub enum EmitKind {
310    /// Per-line `ObsEvent` JSON. Agent parses + publishes on
311    /// `obs.<pc_id>`, drops the stdout from the resulting
312    /// `ExecResult`.
313    Events,
314}
315
316/// v0.31 / #40: declarative "flatten this JSON array into a real
317/// SQLite table" spec on an inventory manifest. The projector
318/// creates the table on first registration (CREATE TABLE IF NOT
319/// EXISTS + indexes) and writes a row per element of
320/// `payload[field]` on every result, scoped by (pc_id, job_id) so
321/// each PC's rows replace cleanly without a per-PC schema.
322#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
323pub struct ExplodeSpec {
324    /// JSON array key under the payload to explode. E.g. `"apps"`
325    /// for `payload: { apps: [{...}, {...}] }`.
326    pub field: String,
327    /// Derived SQLite table name. Operators choose this — pick
328    /// something namespaced + stable (`inventory_sw_apps`, not
329    /// `apps`) so multiple inventory manifests don't collide on a
330    /// generic name.
331    pub table: String,
332    /// Element-level fields that uniquely identify a row inside one
333    /// PC's payload. The full PK is `(pc_id, job_id) + these
334    /// columns`. Required — operators must think about uniqueness
335    /// (e.g. `["name", "source"]` for installed apps because the
336    /// same name appears in multiple uninstall hives).
337    ///
338    /// v0.31 / #41: same tuple drives history identity. When
339    /// `track_history` is on, the projector serialises these
340    /// fields' values into `inventory_history.identity_json` for
341    /// every change event, so queries like "every PC that ever
342    /// installed Chrome (any source)" filter on identity_json
343    /// content without a per-manifest schema.
344    pub primary_key: Vec<String>,
345    /// Per-element fields that become columns in the derived table.
346    pub columns: Vec<ExplodeColumn>,
347    /// v0.31 / #41: when true (default false), the projector
348    /// diffs each PC's incoming payload against the prior rows
349    /// for the same (pc_id, job_id) BEFORE the DELETE-then-INSERT
350    /// replace, and writes added / removed / changed events into
351    /// `inventory_history`. Lets operators answer time-dimension
352    /// questions ("when did Chrome 120 first appear on PC X?",
353    /// "what's the Win 11 23H2 rollout curve") without storing
354    /// per-scan snapshots. Off by default so operators opt in
355    /// per-spec — history has a real storage cost on long-lived
356    /// deployments (mitigated by the 90-day default retention
357    /// sweeper, see `cleanup` module).
358    #[serde(default)]
359    pub track_history: bool,
360}
361
362/// One column in an [`ExplodeSpec`]'s derived table.
363#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
364pub struct ExplodeColumn {
365    /// JSON key under each array element. Becomes the column name
366    /// in the derived SQLite table — we don't rename.
367    pub field: String,
368    /// SQLite affinity: `"text"` (default), `"integer"`, `"real"`.
369    /// Storage maps directly via `sqlx::query.bind(...)`; type
370    /// mismatches at INSERT-time fail loudly rather than silently
371    /// dropping the row.
372    #[serde(default, skip_serializing_if = "Option::is_none")]
373    #[serde(rename = "type")]
374    pub kind: Option<String>,
375    /// When true, the projector creates a `CREATE INDEX` on this
376    /// column at table-creation time. Boost for the common-filter
377    /// columns (`name`, `version`) — operators mark them
378    /// explicitly, the projector won't guess.
379    #[serde(default)]
380    pub index: bool,
381}
382
383#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
384pub struct DisplayField {
385    /// Top-level key in the stdout JSON.
386    pub field: String,
387    /// Human-readable column header.
388    pub label: String,
389    /// Optional render hint — `"number"`, `"bytes"`, `"timestamp"`,
390    /// or `"table"` (#39). Defaults to plain text rendering on the
391    /// SPA side. `"table"` expects the field's value to be a JSON
392    /// array of objects and renders a nested sub-table on the
393    /// per-PC detail page using `columns` as the schema; the fleet
394    /// summary view falls back to showing the row count for
395    /// `"table"` cells so the wide list stays compact.
396    #[serde(default, skip_serializing_if = "Option::is_none")]
397    #[serde(rename = "type")]
398    pub kind: Option<String>,
399    /// v0.30 / #39: when `kind == "table"`, the SPA renders the
400    /// field's value (an array of objects like
401    /// `disks: [{ device_id, size_bytes, ... }]`) as a nested
402    /// sub-table using these columns. Each column is itself a
403    /// `DisplayField`, so the nested cells reuse the same render
404    /// hints (`bytes`, `number`, `timestamp`) — no parallel format
405    /// pipeline. Ignored for any other `kind`.
406    #[serde(default, skip_serializing_if = "Option::is_none")]
407    pub columns: Option<Vec<DisplayField>>,
408}
409
410#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
411pub struct Rollout {
412    #[serde(default)]
413    pub strategy: RolloutStrategy,
414    pub waves: Vec<Wave>,
415}
416
417#[derive(
418    Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
419)]
420#[serde(rename_all = "lowercase")]
421pub enum RolloutStrategy {
422    #[default]
423    Wave,
424}
425
426#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
427pub struct Wave {
428    pub group: String,
429    /// humantime delay measured from the deploy's publish time. wave[0]
430    /// typically has "0s"; subsequent waves use minutes / hours.
431    pub delay: String,
432}
433
434#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default)]
435pub struct Target {
436    #[serde(default)]
437    pub groups: Vec<String>,
438    #[serde(default)]
439    pub pcs: Vec<String>,
440    #[serde(default)]
441    pub all: bool,
442}
443
444impl Target {
445    /// At least one of all / groups / pcs is set.
446    pub fn is_specified(&self) -> bool {
447        self.all || !self.groups.is_empty() || !self.pcs.is_empty()
448    }
449}
450
451#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
452#[serde(deny_unknown_fields)]
453pub struct Execute {
454    pub shell: ExecuteShell,
455    /// Inline script body. Mutually exclusive with [`script_file`]
456    /// and [`script_object`]; exactly one of the three must be set
457    /// (enforced by [`Execute::validate_script_source`] at the
458    /// write-side parse boundaries — `kanade job create` and
459    /// `POST /api/jobs`).
460    ///
461    /// Empty string is treated as **unset** so operators can swap
462    /// to a `script_file:` / `script_object:` alternative just by
463    /// commenting out the body, without having to also drop the
464    /// `script:` key entirely.
465    ///
466    /// [`script_file`]: Self::script_file
467    /// [`script_object`]: Self::script_object
468    #[serde(default, skip_serializing_if = "Option::is_none")]
469    pub script: Option<String>,
470    /// Repo-local file path resolved by the operator-side CLI at
471    /// `kanade job create` time. The CLI reads the file, slots its
472    /// contents into `script`, and clears this field before
473    /// POSTing — so the backend / agents never see `script_file`
474    /// in stored manifests. SPEC §2.4.1.
475    ///
476    /// Resolver lands in a follow-up PR
477    /// (yukimemi/kanade#210); today this field passes parse-time
478    /// validation but the operator-side CLI bails with "not yet
479    /// implemented" until the resolver ships, so manifests that
480    /// reach the backend with `script_file` set are treated as a
481    /// schema-bug.
482    #[serde(default, skip_serializing_if = "Option::is_none")]
483    pub script_file: Option<String>,
484    /// Object Store reference (`<name>/<version>`) into the
485    /// `scripts` bucket (`OBJECT_SCRIPTS`). Agents fetch the body
486    /// at Execute time via `/api/script-objects/{name}/{version}`
487    /// and cache it locally. SPEC §2.4.1.
488    ///
489    /// Resolver lands in the same follow-up PR as `script_file`;
490    /// today this field passes parse-time validation but the
491    /// backend / agent exec paths bail with "not yet implemented"
492    /// when they see it.
493    #[serde(default, skip_serializing_if = "Option::is_none")]
494    pub script_object: Option<String>,
495    /// humantime duration string (e.g. "30s", "10m"). Script-intrinsic
496    /// — represents how long this script reasonably takes to run.
497    pub timeout: String,
498    /// Token + session combination the agent uses to launch the
499    /// script (v0.21). Default = [`RunAs::System`] (Session 0,
500    /// LocalSystem privileges, no GUI) — matches pre-v0.21 behavior.
501    #[serde(default)]
502    pub run_as: RunAs,
503    /// Working directory for the spawned child (v0.21.1). When
504    /// unset, the child inherits the agent's cwd — on Windows that
505    /// means `%SystemRoot%\System32` for the prod service, which is
506    /// almost never what operators actually want. Use an absolute
507    /// path; relative paths are passed through to the OS verbatim.
508    /// `%PROGRAMDATA%` works for `run_as: system`; for `run_as: user`
509    /// you'd want `%USERPROFILE%` (but expansion happens in the
510    /// shell, so write `$env:USERPROFILE` for PowerShell, or set
511    /// it via teravars before `kanade job create`).
512    #[serde(default, skip_serializing_if = "Option::is_none")]
513    pub cwd: Option<String>,
514}
515
516impl Execute {
517    /// Treat an empty `script:` body as "intentionally unset". Operators
518    /// commenting out a block-scalar tend to leave the key behind, and
519    /// failing the validator on `script: ""` would surprise them.
520    fn has_inline_script(&self) -> bool {
521        matches!(&self.script, Some(s) if !s.is_empty())
522    }
523
524    /// Enforce that exactly one of `script` / `script_file` /
525    /// `script_object` is set. Called at the write-side parse
526    /// boundaries (CLI `kanade job create` + backend
527    /// `POST /api/jobs`) so ambiguous YAML is rejected before it
528    /// reaches the JOBS KV. Read paths (projector, agent
529    /// scheduler, list endpoints) skip this check — they only ever
530    /// see what the write path already validated.
531    pub fn validate_script_source(&self) -> Result<(), String> {
532        let inline = self.has_inline_script();
533        let file = self.script_file.is_some();
534        let obj = self.script_object.is_some();
535        let set = [inline, file, obj].into_iter().filter(|b| *b).count();
536        match set {
537            1 => Ok(()),
538            0 => Err("execute: one of `script`, `script_file`, `script_object` must be set".into()),
539            _ => Err(format!(
540                "execute: only one of `script` / `script_file` / `script_object` may be set \
541                 (got script={inline}, script_file={file}, script_object={obj})"
542            )),
543        }
544    }
545}
546
547impl Manifest {
548    /// Cross-field semantic checks that don't fit into pure serde
549    /// derive. Currently delegates to
550    /// [`Execute::validate_script_source`] — see that method's
551    /// docs for the rationale on which call sites should run this.
552    pub fn validate(&self) -> Result<(), String> {
553        self.execute.validate_script_source()?;
554        // Stdout-format compatibility. `inventory:` and `check:` both
555        // consume the SAME single JSON object — they COMPOSE: a check
556        // can extract `status`/`detail` for the Health tab while the
557        // projector explodes the rest into SPA sub-tables. `emit:` is
558        // different — its stdout is NDJSON and the agent omits it from
559        // the result entirely — so it can't be paired with either.
560        if self.emit.is_some() && (self.inventory.is_some() || self.check.is_some()) {
561            return Err(
562                "`emit:` is incompatible with `inventory:` / `check:` — emit's stdout is NDJSON \
563                 timeline events (and omitted from the result), while inventory/check read a \
564                 single JSON object from stdout"
565                    .to_string(),
566            );
567        }
568        // A check's `name` is the Health-tab row id (React key); the
569        // field names tell the agent where to read status/detail.
570        // An empty value is an invisible runtime bug, and the serde
571        // defaults don't guard an operator who writes `status_field:
572        // ""` explicitly — reject all three here.
573        if let Some(check) = &self.check {
574            for (label, value) in [
575                ("check.name", &check.name),
576                ("check.status_field", &check.status_field),
577                ("check.detail_field", &check.detail_field),
578            ] {
579                if value.trim().is_empty() {
580                    return Err(format!("{label} must not be empty"));
581                }
582            }
583            // A present-but-blank `troubleshoot` is a broken
584            // remediation job id (the "修復する" button would target
585            // an empty manifest id) — reject it too.
586            if let Some(troubleshoot) = &check.troubleshoot {
587                if troubleshoot.trim().is_empty() {
588                    return Err("check.troubleshoot must not be empty when set".to_string());
589                }
590            }
591        }
592        // #291: a `client:` job is rendered in the Client App's
593        // catalog (`jobs.list` → `jobs.execute`). serde already makes
594        // `name` + `category` required at parse time; the only gap is
595        // a present-but-blank `name`, which would render an empty row
596        // title — reject it like the other display-id fields.
597        if let Some(client) = &self.client {
598            if client.name.trim().is_empty() {
599                return Err("client.name must not be empty".to_string());
600            }
601            // Optional display fields, when present, must be
602            // meaningful: a blank `description` renders an empty
603            // subtitle and a blank `icon` is a dangling lucide name.
604            // Same present-but-blank guard the `check:` block applies
605            // to its optional `troubleshoot` id.
606            for (label, value) in [
607                ("client.description", &client.description),
608                ("client.icon", &client.icon),
609            ] {
610                if let Some(v) = value {
611                    if v.trim().is_empty() {
612                        return Err(format!("{label} must not be empty when set"));
613                    }
614                }
615            }
616        }
617        Ok(())
618    }
619}
620
621#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
622#[serde(rename_all = "lowercase")]
623pub enum ExecuteShell {
624    Powershell,
625    Cmd,
626}
627
628impl From<ExecuteShell> for Shell {
629    fn from(s: ExecuteShell) -> Self {
630        match s {
631            ExecuteShell::Powershell => Shell::Powershell,
632            ExecuteShell::Cmd => Shell::Cmd,
633        }
634    }
635}
636
637#[cfg(test)]
638mod tests {
639    use super::*;
640
641    /// The example check-job + schedule YAMLs shipped under `configs/`
642    /// must stay valid as the schema evolves (#290 PR-C). `include_str!`
643    /// pins them at compile time so a breaking edit fails `cargo test`
644    /// rather than only `kanade job create` at deploy time.
645    #[test]
646    fn example_check_job_yamls_parse_and_validate() {
647        let jobs = [
648            (
649                "check-bitlocker",
650                include_str!("../../../configs/jobs/check-bitlocker.yaml"),
651            ),
652            (
653                "check-av-signature",
654                include_str!("../../../configs/jobs/check-av-signature.yaml"),
655            ),
656            (
657                "check-cert-expiry",
658                include_str!("../../../configs/jobs/check-cert-expiry.yaml"),
659            ),
660        ];
661        for (name, yaml) in jobs {
662            let m: Manifest =
663                serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{name} parse: {e}"));
664            m.validate()
665                .unwrap_or_else(|e| panic!("{name} validate: {e}"));
666            let check = m
667                .check
668                .as_ref()
669                .unwrap_or_else(|| panic!("{name} must carry a check: hint"));
670            assert!(!check.name.trim().is_empty(), "{name} check.name empty");
671            // These three examples all read admin-only WMI namespaces,
672            // so they run_as system. NOTE: that's a property of these
673            // particular checks, NOT of the `check:` contract — a check
674            // probing user-session state could legitimately run_as user.
675            assert_eq!(
676                m.execute.run_as,
677                RunAs::System,
678                "{name} should run_as system"
679            );
680        }
681    }
682
683    /// The example user-invokable job YAMLs (#291) shipped under
684    /// `configs/jobs/` must stay valid as the `client:` schema
685    /// evolves. `include_str!` pins them at compile time so a breaking
686    /// edit fails `cargo test`, not `kanade job create` at deploy.
687    #[test]
688    fn example_client_job_yamls_parse_and_validate() {
689        let jobs = [
690            (
691                "fix-teams-cache",
692                JobCategory::Troubleshoot,
693                include_str!("../../../configs/jobs/fix-teams-cache.yaml"),
694            ),
695            (
696                "chrome-update",
697                JobCategory::SoftwareUpdate,
698                include_str!("../../../configs/jobs/chrome-update.yaml"),
699            ),
700            (
701                "install-slack",
702                JobCategory::Catalog,
703                include_str!("../../../configs/jobs/install-slack.yaml"),
704            ),
705        ];
706        for (id, category, yaml) in jobs {
707            let m: Manifest =
708                serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{id} parse: {e}"));
709            m.validate()
710                .unwrap_or_else(|e| panic!("{id} validate: {e}"));
711            assert_eq!(m.id, id, "{id} id mismatch");
712            let client = m
713                .client
714                .as_ref()
715                .unwrap_or_else(|| panic!("{id} must carry a client: block"));
716            assert!(!client.name.trim().is_empty(), "{id} client.name empty");
717            assert_eq!(client.category, category, "{id} category");
718        }
719    }
720
721    #[test]
722    fn example_check_schedule_yamls_parse_and_validate() {
723        let schedules = [
724            (
725                "check-bitlocker",
726                include_str!("../../../configs/schedules/check-bitlocker.yaml"),
727            ),
728            (
729                "check-av-signature",
730                include_str!("../../../configs/schedules/check-av-signature.yaml"),
731            ),
732            (
733                "check-cert-expiry",
734                include_str!("../../../configs/schedules/check-cert-expiry.yaml"),
735            ),
736        ];
737        for (name, yaml) in schedules {
738            let s: Schedule =
739                serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("{name} schedule parse: {e}"));
740            s.validate()
741                .unwrap_or_else(|e| panic!("{name} schedule validate: {e}"));
742            assert_eq!(s.job_id, name, "{name} schedule must reference its job");
743        }
744    }
745
746    #[test]
747    fn target_is_specified_requires_at_least_one_field() {
748        let empty = Target::default();
749        assert!(!empty.is_specified());
750
751        let with_all = Target {
752            all: true,
753            ..Target::default()
754        };
755        assert!(with_all.is_specified());
756
757        let with_groups = Target {
758            groups: vec!["canary".into()],
759            ..Target::default()
760        };
761        assert!(with_groups.is_specified());
762
763        let with_pcs = Target {
764            pcs: vec!["pc-01".into()],
765            ..Target::default()
766        };
767        assert!(with_pcs.is_specified());
768    }
769
770    #[test]
771    fn manifest_deserialises_minimal_yaml() {
772        // Matches jobs/echo-test.yaml. v0.18: no target/rollout/jitter
773        // — those live on the schedule / exec request now.
774        let yaml = r#"
775id: echo-test
776version: 0.0.1
777execute:
778  shell: powershell
779  script: "echo 'kanade'"
780  timeout: 30s
781"#;
782        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
783        assert_eq!(m.id, "echo-test");
784        assert_eq!(m.version, "0.0.1");
785        assert!(matches!(m.execute.shell, ExecuteShell::Powershell));
786        assert_eq!(
787            m.execute.script.as_deref().map(str::trim),
788            Some("echo 'kanade'")
789        );
790        assert!(m.execute.script_file.is_none());
791        assert!(m.execute.script_object.is_none());
792        assert_eq!(m.execute.timeout, "30s");
793        assert!(!m.require_approval);
794        m.validate()
795            .expect("inline-script manifest passes validation");
796    }
797
798    #[test]
799    fn manifest_parses_check_job_and_validates() {
800        // An operator-defined health check (#290): a `check:` hint +
801        // a PowerShell script that prints {status, detail}.
802        let yaml = r#"
803id: check-bitlocker
804version: 0.1.0
805execute:
806  shell: powershell
807  run_as: system
808  timeout: 15s
809  script: |
810    [pscustomobject]@{ status = 'ok'; detail = 'all volumes protected' } | ConvertTo-Json -Compress
811check:
812  name: bitlocker
813  troubleshoot: fix-bitlocker
814"#;
815        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
816        let check = m.check.as_ref().expect("check hint present");
817        assert_eq!(check.name, "bitlocker");
818        assert_eq!(check.troubleshoot.as_deref(), Some("fix-bitlocker"));
819        // Field names default to the conventional "status" / "detail".
820        assert_eq!(check.status_field, "status");
821        assert_eq!(check.detail_field, "detail");
822        assert!(m.inventory.is_none() && m.emit.is_none());
823        m.validate().expect("check-only manifest passes validation");
824    }
825
826    #[test]
827    fn manifest_check_defaults_and_custom_fields() {
828        // Minimal: only `name`; status/detail fields default.
829        let m: Manifest = serde_yaml::from_str(
830            r#"
831id: check-disk
832version: 0.1.0
833execute:
834  shell: powershell
835  script: "[pscustomobject]@{ status = 'ok' } | ConvertTo-Json -Compress"
836  timeout: 10s
837check:
838  name: disk_free
839"#,
840        )
841        .expect("parse");
842        let c = m.check.as_ref().unwrap();
843        assert_eq!(c.name, "disk_free");
844        assert_eq!(c.status_field, "status");
845        assert_eq!(c.detail_field, "detail");
846        assert!(c.troubleshoot.is_none());
847        m.validate().expect("validates");
848
849        // The operator can point status/detail at any field of their
850        // free-form inventory object.
851        let m2: Manifest = serde_yaml::from_str(
852            r#"
853id: check-custom
854version: 0.1.0
855execute:
856  shell: powershell
857  script: "echo x"
858  timeout: 10s
859check:
860  name: patch_level
861  status_field: compliance
862  detail_field: summary
863"#,
864        )
865        .expect("parse");
866        let c2 = m2.check.as_ref().unwrap();
867        assert_eq!(c2.status_field, "compliance");
868        assert_eq!(c2.detail_field, "summary");
869    }
870
871    #[test]
872    fn manifest_allows_check_composed_with_inventory() {
873        // `check:` + `inventory:` COMPOSE on the same stdout object:
874        // status/detail → Health tab, the rest → SPA projection +
875        // explode sub-tables. Must pass validation.
876        let yaml = r#"
877id: check-bitlocker-detailed
878version: 0.1.0
879execute:
880  shell: powershell
881  script: "echo x"
882  timeout: 10s
883check:
884  name: bitlocker
885inventory:
886  display:
887    - { field: status, label: Status }
888"#;
889        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
890        assert!(m.check.is_some() && m.inventory.is_some());
891        m.validate().expect("check + inventory compose");
892    }
893
894    #[test]
895    fn manifest_rejects_check_combined_with_emit() {
896        // `emit:` stdout is NDJSON (and omitted from the result), so
897        // it can't pair with `check:` (which needs a single JSON
898        // object on stdout).
899        let yaml = r#"
900id: bad-mix
901version: 0.1.0
902execute:
903  shell: powershell
904  script: "echo x"
905  timeout: 10s
906check:
907  name: bitlocker
908emit:
909  type: events
910"#;
911        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
912        let err = m.validate().expect_err("emit + check must fail");
913        assert!(err.contains("incompatible"), "err: {err}");
914    }
915
916    #[test]
917    fn manifest_rejects_emit_combined_with_inventory() {
918        // The other half of the emit-incompatibility condition.
919        let yaml = r#"
920id: bad-mix-2
921version: 0.1.0
922execute:
923  shell: powershell
924  script: "echo x"
925  timeout: 10s
926emit:
927  type: events
928inventory:
929  display:
930    - { field: status, label: Status }
931"#;
932        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
933        let err = m.validate().expect_err("emit + inventory must fail");
934        assert!(err.contains("incompatible"), "err: {err}");
935    }
936
937    #[test]
938    fn manifest_rejects_empty_check_field_names() {
939        // Empty name / status_field / detail_field are invisible
940        // runtime bugs (empty React key, agent reads the wrong field)
941        // — reject them even though serde supplies non-empty defaults.
942        let base = |inner: &str| {
943            format!(
944                "id: c\nversion: 0.1.0\nexecute:\n  shell: powershell\n  script: \"echo x\"\n  timeout: 10s\ncheck:\n{inner}"
945            )
946        };
947        for inner in [
948            "  name: \"\"\n",
949            "  name: ok\n  status_field: \"\"\n",
950            "  name: ok\n  detail_field: \"   \"\n",
951            // present-but-blank troubleshoot → broken remediation id.
952            "  name: ok\n  troubleshoot: \"  \"\n",
953        ] {
954            let m: Manifest = serde_yaml::from_str(&base(inner)).expect("parse");
955            let err = m.validate().expect_err("empty field must fail");
956            assert!(err.contains("must not be empty"), "err: {err}");
957        }
958    }
959
960    #[test]
961    fn manifest_client_absent_by_default() {
962        // A plain operator job (the overwhelming majority) carries no
963        // `client:` block, so it never surfaces in the end-user
964        // catalog.
965        let yaml = r#"
966id: echo-test
967version: 0.0.1
968execute:
969  shell: powershell
970  script: "echo 'kanade'"
971  timeout: 30s
972"#;
973        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
974        assert!(m.client.is_none());
975        m.validate().expect("operator-only job validates");
976    }
977
978    #[test]
979    fn manifest_client_parses_and_validates() {
980        // The Client App "困ったとき" remediation job shape: a
981        // user-invokable troubleshoot job with the end-user fields the
982        // KLP `jobs.list` wire needs, grouped under `client:`.
983        let yaml = r#"
984id: fix-teams-cache
985version: 1.0.0
986execute:
987  shell: powershell
988  script: "echo clearing"
989  timeout: 60s
990client:
991  name: "Teams のキャッシュをクリア"
992  description: "Teams が重いときに試してください"
993  category: troubleshoot
994  icon: brush-cleaning
995"#;
996        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
997        let c = m.client.as_ref().expect("client block present");
998        assert_eq!(c.name, "Teams のキャッシュをクリア");
999        assert_eq!(
1000            c.description.as_deref(),
1001            Some("Teams が重いときに試してください")
1002        );
1003        assert_eq!(c.category, JobCategory::Troubleshoot);
1004        assert_eq!(c.icon.as_deref(), Some("brush-cleaning"));
1005        m.validate().expect("user-invokable job validates");
1006    }
1007
1008    #[test]
1009    fn manifest_client_minimal_only_name_and_category() {
1010        // description + icon are optional; name + category are the
1011        // serde-required minimum.
1012        let yaml = r#"
1013id: install-slack
1014version: 1.0.0
1015execute:
1016  shell: powershell
1017  script: "echo install"
1018  timeout: 600s
1019client:
1020  name: Slack
1021  category: catalog
1022"#;
1023        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1024        let c = m.client.as_ref().expect("client present");
1025        assert_eq!(c.category, JobCategory::Catalog);
1026        assert!(c.description.is_none() && c.icon.is_none());
1027        m.validate().expect("minimal client validates");
1028    }
1029
1030    #[test]
1031    fn manifest_client_rejects_blank_name() {
1032        // serde guarantees `name`/`category` are present; the one gap
1033        // is a present-but-blank name → empty catalog row title.
1034        let yaml = r#"
1035id: j
1036version: 1.0.0
1037execute:
1038  shell: powershell
1039  script: "echo x"
1040  timeout: 30s
1041client:
1042  name: "   "
1043  category: catalog
1044"#;
1045        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1046        let err = m.validate().expect_err("blank name must fail");
1047        assert!(err.contains("client.name"), "err: {err}");
1048    }
1049
1050    #[test]
1051    fn manifest_client_rejects_blank_optional_fields() {
1052        // description / icon are optional, but a present-but-blank
1053        // value is a bug (empty subtitle / dangling icon name) — reject
1054        // it, mirroring the check: block's troubleshoot guard.
1055        for (field, line) in [
1056            ("client.description", "  description: \"  \"\n"),
1057            ("client.icon", "  icon: \"\"\n"),
1058        ] {
1059            let yaml = format!(
1060                "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}"
1061            );
1062            let m: Manifest = serde_yaml::from_str(&yaml).expect("parse");
1063            let err = m.validate().expect_err("blank optional field must fail");
1064            assert!(err.contains(field), "expected {field} in err: {err}");
1065        }
1066    }
1067
1068    #[test]
1069    fn manifest_client_requires_category_at_parse() {
1070        // A `client:` block missing `category` is a hard parse error
1071        // (serde required field) — no manual validate() needed.
1072        let yaml = r#"
1073id: j
1074version: 1.0.0
1075execute:
1076  shell: powershell
1077  script: "echo x"
1078  timeout: 30s
1079client:
1080  name: "A job"
1081"#;
1082        let r: Result<Manifest, _> = serde_yaml::from_str(yaml);
1083        assert!(
1084            r.is_err(),
1085            "missing category must be a parse error, got {r:?}"
1086        );
1087    }
1088
1089    #[test]
1090    fn manifest_client_rejects_unknown_field() {
1091        // `deny_unknown_fields` on ClientHint catches a fat-fingered
1092        // `displayname:` instead of silently dropping it.
1093        let yaml = r#"
1094id: j
1095version: 1.0.0
1096execute:
1097  shell: powershell
1098  script: "echo x"
1099  timeout: 30s
1100client:
1101  name: "A job"
1102  category: catalog
1103  displayname: oops
1104"#;
1105        let r: Result<Manifest, _> = serde_yaml::from_str(yaml);
1106        assert!(
1107            r.is_err(),
1108            "unknown client field must be a parse error, got {r:?}"
1109        );
1110    }
1111
1112    fn execute_with(
1113        script: Option<&str>,
1114        script_file: Option<&str>,
1115        script_object: Option<&str>,
1116    ) -> Execute {
1117        Execute {
1118            shell: ExecuteShell::Powershell,
1119            script: script.map(str::to_owned),
1120            script_file: script_file.map(str::to_owned),
1121            script_object: script_object.map(str::to_owned),
1122            timeout: "30s".into(),
1123            run_as: RunAs::default(),
1124            cwd: None,
1125        }
1126    }
1127
1128    #[test]
1129    fn validate_accepts_inline_script() {
1130        let e = execute_with(Some("echo hi"), None, None);
1131        assert!(e.validate_script_source().is_ok());
1132    }
1133
1134    #[test]
1135    fn validate_accepts_script_file_alone() {
1136        let e = execute_with(None, Some("scripts/cleanup.ps1"), None);
1137        assert!(e.validate_script_source().is_ok());
1138    }
1139
1140    #[test]
1141    fn validate_accepts_script_object_alone() {
1142        let e = execute_with(None, None, Some("cleanup/1.0.0"));
1143        assert!(e.validate_script_source().is_ok());
1144    }
1145
1146    #[test]
1147    fn validate_treats_empty_inline_script_as_unset() {
1148        // `script: ""` + `script_object` set is the natural shape
1149        // when an operator comments out the YAML block-scalar body
1150        // but leaves the key. Should pass.
1151        let e = execute_with(Some(""), None, Some("cleanup/1.0.0"));
1152        assert!(e.validate_script_source().is_ok());
1153    }
1154
1155    #[test]
1156    fn validate_rejects_zero_sources() {
1157        let e = execute_with(None, None, None);
1158        let err = e.validate_script_source().unwrap_err();
1159        assert!(err.contains("must be set"), "got: {err}");
1160    }
1161
1162    #[test]
1163    fn validate_rejects_empty_inline_only() {
1164        let e = execute_with(Some(""), None, None);
1165        let err = e.validate_script_source().unwrap_err();
1166        assert!(err.contains("must be set"), "got: {err}");
1167    }
1168
1169    #[test]
1170    fn validate_rejects_inline_plus_file() {
1171        let e = execute_with(Some("echo hi"), Some("scripts/cleanup.ps1"), None);
1172        let err = e.validate_script_source().unwrap_err();
1173        assert!(err.contains("only one of"), "got: {err}");
1174    }
1175
1176    #[test]
1177    fn validate_rejects_inline_plus_object() {
1178        let e = execute_with(Some("echo hi"), None, Some("cleanup/1.0.0"));
1179        let err = e.validate_script_source().unwrap_err();
1180        assert!(err.contains("only one of"), "got: {err}");
1181    }
1182
1183    #[test]
1184    fn validate_rejects_file_plus_object() {
1185        let e = execute_with(None, Some("scripts/cleanup.ps1"), Some("cleanup/1.0.0"));
1186        let err = e.validate_script_source().unwrap_err();
1187        assert!(err.contains("only one of"), "got: {err}");
1188    }
1189
1190    #[test]
1191    fn validate_rejects_all_three() {
1192        let e = execute_with(
1193            Some("echo hi"),
1194            Some("scripts/cleanup.ps1"),
1195            Some("cleanup/1.0.0"),
1196        );
1197        let err = e.validate_script_source().unwrap_err();
1198        assert!(err.contains("only one of"), "got: {err}");
1199    }
1200
1201    #[test]
1202    fn manifest_deserialises_script_object_yaml() {
1203        // SPEC §2.4.1 example shape with the Object Store
1204        // reference picked over inline.
1205        let yaml = r#"
1206id: cleanup-disk-temp
1207version: 1.0.1
1208execute:
1209  shell: powershell
1210  script_object: cleanup-disk-temp/1.0.1
1211  timeout: 600s
1212"#;
1213        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
1214        assert_eq!(
1215            m.execute.script_object.as_deref(),
1216            Some("cleanup-disk-temp/1.0.1")
1217        );
1218        assert!(m.execute.script.is_none());
1219        m.validate()
1220            .expect("script_object-only manifest passes validation");
1221    }
1222
1223    #[test]
1224    fn manifest_rejects_typo_in_script_field_name() {
1225        // `deny_unknown_fields` on Execute catches `script_objectt`
1226        // and similar fat-fingers at parse time instead of letting
1227        // them silently fall through to "all three unset".
1228        let yaml = r#"
1229id: typo
1230version: 1.0.0
1231execute:
1232  shell: powershell
1233  script_objectt: oops
1234  timeout: 30s
1235"#;
1236        let r: Result<Manifest, _> = serde_yaml::from_str(yaml);
1237        assert!(r.is_err(), "expected parse error, got {r:?}");
1238    }
1239
1240    #[test]
1241    fn schedule_carries_target_and_rollout() {
1242        let yaml = r#"
1243id: hourly-cleanup-canary
1244when:
1245  per_pc: { every: 1h }
1246job_id: cleanup
1247enabled: true
1248target:
1249  groups: [canary, wave1]
1250jitter: 30s
1251rollout:
1252  strategy: wave
1253  waves:
1254    - { group: canary, delay: 0s }
1255    - { group: wave1,  delay: 5s }
1256"#;
1257        let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
1258        assert_eq!(s.id, "hourly-cleanup-canary");
1259        assert_eq!(s.job_id, "cleanup");
1260        assert_eq!(s.plan.target.groups, vec!["canary", "wave1"]);
1261        assert_eq!(s.plan.jitter.as_deref(), Some("30s"));
1262        let rollout = s.plan.rollout.expect("rollout present");
1263        assert_eq!(rollout.waves.len(), 2);
1264        assert_eq!(rollout.waves[0].group, "canary");
1265        assert_eq!(rollout.waves[1].delay, "5s");
1266        assert_eq!(rollout.strategy, RolloutStrategy::Wave);
1267    }
1268
1269    #[test]
1270    fn schedule_minimal_target_all() {
1271        let yaml = r#"
1272id: kitting
1273when:
1274  per_pc: once
1275enabled: true
1276job_id: scheduled-echo
1277target: { all: true }
1278"#;
1279        let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
1280        assert_eq!(s.id, "kitting");
1281        assert_eq!(s.when, When::PerPc(PerPolicy::Once(OnceLiteral::Once)));
1282        assert!(s.enabled);
1283        assert_eq!(s.job_id, "scheduled-echo");
1284        assert!(s.plan.target.all);
1285        assert!(s.plan.rollout.is_none());
1286        assert!(s.plan.jitter.is_none());
1287        assert!(s.active.is_empty());
1288    }
1289
1290    #[test]
1291    fn schedule_enabled_defaults_to_true() {
1292        let yaml = r#"
1293id: x
1294when:
1295  per_pc: once
1296job_id: y
1297target: { all: true }
1298"#;
1299        let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
1300        assert!(s.enabled);
1301    }
1302
1303    // ---- `when` parsing (#418 Phase 1) ----
1304
1305    fn schedule_yaml_with(when_block: &str) -> String {
1306        format!(
1307            r#"
1308id: x
1309when:
1310{when_block}
1311job_id: y
1312target: {{ all: true }}
1313"#
1314        )
1315    }
1316
1317    #[test]
1318    fn when_per_pc_every_parses_unquoted_humantime() {
1319        // `6h` is digit-led but non-numeric → YAML string, same as
1320        // the old `cooldown: 6h` convention. No quotes needed.
1321        let s: Schedule =
1322            serde_yaml::from_str(&schedule_yaml_with("  per_pc: { every: 6h }")).expect("parse");
1323        assert_eq!(
1324            s.when,
1325            When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() }))
1326        );
1327    }
1328
1329    #[test]
1330    fn when_per_target_every_parses() {
1331        let s: Schedule = serde_yaml::from_str(&schedule_yaml_with("  per_target: { every: 24h }"))
1332            .expect("parse");
1333        assert_eq!(
1334            s.when,
1335            When::PerTarget(PerPolicy::Every(EverySpec {
1336                every: "24h".into()
1337            }))
1338        );
1339    }
1340
1341    #[test]
1342    fn when_per_target_once_parses() {
1343        // Falls out of the shared PerPolicy shape and decide_fire
1344        // already implements it ("any one pc succeeds → skip the
1345        // target forever"), so it is allowed, not rejected.
1346        let s: Schedule =
1347            serde_yaml::from_str(&schedule_yaml_with("  per_target: once")).expect("parse");
1348        assert_eq!(s.when, When::PerTarget(PerPolicy::Once(OnceLiteral::Once)));
1349    }
1350
1351    #[test]
1352    fn when_calendar_time_parses() {
1353        let s: Schedule = serde_yaml::from_str(&schedule_yaml_with(
1354            "  calendar:\n    at: \"09:00\"\n    days: [mon-fri]",
1355        ))
1356        .expect("parse");
1357        match &s.when {
1358            When::Calendar(c) => {
1359                assert_eq!(c.at, "09:00");
1360                assert_eq!(c.days, vec!["mon-fri"]);
1361            }
1362            other => panic!("expected calendar, got {other:?}"),
1363        }
1364    }
1365
1366    #[test]
1367    fn when_calendar_days_default_empty() {
1368        let s: Schedule =
1369            serde_yaml::from_str(&schedule_yaml_with("  calendar:\n    at: \"09:00\""))
1370                .expect("parse");
1371        match &s.when {
1372            When::Calendar(c) => assert!(c.days.is_empty(), "days defaults to empty (= daily)"),
1373            other => panic!("expected calendar, got {other:?}"),
1374        }
1375    }
1376
1377    #[test]
1378    fn when_calendar_datetime_parses_all_separators() {
1379        // one-shot: date+time in hyphen / ISO-T / slash forms
1380        for at in ["2026-06-10 09:00", "2026-06-10T09:00", "2026/06/10 09:00"] {
1381            let block = format!("  calendar:\n    at: \"{at}\"");
1382            let s: Schedule = serde_yaml::from_str(&schedule_yaml_with(&block))
1383                .unwrap_or_else(|e| panic!("parse '{at}': {e}"));
1384            match &s.when {
1385                When::Calendar(c) => {
1386                    use chrono::Datelike;
1387                    let p = c.parse_at().expect("parse_at");
1388                    let d = p.date.expect("datetime at carries a date");
1389                    assert_eq!((d.year(), d.month(), d.day()), (2026, 6, 10), "for '{at}'");
1390                }
1391                other => panic!("expected calendar, got {other:?}"),
1392            }
1393        }
1394    }
1395
1396    #[test]
1397    fn when_rejects_bad_once_keyword() {
1398        // `onec` must be a parse error, not a silently-absorbed
1399        // string (OnceLiteral is a single-variant enum for exactly
1400        // this reason).
1401        let r: Result<Schedule, _> = serde_yaml::from_str(&schedule_yaml_with("  per_pc: onec"));
1402        assert!(r.is_err(), "expected parse error, got {r:?}");
1403    }
1404
1405    #[test]
1406    fn when_rejects_unknown_key_in_every() {
1407        // EverySpec is deny_unknown_fields so `evry:` typos fail
1408        // even under the untagged PerPolicy.
1409        let r: Result<Schedule, _> =
1410            serde_yaml::from_str(&schedule_yaml_with("  per_pc: { evry: 6h }"));
1411        assert!(r.is_err(), "expected parse error, got {r:?}");
1412    }
1413
1414    #[test]
1415    fn when_rejects_unknown_variant() {
1416        let r: Result<Schedule, _> =
1417            serde_yaml::from_str(&schedule_yaml_with("  per_galaxy: once"));
1418        assert!(r.is_err(), "expected parse error, got {r:?}");
1419    }
1420
1421    #[test]
1422    fn when_rejects_old_top_level_cron_field() {
1423        // Pre-#418 shape: top-level `cron:` + no `when:`. Must fail
1424        // loudly (missing `when`), which is what turns stale KV
1425        // blobs into warn-skips after the upgrade.
1426        let yaml = r#"
1427id: x
1428cron: "* * * * * *"
1429job_id: y
1430target: { all: true }
1431"#;
1432        let r: Result<Schedule, _> = serde_yaml::from_str(yaml);
1433        assert!(r.is_err(), "expected parse error, got {r:?}");
1434    }
1435
1436    #[test]
1437    fn when_rejects_retired_cron_escape_hatch() {
1438        // #418 Phase 2 retired `when: { cron: "..." }`. A raw cron
1439        // is now an unknown variant → parse error (operators use the
1440        // calendar form instead).
1441        let r: Result<Schedule, _> =
1442            serde_yaml::from_str(&schedule_yaml_with("  cron: \"0 0 9 * * mon-fri\""));
1443        assert!(
1444            r.is_err(),
1445            "expected parse error for retired cron, got {r:?}"
1446        );
1447    }
1448
1449    #[test]
1450    fn when_round_trips_json_and_yaml() {
1451        // Round-trip through the full Schedule: that is the wire
1452        // unit for both stores (JSON catalog KV + YAML mirror), and
1453        // it exercises the singleton_map field attribute that keeps
1454        // serde_yaml on the map shape instead of `!per_pc` tags.
1455        for when in [
1456            When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1457            When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
1458            When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
1459            When::PerTarget(PerPolicy::Every(EverySpec {
1460                every: "24h".into(),
1461            })),
1462            calendar("09:00", &["mon-fri"]),
1463            calendar("2026-06-10 09:00", &[]),
1464        ] {
1465            let s = schedule_with(when.clone(), RunsOn::Backend);
1466
1467            let json = serde_json::to_string(&s).expect("json serialise");
1468            let back: Schedule = serde_json::from_str(&json).expect("json deserialise");
1469            assert_eq!(back.when, when, "json round-trip for {when}");
1470
1471            let yaml = serde_yaml::to_string(&s).expect("yaml serialise");
1472            assert!(
1473                !yaml.contains('!'),
1474                "yaml must use the map shape, not tags: {yaml}"
1475            );
1476            let back: Schedule = serde_yaml::from_str(&yaml).expect("yaml deserialise");
1477            assert_eq!(back.when, when, "yaml round-trip for {when}");
1478        }
1479    }
1480
1481    #[test]
1482    fn when_once_serialises_as_bare_keyword() {
1483        // The wire shape operators see in the YAML mirror must stay
1484        // the ergonomic `per_pc: once`, not a one-variant map.
1485        let json = serde_json::to_value(When::PerPc(PerPolicy::Once(OnceLiteral::Once)))
1486            .expect("serialise");
1487        assert_eq!(json, serde_json::json!({ "per_pc": "once" }));
1488    }
1489
1490    #[test]
1491    fn when_displays_operator_summary() {
1492        for (when, expected) in [
1493            (
1494                When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1495                "per_pc once",
1496            ),
1497            (
1498                When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
1499                "per_pc every 6h",
1500            ),
1501            (
1502                When::PerTarget(PerPolicy::Every(EverySpec {
1503                    every: "24h".into(),
1504                })),
1505                "per_target every 24h",
1506            ),
1507            (calendar("09:00", &["mon-fri"]), "at 09:00 [mon-fri]"),
1508            (calendar("2026-06-10 09:00", &[]), "at 2026-06-10 09:00"),
1509        ] {
1510            assert_eq!(when.to_string(), expected);
1511        }
1512    }
1513
1514    // ---- lowering (#418: when → engine vocabulary) ----
1515
1516    fn schedule_with(when: When, runs_on: RunsOn) -> Schedule {
1517        Schedule {
1518            id: "x".into(),
1519            when,
1520            job_id: "y".into(),
1521            plan: FanoutPlan::default(),
1522            active: Active::default(),
1523            constraints: Constraints::default(),
1524            tz: ScheduleTz::default(),
1525            starting_deadline: None,
1526            runs_on,
1527            enabled: true,
1528        }
1529    }
1530
1531    fn calendar(at: &str, days: &[&str]) -> When {
1532        When::Calendar(CalendarSpec {
1533            at: at.into(),
1534            days: days.iter().map(|d| (*d).to_string()).collect(),
1535        })
1536    }
1537
1538    #[test]
1539    fn lowering_matches_the_418_table() {
1540        let cases = [
1541            (
1542                When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1543                (POLL_CRON, ExecMode::OncePerPc, None),
1544            ),
1545            (
1546                When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
1547                (POLL_CRON, ExecMode::OncePerPc, Some("6h")),
1548            ),
1549            (
1550                When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
1551                (POLL_CRON, ExecMode::OncePerTarget, None),
1552            ),
1553            (
1554                When::PerTarget(PerPolicy::Every(EverySpec {
1555                    every: "24h".into(),
1556                })),
1557                (POLL_CRON, ExecMode::OncePerTarget, Some("24h")),
1558            ),
1559            // calendar repeating → 6-field cron
1560            (
1561                calendar("09:00", &["mon-fri"]),
1562                ("0 0 9 * * mon-fri", ExecMode::EveryTick, None),
1563            ),
1564            // calendar daily (no days) → DOW *
1565            (
1566                calendar("18:30", &[]),
1567                ("0 30 18 * * *", ExecMode::EveryTick, None),
1568            ),
1569            // calendar one-shot → 7-field year cron
1570            (
1571                calendar("2026-06-10 09:00", &[]),
1572                ("0 0 9 10 6 * 2026", ExecMode::EveryTick, None),
1573            ),
1574        ];
1575        for (when, (cron, mode, cooldown)) in cases {
1576            let l = schedule_with(when.clone(), RunsOn::Backend).lowered();
1577            assert_eq!(l.cron, cron, "cron for {when}");
1578            assert_eq!(l.mode, mode, "mode for {when}");
1579            assert_eq!(l.cooldown.as_deref(), cooldown, "cooldown for {when}");
1580        }
1581    }
1582
1583    #[test]
1584    fn lowered_carries_schedule_tz() {
1585        for (tz, want) in [
1586            (ScheduleTz::Local, ScheduleTz::Local),
1587            (ScheduleTz::Utc, ScheduleTz::Utc),
1588        ] {
1589            let mut s = schedule_with(calendar("09:00", &["mon-fri"]), RunsOn::Backend);
1590            s.tz = tz;
1591            assert_eq!(s.lowered().tz, want, "calendar carries tz");
1592            // reconcile shapes carry tz too (for the active-window check)
1593            let mut s = schedule_with(
1594                When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1595                RunsOn::Backend,
1596            );
1597            s.tz = tz;
1598            assert_eq!(s.lowered().tz, want, "reconcile carries tz");
1599        }
1600    }
1601
1602    #[test]
1603    fn poll_cron_is_accepted_by_the_engine_parser() {
1604        // POLL_CRON is system-generated — if the engine's parser
1605        // ever rejected it every reconcile schedule would die at
1606        // register time. Validate it with the same croner config
1607        // (Seconds::Required, dom_and_dow, year optional).
1608        croner::parser::CronParser::builder()
1609            .seconds(croner::parser::Seconds::Required)
1610            .dom_and_dow(true)
1611            .build()
1612            .parse(POLL_CRON)
1613            .expect("POLL_CRON must parse");
1614    }
1615
1616    // ---- Schedule::validate() (#418 decision F) ----
1617
1618    #[test]
1619    fn validate_accepts_reconcile_shapes() {
1620        for when in [
1621            When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1622            When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
1623            When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
1624            When::PerTarget(PerPolicy::Every(EverySpec {
1625                every: "24h".into(),
1626            })),
1627        ] {
1628            schedule_with(when.clone(), RunsOn::Backend)
1629                .validate()
1630                .unwrap_or_else(|e| panic!("{when} should validate: {e}"));
1631        }
1632    }
1633
1634    #[test]
1635    fn validate_accepts_per_pc_on_agent() {
1636        schedule_with(
1637            When::PerPc(PerPolicy::Every(EverySpec { every: "1h".into() })),
1638            RunsOn::Agent,
1639        )
1640        .validate()
1641        .expect("per_pc + agent is the offline-inventory shape");
1642    }
1643
1644    #[test]
1645    fn validate_rejects_per_target_on_agent() {
1646        let err = schedule_with(
1647            When::PerTarget(PerPolicy::Every(EverySpec {
1648                every: "24h".into(),
1649            })),
1650            RunsOn::Agent,
1651        )
1652        .validate()
1653        .unwrap_err();
1654        assert!(err.contains("per_target"), "got: {err}");
1655        assert!(err.contains("runs_on: agent"), "got: {err}");
1656
1657        // per_target: once is also backend-only.
1658        let err = schedule_with(
1659            When::PerTarget(PerPolicy::Once(OnceLiteral::Once)),
1660            RunsOn::Agent,
1661        )
1662        .validate()
1663        .unwrap_err();
1664        assert!(err.contains("per_target"), "got (once): {err}");
1665        assert!(err.contains("runs_on: agent"), "got (once): {err}");
1666    }
1667
1668    #[test]
1669    fn validate_rejects_bad_every_duration() {
1670        let err = schedule_with(
1671            When::PerPc(PerPolicy::Every(EverySpec { every: "6x".into() })),
1672            RunsOn::Backend,
1673        )
1674        .validate()
1675        .unwrap_err();
1676        assert!(err.contains("when.every"), "got: {err}");
1677    }
1678
1679    #[test]
1680    fn validate_rejects_bad_jitter_and_starting_deadline() {
1681        let mut s = schedule_with(
1682            When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1683            RunsOn::Backend,
1684        );
1685        s.plan.jitter = Some("5x".into());
1686        let err = s.validate().unwrap_err();
1687        assert!(err.contains("jitter"), "got: {err}");
1688
1689        let mut s = schedule_with(
1690            When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1691            RunsOn::Backend,
1692        );
1693        s.starting_deadline = Some("soon".into());
1694        let err = s.validate().unwrap_err();
1695        assert!(err.contains("starting_deadline"), "got: {err}");
1696    }
1697
1698    #[test]
1699    fn validate_accepts_calendar_shapes() {
1700        for when in [
1701            calendar("09:00", &["mon-fri"]),   // weekday morning
1702            calendar("00:00", &["sun"]),       // weekly
1703            calendar("18:30", &[]),            // daily
1704            calendar("2026-06-10 09:00", &[]), // one-shot
1705            calendar("2026/12/25 00:00", &[]), // one-shot, slash form
1706        ] {
1707            schedule_with(when.clone(), RunsOn::Backend)
1708                .validate()
1709                .unwrap_or_else(|e| panic!("{when} should validate: {e}"));
1710        }
1711    }
1712
1713    #[test]
1714    fn validate_rejects_bad_at() {
1715        for bad in ["25:00", "09:60", "9", "noon", "2026-13-01 09:00"] {
1716            let err = schedule_with(calendar(bad, &[]), RunsOn::Backend)
1717                .validate()
1718                .unwrap_err();
1719            assert!(err.contains("when.at"), "for '{bad}', got: {err}");
1720        }
1721    }
1722
1723    #[test]
1724    fn validate_rejects_datetime_at_with_days() {
1725        // A dated `at` is a one-shot — pairing it with days is a
1726        // contradiction (the date already pins the day).
1727        let err = schedule_with(calendar("2026-06-10 09:00", &["mon"]), RunsOn::Backend)
1728            .validate()
1729            .unwrap_err();
1730        assert!(
1731            err.contains("one-shot") && err.contains("days"),
1732            "got: {err}"
1733        );
1734    }
1735
1736    #[test]
1737    fn validate_rejects_bad_day_name() {
1738        // A garbage DOW token is caught by the days pre-flight and
1739        // reported against `when.days`, not the confusing
1740        // "when.at lowered to invalid cron" (claude #432 review).
1741        let err = schedule_with(calendar("09:00", &["funday"]), RunsOn::Backend)
1742            .validate()
1743            .unwrap_err();
1744        assert!(err.contains("when.days"), "got: {err}");
1745        assert!(err.contains("funday"), "names the bad token: {err}");
1746        // a degenerate range like `mon-` reports the whole token, not
1747        // a cryptic empty part (claude #432 follow-up)
1748        let err = schedule_with(calendar("09:00", &["mon-"]), RunsOn::Backend)
1749            .validate()
1750            .unwrap_err();
1751        assert!(err.contains("'mon-'"), "names the whole token: {err}");
1752        // valid names / ranges / numeric / * all pass
1753        for ok in [
1754            calendar("09:00", &["mon-fri"]),
1755            calendar("09:00", &["mon", "wed", "sun"]),
1756            calendar("09:00", &["1-5"]),
1757        ] {
1758            schedule_with(ok.clone(), RunsOn::Backend)
1759                .validate()
1760                .unwrap_or_else(|e| panic!("{ok} should validate: {e}"));
1761        }
1762    }
1763
1764    #[test]
1765    fn calendar_oneshot_instant_detects_past() {
1766        use chrono::TimeZone;
1767        // a dated `at` resolves to an absolute instant…
1768        let c = CalendarSpec {
1769            at: "2024-01-01 09:00".into(),
1770            days: vec![],
1771        };
1772        let t = c
1773            .oneshot_instant(ScheduleTz::Utc)
1774            .expect("one-shot instant");
1775        assert_eq!(
1776            t,
1777            chrono::Utc.with_ymd_and_hms(2024, 1, 1, 9, 0, 0).unwrap()
1778        );
1779        assert!(t < chrono::Utc::now(), "2024 is in the past");
1780        // …while a repeating (time-only) calendar has no instant
1781        let rep = CalendarSpec {
1782            at: "09:00".into(),
1783            days: vec!["mon-fri".into()],
1784        };
1785        assert!(rep.oneshot_instant(ScheduleTz::Utc).is_none());
1786    }
1787
1788    fn schedule_with_active(from: Option<&str>, until: Option<&str>) -> Schedule {
1789        let mut s = schedule_with(
1790            When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1791            RunsOn::Backend,
1792        );
1793        s.active = Active {
1794            from: from.map(str::to_owned),
1795            until: until.map(str::to_owned),
1796        };
1797        s
1798    }
1799
1800    #[test]
1801    fn validate_accepts_active_window() {
1802        schedule_with_active(Some("2026-07-01"), Some("2026-08-01T12:00:00+09:00"))
1803            .validate()
1804            .expect("date + rfc3339 bounds should validate");
1805    }
1806
1807    #[test]
1808    fn validate_rejects_unparseable_active_bound() {
1809        let err = schedule_with_active(Some("July 1st"), None)
1810            .validate()
1811            .unwrap_err();
1812        assert!(err.contains("active"), "got: {err}");
1813    }
1814
1815    #[test]
1816    fn validate_rejects_from_not_before_until() {
1817        let err = schedule_with_active(Some("2026-08-01"), Some("2026-07-01"))
1818            .validate()
1819            .unwrap_err();
1820        assert!(err.contains("strictly before"), "got: {err}");
1821
1822        let err = schedule_with_active(Some("2026-07-01"), Some("2026-07-01"))
1823            .validate()
1824            .unwrap_err();
1825        assert!(err.contains("strictly before"), "got: {err}");
1826    }
1827
1828    // ---- Active window semantics ----
1829
1830    #[test]
1831    fn active_window_is_half_open() {
1832        use chrono::TimeZone;
1833        let active = Active {
1834            from: Some("2026-07-01".into()),
1835            until: Some("2026-08-01".into()),
1836        };
1837        // UTC tz so the date bounds are UTC midnight.
1838        let at = |y, m, d, h| chrono::Utc.with_ymd_and_hms(y, m, d, h, 0, 0).unwrap();
1839        let c = |t| active.contains(t, ScheduleTz::Utc);
1840        assert!(!c(at(2026, 6, 30, 23)), "before from");
1841        assert!(c(at(2026, 7, 1, 0)), "at from (inclusive)");
1842        assert!(c(at(2026, 7, 15, 12)), "inside");
1843        assert!(!c(at(2026, 8, 1, 0)), "at until (exclusive)");
1844        assert!(!c(at(2026, 8, 2, 0)), "after until");
1845    }
1846
1847    #[test]
1848    fn active_empty_window_is_always_active() {
1849        assert!(Active::default().contains(chrono::Utc::now(), ScheduleTz::Local));
1850    }
1851
1852    #[test]
1853    fn active_rfc3339_bound_honours_offset_regardless_of_tz() {
1854        use chrono::TimeZone;
1855        let active = Active {
1856            from: Some("2026-07-01T09:00:00+09:00".into()),
1857            until: None,
1858        };
1859        // RFC3339 carries its own offset → tz arg is ignored.
1860        // 09:00 JST = 00:00 UTC.
1861        for tz in [ScheduleTz::Utc, ScheduleTz::Local] {
1862            assert!(
1863                !active.contains(
1864                    chrono::Utc
1865                        .with_ymd_and_hms(2026, 6, 30, 23, 59, 0)
1866                        .unwrap(),
1867                    tz
1868                )
1869            );
1870            assert!(active.contains(
1871                chrono::Utc.with_ymd_and_hms(2026, 7, 1, 0, 0, 0).unwrap(),
1872                tz
1873            ));
1874        }
1875    }
1876
1877    #[test]
1878    fn active_date_bound_respects_tz() {
1879        // A bare `YYYY-MM-DD` bound is midnight *in the schedule's
1880        // tz* (#418 Phase 2). The UTC interpretation is exact and
1881        // host-independent; assert that precisely.
1882        use chrono::TimeZone;
1883        let utc = Active::parse_bound("2026-07-01", ScheduleTz::Utc).expect("utc");
1884        assert_eq!(
1885            utc,
1886            chrono::Utc.with_ymd_and_hms(2026, 7, 1, 0, 0, 0).unwrap()
1887        );
1888
1889        // The local interpretation must equal what chrono::Local
1890        // computes for the same wall-clock midnight — proves the tz
1891        // path is wired to the host zone (the magnitude vs UTC is
1892        // host-dependent, so we compare against Local directly rather
1893        // than hard-coding the JST offset, keeping CI green on UTC
1894        // runners).
1895        let local = Active::parse_bound("2026-07-01", ScheduleTz::Local).expect("local");
1896        let want = chrono::Local
1897            .with_ymd_and_hms(2026, 7, 1, 0, 0, 0)
1898            .single()
1899            .expect("local midnight is unambiguous")
1900            .with_timezone(&chrono::Utc);
1901        assert_eq!(local, want, "date bound resolved in host-local tz");
1902    }
1903
1904    #[test]
1905    fn active_empty_is_skipped_when_serialising() {
1906        let s = schedule_with(
1907            When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1908            RunsOn::Backend,
1909        );
1910        let json = serde_json::to_value(&s).expect("serialise");
1911        assert!(
1912            json.get("active").is_none(),
1913            "empty active must not appear on the wire: {json}"
1914        );
1915    }
1916
1917    // ---- constraints.window (#418 Phase 3) ----
1918
1919    fn with_window(win: &str) -> Schedule {
1920        let mut s = schedule_with(
1921            When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
1922            RunsOn::Backend,
1923        );
1924        s.constraints.window = Some(win.into());
1925        s
1926    }
1927
1928    #[test]
1929    fn constraints_window_parses_and_round_trips() {
1930        let yaml = r#"
1931id: x
1932when:
1933  per_pc: { every: 6h }
1934job_id: y
1935target: { all: true }
1936constraints:
1937  window: "22:00-05:00"
1938"#;
1939        let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
1940        assert_eq!(s.constraints.window.as_deref(), Some("22:00-05:00"));
1941        let back: Schedule =
1942            serde_json::from_str(&serde_json::to_string(&s).expect("ser")).expect("de");
1943        assert_eq!(back.constraints.window.as_deref(), Some("22:00-05:00"));
1944    }
1945
1946    #[test]
1947    fn constraints_empty_is_skipped_when_serialising() {
1948        let s = schedule_with(
1949            When::PerPc(PerPolicy::Once(OnceLiteral::Once)),
1950            RunsOn::Backend,
1951        );
1952        let json = serde_json::to_value(&s).expect("serialise");
1953        assert!(
1954            json.get("constraints").is_none(),
1955            "empty constraints must not appear on the wire: {json}"
1956        );
1957    }
1958
1959    #[test]
1960    fn window_no_constraint_always_allows() {
1961        let c = Constraints::default();
1962        assert!(c.allows(chrono::Utc::now(), ScheduleTz::Local));
1963    }
1964
1965    #[test]
1966    fn window_same_day_is_half_open() {
1967        use chrono::TimeZone;
1968        let s = with_window("09:00-17:00");
1969        let at = |h, m| chrono::Utc.with_ymd_and_hms(2026, 6, 9, h, m, 0).unwrap();
1970        let a = |t| s.constraints.allows(t, ScheduleTz::Utc);
1971        assert!(!a(at(8, 59)), "before start");
1972        assert!(a(at(9, 0)), "at start (inclusive)");
1973        assert!(a(at(16, 59)), "inside");
1974        assert!(!a(at(17, 0)), "at end (exclusive)");
1975        assert!(!a(at(23, 0)), "after end");
1976    }
1977
1978    #[test]
1979    fn window_crossing_midnight() {
1980        use chrono::TimeZone;
1981        let s = with_window("22:00-05:00");
1982        let at = |h, m| chrono::Utc.with_ymd_and_hms(2026, 6, 9, h, m, 0).unwrap();
1983        let a = |t| s.constraints.allows(t, ScheduleTz::Utc);
1984        assert!(a(at(22, 0)), "at start tonight");
1985        assert!(a(at(23, 30)), "late tonight");
1986        assert!(a(at(3, 0)), "early tomorrow");
1987        assert!(!a(at(5, 0)), "at end (exclusive)");
1988        assert!(!a(at(12, 0)), "midday outside");
1989        assert!(!a(at(21, 59)), "just before start");
1990    }
1991
1992    #[test]
1993    fn window_respects_tz() {
1994        // The same instant is inside the window under one tz and may
1995        // be outside under another. Compare UTC vs Local via the
1996        // host's own offset (kept CI-green on UTC runners like the
1997        // active tz test does).
1998        use chrono::TimeZone;
1999        let s = with_window("09:00-17:00");
2000        let noon_utc = chrono::Utc.with_ymd_and_hms(2026, 6, 9, 12, 0, 0).unwrap();
2001        // Under UTC, 12:00 is inside 09:00-17:00.
2002        assert!(s.constraints.allows(noon_utc, ScheduleTz::Utc));
2003        // Under Local, the verdict tracks the host wall-clock time;
2004        // assert it matches a direct wall_time membership check.
2005        let local_t = noon_utc.with_timezone(&chrono::Local).time();
2006        let in_local = local_t >= chrono::NaiveTime::from_hms_opt(9, 0, 0).unwrap()
2007            && local_t < chrono::NaiveTime::from_hms_opt(17, 0, 0).unwrap();
2008        assert_eq!(s.constraints.allows(noon_utc, ScheduleTz::Local), in_local);
2009    }
2010
2011    #[test]
2012    fn validate_accepts_good_window() {
2013        for w in ["09:00-17:00", "22:00-05:00", "00:00-23:59"] {
2014            with_window(w)
2015                .validate()
2016                .unwrap_or_else(|e| panic!("'{w}' should validate: {e}"));
2017        }
2018    }
2019
2020    #[test]
2021    fn validate_rejects_bad_window() {
2022        for bad in ["9-5", "22:00", "22:00-22:00", "25:00-05:00", "09:00_17:00"] {
2023            let err = with_window(bad).validate().unwrap_err();
2024            assert!(
2025                err.contains("constraints.window"),
2026                "for '{bad}', got: {err}"
2027            );
2028        }
2029    }
2030
2031    #[test]
2032    fn window_fail_closed_on_corrupt_blob() {
2033        // A malformed window (only reachable via a hand-edited KV
2034        // blob — validate() rejects it at create) must BLOCK, not
2035        // silently allow fires during a change-freeze (gemini #452).
2036        let s = with_window("22:00_05:00");
2037        assert!(
2038            !s.constraints.allows(chrono::Utc::now(), ScheduleTz::Utc),
2039            "corrupt window fails closed"
2040        );
2041        // …and the scheduler can surface why it's stuck.
2042        assert!(
2043            s.bad_window().is_some(),
2044            "bad_window reports the parse error"
2045        );
2046        assert!(with_window("22:00-05:00").bad_window().is_none());
2047    }
2048
2049    #[test]
2050    fn calendar_outside_window_is_flagged() {
2051        // at 09:00 can never fall in 22:00-05:00 → never fires.
2052        let mut s = schedule_with(calendar("09:00", &["mon-fri"]), RunsOn::Backend);
2053        s.constraints.window = Some("22:00-05:00".into());
2054        assert!(s.calendar_outside_window(), "09:00 is not in 22:00-05:00");
2055
2056        // at 23:00 IS inside the overnight window → fine.
2057        let mut s = schedule_with(calendar("23:00", &[]), RunsOn::Backend);
2058        s.constraints.window = Some("22:00-05:00".into());
2059        assert!(!s.calendar_outside_window(), "23:00 is in 22:00-05:00");
2060
2061        // reconcile shapes are never flagged (they poll every minute).
2062        let mut s = schedule_with(
2063            When::PerPc(PerPolicy::Every(EverySpec { every: "6h".into() })),
2064            RunsOn::Backend,
2065        );
2066        s.constraints.window = Some("22:00-05:00".into());
2067        assert!(!s.calendar_outside_window(), "reconcile is unaffected");
2068
2069        // no window → never flagged.
2070        let s = schedule_with(calendar("09:00", &[]), RunsOn::Backend);
2071        assert!(!s.calendar_outside_window());
2072    }
2073
2074    #[test]
2075    fn shipped_schedule_configs_parse_and_validate() {
2076        // Every YAML under configs/schedules/ must parse with the
2077        // current Schedule serde AND pass validate() — keeps the
2078        // shipped examples from drifting out of sync with the model
2079        // (#418 removed back-compat, so drift = broken at create).
2080        let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../configs/schedules");
2081        let mut seen = 0;
2082        for entry in std::fs::read_dir(&dir).expect("read configs/schedules") {
2083            let path = entry.expect("dir entry").path();
2084            if path.extension().and_then(|e| e.to_str()) != Some("yaml") {
2085                continue;
2086            }
2087            let body = std::fs::read_to_string(&path).expect("read yaml");
2088            let s: Schedule = serde_yaml::from_str(&body)
2089                .unwrap_or_else(|e| panic!("{} failed to parse: {e}", path.display()));
2090            s.validate()
2091                .unwrap_or_else(|e| panic!("{} failed validate(): {e}", path.display()));
2092            seen += 1;
2093        }
2094        assert!(seen > 0, "no schedule YAMLs found in {}", dir.display());
2095    }
2096
2097    // ---- pre-existing enum wire formats (unchanged by #418) ----
2098
2099    #[test]
2100    fn exec_mode_serialises_snake_case() {
2101        for (mode, expected) in [
2102            (ExecMode::EveryTick, "every_tick"),
2103            (ExecMode::OncePerPc, "once_per_pc"),
2104            (ExecMode::OncePerTarget, "once_per_target"),
2105        ] {
2106            let s = serde_json::to_value(mode).expect("serialise");
2107            assert_eq!(s, serde_json::Value::String(expected.into()));
2108            let back: ExecMode = serde_json::from_value(serde_json::Value::String(expected.into()))
2109                .expect("deserialise");
2110            assert_eq!(back, mode, "round-trip for {expected}");
2111        }
2112    }
2113
2114    #[test]
2115    fn schedule_runs_on_defaults_to_backend() {
2116        let yaml = r#"
2117id: x
2118when:
2119  per_pc: once
2120job_id: y
2121target: { all: true }
2122"#;
2123        let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2124        assert_eq!(s.runs_on, RunsOn::Backend);
2125    }
2126
2127    #[test]
2128    fn schedule_runs_on_agent_parses() {
2129        let yaml = r#"
2130id: offline-inv
2131when:
2132  per_pc: { every: 1h }
2133job_id: inventory-hw
2134target: { all: true }
2135runs_on: agent
2136"#;
2137        let s: Schedule = serde_yaml::from_str(yaml).expect("parse");
2138        assert_eq!(s.runs_on, RunsOn::Agent);
2139        assert_eq!(s.lowered().mode, ExecMode::OncePerPc);
2140    }
2141
2142    #[test]
2143    fn runs_on_serialises_snake_case() {
2144        for (mode, expected) in [(RunsOn::Backend, "backend"), (RunsOn::Agent, "agent")] {
2145            let s = serde_json::to_value(mode).expect("serialise");
2146            assert_eq!(s, serde_json::Value::String(expected.into()));
2147            let back: RunsOn = serde_json::from_value(serde_json::Value::String(expected.into()))
2148                .expect("deserialise");
2149            assert_eq!(back, mode);
2150        }
2151    }
2152
2153    #[test]
2154    fn execute_shell_into_wire_shell() {
2155        assert_eq!(Shell::from(ExecuteShell::Powershell), Shell::Powershell);
2156        assert_eq!(Shell::from(ExecuteShell::Cmd), Shell::Cmd);
2157    }
2158
2159    #[test]
2160    fn manifest_staleness_defaults_to_cached() {
2161        let yaml = r#"
2162id: x
2163version: 1.0.0
2164execute:
2165  shell: powershell
2166  script: "echo"
2167  timeout: 1s
2168"#;
2169        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2170        assert_eq!(m.staleness, Staleness::Cached);
2171    }
2172
2173    #[test]
2174    fn manifest_strict_staleness_parses() {
2175        let yaml = r#"
2176id: urgent-patch
2177version: 2.5.1
2178execute:
2179  shell: powershell
2180  script: Install-Hotfix
2181  timeout: 5m
2182staleness:
2183  mode: strict
2184  max_cache_age: 0s
2185"#;
2186        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2187        match m.staleness {
2188            Staleness::Strict { max_cache_age } => assert_eq!(max_cache_age, "0s"),
2189            other => panic!("expected strict, got {other:?}"),
2190        }
2191    }
2192
2193    #[test]
2194    fn manifest_unchecked_staleness_parses() {
2195        let yaml = r#"
2196id: legacy
2197version: 0.1.0
2198execute:
2199  shell: cmd
2200  script: "echo"
2201  timeout: 1s
2202staleness:
2203  mode: unchecked
2204"#;
2205        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2206        assert_eq!(m.staleness, Staleness::Unchecked);
2207    }
2208
2209    #[test]
2210    fn missing_required_field_errors() {
2211        // `id` missing.
2212        let yaml = r#"
2213version: 1.0.0
2214target: { all: true }
2215execute:
2216  shell: powershell
2217  script: "echo"
2218  timeout: 1s
2219"#;
2220        let r: Result<Manifest, _> = serde_yaml::from_str(yaml);
2221        assert!(r.is_err(), "expected error, got {:?}", r);
2222    }
2223
2224    #[test]
2225    fn display_field_table_kind_round_trips_with_nested_columns() {
2226        // #39: `type: table` + `columns:` on a DisplayField gets
2227        // round-tripped through serde so the SPA receives the
2228        // nested schema verbatim. Nested columns themselves are
2229        // DisplayFields so they can carry `type: bytes` /
2230        // `type: number` for cell formatting.
2231        let yaml = r#"
2232id: inv-hw
2233version: 1.0.0
2234execute:
2235  shell: powershell
2236  script: "echo"
2237  timeout: 60s
2238inventory:
2239  display:
2240    - field: hostname
2241      label: Hostname
2242    - field: disks
2243      label: Disks
2244      type: table
2245      columns:
2246        - field: device_id
2247          label: Drive
2248        - field: size_bytes
2249          label: Size
2250          type: bytes
2251        - field: free_bytes
2252          label: Free
2253          type: bytes
2254        - field: file_system
2255          label: FS
2256"#;
2257        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2258        let inv = m.inventory.as_ref().expect("inventory hint");
2259        let disks = inv
2260            .display
2261            .iter()
2262            .find(|d| d.field == "disks")
2263            .expect("disks display row");
2264        assert_eq!(disks.kind.as_deref(), Some("table"));
2265        let cols = disks.columns.as_ref().expect("table needs columns");
2266        assert_eq!(cols.len(), 4);
2267        assert_eq!(cols[1].field, "size_bytes");
2268        assert_eq!(cols[1].kind.as_deref(), Some("bytes"));
2269    }
2270
2271    #[test]
2272    fn display_field_scalar_kind_keeps_columns_none() {
2273        // Defensive: when type is a scalar (`bytes` / `number` /
2274        // `timestamp`) the `columns` field stays None — the SPA
2275        // uses its presence as the "render nested table" signal,
2276        // so it must not leak in via serde defaults.
2277        let yaml = r#"
2278id: x
2279version: 1.0.0
2280execute:
2281  shell: powershell
2282  script: "echo"
2283  timeout: 5s
2284inventory:
2285  display:
2286    - { field: ram_bytes, label: RAM, type: bytes }
2287"#;
2288        let m: Manifest = serde_yaml::from_str(yaml).expect("parse");
2289        let inv = m.inventory.as_ref().unwrap();
2290        assert!(inv.display[0].columns.is_none());
2291    }
2292}
2293
2294/// Periodic schedule (spec §2.4.3). v0.18.0 carries the fanout plan
2295/// (target + optional rollout + optional jitter) inline; the
2296/// referenced job (`job_id` → [`BUCKET_JOBS`]) supplies only the
2297/// script body. Two schedules of the same job can target different
2298/// groups on different cadences without copying the manifest.
2299///
2300/// #418 Phase 1: the cadence is the single [`When`] field. The old
2301/// `cron` × `mode` × `cooldown` × `auto_disable_when_done` quartet
2302/// is gone (no back-compat — pre-Phase-1 KV blobs fail to parse and
2303/// are warn-skipped; re-`schedule create` to upgrade them). The
2304/// engine underneath is unchanged: [`Schedule::lowered`] maps `when`
2305/// onto the same (cron, ExecMode, cooldown) trio the scheduler and
2306/// `decide_fire` always ran on.
2307#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone)]
2308pub struct Schedule {
2309    pub id: String,
2310    /// When to fire — a reconcile cadence (`per_pc` / `per_target`)
2311    /// or a calendar time trigger (`at` / `days`). See [`When`].
2312    ///
2313    /// `singleton_map`: serde_yaml 0.9 renders externally-tagged
2314    /// enums as `!per_pc` YAML tags by default; this keeps the
2315    /// operator-facing map shape (`when: { per_pc: once }`). JSON
2316    /// output is identical either way, and the schemars schema
2317    /// (external tagging = oneOf of single-key objects) already
2318    /// matches the singleton-map wire shape.
2319    #[serde(with = "serde_yaml::with::singleton_map")]
2320    #[schemars(with = "When")]
2321    pub when: When,
2322    /// Key into [`crate::kv::BUCKET_JOBS`]. Must equal a registered
2323    /// Manifest's `id`.
2324    pub job_id: String,
2325    /// Who + how-to-phase + when-to-stagger. The Manifest doesn't
2326    /// carry these any more — same job + different fanout = different
2327    /// schedule.
2328    #[serde(flatten)]
2329    pub plan: FanoutPlan,
2330    /// Optional validity window. Outside `[from, until)` the
2331    /// schedule is dormant — still registered, still visible, but
2332    /// every tick is skipped (deleted ≠ dormant: a campaign that
2333    /// ended stays inspectable and can be re-armed by editing the
2334    /// window). Checked at tick time on both the backend scheduler
2335    /// and the agent's local scheduler.
2336    #[serde(default, skip_serializing_if = "Active::is_empty")]
2337    pub active: Active,
2338    /// #418 Phase 3: operational constraints gating *when within an
2339    /// active period* a fire may happen. Currently just `window`
2340    /// (a maintenance time-of-day window); future `require`
2341    /// (env gates) and `max_concurrent` land in the same namespace.
2342    /// Evaluated in the schedule's `tz` like the other wall-clock
2343    /// fields. Checked at tick time on both schedulers.
2344    #[serde(default, skip_serializing_if = "Constraints::is_empty")]
2345    pub constraints: Constraints,
2346    /// #418 Phase 2: the timezone this schedule's wall-clock fields
2347    /// are evaluated in — both the calendar `at` firing time AND the
2348    /// `active.{from,until}` window bounds. `local` (default) = the
2349    /// running host's TZ (the agent's for `runs_on: agent`, the
2350    /// backend server's otherwise); `utc` for TZ-independent
2351    /// schedules. Reconcile shapes (`per_pc`/`per_target`) ignore it
2352    /// for firing (poll cron runs every minute regardless) but still
2353    /// honor it for the `active` window.
2354    #[serde(default)]
2355    pub tz: ScheduleTz,
2356    /// v0.22: optional humantime window after a cron tick during
2357    /// which the Command is still considered "live". The scheduler
2358    /// computes `tick_at + starting_deadline` and stamps it onto
2359    /// each Command as `deadline_at`; agents skip Commands they
2360    /// receive after that absolute time. `None` (default) = no
2361    /// deadline, meaning a Command queued in the broker / stream
2362    /// during agent downtime runs whenever the agent reconnects —
2363    /// good for kitting / inventory / cleanup. Set this for
2364    /// time-of-day notifications, lunch reminders, etc., where
2365    /// "fire 3 hours late" would be wrong.
2366    #[serde(default, skip_serializing_if = "Option::is_none")]
2367    pub starting_deadline: Option<String>,
2368    /// v0.23: where does the cron tick happen? `Backend` (default,
2369    /// historical) = backend's scheduler fires Commands via NATS;
2370    /// agents passively receive. `Agent` = each targeted agent runs
2371    /// its own internal cron and fires locally, so the schedule
2372    /// keeps ticking even when the broker is unreachable (laptop on
2373    /// the train, broker maintenance window, full WAN outage). The
2374    /// two locations are mutually exclusive — when `Agent`, the
2375    /// backend scheduler stays out and just keeps the definition in
2376    /// KV for agents to read.
2377    #[serde(default)]
2378    pub runs_on: RunsOn,
2379    #[serde(default = "default_true")]
2380    pub enabled: bool,
2381}
2382
2383/// v0.23 — where the cron tick fires from.
2384#[derive(
2385    Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
2386)]
2387#[serde(rename_all = "snake_case")]
2388pub enum RunsOn {
2389    /// Backend's central scheduler ticks and publishes Commands to
2390    /// NATS. Historical default, what every pre-v0.23 schedule
2391    /// uses. Agent offline ⇒ Command queued in STREAM_EXEC; agent
2392    /// reconnects ⇒ catch-up via [`command_replay`](crate)
2393    /// (see kanade-agent's command_replay module).
2394    #[default]
2395    Backend,
2396    /// Each targeted agent runs the cron tick locally. Survives
2397    /// broker / WAN outages. Best for laptops / mobile devices that
2398    /// roam off the corporate network. Agent must be online for the
2399    /// initial schedule + job-catalog pull, but once cached the
2400    /// agent fires the script standalone.
2401    Agent,
2402}
2403
2404/// Per-pc/per-target dedup semantics for a [`Schedule`] (v0.19).
2405#[derive(
2406    Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
2407)]
2408#[serde(rename_all = "snake_case")]
2409pub enum ExecMode {
2410    /// Fire on every cron tick at the whole target. Historical
2411    /// (pre-v0.19) behavior; no dedup.
2412    #[default]
2413    EveryTick,
2414    /// Fire at each pc until that pc succeeds; then skip it until
2415    /// the optional cooldown elapses (or forever if no cooldown).
2416    /// Use for kitting / first-boot / per-pc compliance checks.
2417    OncePerPc,
2418    /// Fire at the whole target until **any** pc succeeds; then
2419    /// skip the whole target until the optional cooldown elapses
2420    /// (or forever if no cooldown). Use for "one delegate is
2421    /// enough" tasks like license check-in.
2422    OncePerTarget,
2423}
2424
2425/// #418 Phase 1 — the single "when does this fire" axis.
2426///
2427/// Replaces the old `cron` + `mode` + `cooldown` trio whose
2428/// interactions were implicit (cron doubled as both a real
2429/// time-of-day trigger and a reconcile poll period; contradictory
2430/// combinations silently no-opped). Two shapes:
2431///
2432/// * **reconcile** (`per_pc` / `per_target`) — desired-state: "each
2433///   pc (or one delegate) should have run this within `every`".
2434///   The poll period is system-generated ([`POLL_CRON`], every
2435///   minute) and no longer the operator's concern.
2436/// * **calendar** (`{ at, days }`) — a wall-clock time trigger
2437///   (#418 Phase 2, replacing the old raw-cron escape hatch). Fires
2438///   the whole target at the given time, no dedup. `at: "09:00"` +
2439///   `days` repeats; `at: "2026-06-10 09:00"` (a date+time) fires
2440///   exactly once. Evaluated in the schedule's top-level `tz`.
2441#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
2442#[serde(rename_all = "snake_case")]
2443pub enum When {
2444    /// Fire at each targeted pc: `once` (kitting — succeed once,
2445    /// skip forever, forever catching brand-new / re-imaged pcs)
2446    /// or `{ every: <humantime> }` (patrol — re-arm per pc after
2447    /// the interval).
2448    PerPc(PerPolicy),
2449    /// Fire until **any** one pc of the target succeeds, then skip
2450    /// the whole target (`once`) or re-arm after `every`. Needs
2451    /// fleet-wide completion data, so it is backend-only —
2452    /// `runs_on: agent` + `per_target` is rejected by
2453    /// [`Schedule::validate`].
2454    PerTarget(PerPolicy),
2455    /// Calendar time trigger: `{ at: "09:00", days: [mon-fri] }`
2456    /// (repeating) or `{ at: "2026-06-10 09:00" }` (one-shot). Fires
2457    /// the whole target at that wall-clock time in the schedule's
2458    /// `tz` — no dedup, no cooldown.
2459    Calendar(CalendarSpec),
2460}
2461
2462/// Calendar time trigger (#418 Phase 2). `at` is either a time of
2463/// day (`"HH:MM"`, repeating — combine with `days`) or a full
2464/// date+time (`"YYYY-MM-DD HH:MM"`, a one-shot that fires once and
2465/// never again). Evaluated in the schedule's top-level `tz`.
2466#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
2467#[serde(deny_unknown_fields)]
2468pub struct CalendarSpec {
2469    /// `"HH:MM"` (24h) for a repeating trigger, or
2470    /// `"YYYY-MM-DD HH:MM"` (hyphen / slash / `T` separators all
2471    /// accepted) for a one-shot. Parsed lazily —
2472    /// [`Schedule::validate`] rejects garbage at create time.
2473    pub at: String,
2474    /// Day-of-week filter for a time-of-day `at`: `["mon-fri"]`,
2475    /// `["mon","wed","fri"]`, … (passed verbatim to the cron DOW
2476    /// field, so ranges and names both work). Empty = every day.
2477    /// Must be empty when `at` carries a date (the date already
2478    /// pins the day).
2479    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2480    pub days: Vec<String>,
2481}
2482
2483/// Parsed `CalendarSpec.at`: the wall-clock minute/hour, plus the
2484/// date for a one-shot (`None` = repeating time-of-day).
2485struct ParsedAt {
2486    minute: u32,
2487    hour: u32,
2488    date: Option<chrono::NaiveDate>,
2489}
2490
2491impl CalendarSpec {
2492    /// Parse `at`: a date+time (`YYYY-MM-DD HH:MM`, hyphen / slash /
2493    /// `T` separators) is a one-shot; a bare `HH:MM` is repeating.
2494    fn parse_at(&self) -> Result<ParsedAt, String> {
2495        use chrono::Timelike;
2496        let s = self.at.trim();
2497        for fmt in ["%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M", "%Y/%m/%d %H:%M"] {
2498            if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s, fmt) {
2499                return Ok(ParsedAt {
2500                    minute: dt.minute(),
2501                    hour: dt.hour(),
2502                    date: Some(dt.date()),
2503                });
2504            }
2505        }
2506        if let Ok(t) = chrono::NaiveTime::parse_from_str(s, "%H:%M") {
2507            return Ok(ParsedAt {
2508                minute: t.minute(),
2509                hour: t.hour(),
2510                date: None,
2511            });
2512        }
2513        Err(format!(
2514            "when.at: unparseable '{}' (want HH:MM or YYYY-MM-DD HH:MM)",
2515            self.at
2516        ))
2517    }
2518
2519    /// Pre-flight check on the `days` tokens so a bad day name gives
2520    /// a `when.days:`-scoped error instead of croner's confusing
2521    /// "when.at lowered to invalid cron" (claude #432 review). Each
2522    /// token is a day name (`mon`..`sun`), a numeric DOW (`0`..`7`),
2523    /// `*`, or a `-` range of those.
2524    fn validate_days(&self) -> Result<(), String> {
2525        const NAMES: [&str; 7] = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
2526        for tok in &self.days {
2527            // Report the whole token on a malformed range like `mon-`
2528            // (which would otherwise split to a cryptic empty part —
2529            // claude #432 follow-up).
2530            let invalid = |reason: &str| {
2531                Err(format!(
2532                    "when.days: invalid day token '{tok}' ({reason}; \
2533                     want mon..sun, 0-7, a range like mon-fri, or *)"
2534                ))
2535            };
2536            for part in tok.split('-') {
2537                let p = part.trim().to_ascii_lowercase();
2538                if p.is_empty() {
2539                    return invalid("empty range bound");
2540                }
2541                let ok = p == "*"
2542                    || NAMES.contains(&p.as_str())
2543                    || p.parse::<u8>().map(|n| n <= 7).unwrap_or(false);
2544                if !ok {
2545                    return invalid(&format!("'{part}' is not a day"));
2546                }
2547            }
2548        }
2549        Ok(())
2550    }
2551
2552    /// For a one-shot (`at` carries a date), the absolute instant it
2553    /// fires in `tz`. `None` for a repeating calendar. Used to warn
2554    /// about a one-shot whose date is already in the past (it would
2555    /// never fire).
2556    pub fn oneshot_instant(&self, tz: ScheduleTz) -> Option<chrono::DateTime<chrono::Utc>> {
2557        let p = self.parse_at().ok()?;
2558        let date = p.date?;
2559        let naive = date.and_hms_opt(p.hour, p.minute, 0)?;
2560        tz.naive_to_utc(naive)
2561    }
2562
2563    /// The wall-clock time-of-day this calendar fires at (`None` if
2564    /// `at` is unparseable — validate() guards that). Used to detect
2565    /// a calendar whose fire time can never fall inside its
2566    /// `constraints.window` (claude #452 review).
2567    pub fn fire_time(&self) -> Option<chrono::NaiveTime> {
2568        let p = self.parse_at().ok()?;
2569        chrono::NaiveTime::from_hms_opt(p.hour, p.minute, 0)
2570    }
2571
2572    /// Lower to the cron string the scheduler engine runs. Repeating
2573    /// → 6-field `0 {min} {hour} * * {dow}`; one-shot → 7-field
2574    /// `0 {min} {hour} {day} {month} * {year}` (a past year never
2575    /// fires — that's what makes it one-shot).
2576    fn to_cron(&self) -> Result<String, String> {
2577        use chrono::Datelike;
2578        let ParsedAt { minute, hour, date } = self.parse_at()?;
2579        match date {
2580            Some(d) => {
2581                if !self.days.is_empty() {
2582                    return Err(
2583                        "when.at with a date is a one-shot and cannot be combined with days".into(),
2584                    );
2585                }
2586                Ok(format!(
2587                    "0 {minute} {hour} {} {} * {}",
2588                    d.day(),
2589                    d.month(),
2590                    d.year()
2591                ))
2592            }
2593            None => {
2594                let dow = if self.days.is_empty() {
2595                    "*".to_string()
2596                } else {
2597                    self.validate_days()?;
2598                    self.days.join(",")
2599                };
2600                Ok(format!("0 {minute} {hour} * * {dow}"))
2601            }
2602        }
2603    }
2604}
2605
2606/// The timezone a schedule's wall-clock fields (`when.at`,
2607/// `active.{from,until}`) are evaluated in (#418 Phase 2).
2608#[derive(
2609    Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq, Default,
2610)]
2611#[serde(rename_all = "snake_case")]
2612pub enum ScheduleTz {
2613    /// The running host's local timezone — the agent's for
2614    /// `runs_on: agent`, the backend server's otherwise. Default.
2615    #[default]
2616    Local,
2617    /// UTC — for timezone-independent schedules.
2618    Utc,
2619}
2620
2621impl ScheduleTz {
2622    /// Interpret a naive (zoneless) datetime as being in this tz and
2623    /// convert to UTC. On a DST *fold* (the local time occurs twice
2624    /// when clocks go back) we pick `.earliest()` rather than
2625    /// rejecting it; `None` is reserved for a true DST *gap* (a local
2626    /// time that never exists). `Utc` is fixed-offset so neither ever
2627    /// happens; `Local` is whatever timezone the running host is set
2628    /// to and *can* hit a gap/fold on any DST-observing host — not
2629    /// just the JST we run today (gemini + claude #432 review).
2630    fn naive_to_utc(self, naive: chrono::NaiveDateTime) -> Option<chrono::DateTime<chrono::Utc>> {
2631        use chrono::TimeZone;
2632        match self {
2633            ScheduleTz::Utc => Some(chrono::DateTime::from_naive_utc_and_offset(
2634                naive,
2635                chrono::Utc,
2636            )),
2637            ScheduleTz::Local => chrono::Local
2638                .from_local_datetime(&naive)
2639                .earliest()
2640                .map(|dt| dt.with_timezone(&chrono::Utc)),
2641        }
2642    }
2643
2644    /// The wall-clock time-of-day `now` reads as in this tz — used by
2645    /// [`Constraints::allows`] to test a maintenance window
2646    /// (#418 Phase 3). `Utc` is the naive UTC time; `Local` is the
2647    /// running host's local time.
2648    fn wall_time(self, now: chrono::DateTime<chrono::Utc>) -> chrono::NaiveTime {
2649        match self {
2650            ScheduleTz::Utc => now.time(),
2651            ScheduleTz::Local => now.with_timezone(&chrono::Local).time(),
2652        }
2653    }
2654}
2655
2656/// `once` vs `{ every: <humantime> }` — shared by `per_pc` /
2657/// `per_target`. Untagged so the YAML stays the bare keyword or a
2658/// one-key map, nothing more ceremonial.
2659#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
2660#[serde(untagged)]
2661pub enum PerPolicy {
2662    /// The bare string `once`: succeed once, then skip permanently
2663    /// (cooldown = infinity).
2664    Once(OnceLiteral),
2665    /// Re-arm after the humantime interval, e.g. `{ every: 6h }`.
2666    Every(EverySpec),
2667}
2668
2669/// Single-variant enum so serde accepts exactly the string `once`
2670/// (a free-form `String` would swallow typos like `onec`).
2671#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Copy, PartialEq, Eq)]
2672#[serde(rename_all = "snake_case")]
2673pub enum OnceLiteral {
2674    Once,
2675}
2676
2677/// `{ every: <humantime> }`. Standalone struct (not an inline
2678/// struct variant) so `deny_unknown_fields` still bites under the
2679/// untagged [`PerPolicy`] — `{ evry: 6h }` is a parse error, not a
2680/// silently-ignored key.
2681#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Eq)]
2682#[serde(deny_unknown_fields)]
2683pub struct EverySpec {
2684    /// Humantime interval (`10m`, `6h`, `1d`...). Parsed lazily —
2685    /// [`Schedule::validate`] rejects garbage at create time.
2686    pub every: String,
2687}
2688
2689impl PerPolicy {
2690    /// The cooldown this policy lowers to: `once` = `None`
2691    /// (permanent skip), `every` = the interval.
2692    fn cooldown(&self) -> Option<String> {
2693        match self {
2694            PerPolicy::Once(_) => None,
2695            PerPolicy::Every(EverySpec { every }) => Some(every.clone()),
2696        }
2697    }
2698}
2699
2700impl std::fmt::Display for When {
2701    /// Operator-facing one-liner (`per_pc once` / `per_pc every 6h`
2702    /// / `at 09:00 [mon-fri]` / `at 2026-06-10 09:00`) for log
2703    /// lines, audit payloads and the API's `ScheduleSummary`.
2704    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2705        let policy = |p: &PerPolicy| match p {
2706            PerPolicy::Once(_) => "once".to_string(),
2707            PerPolicy::Every(EverySpec { every }) => format!("every {every}"),
2708        };
2709        match self {
2710            When::PerPc(p) => write!(f, "per_pc {}", policy(p)),
2711            When::PerTarget(p) => write!(f, "per_target {}", policy(p)),
2712            When::Calendar(c) if c.days.is_empty() => write!(f, "at {}", c.at),
2713            When::Calendar(c) => write!(f, "at {} [{}]", c.at, c.days.join(",")),
2714        }
2715    }
2716}
2717
2718/// Optional validity window for a [`Schedule`] (#418 decision G).
2719/// Half-open `[from, until)`; either bound may be omitted. Bounds
2720/// are `YYYY-MM-DD` (= that day's 00:00 in the schedule's `tz`) or
2721/// full RFC3339 (offset is honored as-is, `tz` ignored). Kept as
2722/// strings so the JSON Schema the SPA editor consumes stays two
2723/// plain string fields, mirroring `jitter` / `starting_deadline`.
2724///
2725/// #418 Phase 2: bounds are evaluated in the schedule's top-level
2726/// `tz` (was UTC-only in Phase 1) so `tz: local` makes both the
2727/// calendar `at` AND the `active` window local — one consistent
2728/// timezone per schedule.
2729#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default, PartialEq, Eq)]
2730#[serde(deny_unknown_fields)]
2731pub struct Active {
2732    /// Dormant before this instant.
2733    #[serde(default, skip_serializing_if = "Option::is_none")]
2734    pub from: Option<String>,
2735    /// Dormant from this instant on (exclusive).
2736    #[serde(default, skip_serializing_if = "Option::is_none")]
2737    pub until: Option<String>,
2738}
2739
2740impl Active {
2741    /// `skip_serializing_if` helper — an empty window means "always
2742    /// active" and is omitted from the wire format entirely.
2743    pub fn is_empty(&self) -> bool {
2744        self.from.is_none() && self.until.is_none()
2745    }
2746
2747    /// Parse one bound: RFC3339 first (offset honored, `tz`
2748    /// ignored), then bare `YYYY-MM-DD` (00:00 in `tz`).
2749    pub fn parse_bound(s: &str, tz: ScheduleTz) -> Result<chrono::DateTime<chrono::Utc>, String> {
2750        if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(s) {
2751            return Ok(dt.with_timezone(&chrono::Utc));
2752        }
2753        if let Ok(d) = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") {
2754            let midnight = d.and_hms_opt(0, 0, 0).expect("00:00:00 is always valid");
2755            return tz.naive_to_utc(midnight).ok_or_else(|| {
2756                format!("active: bound '{s}' falls in a DST gap for the schedule's tz")
2757            });
2758        }
2759        Err(format!(
2760            "active: unparseable bound '{s}' (want YYYY-MM-DD or RFC3339)"
2761        ))
2762    }
2763
2764    /// Is `now` inside the window? Unparseable bounds are treated
2765    /// as absent here (fail-open) — [`Schedule::validate`] is the
2766    /// place that rejects them loudly; this runs on every tick and
2767    /// must never panic on a stale KV blob.
2768    pub fn contains(&self, now: chrono::DateTime<chrono::Utc>, tz: ScheduleTz) -> bool {
2769        let bound = |s: &Option<String>| s.as_deref().and_then(|s| Self::parse_bound(s, tz).ok());
2770        if bound(&self.from).is_some_and(|from| now < from) {
2771            return false;
2772        }
2773        if bound(&self.until).is_some_and(|until| now >= until) {
2774            return false;
2775        }
2776        true
2777    }
2778}
2779
2780/// Operational constraints on a [`Schedule`] (#418 Phase 3). Where
2781/// [`Active`] decides *over what date range* a schedule is live,
2782/// `Constraints` decides *when, within an active period,* a fire is
2783/// allowed. Only `window` (a maintenance time-of-day window) so far;
2784/// `require` (env gates) and `max_concurrent` will join this struct
2785/// in later phases.
2786#[derive(Serialize, Deserialize, schemars::JsonSchema, Debug, Clone, Default, PartialEq, Eq)]
2787#[serde(deny_unknown_fields)]
2788pub struct Constraints {
2789    /// `"HH:MM-HH:MM"` wall-clock window (evaluated in the schedule's
2790    /// `tz`). Fires outside it are skipped — mainly for reconcile
2791    /// cadences ("patrol every 6h, but only fire overnight") and
2792    /// daytime change-freezes. `start > end` crosses midnight
2793    /// (`"22:00-05:00"` = 22:00 through 05:00 next morning). Parsed
2794    /// lazily; [`Schedule::validate`] rejects garbage at create time.
2795    #[serde(default, skip_serializing_if = "Option::is_none")]
2796    pub window: Option<String>,
2797}
2798
2799impl Constraints {
2800    /// `skip_serializing_if` helper — empty constraints are omitted
2801    /// from the wire format entirely.
2802    pub fn is_empty(&self) -> bool {
2803        self.window.is_none()
2804    }
2805
2806    /// Parse `"HH:MM-HH:MM"` into `(start, end)`. Equal bounds are an
2807    /// error (a zero-width or all-day window is ambiguous — write no
2808    /// window for "always").
2809    pub fn parse_window(s: &str) -> Result<(chrono::NaiveTime, chrono::NaiveTime), String> {
2810        let (a, b) = s
2811            .split_once('-')
2812            .ok_or_else(|| format!("constraints.window: '{s}' must be 'HH:MM-HH:MM'"))?;
2813        let parse = |part: &str| {
2814            chrono::NaiveTime::parse_from_str(part.trim(), "%H:%M")
2815                .map_err(|e| format!("constraints.window: invalid time '{}': {e}", part.trim()))
2816        };
2817        let (start, end) = (parse(a)?, parse(b)?);
2818        if start == end {
2819            return Err(format!(
2820                "constraints.window: start and end are equal ('{s}'); omit window for 'always'"
2821            ));
2822        }
2823        Ok((start, end))
2824    }
2825
2826    /// Is a fire allowed at `now` (evaluated in `tz`)? No window =
2827    /// always allowed. Half-open `[start, end)`; `start > end`
2828    /// crosses midnight.
2829    ///
2830    /// **Fail-closed** on an unparseable window (returns `false`,
2831    /// gemini #452 review): a window is a *restrictive* constraint
2832    /// (change-freeze / overnight-only), so a corrupt one must NOT
2833    /// silently allow fires during the restricted hours. Bad windows
2834    /// are rejected at create time by [`Schedule::validate`]; this
2835    /// only bites a hand-edited KV blob, where blocking is the safe
2836    /// direction. The scheduler warns at register time
2837    /// ([`Schedule::bad_window`]) so a stuck schedule is diagnosable.
2838    /// The tick path never panics regardless.
2839    pub fn allows(&self, now: chrono::DateTime<chrono::Utc>, tz: ScheduleTz) -> bool {
2840        match self.window.as_deref() {
2841            // No window → always allowed.
2842            None => true,
2843            // Window set: membership, or fail-closed if unparseable
2844            // (`window_contains` returns None for a corrupt window).
2845            Some(_) => self.window_contains(tz.wall_time(now)).unwrap_or(false),
2846        }
2847    }
2848
2849    /// Membership of a wall-clock time-of-day in the window. `None`
2850    /// when there is no window or it's unparseable (callers decide
2851    /// the failure direction). `start > end` crosses midnight.
2852    fn window_contains(&self, t: chrono::NaiveTime) -> Option<bool> {
2853        let (start, end) = Self::parse_window(self.window.as_deref()?).ok()?;
2854        Some(if start <= end {
2855            start <= t && t < end
2856        } else {
2857            t >= start || t < end
2858        })
2859    }
2860}
2861
2862/// The system-generated poll cadence every reconcile-shaped `when`
2863/// lowers to. Operators never write this: the real inter-run
2864/// spacing is the `every` cooldown; this only bounds "how soon do
2865/// we notice somebody is due" (#418 decision B took the poll
2866/// period away from the operator).
2867pub const POLL_CRON: &str = "0 * * * * *";
2868
2869/// What a [`When`] lowers to — the exact (cron, mode, cooldown)
2870/// trio the pre-#418 engine ran on. Keeping the engine vocabulary
2871/// unchanged is what lets Phase 1 swap the operator surface without
2872/// touching the tick / dedup machinery.
2873pub struct Lowered {
2874    /// Cron handed to `tokio-cron-scheduler` — [`POLL_CRON`] for
2875    /// reconcile shapes, a 6/7-field cron for calendar shapes.
2876    pub cron: String,
2877    /// Dedup semantics for `decide_fire`.
2878    pub mode: ExecMode,
2879    /// Humantime re-arm interval (`None` = succeed once, skip
2880    /// forever).
2881    pub cooldown: Option<String>,
2882    /// Timezone to evaluate `cron` in (#418 Phase 2). The scheduler
2883    /// passes this to `Job::new_async_tz`. Reconcile shapes carry
2884    /// the schedule's tz too even though POLL_CRON is tz-agnostic,
2885    /// so the same value drives the `active`-window check.
2886    pub tz: ScheduleTz,
2887}
2888
2889impl Schedule {
2890    /// The error message if this schedule's `constraints.window` is
2891    /// set but unparseable, else `None`. The scheduler logs this at
2892    /// register time so a fail-closed (never-firing) schedule from a
2893    /// hand-edited KV blob is diagnosable (gemini #452 review).
2894    pub fn bad_window(&self) -> Option<String> {
2895        let w = self.constraints.window.as_deref()?;
2896        Constraints::parse_window(w).err()
2897    }
2898
2899    /// True when this is a `calendar` schedule whose fire time can
2900    /// never fall inside its `constraints.window` — the cron fires,
2901    /// the window check rejects it, and (firing only at that
2902    /// time-of-day) it effectively never runs. An easy misconfig to
2903    /// set up by accident; the scheduler warns at register time
2904    /// (claude #452 review). Reconcile shapes poll every minute, so
2905    /// they always catch the window opening and aren't affected.
2906    pub fn calendar_outside_window(&self) -> bool {
2907        let When::Calendar(c) = &self.when else {
2908            return false;
2909        };
2910        let Some(t) = c.fire_time() else {
2911            return false;
2912        };
2913        matches!(self.constraints.window_contains(t), Some(false))
2914    }
2915
2916    /// Lower the operator-facing `when` onto the engine vocabulary.
2917    /// Single seam shared by the backend scheduler and the agent's
2918    /// local scheduler so the two can never drift.
2919    pub fn lowered(&self) -> Lowered {
2920        let tz = self.tz;
2921        match &self.when {
2922            When::PerPc(p) => Lowered {
2923                cron: POLL_CRON.into(),
2924                mode: ExecMode::OncePerPc,
2925                cooldown: p.cooldown(),
2926                tz,
2927            },
2928            When::PerTarget(p) => Lowered {
2929                cron: POLL_CRON.into(),
2930                mode: ExecMode::OncePerTarget,
2931                cooldown: p.cooldown(),
2932                tz,
2933            },
2934            // `to_cron` only fails on a malformed `at` (rejected by
2935            // validate() at create time). For a hand-edited KV blob
2936            // that slipped past, emit a deliberately-invalid cron so
2937            // register()'s Job::new_async_tz fails → warn+skip,
2938            // rather than firing at the wrong time.
2939            When::Calendar(c) => Lowered {
2940                cron: c
2941                    .to_cron()
2942                    .unwrap_or_else(|_| "# invalid calendar at".into()),
2943                mode: ExecMode::EveryTick,
2944                cooldown: None,
2945                tz,
2946            },
2947        }
2948    }
2949
2950    /// Cross-field semantic checks that don't fit pure serde derive
2951    /// — the [`Manifest::validate`] counterpart (#418 decision F;
2952    /// pre-Phase-1 a broken schedule was accepted at create time
2953    /// and silently warn-skipped at tick time). Run at every create
2954    /// site: `kanade schedule create` (client-side) and
2955    /// `POST /api/schedules`. The job_id-exists check lives in the
2956    /// API handler instead — it needs the JOBS KV.
2957    pub fn validate(&self) -> Result<(), String> {
2958        if matches!(self.runs_on, RunsOn::Agent) && matches!(self.when, When::PerTarget(_)) {
2959            return Err(
2960                "when.per_target needs fleet-wide completion data and is backend-only; \
2961                 it cannot be combined with runs_on: agent (each agent self-schedules, \
2962                 so per-target dedup would be deduping across a target of 1)"
2963                    .into(),
2964            );
2965        }
2966        if let Some(cd) = self.lowered().cooldown.as_deref() {
2967            humantime::parse_duration(cd)
2968                .map_err(|e| format!("when.every: invalid duration '{cd}': {e}"))?;
2969        }
2970        if let When::Calendar(c) = &self.when {
2971            // Lower the calendar form to its cron (catches a bad `at`
2972            // and the date+days conflict), then validate that cron
2973            // with the same parser configuration tokio-cron-scheduler
2974            // 0.15 uses internally (croner, seconds required,
2975            // DOM-and-DOW both honored, year optional) — create-time
2976            // validation can never accept what register() rejects.
2977            let cron = c.to_cron()?;
2978            croner::parser::CronParser::builder()
2979                .seconds(croner::parser::Seconds::Required)
2980                .dom_and_dow(true)
2981                .build()
2982                .parse(&cron)
2983                .map_err(|e| format!("when.at lowered to invalid cron '{cron}': {e}"))?;
2984        }
2985        // The other humantime strings on the schedule (claude #419
2986        // review): runtime degrades gracefully on both (bad jitter →
2987        // silent no-op, bad starting_deadline → warn + skipped tick),
2988        // but "rejected at create time" should cover every field the
2989        // operator can typo, not just `when`.
2990        if let Some(j) = &self.plan.jitter {
2991            humantime::parse_duration(j)
2992                .map_err(|e| format!("jitter: invalid duration '{j}': {e}"))?;
2993        }
2994        if let Some(sd) = &self.starting_deadline {
2995            humantime::parse_duration(sd)
2996                .map_err(|e| format!("starting_deadline: invalid duration '{sd}': {e}"))?;
2997        }
2998        let from = self
2999            .active
3000            .from
3001            .as_deref()
3002            .map(|s| Active::parse_bound(s, self.tz))
3003            .transpose()?;
3004        let until = self
3005            .active
3006            .until
3007            .as_deref()
3008            .map(|s| Active::parse_bound(s, self.tz))
3009            .transpose()?;
3010        if let (Some(f), Some(u)) = (from, until) {
3011            if f >= u {
3012                return Err(format!(
3013                    "active.from ({}) must be strictly before active.until ({})",
3014                    self.active.from.as_deref().unwrap_or_default(),
3015                    self.active.until.as_deref().unwrap_or_default(),
3016                ));
3017            }
3018        }
3019        // #418 Phase 3: a bad maintenance window is rejected at create
3020        // time (parse_window also catches equal bounds).
3021        if let Some(w) = self.constraints.window.as_deref() {
3022            Constraints::parse_window(w)?;
3023        }
3024        Ok(())
3025    }
3026}
3027
3028fn default_true() -> bool {
3029    true
3030}