Skip to main content

ignition_core/actions/
deploy.rs

1//! Deploy actions: `ign deploy render` (Phase 21) and the `deploy plan`
2//! engine entry point (Phase 22).
3//!
4//! An OFFLINE verb: it reads a declared repo from disk and returns a model.
5//! There is no `Session`, no profile resolution, no network connection and
6//! no credential read; the envelope's `profile` is null. Render reads the
7//! repo and writes nothing, except into the optional `--out` directory, and
8//! only after the whole render succeeded: a refused render leaves `--out`
9//! untouched. The write goes through [`write_out`]'s clobber ladder, which
10//! never deletes and only replaces its own unmodified prior output.
11//!
12//! The `data` keys, in order: `env, gateway, mode, out, files, bytes,
13//! render_sha256, resources, tag_providers, uuids_minted, opaque_files,
14//! warnings`. Later plans add keys additively (the `workspace.rs`
15//! discipline); `out` is always present and is null without `--out`.
16//!
17//! Errors never echo file contents: findings name repo-relative paths and
18//! key names only.
19//!
20//! ## `deploy plan` (22-01)
21//!
22//! [`load_declared`] is the shared front half of every verb that starts from
23//! a declared repo (render, plan, later pull and verify): it reads
24//! `targets/<env>.yaml`, the gateway manifest and the source tree and
25//! renders them. [`deploy_plan`] then compares that render with a node's
26//! live files, read through a [`TreeReader`]. It opens no gateway session,
27//! sends no REST request, takes no scan-lock and writes nothing: the reader
28//! has no write half to call.
29//!
30//! The [`DeployPlanResult`] `data` keys, in order: `env, gateway, mode,
31//! node, transport, render_sha256, plan_sha256, summary, entries,
32//! delete_set, base, warnings`. Later plans add keys additively.
33//! Entries carry paths and sha256 only, never file bytes.
34//!
35//! ## `deploy pull` (22-08, DPLN-06)
36//!
37//! [`deploy_pull`] bootstraps `gateways/<name>/` from the live master through
38//! the same read-only [`TreeReader`]: it reads, builds a tree that renders
39//! back to the live effective view ([`crate::deploy::pull`]), and only then
40//! writes the local repo. It never writes to the gateway host or the gateway
41//! and resolves no session. Replacing a non-empty `resources/`, `tags/` or
42//! `modes/<env>/` needs `yes` ([`deploy_pull_preview`] names what would be
43//! replaced); `manifest.yaml` and other environments' overlays are never
44//! touched. On unix every removal and write goes through descriptor-relative
45//! calls rooted at the repo (`tree::dirfd`): a directory swapped for a
46//! symlink mid-run is refused, never followed.
47//!
48//! The [`DeployPullResult`] `data` keys, in order: `env, gateway, mode, node,
49//! transport, out, files_written, manifest, render_sha256, skipped,
50//! warnings`.
51//!
52//! ## `deploy verify` (22-10, DPLN-07, DPLN-08, DPLN-09)
53//!
54//! [`deploy_verify`] gathers read-only evidence from the master gateway and
55//! judges it with the pure [`verify::evaluate`] (six checks, fixed order; see
56//! [`crate::deploy::verify`] for the rules). It takes a [`DeployApi`] for the
57//! REST resource, scan and mode reads, a [`GatewayApi`] for the log and the
58//! WebDev tag probes, and a [`TreeReader`] for the live files. It takes no
59//! scan-lock, triggers no scan and writes nothing.
60//!
61//! A red verdict is a normal result: `ok: false`, `slug:
62//! deploy_verify_failed`, and [`DeployVerifyResult::failure_exit_code`]
63//! returns `Some(6)` for the binary to apply after rendering (Decision A).
64//! An unreachable gateway, a refused resource, scan or mode read, and a
65//! transport failure are [`CoreError`]s instead, because there is nothing to
66//! judge. No log text, tag value or file byte reaches the result.
67//!
68//! The [`DeployVerifyResult`] `data` keys, in order: `env, gateway, mode,
69//! node, project, ok, slug, checks, resources_checked, warnings`.
70//!
71//! ## `deploy verify --node` (24-09, DPAIR-01, DPAIR-03, D-12)
72//!
73//! [`deploy_verify_node`] adds the backup of a redundant pair. `master` is
74//! [`deploy_verify`] unchanged; `backup` runs the backup checks only and
75//! `both` runs the master's six and then the backup's. The result then also
76//! carries `backup` (the [`crate::deploy::pair::BackupReport`] shape, with
77//! `budget_s` 0, `waited_s` 0 and `resync` null: verify never waits, never
78//! resyncs and never writes). Both keys are absent for `master`, so its
79//! output is byte-identical to the verb before Phase 24.
80
81use std::collections::{BTreeMap, BTreeSet};
82use std::path::{Path, PathBuf};
83use std::time::Duration;
84
85use serde::Serialize;
86
87use crate::client::logs::{DEFAULT_LOG_LIMIT, LogQuery};
88use crate::client::mode::normalize_deployment_mode;
89use crate::client::query::ListQuery;
90use crate::client::scan::{ScanKind, ScanStatus};
91use crate::client::{DeployApi, GatewayApi};
92use crate::deploy::apply::journal::{journal_on_box, journal_present};
93use crate::deploy::component_problem;
94use crate::deploy::exclude;
95use crate::deploy::layout::{PAIR_UNSYNCED_TAG_TYPES, RelPath, collection_path};
96use crate::deploy::ledger::{Ledger, LedgerRoot, ledger_location};
97use crate::deploy::manifest::Manifest;
98use crate::deploy::pair::{
99    BackupCheck, BackupCheckId, BackupNode, BackupReport, BackupSignals, SecretCheckMode,
100    SecretNamesReport, TickOutcome, check_secret_names, collect_referenced_secrets, compare_files,
101    declared_secret_providers, effective_files, exempt_unreadable, files_check,
102    files_skipped_check, mode_backup_check, read_back_backup_check_with, unreachable_check,
103    unreadable_units, unreadable_warnings, verify_report,
104};
105use crate::deploy::plan::{
106    BaseTree, PlanEntry, PlanStatus, PlanSummary, UnreadableUnit, apply_base,
107    live_tag_folder_findings, pair_mode_tag_dirs, pair_mode_tag_files, pair_tag_keys, plan_with,
108    render_diffs,
109};
110use crate::deploy::pull::{PullInput, REASON_PATH_TOO_LONG, Skipped, generate_manifest, pull};
111use crate::deploy::render::{RenderInput, RenderOutcome, RenderedResource, render};
112use crate::deploy::target::{NodeTarget, Redundancy, Target};
113use crate::deploy::transport::{CORE_COLLECTION, DeleteSet, READ_MAX_ENTRIES, Scope, TreeReader};
114use crate::deploy::tree::{
115    RenderMarker, SourceTree, read_repo_file, require_repo_dir, walk_capped, write_out,
116};
117use crate::deploy::verify::{self, CheckStatus};
118use crate::error::CoreError;
119
120/// The `ign deploy render` result (the envelope's `data`).
121#[derive(Debug, Clone, PartialEq, Serialize)]
122pub struct DeployRenderResult {
123    /// The `--env` value.
124    pub env: String,
125    /// Gateway directory name from the target.
126    pub gateway: String,
127    /// Deployment mode from the target.
128    pub mode: String,
129    /// The `--out` directory as given; null without `--out`.
130    pub out: Option<String>,
131    /// Number of rendered files.
132    pub files: usize,
133    /// Total rendered bytes.
134    pub bytes: usize,
135    /// Lowercase hex SHA-256 of the rendered listing (README documents it).
136    pub render_sha256: String,
137    /// Rendered resources, byte-ordered, singletons first.
138    pub resources: Vec<RenderedResource>,
139    /// Tag providers rendered, byte-ordered.
140    pub tag_providers: Vec<String>,
141    /// Collection paths that received a minted uuid, byte-ordered.
142    pub uuids_minted: Vec<String>,
143    /// Collection paths passed through as opaque bytes, byte-ordered.
144    pub opaque_files: Vec<String>,
145    /// Warnings in pipeline order.
146    pub warnings: Vec<String>,
147}
148
149/// Read and parse `targets/<env>.yaml`: the target file's repo-relative
150/// name and the parsed target.
151pub fn load_target(repo: &Path, env: &str) -> Result<(String, Target), CoreError> {
152    if let Some(problem) = component_problem(env) {
153        return Err(CoreError::InvalidInput {
154            reason: format!("--env {env}: {problem}"),
155        });
156    }
157    let target_rel = format!("targets/{env}.yaml");
158    let target_bytes = read_repo_file(
159        repo,
160        &target_rel,
161        Some(&format!("--env {env} selects {target_rel}")),
162    )?;
163    let target = Target::parse(&target_rel, &target_bytes)?;
164    Ok((target_rel, target))
165}
166
167/// A declared repo, loaded and rendered for one environment.
168#[derive(Debug, Clone)]
169pub struct Declared {
170    /// The `--env` value.
171    pub env: String,
172    /// Repo-relative path of the target file (`targets/<env>.yaml`).
173    pub target_file: String,
174    /// The parsed target.
175    pub target: Target,
176    /// The parsed gateway manifest.
177    pub manifest: Manifest,
178    /// The render of the declared tree.
179    pub outcome: RenderOutcome,
180}
181
182impl Declared {
183    /// The effective tag provider exclusion set of this target and manifest
184    /// ([`exclude::effective`]): byte-sorted, always holding `System`. Every
185    /// host read scope and every plan of this tree uses it.
186    pub fn excluded_tag_providers(&self) -> Vec<String> {
187        exclude::effective(&self.target, Some(&self.manifest))
188    }
189}
190
191/// Load the target, the manifest and the source tree of `env` under `repo`
192/// and render them. Reads the repo and nothing else.
193pub fn load_declared(repo: &Path, env: &str) -> Result<Declared, CoreError> {
194    load_declared_in(repo, repo, env)
195}
196
197/// [`load_declared`] with the target read from `targets` instead of `repo`
198/// (`--targets DIR`, D-14): `targets/<env>.yaml` comes from `targets`, the
199/// `gateways/<gateway>/` tree from `repo`. A caller that builds the node's
200/// host passes the same `targets` root to it, so a relative `known_hosts` or
201/// `local.data_dir` resolves against the targets checkout.
202pub fn load_declared_in(repo: &Path, targets: &Path, env: &str) -> Result<Declared, CoreError> {
203    let (target_file, target) = load_target(targets, env)?;
204
205    let gateway_rel = format!("gateways/{}", target.gateway);
206    let gateway_dir = repo.join("gateways").join(&target.gateway);
207    require_repo_dir(repo, "gateways")?;
208    require_repo_dir(repo, &gateway_rel)?;
209
210    let manifest_rel = format!("{gateway_rel}/manifest.yaml");
211    let manifest_bytes = read_repo_file(repo, &manifest_rel, None)?;
212    let manifest = Manifest::parse(&manifest_rel, &manifest_bytes)?;
213
214    let source = SourceTree::read_gateway(&gateway_dir, env)?;
215    let outcome = render(&RenderInput {
216        gateway: &target.gateway,
217        env,
218        manifest: &manifest,
219        target: &target,
220        source: &source,
221    })?;
222    Ok(Declared {
223        env: env.to_string(),
224        target_file,
225        target,
226        manifest,
227        outcome,
228    })
229}
230
231/// Render the declared tree of `env` under `repo`, optionally writing it
232/// (and `render.json`) to `out`.
233pub fn deploy_render(
234    repo: &Path,
235    env: &str,
236    out: Option<&Path>,
237) -> Result<DeployRenderResult, CoreError> {
238    deploy_render_in(repo, repo, env, out)
239}
240
241/// [`deploy_render`] with the target read from `targets` (`--targets DIR`,
242/// D-14). `out` is written wherever the caller names it, never under
243/// `targets`.
244pub fn deploy_render_in(
245    repo: &Path,
246    targets: &Path,
247    env: &str,
248    out: Option<&Path>,
249) -> Result<DeployRenderResult, CoreError> {
250    let Declared {
251        target, outcome, ..
252    } = load_declared_in(repo, targets, env)?;
253
254    // Only a successful render reaches the filesystem.
255    if let Some(out) = out {
256        let marker = RenderMarker::for_tree(&target.gateway, env, &target.mode, &outcome.tree);
257        write_out(&outcome.tree, out, &marker)?;
258    }
259
260    Ok(DeployRenderResult {
261        env: env.to_string(),
262        gateway: target.gateway.clone(),
263        mode: target.mode.clone(),
264        out: out.map(|p| p.to_string_lossy().into_owned()),
265        files: outcome.tree.files.len(),
266        bytes: outcome.tree.bytes(),
267        render_sha256: outcome.tree.render_sha256(),
268        resources: outcome.resources,
269        tag_providers: outcome.tag_providers,
270        uuids_minted: outcome.uuids_minted,
271        opaque_files: outcome.opaque_files,
272        warnings: outcome.warnings,
273    })
274}
275
276/// The `ign deploy plan` result (the envelope's `data`).
277#[derive(Debug, Clone, PartialEq, Serialize)]
278pub struct DeployPlanResult {
279    /// The `--env` value.
280    pub env: String,
281    /// Gateway directory name from the target.
282    pub gateway: String,
283    /// Deployment mode from the target.
284    pub mode: String,
285    /// The node that was read: always `master` in this phase.
286    pub node: String,
287    /// How the node's host was read: `local` or `ssh`.
288    pub transport: String,
289    /// Lowercase hex SHA-256 of the rendered listing.
290    pub render_sha256: String,
291    /// Lowercase hex SHA-256 of the canonical plan text (D-08).
292    pub plan_sha256: String,
293    /// Counts by status; `has_changes` ignores `unmanaged`.
294    pub summary: PlanSummary,
295    /// One entry per unit, in unit-key byte order.
296    pub entries: Vec<PlanEntry>,
297    /// The files plan proposes to delete: the declared-names ledger's files
298    /// minus the rendered files (22-03). Empty without a ledger.
299    pub delete_set: DeleteSet,
300    /// The `--base` render the drift was computed against; null without it.
301    pub base: Option<BaseSummary>,
302    /// Render warnings, then plan warnings.
303    pub warnings: Vec<String>,
304    /// The effective tag provider exclusion set: byte-sorted, always holds
305    /// `System`. Those providers were not read and are never written or
306    /// deleted. Not part of `plan_sha256`.
307    pub excluded_tag_providers: Vec<String>,
308    /// Units the host holds but could not serve whole (a Windows sftp host
309    /// path of 240 characters or more), in key byte order: never an entry,
310    /// never in the delete set, one `U` line each in `plan_sha256`. Always
311    /// empty on Linux and local hosts.
312    pub unreadable: Vec<UnreadableUnit>,
313    /// On a redundant pair, the residue paths of the master's mode
314    /// `ignition/tag-definition` and `ignition/tag-type-definition` folders
315    /// ([`pair_mode_tag_dirs`]), byte-sorted: a folder or a stray file the
316    /// plan does not read, residue like a mode copy of a tag unit (quick
317    /// 261103-ell, F3; quick 261104-7cy, C3). Always empty for a standalone
318    /// target. Not serialized and not part of
319    /// `plan_sha256`: the plan JSON key set is the contract, and the warning
320    /// and the apply refusal carry the folders.
321    #[serde(skip)]
322    pub pair_mode_tag_dirs: Vec<String>,
323}
324
325/// Options of `deploy plan`.
326#[derive(Debug, Clone, Default)]
327pub struct PlanOptions {
328    /// A directory written by `deploy render --out`, re-hashed and used as
329    /// the base of the three-way drift check.
330    pub base: Option<PathBuf>,
331    /// Add redacted unified text diffs to the entries.
332    pub diff: bool,
333}
334
335/// What `--base` was: the directory and its recomputed digest.
336#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
337pub struct BaseSummary {
338    /// The `--base` directory as given.
339    pub dir: String,
340    /// The base's recomputed `render_sha256`.
341    pub render_sha256: String,
342    /// Number of files in the base (`render.json` excluded).
343    pub files: usize,
344}
345
346/// Plan `declared` against the live files `reader` returns for the master
347/// node. Reads once; never writes, never opens a gateway session, never
348/// prints.
349pub async fn deploy_plan(
350    declared: &Declared,
351    reader: &dyn TreeReader,
352    opts: &PlanOptions,
353) -> Result<DeployPlanResult, CoreError> {
354    let node = &declared.target.nodes.master;
355    let transport = match (&node.ssh, &node.local) {
356        (Some(_), _) => "ssh",
357        (None, Some(_)) => "local",
358        (None, None) => "none",
359    };
360    // A base that is not exactly a clean render is refused before any
361    // transport work (22-04).
362    let base = match &opts.base {
363        Some(dir) => {
364            let dir = dir.clone();
365            let (gateway, env, mode) = (
366                declared.target.gateway.clone(),
367                declared.env.clone(),
368                declared.target.mode.clone(),
369            );
370            let read =
371                tokio::task::spawn_blocking(move || BaseTree::read(&dir, &gateway, &env, &mode))
372                    .await
373                    .map_err(|e| CoreError::Internal(format!("--base read task failed: {e}")))?;
374            Some(read?)
375        }
376        None => None,
377    };
378    // An interrupted apply left its journal: the tree may be torn, so nothing
379    // is read or planned until `rollback --run` recovers it (DAPL-09, D-09).
380    // An unreadable journal blocks the same way. Checked after the base (a
381    // bad one costs no transport work) and before the collections are read.
382    if let Some(journal) = journal_on_box(
383        reader,
384        node,
385        &declared.target.gateway,
386        &declared.target.mode,
387    )
388    .await?
389    {
390        return Err(journal_present(
391            &journal,
392            &declared.env,
393            node,
394            &declared.target.gateway,
395            &declared.target.mode,
396        ));
397    }
398    let excluded = declared.excluded_tag_providers();
399    let scope = Scope::from_manifest(&declared.manifest, &declared.target.mode, &excluded);
400    let observed = reader.read(&scope).await?;
401    // A live tag folder named like a tag file (CD5d) is refused before any
402    // planning: an apply would only meet it when it renames onto it
403    // (261030-8m8 review WR-04).
404    let clashes = live_tag_folder_findings(&observed, &declared.target.mode);
405    if !clashes.is_empty() {
406        return Err(CoreError::deploy_findings(clashes));
407    }
408    let gateway = &declared.target.gateway;
409    let mode = &declared.target.mode;
410    // An absent ledger is a first deploy (empty delete set); one that exists
411    // but cannot be trusted is a refusal, never an empty delete set (D-07).
412    let ledger = match reader.read_ledger(gateway, mode).await? {
413        Some(bytes) => Some(Ledger::parse(
414            &ledger_location(LedgerRoot::for_node(node), gateway, mode),
415            &bytes,
416            gateway,
417            mode,
418        )?),
419        None => None,
420    };
421    // A2 (quick 261102-cef): on a redundant pair the tag units are planned
422    // into `core`, where the apply writes them.
423    let mut planned = plan_with(
424        &declared.outcome.tree,
425        &observed,
426        ledger.as_ref(),
427        &scope,
428        &excluded,
429        Redundancy::of(&declared.target),
430    );
431    if let Some(base) = &base {
432        apply_base(&mut planned, base);
433    }
434    if opts.diff {
435        render_diffs(&mut planned, &declared.outcome.tree, &observed);
436    }
437
438    let mut warnings = declared.outcome.warnings.clone();
439    warnings.extend(planned.warnings);
440    let mut pair_mode_tag_dirs = Vec::new();
441    if declared.target.nodes.backup.is_some() {
442        pair_mode_tag_dirs = pair_mode_tag_dirs_of(reader, mode, &planned.entries).await?;
443        warnings.extend(pair_tags_warning(
444            &planned.entries,
445            &planned.delete_set,
446            &pair_mode_tag_dirs,
447            mode,
448        ));
449    }
450    Ok(DeployPlanResult {
451        env: declared.env.clone(),
452        gateway: declared.target.gateway.clone(),
453        mode: declared.target.mode.clone(),
454        node: "master".to_string(),
455        transport: transport.to_string(),
456        render_sha256: declared.outcome.tree.render_sha256(),
457        plan_sha256: planned.plan_sha256,
458        summary: planned.summary,
459        entries: planned.entries,
460        delete_set: planned.delete_set,
461        base: base.map(|b| BaseSummary {
462            dir: b.dir,
463            render_sha256: b.render_sha256,
464            files: b.files.len(),
465        }),
466        warnings,
467        excluded_tag_providers: excluded,
468        unreadable: planned.unreadable,
469        pair_mode_tag_dirs,
470    })
471}
472
473/// The residue paths of a pair's mode collection (quick 261103-ell, F3;
474/// quick 261104-7cy, C3): [`pair_mode_tag_dirs`] over the mode collection's
475/// `ignition/<type>` folders of
476/// [`PAIR_UNSYNCED_TAG_TYPES`], listed whole by [`TreeReader::list_tree`]
477/// (names and kinds; `read` returns in-scope files only). One listing, only
478/// on a pair: `deploy plan` and `deploy verify` call it, and the apply engine
479/// calls it again after staging, before the lock (quick 261104-7cy, C12).
480pub(crate) async fn pair_mode_tag_dirs_of(
481    reader: &dyn TreeReader,
482    mode: &str,
483    entries: &[PlanEntry],
484) -> Result<Vec<String>, CoreError> {
485    let dirs: Vec<(String, String)> = PAIR_UNSYNCED_TAG_TYPES
486        .iter()
487        .map(|ty| ("ignition".to_string(), (*ty).to_string()))
488        .collect();
489    let listed = reader.list_tree(mode, &dirs).await?;
490    Ok(pair_mode_tag_dirs(&listed, entries))
491}
492
493/// The first ten of `items` joined, then `and N more`.
494fn named_list(items: &[String]) -> String {
495    let shown = items.len().min(crate::error::APPLY_LIST_SHOWN);
496    let mut named = items[..shown].join(", ");
497    if items.len() > shown {
498        named.push_str(&format!(" and {} more", items.len() - shown));
499    }
500    named
501}
502
503/// The plan warning of a redundant pair's target whose plan holds tag units
504/// or mode tag type folder content `deploy apply` refuses ([`pair_tag_keys`]
505/// and `dirs`, debug deploy-tag-redundancy-sync): one line naming the first
506/// ten keys and the count, or `None`. Since quick 261102-cef (A2) a pair's tag
507/// units are planned into `core`, so these are only the residue: units whose
508/// files in the mode collection the apply cannot delete, and (quick
509/// 261103-ell, F3; quick 261104-7cy, C3) the files or folders of those type
510/// folders the deploy does not read. A warning, not a refusal: plan
511/// never writes, and the review of the plan should show what apply will
512/// refuse. The empty return tests the combined list, so folder-only residue
513/// warns.
514fn pair_tags_warning(
515    entries: &[PlanEntry],
516    delete: &DeleteSet,
517    dirs: &[String],
518    mode: &str,
519) -> Option<String> {
520    let mut keys = pair_tag_keys(entries, delete);
521    keys.extend(dirs.iter().cloned());
522    keys.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes()));
523    keys.dedup();
524    if keys.is_empty() {
525        return None;
526    }
527    Some(format!(
528        "redundant pair: {} tag unit(s) or folder(s) would leave files or folders in the {mode} \
529         collection that this deploy cannot delete (the ledger does not list them, the delete \
530         set skips them, or they are files or folders the deploy does not read), which Ignition's \
531         redundancy sync cannot carry; deploy apply refuses this plan \
532         (deploy_pair_tags_refused): {}",
533        keys.len(),
534        named_list(&keys)
535    ))
536}
537
538/// The verify warning of a redundant pair whose master still holds
539/// `tag-definition` or `tag-type-definition` files in its mode collection
540/// (quick 261102-cef, O2), or a file or folder of such a type folder the plan
541/// does not read (`dirs`, quick 261103-ell, F3; quick 261104-7cy, C3): one
542/// line naming the first ten paths and
543/// the count, or `None`. `files` are the mode files of units the target does
544/// not declare, whatever their status (`unmanaged` included); a declared
545/// unit's mode copy fails the `files` check instead (quick 261103-ell, F2).
546/// Each such file alone stops the backup from syncing configuration (debug
547/// deploy-tag-redundancy-sync, T6), and so does an unread folder or file. A
548/// warning, not a check: the check ids are the contract. Paths only. The
549/// empty return tests the combined list.
550fn pair_mode_tags_warning(files: Vec<String>, dirs: &[String], mode: &str) -> Option<String> {
551    let mut paths = files;
552    paths.extend(dirs.iter().cloned());
553    if paths.is_empty() {
554        return None;
555    }
556    Some(format!(
557        "redundant pair: the master's {mode} collection holds {} tag file(s) or folder(s), which \
558         Ignition's redundancy sync cannot carry (the backup stops syncing configuration); a \
559         deploy that declares the units and lists the files in its ledger moves them to the core \
560         collection, or remove them on the master (README runbook T6e): {}",
561        paths.len(),
562        named_list(&paths)
563    ))
564}
565
566/// The `ign deploy pull` result (the envelope's `data`).
567#[derive(Debug, Clone, PartialEq, Serialize)]
568pub struct DeployPullResult {
569    /// The `--env` value.
570    pub env: String,
571    /// Gateway directory name from the target.
572    pub gateway: String,
573    /// Deployment mode from the target; its collection is the overlay.
574    pub mode: String,
575    /// The node that was read: always `master` in this phase.
576    pub node: String,
577    /// How the node's host was read: `local` or `ssh`.
578    pub transport: String,
579    /// The directory written, repo-relative: `gateways/<name>`.
580    pub out: String,
581    /// Source files written under `resources/`, `tags/` and `modes/<env>/`
582    /// (the manifest is not counted).
583    pub files_written: usize,
584    /// `kept` when `manifest.yaml` already existed, `generated` otherwise.
585    pub manifest: String,
586    /// Lowercase hex SHA-256 of what `deploy render` produces from the
587    /// pulled tree.
588    pub render_sha256: String,
589    /// Every live unit (or type) not written, byte-sorted by path, each with
590    /// a reason. Never a value.
591    pub skipped: Vec<Skipped>,
592    /// Render warnings of the pulled tree, then pull warnings.
593    pub warnings: Vec<String>,
594    /// The effective tag provider exclusion set: byte-sorted, always holds
595    /// `System`. Those providers were not listed or read, and are left out
596    /// of a generated manifest.
597    pub excluded_tag_providers: Vec<String>,
598}
599
600/// What an existing `gateways/<name>/` holds in the three subtrees a pull
601/// replaces. Gateway-relative paths, files in `resources`, `tags`,
602/// `modes/<env>` order and byte order within each.
603struct Existing {
604    files: Vec<String>,
605    /// Every directory below and including each subtree root.
606    dirs: BTreeSet<String>,
607}
608
609/// Whether `repo/rel` exists, without following a symlink.
610fn repo_entry_exists(repo: &Path, rel: &str) -> Result<bool, CoreError> {
611    match std::fs::symlink_metadata(repo.join(rel)) {
612        Ok(_) => Ok(true),
613        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
614        Err(e) => Err(CoreError::deploy_tree_invalid(
615            rel,
616            None,
617            None,
618            format!("cannot inspect ({})", e.kind()),
619        )),
620    }
621}
622
623/// Inspect the subtrees `deploy pull` would replace. A symlink at any level
624/// of them, or anywhere under them, is `deploy_tree_invalid` and nothing is
625/// changed. `None` when `gateways/<gateway>/` does not exist.
626fn scan_existing(repo: &Path, gateway: &str, env: &str) -> Result<Option<Existing>, CoreError> {
627    let gateway_rel = format!("gateways/{gateway}");
628    for rel in ["gateways", gateway_rel.as_str()] {
629        if !repo_entry_exists(repo, rel)? {
630            return Ok(None);
631        }
632        require_repo_dir(repo, rel)?;
633    }
634    let mut existing = Existing {
635        files: Vec::new(),
636        dirs: BTreeSet::new(),
637    };
638    for sub in [
639        "resources".to_string(),
640        "tags".to_string(),
641        format!("modes/{env}"),
642    ] {
643        if sub.starts_with("modes/") {
644            let modes = format!("{gateway_rel}/modes");
645            if !repo_entry_exists(repo, &modes)? {
646                continue;
647            }
648            require_repo_dir(repo, &modes)?;
649        }
650        let rel = format!("{gateway_rel}/{sub}");
651        if !repo_entry_exists(repo, &rel)? {
652            continue;
653        }
654        require_repo_dir(repo, &rel)?;
655        let mut files: BTreeMap<String, PathBuf> = BTreeMap::new();
656        let mut dirs: BTreeSet<String> = BTreeSet::new();
657        walk_capped(
658            &repo.join(&rel),
659            &sub,
660            &mut files,
661            &mut dirs,
662            READ_MAX_ENTRIES,
663        )
664        .map_err(|why| CoreError::deploy_tree_invalid(&rel, None, None, why))?;
665        existing.dirs.insert(sub.clone());
666        existing.dirs.extend(dirs);
667        existing.files.extend(files.into_keys());
668    }
669    Ok(Some(existing))
670}
671
672/// The refusal prose of a pull that would replace `existing`.
673fn pull_prose(gateway: &str, env: &str, existing: &Existing) -> String {
674    let shown: Vec<&str> = existing.files.iter().take(5).map(String::as_str).collect();
675    let more = if existing.files.len() > shown.len() {
676        ", ..."
677    } else {
678        ""
679    };
680    format!(
681        "deploy pull would replace gateways/{gateway}/resources, tags and modes/{env} with the live tree of nodes.master ({} existing files, e.g. {}{more})",
682        existing.files.len(),
683        shown.join(", ")
684    )
685}
686
687/// What `deploy pull` would replace: `Some(prose)` when the gateway
688/// directory holds files in `resources/`, `tags/` or `modes/<env>/` (the
689/// text the `--yes` guard refuses with), `None` when it is absent or holds
690/// nothing there. Reads the local repo only: no gateway is contacted.
691pub fn deploy_pull_preview(repo: &Path, env: &str) -> Result<Option<String>, CoreError> {
692    deploy_pull_preview_in(repo, repo, env)
693}
694
695/// [`deploy_pull_preview`] with the target read from `targets` (`--targets
696/// DIR`, D-14); the existing tree is still scanned under `repo`.
697pub fn deploy_pull_preview_in(
698    repo: &Path,
699    targets: &Path,
700    env: &str,
701) -> Result<Option<String>, CoreError> {
702    let (_, target) = load_target(targets, env)?;
703    Ok(scan_existing(repo, &target.gateway, env)?
704        .filter(|existing| !existing.files.is_empty())
705        .map(|existing| pull_prose(&target.gateway, env, &existing)))
706}
707
708fn transport_label(node: &NodeTarget) -> &'static str {
709    match (&node.ssh, &node.local) {
710        (Some(_), _) => "ssh",
711        (None, Some(_)) => "local",
712        (None, None) => "none",
713    }
714}
715
716/// Bootstrap `gateways/<gateway>/` from the live master. See the module doc.
717///
718/// `yes` confirms replacing existing `resources/`, `tags/` and `modes/<env>/`
719/// files; without it a non-empty tree is refused as `confirmation_required`
720/// before the gateway is read and before any write.
721pub async fn deploy_pull(
722    repo: &Path,
723    env: &str,
724    reader: &dyn TreeReader,
725    yes: bool,
726) -> Result<DeployPullResult, CoreError> {
727    deploy_pull_in(repo, repo, env, reader, yes).await
728}
729
730/// [`deploy_pull`] with the target read from `targets` (`--targets DIR`,
731/// D-14). The pulled tree and the manifest still live under `repo`.
732pub async fn deploy_pull_in(
733    repo: &Path,
734    targets: &Path,
735    env: &str,
736    reader: &dyn TreeReader,
737    yes: bool,
738) -> Result<DeployPullResult, CoreError> {
739    let (_, target) = load_target(targets, env)?;
740    let gateway = target.gateway.clone();
741    let existing = scan_existing(repo, &gateway, env)?;
742    if let Some(existing) = existing.as_ref().filter(|e| !e.files.is_empty())
743        && !yes
744    {
745        return Err(CoreError::ConfirmationRequired {
746            operation: pull_prose(&gateway, env, existing),
747        });
748    }
749
750    // The manifest: an existing one is used and kept (D-10); otherwise one is
751    // generated from the live types and tag providers.
752    let manifest_rel = format!("gateways/{gateway}/manifest.yaml");
753    let mut left_out: Vec<Skipped> = Vec::new();
754    let (manifest, generated, excluded) = if repo_entry_exists(repo, &manifest_rel)? {
755        let bytes = read_repo_file(repo, &manifest_rel, None)?;
756        let manifest = Manifest::parse(&manifest_rel, &bytes)?;
757        // A kept manifest that declares an excluded provider is refused
758        // before any host read: render would refuse the pulled tree anyway.
759        let excluded = exclude::effective(&target, Some(&manifest));
760        let contradictions = exclude::contradictions(&excluded, &manifest, &manifest_rel);
761        if !contradictions.is_empty() {
762            return Err(CoreError::deploy_findings(contradictions));
763        }
764        (manifest, None, excluded)
765    } else {
766        let excluded = exclude::effective(&target, None);
767        let mut live_types = reader.list_types(CORE_COLLECTION).await?;
768        live_types.extend(reader.list_types(&target.mode).await?);
769        // Provider discovery lists directory names only: no provider is read,
770        // so an excluded one is never walked (and an empty provider directory
771        // is named too).
772        let mut names = reader.list_tag_providers(CORE_COLLECTION).await?;
773        names.extend(reader.list_tag_providers(&target.mode).await?);
774        let providers: Vec<String> = names
775            .into_iter()
776            .filter(|p| !exclude::is_excluded(p, &excluded))
777            .collect::<BTreeSet<String>>()
778            .into_iter()
779            .collect();
780        let (yaml, skipped) = generate_manifest(&live_types, &providers);
781        left_out = skipped;
782        let manifest = Manifest::parse(&manifest_rel, yaml.as_bytes())?;
783        (manifest, Some(yaml), excluded)
784    };
785
786    let scope = Scope::from_manifest(&manifest, &target.mode, &excluded);
787    let observed = reader.read(&scope).await?;
788    let outcome = pull(&PullInput {
789        gateway: &gateway,
790        env,
791        target: &target,
792        manifest: &manifest,
793        observed: &observed,
794    })?;
795
796    write_pulled(
797        repo,
798        &gateway,
799        existing.as_ref(),
800        &outcome.source,
801        generated.as_deref(),
802    )?;
803
804    let removed = removed_unreadable_warnings(&gateway, env, existing.as_ref(), &outcome.skipped);
805    left_out.extend(outcome.skipped);
806    left_out.sort_by(|a, b| a.path.as_bytes().cmp(b.path.as_bytes()));
807    let mut warnings = outcome.rendered.warnings.clone();
808    warnings.extend(removed);
809    warnings.extend(observed.local_units.iter().map(|unit| {
810        format!(
811            "{unit} also exists in the `local` collection, which the gateway loads over the mode collection; pull did not read the `local` copy"
812        )
813    }));
814    Ok(DeployPullResult {
815        env: env.to_string(),
816        gateway: gateway.clone(),
817        mode: target.mode.clone(),
818        node: "master".to_string(),
819        transport: transport_label(&target.nodes.master).to_string(),
820        out: format!("gateways/{gateway}"),
821        files_written: outcome.source.files.len(),
822        manifest: if generated.is_some() {
823            "generated"
824        } else {
825            "kept"
826        }
827        .to_string(),
828        render_sha256: outcome.rendered.tree.render_sha256(),
829        skipped: left_out,
830        warnings,
831        excluded_tag_providers: excluded,
832    })
833}
834
835/// The most removed repo files one [`removed_unreadable_warnings`] line
836/// names before it counts the rest.
837const REMOVED_FILES_SHOWN: usize = 10;
838
839/// One warning per `path_too_long` unit whose repo copy this pull removed
840/// (quick-261029-ms3, WR-05). `--yes` replaces `resources/`, `tags/` and
841/// `modes/<env>/`, and an unreadable unit is never written in part, so the
842/// files the repo held for it are gone after the pull. They are not kept: a
843/// kept copy is not what the host serves, so the pulled tree would no longer
844/// render to the `render_sha256` pull reports and render then plan would not
845/// be empty. The removal is named instead of silent, with every removed
846/// repo file (gateway-relative under `gateways/<gateway>/`); git still holds
847/// them. Nothing for a unit the repo did not hold.
848fn removed_unreadable_warnings(
849    gateway: &str,
850    env: &str,
851    existing: Option<&Existing>,
852    skipped: &[Skipped],
853) -> Vec<String> {
854    let Some(existing) = existing else {
855        return Vec::new();
856    };
857    let mode_prefix = format!("modes/{env}/");
858    let mut out = Vec::new();
859    for unit in skipped.iter().filter(|s| s.reason == REASON_PATH_TOO_LONG) {
860        let under = format!("{}/", unit.path);
861        let removed: Vec<String> = existing
862            .files
863            .iter()
864            .filter(|file| {
865                let base = file.strip_prefix(&mode_prefix).unwrap_or(file);
866                RelPath::new(base)
867                    .ok()
868                    .and_then(|p| collection_path(&p).ok())
869                    .is_some_and(|c| c.as_str() == unit.path || c.as_str().starts_with(&under))
870            })
871            .map(|file| format!("gateways/{gateway}/{file}"))
872            .collect();
873        if removed.is_empty() {
874            continue;
875        }
876        let take = removed.len().min(REMOVED_FILES_SHOWN);
877        let mut named = removed[..take].join(", ");
878        if removed.len() > take {
879            named.push_str(&format!(" and {} more", removed.len() - take));
880        }
881        out.push(format!(
882            "{} has a live path the host's sftp server cannot read ({REASON_PATH_TOO_LONG}), so pull did not write it and removed its {} repo file(s): {named}; the live unit is unchanged and git still holds the removed files",
883            unit.path,
884            removed.len()
885        ));
886    }
887    out
888}
889
890/// A name that reaches the repo from the gateway is checked once more here:
891/// render already refused unsafe names, so this only fails closed.
892fn safe_component(name: &str) -> bool {
893    !name.is_empty()
894        && name != "."
895        && name != ".."
896        && !name.contains(['/', '\\', '\0'])
897        && !name.chars().any(char::is_control)
898}
899
900/// Refuse a pulled tree that cannot be written whole. This runs before the
901/// first delete, so a pull that would fail part-way fails with the existing
902/// tree untouched (CR-01): an unsafe path component, two paths that collide
903/// when case is folded (a case-insensitive filesystem keeps only one), and a
904/// path that is both a file and a directory.
905fn validate_pulled(gateway: &str, source: &SourceTree) -> Result<(), CoreError> {
906    let refuse = |path: &str, why: String| {
907        CoreError::deploy_tree_invalid(format!("gateways/{gateway}/{path}"), None, None, why)
908    };
909    let mut folded: BTreeMap<String, &str> = BTreeMap::new();
910    let mut dir_folds: BTreeSet<String> = BTreeSet::new();
911    for path in source.files.keys() {
912        let parts: Vec<&str> = path.components().collect();
913        if parts.iter().any(|c| !safe_component(c)) {
914            return Err(refuse(
915                path.as_str(),
916                "has an unsafe path component; refused before anything was changed".to_string(),
917            ));
918        }
919        let fold = path.as_str().to_lowercase();
920        if let Some(other) = folded.insert(fold.clone(), path.as_str()) {
921            return Err(refuse(
922                path.as_str(),
923                format!(
924                    "collides with {other} when case is folded; a case-insensitive filesystem keeps only one. Refused before anything was changed"
925                ),
926            ));
927        }
928        for end in fold.match_indices('/').map(|(i, _)| i) {
929            dir_folds.insert(fold[..end].to_string());
930        }
931    }
932    for (fold, path) in &folded {
933        if dir_folds.contains(fold) {
934            return Err(refuse(
935                path,
936                "is both a file and a directory in the pulled tree; refused before anything was changed"
937                    .to_string(),
938            ));
939        }
940    }
941    Ok(())
942}
943
944/// Validate the pulled tree, then replace what a pull replaces and write it
945/// (see [`write_pulled_fs`]). The validation comes first so no destructive
946/// step runs for a tree that is known to be unwritable.
947fn write_pulled(
948    repo: &Path,
949    gateway: &str,
950    existing: Option<&Existing>,
951    source: &SourceTree,
952    manifest: Option<&str>,
953) -> Result<(), CoreError> {
954    validate_pulled(gateway, source)?;
955    write_pulled_fs(repo, gateway, existing, source, manifest)
956}
957
958/// Remove what a pull replaces, then write the pulled tree and, when one was
959/// generated, the manifest. Unix: every step is descriptor-relative and
960/// rooted at the repo, so no component can be redirected by a symlink.
961#[cfg(unix)]
962fn write_pulled_fs(
963    repo: &Path,
964    gateway: &str,
965    existing: Option<&Existing>,
966    source: &SourceTree,
967    manifest: Option<&str>,
968) -> Result<(), CoreError> {
969    use crate::deploy::tree::dirfd::{self, FdError, Kind};
970    use std::io::Write;
971
972    let fail = |what: &str, e: FdError| match e {
973        FdError::Changed => CoreError::deploy_tree_invalid(
974            what,
975            None,
976            None,
977            "changed while it was written (a symlink or a non-directory appeared); refused, nothing outside the gateway directory was touched",
978        ),
979        FdError::Io(io) => CoreError::Internal(format!("cannot write {what}: {io}")),
980        other => CoreError::Internal(format!("cannot write {what}: {other:?}")),
981    };
982
983    // The repo root is the operator's own and may legitimately be reached
984    // through a symlink (`--repo link`), so it is resolved once here; every
985    // component below it is then opened without following symlinks.
986    let repo_real = std::fs::canonicalize(repo).map_err(|e| {
987        CoreError::Internal(format!("cannot resolve the repo {}: {e}", repo.display()))
988    })?;
989    let root = dirfd::open_root(&repo_real).map_err(|e| fail("the repo", e))?;
990    let gateways = dirfd::ensure_dir(&root, "gateways").map_err(|e| fail("gateways", e))?;
991    let gw_label = format!("gateways/{gateway}");
992    let gw = dirfd::ensure_dir(&gateways, gateway).map_err(|e| fail(&gw_label, e))?;
993
994    if let Some(existing) = existing {
995        for file in &existing.files {
996            let parts: Vec<&str> = file.split('/').collect();
997            let (name, parents) = parts.split_last().ok_or_else(|| {
998                CoreError::Internal("an existing file has an empty path".to_string())
999            })?;
1000            let label = format!("{gw_label}/{file}");
1001            let dir = dirfd::walk(&gw, parents).map_err(|e| fail(&label, e))?;
1002            match dirfd::kind_at(&dir, name).map_err(|e| fail(&label, e))? {
1003                Kind::File => dirfd::unlink_at(&dir, name).map_err(|e| fail(&label, e))?,
1004                Kind::Missing => {}
1005                _ => return Err(fail(&label, FdError::Changed)),
1006            }
1007        }
1008        // children before parents: a child path sorts after its parent
1009        for dir_path in existing.dirs.iter().rev() {
1010            let parts: Vec<&str> = dir_path.split('/').collect();
1011            let Some((name, parents)) = parts.split_last() else {
1012                continue;
1013            };
1014            let label = format!("{gw_label}/{dir_path}");
1015            let parent = dirfd::walk(&gw, parents).map_err(|e| fail(&label, e))?;
1016            match dirfd::rmdir_at(&parent, name) {
1017                Ok(()) | Err(FdError::Missing) => {}
1018                Err(FdError::NotEmpty) => {
1019                    return Err(CoreError::deploy_tree_invalid(
1020                        label,
1021                        None,
1022                        None,
1023                        "is not empty after the pull removed its files (something wrote into it); nothing more was changed",
1024                    ));
1025                }
1026                Err(e) => return Err(fail(&label, e)),
1027            }
1028        }
1029    }
1030
1031    if let Some(text) = manifest {
1032        let label = format!("{gw_label}/manifest.yaml");
1033        let mut file = dirfd::create_new(&gw, "manifest.yaml").map_err(|e| fail(&label, e))?;
1034        file.write_all(text.as_bytes())
1035            .map_err(|e| CoreError::Internal(format!("cannot write {label}: {e}")))?;
1036    }
1037    for (path, bytes) in &source.files {
1038        let label = format!("{gw_label}/{path}");
1039        let parts: Vec<&str> = path.components().collect();
1040        if parts.iter().any(|c| !safe_component(c)) {
1041            return Err(CoreError::deploy_tree_invalid(
1042                label,
1043                None,
1044                None,
1045                "has an unsafe path component; refused",
1046            ));
1047        }
1048        let (name, parents) = parts
1049            .split_last()
1050            .ok_or_else(|| CoreError::Internal("a pulled file has an empty path".to_string()))?;
1051        let dir = dirfd::ensure_walk(&gw, parents).map_err(|e| fail(&label, e))?;
1052        let mut file = dirfd::create_new(&dir, name).map_err(|e| fail(&label, e))?;
1053        file.write_all(bytes)
1054            .map_err(|e| CoreError::Internal(format!("cannot write {label}: {e}")))?;
1055    }
1056    Ok(())
1057}
1058
1059/// Non-unix: std has no descriptor-relative primitives, so the replaced
1060/// subtrees were checked for symlinks by [`scan_existing`] just before and
1061/// the operations are path-based (the 22-01 residual for Windows).
1062#[cfg(not(unix))]
1063fn write_pulled_fs(
1064    repo: &Path,
1065    gateway: &str,
1066    existing: Option<&Existing>,
1067    source: &SourceTree,
1068    manifest: Option<&str>,
1069) -> Result<(), CoreError> {
1070    use std::io::Write;
1071
1072    let io = |what: &Path, e: std::io::Error| {
1073        CoreError::Internal(format!("cannot write {}: {e}", what.display()))
1074    };
1075    let gw = repo.join("gateways").join(gateway);
1076    std::fs::create_dir_all(&gw).map_err(|e| io(&gw, e))?;
1077    if let Some(existing) = existing {
1078        for file in &existing.files {
1079            let path = file.split('/').fold(gw.clone(), |acc, c| acc.join(c));
1080            std::fs::remove_file(&path).map_err(|e| io(&path, e))?;
1081        }
1082        for dir_path in existing.dirs.iter().rev() {
1083            let path = dir_path.split('/').fold(gw.clone(), |acc, c| acc.join(c));
1084            std::fs::remove_dir(&path).map_err(|e| io(&path, e))?;
1085        }
1086    }
1087    let create = |path: &Path, bytes: &[u8]| -> Result<(), CoreError> {
1088        if let Some(parent) = path.parent() {
1089            std::fs::create_dir_all(parent).map_err(|e| io(parent, e))?;
1090        }
1091        let mut file = std::fs::OpenOptions::new()
1092            .write(true)
1093            .create_new(true)
1094            .open(path)
1095            .map_err(|e| io(path, e))?;
1096        file.write_all(bytes).map_err(|e| io(path, e))
1097    };
1098    if let Some(text) = manifest {
1099        create(&gw.join("manifest.yaml"), text.as_bytes())?;
1100    }
1101    for (path, bytes) in &source.files {
1102        let parts: Vec<&str> = path.components().collect();
1103        if parts.iter().any(|c| !safe_component(c)) {
1104            return Err(CoreError::deploy_tree_invalid(
1105                format!("gateways/{gateway}/{path}"),
1106                None,
1107                None,
1108                "has an unsafe path component; refused",
1109            ));
1110        }
1111        let dest = parts.iter().fold(gw.clone(), |acc, c| acc.join(c));
1112        create(&dest, bytes)?;
1113    }
1114    Ok(())
1115}
1116
1117/// Options of `deploy verify`.
1118#[derive(Debug, Clone)]
1119pub struct VerifyOptions {
1120    /// How long the probes are polled before the verdict.
1121    pub probe_deadline: Duration,
1122    /// Time between two probe polls.
1123    pub probe_interval: Duration,
1124    /// How long after a scan completed the gateway log is trusted to hold its
1125    /// lines (CV1). Skipped when the gateway has logged since.
1126    pub log_settle: Duration,
1127}
1128
1129impl Default for VerifyOptions {
1130    fn default() -> Self {
1131        Self {
1132            probe_deadline: verify::PROBE_DEADLINE,
1133            probe_interval: verify::PROBE_INTERVAL,
1134            log_settle: Duration::from_millis(verify::LOG_SETTLE_MS.unsigned_abs()),
1135        }
1136    }
1137}
1138
1139/// The `ign deploy verify` result (the envelope's `data`).
1140#[derive(Debug, Clone, PartialEq, Serialize)]
1141pub struct DeployVerifyResult {
1142    /// The `--env` value.
1143    pub env: String,
1144    /// Gateway directory name from the target.
1145    pub gateway: String,
1146    /// Deployment mode from the target.
1147    pub mode: String,
1148    /// The node that was verified: `master` (the default), `backup` or
1149    /// `both`.
1150    pub node: String,
1151    /// The WebDev project the tag probes were read through.
1152    pub project: String,
1153    /// True when no check failed.
1154    pub ok: bool,
1155    /// `deploy_verify_failed` when `ok` is false, else null.
1156    pub slug: Option<&'static str>,
1157    /// The six checks in fixed order.
1158    pub checks: Vec<verify::Check>,
1159    /// Declared resources `read_back` compared.
1160    pub resources_checked: usize,
1161    /// Non-failing notes.
1162    pub warnings: Vec<String>,
1163    /// The effective tag provider exclusion set: byte-sorted, always holds
1164    /// `System`. Those providers were not read or checked.
1165    pub excluded_tag_providers: Vec<String>,
1166    /// The backup node's checks (`--node backup|both`, V2 shape with
1167    /// `budget_s` 0, `waited_s` 0 and `resync` null); absent for `master`.
1168    #[serde(skip_serializing_if = "Option::is_none")]
1169    pub backup: Option<BackupReport>,
1170    /// The secret-name check of each node `--node` ran (V7); absent for
1171    /// `master`.
1172    #[serde(skip_serializing_if = "Option::is_none")]
1173    pub secrets: Option<SecretNamesReport>,
1174}
1175
1176impl DeployVerifyResult {
1177    /// The red-verdict exit passthrough (Decision A), mirroring
1178    /// [`crate::actions::testing::TestingRunResult::failure_exit_code`]:
1179    /// `Some(6)` (target state) when a check failed, `None` otherwise. The
1180    /// envelope renders first; the binary decides the exit after.
1181    pub fn failure_exit_code(&self) -> Option<u8> {
1182        (!self.ok).then_some(6)
1183    }
1184}
1185
1186/// Most pages of log lines read before the answer is called truncated.
1187const LOG_PAGES_MAX: usize = 5;
1188
1189/// Verify the master gateway against `declared` (DPLN-07, DPLN-08, DPLN-09).
1190///
1191/// Reads only: `scan_status`, `deployment_mode`, `resource_list` or
1192/// `resource_singleton` per declared non-tag type, one read of the live files
1193/// through `reader`, the gateway log, and the tag probes through `project`'s
1194/// WebDev route. It acquires no scan-lock, triggers no scan and writes no
1195/// file. Evidence is gathered, then judged by [`verify::evaluate_with`].
1196///
1197/// `read_back` is judged with the plan status of each declared resource,
1198/// from the same live-file read (quick 261031-bbs, R2): a REST difference,
1199/// or a resource missing from the REST effective view, on a resource the
1200/// plan reports `same` is a `warn` finding plus a `data.warnings` line (the
1201/// files on disk equal the repo; the gateway's REST view differs through its
1202/// own defaults or normalisation). Any other status (`changed`, `added`, or
1203/// none) stays red. A verify whose only `read_back` findings warn is green.
1204///
1205/// On a redundant pair (quick 261102-cef, A2) the live files are planned with
1206/// [`plan_with`] and [`Redundancy::Pair`], so the `files` check judges each
1207/// tag-definition and tag-type-definition unit against its `core` copy,
1208/// where a deploy writes it. A declared unit that also has files in the
1209/// master's mode collection fails that check with a `files_differ` finding
1210/// naming the paths (quick 261103-ell, F2): the copy overrides `core` and
1211/// blocks the backup's sync. The mode files of undeclared units, and an empty
1212/// mode tag type folder (F3), add one `data.warnings` line naming the paths
1213/// (O2); they are a warning, not a check, so the check ids are unchanged.
1214///
1215/// A gateway that cannot be reached ([`CoreError::Network`]), any refusal of
1216/// the resource, scan or mode reads, and a transport failure return a
1217/// [`CoreError`]: there is nothing to judge. A refusal of the log or tag
1218/// source is evidence, not an error: that check fails with the refusal's slug
1219/// (never its text). A red verdict is a normal `Ok` result; the binary maps
1220/// it to exit 6 through [`DeployVerifyResult::failure_exit_code`].
1221pub async fn deploy_verify(
1222    declared: &Declared,
1223    project: &str,
1224    deploy: &dyn DeployApi,
1225    gateway: &dyn GatewayApi,
1226    reader: &dyn TreeReader,
1227    opts: &VerifyOptions,
1228) -> Result<DeployVerifyResult, CoreError> {
1229    deploy_verify_judged(
1230        declared,
1231        project,
1232        deploy,
1233        gateway,
1234        reader,
1235        opts,
1236        VerifyContext::Standalone,
1237    )
1238    .await
1239}
1240
1241/// Who asks for a master verify, which decides how `read_back` is judged
1242/// (quick 261031-bbs).
1243#[derive(Debug, Clone, Copy)]
1244pub enum VerifyContext<'a> {
1245    /// `ign deploy verify`.
1246    Standalone,
1247    /// The verify after an apply's scan: `read_back` is judged against the
1248    /// REST baseline the apply read before it wrote anything.
1249    PostApply(&'a verify::ReadBackBaseline),
1250}
1251
1252/// [`deploy_verify`], with `read_back` judged for `ctx`.
1253pub(crate) async fn deploy_verify_judged(
1254    declared: &Declared,
1255    project: &str,
1256    deploy: &dyn DeployApi,
1257    gateway: &dyn GatewayApi,
1258    reader: &dyn TreeReader,
1259    opts: &VerifyOptions,
1260    ctx: VerifyContext<'_>,
1261) -> Result<DeployVerifyResult, CoreError> {
1262    deploy_verify_planned(declared, project, deploy, gateway, reader, opts, ctx)
1263        .await
1264        .map(|(result, _)| result)
1265}
1266
1267/// [`deploy_verify_judged`], also returning the plan status of every
1268/// declared non-tag resource (the `read_back` keys) from its one live-file
1269/// read, so `--node both` can judge the backup by the same rule without
1270/// reading the tree again.
1271async fn deploy_verify_planned(
1272    declared: &Declared,
1273    project: &str,
1274    deploy: &dyn DeployApi,
1275    gateway: &dyn GatewayApi,
1276    reader: &dyn TreeReader,
1277    opts: &VerifyOptions,
1278    ctx: VerifyContext<'_>,
1279) -> Result<(DeployVerifyResult, BTreeMap<String, PlanStatus>), CoreError> {
1280    let expected =
1281        verify::Expected::from_render(&declared.target.mode, &declared.outcome, &declared.manifest);
1282
1283    let scan = deploy.scan_status(ScanKind::Config).await?;
1284    let scan_read_at = std::time::Instant::now();
1285    let mode = normalize_deployment_mode(deploy.deployment_mode().await?);
1286    let read_back = gather_read_back(deploy, &expected).await?;
1287
1288    // The live files, once, over the read-only transport. Tag units are the
1289    // ones judged; reading is also what proves the host is reachable.
1290    let excluded = declared.excluded_tag_providers();
1291    let scope = Scope::from_manifest(&declared.manifest, &declared.target.mode, &excluded);
1292    let live = reader.read(&scope).await?;
1293    // A2 (quick 261102-cef): on a pair the tag units are judged against
1294    // `core`, where a deploy writes them.
1295    let redundancy = Redundancy::of(&declared.target);
1296    let planned = plan_with(
1297        &declared.outcome.tree,
1298        &live,
1299        None,
1300        &scope,
1301        &excluded,
1302        redundancy,
1303    );
1304    // On a pair, a declared tag unit's copy in the mode collection fails the
1305    // `files` check (quick 261103-ell, F2: the gateway's own edits of a core
1306    // tag stay in `core`, so a mode copy is drift that blocks the backup's
1307    // sync); the warning names the other mode tag files and the unread files
1308    // and folders of the mode tag type folders (F3; quick 261104-7cy, C3).
1309    let (mode_copies, residue) = if redundancy == Redundancy::Pair {
1310        let dirs = pair_mode_tag_dirs_of(reader, &declared.target.mode, &planned.entries).await?;
1311        let mut mode_copies: BTreeMap<String, Vec<String>> = BTreeMap::new();
1312        for entry in planned
1313            .entries
1314            .iter()
1315            .filter(|entry| expected.tag_units.contains(&entry.key))
1316        {
1317            let paths = pair_mode_tag_files(std::slice::from_ref(entry));
1318            if !paths.is_empty() {
1319                mode_copies.insert(entry.key.clone(), paths);
1320            }
1321        }
1322        let claimed: BTreeSet<&String> = mode_copies.values().flatten().collect();
1323        let others: Vec<String> = pair_mode_tag_files(&planned.entries)
1324            .into_iter()
1325            .filter(|path| !claimed.contains(path))
1326            .collect();
1327        let residue = pair_mode_tags_warning(others, &dirs, &declared.target.mode);
1328        (mode_copies, residue)
1329    } else {
1330        (BTreeMap::new(), None)
1331    };
1332    let files = planned
1333        .entries
1334        .iter()
1335        .filter(|entry| expected.tag_units.contains(&entry.key))
1336        .map(|entry| (entry.key.clone(), entry.status))
1337        .collect();
1338    let file_status = resource_statuses(&expected, &planned.entries);
1339
1340    let logs = if scan.scan_active {
1341        // Its lines are not readable yet; the check reports "not observed".
1342        Ok(Vec::new())
1343    } else {
1344        settle_logs(gateway, &scan, scan_read_at, opts.log_settle).await?;
1345        read_logs(gateway, verify::log_window_start(&scan)).await?
1346    };
1347    let probes = if expected.probes.is_empty() {
1348        None
1349    } else {
1350        Some(poll_probes(gateway, project, &expected.probes, opts).await?)
1351    };
1352
1353    let observed = verify::ObservedState {
1354        scan,
1355        mode,
1356        read_back,
1357        files,
1358        logs,
1359        probes,
1360        unreadable: planned
1361            .unreadable
1362            .iter()
1363            .map(|unit| (unit.key.clone(), unit.declared))
1364            .collect(),
1365        mode_copies,
1366    };
1367    let judge = match ctx {
1368        VerifyContext::Standalone => verify::ReadBackJudge::Standalone {
1369            file_status: &file_status,
1370        },
1371        VerifyContext::PostApply(baseline) => verify::ReadBackJudge::Baseline(baseline),
1372    };
1373    let report = verify::evaluate_with(&expected, &observed, &judge);
1374    let mut warnings = report.warnings;
1375    warnings.extend(residue);
1376    let result = DeployVerifyResult {
1377        env: declared.env.clone(),
1378        gateway: declared.target.gateway.clone(),
1379        mode: declared.target.mode.clone(),
1380        node: "master".to_string(),
1381        project: project.to_string(),
1382        ok: report.ok,
1383        slug: report.slug,
1384        checks: report.checks,
1385        resources_checked: report.resources_checked,
1386        warnings,
1387        excluded_tag_providers: excluded,
1388        backup: None,
1389        secrets: None,
1390    };
1391    Ok((result, file_status))
1392}
1393
1394/// The plan status of every declared non-tag resource `read_back` compares,
1395/// keyed like [`verify::ExpectedResource::key`] (a resource's unit key).
1396fn resource_statuses(
1397    expected: &verify::Expected,
1398    entries: &[PlanEntry],
1399) -> BTreeMap<String, PlanStatus> {
1400    let keys: BTreeSet<&str> = expected.resources.iter().map(|r| r.key.as_str()).collect();
1401    entries
1402        .iter()
1403        .filter(|entry| keys.contains(entry.key.as_str()))
1404        .map(|entry| (entry.key.clone(), entry.status))
1405        .collect()
1406}
1407
1408/// Which node(s) `deploy verify --node` looks at (D-12, V6).
1409#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1410pub enum VerifyNode {
1411    /// The six master checks; the default and byte-identical to the verb
1412    /// before Phase 24.
1413    Master,
1414    /// Only the backup checks.
1415    Backup,
1416    /// The six master checks, then the backup checks.
1417    Both,
1418}
1419
1420impl VerifyNode {
1421    /// The word the flag and `data.node` use.
1422    pub fn as_str(self) -> &'static str {
1423        match self {
1424            VerifyNode::Master => "master",
1425            VerifyNode::Backup => "backup",
1426            VerifyNode::Both => "both",
1427        }
1428    }
1429}
1430
1431/// The master's surfaces `deploy verify --node master|both` reads through.
1432pub type VerifyMasterSurfaces<'a> = (&'a dyn DeployApi, &'a dyn GatewayApi);
1433
1434/// `deploy verify` over one node or both (D-12, V6).
1435///
1436/// `Master` is [`deploy_verify`] unchanged. `Backup` runs the backup checks
1437/// only: the redundancy check (`peerConnected` and not `syncPending`), the
1438/// mode, the REST read-back of the render, and the backup's files against the
1439/// master's live copy, read through `master_reader` (a `warn` when `backup`
1440/// has no file transport). `Both` runs the master's six checks, then the
1441/// backup's. Like `deploy_verify` it never waits, never resyncs and never
1442/// writes: the backup is reached through [`BackupNode`], which has no write
1443/// to express, and the master only through reads.
1444///
1445/// The backup's `read_back` follows the master's standalone rule (R2): a
1446/// difference or a missing resource is a `warn` when the master's plan
1447/// reports that resource `same`, and red otherwise. The statuses come from
1448/// the master verify's own live-file read (`Both`), or from the master read
1449/// the backup `files` check makes (`Backup`). With neither (a backup with no
1450/// file transport under `--node backup`, or a files read that failed), the
1451/// backup's `read_back` is judged strictly (P10).
1452///
1453/// The plans are routed as [`deploy_verify`]'s (quick 261102-cef, A2), and
1454/// the mode-residue warning of a pair comes from the master verify, so
1455/// `Both` carries it once and `Backup` not at all. Likewise a declared unit's
1456/// mode copy fails the master's `files` check (quick 261103-ell, F2) and
1457/// leaves the backup checks as they are.
1458///
1459/// `master` is required for `Master` and `Both`, `backup` for `Backup` and
1460/// `Both`; a missing one is [`CoreError::InvalidInput`].
1461pub async fn deploy_verify_node(
1462    declared: &Declared,
1463    project: &str,
1464    node: VerifyNode,
1465    master: Option<VerifyMasterSurfaces<'_>>,
1466    master_reader: &dyn TreeReader,
1467    backup: Option<&BackupNode<'_>>,
1468    opts: &VerifyOptions,
1469) -> Result<DeployVerifyResult, CoreError> {
1470    let missing = |what: &str| CoreError::InvalidInput {
1471        reason: format!("deploy verify --node {} needs the {what}", node.as_str()),
1472    };
1473    let (master_result, master_status) = if node == VerifyNode::Backup {
1474        (None, None)
1475    } else {
1476        let (deploy, gateway) = master.ok_or_else(|| missing("master surfaces"))?;
1477        let (result, status) = deploy_verify_planned(
1478            declared,
1479            project,
1480            deploy,
1481            gateway,
1482            master_reader,
1483            opts,
1484            VerifyContext::Standalone,
1485        )
1486        .await?;
1487        (Some(result), Some(status))
1488    };
1489    if node == VerifyNode::Master {
1490        return master_result.ok_or_else(|| missing("master surfaces"));
1491    }
1492    let backup = backup.ok_or_else(|| missing("backup node"))?;
1493    let expected =
1494        verify::Expected::from_render(&declared.target.mode, &declared.outcome, &declared.manifest);
1495    let mut backup_warnings = Vec::new();
1496    let (tick, compared) = verify_backup_tick(
1497        declared,
1498        &expected,
1499        master_reader,
1500        backup,
1501        master_status.as_ref(),
1502        &mut backup_warnings,
1503    )
1504    .await;
1505    let report = verify_report(&tick);
1506
1507    let mut result = match master_result {
1508        // The master verify has not read the backup, so the backup's
1509        // read-back warnings are carried here exactly as `--node backup`
1510        // reports them (WR-03).
1511        Some(mut result) => {
1512            result.warnings.extend(backup_warnings);
1513            result
1514        }
1515        None => DeployVerifyResult {
1516            env: declared.env.clone(),
1517            gateway: declared.target.gateway.clone(),
1518            mode: declared.target.mode.clone(),
1519            node: String::new(),
1520            project: project.to_string(),
1521            ok: true,
1522            slug: None,
1523            checks: Vec::new(),
1524            resources_checked: compared,
1525            warnings: backup_warnings,
1526            excluded_tag_providers: declared.excluded_tag_providers(),
1527            backup: None,
1528            secrets: None,
1529        },
1530    };
1531    // The secret names each checked node holds, master first (V7). Names
1532    // only: `secret_names` never reads a value.
1533    let secrets = verify_secret_names(
1534        declared,
1535        master
1536            .map(|(deploy, _)| deploy)
1537            .filter(|_| node != VerifyNode::Backup),
1538        Some(backup),
1539    )
1540    .await?;
1541    let secrets_ok = secrets.nodes.iter().all(|n| n.status != CheckStatus::Fail);
1542    result.node = node.as_str().to_string();
1543    result.ok = result.ok && report.converged && secrets_ok;
1544    result.slug = (!result.ok).then_some(verify::VERIFY_FAILED_SLUG);
1545    result.backup = Some(report);
1546    result.secrets = Some(secrets);
1547    Ok(result)
1548}
1549
1550/// The secret-name check of `deploy verify --node` (D-13, V7): for each node
1551/// handed in (master first), one `secret_names` listing per provider the
1552/// render references, judged in [`SecretCheckMode::Verify`] so a provider the
1553/// render declares but a node lacks counts as `missing` (K8, verify side).
1554/// A listing that is refused for any reason other than "no provider" or
1555/// "cannot list" is an error: there is nothing to judge.
1556async fn verify_secret_names(
1557    declared: &Declared,
1558    master: Option<&dyn DeployApi>,
1559    backup: Option<&BackupNode<'_>>,
1560) -> Result<SecretNamesReport, CoreError> {
1561    let referenced = collect_referenced_secrets(&declared.outcome.tree);
1562    let declared_providers = declared_secret_providers(&declared.outcome.tree);
1563    let providers: BTreeSet<&str> = referenced.iter().map(|(p, _)| p.as_str()).collect();
1564    let mut nodes = Vec::with_capacity(2);
1565    if let Some(master) = master {
1566        let mut listings = BTreeMap::new();
1567        for provider in &providers {
1568            listings.insert(
1569                (*provider).to_string(),
1570                master.secret_names(provider).await?,
1571            );
1572        }
1573        nodes.push(check_secret_names(
1574            "master",
1575            &referenced,
1576            &listings,
1577            &declared_providers,
1578            SecretCheckMode::Verify,
1579        ));
1580    }
1581    if let Some(backup) = backup {
1582        let mut listings = BTreeMap::new();
1583        for provider in &providers {
1584            listings.insert(
1585                (*provider).to_string(),
1586                backup.secret_names(provider).await?,
1587            );
1588        }
1589        nodes.push(check_secret_names(
1590            "backup",
1591            &referenced,
1592            &listings,
1593            &declared_providers,
1594            SecretCheckMode::Verify,
1595        ));
1596    }
1597    Ok(SecretNamesReport::ordered(nodes))
1598}
1599
1600/// One look at the backup for `deploy verify`: its redundancy status, mode,
1601/// REST read-back and files. Every read that cannot be answered becomes an
1602/// `unreachable` check carrying the error code (never the gateway's words);
1603/// verify has no pre-apply sample, so the sync timestamp is recorded, not
1604/// compared. Returns the tick and the number of resources the read-back
1605/// compared.
1606///
1607/// The read-back is judged by the standalone rule with `master_status` (the
1608/// master verify's plan statuses, `--node both`), else with the statuses of
1609/// the master read the files check makes (`--node backup`), else strictly.
1610/// The files check therefore runs first; the report order and the warning
1611/// order (read-back, then files) are unchanged.
1612async fn verify_backup_tick(
1613    declared: &Declared,
1614    expected: &verify::Expected,
1615    master_reader: &dyn TreeReader,
1616    backup: &BackupNode<'_>,
1617    master_status: Option<&BTreeMap<String, PlanStatus>>,
1618    warnings: &mut Vec<String>,
1619) -> (TickOutcome, usize) {
1620    let signals = match backup.status().await {
1621        Ok(status) => Ok(BackupSignals::from_status(None, &status)),
1622        Err(error) => Err(error.code().to_string()),
1623    };
1624    let mode = match backup.deployment_mode().await {
1625        Ok(mode) => mode_backup_check(expected, mode.as_deref()),
1626        Err(error) => unreachable_check(BackupCheckId::Mode, error.code()),
1627    };
1628    let mut files_warnings = Vec::new();
1629    let (files, files_checked, files_status) = verify_backup_files(
1630        declared,
1631        expected,
1632        master_reader,
1633        backup,
1634        master_status.is_none(),
1635        &mut files_warnings,
1636    )
1637    .await;
1638    let file_status = master_status.or(files_status.as_ref());
1639    let judge = match file_status {
1640        Some(file_status) => verify::ReadBackJudge::Standalone { file_status },
1641        None => verify::ReadBackJudge::Strict,
1642    };
1643    let mut compared = 0;
1644    let read_back = match backup.read_back(expected).await {
1645        Ok(read_back) => {
1646            let (check, count) =
1647                read_back_backup_check_with(expected, &read_back, &judge, warnings);
1648            compared = count;
1649            check
1650        }
1651        Err(error) => unreachable_check(BackupCheckId::ReadBack, error.code()),
1652    };
1653    warnings.extend(files_warnings);
1654    (
1655        TickOutcome {
1656            signals,
1657            exhausted: false,
1658            content: Some(vec![mode, read_back, files]),
1659            files_checked,
1660            stable_ticks: 0,
1661        },
1662        compared,
1663    )
1664}
1665
1666/// The backup's files against the master's live copy of every rendered path.
1667/// Nothing was deleted by verify, so no path is expected absent. A rendered
1668/// unit either node cannot read fails the check as `unreadable`; any other
1669/// unreadable unit is a warning (quick-261029-ms3).
1670///
1671/// With `want_status`, the plan status of each declared non-tag resource is
1672/// also computed from the master read this check already makes, for the
1673/// backup's `read_back` judge; `None` when that read did not happen.
1674async fn verify_backup_files(
1675    declared: &Declared,
1676    expected: &verify::Expected,
1677    master_reader: &dyn TreeReader,
1678    backup: &BackupNode<'_>,
1679    want_status: bool,
1680    warnings: &mut Vec<String>,
1681) -> (BackupCheck, usize, Option<BTreeMap<String, PlanStatus>>) {
1682    let scope = Scope::from_manifest(
1683        &declared.manifest,
1684        &declared.target.mode,
1685        &declared.excluded_tag_providers(),
1686    );
1687    let backup_files = match backup.read_files(&scope).await {
1688        // A backup with no file transport cannot be asked.
1689        None => return (files_skipped_check(), 0, None),
1690        Some(Ok(observed)) => observed,
1691        Some(Err(error)) => {
1692            return (
1693                unreachable_check(BackupCheckId::Files, &format!("backup {}", error.code())),
1694                0,
1695                None,
1696            );
1697        }
1698    };
1699    let backup_unreadable = unreadable_units(&backup_files);
1700    let backup_files = effective_files(backup_files);
1701    let master_files = match master_reader.read(&scope).await {
1702        Ok(observed) => observed,
1703        Err(error) => {
1704            return (
1705                unreachable_check(BackupCheckId::Files, &format!("master {}", error.code())),
1706                0,
1707                None,
1708            );
1709        }
1710    };
1711    let status = want_status.then(|| {
1712        let excluded = declared.excluded_tag_providers();
1713        let planned = plan_with(
1714            &declared.outcome.tree,
1715            &master_files,
1716            None,
1717            &scope,
1718            &excluded,
1719            Redundancy::of(&declared.target),
1720        );
1721        resource_statuses(expected, &planned.entries)
1722    });
1723    let master_unreadable = unreadable_units(&master_files);
1724    let master_files = effective_files(master_files);
1725    let present: BTreeSet<_> = declared.outcome.tree.files.keys().cloned().collect();
1726    let absent = BTreeSet::new();
1727    let mut comparison = compare_files(&master_files, &backup_files, &present, &absent);
1728    exempt_unreadable(
1729        &mut comparison,
1730        &present,
1731        &absent,
1732        &[
1733            ("master", &master_unreadable),
1734            ("backup", &backup_unreadable),
1735        ],
1736    );
1737    warnings.extend(unreadable_warnings(&comparison));
1738    (files_check(&comparison), comparison.checked, status)
1739}
1740
1741/// Read each declared, comparable resource from the REST effective view:
1742/// one list per named type, one singleton read per singleton. An absent
1743/// resource (or a type whose route answers 404) is `Missing`; any other
1744/// refusal is an error.
1745pub(crate) async fn gather_read_back(
1746    deploy: &dyn DeployApi,
1747    expected: &verify::Expected,
1748) -> Result<BTreeMap<String, verify::Remote>, CoreError> {
1749    let classify = |record: crate::client::config_resources::ResourceRecord| {
1750        if record.collection.as_deref() == Some(verify::SYSTEM_COLLECTION) {
1751            verify::Remote::System
1752        } else {
1753            verify::Remote::Config(record.config)
1754        }
1755    };
1756    let mut lists: BTreeMap<(String, String), Option<Vec<_>>> = BTreeMap::new();
1757    let mut out = BTreeMap::new();
1758    for res in &expected.resources {
1759        let remote = match &res.name {
1760            None => match deploy.resource_singleton(&res.module, &res.ty, None).await {
1761                Ok(record) => classify(record),
1762                Err(CoreError::NotFound { .. }) => verify::Remote::Missing,
1763                Err(e) => return Err(e),
1764            },
1765            Some(name) => {
1766                let type_key = (res.module.clone(), res.ty.clone());
1767                if !lists.contains_key(&type_key) {
1768                    let items = match deploy
1769                        .resource_list(&res.module, &res.ty, &ListQuery::default())
1770                        .await
1771                    {
1772                        Ok(envelope) => Some(envelope.items),
1773                        Err(CoreError::NotFound { .. }) => None,
1774                        Err(e) => return Err(e),
1775                    };
1776                    lists.insert(type_key.clone(), items);
1777                }
1778                lists
1779                    .get(&type_key)
1780                    .and_then(Option::as_ref)
1781                    .and_then(|items| items.iter().find(|r| r.name.as_deref() == Some(name)))
1782                    .map_or(verify::Remote::Missing, |r| classify(r.clone()))
1783            }
1784        };
1785        out.insert(res.key.clone(), remote);
1786    }
1787    Ok(out)
1788}
1789
1790/// Wait until an empty answer from the log means something (CV1: a line is
1791/// readable up to about 1.5 s after the scan completed). The wait is skipped
1792/// when the gateway has logged anything stamped after the settle point.
1793async fn settle_logs(
1794    gateway: &dyn GatewayApi,
1795    scan: &ScanStatus,
1796    scan_read_at: std::time::Instant,
1797    settle: Duration,
1798) -> Result<(), CoreError> {
1799    if settle.is_zero() {
1800        return Ok(());
1801    }
1802    let settle_ms = i64::try_from(settle.as_millis()).unwrap_or(i64::MAX);
1803    let probe = LogQuery {
1804        start_time: Some(scan.last_scan_timestamp.saturating_add(settle_ms)),
1805        limit: 1,
1806        ..LogQuery::default()
1807    };
1808    match gateway.logs(&probe).await {
1809        Ok(envelope) if !envelope.items.is_empty() => return Ok(()),
1810        Err(e @ CoreError::Network { .. }) => return Err(e),
1811        // An unreadable log is reported by the real read that follows.
1812        Ok(_) | Err(_) => {}
1813    }
1814    tokio::time::sleep(settle.saturating_sub(scan_read_at.elapsed())).await;
1815    Ok(())
1816}
1817
1818/// The WARN-or-higher log lines since `start_time` (CV1: no `logger` filter,
1819/// no `endTime`; matched client-side). The inner `Err` is the slug of why
1820/// they could not be read.
1821async fn read_logs(
1822    gateway: &dyn GatewayApi,
1823    start_time: i64,
1824) -> Result<Result<Vec<verify::LogLine>, String>, CoreError> {
1825    let mut lines = Vec::new();
1826    let mut offset = 0i64;
1827    for _ in 0..LOG_PAGES_MAX {
1828        let query = LogQuery {
1829            start_time: Some(start_time),
1830            min_level: Some("WARN".to_string()),
1831            limit: DEFAULT_LOG_LIMIT,
1832            offset,
1833            ..LogQuery::default()
1834        };
1835        match gateway.logs(&query).await {
1836            Ok(envelope) => {
1837                let count = i64::try_from(envelope.items.len()).unwrap_or(i64::MAX);
1838                lines.extend(envelope.items.into_iter().map(|e| verify::LogLine {
1839                    timestamp: e.timestamp,
1840                    logger: e.logger_name,
1841                    message: e.message,
1842                }));
1843                if count < DEFAULT_LOG_LIMIT {
1844                    return Ok(Ok(lines));
1845                }
1846                offset += count;
1847            }
1848            Err(e @ CoreError::Network { .. }) => return Err(e),
1849            Err(e) => return Ok(Err(e.code().to_string())),
1850        }
1851    }
1852    Ok(Err("logs_truncated".to_string()))
1853}
1854
1855/// Read `paths` through the WebDev `tags` route every `probe_interval` until
1856/// every one is Good or `probe_deadline` passed; the last reading is judged.
1857/// Values are dropped at once. The inner `Err` is the slug of why the route
1858/// could not be read.
1859async fn poll_probes(
1860    gateway: &dyn GatewayApi,
1861    project: &str,
1862    paths: &[String],
1863    opts: &VerifyOptions,
1864) -> Result<Result<Vec<verify::ProbeReading>, String>, CoreError> {
1865    let interval = opts.probe_interval.max(Duration::from_millis(1));
1866    let started = tokio::time::Instant::now();
1867    loop {
1868        let readings: Vec<verify::ProbeReading> =
1869            match crate::actions::tags::tags_read(gateway, project, paths).await {
1870                Ok(result) => result
1871                    .results
1872                    .into_iter()
1873                    .enumerate()
1874                    .map(|(i, row)| verify::ProbeReading {
1875                        // A row that does not echo its path is the one asked for.
1876                        path: if row.path.is_empty() {
1877                            paths.get(i).cloned().unwrap_or_default()
1878                        } else {
1879                            row.path
1880                        },
1881                        quality: row.quality,
1882                    })
1883                    .collect(),
1884                Err(e @ CoreError::Network { .. }) => return Err(e),
1885                Err(e) => return Ok(Err(e.code().to_string())),
1886            };
1887        let all_good = paths.iter().all(|path| {
1888            readings
1889                .iter()
1890                .any(|r| &r.path == path && r.quality.starts_with("Good"))
1891        });
1892        if all_good || started.elapsed() + interval > opts.probe_deadline {
1893            return Ok(Ok(readings));
1894        }
1895        tokio::time::sleep(interval).await;
1896    }
1897}
1898
1899#[cfg(test)]
1900mod tests {
1901    use super::*;
1902
1903    /// A repo whose `gateways/g/resources/` holds one file the pull replaces.
1904    fn repo_with_existing() -> (tempfile::TempDir, Existing) {
1905        let repo = tempfile::TempDir::new().expect("repo");
1906        let dir = repo.path().join("gateways/g/resources/ignition/x");
1907        std::fs::create_dir_all(&dir).expect("dirs");
1908        std::fs::write(dir.join("keep.json"), b"{\"keep\": true}").expect("file");
1909        let existing = Existing {
1910            files: vec!["resources/ignition/x/keep.json".to_string()],
1911            dirs: ["resources", "resources/ignition", "resources/ignition/x"]
1912                .iter()
1913                .map(|d| d.to_string())
1914                .collect(),
1915        };
1916        (repo, existing)
1917    }
1918
1919    fn snapshot(root: &Path) -> BTreeMap<PathBuf, Vec<u8>> {
1920        fn walk(dir: &Path, out: &mut BTreeMap<PathBuf, Vec<u8>>) {
1921            for entry in std::fs::read_dir(dir).expect("read_dir") {
1922                let path = entry.expect("entry").path();
1923                if path.is_dir() {
1924                    out.insert(path.clone(), Vec::new());
1925                    walk(&path, out);
1926                } else {
1927                    out.insert(path.clone(), std::fs::read(&path).expect("read"));
1928                }
1929            }
1930        }
1931        let mut out = BTreeMap::new();
1932        walk(root, &mut out);
1933        out
1934    }
1935
1936    /// CR-01: a pulled tree that cannot be written is refused before the first
1937    /// delete, so the existing tree is left exactly as it was.
1938    fn assert_refused_untouched(files: &[(&str, &str)], needle: &str) {
1939        let (repo, existing) = repo_with_existing();
1940        let before = snapshot(repo.path());
1941        let source = SourceTree::from_files(files.iter().copied()).expect("source");
1942        let err =
1943            write_pulled(repo.path(), "g", Some(&existing), &source, None).expect_err("refused");
1944        assert_eq!(err.code(), "deploy_tree_invalid", "{err}");
1945        assert!(err.to_string().contains(needle), "{needle}: {err}");
1946        assert_eq!(
1947            snapshot(repo.path()),
1948            before,
1949            "a refused pull must not delete the existing tree"
1950        );
1951    }
1952
1953    #[test]
1954    fn unsafe_component_is_refused_before_any_delete() {
1955        assert_refused_untouched(
1956            &[
1957                ("resources/ignition/x/fine.json", "{}"),
1958                ("resources/ignition/x/bad\nname.json", "{}"),
1959            ],
1960            "unsafe path component",
1961        );
1962    }
1963
1964    #[test]
1965    fn case_fold_collision_is_refused_before_any_delete() {
1966        assert_refused_untouched(
1967            &[
1968                ("resources/ignition/x/Foo.json", "{}"),
1969                ("resources/ignition/x/foo.json", "{}"),
1970            ],
1971            "case is folded",
1972        );
1973    }
1974
1975    #[test]
1976    fn file_and_directory_clash_is_refused_before_any_delete() {
1977        assert_refused_untouched(
1978            &[
1979                ("resources/ignition/x", "{}"),
1980                ("resources/ignition/x/a.json", "{}"),
1981            ],
1982            "both a file and a directory",
1983        );
1984    }
1985
1986    #[test]
1987    fn a_valid_tree_replaces_the_existing_one() {
1988        let (repo, existing) = repo_with_existing();
1989        let source =
1990            SourceTree::from_files([("resources/ignition/x/new.json", "{}")]).expect("source");
1991        write_pulled(repo.path(), "g", Some(&existing), &source, None).expect("written");
1992        let dir = repo.path().join("gateways/g/resources/ignition/x");
1993        assert!(!dir.join("keep.json").exists());
1994        assert!(dir.join("new.json").is_file());
1995    }
1996}