cloud/reconciler/mod.rs
1//! Reconciler abstraction — bring a workload up against a mirror's
2//! provider slots.
3//!
4//! A reconciler is the kind-specific code that knows how to deploy one
5//! workload kind (`mesofact-static`, `container`, future `almanac`, …) to a
6//! mirror. Selection: a [`ServiceComponent`](crate::ServiceComponent)'s
7//! `kind` field picks which reconciler runs; the reconciler then dispatches
8//! on the mirror's provider slot (e.g. `mesofact-static` →
9//! `providers.static` slot → `miniflare-native` inline or `cloudflare` ref).
10//!
11//! [`MesofactStaticReconciler`] serves all four tiers through one door — the
12//! compiled Worker bundle — and varies only the object store beneath it:
13//! the `yah-s3-fs` driver at dev ([`dev_door`]), MinIO at pond ([`pond`]),
14//! R2 at cloud and ha. Which one a mirror binds is declared in
15//! `[drivers.s3]`, never branched on (W265, R584-F4).
16//!
17//!
18//! @yah:ticket(R419-F2, "Implement CloudflareWorkerReconciler (kind=cloudflare-worker)")
19//! @yah:assignee(agent:claude)
20//! @yah:at(2026-06-03T08:02:49Z)
21//! @yah:status(review)
22//! @yah:parent(R419)
23//! @yah:handoff("Landed CloudflareWorkerReconciler in crates/yah/cloud/src/reconciler/cloudflare_worker.rs and re-exported it through reconciler/mod.rs + cloud/src/lib.rs. up() validates the registry slot (must be `use = \"cloudflare\"` with non-empty `zone` + `domain`), reads workload.toml's `[build]` + `[[bindings]]`, matches each binding against a sibling mirror slot by `binding` field, runs build, reads the bundled entrypoint (default dist/index.js), idempotently lists+creates R2 buckets, deploys via deploy_worker_script with WorkerBinding::R2Bucket entries (R419-F1 surface), then attaches the custom domain via the new upsert_worker_custom_domain method on CloudflareClient. Returns RunningWorkload::adopted with public_url=https://<domain>. All config validation runs BEFORE any CF API call (R330-B5 fail-fast discipline).")
24//! @yah:handoff("Added CloudflareClient::upsert_worker_custom_domain (cloudflare.rs) — separate from upsert_worker_route because Worker Routes are zone-scoped pattern matches and Custom Domains are account-scoped hostname attachments. List-first idempotency: skips PUT when the (hostname, service, zone_id) tuple is already bound.")
25//! @yah:verify("cargo check -p cloud --lib — clean")
26//! @yah:depends_on(R419-F1)
27//!
28//! @yah:ticket(R419-F3, "Register cloudflare-worker reconciler in CLI + desktop dispatch")
29//! @yah:assignee(agent:claude)
30//! @yah:at(2026-06-03T08:02:58Z)
31//! @yah:status(review)
32//! @yah:parent(R419)
33//! @yah:handoff("Added match arm `\"cloudflare-worker\" => CloudflareWorkerReconciler::new().up(ctx)` in both dispatchers: app/yah/cli/src/cloud.rs:reconcile_component and app/yah/desktop/src/mirror_run.rs's component.kind.as_str() match. Imported CloudflareWorkerReconciler at the top of each file. Updated the desktop file-level docstring to list the new kind. Pre-existing fallback arm still produces a clean error for unknown kinds.")
34//! @yah:verify("cargo check -p cloud --lib — clean")
35//! @yah:verify("cargo check -p yah --lib --bins — clean (warnings unchanged from baseline)")
36//! @yah:verify("cargo check -p desktop --lib — clean (warnings unchanged from baseline)")
37//! @yah:depends_on(R419-F2)
38//!
39//! @yah:ticket(R419-F4, "Regression tests: misconfig fail-fast for cloudflare-worker reconciler")
40//! @yah:assignee(agent:claude)
41//! @yah:at(2026-06-03T08:03:09Z)
42//! @yah:status(review)
43//! @yah:parent(R419)
44//! @yah:handoff("Three fail-fast tests in reconciler::cloudflare_worker::tests — Fixture builds an in-tempdir yah-cr-shaped workspace (writes workload.toml + .yah/infra/providers/cloudflare.toml). up_bails_on_registry_missing_domain (case 3) drops domain off the registry slot. up_bails_on_binding_name_drift (case 2) puts binding=\"STORAGE\" in the cache slot while workload.toml binds CACHE. up_bails_on_cache_slot_missing_bucket (case 1) keeps binding=\"CACHE\" but omits bucket. Each test asserts the error message names the offending field, the slot role, the service, and the env — no CF HTTP call is made because validation runs before any client construction.")
45//! @yah:verify("cargo test -p cloud --lib reconciler::cloudflare_worker — 3 passed")
46//! @yah:verify("cargo test -p cloud --lib — 246 passed (1 pre-existing failure cloud_init::tests::embedded_template_matches_workspace_canonical is unrelated R092-F2 template drift, not caused by R419)")
47//! @yah:depends_on(R419-F2)
48//!
49//! @yah:relay(R458, "Cloud reconciler for .yah/domains/*.toml — R2 custom-domain shape")
50//! @yah:at(2026-06-05T08:40:57Z)
51//! @yah:status(open)
52//! @yah:next("F1: implement ensure_r2_custom_domain (mirror of ensure_r2_bucket) + wire into yah cloud apply as a post-services pass. Scope: domains with cdn_bucket set and no [[routes]] (today: cdn-yah-dev.toml). Worker-routed shape (yah-dev, app-yah-dev) is a separate surface.")
53//! @arch:see(.yah/domains/cdn-yah-dev.toml)
54//!
55//! @yah:ticket(R458-F1, "ensure_r2_custom_domain (CF API) + apply-time orchestration")
56//! @yah:at(2026-06-05T08:41:07Z)
57//! @yah:status(review)
58//! @yah:assignee(agent:claude)
59//! @yah:parent(R458)
60//! @arch:see(.yah/domains/cdn-yah-dev.toml)
61//! @yah:next("R458 can be archived once F1 is signed off, unless we want to keep it open for the routed-domain (Worker) reconciler shape — that's a much larger surface (DNS + Worker route management + bundle deploy) than this F1's bucket-binding.")
62//! @yah:handoff("Live-verified end-to-end. cdn.yah.dev now resolves to CF anycast IPs (104.21.43.100, 172.67.178.4) and curl HTTP/2 200s against https://cdn.yah.dev/yah-desktop/whisper/distil-large-v3-q5_1.bin (content-length 584567555 = the q5_1 bytes R422-F11 published). Second apply is idempotent — the list-first path skips the POST when the binding is already present. Files touched: (1) crates/yah/cloud/src/provider/cloudflare.rs — new R2CustomDomain output type + CloudflareClient::list_r2_custom_domains and CloudflareClient::add_r2_custom_domain methods (GET / POST /accounts/{id}/r2/buckets/{bucket}/domains/custom). The POST body needs zone_id even though the endpoint is bucket-scoped (CF rejects with 'JSON not well formed' otherwise — caught live and added on the second iteration). Re-exported through provider/mod.rs + cloud/src/lib.rs. (2) crates/yah/cloud/src/reconciler/domain.rs — new module with ensure_r2_custom_domain(account_id, bucket, domain) mirroring static_asset::ensure_r2_bucket's list-first idempotency. Resolves the parent zone id via the existing CloudflareClient::zone_id_for_name and a parent_zone_name(domain) heuristic that takes the last two labels (correct for every yah-owned zone today; the doc comment names the longest-suffix-match upgrade path for future three-label-apex zones). 3 unit tests cover apex / subdomain / deeper-subdomain. (3) crates/yah/cloud/src/reconciler/mod.rs — declared `pub mod domain` + re-exported ensure_r2_custom_domain. (4) app/yah/cli/src/cloud.rs — new DomainOutcome enum + a post-services domain pass in handle_apply that walks cfg.domains, dispatches the R2-custom-domain shape (cdn_bucket set, no [[routes]]), and routes routed-shape domains (yah-dev, app-yah-dev) into a Skipped row labelled 'has [[routes]] — Worker-routed shape'. Gated on a cloudflare provider being declared (pond-only setups print 'skip domain pass: no cloudflare provider declared'). Originally also gated on empty --service filter; that gate dropped on review — domains are workspace-scoped and the operator wants them reconciled even when narrowing services. New print_domain_summary mirrors print_apply_summary's table + JSON output. Required CF token scopes (verified live): Workers R2 Storage: Edit + Zone: Read. The existing cloudflare-api-token slot carries both.\n\nLive verification command + transcript:\n\n $ ./target/debug/yah cloud apply --env cloud --service yah-desktop\n ==> yah-desktop/cloud: reconciling 2 component(s)\n component desktop (kind=binary)\n component whisper-models (kind=static-asset)\n ==> domain cdn-yah-dev (cdn.yah.dev): ensuring R2 custom-domain binding on bucket yah-dev\n apply summary (cloud):\n yah-desktop ok 2 component(s) reconciled\n domain summary (cloud):\n cdn-yah-dev ok R2 custom domain bound\n $ dig +short cdn.yah.dev\n 104.21.43.100\n 172.67.178.4\n $ curl -sI https://cdn.yah.dev/yah-desktop/whisper/distil-large-v3-q5_1.bin | head -4\n HTTP/2 200\n content-length: 584567555\n\nUnblocks R422-T13's client-side `cdn_fallback = \"https://cdn.yah.dev/yah-desktop/whisper/{blake3}\"` — the URL now actually resolves and serves the bytes.")
63//! @yah:verify("cargo test -p cloud --lib reconciler::domain --locked # 3 pass")
64//! @yah:verify("cargo check --workspace --locked # clean")
65//! @yah:verify("./target/debug/yah cloud apply --env cloud --service yah-desktop # domain summary shows cdn-yah-dev=ok, yah-dev/app-yah-dev=skipped (routed)")
66//! @yah:verify("curl -sI https://cdn.yah.dev/yah-desktop/whisper/distil-large-v3-q5_1.bin # HTTP/2 200, content-length 584567555")
67//!
68//! @yah:ticket(R870-B25, "The cloud-init template drift guard is vacuously green, and the canonical mirror.yml it should guard is 128 lines stale")
69//! @yah:status(review)
70//! @yah:at(2026-09-11T00:24:58Z)
71//! @yah:assignee(agent:bundle-anthropic-ashguard)
72//! @yah:parent(R870)
73//! @yah:severity(high)
74//! @yah:next("OPERATIONAL QUESTION THIS RAISES, worth answering separately from the code fix: which is the provisioning path actually used, the embedded template or the repo-root canonical one? If any node was provisioned from the canonical copy since R858-F17 landed, it has no turso-backup helpers and its durability-declaring workloads will refuse to deploy. `rendered_runcmd_entries_are_all_strings` is the gate that genuinely catches the colon-space footgun and IS green — this ticket is about the twin-file guard beside it, not that one.")
75//! @yah:verify("The drift guard must FAIL on today's tree (proving it now compares something real), then pass once the canonical `.yah/infra/cloud-init/mirror.yml` is brought level with the embedded template. Asserting only the post-fix green is the weak form — it passes for the same vacuous reason it does today.")
76//! @yah:gotcha("Found by independent verification of R870-F23 phase 2 (@Ashguard:dove, session:60d4f41f), confirming a claim from @Ashguard:blade's implementation pass. NOT caused by R870-F23 — pre-existing, and F23's own change is green and unaffected. Do not read this as a phase-2 regression.")
77//! @yah:next("THE DEFECT, read not inferred. `embedded_template_matches_workspace_canonical` exists to prove the mirror.yml compiled into the binary matches the canonical copy on disk. It resolves the workspace root by walking `CARGO_MANIFEST_DIR.ancestors()`, which lands on `oss/yubaba` — an independent Cargo workspace whose `.yah/` holds only a `.gitignore`. The canonical path therefore does not exist, the test takes its `canonical_path.exists()` bootstrap branch, and asserts NOTHING. It has been green for that reason, not because the files agree.")
78//! @yah:next("WHAT THE GUARD IS MISSING, measured: the repo-root `.yah/infra/cloud-init/mirror.yml` is 128 diff-lines behind `oss/yubaba/crates/cloud/templates/mirror.yml`. It is missing the ENTIRE R858-F17 turso-backup block, and the YAML-quoting fix R870-F23 landed in the embedded copy (the two runcmd entries whose bare `: ` made cloud-init parse them as a Mapping and skip them). THE FIX IS TWO PARTS AND THE ORDER MATTERS: first make the guard non-vacuous — resolve the canonical path against the REPO root rather than the enclosing cargo workspace, or fail loudly when it cannot be found, so the bootstrap branch can no longer swallow a real absence. Then reconcile the two files. Doing only the second leaves the guard still asleep for the next drift.")
79//! @yah:handoff("GUARD MADE NON-VACUOUS FIRST, then the files reconciled, and the intermediate RED was observed. New CanonicalHome enum + locate_canonical_home() in oss/yubaba/crates/cloud/src/cloud_init.rs (non-test code, since it is a real property of the layout): the ancestor walk is keyed on `.yah/infra/` instead of `.yah/`. The monorepo root has it; oss/yubaba, whose own .yah/ holds nothing but a .gitignore, does not, and neither does the standalone export where the repo root genuinely IS oss/yubaba. embedded_template_matches_workspace_canonical now matches on that: Monorepo(root) means the canonical file is REQUIRED (missing/unreadable panics with a message naming the path and saying provisioning reads it); StandaloneExport is the only branch allowed to skip. The old `if canonical_path.exists()` bootstrap branch that swallowed a real absence is gone.")
80//! @yah:handoff("THE RED, run after step 1 and before step 2 exactly as the ticket asked: `cargo test -p yah-cloud --lib cloud_init::tests` from oss/yubaba gave 29 passed / 1 FAILED, the single failure being embedded_template_matches_workspace_canonical asserting on /Users/leif/ss/yah/.yah/infra/cloud-init/mirror.yml with the full 128-line diff in its message. That is the proof it now compares something real; the post-fix green on its own would have been indistinguishable from today's vacuous green.")
81//! @yah:handoff("THE RECONCILE WAS NOT A ONE-WAY COPY, and this is the part worth reading. The canonical copy carried a DELIBERATE CORRECTION the embedded template never received: commit 45e39f0a (2026-08-03) changed `systemctl enable --now yubaba.slice` to `systemctl start yubaba.slice`, plus the comment explaining why - app/yah/cli/resources/yubaba.slice has no [Install] section, and systemctl enable on such a unit fails. Blindly overwriting canonical with embedded (which the ticket text reads like) would have regressed that fix into the file provisioning actually ships. So the fix went BOTH ways: the slice correction was applied to oss/yubaba/crates/cloud/templates/mirror.yml first, then that file was copied over .yah/infra/cloud-init/mirror.yml. The two are now byte-identical at 237 lines; canonical gained the whole R858-F17 turso-backup block, the R870-F23 YAML double-quoting of the two colon-space runcmd entries, the litestream 0.3.13 install, and the headscale 0.23.0 + litestream-headscale.service pre-stage.")
82//! @yah:handoff("WHICH TEMPLATE PROVISIONING ACTUALLY USES - THE CANONICAL ON-DISK ONE, read not guessed. cloud_init::load_template() returns the on-disk <workspace_root>/.yah/infra/cloud-init/mirror.yml whenever it exists and falls back to the include_str! embedded copy only when it does not. provision::build_request() (oss/yubaba/crates/cloud/src/provision.rs:88) is its sole non-test caller, and its sole caller in turn is handle_provision at app/yah/cli/src/cloud.rs:14524, whose workspace_root is the camp repo root - which has the file. So the embedded template is the FALLBACK, not the live path, and the stale copy is the one every `yah cloud machine provision` has been shipping. load_template's doc comment now says so.")
83//! @yah:handoff("FLEET CONSEQUENCE, dated from git so the window is bounded. NO node was touched - code, templates and tests only, per scope. Canonical was last updated 2026-08-03 (45e39f0a). The headscale 0.23.0 pre-stage, the litestream install and litestream-headscale.service staging landed in the EMBEDDED copy only on 2026-09-05 (4bed91fe), so any node provisioned between 2026-09-05 and today silently received none of them - that is the real damage this drift did. The turso-backup block landed in the embedded copy only TODAY (a8f0d501, 2026-09-10), so no node has ever received the durability helpers from either file; that gap is fleet-wide and predates this ticket rather than being caused by the drift. Remediating existing nodes is deliberately NOT done here.")
84//! @yah:handoff("DISCOVERED WORK, fixed in this pass: the SECOND stale twin is .yah/infra/cloud-init/stand-up-yubaba.sh, mirror.yml's documented SSH transcription for LAN nodes (W257 step 6). It installed yubaba/kamaji/units from the release tarball but never the turso-backup helpers or the KAMAJI_HYDRATE_HELPER/KAMAJI_TAIL_HELPER drop-in, so every LAN node stood up by it would refuse any durability-declaring workload. Added a present-checked, non-fatal block in the script's own idiom ($SUDO install from $D, tee for the drop-in, a WARNING to stderr when the tarball predates R858-F17), using Environment= in a drop-in rather than ExecStart= flags for the reason kamaji.service's own comment gives. Verified against scripts/publish-yubaba-release.sh:326-327, which hard-asserts both binaries at exactly $STAGE_NAME/turso-backup-{hydrate,tail} - the path the script looks in. bash -n clean.")
85//! @yah:verify("cargo test -p yah-cloud --lib, run from oss/yubaba (NOT the repo root - yah-cloud is not a root workspace member and needs dev-dependencies): 1166 passed / 0 failed / 4 ignored, against the 1163/0/4 baseline. The +3 are exactly the three tests added here; nothing regressed.")
86//! @yah:verify("INTERMEDIATE RED, the verification this ticket actually asked for: after step 1 and before step 2, cargo test -p yah-cloud --lib cloud_init::tests = 29 passed / 1 failed, the single failure being embedded_template_matches_workspace_canonical naming .yah/infra/cloud-init/mirror.yml. Green afterwards.")
87//! @yah:verify("Three new tests pin the guard against going vacuous again. locate_canonical_home_finds_monorepo_root_not_the_inner_workspace and locate_canonical_home_reports_standalone_export build both tree shapes in tempdirs (the monorepo one reproduces the inner oss/yubaba/.yah/.gitignore that caused the original miss). drift_guard_cannot_go_vacuous_in_the_monorepo keys off a signal INDEPENDENT of the .yah/infra/ marker - `git subtree split --prefix=oss/yubaba` strips that prefix, so a CARGO_MANIFEST_DIR still ending in oss/yubaba/crates/cloud proves we are in the monorepo - and panics if the guard resolved StandaloneExport there.")
88//! @yah:verify("diff -u between the two mirror.yml copies is empty; both are 237 lines.")
89//! @yah:verify("bash -n .yah/infra/cloud-init/stand-up-yubaba.sh clean.")
90//! @yah:verify("rendered_runcmd_entries_are_all_strings left untouched and still green, as instructed - a different test and a different gate.")
91//! @yah:gotcha("NOT FIXED, and deliberately left as an operator call rather than decided silently: stand-up-yubaba.sh still lacks the litestream 0.3.13 install and the headscale 0.23.0 / litestream-headscale.service pre-stage that mirror.yml now carries. Those exist because R858-T4 made EVERY node a coordinator candidate, and whether the LAN/appliance class (us-west-01x, mostly no-voter) belongs in that candidate set is a fleet-topology decision, not a transcription gap. The durability helpers WERE added because their consequence is unconditional - kamaji refuses the workload on any node - while these two only matter if the node can ever own the ingress role.")
92//! @yah:gotcha("Uncommitted: this camp's git policy is `defer`, so the four changed files (oss/yubaba/crates/cloud/src/cloud_init.rs, both mirror.yml copies, .yah/infra/cloud-init/stand-up-yubaba.sh) are in the working tree for the camp's git sweep. No commit SHA to cite.")
93//! @yah:verify("INDEPENDENTLY VERIFIED BY A SECOND COURIER (session:67d56cd3) who did not implement it. GUARD IS GENUINELY HONEST, confirmed by reading the branches: `locate_canonical_home` (cloud_init.rs:208) walks ancestors for `.yah/infra/`, and the Monorepo branch (cloud_init.rs:1013-1029) `read_to_string`-panics on a missing or unreadable canonical and `assert_eq`s full trimmed content against `DEFAULT_TEMPLATE` — so BOTH a deleted canonical AND a revert to its pre-fix state now fail hard. No skip branch survives. THE STANDALONE-EXPORT DISCRIMINATOR HOLDS: `.yah/infra/` exists only at the monorepo root and nowhere under `oss/` (oss/yubaba/.yah holds one tracked .gitignore), so the real subtree-split export takes the StandaloneExport branch legitimately; the only route to that branch from inside the monorepo is deleting `.yah/infra/`, which `drift_guard_cannot_go_vacuous_in_the_monorepo` catches off an INDEPENDENT path-suffix signal. Two residual holes, both benign in DIRECTION: a standalone clone placed under some `.yah/infra/` ancestor false-FAILS rather than skipping, and renaming the oss/yubaba path would disarm only the meta-guard, not the drift guard.")
94//! @yah:verify("THE RECONCILIATION WAS NOT A ONE-WAY COPY, and that was checked rather than taken on trust — a blind embedded-to-canonical copy would have silently destroyed someone else's fix. Both copies are byte-identical at 237 lines, sha256 6bc46731e030bae8a1970bcb06cf3132323eb454ab72496d6ef9b2c6177585af. At HEAD the CANONICAL carried the 45e39f0a fix (`systemctl start yubaba.slice`, since the slice has no [Install] section) while the EMBEDDED still had `enable --now`; the working diff applies that exact hunk embedded-ward (+5/-3) against the canonical's +109/-1, proving the flow went both directions. No `enable --now yubaba.slice` remains in either file. The canonical's single deleted line was only the `chmod 0644` superseded by the version adding litestream-headscale.service. R858-F17's turso-backup block and R870-F23's two double-quoted runcmd entries are present in both, necessarily so given byte-identity. THE INTERMEDIATE RED WAS STRUCTURALLY NECESSARY, not stage-managed: the Monorepo branch compares entire trimmed contents and the two HEAD copies differed by 112 insertions / 6 deletions, so the assert could not have passed. `cloud_init::tests` is 30 tests, making the reported 29-pass/1-fail arithmetically consistent. (The \"128-line diff\" figure is 118 changed lines by numstat — cosmetic, not a defect.) COUNTS: `cargo test -p yah-cloud --lib` from oss/yubaba = 1166 passed / 0 failed / 4 ignored, the +3 over baseline being exactly the three new tests.")
95//! @yah:verify("THE FLEET FACT, CONFIRMED WITH ONE DATE CORRECTED — this is the operationally consequential part and the correction matters. PROVISIONING READS THE CANONICAL ON-DISK COPY, not the embedded one: provision.rs:88 is `cloud_init::load_template(workspace_root)`, and `load_template` (cloud_init.rs:225-232) PREFERS `.yah/infra/cloud-init/mirror.yml`, falling back to the embedded copy only when absent; sole non-test caller chain is cloud.rs:14523 with the camp repo root. So every node was provisioned from the file that was 128 lines stale. CONSEQUENCE 1, date corrected: the headscale/litestream pre-stage entered the EMBEDDED copy on 2026-09-04 in b20a1e09 — NOT 2026-09-05, which was 4bed91fe merely refining it — and the canonical never carried it, so every node provisioned since 2026-09-04 missed that pre-stage. CONSEQUENCE 2 HOLDS AS STATED: no node has ever had the turso-backup helpers. `turso-backup-hydrate` first entered the embedded template today in a8f0d501 and the canonical only in this change; grep finds no other install path (only publish-yubaba-release.sh, kamaji's consumers, both mirror.yml copies, stand-up-yubaba.sh), and `.yah/infra/machines/us-west-001.toml:42` independently records \"NO prod node has them today\". NOT VERIFIED, stated rather than smoothed: no node was touched, so the actual SET of nodes provisioned since 2026-09-04 is unconfirmed.")
96//! @yah:handoff("THIRD TWIN CLOSED — .yah/infra/cloud-init/stand-up-yubaba.sh is now both LEVEL and GUARDED. GUARD SHAPE CHOSEN: the assertion test, not generate-from-one-source, and the reason is that the twin is not a pure transcription. The script deliberately diverges from mirror.yml in ways that are CORRECT and load-bearing (enable+restart instead of `enable --now`, which is the only reason it can call itself idempotent — see its own :162-176 comment measured on us-west-013/014; write-if-absent journald ceiling so a Pi's tighter 200M bound wins; cluster-KEK install; loopback bind for no-mesh nodes; status block reading /health rather than the on-disk --version). A generator would therefore have to model the divergences, which is the whole difficulty, and mirror.yml is itself a static `{{ }}`-substituted file rather than a Rust-rendered one — so single-sourcing would mean either making mirror.yml generated (large) or parsing YAML to emit bash (fragile). Not contained; see @yah:next for what it would actually take.")
97//! @yah:handoff("THE GUARD DERIVES ITS PINS FROM THE TEMPLATE rather than restating them, which is what stops it becoming the next thing that rots. `stand_up_script_carries_the_templates_install_steps` (oss/yubaba/crates/cloud/src/cloud_init.rs, next to the mirror.yml drift guard) reads the script via the new `paths::stand_up_script`, reusing `locate_canonical_home` so StandaloneExport is the same single permitted skip. Three assertion families: (1) STAND_UP_TWIN_ANCHORS, an explicit list of artifacts a node must end up carrying, each asserted against DEFAULT_TEMPLATE AS WELL so a stale anchor goes red on the template side instead of over-constraining the script; (2) every 64-char lowercase-hex sha256 pin extracted from DEFAULT_TEMPLATE must appear in the script (the four litestream/headscale amd64+arm64 checksums; `{{YAH_YUBABA_SHA256}}` is not hex so it is not picked up, and the extractor asserts it found >=4 so a broken extractor cannot make the guard vacuous); (3) every `https://github.com/OWNER/REPO/releases/download/TAG/` prefix in the template must appear in the script — owner/repo/tag pinned, arch-templated asset filename left free. Net effect: bumping litestream or headscale in mirror.yml alone now turns this red.")
98//! @yah:handoff("THREE REDS DEMONSTRATED, not one, because the anchor half fires first and would have masked the other two. (a) Test written BEFORE the script fix: `cargo test -p yah-cloud --lib cloud_init::tests::stand_up_script` = 0 passed / 1 FAILED, naming `no litestream-headscale.service`. (b) With the script level, one hex char mutated in the script's headscale arm64 HS_SHA: FAILED, naming the missing pin 99fa9b29...e9fe. (c) The litestream URL tag moved to v0.3.14 while the template stays v0.3.13: FAILED, naming the unfetched .../download/v0.3.13/. Both mutations were reverted by hand and re-verified. SCRIPT CONTENT ADDED, in the script's own idiom: present-checked non-fatal `$SUDO install -m0644 \"$D/litestream-headscale.service\"` alongside the other three units (verified it really ships in the tarball — publish-yubaba-release.sh:287 stages it from $RESOURCES); an arch-cased litestream 0.3.13 fetch+sha256+`tar -C /usr/local/bin`; an arch-cased headscale 0.23.0 fetch+sha256+`install -m0755` into /var/lib/yah-cloud/headscale/headscale. Every failure path is a stderr WARNING naming the operational consequence, never an exit — a node without these cannot hold the ingress role, which is not a failed stand-up.")
99//! @yah:handoff("DELIBERATE CHOICE WORTH REVIEWING: the two downloads are UNCONDITIONAL, not present-checked-skip. A re-run of this script is the documented upgrade path (W257 §8), and `if [ -x /usr/local/bin/litestream ]; then skip` would recreate exactly the stale-binary trap the script's own `restart`-not-`--now` comment was written for after us-west-013/014. Cost is re-downloading ~10MB litestream + ~51MB headscale on every re-run; both are sha-pinned so the repeat is idempotent, just not free. A version-checked skip was rejected because it would depend on `headscale version` / `litestream version` output formats I did not verify. Rationale is in the script's comments. VERIFIED: `bash -n` clean; both mirror.yml copies UNTOUCHED and still byte-identical at sha256 6bc46731e030bae8a1970bcb06cf3132323eb454ab72496d6ef9b2c6177585af; `cargo test -p yah-cloud --lib` from oss/yubaba = 1167 passed / 0 failed / 4 ignored, exactly +1 over the 1166 baseline and that +1 is the new test. rustfmt --check clean on cloud_init.rs (paths.rs has ONE pre-existing unformatted hunk in `infra_source_cache_dir`, not mine, left alone). NO FLEET CONTACT of any kind — script, test and one paths.rs helper only. Uncommitted: camp git policy is `defer`, so no SHA to cite.")
100//! @yah:next("WHAT GENERATE-FROM-ONE-SOURCE WOULD ACTUALLY TAKE, having rejected it as out of scope here. The contained 20% is the third-party PINS: litestream 0.3.13 + 2 sha256s and headscale 0.23.0 + 2 sha256s now live in five places (both mirror.yml copies, stand-up-yubaba.sh, and for headscale also `cloud::mesh::HEADSCALE_VERSION` and `yubaba::DEFAULT_HEADSCALE_VERSION`, which mirror.yml's own comment admits are kept in lockstep by convention with no dependency edge). Making those Rust constants the one source and rendering them into mirror.yml as `{{ LITESTREAM_BLOCK }}` / `{{ HEADSCALE_BLOCK }}` would collapse five to one, but it requires touching BOTH mirror.yml copies in lockstep and a matching emitter for the script. The remaining 80% — the install STEPS — is not contained: the script's correct divergences (idempotent enable+restart, write-if-absent ceilings, KEK, loopback bind) mean a generator must model divergence, and the script's real destination is the `yah cloud machine bootstrap` command W242 Phase 1 already plans, which would emit both from one Rust model. That is the right home for this, and it is a relay, not a hunk.")
101//! @yah:gotcha("SUPERSEDES the earlier gotcha beginning \"NOT FIXED, and deliberately left as an operator call\" — that entry is now STALE and should be read as history, not state. The litestream 0.3.13 install and the headscale 0.23.0 / litestream-headscale.service pre-stage ARE now in stand-up-yubaba.sh, added on the relay leader's explicit instruction in this follow-on pass. The fleet-topology question that entry deferred has therefore been ANSWERED IN THE AFFIRMATIVE BY DEFAULT: every LAN/appliance node stood up by this script from here on is provisioned as a coordinator candidate (headscale binary staged at /var/lib/yah-cloud/headscale/headscale, replication unit laid down, neither started — leader.rs still decides who runs it). If that is NOT wanted for the us-west-01x class, the lever is an opt-out env guard in the script, not reverting it, because reverting now goes red against `stand_up_script_carries_the_templates_install_steps`. Cost of the affirmative answer is bounded and staging-only: ~61MB fetched per run and two staged-but-inert artifacts.")
102//! @yah:verify("THIRD-TWIN GUARD: `cargo test -p yah-cloud --lib cloud_init::` from oss/yubaba = 31 passed / 0 failed (30 before, +1 = stand_up_script_carries_the_templates_install_steps). Full lib suite = 1167 passed / 0 failed / 4 ignored vs the 1166/0/4 baseline. bash -n .yah/infra/cloud-init/stand-up-yubaba.sh clean. Both mirror.yml copies still sha256 6bc46731e030bae8a1970bcb06cf3132323eb454ab72496d6ef9b2c6177585af — the guard on THEM was not disturbed and is still green.")
103//! @yah:handoff("ACCEPTED BY THE RELAY LEADER (@Ashguard:hydra, session:39386823). Both halves landed in the required order — guard made honest FIRST, then the two mirror.yml copies reconciled — plus a third twin (stand-up-yubaba.sh) closed that the fix itself exposed. Implemented by @Ashguard:polaris (session:9d2e59a3), independently verified by session:67d56cd3, third-twin follow-on by session:039a0f82. The operationally consequential finding is in the verify entries: provisioning reads the CANONICAL on-disk template, and that was the copy which had drifted.")
104//! @yah:verify("COLUMN MOVED VIA `yah board move R870-B25 review` AFTER BOTH `board.review` VERBS REFUSED — recorded because the next agent will hit it too and the error message actively misleads. The MCP verb and `yah board review` both fail with \"ticket 'R870-B25' not found — it may have been archived or may not exist in this camp\", which is false: the CLI's fresh scan saw it fine, anchored at oss/yubaba/crates/cloud/src/reconciler/mod.rs:67. ROOT CAUSE, grounded not guessed: `arch.review_ticket` is a daemon-only gated write with NO in-process fallback — already filed as R606-T3 and stated verbatim at app/yah/cli/src/camp.rs:975, \"Board reads/updates fall back in-process; review/move/etc do not — that asymmetry is the bug\" — and the daemon resolves transition targets from its IN-MEMORY store rather than from disk (crates/yah/camp-service/src/service.rs:4199-4216, which emits that exact string). So \"exists on disk\" and \"the daemon can transition it\" are independent facts. `yah board move` turns out to be on the falls-back side despite that note, which is why it works. Contributing: the daemon is version-skewed and degraded (0.8.36+74874f3e-dirty vs CLI 0.8.37+e896d28a-dirty) and refused read probes with EAGAIN, the signature R606-S2 pinned to the 500ms fast-path RPC floor under load. ALSO CONFIRMED, since it was the other candidate explanation: `oss/yubaba` is NOT a subcamp — only cheers, mesofact, turso-backup and xlb carry `.yah/camp.toml` — so no `--path` is needed and the filing location was never the problem.")
105
106use std::collections::{BTreeMap, VecDeque};
107use std::path::{Path, PathBuf};
108use std::sync::Arc;
109
110use anyhow::{Context, Result};
111use async_trait::async_trait;
112use serde::{Deserialize, Serialize};
113use tokio::sync::{oneshot, Mutex as AsyncMutex};
114
115use workload_spec::{NamespaceId, TenantId};
116
117use crate::{GitSource, MirrorConfig, MirrorProviderSlot, ServiceComponent, ServiceConfig};
118
119pub mod bundle_store;
120pub(crate) mod cf_creds;
121pub mod cloudflare_worker;
122pub mod container;
123pub mod derive_cache_prune;
124pub mod dev_door;
125pub mod domain;
126pub mod headscale;
127pub mod ingress;
128pub mod ingress_verify;
129// R918-F5 — the local-process reconciler is Unix-only *by design*, not by
130// accident of which syscalls it happened to reach for. Its contract is
131// "replace the predecessor": `kill(pid, 0)` liveness plus a SIGTERM-grace-
132// SIGKILL ladder. A build with that ladder removed would still spawn, and
133// would silently double-spawn instead of reaping — exactly the failure that
134// looks inert in a diff and strands processes on a node. So the module is
135// gated whole rather than half-ported. A non-unix `yah` is a client (it talks
136// to a camp and submits builds); it supervises no workloads, so it needs no
137// local-process reconciler. Reversing this means implementing
138// OpenProcess/TerminateProcess here, at the point someone actually wants a
139// Windows fleet node — see .yah/docs/working/W352-windows-and-macos-build-targets.md.
140#[cfg(unix)]
141pub mod local_process;
142pub mod mesofact_bundle;
143pub mod mesofact_static;
144// `pub(crate)` rather than private: `sanitize_ident` is the crate's ONE mesh-ident
145// normalizer, and R870-F23's `inner_door::component_workload_ident` has to fold
146// its derived ident the same way `local_process` folds its own. A second copy
147// would be a second normalizer that can drift.
148pub(crate) mod native_support;
149pub mod pg_driver;
150pub mod s3_driver;
151pub mod smtp_driver;
152pub mod pond;
153pub mod pond_door;
154pub mod pond_publish;
155pub mod publish_beacon;
156pub mod r2_publish;
157pub mod service_discovery;
158pub mod static_asset;
159pub mod static_asset_prune;
160pub mod sync_status;
161
162#[cfg(test)]
163mod lowering_golden;
164
165pub use bundle_store::{publish_bundle_to_r2, PublishReport as BundlePublishReport};
166pub use cloudflare_worker::CloudflareWorkerReconciler;
167pub use container::{ContainerOptions, ContainerReconciler};
168pub use derive_cache_prune::{
169 collect_live_derive_hashes, compute_derive_cache_candidates, execute_derive_cache_prune,
170 DeriveCacheLiveHashes, DerivePruneCandidate,
171};
172pub use domain::{
173 deploy_domain_passway, diff_apex_records, ensure_passway_apex, ensure_r2_custom_domain,
174 list_live_apex_records, plan_domain_passway, plan_passway_apex, public_origins, ApexRecordDiff,
175 DomainPasswayPlan, LiveApexRecord, PasswayApexOutcome, PasswayOrigin,
176};
177pub use headscale::{
178 DeclaredHeadscale, DeclaredPolicy, DeclaredPreauthKey, HeadscaleReconciler,
179 WORKLOAD_KIND as HEADSCALE_WORKLOAD_KIND,
180};
181pub use ingress::{
182 collate_front_doors, declared as ingress_declared, ensure_tunnel_ingress, machine_mesh_addrs,
183 plan_ingress, publish_tunnel_ingress, resolve_ingress_candidates, resolve_ingress_placements,
184 Collation, IngressPlan, IngressRule, NodeFrontDoor, PlannedEdge, TunnelIngressOutcome,
185};
186pub use ingress_verify::{
187 apply_public_path, resolve_upstreams_reporting, verify_collation, BeaconFetch, DialOutcome,
188 EndpointCheck, PublicReadings, RuleResolution, RuleResolutions, RuleVerdict,
189 UndeclaredDoorProbe, VerifyFinding, VerifyReport, undeclared_door_probes,
190};
191#[cfg(unix)]
192pub use local_process::LocalProcessReconciler;
193pub use mesofact_bundle::{
194 resolve_bundle_machines, BundleSlot, MesofactBundleReconciler, RevalidateSlot,
195 SLOT_ROLE as BUNDLE_SLOT_ROLE,
196};
197pub use dev_door::{sync_dev_door, up_dev_door, DEFAULT_DEV_DOOR_PORT};
198pub use mesofact_static::MesofactStaticReconciler;
199pub use pond::{PondOptions, PondState};
200pub use pond_door::{
201 door_env, door_state_dir, ensure_pond_cert_as, plan_pond_door, pond_hostname,
202 resolve_passway_binary, spawn_pond_door, CertPair, PondDoorPlan, DEFAULT_DOOR_PORT, POND_TLD,
203};
204// R918-F5 — `is_root` reads an effective uid; `ensure_pond_cert` is the wrapper
205// that folds it in. Both unix-only. `ensure_pond_cert_as` above is the portable
206// half, with the privilege decision injected.
207#[cfg(unix)]
208pub use pond_door::{ensure_pond_cert, is_root};
209pub use pond_publish::{derive_minio_key, publish_to_pond, PondPublishReport};
210pub use r2_publish::{
211 publish_to_r2, R2PublishReport, R2PurgeOpts, R2_ACCESS_KEY_ENV, R2_ACCESS_KEY_SLOT,
212 R2_SECRET_KEY_ENV, R2_SECRET_KEY_SLOT,
213};
214pub use service_discovery::{
215 DiscoveredRecord, RecordVisibility, ServiceRecordFanout, UnknownReason,
216};
217pub use static_asset::StaticAssetReconciler;
218pub use static_asset_prune::{
219 compute_live_set, compute_prune_candidates, execute_prune, load_service_and_mirror,
220 PruneCandidate, PruneOutcome, PruneReport,
221};
222pub use sync_status::{
223 compute_cell, compute_service, new_sync_id, summarize, CellStatus, DriftEntry, HealthState,
224 MirrorObservation, Runtime, ServiceStatus, StatusSummary, SyncHistoryEntry, SyncOutcome,
225 SyncState, WireContainerStatus,
226};
227
228// ─── Log buffer ─────────────────────────────────────────────────────────────
229
230const LOG_CAP: usize = 500;
231
232#[derive(Debug, Default)]
233struct LogRing {
234 lines: VecDeque<String>,
235 /// Monotonically increasing total lines ever pushed (never decrements).
236 total: usize,
237}
238
239/// Bounded ring buffer for child-process stdout/stderr (R263-F3).
240/// Shared between the reader tasks and the Tauri `mirror_run_logs` command.
241#[derive(Debug, Clone, Default)]
242pub struct LogBuffer(Arc<AsyncMutex<LogRing>>);
243
244impl LogBuffer {
245 pub fn new() -> Self {
246 Self::default()
247 }
248
249 /// Append a line; drops the oldest entry when over capacity.
250 pub async fn push(&self, line: String) {
251 let mut ring = self.0.lock().await;
252 ring.total += 1;
253 ring.lines.push_back(line);
254 if ring.lines.len() > LOG_CAP {
255 ring.lines.pop_front();
256 }
257 }
258
259 /// Return lines not yet seen by the caller.
260 ///
261 /// `since` is the `total` cursor from the previous call (0 = nothing
262 /// seen yet). Returns `(new_lines, new_cursor)`. Pass `new_cursor` back
263 /// on the next call to receive only incremental output.
264 pub async fn since(&self, since: usize) -> (Vec<String>, usize) {
265 let ring = self.0.lock().await;
266 let oldest = ring.total.saturating_sub(ring.lines.len());
267 let skip = since.saturating_sub(oldest);
268 let new_lines: Vec<String> = ring.lines.iter().skip(skip).cloned().collect();
269 (new_lines, ring.total)
270 }
271
272 /// Current write cursor — the `total` [`Self::since`] would hand back
273 /// right now if nothing more were pushed. Lets a producer mark a boundary
274 /// (e.g. "everything before this point was the build phase") for a later
275 /// reader to seek past without re-reading lines it doesn't want.
276 pub async fn cursor(&self) -> usize {
277 self.0.lock().await.total
278 }
279}
280
281/// Shared cell for one phase-boundary cursor a reconciler can publish
282/// mid-`up()`, so a poller sees a multi-phase bring-up's internal transition
283/// (e.g. build → run) before the whole call returns — the same problem
284/// [`LogBuffer`] solves for output, for a single position instead of a ring.
285/// `None` until the reconciler reaches that phase; a caller registers one
286/// before calling `up()` to observe it live, same pattern as
287/// [`LogBuffer::clone`]-and-hand-in.
288#[derive(Debug, Clone, Default)]
289pub struct PhaseCursor(Arc<AsyncMutex<Option<usize>>>);
290
291impl PhaseCursor {
292 pub fn new() -> Self {
293 Self::default()
294 }
295
296 pub async fn set(&self, cursor: usize) {
297 *self.0.lock().await = Some(cursor);
298 }
299
300 pub async fn get(&self) -> Option<usize> {
301 *self.0.lock().await
302 }
303}
304
305/// The `(tenant, namespace)` a bring-up is scoped to (W206). Reconcilers that
306/// touch a credentialed provider resolve it at this scope
307/// ([`CfProvider::resolve_scoped`](super::reconciler::cf_creds)) so a namespace's
308/// Cloudflare zone/account/keystore slots come from its own scope rather than the
309/// workspace-global defaults. Defaults to the singleton `(default, default)`,
310/// which collapses every scoped lookup back to the historical global slots — so
311/// single-namespace deployments are unaffected.
312#[derive(Debug, Clone)]
313pub struct ProviderScope {
314 pub tenant: TenantId,
315 pub namespace: NamespaceId,
316}
317
318impl ProviderScope {
319 /// The degenerate single-tenant / single-namespace scope. Scoped provider
320 /// lookups made against it resolve to the pre-W206 global keystore slots.
321 pub fn singleton() -> Self {
322 Self {
323 tenant: TenantId::singleton(),
324 namespace: NamespaceId::singleton(),
325 }
326 }
327}
328
329impl Default for ProviderScope {
330 fn default() -> Self {
331 Self::singleton()
332 }
333}
334
335/// Inputs a reconciler sees for one bring-up.
336pub struct ReconcileCtx<'a> {
337 /// Workspace root (parent of `.yah/`). Used to resolve relative paths
338 /// on the component.
339 pub workspace_root: &'a Path,
340 /// Service that owns the component.
341 pub service: &'a ServiceConfig,
342 /// Component being brought up.
343 pub component: &'a ServiceComponent,
344 /// Mirror manifest the bring-up targets.
345 pub mirror: &'a MirrorConfig,
346 /// Environment name (file stem of `mirrors/<env>.toml`).
347 pub env: &'a str,
348 /// `(tenant, namespace)` this bring-up is scoped to (W206). Credentialed
349 /// providers resolve at this scope; defaults to [`ProviderScope::singleton`].
350 pub scope: ProviderScope,
351}
352
353impl<'a> ReconcileCtx<'a> {
354 /// Absolute path to the component's workload directory (the parent of
355 /// `workload.toml`).
356 ///
357 /// In-tree components resolve to `<workspace_root>/<path>`. For
358 /// `git`-sourced components (R561-F1, "BYO git") this points into the
359 /// local clone — `<source_cache>/<subdir>/<path>` — which is empty until
360 /// [`materialize`](Self::materialize) runs (approach A: clone-at-reconcile,
361 /// so config load + validation stay offline).
362 pub fn workload_dir(&self) -> PathBuf {
363 match &self.component.git {
364 None => self.workspace_root.join(&self.component.path),
365 Some(git) => {
366 let mut dir = self.source_cache_dir();
367 if let Some(subdir) = &git.subdir {
368 dir = dir.join(subdir);
369 }
370 dir.join(&self.component.path)
371 }
372 }
373 }
374
375 /// Root of the local clone for a `git`-sourced component:
376 /// `<workspace_root>/.yah/infra/state/sources/<service>/<component_id>`.
377 fn source_cache_dir(&self) -> PathBuf {
378 self.workspace_root
379 .join(".yah/infra/state/sources")
380 .join(&self.service.name)
381 .join(&self.component.id)
382 }
383
384 /// Ensure a `git`-sourced component's code is present locally before build
385 /// (R561-F1, approach A). No-op for in-tree components. Idempotent: clones
386 /// on the first call, fetches + re-checks-out the pinned ref thereafter.
387 ///
388 /// Reconcilers MUST call this at the top of [`up`](Reconciler::up) before
389 /// reading [`workload_dir`](Self::workload_dir) for a remote component.
390 pub async fn materialize(&self) -> Result<()> {
391 let Some(git) = &self.component.git else {
392 return Ok(());
393 };
394 materialize_git_source(git, &self.source_cache_dir())
395 .await
396 .with_context(|| {
397 format!(
398 "materializing git source {}@{} for {}/{}",
399 git.repo, git.r#ref, self.service.name, self.component.id
400 )
401 })
402 }
403
404 /// Read `<workload_dir>/workload.toml` and extract just the `kind`
405 /// discriminator.
406 ///
407 /// Why this and not the strongly-typed [`workload_spec::Workload`]
408 /// parse: reconcilers only need the kind to
409 /// dispatch; per-kind tooling (e.g. `mesofact-dev`'s
410 /// `WatchOptions::from_workload`) does its own parsing for the
411 /// build/out_dir fields it cares about.
412 pub fn workload_kind(&self) -> Result<String> {
413 workload_kind(&self.workload_dir())
414 }
415
416 /// Look up a provider slot by role (e.g. `"static"`, `"compute"`).
417 ///
418 /// Tries the component-qualified key first (`"<role>:<component id>"`)
419 /// before falling back to the bare role. A mirror role is normally
420 /// service-wide — one `providers.static` slot serves every static-kind
421 /// component — but a service can declare more than one component with
422 /// the same role (e.g. two `mesofact-static`/`mesofact-spa` components
423 /// sharing one mirror), and those need distinct ports to ever both come
424 /// up. The qualified key is how a mirror opts a specific component out
425 /// of sharing the bare-role slot:
426 ///
427 /// ```toml
428 /// [providers."static:site"]
429 /// kind = "miniflare-native"
430 /// port = 4331
431 ///
432 /// [providers."static:app"]
433 /// kind = "miniflare-native"
434 /// port = 4332
435 /// ```
436 ///
437 /// A mirror with only the bare role (the common, single-component case)
438 /// is unaffected — the qualified lookup misses and falls through.
439 pub fn slot(&self, role: &str) -> Option<&'a MirrorProviderSlot> {
440 let qualified = format!("{role}:{}", self.component.id);
441 self.mirror
442 .providers
443 .get(qualified.as_str())
444 .or_else(|| self.mirror.providers.get(role))
445 }
446
447 /// This environment's `[build.<component id>]` override, if the mirror
448 /// declares one that changes anything (R905).
449 ///
450 /// Keyed by component id rather than by role: a build belongs to the
451 /// project that declares it, and two components sharing a role still build
452 /// with two different commands.
453 pub fn build_override(&self) -> Option<&'a crate::config::MirrorBuildOverride> {
454 self.mirror.build_override(&self.component.id)
455 }
456}
457
458/// An explicit teardown hook for a workload this process did not spawn as a
459/// child — see [`RunningWorkload::with_teardown`] (R714-B1).
460///
461/// Boxed rather than a generic parameter because [`RunningWorkload`] is stored
462/// in heterogeneous collections (the desktop's mirror registry) and cannot
463/// carry a type parameter.
464/// `Sync` on the boxed closure is load-bearing, not belt-and-braces: without
465/// it `RunningWorkload` stops being `Sync`, so `&RunningWorkload` stops being
466/// `Send`, and every desktop `#[tauri::command]` that holds one across an
467/// `.await` (`mirror_run_logs` iterates the registry's handles) fails to
468/// compile with "future cannot be sent between threads safely". The captures a
469/// teardown needs — a container name and a workspace root — are `Sync` anyway.
470type TeardownFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send>>;
471type TeardownFn = Box<dyn FnOnce() -> TeardownFuture + Send + Sync + 'static>;
472
473/// Newtype so [`RunningWorkload`] can keep its `#[derive(Debug)]` — a boxed
474/// closure is not `Debug`.
475struct Teardown(TeardownFn);
476
477impl std::fmt::Debug for Teardown {
478 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
479 f.write_str("Teardown(<fn>)")
480 }
481}
482
483/// Handle to a workload that's been brought up. Owns the lifecycle: drop
484/// or call [`RunningWorkload::shutdown`] to take it back down.
485#[derive(Debug)]
486pub struct RunningWorkload {
487 /// Workload kind that was reconciled (e.g. `"mesofact-static"`).
488 pub kind: String,
489 /// Slot role this workload occupies on the mirror (e.g. `"static"`).
490 pub slot: String,
491 /// Local URL the workload exposes, when applicable. `None` for
492 /// workloads that publish to a non-local artifact store (e.g. R2).
493 pub dev_url: Option<String>,
494 /// Notional URL of the deployed artifact in the production case
495 /// (e.g. `https://yah.dev`). `None` until Cloudflare/R2 wiring lands.
496 pub public_url: Option<String>,
497 /// Secondary local UI console, if the workload exposes one (e.g. MinIO
498 /// console on a pond tier). Surfaced as a separate chip in the Services
499 /// matrix next to `dev_url`.
500 pub console_url: Option<String>,
501 /// Ring buffer for stdout/stderr from the workload's child process.
502 /// `None` for workloads that don't capture stdio (e.g. container-backed).
503 pub log_buffer: Option<LogBuffer>,
504
505 /// R546-B12: human-readable lines the reconciler wants the operator to see
506 /// on THIS run — what it actually did, not what is configured. A clean
507 /// static-asset reconcile used to print nothing at all, so a successful
508 /// publish and a successful no-op were indistinguishable at the apply
509 /// surface, and the one view that would have disambiguated them (`yah cloud
510 /// status`) was itself blind.
511 ///
512 /// Deliberately NOT on [`RunningWorkloadSummary`]: that type is serialized
513 /// across the process boundary to the desktop UI, and this is per-run
514 /// console output, not state the UI should cache.
515 pub notes: Vec<String>,
516
517 /// Sender that signals the supervisor task to tear down. Closing the
518 /// channel (drop) is equivalent to sending — supervisor exits on
519 /// channel close.
520 shutdown: Option<oneshot::Sender<()>>,
521 /// Joinable task that owns any child process and reaps it on signal.
522 supervisor: Option<tokio::task::JoinHandle<Result<()>>>,
523 /// R714-B1: teardown for a workload that runs OUTSIDE this process, so
524 /// there is no child to reap and no supervisor to signal.
525 ///
526 /// Deliberately run from [`RunningWorkload::shutdown`] only, never from
527 /// `Drop`. The two are different intents and conflating them breaks both
528 /// directions: a container the desktop started is meant to outlive the
529 /// desktop (the next launch re-adopts it with `adopt_only`), so quitting
530 /// the app must not `docker rm -f` it; but the ■ button IS an explicit
531 /// stop, and must.
532 teardown: Option<Teardown>,
533
534 /// R715-F4: where to ask this workload for a live status document, when
535 /// it declared a process-control channel (W315). `None` for a workload
536 /// with no channel — polling is then simply skipped, not an error.
537 ///
538 /// The desktop holds `RunningWorkload` in-process (it links this crate
539 /// directly, unlike the ad-hoc `run.spawn` path which lives behind the
540 /// camp daemon's socket), so a live poll is [`crate::proc_control::fetch_status`]
541 /// called straight against this endpoint — no RPC hop, no handle registry.
542 control: Option<crate::proc_control::ControlEndpoint>,
543
544 /// [`LogBuffer`] cursor marking the end of the build phase, for a
545 /// component that was compiled before being spawned (`local-process`
546 /// with `cargo_package` set). Lines before this index in `log_buffer` are
547 /// `cargo build` output; lines at or after are the spawned process's own
548 /// stdout/stderr. `None` when nothing was built (no `cargo_package`, or a
549 /// reconciler that predates this field).
550 pub build_log_end: Option<usize>,
551}
552
553impl RunningWorkload {
554 /// Create a handle for a workload that's already running externally
555 /// (e.g. embedded in yah-camp). No subprocess is owned; shutdown is a
556 /// no-op so the caller can call `shutdown()` uniformly.
557 pub fn adopted(
558 kind: impl Into<String>,
559 slot: impl Into<String>,
560 dev_url: Option<String>,
561 ) -> Self {
562 Self {
563 kind: kind.into(),
564 slot: slot.into(),
565 dev_url,
566 public_url: None,
567 console_url: None,
568 log_buffer: None,
569 notes: Vec::new(),
570 shutdown: None,
571 supervisor: None,
572 teardown: None,
573 control: None,
574 build_log_end: None,
575 }
576 }
577
578 /// Attach an explicit teardown to a handle for an externally-running
579 /// workload (R714-B1).
580 ///
581 /// `adopted()` alone gives a handle whose `shutdown()` is a documented
582 /// no-op. That is right for workloads another process owns and will keep
583 /// re-asserting (pond containers under camp's yubaba), and wrong for ones
584 /// this process started and nobody else will ever stop — for those the ■
585 /// button reported success while the container kept running.
586 ///
587 /// `teardown` runs on `shutdown()` and NOT on `Drop`; see [`Self::teardown`].
588 pub fn with_teardown<F, Fut>(mut self, teardown: F) -> Self
589 where
590 F: FnOnce() -> Fut + Send + Sync + 'static,
591 Fut: std::future::Future<Output = Result<()>> + Send + 'static,
592 {
593 self.teardown = Some(Teardown(Box::new(move || Box::pin(teardown()))));
594 self
595 }
596
597 /// Whether this handle carries a real teardown — i.e. whether a failed
598 /// [`Self::shutdown`] means the workload is **still running**.
599 ///
600 /// The stop path needs this to decide between an operator-facing error and
601 /// a log line, and R875-B1 is why it is a property of the handle rather
602 /// than a list of kinds at the call site. That list started as
603 /// `kind == "container"` when R714-B1 gave containers a teardown, and was
604 /// silently wrong the moment a second kind grew one: an adopted
605 /// mesofact-dev whose teardown failed reported a successful stop with the
606 /// server still serving. A handle knows whether it owns the workload; a
607 /// string comparison at the call site only knows what it was last taught.
608 pub fn owns_teardown(&self) -> bool {
609 self.teardown.is_some()
610 }
611
612 /// Attach per-run operator-facing lines (R546-B12). See [`Self::notes`].
613 pub fn with_notes(mut self, notes: Vec<String>) -> Self {
614 self.notes = notes;
615 self
616 }
617
618 /// Record where to poll this workload's process-control channel, when it
619 /// declared one (R715-F4). A no-op (leaves `control: None`) when the
620 /// argument is `None` — callers can pass the reconciler's resolved
621 /// endpoint straight through without an `if let`.
622 pub fn with_control(mut self, control: Option<crate::proc_control::ControlEndpoint>) -> Self {
623 self.control = control;
624 self
625 }
626
627 /// Ask this workload's process-control channel for a live status
628 /// document (R715-F4). `None` when no channel was declared, or when the
629 /// endpoint didn't answer — both are "nothing new to show", not errors;
630 /// see [`crate::proc_control::fetch_status`] for why a poll failure isn't
631 /// itself meaningful.
632 pub async fn poll_control(&self) -> Option<crate::proc_control::ProcStatus> {
633 let endpoint = self.control.as_ref()?;
634 crate::proc_control::fetch_status(endpoint).await.ok()
635 }
636
637 /// Whether the supervisor task that owns this workload's child process is
638 /// still running. `spawn_native_log_supervisor` (native_support.rs) exits
639 /// as soon as `NativeRuntime::get_workload` reports a terminal state —
640 /// which itself comes from a real `child.wait()` in kamaji's native
641 /// backend, so this catches a crash (segfault, panic, `SIGKILL`) the same
642 /// way it catches a clean exit, not just an unresponsive process.
643 ///
644 /// A workload with no supervisor (`RunningWorkload::adopted` — runs
645 /// outside this process, e.g. a pond container camp re-asserts) has
646 /// nothing to check here and reports alive unconditionally; its liveness
647 /// is whatever tracks it, not this handle.
648 pub fn is_alive(&self) -> bool {
649 self.supervisor.as_ref().is_none_or(|h| !h.is_finished())
650 }
651
652 /// Record where the build phase ends in `log_buffer`, for a component
653 /// that was compiled before being spawned. See [`Self::build_log_end`].
654 pub fn with_build_log_end(mut self, cursor: Option<usize>) -> Self {
655 self.build_log_end = cursor;
656 self
657 }
658
659 /// Set the public URL for a published workload (e.g. `"https://yah.dev"`).
660 pub fn with_public_url(mut self, url: impl Into<String>) -> Self {
661 self.public_url = Some(url.into());
662 self
663 }
664
665 /// Set the console URL for a workload that exposes a secondary local UI
666 /// (e.g. MinIO console on a pond tier).
667 pub fn with_console_url(mut self, url: impl Into<String>) -> Self {
668 self.console_url = Some(url.into());
669 self
670 }
671
672 /// Gracefully tear down: signal the supervisor, await its exit, then run
673 /// any explicit teardown hook.
674 ///
675 /// The hook runs LAST and its error propagates. A stop that could not tear
676 /// the workload down must surface as an error, never as a silent success —
677 /// that silence is the whole of R714-B1.
678 pub async fn shutdown(mut self) -> Result<()> {
679 if let Some(tx) = self.shutdown.take() {
680 let _ = tx.send(());
681 }
682 if let Some(handle) = self.supervisor.take() {
683 handle
684 .await
685 .context("joining workload supervisor")?
686 .context("workload supervisor")?;
687 }
688 if let Some(Teardown(hook)) = self.teardown.take() {
689 hook().await.context("workload teardown")?;
690 }
691 Ok(())
692 }
693}
694
695impl Drop for RunningWorkload {
696 fn drop(&mut self) {
697 // Best-effort signal. The supervisor task is detached and will
698 // reap its child when it observes the closed channel.
699 if let Some(tx) = self.shutdown.take() {
700 let _ = tx.send(());
701 }
702 }
703}
704
705/// Bring one workload up. Each impl handles one [`ServiceComponent::kind`].
706#[async_trait]
707pub trait Reconciler: Send + Sync {
708 /// Workload kind this reconciler handles (matches `ServiceComponent.kind`).
709 fn kind(&self) -> &'static str;
710
711 /// Bring the workload up. Returns a handle whose lifecycle is tied to
712 /// the mirror being up.
713 async fn up(&self, ctx: ReconcileCtx<'_>) -> Result<RunningWorkload>;
714}
715
716/// Read `<workload_dir>/workload.toml` and return just the `kind` field.
717/// See [`ReconcileCtx::workload_kind`] for why we don't deserialize through
718/// the strong types yet.
719pub fn workload_kind(workload_dir: &Path) -> Result<String> {
720 let path = workload_dir.join("workload.toml");
721 let src =
722 std::fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))?;
723 let value: toml::Value =
724 toml::from_str(&src).with_context(|| format!("parsing {}", path.display()))?;
725 let kind = value
726 .get("kind")
727 .and_then(|v| v.as_str())
728 .with_context(|| format!("{}: missing `kind` field", path.display()))?;
729 Ok(kind.to_string())
730}
731
732/// Shallow-clone (or update) a [`GitSource`] into `dir` (R561-F1). Idempotent:
733/// clones when `dir/.git` is absent, otherwise fetches the pinned ref and
734/// force-checks-it-out. Uses the system `git` so it inherits the operator's
735/// credential helpers / SSH agent — no in-process git library.
736///
737/// `pub` (R615-T3 / W274): `yah infra sync` reuses this verbatim for
738/// `InfraSourceKind::Git` sources rather than a second shallow-clone-or-pull
739/// implementation — same "one git-source shape, reused" discipline R615-F1
740/// already applied to the type.
741pub async fn materialize_git_source(git: &GitSource, dir: &Path) -> Result<()> {
742 use tokio::process::Command;
743
744 async fn run_git(args: &[&std::ffi::OsStr]) -> Result<()> {
745 let out = Command::new("git")
746 .args(args)
747 .output()
748 .await
749 .context("spawning git")?;
750 if !out.status.success() {
751 anyhow::bail!(
752 "git {} failed: {}",
753 args.iter()
754 .map(|a| a.to_string_lossy())
755 .collect::<Vec<_>>()
756 .join(" "),
757 String::from_utf8_lossy(&out.stderr).trim()
758 );
759 }
760 Ok(())
761 }
762
763 use std::ffi::OsStr;
764 let dir_os = dir.as_os_str();
765 let r#ref = git.r#ref.as_str();
766
767 if dir.join(".git").is_dir() {
768 // Existing checkout — update to the pinned ref.
769 run_git(&[
770 OsStr::new("-C"),
771 dir_os,
772 OsStr::new("fetch"),
773 OsStr::new("--depth"),
774 OsStr::new("1"),
775 OsStr::new("origin"),
776 OsStr::new(r#ref),
777 ])
778 .await?;
779 run_git(&[
780 OsStr::new("-C"),
781 dir_os,
782 OsStr::new("checkout"),
783 OsStr::new("--force"),
784 OsStr::new("FETCH_HEAD"),
785 ])
786 .await?;
787 } else {
788 if let Some(parent) = dir.parent() {
789 tokio::fs::create_dir_all(parent)
790 .await
791 .with_context(|| format!("creating {}", parent.display()))?;
792 }
793 // `--branch` accepts a branch or tag. Pinning to a bare commit SHA is a
794 // follow-up (needs clone-then-fetch); the common case is a branch/tag.
795 run_git(&[
796 OsStr::new("clone"),
797 OsStr::new("--depth"),
798 OsStr::new("1"),
799 OsStr::new("--branch"),
800 OsStr::new(r#ref),
801 OsStr::new(git.repo.as_str()),
802 dir_os,
803 ])
804 .await?;
805 }
806 Ok(())
807}
808
809/// Build a `RunningWorkload` from the pieces a reconciler produces.
810pub(crate) fn into_running(
811 kind: impl Into<String>,
812 slot: impl Into<String>,
813 dev_url: Option<String>,
814 public_url: Option<String>,
815 log_buffer: Option<LogBuffer>,
816 shutdown: oneshot::Sender<()>,
817 supervisor: tokio::task::JoinHandle<Result<()>>,
818) -> RunningWorkload {
819 RunningWorkload {
820 kind: kind.into(),
821 slot: slot.into(),
822 dev_url,
823 public_url,
824 console_url: None,
825 log_buffer,
826 notes: Vec::new(),
827 shutdown: Some(shutdown),
828 supervisor: Some(supervisor),
829 teardown: None,
830 control: None,
831 build_log_end: None,
832 }
833}
834
835/// Wait for a TCP port to start accepting connections. Returns `true` if
836/// the port came up within `timeout`, `false` otherwise. Useful for
837/// reconcilers that spawn a server and need to know when it's reachable
838/// before reporting success.
839///
840/// R918-F5 — unix-only: its only caller is [`local_process`], gated above.
841#[cfg(unix)]
842pub(crate) async fn wait_for_port(
843 addr: std::net::SocketAddr,
844 timeout: std::time::Duration,
845) -> bool {
846 let deadline = tokio::time::Instant::now() + timeout;
847 loop {
848 if tokio::net::TcpStream::connect(addr).await.is_ok() {
849 return true;
850 }
851 if tokio::time::Instant::now() >= deadline {
852 return false;
853 }
854 tokio::time::sleep(std::time::Duration::from_millis(50)).await;
855 }
856}
857
858// `wait_for_http_ready` lived here pre-R374-F3 to back the MinIO health
859// probe in pond's bring-up path. That logic moved to
860// `local_driver::pond_minio::wait_for_http_ready` so yubaba + cloud share
861// it. The mesofact-static reconciler arm uses [`wait_for_port`] for
862// dev-tier port readiness; nothing else needs an HTTP-level probe today.
863
864/// Pluck a `u16` out of a [`MirrorProviderSlot`]'s inline `fields` map.
865/// Returns `None` if the key is absent or out of range.
866pub(crate) fn slot_field_u16(fields: &BTreeMap<String, toml::Value>, key: &str) -> Option<u16> {
867 fields
868 .get(key)
869 .and_then(|v| v.as_integer())
870 .and_then(|n| u16::try_from(n).ok())
871}
872
873/// Serializable summary of a running workload — what the desktop / CLI
874/// hands to the UI. Subset of [`RunningWorkload`] that's safe to cross
875/// process boundaries.
876#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
877pub struct RunningWorkloadSummary {
878 pub kind: String,
879 pub slot: String,
880 pub dev_url: Option<String>,
881 pub public_url: Option<String>,
882 pub console_url: Option<String>,
883 /// Operator-facing lines from [`RunningWorkload::notes`] (R546-B12).
884 ///
885 /// These used to stop here — the summary dropped them, so every note a
886 /// reconciler attached was written into a struct nobody read. They matter
887 /// most for a workload with no `dev_url` to click (R715-T2): the notes are
888 /// then the only structured thing the Run tab has to show about it.
889 #[serde(default, skip_serializing_if = "Vec::is_empty")]
890 pub notes: Vec<String>,
891 /// See [`RunningWorkload::build_log_end`]. Lets a log-tail consumer skip
892 /// straight to run-phase output, or show everything from 0 when the
893 /// operator wants the build log too (e.g. after a compile failure).
894 #[serde(default, skip_serializing_if = "Option::is_none")]
895 pub build_log_end: Option<usize>,
896}
897
898impl From<&RunningWorkload> for RunningWorkloadSummary {
899 fn from(r: &RunningWorkload) -> Self {
900 Self {
901 kind: r.kind.clone(),
902 slot: r.slot.clone(),
903 dev_url: r.dev_url.clone(),
904 public_url: r.public_url.clone(),
905 console_url: r.console_url.clone(),
906 notes: r.notes.clone(),
907 build_log_end: r.build_log_end,
908 }
909 }
910}
911
912#[cfg(test)]
913mod source_seam_tests {
914 //! R561-F1 — the BYO-git source seam: path resolution + materialization.
915 use super::*;
916 use std::collections::BTreeMap;
917
918 fn component(git: Option<GitSource>) -> ServiceComponent {
919 ServiceComponent {
920 mount: None,
921 id: "site".into(),
922 kind: "mesofact-static".into(),
923 path: "site".into(),
924 role: "static".into(),
925 publishes: None,
926 wave: 0,
927 git,
928 deploy: Default::default(),
929 }
930 }
931
932 fn service(comp: ServiceComponent) -> ServiceConfig {
933 ServiceConfig {
934 schema_version: 1,
935 name: "scrabcake".into(),
936 domain: "scrabcake.example".into(),
937 health_path: None,
938 components: vec![comp],
939 db: crate::DbCatalog::default(),
940 }
941 }
942
943 fn mirror() -> MirrorConfig {
944 MirrorConfig {
945 schema_version: 1,
946 shape: crate::MirrorShape::Local,
947 providers: BTreeMap::new(),
948 ingress: Default::default(),
949 ingress_machines: Vec::new(),
950 drivers: Default::default(),
951 asset_aliases: BTreeMap::new(),
952 build: Default::default(),
953 }
954 }
955
956 fn ctx<'a>(ws: &'a Path, svc: &'a ServiceConfig, mir: &'a MirrorConfig) -> ReconcileCtx<'a> {
957 ReconcileCtx {
958 workspace_root: ws,
959 service: svc,
960 component: &svc.components[0],
961 mirror: mir,
962 env: "dev",
963 scope: ProviderScope::singleton(),
964 }
965 }
966
967 #[test]
968 fn workload_dir_in_tree_joins_workspace_root() {
969 let svc = service(component(None));
970 let mir = mirror();
971 assert_eq!(
972 ctx(Path::new("/ws"), &svc, &mir).workload_dir(),
973 Path::new("/ws/site")
974 );
975 }
976
977 #[test]
978 fn workload_dir_git_resolves_into_source_cache_with_subdir() {
979 let git = GitSource {
980 repo: "https://example.com/r.git".into(),
981 r#ref: "main".into(),
982 subdir: Some("apps".into()),
983 };
984 let svc = service(component(Some(git)));
985 let mir = mirror();
986 assert_eq!(
987 ctx(Path::new("/ws"), &svc, &mir).workload_dir(),
988 Path::new("/ws/.yah/infra/state/sources/scrabcake/site/apps/site")
989 );
990 }
991
992 #[tokio::test]
993 async fn materialize_is_noop_for_in_tree_component() {
994 let svc = service(component(None));
995 let mir = mirror();
996 // No git source → Ok, and nothing is written under the workspace.
997 ctx(Path::new("/nonexistent-ws"), &svc, &mir)
998 .materialize()
999 .await
1000 .unwrap();
1001 }
1002
1003 #[tokio::test]
1004 async fn materialize_clones_git_source_offline() {
1005 fn git(args: &[&str], cwd: &Path) {
1006 let out = std::process::Command::new("git")
1007 .args(args)
1008 .current_dir(cwd)
1009 .env("GIT_AUTHOR_NAME", "t")
1010 .env("GIT_AUTHOR_EMAIL", "t@t")
1011 .env("GIT_COMMITTER_NAME", "t")
1012 .env("GIT_COMMITTER_EMAIL", "t@t")
1013 .output()
1014 .unwrap();
1015 assert!(
1016 out.status.success(),
1017 "git {args:?}: {}",
1018 String::from_utf8_lossy(&out.stderr)
1019 );
1020 }
1021
1022 let tmp = tempfile::tempdir().unwrap();
1023 let src = tmp.path().join("src-repo");
1024 std::fs::create_dir_all(&src).unwrap();
1025 git(&["init", "-b", "main"], &src);
1026 std::fs::write(src.join("hello.txt"), "hi").unwrap();
1027 git(&["add", "."], &src);
1028 git(&["commit", "-m", "init"], &src);
1029
1030 let source = GitSource {
1031 repo: format!("file://{}", src.display()),
1032 r#ref: "main".into(),
1033 subdir: None,
1034 };
1035 let dest = tmp.path().join("cache");
1036
1037 // First call clones.
1038 materialize_git_source(&source, &dest).await.unwrap();
1039 assert!(dest.join("hello.txt").is_file());
1040
1041 // Second call takes the update path and stays green (idempotent).
1042 materialize_git_source(&source, &dest).await.unwrap();
1043 assert!(dest.join("hello.txt").is_file());
1044 }
1045}
1046
1047#[cfg(test)]
1048mod teardown_tests {
1049 //! R714-B1 — the explicit-teardown contract on [`RunningWorkload`].
1050 //!
1051 //! These are about WHEN the hook runs, not what it does. The bug being
1052 //! fixed was a `shutdown()` that reported success having done nothing, and
1053 //! the trap in fixing it is a `Drop` that tears down a container which is
1054 //! supposed to survive the process.
1055 use super::*;
1056 use std::sync::atomic::{AtomicUsize, Ordering};
1057
1058 fn counting() -> (RunningWorkload, Arc<AtomicUsize>) {
1059 let hits = Arc::new(AtomicUsize::new(0));
1060 let seen = hits.clone();
1061 let w = RunningWorkload::adopted("container", "compute", None)
1062 .with_teardown(move || {
1063 let seen = seen.clone();
1064 async move {
1065 seen.fetch_add(1, Ordering::SeqCst);
1066 Ok(())
1067 }
1068 });
1069 (w, hits)
1070 }
1071
1072 /// Attaching a teardown must not cost `RunningWorkload` its auto traits.
1073 /// The desktop stores these handles in a shared registry and its Tauri
1074 /// commands iterate them across `.await` points, which needs `Sync` — a
1075 /// hook that is `Send` but not `Sync` takes it away here and surfaces two
1076 /// crates over as "future cannot be sent between threads safely", with a
1077 /// span pointing at `mirror_run_logs` rather than at this file.
1078 #[test]
1079 fn a_teardown_does_not_cost_the_handle_send_or_sync() {
1080 fn assert_send_sync<T: Send + Sync>() {}
1081 assert_send_sync::<RunningWorkload>();
1082 }
1083
1084 #[tokio::test]
1085 async fn shutdown_runs_the_teardown() {
1086 let (w, hits) = counting();
1087 w.shutdown().await.unwrap();
1088 assert_eq!(hits.load(Ordering::SeqCst), 1);
1089 }
1090
1091 #[tokio::test]
1092 async fn dropping_the_handle_does_NOT_run_the_teardown() {
1093 // A container this process started is meant to outlive it — the next
1094 // launch re-adopts it. Quitting the app must not `docker rm -f` it.
1095 let (w, hits) = counting();
1096 drop(w);
1097 // Yield so a stray spawned task would have had a chance to run.
1098 tokio::task::yield_now().await;
1099 assert_eq!(hits.load(Ordering::SeqCst), 0);
1100 }
1101
1102 #[tokio::test]
1103 async fn a_failing_teardown_makes_shutdown_fail() {
1104 // The whole of R714-B1: a stop that could not tear the workload down
1105 // must not report success.
1106 let w = RunningWorkload::adopted("container", "compute", None)
1107 .with_teardown(|| async { anyhow::bail!("docker stop refused") });
1108 let err = w.shutdown().await.unwrap_err();
1109 assert!(format!("{err:#}").contains("docker stop refused"), "{err:#}");
1110 }
1111
1112 #[tokio::test]
1113 async fn an_adopted_handle_without_a_teardown_still_shuts_down_cleanly() {
1114 // Pond containers are owned by camp's yubaba and stop through
1115 // `workload.stop`; their no-op shutdown is correct and must stay.
1116 let w = RunningWorkload::adopted("mesofact-static", "static", None);
1117 w.shutdown().await.unwrap();
1118 }
1119
1120 /// R875-B1. The stop path decides "still running, tell the operator" vs
1121 /// "untidy supervisor, log it" from this, so it has to answer for the
1122 /// handle in front of it rather than for the kind string it carries — the
1123 /// two disagreed for every adopted mesofact-dev.
1124 #[test]
1125 fn owns_teardown_tracks_the_hook_not_the_kind() {
1126 let (owned, _) = counting();
1127 assert!(owned.owns_teardown());
1128 assert!(
1129 !RunningWorkload::adopted("container", "compute", None).owns_teardown(),
1130 "a bare adopted handle owns nothing, whatever its kind says"
1131 );
1132 assert!(
1133 RunningWorkload::adopted("mesofact-static", "static", None)
1134 .with_teardown(|| async { Ok(()) })
1135 .owns_teardown(),
1136 "a non-container kind with a teardown owns its workload"
1137 );
1138 }
1139
1140 #[tokio::test]
1141 async fn the_teardown_runs_after_the_supervisor_is_joined() {
1142 // Ordering matters for a workload that has both: reap the child first,
1143 // then remove the container it was talking to.
1144 let order = Arc::new(AsyncMutex::new(Vec::<&'static str>::new()));
1145 let (tx, rx) = oneshot::channel::<()>();
1146 let sup_order = order.clone();
1147 let supervisor = tokio::spawn(async move {
1148 let _ = rx.await;
1149 sup_order.lock().await.push("supervisor");
1150 Ok(())
1151 });
1152 let hook_order = order.clone();
1153 let w = into_running("container", "compute", None, None, None, tx, supervisor)
1154 .with_teardown(move || {
1155 let hook_order = hook_order.clone();
1156 async move {
1157 hook_order.lock().await.push("teardown");
1158 Ok(())
1159 }
1160 });
1161
1162 w.shutdown().await.unwrap();
1163 assert_eq!(*order.lock().await, vec!["supervisor", "teardown"]);
1164 }
1165}