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