Skip to main content

workload_spec/
lib.rs

1//! `WorkloadSpec` — typed wire format for yubaba workloads.
2//!
3//! This crate is the schema source of truth. It has zero dependencies on
4//! yubaba; yubaba depends on it, not the other way around. Agents and desktop
5//! code that construct specs can link this crate without pulling in yubaba's
6//! containerd client.
7//!
8//! Three validation layers live in [`validate`]: shape (sync, no I/O),
9//! semantic (reads yubaba state), and environment (deploy-time). The schema
10//! types live at the top level.
11//!
12//! @yah:ticket(R222-T3, "Workload schema doesn't match per-kind on-disk shapes (mesofact-static)")
13//! @yah:assignee(agent:claude)
14//! @yah:at(2026-05-18T16:47:03Z)
15//! @yah:status(review)
16//! @yah:parent(R222)
17//! @yah:handoff("Picked option (a): tagged-enum Workload envelope with per-kind variants. Added Workload { MesofactStatic(MesofactStaticWorkload), Container(WorkloadSpec) } + BuildConfig in workload-spec. WorkloadSpec stays the containerd RPC wire type (now also the kind=\"container\" variant payload). xtask emit-schemas now renders workload.toml.schema.json as a oneOf over kind; schema drift test green. TS export updated. Arch doc 'workloads — colocated, not registered' rewritten to describe the envelope + both example kinds; B4 outlook updated to point at Workload.")
18//! @yah:verify("cargo check -p cloud && cargo test -p cloud && cargo check -p yah && cargo check -p agent-tools && cargo check -p yah --tests && cargo check -p agent-tools --tests")
19//! @yah:verify("cargo test -p xtask  # schema drift test must stay green")
20//! @yah:verify("cargo run -p workload-spec --bin export-ts  # idempotent regen")
21//!
22//! @yah:ticket(R256-F7, "Model mesofact container as two roles: transient build/publish job vs long-lived SSR/SPA runtime")
23//! @yah:assignee(agent:claude)
24//! @yah:at(2026-05-25T20:08:29Z)
25//! @yah:status(review)
26//! @yah:parent(R256)
27//! @yah:next("role A — build/publish job: transient task that runs the build and PUTs to the object store, then exits/GC'd; needed whenever there is a build step (SSR or not)")
28//! @yah:next("role B — SSR/SPA runtime: long-lived container, only present when the app has realtime/dynamic pages (this is what the 'only if SSR/SPA' gate applies to)")
29//! @yah:next("decide the fidelity knob: does the build run in-container (matches CI, max fidelity, costs image+cold-start) or on host with yubaba orchestrating only the serving edge?")
30//! @yah:assumes("in cloud these are separate: CI/build job produces artifacts, R2+CDN serve them, and a distinct worker serves any SSR — so one merged 'mesofact container' is the trap")
31//! @yah:handoff("BuildMode enum added to workload-spec with HostSide (default) and InContainer { image } variants. MesofactStaticWorkload gains build_mode: BuildMode (skip_serializing_if default) and ssr_runtime: Option<WorkloadSpec>. Encodes the two-role model: build step is always transient; SSR companion is optional long-lived. Fidelity knob decision: HostSide = host watcher (dev+sim), InContainer = CI-fidelity (cloud/ha). All three codegen targets updated: export-ts.rs, packages/yah/workload-spec/index.ts, .yah/schema/workload.toml.schema.json. Schema drift tests pass.")
32//! @yah:verify("cargo check -p workload-spec --locked")
33//! @yah:verify("cargo test -p xtask --locked  # schema_drift tests pass")
34//! @yah:verify("cargo test -p cloud --locked --lib  # 165 passed")
35//!
36//! @yah:ticket(R256-F9, "Almanac as a dependency manifest: orchestrator verifies I/O targets live before run; output invalidates mesofact sources cleanly")
37//! @yah:assignee(agent:claude)
38//! @yah:at(2026-05-25T21:28:15Z)
39//! @yah:status(review)
40//! @yah:parent(R256)
41//! @yah:next("almanac is a manifest declaring inputs + outputs + cadence + command — NOT a bash cron; the declared I/O is the contract")
42//! @yah:next("before a run the orchestrator verifies declared inputs exist AND output targets (e.g. the mesofact app + its source/object store) are reachable; if not → the run fails or waits/times out rather than producing orphaned output")
43//! @yah:next("almanac output invalidates downstream mesofact sources cleanly + deliberately (a declared dependency edge, not a blunt rebuild-everything) — this is the reason it's a named manifest, not a shell cron")
44//! @yah:next("decide the not-ready policy knob: fail-fast vs wait-with-timeout vs requeue")
45//! @yah:next("generalizes the OpenRouter refresher (spawn_almanac_refresher), which is the degenerate no-dependency case (output = JSON cache, no app target)")
46//! @yah:assumes("precondition enforcement lives in the shared scheduler layer (embedded by camp for dev/sim, yubaba for cloud/ha) — it needs the workload registry + xlb-net discovery to answer 'is the target up?', which a bash cron lacks")
47//! @arch:see(.yah/docs/architecture/A024-vocabulary.md)
48//! @yah:depends_on(R256-F6)
49//! @yah:handoff("AlmanacTarget (Http/Tcp probe), NotReadyPolicy (WaitWithTimeout default=5s/FailFast/Requeue), Cadence (Once/Every/Cron), and AlmanacManifest types added to workload-spec. Workload enum gains Almanac(AlmanacManifest) variant (kind='almanac'). NotReadyPolicy::WaitWithTimeout(5s) is the default — matches sim-tier spinup budget. AlmanacManifest.invalidates: Vec<MeshIdent> declares downstream cache-bust targets. export-ts.rs updated; index.ts and workload.toml.schema.json regenerated; drift tests pass. The degenerate case (no inputs, no outputs, Cron, no invalidates) is exactly the OpenRouter refresher pattern. The orchestrator precondition enforcement (xlb-net probing) is left for R276/yubaba integration.")
50//! @yah:verify("cargo check -p workload-spec --locked")
51//! @yah:verify("cargo test -p xtask --locked  # schema drift tests pass")
52//! @yah:verify("cargo test -p cloud --locked --lib  # 165 passed")
53//!
54//! @yah:relay(R335, "Almanac mirror-binding — scope a feed to the mirror it affects")
55//! @yah:at(2026-05-27T02:19:09Z)
56//! @yah:status(open)
57//! @arch:see(.yah/docs/working/W058-almanac-mirror-binding.md)
58//! @yah:depends_on(R256-F9)
59//!
60//! @yah:ticket(R335-S1, "Decide cross-env pollution mechanism: extend R256-F9 manifest vs add per-mirror capability")
61//! @yah:assignee(agent:claude)
62//! @yah:at(2026-05-27T02:19:33Z)
63//! @yah:kind(spike)
64//! @yah:status(review)
65//! @yah:phase(P1)
66//! @yah:parent(R335)
67//! @yah:gotcha("Build ON R256-F9's AlmanacManifest (workload-spec/src/lib.rs) — do NOT invent a parallel manifest. R256-F9 is in review.")
68//! @yah:depends_on(R256-F9)
69//! @yah:handoff("Decided. Recorded in almanac-mirror-binding.md §11. KEY FINDING: two almanac paths exist; the live R330 feed uses almanac::FeedConfig (on_change=MesofactRebuild{service,route} — a service id, NOT a MeshIdent), so it never touches AlmanacManifest.invalidates. Verdict on the S1 title: NEITHER extend the manifest nor (yet) add capability is the accident fix — dev->cloud is ALREADY blocked by construction (feed path = process locality + per-mirror reconciler + MinIO/R2 backend split; manifest path = no camp-embedded MeshState, mesh resolution is yubaba-raft-only). Residual holes: /revalidate receiver is UNAUTHENTICATED, and same-tier (two clouds on one R2) has no per-mirror key prefix.")
70//! @yah:next("FILED: R335-F3 (P1, no yubaba dep) mirror-aware /revalidate receiver — reject feeds not bound to this mirror; satisfies R335-T2; lands with R330-F4.")
71//! @yah:next("FILED: R335-F4 (P2) per-mirror artifact key prefix in derive_minio_key/publish_to_r2 — closes same-tier collision.")
72//! @yah:next("FILED: R335-F5 (P3, BLOCKED on yubaba control plane) per-mirror capability gate on /revalidate via yubaba/xlb-net node identity.")
73//!
74//! @yah:ticket(R278-F4, "RolloutPolicy schema in workload-spec (TOML types)")
75//! @yah:assignee(agent:claude)
76//! @yah:at(2026-06-01T02:31:25Z)
77//! @yah:status(review)
78//! @yah:parent(R278)
79//! @yah:next("Add src/rollout.rs with RolloutPolicy, RolloutStrategy, RolloutGate, RolloutStep, RolloutOnFailure")
80//! @yah:next("Export pub mod rollout from lib.rs")
81//! @yah:next("Add TS export via ts-rs in export-ts.rs")
82//! @arch:see(.yah/docs/working/W140-yah-yubaba-ci-cd.md)
83//! @yah:handoff("RolloutPolicy, RolloutStrategy, RolloutGate, RolloutStep, RolloutOnFailure added to workload-spec/src/rollout.rs. Exported from lib.rs. toml dev-dep added for round-trip test. Tests: rollout::tests::round_trip_toml + on_failure_default both green.")
84//!
85//! @yah:ticket(R429-T1, "Workload::StaticAsset variant + schema in workload-spec (catalog + aliases)")
86//! @yah:assignee(agent:claude)
87//! @yah:at(2026-06-03T23:24:20Z)
88//! @yah:status(review)
89//! @yah:phase(P1)
90//! @yah:parent(R429)
91//! @yah:next("Add Workload::StaticAsset(StaticAssetWorkload) variant alongside the existing MesofactStatic + Container envelopes. Mirror the tagged-enum shape R222-T3 established.")
92//! @yah:next("StaticAssetWorkload fields: kind='static-asset' tag, assets: Vec<AssetEntry>, aliases: BTreeMap<String, String>. AssetEntry { filename: String, source: PathBuf, blake3: BlakeHash }.")
93//! @yah:next("BlakeHash newtype validates 64-hex-char shape (reuse from existing places if available, else introduce here).")
94//! @yah:next("Closed-catalog invariant: aliases values MUST be filenames present in the assets list. Reject at load with a clear error pointing at the offending alias key + bad filename.")
95//! @yah:next("Mirror schema extension: optional [asset_aliases] BTreeMap<String, String> on MirrorConfig. Semantic validator (when both workload + mirror are loaded together) rejects mirror aliases whose target filename isn't in the catalog.")
96//! @yah:next("Regenerate the workload.toml.schema.json via xtask emit-schemas (R222-B4). Confirm the drift test stays green.")
97//! @yah:next("TS mirror: extend packages/yah/workload-spec/index.ts with the StaticAsset variant + AssetEntry. Confirm bun typecheck stays green.")
98//! @yah:verify("cargo check -p workload-spec --locked")
99//! @yah:verify("cargo test -p workload-spec")
100//! @yah:verify("cargo run -p workload-spec --bin export-ts")
101//! @yah:verify("cargo test -p xtask")
102//! @arch:see(.yah/docs/working/W160-atomic-release-waves.md)
103//!
104//! @yah:ticket(R429-F2, "static-asset reconciler: BLAKE3 verify + S3 PUT against mirror's object_store + drift")
105//! @yah:assignee(agent:claude)
106//! @yah:at(2026-06-03T23:24:38Z)
107//! @yah:status(review)
108//! @yah:phase(P2)
109//! @yah:parent(R429)
110//! @yah:next("New reconciler that handles kind='static-asset' in the same service-sync loop that already runs mesofact-static + container. Same wave-gate semantics, same drift shape.")
111//! @yah:next("For each [[asset]] row: hash source file (BLAKE3) and compare to manifest entry. Mismatch → surface as drift, halt push for that asset until rebuild.")
112//! @yah:next("Resolve mirror's object_store provider → R2 bucket + credentials. HEAD cas/filename; if absent or different content-length → PUT. Idempotent on re-run.")
113//! @yah:next("Drift detection: list bucket contents under the component's prefix, compare against catalog filenames. Files in bucket ∖ catalog → report as drift (do NOT delete; that's the prune verb's job).")
114//! @yah:next("ServicesView's existing matrix consumes the new drift shape automatically. Confirm SyncGlyph/DriftList render correctly for a static-asset row without UI changes.")
115//! @yah:next("MockR2 in tests: HashMap<key, bytes> implementing the S3 surface the reconciler hits. Cover: push first-time, push idempotent, drift catches catalog-vs-bucket mismatch, BLAKE3 mismatch halts push.")
116//! @yah:next("Real-R2 integration test gated behind YAH_TEST_R2_BUCKET env var — one round-trip against a scratch bucket; skipped otherwise.")
117//! @yah:verify("cargo check --workspace --locked")
118//! @yah:verify("cargo test -p <reconciler-crate>  # crate TBD by impl agent")
119//! @yah:verify("cargo test -p workload-spec")
120//! @yah:gotcha("Auto-delete is OFF — reconciler reports drift on bucket∖catalog files but never DELETEs. That's the prune verb (R429-T2). Easy bug to introduce when 'cleaning up drift'; don't.")
121//! @yah:gotcha("S3 multipart upload threshold matters — distil-large-v3 is ~270MB which is over the 5MB single-PUT limit on R2's strictest mode. Use aws-sdk-s3's multipart helper for assets >100MB.")
122//! @yah:gotcha("Long-running progress MUST surface in QED/task-pane per the long-running-yah-surface rule. Don't silently spin in a tokio task; model as a Task with progress events.")
123//! @arch:see(.yah/docs/working/W160-atomic-release-waves.md)
124//! @yah:depends_on(R429-T1)
125//!
126//! @yah:ticket(R429-T3, "yah service prune verb: candidate enumeration + operator-confirm delete")
127//! @yah:assignee(agent:claude)
128//! @yah:at(2026-06-03T23:24:52Z)
129//! @yah:status(review)
130//! @yah:phase(P3)
131//! @yah:parent(R429)
132//! @yah:next("yah service prune <service-name> enumerates files present in the bucket but not referenced by any current mirror's resolved alias graph. Lists candidates + sizes + last-modified, requires explicit operator confirm before DELETE.")
133//! @yah:next("Resolution graph: for each mirror, walk [asset_aliases] → catalog [aliases] → catalog [[asset]] rows. Union across all mirrors = live set. Bucket ∖ live set = prune candidates.")
134//! @yah:next("MCP tool mcp__yah__service_prune routes through approval gate (write verb). Read counterpart mcp__yah__service_prune_status auto-passes — returns the candidate list without acting.")
135//! @yah:next("Camp: Tauri command + a 'Prune candidates' panel in the existing DeployPanel for each service, showing the candidate table with per-row checkboxes + confirm.")
136//! @yah:next("Analytics-driven candidate filter (old AND unaccessed-for-N-days) is OUT OF SCOPE for this ticket — needs access logs we don't aggregate yet. The candidate set today is purely catalog-derived.")
137//! @yah:next("User-asset TTL is OUT OF SCOPE — different surface, access-pattern-based, separate relay when it lands.")
138//! @yah:verify("cargo test -p <prune-crate>")
139//! @yah:verify("yah service prune yah-desktop --dry-run lists candidates")
140//! @arch:see(.yah/docs/working/W160-atomic-release-waves.md)
141//! @yah:depends_on(R429-F2)
142//! @yah:handoff("CLI + library + MCP all landed; UI deferred to R429-F4 (filed). Library lives in crates/yah/cloud/src/reconciler/static_asset_prune.rs and exposes compute_live_set (pure resolution graph), compute_prune_candidates (live + LIST + diff), execute_prune (DELETE), and load_service_and_mirror (path helper). CLI verb is `yah cloud service prune <name> --env <env> [--dry-run] [--yes] [--format=table|json]` at app/yah/cli/src/cloud.rs (ServiceCommands::Prune + handle_service_prune). MCP tools cloud.service_prune_status (read, auto-pass, --dry-run --format=json) and cloud.service_prune (write, --yes --format=json) dispatch through build_command(). New S3 helper sign_s3_get_with_query in local-driver covers ListObjectsV2 (the existing s3_sign helpers don't handle canonical query strings); ListObjectsV2 response is parsed with a tiny hand-rolled split_tags helper to avoid a quick-xml workspace dep. Tests: 12 prune-module unit tests (live-set union, kind filtering, list response parse for single/empty/truncated/no-token, candidate filtering including catalog manifest sidecar exclusion) + 1 s3_sign helper test + 2 MCP build_command tests. cargo check --workspace clean. cargo test -p cloud --lib: 279 pass (1 pre-existing failure cloud_init::tests::embedded_template_matches_workspace_canonical unrelated, per R419-F4 docstring). cargo test -p yah --lib: 299 pass.")
143//! @yah:next("R429-F4 carries the Tauri + DeployPanel UI work — depends_on R429-T3, status=open.")
144//! @yah:verify("cargo check --workspace --locked")
145//! @yah:verify("cargo test -p cloud --lib reconciler::static_asset_prune  # 12 pass")
146//! @yah:verify("cargo test -p yah --lib mcp::tools::tests::cloud_service_prune  # 2 pass")
147//! @yah:verify("yah cloud service prune --help  # renders usage with --env/--dry-run/--yes/--format")
148//!
149//! @arch:see(.yah/docs/working/W164-derived-static-assets.md)
150//!
151//! @yah:ticket(R438-T2, "AssetEntry XOR: source vs derive + shape_static_asset rules")
152//! @yah:assignee(agent:claude)
153//! @yah:at(2026-06-04T21:06:51Z)
154//! @yah:status(review)
155//! @yah:phase(P1)
156//! @yah:parent(R438)
157//! @yah:next("AssetEntry.source: PathBuf → Option<PathBuf>")
158//! @yah:next("Add AssetEntry.derive: Option<AssetDerive> with fetch + optional transform")
159//! @yah:next("Extend shape_static_asset to enforce exactly-one(source, derive) + license closed-set")
160//! @yah:verify("Both-set and neither-set fail shape validation with ShapeError::Field")
161//! @yah:verify("Legacy TOMLs with only source still parse + serialize identically")
162//! @arch:see(.yah/docs/working/W164-derived-static-assets.md)
163//! @yah:handoff("AssetEntry now carries Option<PathBuf> source + Option<AssetDerive> derive (both skip_serializing_if). New types AssetDerive {fetch: FetchSource, transform: Option<TransformSpec>} and TransformSpec {recipe, params} added to workload-spec/src/lib.rs. validate.rs grew FieldPath::Asset(usize, &'static str) and shape_static_asset enforces XOR: both-set or neither-set fail with ShapeError::Field { path: Asset(i, \"source\") }. 4 new tests cover derive-mode round-trip, legacy source-only TOML round-trip without leaking a derive field, both-set rejection, neither-set rejection, and both-modes-accepted positive case. Cloud reconciler (static_asset.rs:360) now bails on derive-mode with a pointer to R438-T5 until the materialize step lands. 3 test fixtures updated with source: Some(...) + derive: None. export-ts regenerated (TransformSpec + AssetDerive emitted); xtask emit-schemas regenerated workload.toml.schema.json; schema_drift test green. workload-spec: 24/24, cloud static_asset: 25/25, xtask: 2/2.")
164//!
165//! @yah:ticket(R438-T3, "ImageRef digest-pin enforcement at deserialize")
166//! @yah:assignee(agent:claude)
167//! @yah:at(2026-06-04T21:06:55Z)
168//! @yah:status(review)
169//! @yah:phase(P1)
170//! @yah:parent(R438)
171//! @arch:see(.yah/docs/working/W164-derived-static-assets.md)
172//! @arch:see(.yah/docs/working/W165-mesofact-build-mode-lowering.md)
173//! @yah:handoff("ImageRef now accepts either a string form (digest-pinned, W164/W165 path) or the legacy struct form (backwards-compat for WorkloadSpec configs). String form requires @sha256:<hex> suffix: bare-tag, non-sha256, and non-hex digests all reject at serde-deserialize. Single parser compose_import::parse_pinned_image_ref is the rule's one home; T4 (recipes) and T6 (BuildMode::InContainer) will both deserialize images through this string path. Custom Deserialize uses untagged enum (Pinned(String) | Struct(Fields)); Serialize/TS/JsonSchema derives stay on the struct so wire output and TS exports are unchanged. 6 new tests cover: bare-tag reject, pinned accept (docker.io + ghcr.io), non-sha256 algorithm reject, empty/non-hex digest reject, struct-form still works with digest=None, struct-form TOML round-trip. workload-spec: 30/30 lib + 18/18 semantic + 6/6 shape_fixtures. xtask schema_drift green after emit-schemas regen. cargo check --workspace clean.")
174//! @yah:next("Tighten ImageRef workspace-wide: digest: Option<String> → digest: String (required). tag stays as the human-readable identifier; digest is the source of truth. Rationale: every image we execute should be reproducible-by-construction; the on-disk shape should make unpinned-image bugs impossible.")
175//! @yah:next("Every existing ImageRef construction site updates to pass a digest. Call sites known today (~10): yubaba integration tests (fake digests via a test helper), yubaba/runtime/{containerd,fake}, yubaba/deploy/{mesh_resolve,env_validate}, local-runtime, cloud/config, workload-spec round_trip tests, restart_policy tests, compose_import::parse_image_ref. The break is bounded — single PR, no surprise call sites outside the workspace.")
176//! @yah:next("compose_import::parse_image_ref returns Result<ImageRef, ParseImageRefError> with an UnpinnedImage variant. Docker-compose strings without @sha256: become an explicit parse error — callers must pre-resolve tags to digests (most compose imports already happen at yubaba submission time where a pinning pass can run).")
177//! @yah:next("Add task::local::test_support::test_digest() (or similar) for test fixtures — a fixed valid-format sha256 string so tests don't have to mint their own.")
178//! @yah:next("Recipe TOML loader (T4) and W165 BuildMode::InContainer (T6) inherit the new requirement for free — they consume ImageRef and digest is now structurally required.")
179//! @yah:verify("cargo check --workspace --locked passes after the tightening + call-site migration (yubaba, runtime, local-runtime, mesh_resolve, env_validate, cloud/config, compose_import)")
180//! @yah:verify("ImageRef without digest no longer constructs — parse-time + type-level enforcement. cargo test -p workload-spec round_trip + restart_policy pass with updated fixtures.")
181//! @yah:verify("compose_import::parse_image_ref(\"node:20\") → Err(UnpinnedImage); parse_image_ref(\"node:20@sha256:...\") → Ok.")
182//! @yah:verify("cargo test -p yubaba --tests + -p local-driver passes with new test_digest() helper in place of bare tags.")
183//! @yah:gotcha("Earlier framing assumed T3 needed a PinnedImageRef newtype to avoid breaking yubaba. Reversed after design discussion 2026-06-04: breaking yubaba in service of reproducibility-by-construction is the right architectural move. Digest is required workspace-wide; tag stays as a human-readable identifier. ~10 call sites migrate in one PR.")
184//! @yah:gotcha("MesofactStaticWorkload.build_mode → InContainer { image } today is declared but never executed (build always runs on host — see W165). Once T3 tightens ImageRef, the wired-up build_mode lowering in T6 inherits digest-pinning automatically, closing W165 OQ#1's escape hatch.")
185//! @yah:assumes("No production yubaba deployment ships ImageRefs we don't already digest-pin. Spot-check Hetzner/cloud yah-castle workload specs before merging the tightening; if any prod path uses tag-only, it gets pinned in the same PR.")
186//! @yah:handoff("Pushed back from review 2026-06-04 — user reaffirmed the workspace-wide tightening direction. What landed (untagged Deserialize accepting string-form OR struct-form-with-digest:Option) ships digest enforcement at the W164/W165 wire surfaces but leaves the struct-form escape hatch (digest: None still constructs). User: 'breaking yubaba in order to improve it architecturally is fine'. Final shape needs both: (a) keep the string-form parser as a recipe-author convenience (image = \"ghcr.io/x@sha256:...\"), AND (b) tighten the struct form's digest: Option<String> → String. Then both paths land at the same digest-required field and unpinned-image bugs become impossible by construction. ~10 call sites still need migration (yubaba/runtime/{containerd,fake}, yubaba/deploy/{mesh_resolve,env_validate}, local-driver/local_runtime, cloud/config, workload-spec tests/round_trip + tests/restart_policy + yubaba integration_* + yubaba/tests/integration_public_ingress + integration_operator_bridge + integration_mesh + integration_single_node). Add task::local::test_support::test_digest() returning a fixed valid-format sha256 string. Pick this up by claiming R438-T3.")
187//! @yah:handoff("Workspace-wide ImageRef tightening landed. (a) ImageRef.digest: Option<String> → String at workload-spec/src/lib.rs:923; Deserialize struct arm now requires the field; untagged string-form parser at compose_import::parse_pinned_image_ref untouched. (b) ImageRef::docker_ref() now always emits tag@digest pair (informational tag alongside content-addressed digest). (c) validate.rs ImageTag check tightened: tag must be non-empty (digest presence is type-enforced now). (d) compose_import::parse_image_ref returns Result<ImageRef, String> — alias for parse_pinned_image_ref. import_compose gained ImportError::UnpinnedImage { service, image, reason } variant; only external caller (yah workload import in app/yah/cli/src/workload.rs) propagates the error type. (e) New workload_spec::testing module (doc-hidden) exposes TEST_DIGEST const + test_digest() fn — all-zeros 64-hex sentinel. (f) task::default_image::catalog_image falls back to testing::test_digest() when the per-image env var is unset, preserving the infallible API but making unset-digest visible at runtime via docker pull failure. default_buildkit_image follows the same pattern. (g) Migrated ~22 struct-form construction sites: workload-spec tests (round_trip/semantic/restart_policy + all 15 fixture JSONs + 3 compose YAML fixtures + matching expected.json), task crate (default_image/integration/lib/local/remote), yubaba (runtime/{containerd,fake}, deploy/{mesh_resolve,env_validate}, all 4 integration_*.rs files), cloud/config (3 sites), local-driver (local_runtime + pond_ssr_runtime), scryer/beholders, kamaji/{server,native,containerd}. (h) ImageSource::pull trait signature tightened: digest: Option<&'a str> → digest: &'a str (only one impl in yubaba/env_validate). (i) Read-site cleanup: yubaba::runtime::containerd::image_ref, kamaji::containerd::image_ref, local-driver::pond_ssr_runtime::compose_image_ref, task::local::image_ref_arg — all dropped Option ceremony, always emit tag@digest. cargo check --workspace clean. cargo test -p workload-spec: 82 pass. cargo test -p task --lib: 59 pass. Pre-existing test failures in cloud (5: 1 cloud_init drift, 4 mesofact_static adopt) and yubaba tests (pond_reconciler_smoke missing ssr_runtime/worker_mode/ssr_origin fields) are unrelated to ImageRef — separate ticket. R438-T4 (recipe loader) and R438-T6 (BuildMode::InContainer) inherit digest-required structurally with zero per-consumer work.")
188//!
189//! @yah:ticket(R438-T7, "Golden tests: recipe→ForgeSpec lowering + BuildMode→ForgeSpec lowering parity")
190//! @yah:assignee(agent:claude)
191//! @yah:at(2026-06-04T21:07:30Z)
192//! @yah:status(review)
193//! @yah:phase(P3)
194//! @yah:parent(R438)
195//! @yah:next("Golden test: sample transform recipe + asset.derive.transform.params lowers to expected ForgeSpec (argv, image digest, TaskPlacement)")
196//! @yah:next("Golden test: MesofactStaticWorkload with build_mode=in_container lowers to expected ForgeSpec")
197//! @yah:next("Round-trip parity: same Subprocess + Local + Container quadrant for both consumers; regression-guards argv-substitution and image-pin drop-through")
198//! @yah:verify("cargo test -p workload-spec lowering_golden_*")
199//! @yah:verify("Golden files versioned; updates require explicit --update flag")
200//! @arch:see(.yah/docs/working/W164-derived-static-assets.md)
201//! @arch:see(.yah/docs/working/W165-mesofact-build-mode-lowering.md)
202//! @yah:depends_on(R438-T5)
203//! @yah:depends_on(R438-T6)
204//! @yah:handoff("T7 landed. (1) Extracted pure lowering helpers exposed at pub(crate):\\n  - mesofact_static::lower_build_to_forge_spec(workload_dir, &BuildConfig, &BuildMode) -> ForgeSpec (run_build now wraps this)\\n  - static_asset::lower_recipe_step_to_forge_spec(&TransformRecipe, &RecipeStep, substituted_argv) -> ForgeSpec (materialize_transform now calls this for each step)\\n(2) New cfg(test) module crates/yah/cloud/src/reconciler/lowering_golden.rs registered from reconciler/mod.rs. Five golden tests:\\n  - golden_recipe_step_lowers_to_pinned_local_container_subprocess (recipe → ForgeSpec shape: argv, image digest, timeout, label, initiator)\\n  - golden_recipe_step_with_zero_timeout_lowers_to_none (regression-guards the timeout=0 → None mapping)\\n  - golden_build_in_container_lowers_to_pinned_local_container_subprocess (BuildMode::InContainer → sh -c shell wrap + pinned image + cwd label)\\n  - golden_build_host_side_lowers_to_native_quadrant_without_image (BuildMode::HostSide → image=None + TaskRuntime::Native)\\n  - parity_recipe_and_build_in_container_share_quadrant (THE architectural invariant: both consumers land in the same Subprocess + Local + Container quadrant with sha256-pinned images and Gnome initiators — lets one ForgeExecutor dispatch handle both)\\n(3) Test artifacts are hand-coded assertions, not insta/snapshot files — workspace has no insta infra and explicit-Pin tests give clearer diff on drift than auto-update snapshots. The W164/W165 lowering shape is now regression-guarded against silent drift in either consumer. cargo test -p cloud --lib reconciler::lowering_golden: 5 pass. Workspace check clean.")
205//! @yah:next("Sign off → archive R438-T7")
206//! @yah:next("T8 (worked examples) now has tested lowering primitives to reference")
207//! @yah:verify("cargo test -p cloud --lib reconciler::lowering_golden — 5 pass")
208//! @yah:verify("cargo test -p cloud --lib reconciler:: — 124 pass; 4 pre-existing R441-B4 adopt_only failures (port 4321 dev-box collision) unrelated")
209//! @yah:verify("cargo check --workspace --locked — clean (warnings only)")
210//! @yah:verify("Parity test asserts both lowerings produce TaskPlacement{Local, Container} + ForgeCommand::Subprocess + sha256-pinned image — the shared executor dispatch invariant")
211//! @yah:gotcha("Test location pivot: original ticket said `cargo test -p workload-spec lowering_golden_*` but the lowering primitives don't live in workload-spec — ForgeSpec/TaskPlacement are in task, and the actual lowering helpers are in cloud (both consumers live there). Tests landed in cloud as `reconciler::lowering_golden`. If a future consumer outside cloud needs the BuildMode lowering, lift `lower_build_to_forge_spec` up to task::transforms alongside the existing recipe lowering primitives.")
212//! @yah:gotcha("No snapshot/insta infra in workspace — 'Golden files versioned; updates require explicit --update flag' verify line interpreted as hand-coded explicit assertions instead. Drift surfaces as a single-file test diff on the lowering helper, which is more readable than a .snap diff for the small ForgeSpec shape these tests cover.")
213//!
214//! @yah:ticket(R594-F2, "Ingress workload kind in workload-spec: pinned-per-node appliance on public-ip-tainted machines")
215//! @yah:status(review)
216//! @yah:assignee(agent:claude)
217//! @yah:at(2026-07-03T06:03:30Z)
218//! @yah:phase(P2)
219//! @yah:parent(R594)
220//! @yah:next("Add the ingress workload kind to the Workload enum (lib.rs:296) as an appliance in the R572 archetype sense: pinned-per-node, non-drainable, placed by yubaba only on machines carrying a public-ip taint, supervised by kamaji. Depends on R572-F1 (lifecycle archetype discriminator) so the archetype field exists to mark it. Breaking change is fine (pre-release house style); update kamaji-bin server.rs InvalidSpec rejection list deliberately — kamaji MUST accept this kind (it supervises the proxy), unlike MesofactStatic/Almanac/StaticAsset.")
221//! @yah:verify("cargo test -p yah-workload-spec; cargo check -p yubaba -p kamaji-bin; kamaji admission accepts kind=ingress in a unit fixture")
222//! @yah:gotcha("RUNS SOLO: workload-spec is the shared-type DAG sink (yah-base) — every lane (yubaba, kamaji, qed, host app) rebuilds on its change. Pause all other wave-2/3 implementer lanes while this is active, and check R572-T2 (cpu_millis, Handoff) + R572-F1 owner state before claiming — same file.")
223//! @yah:depends_on(R572-F1)
224//! @yah:tier(Cleric)
225//! @yah:handoff("Modeled the W267 public-ingress appliance as a container-shaped workload (Workload::Container(WorkloadSpec)), not a new Workload variant: mark archetype = Some(LifecycleArchetype::Appliance) (R572-F1, pinned/non-drainable) and declare the public-ip placement requirement via a new annotation-based marker on WorkloadSpec (same zero-blast-radius pattern as existing wants_host_network/HOST_NETWORK_ANNOTATION, chosen specifically to avoid the ~26-call-site churn a new plain field forced for R572-F1's archetype field, and to avoid an exhaustive-match update in peer-owned kamaji-proto/codec.rs that a new Workload variant would force). Added: WorkloadSpec::requires_taint() -> Option<&str>, const REQUIRES_TAINT_ANNOTATION = \"yah.placement.requires-taint\", const PUBLIC_IP_TAINT = \"public-ip\", plus doc comments on Workload::Container recording the modeling decision and its rationale, all in oss/yah-base/crates/workload-spec/src/lib.rs (single file changed). This only declares the requirement as inert metadata — matching taint field on machine TOML is R572-F3 (not yet present) and scheduler enforcement is R572-F5; both out of scope here, noted in the doc comments. Verified kamaji needs NO change: deploy_workload's match in kamaji-bin/src/server.rs already dispatches any Workload::Container(_) to the containerd backend regardless of tier/annotations (only MesofactStatic/Almanac/StaticAsset hit the InvalidSpec rejection arm), confirmed by reading the code and by the existing deploy_container_without_feature_says_so / deploy_mesofast_static_is_rejected_as_invalid_spec unit tests both still passing unmodified. 2 new unit tests added (ingress_marked_spec_is_appliance_and_carries_public_ip_placement_requirement, ingress_marked_spec_round_trips_through_json_as_a_container_workload). cargo test -p yah-workload-spec --lib: 38/38 pass. cargo test -p yah-workload-spec --test round_trip: 7 pass, exactly the same pre-existing 2 postcard failures (round_trip_full_spec_through_postcard, workload_container_round_trips_through_postcard — R590-B3, unrelated) as before this change, confirmed not increased. cargo check -p yah-workload-spec / -p yubaba / -p kamaji-bin all clean, plus full cargo check --workspace in both oss/kamaji and oss/yubaba clean (only pre-existing unrelated warnings). No peer-owned file touched or needed.")
226//!
227//! @yah:ticket(R590-B10, "forge workload 256MB cgroup memory limit SIGKILLs real builds — rusty-v8 checkout OOMs (bumped to 32GB stopgap)")
228//! @yah:at(2026-07-12T00:14:52Z)
229//! @yah:status(review)
230//! @yah:assignee(agent:claude)
231//! @yah:parent(R590)
232//! @yah:severity(blocks-on-box-green)
233//! @yah:next("Proper fix: thread a per-step memory request from the pipeline (QedStep) through ForgeSpec -> WorkloadSpec so a build declares its footprint, instead of a blanket forge default. Also consider: build_oci_spec should treat memory_mb==0 as 'omit the cgroup limit' (unlimited) so dedicated build-workers aren't capped by an arbitrary constant; pair with a node-sized default. Revisit the 32GB stopgap once per-step resources land.")
234//! @yah:verify("yah qed run rusty-v8-musl on us-west-002 completes the checkout + gn/ninja compile without an OOM SIGKILL; a small forge task still runs (32GB is a ceiling, not a reservation).")
235//! @yah:gotcha("PROVEN live (2026-07-11): with B7 networking fixed, the rusty-v8 build cloned the full V8 tree then `git checkout third_party/icu` DIED OF SIGNAL 9 (OOM). WorkloadSpec::for_forge set resources.memory_mb=256, which build_oci_spec turns into a hard cgroup memory.limit. /tmp is a RAM-backed tmpfs so the multi-GB source checkout counts against that 256MB too. Bumped for_forge to 32768 (32GB) as a CLI-side stopgap; verified the build proceeds past icu.")
236//! @yah:handoff("FIXED + PROVEN LIVE (2026-07-11). WorkloadSpec::for_forge memory_mb 256 -> 32768 (oss/yah-base/crates/workload-spec/src/lib.rs). CLI-only change (spec is client-built), no kamaji redeploy. RESULT: with B7 networking, the rusty-v8 build previously OOM'd (SIGKILL/signal 9) at the icu git-checkout under the 256MB cgroup cap; now it clones the full V8 tree AND proceeds past icu into cargo/gn compilation (Compiling icu_locale_data/icu_calendar_data...) with task RUNNING. Stopgap 32GB ceiling; proper per-step memory request from the pipeline is the follow-up in the ticket body.")
237//!
238//! @yah:ticket(R546-B7, "workload_spec::Workload envelope is externally tagged (missing serde tag=kind) — no flat on-disk workload.toml can parse through it, broke yah cloud apply for EVERY static-asset component")
239//! @yah:phase(P1)
240//! @yah:status(review)
241//! @yah:assignee(agent:bundle-anthropic-ashguard)
242//! @yah:at(2026-08-03T00:44:43Z)
243//! @yah:parent(R546)
244//! @yah:next("DO NOT simply add `#[serde(tag = \"kind\")]` without checking postcard: `Workload` is also a postcard wire type on the kamaji RPC path (kamaji-proto/src/codec.rs matches on it; round_trip tests exist). postcard is non-self-describing and cannot decode internally-tagged enums, so naive tagging risks breaking the kamaji wire. Decide deliberately: (a) tag it and prove the postcard round-trips still pass, or (b) split the types — an on-disk `WorkloadManifest` with tag=kind, leaving `Workload` as the untagged wire type.")
245//! @yah:next("INTERIM FIX ALREADY LANDED (unblocks publishing): static_asset.rs::load_workload no longer routes through the envelope — it deserializes a small `KindProbe { kind }`, validates kind == \"static-asset\", then parses `StaticAssetWorkload` directly. Same approach seed_derivation_for_target already used successfully. This restored `yah cloud apply` and got the x86_64 rusty-v8 artifact published to the CDN (HTTP 200). The ENVELOPE itself is still broken for every other caller/kind.")
246//! @yah:next("Fix the test/example disagreement: lib.rs ~L2544 should assert the FLAT `kind = \"...\"` shape that real files use, and examples/parse_whisper_toml.rs should run in CI so this cannot regress silently again.")
247//! @yah:gotcha("SEVERITY: this silently broke `yah cloud apply` for EVERY static-asset component, not just rusty-v8. Verified against the long-published whisper catalog via the repo's own examples/parse_whisper_toml.rs, which panics with the identical error — so the breakage is general and pre-existing, not caused by the R546 hash edits.")
248//! @yah:gotcha("ROOT CAUSE: `pub enum Workload` (oss/yah-base/crates/workload-spec/src/lib.rs ~L386) derives Deserialize with ONLY `#[serde(rename_all = \"kebab-case\")]` — there is NO `#[serde(tag = \"kind\")]`, despite its own doc comment stating 'the `kind` field on the wire is the serde discriminator'. Without the tag it is EXTERNALLY tagged, so serde wants a map with exactly ONE key (the variant name). Every real workload.toml is FLAT (`kind = \"static-asset\"` + `schema_version` + `[[asset]]` + `[aliases]`), i.e. a multi-key map -> `TomlError: wanted exactly 1 element, more than 1 element`, reported confusingly at line 1 col 1.")
249//! @yah:gotcha("WHY THE UNIT TEST DIDN'T CATCH IT: the passing test at lib.rs ~L2544 feeds the EXTERNALLY-tagged shape `[[static-asset.asset]]`, which no on-disk file actually uses. So the test asserts the broken encoding and the example (parse_whisper_toml.rs) asserting the REAL encoding was never run in CI. The test and the example disagree; the example is right.")
250//! @yah:handoff("DONE. Workload now carries TWO wire shapes behind hand-written Serialize/Deserialize that branch on is_human_readable() -- option (a) and (b) from the ticket's next-steps merged into one type instead of splitting it. TOML/JSON get the INTERNAL `kind` tag (the flat shape every on-disk file uses); postcard keeps the EXTERNAL variant-index encoding R590-B3 established for the kamaji UDS. Same idiom ImageRef already used for its string-vs-struct form, so there is now one precedent, not two mechanisms. Mirror enums (WorkloadTagged/WorkloadExternal + borrowing twins) carry the two encodings; their variant ORDER is load-bearing for postcard and is commented as such. schemars/ts-rs get tag=kind + rename_all via #[schemars(...)]/#[ts(...)] so the generated JSON schema and TS bindings describe the on-disk shape instead of the wire shape.")
251//! @yah:handoff("SECOND BLOCKER, fixed in the same pass: after the tagging fix only 2 of 8 on-disk workload.toml files still parsed. SchemaVersion is a unit-variant enum wanting the string \"V1\", but 6 files (every mesofact-static + container + cloudflare-worker component) are authored `schema_version = 1`, and R438-T6 had worked around it by hand-extracting raw toml::Value subtrees in read_mesofact_build. Gave SchemaVersion a liberal-read/canonical-write Deserialize (accepts 1, \"V1\", \"v1\"; always serializes \"V1\"), on the same is_human_readable branch so postcard is untouched. B7's stated goal is not met without it -- a fixed envelope that still rejects 6 of 8 files is not fixed.")
252//! @yah:handoff("FILES. (1) oss/yah-base/crates/workload-spec/src/lib.rs -- Workload dual-shape impls + mirror enums; the lib.rs ~L2544 test that asserted the broken `[[static-asset.asset]]` encoding rewritten to the flat form, plus a new test pinning BOTH halves (flat kind in JSON, postcard round-trip). (2) .../src/version.rs -- SchemaVersion custom Deserialize + 3 tests. (3) .../src/bin/export-ts.rs -- path was 3 parents up from CARGO_MANIFEST_DIR, but 75d8df7e moved the crate under oss/yah-base and added a level, so since that commit the bin silently wrote to oss/yah-base/packages/ and the committed TS stopped tracking the Rust types (last real update Jun 28). Now 4. (4) scripts/check-workload-spec-ts.sh -- `cargo run -p yah-workload-spec` fails from the camp root (crate is in the excluded oss/yah-base workspace); switched to --manifest-path. It was dead since the same commit. (5) packages/yah/workload-spec/index.ts + .yah/schema/workload.toml.schema.json regenerated (mirror.toml.schema.json also moved -- that is @Ashguard:dragon's W267 ingress field swept in by the shared regen, not mine).")
253//! @yah:handoff("FIXTURE SWEEP (flagged by @Ashguard:dove mid-turn -- my change, my sweep): 8 yah-cloud tests were red on hand-written externally-tagged TOML. Fixed reconciler/derive_cache_prune.rs (2 fixtures), reconciler/static_asset_prune.rs (4), validate.rs (1), tests/whisper_derive_e2e.rs (1), app/yah/cli/src/cloud.rs (alias-collision fixture + the_deploy_body_parses_as_a_workload_envelope_not_a_bare_spec, which asserted external tagging and now asserts a flat `kind`). NOT touched: yubaba/src/lib.rs bundle_deploy_tests -- @Ashguard:dove already rewrote that one in their own relay's module and asked me not to double-fix.")
254//! @yah:handoff("COMMENTS CORRECTED, not left lying: static_asset.rs::load_workload and asset_status.rs both carried R546-B7 comments asserting the envelope is externally tagged and unusable. Both now say the envelope works and the direct StaticAssetWorkload parse is a deliberate shortcut (load_workload keeps it to produce a precise wrong-kind error naming the kind found; asset_status keeps it because component.kind is already checked upstream).")
255//! @yah:verify("THE TICKET'S OWN REPRO NOW PASSES: `cargo run --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --example parse_whisper_toml` -> 'parsed as StaticAsset / assets: 2 / aliases: 3 / shape validation: ok / populated-shape verifier: ok'. It panicked before against the long-published whisper catalog.")
256//! @yah:verify("NEW CI GATE (the ticket's third next-step): xtask/tests/workload_envelope.rs walks the whole camp for workload.toml, parses every file whose kind is one of the four modelled variants through workload_spec::Workload, and hard-asserts no error contains 'wanted exactly 1 element' -- the exact external-tagging signature. It lives in xtask, not workload-spec, because that crate is in the standalone-exported oss/yah-base workspace and cannot reach app/ or .yah/. Runs under the check pipeline's existing cargo-test step. Carries a SHRINK-ONLY KNOWN_GAPS list: a file that starts parsing FAILS the test until its entry is deleted, and a stale entry (file moved/deleted) also fails, so the list cannot rot or grow silently.")
257//! @yah:verify("GREEN: yah-workload-spec --all-features (55 lib + round_trip/postcard + shape_fixtures, 10 targets, 0 failed); kamaji-proto 25/25 incl. deploy_container_round_trip and deploy_string_pinned_image_ref_survives_postcard_wire (the exact UDS path R590-B3 fixed -- proves the binary branch is byte-unchanged); yubaba --lib 339/339; yah-cloud 610 lib + whisper_derive_e2e 1/1; yah --lib cloud:: 108/108; xtask schema_drift 2/2 and workload_envelope 1/1.")
258//! @yah:verify("NOT MINE, seen while verifying: (a) reconciler::pond::tests::{ensure_sim_port_free_ok_when_unbound, port_has_listener_...} flake in a full-suite run and pass in isolation -- they bind real ports and race on a busy dev box. (b) 10 arch::ticket::tests failures in `cargo test -p yah --lib` from a peer's in-flight @yah: annotation-parser work; app/yah/cli/src/arch/ticket.rs has zero references to workload_spec, so nothing in this change can reach them. (c) MirrorConfig's new `ingress` field (@Ashguard:dragon, W267/R594) broke the yah-cloud and yah-cli test builds mid-session; it cleared on its own as they swept call sites -- I did not touch their files.")
259//! @yah:gotcha("VARIANT ORDER IS LOAD-BEARING. WorkloadExternal / WorkloadExternalRef in lib.rs must list variants in the SAME order as Workload -- postcard encodes an external tag as the variant INDEX, so reordering or inserting a variant anywhere but the end silently decodes kamaji UDS frames into the wrong variant. There is no type error for this. Commented at the definitions; the round_trip postcard tests catch a mismatch only if the payload types differ enough to fail decode.")
260//! @yah:gotcha("TWO GAPS DELIBERATELY NOT CLOSED, filed as R658 (umbrella) -> R658-B1 (MesofactStaticWorkload.routes is a required top-level field but all 4 real files AND the CLI scaffold write it inside [build], so TOML scopes it to build.routes) and R658-B2 (kind = \"container\" selects two incompatible schemas: Workload::Container(WorkloadSpec) vs ContainerReconciler's local docker build/run shape). Neither is the B7 tagging bug -- they were invisible until the envelope started being exercised. B2 needs an operator naming decision. Both are pinned in workload_envelope.rs's KNOWN_GAPS so they cannot be forgotten or silently widened.")
261//!
262//! @yah:ticket(R626-S3, "Where does desired-state live? Durable per-workload replica count that survives reconcile loops and camp restarts (0↔1 vs scale-to-N)")
263//! @yah:status(review)
264//! @yah:assignee(agent:bundle-anthropic-glimmerstone)
265//! @yah:at(2026-07-23T17:47:24Z)
266//! @yah:kind(spike)
267//! @yah:phase(P3)
268//! @yah:parent(R626)
269//! @yah:handoff("DECIDED + LANDED. Desired state lives in the CAMP DAEMON, in a durable camp-local document at <camp>/.yah/state/desired-state.json, and NEVER crosses the kamaji or yubaba wire. The governing principle, written to survive the tier: desired state belongs to the DECLARER, not the supervisor — whoever re-asserts a deployment owns the record of whether it is wanted, because anything stored below the declarer is overwritten by the declarer's next re-assert. In the pond/dev tier the declarer is camp (ensure_pond_running -> reconcile_pond_deploys -> deploy_pond_mirrors, which runs at every camp start AND every pond.ensure_running RPC). In cloud the same rule points at the CloudConfig reconciler's raft store. Kamaji is never the holder in either tier.")
270//! @yah:handoff("REJECTIONS, with the reason each is not a near-miss. kamaji-local: kamaji is deliberately imperative (Deploy/Stop/List, crash-restart delegated to dockerd's policy per R626-F2) — it holds no desired set and runs no reconcile loop, so storing intent there means giving it a SECOND reconciler that can disagree with camp's, and it still loses to camp's POST /pond/deploy from above. yubaba raft: right answer at cloud scale, wrong scope here — the pond yubaba is a container camp starts, its PondRegistry is in-memory (a restart forgets everything), and a single-camp dev tier has no quorum to be consistent about. Git-tracked config: camp.toml/mirror.toml are the DECLARATION (what exists); a stop is per-machine operator intent (systemctl disable, not editing the unit file) and must not propagate to a teammate's checkout — hence .yah/state/ is gitignored, in both the camp's .gitignore and the scaffold_camp_skeleton template.")
271//! @yah:handoff("SHAPE: one knob, `replicas`, where 0 = stopped — deliberately the SAME axis as workload_spec::WorkloadSpec.replicas so scale-to-N later lifts a ceiling instead of adding a second concept beside a boolean. MAX_SUPPORTED_REPLICAS = 1 today and set_replicas REJECTS anything higher rather than persisting an intent no supervisor can honour (a clamp would silently record something the operator did not ask for). No record = replicas 1: a declared workload runs unless someone said otherwise. updated_at + reason ride along so a stale intent is legible and the UI can say when/why. Writes are tmp-then-rename; reads FAIL OPEN (missing/unreadable/corrupt/newer-schema all mean 'everything runs', corrupt file preserved as .corrupt-<epoch_ms>) — fail-closed would mass-stop a camp on one bad byte, and a resurrection is the recoverable failure.")
272//! @yah:handoff("LANDED: (1) app/yah/cli/src/desired_state.rs — DesiredStateDoc / WorkloadDesire / DesiredStateStore (load, desired_replicas, is_stopped, stopped_keys, set_replicas, stop, start, forget), 10 unit tests incl. survives-a-camp-restart, per-workload isolation, replicas>1 rejected AND not written, corrupt-file quarantine + fail-open, newer-schema fail-open, and an explicit 'a stop is not a failure' guard on the serialized document. (2) camp.rs: deploy_pond_mirrors and reconcile_pond_deploys now consult the store and skip stopped idents — this is THE enforcement point, since camp's re-assert is the only place 'stay stopped' can be honoured. Extracted pond_idents_needing_deploy(declared, registered, stopped) as a pure helper with 5 tests, because the stopped-subtraction is the load-bearing half: a stopped workload is absent from yubaba's registry ON PURPOSE and is indistinguishable from a failed deploy without the intent record. (3) .yah/.gitignore + app/yah/cli/templates/yah-gitignore-default gain /state. (4) .yah/docs/working/W287-desired-state-for-supervised-workloads.md carries the full rationale, the rejected options, the F4 build-on list, and the scale-to-N scoping.")
273//! @yah:handoff("DELIBERATE NON-GOAL: writing intent does NOT actuate. The durable record must land even when the stop call fails, or a failed stop comes back on the next reconcile. Actuation is R626-F4's job.")
274//! @yah:next("R626-F4 is unblocked and now has a concrete spec — see W287 §5. It needs three things this ticket deliberately did not build: (a) a per-ident teardown on yubaba (PondRegistry has only shutdown_all, which drains everything; /pond/deploy and /pond/state are the only pond routes), (b) camp RPC methods workload.stop / workload.start writing through DesiredStateStore, (c) desired-vs-actual reporting.")
275//! @yah:next("DO NOT add a Stopped variant to PondPhase (R626-F2's noted gap). PondPhase is yubaba's observation of REALITY; intent never crosses that wire by this decision. Camp is the one process holding both halves — render the pair instead: desired=stopped + actual=absent reads 'deliberately stopped'; desired=running + actual=absent reads 'down'.")
276//! @yah:next("Scale-to-N stays scoped, not committed (W287 §6). WorkloadSpec.replicas makes N look one constant away; it is not. kamaji native.rs:592 rejects replicas>1, and the docker backend names containers by mesh identity (one identity, one container). N needs a placement layer above the single-workload supervisor: per-replica naming (identity==container name is what makes teardown resolve), per-replica host ports (pond publishes fixed ones — two replicas collide), per-replica mesh identity (a load-balanced set is an xlb-net concern), and a placement decision that is yubaba's job on a fleet. Lifting MAX_SUPPORTED_REPLICAS is the entry point once that layer exists.")
277//! @yah:next("Wire DesiredStateStore::forget into the undeclare path so the document doesn't accumulate intent for mirrors that no longer exist.")
278//! @yah:verify("cargo test -p yah --lib desired_state — 15 pass (10 desired_state::tests + 5 camp::r626_s3_desired_state_gate_tests), 0 fail")
279//! @yah:verify("cargo check -p yah — clean (note: this camp's tree is shared and was transiently broken by peers' in-flight edits in oss/qed, yah-party, and yah-almanac during this run; none touched by this ticket)")
280//! @yah:verify("BEHAVIOUR BAR (the one that matters): DesiredStateStore::for_camp(root).stop(ident) followed by a FRESH store over the same root still reports is_stopped — that is exactly a camp restart — and pond_idents_needing_deploy then omits that ident from an EMPTY registry, which is exactly a restarted yubaba. Asserted in camp::r626_s3_desired_state_gate_tests::a_stop_survives_a_camp_restart_end_to_end.")
281//! @yah:gotcha("The store is camp-local and GITIGNORED on purpose. If a future ticket wants a stop to be shared/durable in the repo, that is a different decision (declaration vs intent) — re-open W287 §2 rather than moving the file into tracked territory.")
282//! @yah:gotcha("Reads fail OPEN. Never 'harden' this into fail-closed: an unreadable document would then stop an entire camp, and the failure would be silent (nothing starts) rather than visible (the workload comes back).")
283//!
284//!
285//! @yah:relay(R658, "workload.toml envelope: two type-vs-reality mismatches R546-B7 uncovered but did not fix")
286//! @yah:at(2026-08-03T00:43:00Z)
287//! @yah:status(open)
288//! @yah:assignee(agent:bundle-anthropic-ashguard)
289//! @yah:parent(R546)
290//!
291//! @yah:ticket(R658-B1, "MesofactStaticWorkload.routes is a required top-level field, but every real file and the CLI scaffold write it inside [build]")
292//! @yah:at(2026-08-03T00:43:04Z)
293//! @yah:status(open)
294//! @yah:assignee(agent:bundle-anthropic-ashguard)
295//! @yah:parent(R658)
296//! @yah:next("REPRO: `cargo test -p xtask --test workload_envelope` with the file's KNOWN_GAPS entry deleted -> `missing field `routes``. Affects app/yah/web/marketing/workload.toml, external/scrabcake/site/workload.toml, .yah/infra/state/sources/scrabcake/site/site/workload.toml, oss/yubaba/crates/cloud/testdata/mesofact-in-container/workload.toml.")
297//! @yah:next("ROOT CAUSE: TOML scopes every key after a table header into that table. All four files write `routes = \"./mesofact.routes.ts\"` AFTER `[build]`, so it deserializes as `build.routes` -- but MesofactStaticWorkload declares `routes` as a required TOP-LEVEL field. BuildConfig ignores the unknown key, so it vanished silently.")
298//! @yah:next("THE SCAFFOLD AGREES WITH THE FILES, NOT THE TYPE: SITE_WORKLOAD_TOML in app/yah/cli/src/cloud.rs (~line 4977) emits `routes` inside [build] too, so every newly scaffolded site inherits the mismatch. Fix the type or fix the scaffold -- but they must agree, and whichever moves needs the other four files migrated with it.")
299//! @yah:next("WHY IT WENT UNNOTICED: nothing reads `routes` off the envelope. mesofact-static's reconciler never loads MesofactStaticWorkload whole (read_mesofact_build does raw toml::Value subtree extraction, R438-T6), and mesofact-build reads mesofact.routes.ts directly. The field is declared but dead.")
300//! @yah:next("AFTER FIXING: delete the four `missing field `routes`` entries from KNOWN_GAPS in xtask/tests/workload_envelope.rs -- that test FAILS on a stale entry, so it will tell you.")
301//!
302//! @yah:ticket(R658-B2, "kind = \"container\" means two incompatible shapes — Workload::Container(WorkloadSpec) vs ContainerReconciler's local docker build/run")
303//! @yah:at(2026-08-03T00:43:24Z)
304//! @yah:status(open)
305//! @yah:assignee(agent:bundle-anthropic-ashguard)
306//! @yah:parent(R658)
307//! @yah:next("REPRO: `cargo test -p xtask --test workload_envelope` with the KNOWN_GAPS entry for crates/yah/cloud-admin/workload.toml deleted -> `missing field `image``.")
308//! @yah:next("THE COLLISION: workload_spec::Workload maps `kind = \"container\"` to WorkloadSpec -- an OCI spec with a required digest-pinned `image`, handed to yubaba over the kamaji wire. ContainerReconciler (oss/yubaba/crates/cloud/src/reconciler/container.rs, R602-T1) reads the SAME discriminator as a local-docker shape: `[build] dockerfile/context/image` + `[run] port/host_port/[run.env]`. crates/yah/cloud-admin/workload.toml is the second shape and cannot parse as the first.")
309//! @yah:next("SO ONE `kind` STRING SELECTS TWO INCOMPATIBLE SCHEMAS, and which one you get depends on which loader happens to read the file. Nothing detects the mismatch -- ContainerReconciler parses its own struct, so the envelope is never consulted for these files.")
310//! @yah:next("DECISION NEEDED (operator call, do not pick unilaterally): (a) rename the local-docker kind (e.g. `local-container`) and migrate crates/yah/cloud-admin/workload.toml + the ContainerReconciler dispatch, or (b) model the local-docker shape as a second Workload variant so the envelope covers both, or (c) declare the envelope non-authoritative for `container` and document it. (a) is the smallest and the only one that makes the discriminator honest.")
311//! @yah:next("AFTER FIXING: delete the `missing field `image`` entry from KNOWN_GAPS in xtask/tests/workload_envelope.rs.")
312
313use std::collections::BTreeMap;
314use std::collections::HashMap;
315use std::path::PathBuf;
316
317use serde::{Deserialize, Serialize};
318use ts_rs::TS;
319
320pub mod compose_import;
321pub mod control_plane_install;
322pub mod rollout;
323pub mod secrets;
324pub mod validate;
325mod version;
326
327pub use version::SchemaVersion;
328
329// ── Duration ──────────────────────────────────────────────────────────────────
330
331/// Duration expressed as an integer millisecond count.
332///
333/// Used for healthcheck intervals, timeouts, delays, and stop grace periods.
334/// Chosen over `std::time::Duration` to keep serde support dependency-free.
335#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
336#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
337#[ts(type = "number")]
338pub struct Millis(pub u64);
339
340impl Millis {
341    pub fn from_secs(s: u64) -> Self {
342        Self(s * 1000)
343    }
344
345    pub fn from_ms(ms: u64) -> Self {
346        Self(ms)
347    }
348
349    pub fn as_ms(self) -> u64 {
350        self.0
351    }
352
353    pub fn as_secs_f64(self) -> f64 {
354        self.0 as f64 / 1000.0
355    }
356}
357
358// ── Primitive newtypes ────────────────────────────────────────────────────────
359
360/// Opaque identifier for a yubaba-managed machine within the cluster.
361///
362/// Used by the semantic validation layer for admission-control capacity checks.
363/// Yubaba passes its own machine ID when validating a spec before deployment.
364#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TS)]
365#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
366pub struct MachineId(pub String);
367
368/// DNS-segment identity for a workload on the cluster mesh, e.g.
369/// `"noisetable-api.pdx"`. Regex constraint: `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`,
370/// length ≤ 63. Enforced in shape validation (R090-F2).
371#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
372#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
373pub struct MeshIdent(pub String);
374
375/// Tier classification that governs admission control and mesh `allow_from`
376/// filtering. Known values: `"public"`, `"tenant"`, `"private"`, `"infra"`.
377/// Custom tiers are allowed per cluster; shape validation warns on unknowns
378/// rather than rejecting them (R090-F2).
379#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
380#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
381pub struct TierTag(pub String);
382
383/// Default single-tenant identity written to specs that predate the tenant
384/// axis (W206). Its concrete string is arbitrary — what matters is that a
385/// single-tenant cluster only ever sees this one value, so every per-tenant
386/// isolation primitive collapses to a no-op. See [`TenantId::singleton`].
387pub const DEFAULT_TENANT: &str = "default";
388
389/// Default single-namespace identity for specs that predate the namespace
390/// axis (W206). See [`NamespaceId::singleton`].
391pub const DEFAULT_NAMESPACE: &str = "default";
392
393/// Tenant **isolation** axis (W206). Separates one operator's workloads from
394/// another's at the network / DB / mesh-identity level. Orthogonal to
395/// [`NamespaceId`] (routing/naming) and [`TierTag`] (workload class within a
396/// `(tenant, namespace)` pair).
397///
398/// **Degenerate case:** when a yubaba reconciler sees only one `TenantId`
399/// across every workload on a machine, per-tenant Podman networks collapse
400/// into the shared tier networks, the tenant prefix on mesh identity is
401/// dropped, and PostgreSQL role separation is skipped — isolation primitives
402/// become no-ops. You pay only when more than one tenant is present. Specs
403/// written before this axis existed deserialize to [`TenantId::singleton`].
404#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, TS)]
405#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
406pub struct TenantId(pub String);
407
408impl TenantId {
409    /// The singleton tenant used for back-compat with single-tenant (current)
410    /// deployments. Specs written before the tenant axis existed deserialize
411    /// to this value via the `#[serde(default)]` on [`WorkloadSpec::tenant`],
412    /// keeping the whole cluster single-tenant so every isolation primitive
413    /// stays a no-op.
414    pub fn singleton() -> Self {
415        Self(DEFAULT_TENANT.to_string())
416    }
417
418    /// Whether this is the singleton (degenerate single-tenant) identity.
419    pub fn is_singleton(&self) -> bool {
420        self.0 == DEFAULT_TENANT
421    }
422}
423
424/// Namespace **routing/naming** axis (W206). A pure naming key that never
425/// affects isolation: it selects the config root, disambiguates service DNS
426/// names within a tenant, prefixes object-store bucket names within a tenant's
427/// bucket scope, and selects the provider zone (e.g. `noisetable.com` vs
428/// `yah.dev`). Two namespaces in the same tenant share networks, mesh-identity
429/// space, and PG cluster — they simply cannot collide on workload names or
430/// external domains. Specs written before this axis existed deserialize to
431/// [`NamespaceId::singleton`].
432#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, TS)]
433#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
434pub struct NamespaceId(pub String);
435
436impl NamespaceId {
437    /// The singleton namespace used for back-compat with single-namespace
438    /// (current) deployments. Specs written before the namespace axis existed
439    /// deserialize to this value via the `#[serde(default)]` on
440    /// [`WorkloadSpec::namespace`].
441    pub fn singleton() -> Self {
442        Self(DEFAULT_NAMESPACE.to_string())
443    }
444
445    /// Whether this is the singleton (degenerate single-namespace) identity.
446    pub fn is_singleton(&self) -> bool {
447        self.0 == DEFAULT_NAMESPACE
448    }
449}
450
451// ── Workload (on-disk envelope) ──────────────────────────────────────────────
452
453/// On-disk `workload.toml` manifest. Each variant matches one
454/// `ServiceComponent.kind` value; the `kind` field on the wire is the serde
455/// discriminator.
456///
457/// This is the **on-disk** envelope — distinct from [`WorkloadSpec`], the
458/// containerd wire format yubaba receives over RPC. A `kind = "container"`
459/// workload deserializes its remaining fields as a `WorkloadSpec`; other
460/// kinds carry their own per-reconciler payload shape.
461///
462/// **Never put `#[serde(skip_serializing_if = "Option::is_none")]` on a field
463/// of this enum or any type it reaches.** These types ride the kamaji-proto
464/// **postcard** wire, which is non-self-describing and positional:
465/// `skip_serializing_if` omits the field's byte on serialize while decode still
466/// expects to read it at that offset, so the byte stream misaligns and the
467/// round-trip fails. Use `#[serde(default)]` + `#[ts(optional = nullable)]`
468/// instead — that still gives TOML/JSON back-compat (missing field → `None`)
469/// while the field is always encoded. `MesofactStaticWorkload::ssr_runtime` and
470/// `::serve_bundle` are the reference shape.
471/// **Two wire shapes, one type (R546-B7).** `Serialize`/`Deserialize` are
472/// hand-written and branch on [`is_human_readable`](serde::Deserializer::is_human_readable):
473///
474/// - **TOML/JSON (human-readable)** → *internally* tagged on `kind`, i.e. the
475///   flat shape every on-disk `workload.toml` actually uses
476///   (`kind = "static-asset"` beside `schema_version`, `[[asset]]`, `[aliases]`).
477/// - **postcard (binary)** → *externally* tagged, byte-identical to the derived
478///   representation R590-B3 established for the kamaji UDS.
479///
480/// Why not just `#[serde(tag = "kind")]`: internal tagging buffers through
481/// `deserialize_any`, which postcard (non-self-describing) refuses with
482/// `WontImplement` — that is exactly the failure R590-B3 fixed by flipping this
483/// enum to external tagging. But external tagging wants a single-key map, so
484/// every flat on-disk file then failed with `wanted exactly 1 element, more
485/// than 1 element` and `yah cloud apply` broke for every static-asset
486/// component. Branching on the format satisfies both, and mirrors what
487/// [`ImageRef`] already does for its string-vs-struct form.
488#[derive(Debug, Clone, PartialEq, TS)]
489#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
490#[cfg_attr(
491    feature = "json-schema",
492    schemars(tag = "kind", rename_all = "kebab-case")
493)]
494#[ts(tag = "kind", rename_all = "kebab-case")]
495pub enum Workload {
496    /// Static-site build that publishes an artifact directory to the
497    /// service's `static` provider slot. Reconciled by the
498    /// `mesofact-static` reconciler — does not deploy to yubaba.
499    MesofactStatic(MesofactStaticWorkload),
500
501    /// Containerd workload handed to yubaba over RPC. The inline fields
502    /// are the full [`WorkloadSpec`] minus the `kind` discriminator.
503    ///
504    /// This is also the shape of the W267 sovereign-public-ingress appliance
505    /// (R594-F2): a container-kind workload with `archetype =
506    /// Some(LifecycleArchetype::Appliance)` and
507    /// `requires_taint() == Some(PUBLIC_IP_TAINT)`, **not** a dedicated
508    /// `Workload::ingress(..)` variant. It runs an ordinary OCI image (the
509    /// `passway` proxy, R594-F4) supervised by kamaji exactly like any other
510    /// `Container`, so no admission-list or wire-codec change was needed to
511    /// let kamaji accept it. A new enum variant would have forced an
512    /// exhaustive-match update in every `Workload` consumer, including
513    /// peer-owned `kamaji-proto/src/codec.rs` — the archetype + annotation
514    /// combination expresses "this is the public ingress appliance" without
515    /// that blast radius. See [`WorkloadSpec::requires_taint`] and
516    /// [`LifecycleArchetype::Appliance`].
517    Container(WorkloadSpec),
518
519    /// Data-pipeline job with declared I/O and a readiness policy. The
520    /// orchestrator checks all `inputs` are reachable before each run and
521    /// verifies `outputs` afterward. Generalises the OpenRouter JSON-cache
522    /// refresher (`spawn_almanac_refresher`) to the full manifest form.
523    Almanac(AlmanacManifest),
524
525    /// Content-addressed static files uploaded to the mirror's `object_store`
526    /// provider slot. Wave-0 by default — gating mesofact and container waves.
527    /// Rollback is a pointer-flip via `mirror.toml [asset_aliases]`; bytes are
528    /// append-only and never re-pushed on rollback. See W160.
529    StaticAsset(StaticAssetWorkload),
530}
531
532impl Workload {
533    /// The `kind` discriminator this variant serializes as — the same string a
534    /// `workload.toml` writes and a `ServiceComponent.kind` names.
535    ///
536    /// Lives here rather than at a call site because this enum now has FIVE
537    /// places that enumerate its variants (itself plus the four tagging
538    /// mirrors below); a caller-local match would be a sixth, in another crate,
539    /// with nothing to force it to keep up.
540    pub fn kind_str(&self) -> &'static str {
541        match self {
542            Workload::MesofactStatic(_) => "mesofact-static",
543            Workload::Container(_) => "container",
544            Workload::Almanac(_) => "almanac",
545            Workload::StaticAsset(_) => "static-asset",
546        }
547    }
548}
549
550/// Internally-tagged mirror of [`Workload`] — the on-disk shape. Only ever
551/// reached on the human-readable branch, so its `deserialize_any` buffering is
552/// never asked of postcard.
553#[derive(Serialize, Deserialize)]
554#[serde(tag = "kind", rename_all = "kebab-case")]
555enum WorkloadTagged {
556    MesofactStatic(MesofactStaticWorkload),
557    Container(WorkloadSpec),
558    Almanac(AlmanacManifest),
559    StaticAsset(StaticAssetWorkload),
560}
561
562/// Borrowing twin of [`WorkloadTagged`] so `Serialize` need not clone the
563/// payload. Variant order must match [`Workload`].
564#[derive(Serialize)]
565#[serde(tag = "kind", rename_all = "kebab-case")]
566enum WorkloadTaggedRef<'a> {
567    MesofactStatic(&'a MesofactStaticWorkload),
568    Container(&'a WorkloadSpec),
569    Almanac(&'a AlmanacManifest),
570    StaticAsset(&'a StaticAssetWorkload),
571}
572
573/// Externally-tagged mirror — the postcard wire shape R590-B3 established.
574/// postcard encodes an external tag as the *variant index*, so the variant
575/// ORDER here is load-bearing: it must match [`Workload`] exactly or the
576/// kamaji UDS silently decodes into the wrong variant.
577#[derive(Serialize, Deserialize)]
578#[serde(rename_all = "kebab-case")]
579enum WorkloadExternal {
580    MesofactStatic(MesofactStaticWorkload),
581    Container(WorkloadSpec),
582    Almanac(AlmanacManifest),
583    StaticAsset(StaticAssetWorkload),
584}
585
586/// Borrowing twin of [`WorkloadExternal`]. Same order requirement.
587#[derive(Serialize)]
588#[serde(rename_all = "kebab-case")]
589enum WorkloadExternalRef<'a> {
590    MesofactStatic(&'a MesofactStaticWorkload),
591    Container(&'a WorkloadSpec),
592    Almanac(&'a AlmanacManifest),
593    StaticAsset(&'a StaticAssetWorkload),
594}
595
596impl Serialize for Workload {
597    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
598    where
599        S: serde::Serializer,
600    {
601        if s.is_human_readable() {
602            match self {
603                Workload::MesofactStatic(w) => WorkloadTaggedRef::MesofactStatic(w),
604                Workload::Container(w) => WorkloadTaggedRef::Container(w),
605                Workload::Almanac(w) => WorkloadTaggedRef::Almanac(w),
606                Workload::StaticAsset(w) => WorkloadTaggedRef::StaticAsset(w),
607            }
608            .serialize(s)
609        } else {
610            match self {
611                Workload::MesofactStatic(w) => WorkloadExternalRef::MesofactStatic(w),
612                Workload::Container(w) => WorkloadExternalRef::Container(w),
613                Workload::Almanac(w) => WorkloadExternalRef::Almanac(w),
614                Workload::StaticAsset(w) => WorkloadExternalRef::StaticAsset(w),
615            }
616            .serialize(s)
617        }
618    }
619}
620
621impl<'de> Deserialize<'de> for Workload {
622    fn deserialize<D>(de: D) -> Result<Self, D::Error>
623    where
624        D: serde::Deserializer<'de>,
625    {
626        if de.is_human_readable() {
627            Ok(match WorkloadTagged::deserialize(de)? {
628                WorkloadTagged::MesofactStatic(w) => Workload::MesofactStatic(w),
629                WorkloadTagged::Container(w) => Workload::Container(w),
630                WorkloadTagged::Almanac(w) => Workload::Almanac(w),
631                WorkloadTagged::StaticAsset(w) => Workload::StaticAsset(w),
632            })
633        } else {
634            Ok(match WorkloadExternal::deserialize(de)? {
635                WorkloadExternal::MesofactStatic(w) => Workload::MesofactStatic(w),
636                WorkloadExternal::Container(w) => Workload::Container(w),
637                WorkloadExternal::Almanac(w) => Workload::Almanac(w),
638                WorkloadExternal::StaticAsset(w) => Workload::StaticAsset(w),
639            })
640        }
641    }
642}
643
644/// `kind = "mesofact-static"` payload — static-site build colocated with the
645/// frontend it deploys.
646///
647/// The two-role model (R256-F7): a build/publish step plus an optional
648/// SSR/SPA runtime companion. The build step is always transient (runs once,
649/// publishes, exits). The companion is long-lived and only present when the
650/// app has dynamic/server-rendered pages; pure static sites leave it `None`.
651#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
652#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
653pub struct MesofactStaticWorkload {
654    /// Wire-format version. Always `V1` today.
655    pub schema_version: SchemaVersion,
656
657    /// Build command + output directory.
658    pub build: BuildConfig,
659
660    /// Path (relative to the manifest) of the routes module the
661    /// `mesofact-static` reconciler reads to enumerate routes.
662    pub routes: PathBuf,
663
664    /// Where the build command runs. Default: `HostSide` (mesofact-dev on the
665    /// host). Set to `InContainer` for cloud/HA where no host watcher is
666    /// present and CI-fidelity build environments are required.
667    #[serde(default)]
668    pub build_mode: BuildMode,
669
670    /// Optional SSR/SPA runtime companion container.
671    ///
672    /// `None` → pure static site; Caddy (or equivalent CDN) serves all
673    /// requests directly from the object store. This is the common case for
674    /// dev-yah today.
675    ///
676    /// `Some` → the workload spec describes a long-lived container that
677    /// handles dynamic/SSR requests. Caddy routes static asset paths to
678    /// the object store and all other paths to this container. The companion
679    /// uses `RestartPolicy::Always`; the orchestrator (camp or yubaba)
680    /// ensures it stays up alongside the Caddy edge.
681    #[ts(optional = nullable)]
682    pub ssr_runtime: Option<WorkloadSpec>,
683
684    /// Serve-time reference to a published W272 bundle (R599-F4).
685    ///
686    /// `Some` → the built app is deployed as a content-addressed bundle that
687    /// kamaji materializes from the bundle store (R599-F1) and serves via its
688    /// native backend, instead of (or in addition to) the build reconciler
689    /// pushing `dist/` to the object-store/CDN. `None` → legacy
690    /// build-and-publish-only workload — kamaji rejects that form as yubaba's
691    /// `mesofact-static` reconciler's responsibility.
692    ///
693    /// No `skip_serializing_if`: like `ssr_runtime`, this field is always
694    /// encoded so the postcard wire codec (non-self-describing, positional)
695    /// round-trips — `skip_serializing_if` would omit the byte on serialize
696    /// while decode still expects it. `#[serde(default)]` keeps every existing
697    /// `mesofact-static` TOML/JSON that predates this field parsing to `None`.
698    #[serde(default)]
699    #[ts(optional = nullable)]
700    pub serve_bundle: Option<MesofactServeBundle>,
701
702    /// Revalidate receiver for the almanac push model (R330-F12).
703    ///
704    /// `Some` → kamaji also forks `mesofact serve --revalidate <workload>`
705    /// alongside the bundle's static serve (or in place of it when
706    /// `serve_bundle` is `None`). The receiver is ephemeral-V8: each
707    /// `POST /revalidate` poke boots a V8 isolate, re-renders the route,
708    /// republishes to the CDN, then drops the isolate.
709    ///
710    /// Env vars are resolved at deploy time (R2 creds + mirror bearer) so
711    /// the node never sees keystore slot names.
712    #[serde(default)]
713    #[ts(optional = nullable)]
714    pub revalidate_receiver: Option<MesofactRevalidateReceiver>,
715}
716
717/// Revalidate receiver config (R330-F12) — tells kamaji to fork a second
718/// `mesofact serve --revalidate` process alongside the static bundle server.
719///
720/// The receiver is the almanac push endpoint: a lightweight resident axum
721/// server mounting `POST /revalidate` that boots V8 on each poke, re-renders
722/// the invalidated route, publishes to R2/CDN, then drops the isolate.
723#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
724#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
725pub struct MesofactRevalidateReceiver {
726    /// Routes the receiver accepts pokes for (allowlist).
727    /// Empty vec → all routes in the workload's manifest are revalidatable.
728    #[serde(default)]
729    pub routes: Vec<String>,
730
731    /// Path to `mesofact.config.toml` carrying the `[publish]` block
732    /// (bucket / zone / env-named credentials). Relative to the workload
733    /// directory. Default: `"mesofact.config.toml"`.
734    #[serde(default = "default_publish_config_path")]
735    pub publish_config: String,
736
737    /// Env var name holding the bearer secret for this tenant, resolved
738    /// at deploy time and set as `MESOFACT_MIRROR_KEY` on the receiver
739    /// process. `None` → open receiver (no bearer check).
740    #[ts(optional = nullable)]
741    pub mirror_key_env: Option<String>,
742
743    /// Environment variables set on the revalidate process by kamaji.
744    /// Keys are the canonical env var names (`MESOFACT_S3_ACCESS_KEY_ID`,
745    /// `MESOFACT_S3_SECRET_ACCESS_KEY`, `CLOUDFLARE_API_TOKEN`,
746    /// `MESOFACT_MIRROR_KEY`). Values are resolved from the keystore at
747    /// deploy time — the node never sees slot names.
748    #[serde(default)]
749    pub env: std::collections::BTreeMap<String, String>,
750
751    /// Feed-fetch tier (R330-F31) — the almanac feeds whose artifacts must be
752    /// refreshed **on the node** for a poke to have anything new to render.
753    ///
754    /// Empty → no fetcher; the receiver re-renders whatever data the bundle was
755    /// built with (correct for a site whose data only changes at build time,
756    /// silently stale for one whose data is a live feed). Non-empty → kamaji
757    /// forks a third resident process, `bins/<triple>/almanac-feed`, next to the
758    /// receiver.
759    #[serde(default)]
760    pub feeds: Vec<AlmanacFeed>,
761
762    /// Seconds between feed-fetch ticks. Ignored when `feeds` is empty.
763    ///
764    /// This is the site's freshness bound: a release lands, and the next tick
765    /// refreshes + pokes. `FeedRunner`'s change-suppression means an idle tick
766    /// costs one conditional fetch, so a short interval is affordable.
767    #[serde(default = "default_feed_interval_secs")]
768    pub feed_interval_secs: u64,
769
770    /// Workspace-relative path of the component whose build produced this
771    /// bundle, e.g. `app/yah/web/marketing` (R330-F31).
772    ///
773    /// Reconciles two roots for one file: a feed declares `emit.artifact`
774    /// workspace-relative (that is where it is authored), while the route
775    /// declares the same file project-relative (that is what the bundle
776    /// carries). The fetcher strips this prefix to get from one to the other.
777    /// `None` → the two already coincide.
778    #[serde(default)]
779    #[ts(optional = nullable)]
780    pub feed_project_prefix: Option<String>,
781}
782
783/// One almanac feed handed to the on-node fetcher (R330-F31).
784///
785/// The definition travels **by value**, not by path: the node has no copy of
786/// the camp's `.yah/almanac/` tree, and staging one into the content-addressed
787/// bundle would put a mutable-by-nature config inside an immutable artifact.
788/// The fetcher parses `config_toml` with the same `FeedConfig` type that reads
789/// the file at the source, so there is one schema and no drift.
790#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
791#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
792pub struct AlmanacFeed {
793    /// Feed name — the `.yah/almanac/<name>.toml` stem. Logs/diagnostics only;
794    /// `config_toml` is authoritative.
795    pub name: String,
796
797    /// Verbatim contents of the feed definition TOML.
798    pub config_toml: String,
799}
800
801fn default_publish_config_path() -> String {
802    "mesofact.config.toml".to_string()
803}
804
805/// Five minutes: fast enough that a release is live on yah.dev before anyone
806/// goes looking, slow enough to be invisible against a source API's rate limit.
807fn default_feed_interval_secs() -> u64 {
808    300
809}
810
811/// Serve-time reference to a published W272 bundle (R599-F4) — the
812/// `{bundle_digest, runtime, lifecycle}` triple a `mesofact-static` workload
813/// carries when kamaji, not the build reconciler, serves it.
814#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
815#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
816pub struct MesofactServeBundle {
817    /// BLAKE3 digest of the published bundle manifest — the content-address
818    /// kamaji materializes from the bundle store (`yah_mesofact_bundle`,
819    /// R599-F1). Same 64-hex shape the bundle crate's `BundleHash` validates.
820    pub digest: BlakeHash,
821
822    /// Runtime that serves the bundle: `"self"` (bundle ships its own
823    /// `bins/<triple>/serve`) or `"mesofact/<version>"` (resolve the stock
824    /// serve runtime asset from the node cache). Wire-mirrors
825    /// `yah_mesofact_bundle::BundleRuntime`; kept as a plain `String` here so
826    /// workload-spec stays free of the bundle crate and its non-TS/schema
827    /// newtypes.
828    pub runtime: String,
829
830    /// How kamaji supervises the served bundle. Default: keep-alive.
831    #[serde(default)]
832    pub lifecycle: BundleLifecycle,
833
834    /// Port the served bundle listens on (R599-F12). This is the bundle-tier
835    /// analogue of a container's `expose.mesh.ports`: the *declared* serving
836    /// port, which a proxy pairs with the workload's mesh IP to get a dialable
837    /// address.
838    ///
839    /// `None` → kamaji falls back to its node-wide default
840    /// (`KAMAJI_BUNDLE_PORT` / `DEFAULT_BUNDLE_PORT` = 8080), which is the
841    /// pre-R599-F12 behaviour and is only correct while a node hosts one
842    /// bundle. Declaring a port per workload is what lets one node host
843    /// several — that singleton default is the thing it removes.
844    ///
845    /// No `skip_serializing_if` — see `serve_bundle`'s note: the postcard wire
846    /// codec is positional, so an omitted byte shifts every later field.
847    #[serde(default)]
848    #[ts(optional = nullable)]
849    pub port: Option<u16>,
850}
851
852/// Lifecycle mode for a served bundle (W272 §3).
853#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
854#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
855#[serde(rename_all = "snake_case")]
856pub enum BundleLifecycle {
857    /// Fork at deploy, keep resident, restart per policy — today's server
858    /// archetype. Memory is resident for the workload's lifetime.
859    KeepAlive,
860
861    /// Kamaji owns the listen socket, forks the runtime on the first connection
862    /// (fd-passing), and reaps it after `idle_ttl` with zero connections — the
863    /// "serverless" tier (zero memory when idle). The JIT fork/reap mechanics
864    /// land in R599-F6; this variant only declares the intent + budget.
865    OnDemand {
866        /// Idle time with no live connections before kamaji reaps the process.
867        idle_ttl: Millis,
868    },
869}
870
871impl Default for BundleLifecycle {
872    /// Keep-alive — the resident server archetype — matches the current
873    /// deploy-and-supervise default.
874    fn default() -> Self {
875        BundleLifecycle::KeepAlive
876    }
877}
878
879/// Build step that produces the static artifact published by a
880/// `mesofact-static` workload.
881#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
882#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
883pub struct BuildConfig {
884    /// Shell command run from the manifest's directory, e.g. `"bun run build"`.
885    pub command: String,
886
887    /// Output directory (relative to the manifest) the reconciler uploads.
888    pub out_dir: PathBuf,
889
890    /// Data-only re-render command (W225 §3 "revalidate"), run from the
891    /// manifest's directory against the **already-built** `out_dir` — no
892    /// bundler. `{route}` is substituted with the invalidated route pattern,
893    /// e.g. `"cargo run -p mesofact-build -- render . --route {route} --all"`.
894    /// Absent → a revalidate dispatch republishes `out_dir` as-is.
895    #[serde(default)]
896    pub render_command: Option<String>,
897}
898
899// ── BuildMode ─────────────────────────────────────────────────────────────────
900
901/// Where the build command runs for a `mesofact-static` workload.
902///
903/// The two-role split encodes the F7 design decision: build/publish is a
904/// **transient job** (runs once, exits, GC'd); SSR/SPA serving is a separate
905/// **long-lived companion container** (optional, only for dynamic pages). A
906/// single merged "mesofact container" is the trap — in cloud, CI builds the
907/// artifact, R2+CDN serve it, and a distinct worker handles any SSR.
908///
909/// Default: `HostSide` — mesofact-dev runs the build on the host and publishes
910/// to the tier's object store. No container overhead; compatible with dev and
911/// sim tiers.
912#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, TS)]
913#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
914#[serde(rename_all = "snake_case")]
915pub enum BuildMode {
916    /// Build command runs on the host (mesofact-dev watcher). The watcher
917    /// publishes the output to the tier's object store (DistPointer for dev,
918    /// MinIO for sim). Compatible with all tiers; zero container overhead.
919    #[default]
920    HostSide,
921
922    /// Build runs inside a transient container matching the CI image. Higher
923    /// fidelity (environment matches CI exactly); costs image pull +
924    /// container cold-start. Required for cloud/HA where no mesofact-dev
925    /// watcher is running on the host.
926    InContainer {
927        /// Container image that runs the build (e.g. `"ghcr.io/org/app-build:v1.2"`).
928        /// Must have the build toolchain installed. The container is started with
929        /// the workspace root bind-mounted, runs `build.command`, uploads
930        /// `build.out_dir` to the object store, then exits.
931        image: ImageRef,
932    },
933}
934
935// ── AlmanacManifest ───────────────────────────────────────────────────────────
936
937/// An observable endpoint the almanac scheduler probes to check readiness.
938///
939/// Used for both inputs (checked before the run) and outputs (verified after
940/// a successful run to confirm the job produced something reachable).
941/// The probe is intentionally lightweight — no S3 SigV4, no xlb-net discovery
942/// required; a simple TCP connect or HTTP GET is enough for the dev/sim tier.
943#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
944#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
945#[serde(rename_all = "snake_case")]
946pub enum AlmanacTarget {
947    /// Issue an HTTP GET to `url`; ready when the server responds with
948    /// `expect_status` (default: any 2xx).
949    Http {
950        url: String,
951        #[ts(optional = nullable)]
952        expect_status: Option<u16>,
953    },
954
955    /// Establish a TCP connection to `host:port`; ready when the connect
956    /// succeeds. Used for non-HTTP services (e.g. MinIO API on port 9000)
957    /// and as a lighter probe when an HTTP endpoint isn't stable yet.
958    Tcp { host: String, port: u16 },
959}
960
961/// What the almanac scheduler does when a precondition check fails.
962///
963/// The F9 design decision: `WaitWithTimeout` is the default. Fail-fast is
964/// too brittle for the sim tier (containers may still be cold-starting);
965/// requeue-with-no-ceiling can block the scheduler indefinitely. The
966/// recommended timeout for sim is the container spinup budget (~5 s cold,
967/// ~1 s warm): set `timeout` to a few seconds, then let the retry cadence
968/// handle transient glitches.
969#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
970#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
971#[serde(rename_all = "snake_case")]
972pub enum NotReadyPolicy {
973    /// Wait up to `timeout` for all preconditions to pass before aborting
974    /// the run. The run is skipped (not rescheduled); the next cadence tick
975    /// will retry. Suitable when targets occasionally lag at startup.
976    WaitWithTimeout {
977        /// How long to wait for each precondition to become reachable. The
978        /// scheduler polls with a short sleep between attempts.
979        timeout: Millis,
980    },
981
982    /// Abort immediately if any precondition check fails. Suitable for
983    /// integration-test harnesses where a missing dependency is always a
984    /// hard error.
985    FailFast,
986
987    /// Requeue with exponential backoff up to `max_attempts` times. After
988    /// exhaustion the run is marked failed. Suitable for cloud/HA where
989    /// transient dependency outages are expected.
990    Requeue {
991        /// Maximum number of requeue attempts before the run is marked failed.
992        max_attempts: u32,
993        /// Initial backoff between attempts, in milliseconds.
994        backoff: Millis,
995    },
996}
997
998impl Default for NotReadyPolicy {
999    /// Default is `WaitWithTimeout { timeout: 5 seconds }` — matches the
1000    /// container spinup budget for the sim tier (few-second cold, sub-second warm).
1001    fn default() -> Self {
1002        Self::WaitWithTimeout { timeout: Millis::from_secs(5) }
1003    }
1004}
1005
1006/// When the almanac scheduler triggers a run.
1007#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1008#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1009#[serde(rename_all = "snake_case")]
1010pub enum Cadence {
1011    /// Run once at first opportunity, then never again.
1012    Once,
1013
1014    /// Run repeatedly with a fixed interval between the end of one run and
1015    /// the start of the next. Equivalent to `sleep N && run` in a loop.
1016    Every {
1017        /// Minimum time between consecutive run completions.
1018        interval: Millis,
1019    },
1020
1021    /// Run on a UTC cron schedule (standard 5-field expression, e.g.
1022    /// `"0 */6 * * *"` for every 6 hours). The scheduler evaluates the
1023    /// expression relative to UTC midnight.
1024    Cron { expression: String },
1025}
1026
1027/// `kind = "almanac"` manifest — a declared data-pipeline job.
1028///
1029/// An almanac job is the generalisation of the OpenRouter refresher
1030/// (`spawn_almanac_refresher`): it declares its I/O contract explicitly so
1031/// the orchestrator can enforce preconditions before each run and verify
1032/// outputs afterward. The degenerate case (no inputs, no app target, cron
1033/// schedule) is exactly the OpenRouter JSON-cache refresher.
1034///
1035/// Lifecycle:
1036/// 1. Cadence tick fires.
1037/// 2. Scheduler probes every `inputs` target. If any fail → apply
1038///    `not_ready_policy`.
1039/// 3. Command runs (`sh -c command` from the workload directory).
1040/// 4. Scheduler probes every `outputs` target. Failure → mark run as
1041///    failed but do not retry.
1042/// 5. Any workloads listed in `invalidates` receive a cache-bust signal
1043///    (implementation detail of the orchestrator; in camp this is a
1044///    rebuild trigger on the mesofact-dev watcher).
1045#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1046#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1047pub struct AlmanacManifest {
1048    /// Wire-format version. Always `V1` today.
1049    pub schema_version: SchemaVersion,
1050
1051    /// Shell command executed via `sh -c` from the workload directory.
1052    pub command: String,
1053
1054    /// When to run.
1055    pub cadence: Cadence,
1056
1057    /// Input targets that must be reachable before the command runs.
1058    /// Empty list → no precondition checks (degenerate case).
1059    #[serde(default)]
1060    pub inputs: Vec<AlmanacTarget>,
1061
1062    /// Output targets verified after a successful run.
1063    /// Empty list → no post-run verification.
1064    #[serde(default)]
1065    pub outputs: Vec<AlmanacTarget>,
1066
1067    /// What to do when a precondition check fails.
1068    /// Default: `WaitWithTimeout { timeout: 5000ms }`.
1069    #[serde(default)]
1070    pub not_ready_policy: NotReadyPolicy,
1071
1072    /// Mesh identities of workloads to notify after a successful run.
1073    /// The orchestrator sends a cache-bust signal to each entry so
1074    /// downstream consumers can reload their data (e.g. mesofact-dev
1075    /// triggers a rebuild when the OpenRouter cache refreshes).
1076    /// Empty list → no downstream invalidation.
1077    #[serde(default)]
1078    pub invalidates: Vec<MeshIdent>,
1079}
1080
1081// ── StaticAssetWorkload ───────────────────────────────────────────────────────
1082
1083/// BLAKE3 content hash expressed as exactly 64 ASCII hex digits.
1084///
1085/// This is the content-address key for every file in the static-asset catalog.
1086/// Deserialization rejects values that do not conform — 64 hex chars, case
1087/// insensitive. Mismatch between the recorded hash and the source file halts
1088/// the upload step in the reconciler.
1089#[derive(Debug, Clone, PartialEq, Eq, Serialize, TS)]
1090#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1091#[ts(type = "string")]
1092pub struct BlakeHash(pub String);
1093
1094impl<'de> Deserialize<'de> for BlakeHash {
1095    fn deserialize<D>(de: D) -> Result<Self, D::Error>
1096    where
1097        D: serde::Deserializer<'de>,
1098    {
1099        let s = String::deserialize(de)?;
1100        if s.len() != 64 || !s.bytes().all(|b| b.is_ascii_hexdigit()) {
1101            return Err(serde::de::Error::custom(format!(
1102                "blake3 hash must be exactly 64 hex digits, got {:?}",
1103                s
1104            )));
1105        }
1106        Ok(BlakeHash(s))
1107    }
1108}
1109
1110// ── License & FetchSource (W164) ──────────────────────────────────────────────
1111
1112/// Closed-set, parse-time-enforced license tag. Mirrors the workspace
1113/// permissive-license rule (MIT / Apache-2.0 / BSD-2/3-Clause / ISC). Adding a
1114/// variant is an explicit schema change — non-permissive strings
1115/// (`"GPL-3.0"`, `"AGPL"`, etc.) fail at serde-deserialize before any shape
1116/// validator runs.
1117///
1118/// Shared between `asset.derive.fetch.license` (W164, required) and a future
1119/// `almanac::ReleaseSource.license` migration (R438-F10, optional).
1120#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
1121#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1122#[serde(rename_all = "kebab-case")]
1123pub enum License {
1124    Mit,
1125    Apache2,
1126    Bsd2Clause,
1127    Bsd3Clause,
1128    Isc,
1129}
1130
1131/// Shared fetch primitive — usable by `asset.derive` today, and by Almanac's
1132/// `ReleaseSource` after a follow-up migration (R438-F10). Defined once in
1133/// workload-spec so both consumers reject the same set of non-permissive
1134/// licenses.
1135///
1136/// The `blake3` hash pins the upstream bytes; mismatch at fetch time is a hard
1137/// error in the reconciler. The `license` field is **required** here — every
1138/// derived asset must declare its upstream license. If/when Almanac adopts
1139/// `FetchSource`, the Almanac side may wrap this in a struct with
1140/// `Option<License>` since release manifests have no distribution license per
1141/// se.
1142#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1143#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1144pub struct FetchSource {
1145    /// Upstream URL fetched verbatim. Reconciler retry policy is configured
1146    /// elsewhere (R438-F11); the URL itself is opaque to workload-spec.
1147    pub url: String,
1148
1149    /// Expected BLAKE3 hash of the fetched bytes (64 hex characters). The
1150    /// reconciler verifies this after download and aborts on mismatch.
1151    pub blake3: BlakeHash,
1152
1153    /// Upstream license. Closed-set, parse-time enforced.
1154    pub license: License,
1155}
1156
1157/// Optional transform applied after a [`FetchSource`] download, lowering to a
1158/// `ForgeCommand::Subprocess` via the recipe loader (R438-T4). The transform's
1159/// output is content-addressed by the entry's `blake3` (the recipe runs only
1160/// when the cache misses).
1161#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1162#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1163pub struct TransformSpec {
1164    /// Named recipe under `.yah/qed/transforms/<recipe>.toml`. Loader rejects
1165    /// missing recipes at materialize time.
1166    pub recipe: String,
1167
1168    /// `{{key}}` substitutions passed to the recipe argv at element
1169    /// granularity (no shell, no string concat). Empty when the recipe is
1170    /// fully parameterless.
1171    #[serde(default)]
1172    pub params: BTreeMap<String, String>,
1173}
1174
1175/// W212/R518: the committed derivation lock — the in-tree action-cache
1176/// receipt. `input_hash` is the input-addressed derivation key computed over
1177/// the complete declared input set (fetched-input pin ⊕ recipe-file bytes ⊕
1178/// invocation params ⊕ schema version); `output_blake3` is what those inputs
1179/// produced (== the entry's `blake3`). The reconciler skips the entire build
1180/// (no fetch, no transform, no PUT) when the lock matches the inputs recomputed
1181/// from the current pins and the bucket already holds the output — the
1182/// Nix-substituter / Bazel-remote-cache behaviour. Written by the R510 bind
1183/// path from the reconciler's `discovered_input_hash:<filename>` output; the
1184/// `git diff` on this block is the receipt that the derivation rolled.
1185#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1186#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1187pub struct DeriveLock {
1188    /// Input-addressed derivation key (BLAKE3 hex). A change to any declared
1189    /// input flips this, so a stale lock never produces a false skip.
1190    pub input_hash: String,
1191    /// Output the locked inputs produced (BLAKE3 hex; equals the entry's
1192    /// `blake3`). Carried so the lock is a self-contained action-cache entry.
1193    pub output_blake3: String,
1194}
1195
1196/// Provenance chain for a derived asset: required `fetch` step, optional
1197/// `transform` step. Materialized bytes replace `AssetEntry.source` for the
1198/// rest of the static-asset reconcile loop.
1199#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1200#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1201pub struct AssetDerive {
1202    /// Upstream fetch — URL + content-pin + license.
1203    pub fetch: FetchSource,
1204
1205    /// Post-fetch transform. `None` → the fetched bytes ARE the asset
1206    /// (entry `blake3` must match fetch `blake3`).
1207    #[serde(default)]
1208    #[ts(optional = nullable)]
1209    pub transform: Option<TransformSpec>,
1210
1211    /// W212/R518: committed derivation lock (input-addressed action-cache
1212    /// receipt). Absent until the first successful build writes it via the
1213    /// bind path. When present and current, enables the substituter-style
1214    /// build skip.
1215    #[serde(default)]
1216    #[ts(optional = nullable)]
1217    pub lock: Option<DeriveLock>,
1218}
1219
1220/// A single file entry in the static-asset catalog.
1221///
1222/// One `[[asset]]` row per bucket object. Multiple rows for different variants
1223/// (e.g. q5 and q4 whisper models) are fine — each declares its own filename
1224/// and hash. The reconciler treats the catalog as exhaustive and append-only:
1225/// new rows trigger a PUT; removed rows surface as drift (never a DELETE).
1226///
1227/// **Source-vs-derive XOR.** Exactly one of `source` or `derive` must be set.
1228/// Legacy local-bytes assets keep `source = "..."`; W164 derived assets set
1229/// `[asset.derive]` instead. [`validate::shape_static_asset`] enforces the
1230/// XOR; both-set and neither-set are hard `ShapeError::Field`.
1231#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1232#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1233pub struct AssetEntry {
1234    /// Destination path within the bucket, e.g.
1235    /// `"whisper/distil-large-v3-q5_1.bin"`. Must be unique in the catalog.
1236    /// Used as the S3 object key by the reconciler.
1237    pub filename: String,
1238
1239    /// Path to a local source file, relative to the `workload.toml` directory.
1240    /// Mutually exclusive with `derive`.
1241    #[serde(default)]
1242    #[ts(optional = nullable)]
1243    pub source: Option<PathBuf>,
1244
1245    /// Declared fetch (+ optional transform) provenance chain. The reconciler
1246    /// materializes the bytes into a content-addressed cache; the cache path
1247    /// then replaces `source` for the rest of the upload pipeline. Mutually
1248    /// exclusive with `source`.
1249    #[serde(default)]
1250    #[ts(optional = nullable)]
1251    pub derive: Option<AssetDerive>,
1252
1253    /// Expected BLAKE3 hash of the *final* asset bytes (64 hex characters).
1254    /// For `source` mode, this is hashed before upload. For `derive` mode,
1255    /// it's the post-transform (or post-fetch when no transform) output.
1256    /// Mismatch aborts the upload.
1257    pub blake3: BlakeHash,
1258}
1259
1260/// `kind = "static-asset"` payload — content-addressed bucket catalog.
1261///
1262/// The reconciler makes the bucket match the `[[asset]]` list exactly
1263/// (append-only: new rows → PUT; removed rows → drift report, not DELETE).
1264/// Rollback is pointer-flip via `mirror.toml [asset_aliases]` — bytes never
1265/// move during rollback.
1266///
1267/// **Closed-catalog invariant**: every value in `[aliases]` must be a
1268/// `filename` that exists in `[[asset]]`. Enforced by
1269/// [`validate::shape_static_asset`]. Mirror overrides (`[asset_aliases]` in
1270/// `mirror.toml`) are bound by the same rule — the alias graph can only
1271/// resolve to filenames already in the catalog.
1272#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1273#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1274pub struct StaticAssetWorkload {
1275    /// Wire-format version. Always `V1` today.
1276    pub schema_version: SchemaVersion,
1277
1278    /// Exhaustive catalog of files this component manages in the bucket.
1279    ///
1280    /// Named `asset` on disk (TOML `[[asset]]` array-of-tables) to follow TOML
1281    /// convention; accessed as `.assets` in Rust code.
1282    #[serde(rename = "asset", default)]
1283    pub assets: Vec<AssetEntry>,
1284
1285    /// Canonical logical-name → filename mappings for this component.
1286    ///
1287    /// Values must be filenames present in `assets` — validated by
1288    /// [`validate::shape_static_asset`]. Mirror files may override individual
1289    /// entries via `[asset_aliases]` but may never reference filenames absent
1290    /// from this catalog.
1291    #[serde(default)]
1292    pub aliases: BTreeMap<String, String>,
1293}
1294
1295// ── Lifecycle archetype (R572-F1 / W244) ───────────────────────────────────────
1296
1297/// Explicit lifecycle archetype for a `kind = "container"` workload (W244).
1298///
1299/// The question that actually matters to a scheduler: *"can I kill this and
1300/// recreate it somewhere else?"* Before this field existed, the answer was
1301/// inferred per-spec from `volumes.is_empty()` + `restart_policy` — fragile
1302/// absence-as-policy, the same trap W243 calls out on the node-taint side.
1303/// This type makes the answer structural instead of guessed.
1304///
1305/// This ticket (R572-F1) adds the discriminator only. The reconciler does not
1306/// yet branch on it (R572-F4) and neither does the scheduler (R572-F5).
1307#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
1308#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1309#[serde(rename_all = "kebab-case")]
1310pub enum LifecycleArchetype {
1311    /// k8s analogue: Deployment. Stateless and fungible — the scheduler may
1312    /// move it, scale it to N replicas, or restart it on a different node
1313    /// with zero consequence. Drainable.
1314    Server,
1315
1316    /// k8s analogue: StatefulSet. Stable identity + a volume that must
1317    /// follow it; at most one live instance. Not drainable — the reconciler
1318    /// must not schedule it onto a different node. Example: a postgres peer,
1319    /// headscale (W267/R591).
1320    Appliance,
1321
1322    /// k8s analogue: Job. Runs to completion with declared inputs/outputs,
1323    /// then is gone — no steady-state identity. `almanac` is the first
1324    /// job-family member; forge runs (`WorkloadSpec::for_forge`, used by QED)
1325    /// are the `container`-kind instance of this archetype.
1326    Job,
1327}
1328
1329impl LifecycleArchetype {
1330    /// The repel-taint key for this archetype (R572-F5). A node carrying the
1331    /// taint `"no-<key>"` repels workloads of this class unless they
1332    /// explicitly tolerate it.
1333    ///
1334    /// Examples: `Server` → `"server"` (repelled by `"no-server"`);
1335    /// `Appliance` → `"appliance"` (repelled by `"no-appliance"`).
1336    pub fn taint_key(&self) -> &'static str {
1337        match self {
1338            Self::Server => "server",
1339            Self::Appliance => "appliance",
1340            Self::Job => "job",
1341        }
1342    }
1343
1344    /// The pre-R572 inference this field replaces, kept only to give
1345    /// `WorkloadSpec::effective_archetype` a behavior-preserving fallback for
1346    /// specs written before this field existed (`archetype: None`).
1347    ///
1348    /// A volume that must follow the workload is the strongest signal of
1349    /// durable state → [`Self::Appliance`]. Absent that, `RestartPolicy::Never`
1350    /// is the existing forge/run-once convention (see
1351    /// [`RestartPolicy::Never`]'s doc comment) → [`Self::Job`]. Everything
1352    /// else defaults to the common case, [`Self::Server`].
1353    fn infer(volumes: &[VolumeMount], restart_policy: &RestartPolicy) -> Self {
1354        if !volumes.is_empty() {
1355            LifecycleArchetype::Appliance
1356        } else if matches!(restart_policy, RestartPolicy::Never) {
1357            LifecycleArchetype::Job
1358        } else {
1359            LifecycleArchetype::Server
1360        }
1361    }
1362}
1363
1364// ── WorkloadSpec ──────────────────────────────────────────────────────────────
1365
1366/// Complete typed description of a containerd workload handed to yubaba over
1367/// RPC. This is also the payload of the `kind = "container"` variant of
1368/// [`Workload`] on disk.
1369///
1370/// Yubaba never accepts compose YAML on its RPC surface — agents, the desktop,
1371/// and operator CLIs all hand yubaba `WorkloadSpec` values. See the arch doc
1372/// for the validation layers and evolution rules.
1373#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1374#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1375pub struct WorkloadSpec {
1376    /// Wire-format version; always `V1` today. Present at the top level so
1377    /// rolling clusters can detect and migrate across schema generations.
1378    pub schema_version: SchemaVersion,
1379
1380    /// DNS-friendly workload name, e.g. `"noisetable-api"`. Regex:
1381    /// `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, length ≤ 63.
1382    pub name: String,
1383
1384    /// Container image to pull.
1385    pub image: ImageRef,
1386
1387    /// Tier tag controlling admission control and mesh filtering.
1388    pub tier: TierTag,
1389
1390    /// Tenant **isolation** axis (W206). Separates operators' workloads at the
1391    /// network / DB / mesh-identity level. Defaults to [`TenantId::singleton`]
1392    /// for specs that predate the axis, so single-tenant clusters keep every
1393    /// isolation primitive a no-op. Orthogonal to [`Self::tier`] (class) and
1394    /// [`Self::namespace`] (routing).
1395    #[serde(default = "TenantId::singleton")]
1396    pub tenant: TenantId,
1397
1398    /// Namespace **routing/naming** axis (W206). A pure naming key — never
1399    /// affects isolation; disambiguates DNS names and selects config root /
1400    /// provider zone within a tenant. Defaults to [`NamespaceId::singleton`].
1401    #[serde(default = "NamespaceId::singleton")]
1402    pub namespace: NamespaceId,
1403
1404    /// Target replica count. `0` registers the workload without deploying it.
1405    /// Range: 0–100 (cluster-wide cap; operator can raise it).
1406    pub replicas: u32,
1407
1408    /// Override the image's `CMD`. `None` leaves the image default.
1409    #[ts(optional = nullable)]
1410    pub command: Option<Vec<String>>,
1411
1412    /// Override the image's `ENTRYPOINT`. `None` leaves the image default.
1413    #[ts(optional = nullable)]
1414    pub entrypoint: Option<Vec<String>>,
1415
1416    /// Working directory inside the container.
1417    #[ts(optional = nullable)]
1418    pub workdir: Option<PathBuf>,
1419
1420    /// User to run as, e.g. `"1000:1000"` or `"appuser"`.
1421    #[ts(optional = nullable)]
1422    pub user: Option<String>,
1423
1424    /// Environment variables. Values may be literals, secret refs, or
1425    /// mesh-address references resolved by yubaba at deploy time.
1426    #[serde(default)]
1427    pub env: Vec<EnvVar>,
1428
1429    /// Secret mounts. Values never appear in the spec JSON — only references.
1430    #[serde(default)]
1431    pub secrets: Vec<SecretMount>,
1432
1433    /// Volume mounts.
1434    #[serde(default)]
1435    pub volumes: Vec<VolumeMount>,
1436
1437    /// Hard resource caps enforced by containerd/cgroups.
1438    pub resources: ResourceLimits,
1439
1440    /// Mesh idents that must reach `Ready` before this workload starts.
1441    #[serde(default)]
1442    pub depends_on: Vec<MeshIdent>,
1443
1444    /// Container liveness/readiness probe.
1445    #[ts(optional = nullable)]
1446    pub healthcheck: Option<Healthcheck>,
1447
1448    /// What yubaba does when the container exits.
1449    pub restart_policy: RestartPolicy,
1450
1451    /// Explicit lifecycle archetype (R572-F1 / W244): `server`, `appliance`,
1452    /// or `job`. `None` means the spec predates this field (or the author
1453    /// didn't set it) — callers MUST NOT read this directly to decide
1454    /// drainability; use [`WorkloadSpec::effective_archetype`], which falls
1455    /// back to the pre-R572 `volumes`/`restart_policy` inference so no
1456    /// existing spec's effective meaning changes.
1457    ///
1458    /// Additive: this field did not exist before R572-F1. Reconciler (F4)
1459    /// and scheduler (F5) branching on the resolved archetype are separate,
1460    /// later tickets — this field alone changes no runtime behavior.
1461    #[serde(default)]
1462    #[ts(optional = nullable)]
1463    pub archetype: Option<LifecycleArchetype>,
1464
1465    /// Graceful shutdown configuration.
1466    pub stop_policy: StopPolicy,
1467
1468    /// Network exposure configuration — mesh, public, and operator channels
1469    /// are independent and can be set in any combination.
1470    pub expose: ExposeSpec,
1471
1472    /// OCI-style labels, passed through to the container. Opaque to yubaba.
1473    #[serde(default)]
1474    pub labels: HashMap<String, String>,
1475
1476    /// Yah-specific metadata, conventionally prefixed `yah.*`. Opaque to
1477    /// yubaba beyond `yah.forge=true` which suppresses the Never-restart guard.
1478    #[serde(default)]
1479    pub annotations: HashMap<String, String>,
1480}
1481
1482impl WorkloadSpec {
1483    /// Build a `WorkloadSpec` for a forge run.
1484    ///
1485    /// Sets the conventional forge fields in one place so callers cannot
1486    /// forget any of them:
1487    ///
1488    /// - `restart_policy = Never`
1489    /// - `archetype = Some(LifecycleArchetype::Job)` — a forge run is
1490    ///   exactly the `container`-kind instance of the job archetype (W244);
1491    ///   set explicitly rather than left to infer since this constructor
1492    ///   knows its own shape
1493    /// - `expose.public = None`, `expose.operator = None`
1494    /// - `expose.mesh.identity = "forge.<forge_id>"`
1495    /// - `annotations["yah.forge"] = "true"` (suppresses the shape warning)
1496    /// - `tier` and `image` come from the caller; `ports` becomes the mesh
1497    ///   port list (empty is valid — forge jobs often don't expose ports)
1498    ///
1499    /// All other fields are set to safe defaults. Callers can mutate the
1500    /// returned value to fill in `command`, `env`, `resources`, etc.
1501    pub fn for_forge(
1502        forge_id: &str,
1503        image: ImageRef,
1504        tier: TierTag,
1505        ports: Vec<u16>,
1506    ) -> Self {
1507        let mut annotations = HashMap::new();
1508        annotations.insert("yah.forge".into(), "true".into());
1509
1510        WorkloadSpec {
1511            schema_version: SchemaVersion::V1,
1512            // NB: DNS-label safe (no dots) — `check_name` validation rejects
1513            // dots here. The container_id derives from this; the state-poll
1514            // keys off `expose.mesh.identity` (`forge.<id>`) instead, so those
1515            // two must be reconciled at the read path, NOT by dotting the name
1516            // (see R590-B9).
1517            name: format!("forge-{forge_id}"),
1518            image,
1519            tier,
1520            tenant: TenantId::singleton(),
1521            namespace: NamespaceId::singleton(),
1522            replicas: 1,
1523            command: None,
1524            entrypoint: None,
1525            workdir: None,
1526            user: None,
1527            env: vec![],
1528            secrets: vec![],
1529            volumes: vec![],
1530            resources: ResourceLimits {
1531                // R590-B10: forge workloads are BUILDS (cargo, buildkit, a
1532                // from-source V8 checkout+compile), not tiny services. The old
1533                // 256 MB placeholder became a hard cgroup memory.limit in
1534                // build_oci_spec and SIGKILL'd the rusty-v8 build mid-checkout
1535                // (git checkout of third_party/icu died of signal 9) — the
1536                // more so because /tmp is a RAM-backed tmpfs, so the source
1537                // tree counts against this limit too. 32 GiB is a bounded
1538                // ceiling that fits the V8 build's >12 GB peak with headroom,
1539                // protects the host from a runaway (vs truly unlimited), and is
1540                // above physical RAM on smaller build-workers (⇒ effectively
1541                // unlimited there). A per-step memory request threaded from the
1542                // pipeline is the eventual right model (see R590-B10).
1543                memory_mb: 32768,
1544                cpu_millis: 512,
1545                ephemeral_storage_mb: 512,
1546            },
1547            depends_on: vec![],
1548            healthcheck: None,
1549            restart_policy: RestartPolicy::Never,
1550            archetype: Some(LifecycleArchetype::Job),
1551            stop_policy: StopPolicy {
1552                signal: 15,
1553                grace_period: Millis::from_secs(30),
1554            },
1555            expose: ExposeSpec {
1556                mesh: MeshExpose {
1557                    identity: MeshIdent(format!("forge.{forge_id}")),
1558                    ports,
1559                    allow_from: vec![],
1560                },
1561                public: None,
1562                operator: None,
1563            },
1564            labels: HashMap::new(),
1565            annotations,
1566        }
1567    }
1568
1569    /// Whether this workload requests the **host network namespace** rather
1570    /// than an isolated one.
1571    ///
1572    /// Opt-in via `annotations["yah.network"] == "host"` (see
1573    /// [`HOST_NETWORK_ANNOTATION`] / [`HOST_NETWORK_VALUE`]). Default is the
1574    /// isolated netns every other workload gets — host networking is a
1575    /// privileged escape hatch for the few infra workloads that must bind a
1576    /// host port so an on-host ingress (e.g. a Cloudflare tunnel reaching
1577    /// `127.0.0.1:<port>`) can route to them without CNI/bridge plumbing.
1578    ///
1579    /// The backend (kamaji) is responsible for **guarding** this: host
1580    /// networking is only honoured for `tier == "infra"` workloads; a
1581    /// non-infra workload that sets the annotation is rejected at deploy. See
1582    /// `validate_spec_for_constable`.
1583    pub fn wants_host_network(&self) -> bool {
1584        self.annotations
1585            .get(HOST_NETWORK_ANNOTATION)
1586            .map(|v| v == HOST_NETWORK_VALUE)
1587            .unwrap_or(false)
1588    }
1589
1590    /// Resolve the lifecycle archetype (R572-F1 / W244): the explicit
1591    /// [`Self::archetype`] if set, otherwise the pre-R572 inference from
1592    /// `volumes`/`restart_policy` this field replaces.
1593    ///
1594    /// This is the one seam callers should use to ask "can I kill and
1595    /// reschedule this?" — it is intentionally the *only* place that
1596    /// implements the fallback, so behavior for pre-existing specs (no
1597    /// `archetype` on disk) is identical to what it was before this field
1598    /// existed. Consumers (reconciler R572-F4, scheduler R572-F5) branch on
1599    /// the return value; this crate does not itself change any reconciler or
1600    /// scheduler behavior.
1601    pub fn effective_archetype(&self) -> LifecycleArchetype {
1602        self.archetype
1603            .unwrap_or_else(|| LifecycleArchetype::infer(&self.volumes, &self.restart_policy))
1604    }
1605
1606    /// Fully-qualified mesh identity `<tenant>/<namespace>/<name>` (W206 /
1607    /// R558-F3), where `<name>` is this workload's [`MeshExpose::identity`].
1608    ///
1609    /// Within a tenant, workloads still address each other by the short
1610    /// identity (namespace disambiguates only on collision); the FQN is what
1611    /// makes the identity unambiguous across tenants and is exactly what a
1612    /// [`MeshPeer::CrossTenant`] grant names.
1613    pub fn fq_mesh_identity(&self) -> String {
1614        format!(
1615            "{}/{}/{}",
1616            self.tenant.0, self.namespace.0, self.expose.mesh.identity.0
1617        )
1618    }
1619
1620    /// The taint this workload requires its node to carry, if any (R594-F2 /
1621    /// W267 sovereign public ingress).
1622    ///
1623    /// Opt-in via `annotations["yah.placement.requires-taint"] = "<taint
1624    /// name>"` (see [`REQUIRES_TAINT_ANNOTATION`]) — same annotation-based,
1625    /// zero-blast-radius shape as [`Self::wants_host_network`], chosen so
1626    /// declaring this requirement does not force a struct-literal edit at
1627    /// every existing `WorkloadSpec { .. }` construction site the way a new
1628    /// plain field would (see R572-F1's handoff: ~26 sites for one field).
1629    ///
1630    /// **This only declares the requirement — nothing matches it yet.** The
1631    /// taint itself doesn't exist on the machine-TOML side until
1632    /// [R572-F3](yah://arch/symbol/R572) adds a `taints` list there, and
1633    /// nothing enforces repel-unless-tolerate placement until
1634    /// [R572-F5](yah://arch/symbol/R572)'s scheduler lands. Until then this
1635    /// is inert metadata a future scheduler can read.
1636    ///
1637    /// The public-ingress appliance (W267) is the first user: a
1638    /// `kind = "container"` workload with `archetype =
1639    /// Some(LifecycleArchetype::Appliance)` and
1640    /// `requires_taint() == Some(PUBLIC_IP_TAINT)`, so yubaba may one day
1641    /// place it only on machines carrying the `"public-ip"` taint and kamaji
1642    /// supervises it like any other container (no new `Workload` variant —
1643    /// see [`Workload::Container`]'s doc comment).
1644    pub fn requires_taint(&self) -> Option<&str> {
1645        self.annotations
1646            .get(REQUIRES_TAINT_ANNOTATION)
1647            .map(String::as_str)
1648    }
1649
1650    /// Whether this workload must be run by kamaji's **native** (fork+exec)
1651    /// backend on the node's own userland, rather than by a container backend
1652    /// (R577-T1 / W254).
1653    ///
1654    /// Opt-in via `annotations["yah.exec"] == "native"` (see
1655    /// [`NATIVE_EXEC_ANNOTATION`] / [`NATIVE_EXEC_VALUE`]) — the same
1656    /// annotation-shaped, zero-blast-radius marker as
1657    /// [`Self::wants_host_network`] and [`Self::requires_taint`], chosen over
1658    /// a new plain field for the reason R572-F1 recorded: a field forces a
1659    /// struct-literal edit at every existing construction site and an
1660    /// exhaustive-match update in `kamaji-proto`'s codec, and this marker
1661    /// needs neither.
1662    ///
1663    /// # Why an annotation and not a runtime enum on the wire
1664    ///
1665    /// The remote-execution wire already carries exactly one workload shape —
1666    /// `Workload::Container(WorkloadSpec)` — and every layer between the
1667    /// dispatcher and the node (yubaba admission, mesh assignment, log
1668    /// ingest, produced-file retrieval, teardown) is written against it. A
1669    /// Darwin build differs from a Linux build in *one* respect: there is no
1670    /// container that can host it, because you cannot containerize the Darwin
1671    /// kernel. Marking that one difference keeps the rest of the path shared
1672    /// instead of growing a parallel `exec_native` RPC that would have to
1673    /// re-implement all of it.
1674    ///
1675    /// `image` stays populated for a native workload and is **identity
1676    /// metadata only** — nothing is pulled; the native backend resolves argv
1677    /// from `entrypoint` + `command` (container semantics) and execs it on
1678    /// the host.
1679    pub fn wants_native_exec(&self) -> bool {
1680        self.annotations
1681            .get(NATIVE_EXEC_ANNOTATION)
1682            .map(|v| v == NATIVE_EXEC_VALUE)
1683            .unwrap_or(false)
1684    }
1685
1686    /// Whether this workload builds its **own unprivileged container sandbox**
1687    /// inside the one the backend gives it, and therefore needs the two
1688    /// capabilities plus the `no_new_privs` relaxation that setting up a
1689    /// user namespace requires (R636-B2).
1690    ///
1691    /// Opt-in via `annotations["yah.sandbox"] == "nested"` (see
1692    /// [`NESTED_SANDBOX_ANNOTATION`] / [`NESTED_SANDBOX_VALUE`]) — the same
1693    /// annotation-shaped, zero-blast-radius marker as
1694    /// [`Self::wants_host_network`] and [`Self::wants_native_exec`].
1695    ///
1696    /// # What it actually grants, and why exactly that
1697    ///
1698    /// Rootless BuildKit (the only user today: remote `build-image` steps
1699    /// dispatch `moby/buildkit:*-rootless`) boots through `rootlesskit`, which
1700    /// must map a range of sub-uids into a fresh user namespace. It does that
1701    /// by exec'ing the **setuid-root** helpers `newuidmap` / `newgidmap`, so
1702    /// it needs `CAP_SETUID` + `CAP_SETGID` in the bounding set *and*
1703    /// `noNewPrivileges = false` (with `no_new_privs` on, the kernel silently
1704    /// strips the setuid bit and the helper fails with "Could not set caps").
1705    ///
1706    /// Each of those three was measured on us-west-002 to be **individually
1707    /// necessary** — dropping any one of them puts `rootlesskit` back to
1708    /// failing before the first layer:
1709    ///
1710    /// | grant | `rootlesskit` result |
1711    /// |---|---|
1712    /// | baseline (`CAP_NET_BIND_SERVICE` only, `nnp` on) | `fork/exec /usr/bin/newuidmap: operation not permitted` |
1713    /// | `+CAP_SETUID` only, `nnp` off | `fork/exec /usr/bin/newgidmap: operation not permitted` |
1714    /// | `+CAP_SETUID +CAP_SETGID`, `nnp` **on** | `newuidmap: Could not set caps` |
1715    /// | `+CAP_SETUID +CAP_SETGID`, `nnp` off | starts; build runs to completion |
1716    ///
1717    /// It is deliberately *not* `CAP_SYS_ADMIN`: a non-rootless buildkitd
1718    /// would need that instead, which is a far wider grant. Emptying
1719    /// `/etc/subuid` to force `rootlesskit`'s single-mapping path does not
1720    /// avoid the helpers either — it just fails earlier with "No subuid
1721    /// ranges found".
1722    ///
1723    /// **The backend guards this.** Like host networking, it is honoured only
1724    /// for `tier == "infra"` workloads; a non-infra workload that sets the
1725    /// annotation is rejected at deploy. Every other workload keeps the
1726    /// `CAP_NET_BIND_SERVICE`-only, `no_new_privs` baseline.
1727    ///
1728    /// # Mutually exclusive with [`Self::wants_native_exec`]
1729    ///
1730    /// This grant is defined in terms of an **OCI process spec** — a
1731    /// capability set and a `noNewPrivileges` bit. A native (fork+exec)
1732    /// workload has no OCI spec, so there is nothing to apply it to; kamaji
1733    /// refuses a spec carrying both markers rather than accepting a request
1734    /// for widened privileges and silently dropping it (R577-T1 owns that
1735    /// refusal). The two are independent *annotations* — neither implies the
1736    /// other, which is what
1737    /// `nested_sandbox_marker_is_independent_of_the_other_markers` pins — but
1738    /// they are not a legal *pair*.
1739    ///
1740    /// If a future runtime does have a sandbox worth widening (a MacVM under
1741    /// W254, say), give it its own annotation rather than relaxing that
1742    /// refusal. The grant this marker names is `CAP_SETUID` + `CAP_SETGID` +
1743    /// `no_new_privs` off and nothing else; letting it mean a different
1744    /// privilege set per backend would make "what does `yah.sandbox=nested`
1745    /// grant?" unanswerable without knowing which backend received it, which
1746    /// is precisely what a security-relevant marker must not be.
1747    pub fn wants_nested_sandbox(&self) -> bool {
1748        self.annotations
1749            .get(NESTED_SANDBOX_ANNOTATION)
1750            .map(|v| v == NESTED_SANDBOX_VALUE)
1751            .unwrap_or(false)
1752    }
1753}
1754
1755/// Annotation key requesting a workload share the host network namespace.
1756/// See [`WorkloadSpec::wants_host_network`].
1757pub const HOST_NETWORK_ANNOTATION: &str = "yah.network";
1758
1759/// Annotation value (for [`HOST_NETWORK_ANNOTATION`]) selecting host
1760/// networking. Any other value leaves the workload in an isolated netns.
1761pub const HOST_NETWORK_VALUE: &str = "host";
1762
1763/// Annotation key declaring that a workload must land only on a node
1764/// carrying a specific taint. See [`WorkloadSpec::requires_taint`].
1765pub const REQUIRES_TAINT_ANNOTATION: &str = "yah.placement.requires-taint";
1766
1767/// Taint name (for [`REQUIRES_TAINT_ANNOTATION`]) identifying machines with
1768/// a publicly-routable IP — the W267 sovereign-ingress placement
1769/// requirement. The corresponding taint field on the machine TOML doesn't
1770/// exist yet (R572-F3); this constant is the agreed-upon name both sides
1771/// will use once it does.
1772pub const PUBLIC_IP_TAINT: &str = "public-ip";
1773
1774/// Annotation key selecting kamaji's native (fork+exec) backend for a
1775/// workload. See [`WorkloadSpec::wants_native_exec`].
1776pub const NATIVE_EXEC_ANNOTATION: &str = "yah.exec";
1777
1778/// Annotation value (for [`NATIVE_EXEC_ANNOTATION`]) selecting native
1779/// host execution. Any other value leaves the workload on a container
1780/// backend.
1781pub const NATIVE_EXEC_VALUE: &str = "native";
1782
1783/// Annotation key requesting the capabilities a workload needs to stand up an
1784/// unprivileged container sandbox of its own.
1785/// See [`WorkloadSpec::wants_nested_sandbox`].
1786pub const NESTED_SANDBOX_ANNOTATION: &str = "yah.sandbox";
1787
1788/// Annotation value (for [`NESTED_SANDBOX_ANNOTATION`]) requesting the
1789/// nested-sandbox grant (`CAP_SETUID` + `CAP_SETGID`, `no_new_privs` off).
1790/// Any other value leaves the workload on the baseline sandbox.
1791pub const NESTED_SANDBOX_VALUE: &str = "nested";
1792
1793// ── ImageRef ─────────────────────────────────────────────────────────────────
1794
1795/// Container image reference identifying a specific image to pull.
1796///
1797/// **Digest is required.** Every executable image reference in the workspace
1798/// is content-addressed by `sha256:<hex>`. The `tag` is preserved as a
1799/// human-readable identifier but is not the source of truth — registries
1800/// return mutable `tag → digest` mappings and we don't trust them for
1801/// reproducibility. R438-T3 tightened `digest: Option<String> → String` to
1802/// make unpinned-image bugs impossible by construction.
1803///
1804/// **Two deserialize shapes.** The struct form
1805/// (`registry`/`repository`/`tag`/`digest` fields) is the on-disk envelope.
1806/// A **string form** (`image = "ghcr.io/foo/bar:v1@sha256:<hex>"`) is also
1807/// accepted and is the shape W164 transform recipes (R438-T4) and W165
1808/// `BuildMode::InContainer` (R438-T6) use. Both shapes go through a single
1809/// parser ([`compose_import::parse_pinned_image_ref`]) that rejects
1810/// bare-tag references at serde-deserialize.
1811#[derive(Debug, Clone, PartialEq, Eq, Serialize, TS)]
1812#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1813pub struct ImageRef {
1814    /// Registry hostname, e.g. `"ghcr.io"` or `"localhost:5000"`.
1815    pub registry: String,
1816
1817    /// Repository path, e.g. `"noisetable/api"`.
1818    pub repository: String,
1819
1820    /// Tag, e.g. `"v1.4.2"` or `"latest"`. Informational — the digest is
1821    /// the source of truth for image identity.
1822    pub tag: String,
1823
1824    /// Content-addressed pinned identity, e.g. `"sha256:abc..."`. Required.
1825    pub digest: String,
1826}
1827
1828impl<'de> Deserialize<'de> for ImageRef {
1829    fn deserialize<D>(de: D) -> Result<Self, D::Error>
1830    where
1831        D: serde::Deserializer<'de>,
1832    {
1833        #[derive(Deserialize)]
1834        struct Fields {
1835            registry: String,
1836            repository: String,
1837            tag: String,
1838            digest: String,
1839        }
1840
1841        // The string-or-struct `untagged` probe requires `deserialize_any`,
1842        // which only self-describing formats support. Postcard — the binary
1843        // wire behind the kamaji UDS — returns `WontImplement` for it, so a
1844        // `Workload::Container(WorkloadSpec)` carrying a nested `ImageRef`
1845        // failed to decode and every container deploy 500'd (R590-B3).
1846        //
1847        // The string form is purely an authoring convenience in human-readable
1848        // configs (`image = "ghcr.io/…@sha256:…"` in recipe/workload TOML and
1849        // JSON); the binary wire only ever carries the derived struct form
1850        // (Serialize is a plain struct derive). So branch on the format: text
1851        // keeps the string-or-struct convenience via `untagged`; binary decodes
1852        // the plain positional struct with no `deserialize_any`.
1853        if de.is_human_readable() {
1854            #[derive(Deserialize)]
1855            #[serde(untagged)]
1856            enum Repr {
1857                // Order matters for `untagged`: try the string form first so
1858                // explicit strings don't get coerced into a struct error.
1859                Pinned(String),
1860                Struct(Fields),
1861            }
1862
1863            match Repr::deserialize(de)? {
1864                Repr::Pinned(s) => {
1865                    compose_import::parse_pinned_image_ref(&s).map_err(serde::de::Error::custom)
1866                }
1867                Repr::Struct(f) => Ok(ImageRef {
1868                    registry: f.registry,
1869                    repository: f.repository,
1870                    tag: f.tag,
1871                    digest: f.digest,
1872                }),
1873            }
1874        } else {
1875            let f = Fields::deserialize(de)?;
1876            Ok(ImageRef {
1877                registry: f.registry,
1878                repository: f.repository,
1879                tag: f.tag,
1880                digest: f.digest,
1881            })
1882        }
1883    }
1884}
1885
1886// ── testing helpers ───────────────────────────────────────────────────────────
1887
1888/// Fixture helpers for test code that needs to construct types whose schemas
1889/// would otherwise demand operator-pinned values (digests, hashes). Doc-hidden
1890/// to discourage misuse from non-test code — production paths must source
1891/// digests from registry resolution or compile-time injection.
1892#[doc(hidden)]
1893pub mod testing {
1894    /// Fixed valid-format sha256 digest for test fixtures. All-zeros marker
1895    /// is impossible for any real image, so a leaked test fixture in a
1896    /// production code-path surfaces obviously.
1897    ///
1898    /// Aliases [`super::ImageRef::UNPINNED_DIGEST`] — the two are deliberately
1899    /// the same value: the fixture sentinel and the production "unpinned"
1900    /// marker must agree so [`super::ImageRef::pull_ref`]'s tag-fallback fires
1901    /// on exactly the digest `catalog_image` writes.
1902    pub const TEST_DIGEST: &str = super::ImageRef::UNPINNED_DIGEST;
1903
1904    /// Owned `String` form of [`TEST_DIGEST`] for fixture constructors.
1905    pub fn test_digest() -> String {
1906        TEST_DIGEST.to_string()
1907    }
1908}
1909
1910// ── EnvVar ────────────────────────────────────────────────────────────────────
1911
1912/// A single environment variable injected into the container.
1913#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1914#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1915pub struct EnvVar {
1916    /// Variable name, conventionally `SCREAMING_SNAKE_CASE`.
1917    pub name: String,
1918
1919    /// Value source.
1920    pub value: EnvValue,
1921}
1922
1923/// Value source for an environment variable.
1924#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1925#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1926#[serde(rename_all = "snake_case")]
1927pub enum EnvValue {
1928    /// Static string baked into the spec.
1929    Literal { value: String },
1930
1931    /// Resolved from a yubaba secret at deploy time; the secret value never
1932    /// appears in the spec JSON.
1933    FromSecret { secret: String, key: String },
1934
1935    /// Resolved from another workload's mesh address at deploy time by yubaba.
1936    /// Lets workloads reference each other symbolically without IP pinning.
1937    FromMesh { ident: MeshIdent, kind: MeshLookup },
1938}
1939
1940/// Which aspect of a mesh peer's address to inject.
1941#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
1942#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1943#[serde(rename_all = "snake_case")]
1944pub enum MeshLookup {
1945    /// Full URL, e.g. `"http://noisetable-db.pdx:5432"`.
1946    Url,
1947    /// Hostname only, e.g. `"noisetable-db.pdx"`.
1948    Host,
1949    /// Port only, e.g. `"5432"`.
1950    Port,
1951}
1952
1953// ── Secrets ───────────────────────────────────────────────────────────────────
1954
1955/// A secret value mounted into the container as an env var or file.
1956///
1957/// The secret value never appears in the spec JSON — only the reference.
1958/// Yubaba audits secret access per workload from these references.
1959#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1960#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1961pub struct SecretMount {
1962    /// Where yubaba reads the secret value from.
1963    pub source: SecretRef,
1964
1965    /// How the secret is surfaced inside the container.
1966    pub target: SecretTarget,
1967}
1968
1969/// Where yubaba resolves the secret value from.
1970#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1971#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1972#[serde(rename_all = "snake_case")]
1973pub enum SecretRef {
1974    /// Per-machine yubaba secret store at `/var/lib/yah/yubaba/secrets/`.
1975    LocalFile { path: PathBuf },
1976
1977    /// Raft-replicated cluster secret spanning all machines (planned; not in
1978    /// V1 deployment). Sketch preserved for wire compatibility.
1979    Cluster { name: String },
1980}
1981
1982/// How the secret is surfaced inside the container.
1983#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1984#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1985#[serde(rename_all = "snake_case")]
1986pub enum SecretTarget {
1987    /// Injected as an environment variable. Value never appears in spec JSON.
1988    /// Prefer `File` — env vars leak through subprocess env and log dumps.
1989    EnvVar { name: String },
1990
1991    /// Mounted as a file inside the container at `path` with `mode` (octal).
1992    File { path: PathBuf, mode: u32 },
1993}
1994
1995// ── Volumes ───────────────────────────────────────────────────────────────────
1996
1997/// A volume mount inside the container.
1998#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1999#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2000pub struct VolumeMount {
2001    /// Backing volume source.
2002    pub source: VolumeSource,
2003
2004    /// Absolute path inside the container.
2005    pub target: PathBuf,
2006
2007    /// Whether the container sees the volume as read-only.
2008    pub read_only: bool,
2009}
2010
2011/// Backing source for a volume mount.
2012#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2013#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2014#[serde(rename_all = "snake_case")]
2015pub enum VolumeSource {
2016    /// Yubaba-managed named volume; created on first use.
2017    Named { name: String },
2018
2019    /// Operator-managed host path. Yubaba rejects bind mounts unless
2020    /// `WorkloadSpec.tier == "infra"`; shape validation enforces this.
2021    Bind { host_path: PathBuf },
2022
2023    /// In-memory tmpfs; discarded on container stop. `size_mb` caps space
2024    /// consumed by the writable layer.
2025    Tmpfs { size_mb: u32 },
2026}
2027
2028// ── Durable forge produced-artifact convention (R603-T5) ──────────────────────
2029
2030/// Convention for a remote forge step's durable produced artifacts.
2031///
2032/// A remote build (e.g. the rusty_v8 musl build on a build-worker) writes its
2033/// output tarball to a path *inside* the container. The container's rootfs is
2034/// destroyed when kamaji reaps the EXITED container — so if the camp daemon is
2035/// down when the build finishes, the artifact is gone before boot-reconcile can
2036/// retrieve it (R603-T4 surfaced this as `Success`-but-`UNPUBLISHED`).
2037///
2038/// The fix (R603-T5) is a **host-persistent bind mount**: forge Subprocess
2039/// workloads mount [`HOST_ROOT`]`/<forge_id>` onto [`CONTAINER_DIR`], so a
2040/// build that writes its `produces` under `/yah/produced` lands the bytes on
2041/// the worker's host filesystem. yubaba then reads them back from the host path
2042/// ([`host_path`]) — which outlives container reaping — instead of the
2043/// unreachable container rootfs.
2044///
2045/// The container-side path and the host root are a shared convention between
2046/// three crates: the qed `build_workload_spec` that adds the mount, kamaji that
2047/// binds it, and the yubaba handler that reads + reaps it. Keeping it here (the
2048/// crate all three already depend on) is the single source of truth.
2049pub mod forge_produced {
2050    use std::path::{Path, PathBuf};
2051
2052    /// Conventional container-side directory a remote forge step writes its
2053    /// durable produced artifacts to. Bind-mounted onto a host-persistent dir.
2054    pub const CONTAINER_DIR: &str = "/yah/produced";
2055
2056    /// Host root under which each forge's durable produced dir lives, one
2057    /// subdir per run: `<HOST_ROOT>/<forge_id>/`. yubaba owns this directory —
2058    /// it creates the per-forge subdir at deploy, serves reads from it, and
2059    /// reaps it on teardown / TTL sweep.
2060    pub const HOST_ROOT: &str = "/var/lib/yah/qed/produced";
2061
2062    /// Forge mesh idents are `forge.<id>` (see [`WorkloadSpec::for_forge`]).
2063    /// Extract the bare `<id>`, or `None` for a non-forge ident.
2064    ///
2065    /// [`WorkloadSpec::for_forge`]: super::WorkloadSpec::for_forge
2066    pub fn forge_id_from_ident(ident: &str) -> Option<&str> {
2067        ident.strip_prefix("forge.")
2068    }
2069
2070    /// The host-persistent produced directory for one forge run.
2071    pub fn host_dir(forge_id: &str) -> PathBuf {
2072        PathBuf::from(HOST_ROOT).join(forge_id)
2073    }
2074
2075    /// Translate a container-side produced path to its durable host path for a
2076    /// given forge run. Returns `None` when `container_path` is not under
2077    /// [`CONTAINER_DIR`] (the caller then knows the artifact was not written to
2078    /// the durable location and won't survive reaping), or when the relative
2079    /// path contains a `..` component (a traversal attempt that could escape the
2080    /// per-forge dir — the reader must never serve a file outside it).
2081    pub fn host_path(forge_id: &str, container_path: &Path) -> Option<PathBuf> {
2082        let rel = container_path.strip_prefix(CONTAINER_DIR).ok()?;
2083        if rel
2084            .components()
2085            .any(|c| matches!(c, std::path::Component::ParentDir))
2086        {
2087            return None;
2088        }
2089        Some(host_dir(forge_id).join(rel))
2090    }
2091
2092    /// The durable produced-dir bind mount for a forge run: host
2093    /// `<HOST_ROOT>/<forge_id>` → container [`CONTAINER_DIR`], writable.
2094    pub fn durable_mount(forge_id: &str) -> super::VolumeMount {
2095        super::VolumeMount {
2096            source: super::VolumeSource::Bind {
2097                host_path: host_dir(forge_id),
2098            },
2099            target: PathBuf::from(CONTAINER_DIR),
2100            read_only: false,
2101        }
2102    }
2103
2104    /// True when `path` is (or is under) the conventional durable produced dir
2105    /// — the guard qed uses to enforce that declared `produces` land somewhere
2106    /// reap-durable.
2107    pub fn is_durable_path(path: &Path) -> bool {
2108        path.starts_with(CONTAINER_DIR)
2109    }
2110}
2111
2112// ── Forge host-state root (R636-B1) ───────────────────────────────────────────
2113
2114/// The one host directory tree a QED forge step's bind mounts may live under.
2115///
2116/// # Why this is a named root rather than a list of paths
2117///
2118/// runc refuses a bind whose source is missing, and the OCI mapper never
2119/// mkdirs one — so *something* has to create each host dir before deploy.
2120/// yubaba does, but only for paths it recognizes, and "recognizes" was
2121/// originally a hardcoded match on the produced dir. Every new forge mount then
2122/// re-learned the lesson the expensive way, on a real box, minutes into a
2123/// build: R603-B6 for `produced/`, then R636-B1 for `build-out/`, each
2124/// surfacing as the same opaque `failed to fulfil mount request: … no such file
2125/// or directory` from deep inside containerd.
2126///
2127/// Naming the *root* makes the rule checkable instead of enumerable: yubaba
2128/// creates any forge bind under [`HOST_ROOT`], and `yubaba.service` grants the
2129/// root once via `StateDirectory=yah/qed`. A third mount needs no new code and
2130/// no unit-file edit — it only has to live here.
2131///
2132/// The prefix bound is load-bearing in the other direction too: it is what
2133/// keeps a workload spec from asking yubaba to mkdir an arbitrary host path.
2134pub mod forge_state {
2135    use std::path::Path;
2136
2137    /// Root of the forge's host-persistent state. Both
2138    /// [`super::forge_produced::HOST_ROOT`] and [`BUILD_OUT_DIR`] are under it.
2139    pub const HOST_ROOT: &str = "/var/lib/yah/qed";
2140
2141    /// Host directory a `build-image` step's OCI archive is written to, bound
2142    /// at `/yah/build/out` in the BuildKit container. Shared (rather than
2143    /// per-forge like `produced/`) because the archive is named after the image
2144    /// tag, which is already unique per build.
2145    pub const BUILD_OUT_DIR: &str = "/var/lib/yah/qed/build-out";
2146
2147    /// Whether yubaba may create `host_path` on behalf of a forge workload.
2148    ///
2149    /// Rejects anything outside [`HOST_ROOT`], and anything with a `..`
2150    /// component — `/var/lib/yah/qed/../../../etc` starts with the root as a
2151    /// string and is nowhere near it as a path.
2152    pub fn is_forge_state_path(host_path: &Path) -> bool {
2153        !host_path
2154            .components()
2155            .any(|c| matches!(c, std::path::Component::ParentDir))
2156            && host_path.starts_with(HOST_ROOT)
2157    }
2158}
2159
2160#[cfg(test)]
2161mod forge_state_tests {
2162    use super::forge_state::*;
2163    use std::path::Path;
2164
2165    #[test]
2166    fn both_known_forge_roots_are_under_the_state_root() {
2167        assert!(is_forge_state_path(Path::new(
2168            super::forge_produced::HOST_ROOT
2169        )));
2170        assert!(is_forge_state_path(Path::new(BUILD_OUT_DIR)));
2171        assert!(is_forge_state_path(&super::forge_produced::host_dir(
2172            "abc-123"
2173        )));
2174    }
2175
2176    /// A spec must not be able to steer yubaba's mkdir anywhere it likes —
2177    /// neither by naming an unrelated absolute path nor by climbing out with
2178    /// `..`, which a plain string prefix check would wave through.
2179    #[test]
2180    fn paths_outside_the_root_are_refused() {
2181        for bad in [
2182            "/var/lib/yah/yubaba",
2183            "/etc/systemd/system",
2184            "/var/lib/yah/qed/../../../etc",
2185            "relative/path",
2186        ] {
2187            assert!(
2188                !is_forge_state_path(Path::new(bad)),
2189                "{bad} must not be creatable by a forge spec"
2190            );
2191        }
2192    }
2193}
2194
2195// ── Resources ─────────────────────────────────────────────────────────────────
2196
2197/// Hard resource caps enforced by containerd/cgroups at runtime.
2198#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2199#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2200pub struct ResourceLimits {
2201    /// Maximum RAM the container may allocate, in MiB. The container is OOM-
2202    /// killed if it exceeds this.
2203    pub memory_mb: u32,
2204
2205    /// CPU **request** in millicores (k8s convention): `1000` = one full core,
2206    /// `250` = `.25 CPU`. Unlike a Docker relative weight this is an
2207    /// allocatable quantity a bin-packer can subtract from a node's budget.
2208    /// `0` means "no CPU limit". Backends that speak a relative weight derive
2209    /// it via [`ResourceLimits::cpu_shares`].
2210    pub cpu_millis: u32,
2211
2212    /// Cap on the writable layer + tmpfs footprint, in MiB.
2213    pub ephemeral_storage_mb: u32,
2214}
2215
2216impl ResourceLimits {
2217    /// The Docker/OCI relative CPU weight (`cpu.shares`, where `1024` ≈ one
2218    /// core) equivalent to this millicore request. The containerd and docker
2219    /// backends express CPU as a weight rather than a millicore request, so
2220    /// they derive it here instead of storing shares: `1000m` ⇒ `1024`.
2221    pub fn cpu_shares(&self) -> u64 {
2222        (u64::from(self.cpu_millis) * 1024) / 1000
2223    }
2224}
2225
2226// ── Healthcheck ───────────────────────────────────────────────────────────────
2227
2228/// Container health probe configuration.
2229#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2230#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2231pub struct Healthcheck {
2232    /// The probe executed to determine container health.
2233    pub probe: HealthProbe,
2234
2235    /// How often the probe runs.
2236    pub interval: Millis,
2237
2238    /// Per-probe timeout; a slow response counts as failure.
2239    pub timeout: Millis,
2240
2241    /// Time to wait after container start before the first probe. Shape
2242    /// validation warns (not errors) if this is less than
2243    /// `stop_policy.grace_period * 2`.
2244    pub initial_delay: Millis,
2245
2246    /// Number of consecutive failures before the container is marked
2247    /// `Unhealthy`.
2248    pub failure_threshold: u32,
2249}
2250
2251/// Mechanism used to check container health.
2252#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2253#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2254#[serde(rename_all = "snake_case")]
2255pub enum HealthProbe {
2256    /// HTTP GET to `path` on `port`. A 2xx (or `expect_status` if set)
2257    /// response counts as healthy.
2258    HttpGet {
2259        path: String,
2260        port: u16,
2261        #[ts(optional = nullable)]
2262        expect_status: Option<u16>,
2263    },
2264
2265    /// Run `argv` inside the container; exit-0 counts as healthy.
2266    Exec { argv: Vec<String> },
2267
2268    /// TCP connection to `port`; a successful connect counts as healthy.
2269    TcpConnect { port: u16 },
2270}
2271
2272// ── Restart / Stop ────────────────────────────────────────────────────────────
2273
2274/// What yubaba does when the container exits.
2275#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2276#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2277#[serde(rename_all = "snake_case")]
2278pub enum RestartPolicy {
2279    /// Restart unconditionally on any exit.
2280    Always,
2281
2282    /// Restart on non-zero exit, up to `max_attempts` times with exponential
2283    /// backoff. After exhaustion, the workload is marked `Failed`.
2284    OnFailure {
2285        max_attempts: u32,
2286        backoff: BackoffPolicy,
2287    },
2288
2289    /// Do not restart. The container runs once and exits.
2290    ///
2291    /// **Forge convention.** Forge runs (R094) synthesize a `WorkloadSpec`
2292    /// using [`WorkloadSpec::for_forge`] which sets all the conventional fields
2293    /// together:
2294    ///
2295    /// - `restart_policy = Never`
2296    /// - `expose.public = None`, `expose.operator = None`
2297    /// - `expose.mesh.identity = "forge.<forge_id>"` — distinguishable from
2298    ///   persistent mirror identities at the mesh layer
2299    /// - `tier = "infra"` (or the forge-spec's effective tier)
2300    /// - `annotations["yah.forge"] = "true"` — suppresses the shape warning
2301    ///
2302    /// Using `Never` on a persistent mirror (not a forge run) means the mirror
2303    /// stays dead after any exit — a likely misconfiguration. Shape validation
2304    /// emits a soft warning unless `annotations["yah.forge"] == "true"` is
2305    /// present. See R094 forge.
2306    Never,
2307}
2308
2309/// Exponential backoff parameters for `RestartPolicy::OnFailure`.
2310#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2311#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2312pub struct BackoffPolicy {
2313    /// Initial delay before the first restart, in milliseconds.
2314    pub initial_ms: u32,
2315
2316    /// Maximum delay between retries, in milliseconds.
2317    pub max_ms: u32,
2318
2319    /// Backoff multiplier applied to each successive delay.
2320    pub multiplier: f32,
2321}
2322
2323/// Graceful shutdown configuration for yubaba's stop sequence.
2324#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2325#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2326pub struct StopPolicy {
2327    /// Signal number sent first, e.g. `15` (SIGTERM) or `2` (SIGINT).
2328    pub signal: i32,
2329
2330    /// Time yubaba waits after sending `signal` before issuing SIGKILL.
2331    pub grace_period: Millis,
2332}
2333
2334// ── Expose ────────────────────────────────────────────────────────────────────
2335
2336/// Network exposure configuration. The three channels are independent; any
2337/// combination is valid.
2338#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2339#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2340pub struct ExposeSpec {
2341    /// Mesh-internal exposure. Required; every workload must have a mesh
2342    /// identity even if no other workload currently reaches it.
2343    pub mesh: MeshExpose,
2344
2345    /// Public internet exposure via a Cloudflare tunnel route. `None` means
2346    /// the workload is not internet-reachable.
2347    #[ts(optional = nullable)]
2348    pub public: Option<PublicExpose>,
2349
2350    /// Operator-facing exposure via a Tailscale ACL tag. `None` means the
2351    /// workload is not operator-reachable via Tailscale.
2352    #[ts(optional = nullable)]
2353    pub operator: Option<OperatorExpose>,
2354}
2355
2356/// A peer permitted to initiate mesh connections to a workload (W206 / R558-F3).
2357///
2358/// Cross-tenant access is **deny-by-default**: a workload accepts inter-tenant
2359/// traffic only from peers it lists explicitly as [`MeshPeer::CrossTenant`].
2360/// Same-tenant access stays tier-based ([`MeshPeer::Tier`]) — the pre-R558
2361/// model — and an `allow_from` with no `Tier` entries still admits every
2362/// same-tenant peer (the historical "empty = allow all" default).
2363///
2364/// External serde tagging keeps this postcard-safe (R590-B3): no internal tag,
2365/// no untagged, no `skip_serializing_if`.
2366#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2367#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2368#[serde(rename_all = "snake_case")]
2369pub enum MeshPeer {
2370    /// Any **same-tenant** workload whose `tier` matches this tag. This is the
2371    /// pre-R558 `allow_from` semantics.
2372    Tier(TierTag),
2373
2374    /// A specific workload in **another tenant**, addressed by its fully
2375    /// qualified mesh identity `<tenant>/<namespace>/<name>`. There is no
2376    /// cross-tenant tier wildcard — each cross-tenant peer is granted
2377    /// individually, so a shared fleet stays isolated unless an operator opts
2378    /// in here.
2379    CrossTenant {
2380        tenant: TenantId,
2381        namespace: NamespaceId,
2382        /// Peer's mesh identity (its [`MeshExpose::identity`]).
2383        name: MeshIdent,
2384    },
2385}
2386
2387/// Mesh-internal port exposure and peer access control.
2388#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2389#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2390pub struct MeshExpose {
2391    /// DNS-segment mesh identity for this workload. Must be unique in the
2392    /// cluster. Regex: `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, length ≤ 63.
2393    pub identity: MeshIdent,
2394
2395    /// Container-side ports this workload listens on. Other workloads reach
2396    /// it at `<identity>:<port>` on the mesh.
2397    pub ports: Vec<u16>,
2398
2399    /// Peers permitted to initiate connections to this workload on the mesh
2400    /// (W206 / R558-F3). Same-tenant tier rules and explicit cross-tenant
2401    /// grants share this one list. With **no** [`MeshPeer::Tier`] entries every
2402    /// same-tenant peer is admitted (the historical "empty = allow all"
2403    /// default); cross-tenant peers are always denied unless named by a
2404    /// [`MeshPeer::CrossTenant`] entry. See [`MeshExpose::admits_peer`].
2405    #[serde(default)]
2406    pub allow_from: Vec<MeshPeer>,
2407}
2408
2409impl MeshExpose {
2410    /// Whether a peer may initiate a mesh connection to a workload whose mesh
2411    /// exposure is `self`. `own_tenant` is the tenant of the workload being
2412    /// protected; the remaining arguments identify the connecting peer.
2413    ///
2414    /// Deny-by-default across tenants (W206 / R558-F3):
2415    /// - **Same tenant** (`own_tenant == peer_tenant`): admitted when the
2416    ///   peer's tier matches a [`MeshPeer::Tier`] rule, or when there are no
2417    ///   `Tier` rules at all (historical "empty `allow_from` = allow all
2418    ///   same-tenant").
2419    /// - **Cross tenant**: admitted only when an explicit
2420    ///   [`MeshPeer::CrossTenant`] entry matches the peer's
2421    ///   `(tenant, namespace, name)`.
2422    pub fn admits_peer(
2423        &self,
2424        own_tenant: &TenantId,
2425        peer_tenant: &TenantId,
2426        peer_namespace: &NamespaceId,
2427        peer_name: &MeshIdent,
2428        peer_tier: &TierTag,
2429    ) -> bool {
2430        if own_tenant == peer_tenant {
2431            let mut has_tier_rule = false;
2432            for peer in &self.allow_from {
2433                if let MeshPeer::Tier(t) = peer {
2434                    has_tier_rule = true;
2435                    if t == peer_tier {
2436                        return true;
2437                    }
2438                }
2439            }
2440            // No same-tenant tier restriction declared → admit all same-tenant.
2441            !has_tier_rule
2442        } else {
2443            self.allow_from.iter().any(|peer| {
2444                matches!(
2445                    peer,
2446                    MeshPeer::CrossTenant { tenant, namespace, name }
2447                        if tenant == peer_tenant
2448                            && namespace == peer_namespace
2449                            && name == peer_name
2450                )
2451            })
2452        }
2453    }
2454}
2455
2456/// The name by which a workload is addressed **within its own tenant** (W206 /
2457/// R558-F3), given every `(namespace, identity)` pair present in that tenant.
2458///
2459/// Within a tenant, a workload is reached by its short mesh `identity` when that
2460/// identity is unique across the tenant's namespaces. When two namespaces
2461/// expose the same identity, the name is ambiguous, so both are disambiguated
2462/// by a namespace prefix — `<namespace>.<identity>` (e.g. `yah.runner` vs
2463/// `noisetable.runner`). Cross-tenant addressing always uses the full FQN
2464/// ([`WorkloadSpec::fq_mesh_identity`]) and is out of scope here.
2465pub fn intra_tenant_address(
2466    namespace: &NamespaceId,
2467    identity: &MeshIdent,
2468    tenant_workloads: &[(NamespaceId, MeshIdent)],
2469) -> String {
2470    let collides = tenant_workloads
2471        .iter()
2472        .any(|(ns, id)| id == identity && ns != namespace);
2473    if collides {
2474        format!("{}.{}", namespace.0, identity.0)
2475    } else {
2476        identity.0.clone()
2477    }
2478}
2479
2480/// Public internet exposure via a Cloudflare tunnel route.
2481#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2482#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2483pub struct PublicExpose {
2484    /// Public hostname to route, e.g. `"api.noisetable.io"`. Semantic
2485    /// validation checks that this hostname is owned by a configured CF zone.
2486    pub hostname: String,
2487
2488    /// Container-side port to route traffic to. Shape validation requires this
2489    /// port to appear in `expose.mesh.ports`.
2490    pub port: u16,
2491
2492    /// TLS configuration for the public endpoint.
2493    pub tls: PublicTls,
2494}
2495
2496/// TLS mode for a public endpoint.
2497#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2498#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2499#[serde(rename_all = "snake_case")]
2500pub enum PublicTls {
2501    /// Cloudflare manages the TLS certificate (default; requires a proxied DNS
2502    /// record in the configured zone).
2503    CfManaged,
2504
2505    /// User-supplied certificate referenced by name in the yubaba secret store.
2506    UserCertRef { name: String },
2507}
2508
2509/// Operator-facing exposure via a Tailscale ACL tag.
2510#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2511#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2512pub struct OperatorExpose {
2513    /// Tailscale ACL tag granting access, e.g. `"tag:noisetable-ops"`. Semantic
2514    /// validation checks that this tag exists in the cluster's Tailscale ACL.
2515    pub tailscale_tag: String,
2516
2517    /// Container-side port to expose to Tailscale-authorized operators.
2518    pub port: u16,
2519}
2520
2521// ── ImageRef helpers ──────────────────────────────────────────────────────────
2522
2523impl ImageRef {
2524    /// The all-zeros sha256 digest that marks an image reference as **not
2525    /// content-pinned**. No real image can carry it, so a build that never
2526    /// injected a compile-time digest (dev builds) or a catalog image that
2527    /// isn't published-and-pinned yet lands on this sentinel. This is the
2528    /// single source of truth both the catalog emitter
2529    /// (`task::default_image::catalog_image`, which writes it) and the
2530    /// container-runtime resolvers ([`Self::pull_ref`], via kamaji) agree on —
2531    /// keeping them here means they cannot drift. [`testing::TEST_DIGEST`] is
2532    /// the same value re-exported for fixtures.
2533    pub const UNPINNED_DIGEST: &'static str =
2534        "sha256:0000000000000000000000000000000000000000000000000000000000000000";
2535
2536    /// Parse a full digest-pinned image reference —
2537    /// `[registry/]repo[:tag]@sha256:<hex>` — into its parts.
2538    ///
2539    /// This is the public door onto the same parser the `ImageRef` string-form
2540    /// `Deserialize` arm uses, so a config that spells an image as one string
2541    /// (a qed `step.image`, a transform recipe) and a config that spells it as
2542    /// a struct land on identical semantics. A bare tag is rejected: the whole
2543    /// point of the string form is that it carries the digest.
2544    pub fn parse_pinned(s: &str) -> Result<Self, String> {
2545        compose_import::parse_pinned_image_ref(s)
2546    }
2547
2548    /// Format this reference as a Docker-compatible image string,
2549    /// `{registry}/{repository}:{tag}@{digest}`. Tag is included for human
2550    /// readability; the digest is what the pull resolves against. Always emits
2551    /// the digest — this is the display/logging form; use [`Self::pull_ref`]
2552    /// for the string handed to a container runtime.
2553    pub fn docker_ref(&self) -> String {
2554        format!("{}/{}:{}@{}", self.registry, self.repository, self.tag, self.digest)
2555    }
2556
2557    /// True when this reference carries a real content-addressed digest, i.e.
2558    /// its digest is not the all-zeros [`Self::UNPINNED_DIGEST`] sentinel.
2559    pub fn is_pinned(&self) -> bool {
2560        self.digest != Self::UNPINNED_DIGEST
2561    }
2562
2563    /// The reference string to hand a container runtime for pull/resolve.
2564    ///
2565    /// - **Pinned** (real digest): `{registry}/{repository}:{tag}@{digest}` —
2566    ///   content-addressed, the reproducible path.
2567    /// - **Unpinned** (all-zeros [`Self::UNPINNED_DIGEST`]): `{registry}/{repository}:{tag}`
2568    ///   — tag-only. No registry or local store holds an image under the
2569    ///   sentinel digest, so `…@sha256:0000…` can never resolve; a
2570    ///   tag-pulled or locally-built image is keyed by `registry/repo:tag`.
2571    ///   This is the tag-fallback path that lets a not-yet-published catalog
2572    ///   image (e.g. a from-source build-worker image) still pull by tag.
2573    pub fn pull_ref(&self) -> String {
2574        if self.is_pinned() {
2575            format!("{}/{}:{}@{}", self.registry, self.repository, self.tag, self.digest)
2576        } else {
2577            format!("{}/{}:{}", self.registry, self.repository, self.tag)
2578        }
2579    }
2580}
2581
2582// ── WorkloadRuntime trait ─────────────────────────────────────────────────────
2583
2584/// Shared interface for deploying and managing `WorkloadSpec` containers.
2585///
2586/// This is the keystone abstraction (R256-F10) that makes sim and cloud
2587/// literally interchangeable at the container level:
2588///
2589/// - **Camp/sim tier**: `LocalDockerRuntime` in `cloud` implements this trait
2590///   via the docker CLI pointed at OrbStack (or any Docker-compatible socket).
2591///   No mesh — containers communicate over OrbStack's bridge network.
2592///
2593/// - **Yubaba/cloud-HA tier**: `yubaba::runtime::ContainerRuntime` (gRPC to
2594///   containerd) will implement this trait. Mesh assignment is a separate
2595///   orchestration step on top (handled by yubaba's raft layer), not part
2596///   of the shared deploy/supervise interface.
2597///
2598/// Callers that type against `WorkloadRuntime` automatically work with both
2599/// backends. Reconcilers in `cloud` use it today; yubaba wires its own impl
2600/// when R276 Tier-3 lands.
2601#[async_trait::async_trait]
2602pub trait WorkloadRuntime: Send + Sync {
2603    /// Deploy a workload described by `spec`. Pulls the image if needed,
2604    /// creates and starts the container, and returns an opaque workload ID
2605    /// (typically the container name derived from `spec.name`).
2606    ///
2607    /// Idempotent: re-deploying a running workload replaces it cleanly.
2608    async fn deploy_workload(&self, spec: &WorkloadSpec) -> anyhow::Result<String>;
2609
2610    /// Tear down a deployed workload — stop the process and remove all
2611    /// associated state. No-op when the workload is already gone.
2612    async fn teardown_workload(&self, name: &str) -> anyhow::Result<()>;
2613
2614    /// Returns `true` when the named workload is currently running (i.e.
2615    /// the container process is alive and has not exited).
2616    async fn is_running(&self, name: &str) -> anyhow::Result<bool>;
2617
2618    /// Probe the runtime backend. Returns `true` when the backend socket is
2619    /// reachable and healthy (e.g. docker daemon up, containerd gRPC up).
2620    /// Used by health endpoints and startup checks.
2621    async fn runtime_health(&self) -> anyhow::Result<bool>;
2622}
2623
2624// ── Tests ─────────────────────────────────────────────────────────────────────
2625
2626#[cfg(test)]
2627mod tests {
2628    use super::*;
2629
2630    // ── R603-T5 durable forge produced convention ──────────────────────────────
2631
2632    #[test]
2633    fn forge_produced_ident_parse() {
2634        assert_eq!(forge_produced::forge_id_from_ident("forge.abc123"), Some("abc123"));
2635        assert_eq!(forge_produced::forge_id_from_ident("svc.web"), None);
2636        assert_eq!(forge_produced::forge_id_from_ident("abc123"), None);
2637    }
2638
2639    #[test]
2640    fn forge_produced_host_path_translates_under_convention_dir() {
2641        let hp = forge_produced::host_path(
2642            "fid",
2643            std::path::Path::new("/yah/produced/librusty_v8.tar.gz"),
2644        )
2645        .expect("path under the convention dir translates");
2646        assert_eq!(
2647            hp,
2648            PathBuf::from("/var/lib/yah/qed/produced/fid/librusty_v8.tar.gz")
2649        );
2650    }
2651
2652    #[test]
2653    fn forge_produced_host_path_rejects_paths_outside_convention_dir() {
2654        assert_eq!(
2655            forge_produced::host_path("fid", std::path::Path::new("/tmp/x.tar.gz")),
2656            None,
2657            "a path outside /yah/produced has no durable host mapping"
2658        );
2659    }
2660
2661    #[test]
2662    fn forge_produced_host_path_rejects_traversal() {
2663        // A `..` component must never let a read escape the per-forge dir.
2664        assert_eq!(
2665            forge_produced::host_path(
2666                "fid",
2667                std::path::Path::new("/yah/produced/../../etc/passwd")
2668            ),
2669            None,
2670            "traversal out of the per-forge dir must be refused"
2671        );
2672    }
2673
2674    #[test]
2675    fn forge_produced_durable_mount_shape() {
2676        let m = forge_produced::durable_mount("fid");
2677        assert_eq!(m.target, PathBuf::from("/yah/produced"));
2678        assert!(!m.read_only, "the build must be able to write to it");
2679        assert_eq!(
2680            m.source,
2681            VolumeSource::Bind {
2682                host_path: PathBuf::from("/var/lib/yah/qed/produced/fid"),
2683            }
2684        );
2685    }
2686
2687    const HASH_64: &str = "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890";
2688
2689    #[test]
2690    fn blake_hash_accepts_64_hex() {
2691        let h: BlakeHash = toml::from_str(&format!("x = \"{HASH_64}\""))
2692            .map(|t: toml::Table| t["x"].as_str().unwrap().to_owned())
2693            .map(|s| serde_json::from_value(serde_json::Value::String(s)).unwrap())
2694            .unwrap();
2695        assert_eq!(h.0, HASH_64);
2696    }
2697
2698    #[test]
2699    fn blake_hash_rejects_wrong_length() {
2700        let short = "abcdef";
2701        let res: Result<BlakeHash, _> =
2702            serde_json::from_value(serde_json::Value::String(short.into()));
2703        assert!(res.is_err());
2704    }
2705
2706    #[test]
2707    fn blake_hash_rejects_non_hex() {
2708        let bad = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
2709        let res: Result<BlakeHash, _> =
2710            serde_json::from_value(serde_json::Value::String(bad.into()));
2711        assert!(res.is_err());
2712    }
2713
2714    fn image_ref(digest: &str) -> ImageRef {
2715        ImageRef {
2716            registry: "ghcr.io".into(),
2717            repository: "yah-ai/rusty-v8-musl-builder".into(),
2718            tag: "latest".into(),
2719            digest: digest.into(),
2720        }
2721    }
2722
2723    #[test]
2724    fn is_pinned_distinguishes_real_digest_from_sentinel() {
2725        assert!(!image_ref(ImageRef::UNPINNED_DIGEST).is_pinned());
2726        assert!(!image_ref(&testing::test_digest()).is_pinned());
2727        assert!(image_ref("sha256:deadbeef").is_pinned());
2728    }
2729
2730    #[test]
2731    fn pull_ref_pinned_carries_tag_and_digest() {
2732        assert_eq!(
2733            image_ref("sha256:deadbeef").pull_ref(),
2734            "ghcr.io/yah-ai/rusty-v8-musl-builder:latest@sha256:deadbeef",
2735        );
2736    }
2737
2738    #[test]
2739    fn pull_ref_unpinned_falls_back_to_tag_only() {
2740        // An unpinned catalog image (all-zeros sentinel) resolves by tag —
2741        // no store holds `…@sha256:0000…`, so the tag is the only usable key.
2742        assert_eq!(
2743            image_ref(ImageRef::UNPINNED_DIGEST).pull_ref(),
2744            "ghcr.io/yah-ai/rusty-v8-musl-builder:latest",
2745        );
2746    }
2747
2748    #[test]
2749    fn test_digest_alias_is_the_unpinned_sentinel() {
2750        assert_eq!(testing::TEST_DIGEST, ImageRef::UNPINNED_DIGEST);
2751    }
2752
2753    #[test]
2754    fn static_asset_workload_round_trips() {
2755        let src = format!(
2756            r#"
2757schema_version = "V1"
2758
2759[[asset]]
2760filename = "whisper/distil-large-v3-q5_1.bin"
2761source   = "sources/distil-large-v3-q5_1.bin"
2762blake3   = "{HASH_64}"
2763
2764[[asset]]
2765filename = "whisper/distil-large-v3-q4_0.bin"
2766source   = "sources/distil-large-v3-q4_0.bin"
2767blake3   = "{HASH_64}"
2768
2769[aliases]
2770"whisper-default" = "whisper/distil-large-v3-q5_1.bin"
2771"#
2772        );
2773        let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
2774        assert_eq!(w.assets.len(), 2);
2775        assert_eq!(w.assets[0].filename, "whisper/distil-large-v3-q5_1.bin");
2776        assert_eq!(w.assets[0].blake3.0, HASH_64);
2777        assert_eq!(w.aliases["whisper-default"], "whisper/distil-large-v3-q5_1.bin");
2778
2779        let back = toml::to_string(&w).expect("serialize");
2780        let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
2781        assert_eq!(w, w2);
2782    }
2783
2784    #[test]
2785    fn license_round_trip_each_variant() {
2786        // Wire format is whatever serde's `rename_all = "kebab-case"` emits.
2787        // heck's kebab-case keeps letter→digit attached but splits digit→uppercase,
2788        // so `Apache2 → "apache2"` and `Bsd2Clause → "bsd2-clause"`.
2789        for (variant, on_wire) in [
2790            (License::Mit, "mit"),
2791            (License::Apache2, "apache2"),
2792            (License::Bsd2Clause, "bsd2-clause"),
2793            (License::Bsd3Clause, "bsd3-clause"),
2794            (License::Isc, "isc"),
2795        ] {
2796            let ser = serde_json::to_value(variant).expect("serialize");
2797            assert_eq!(ser, serde_json::Value::String(on_wire.into()));
2798            let back: License = serde_json::from_value(ser).expect("deserialize");
2799            assert_eq!(back, variant);
2800        }
2801    }
2802
2803    #[test]
2804    fn license_rejects_non_permissive_variants() {
2805        for unknown in ["GPL-3.0", "AGPL", "lgpl-2.1", "unknown", "MIT"] {
2806            let res: Result<License, _> =
2807                serde_json::from_value(serde_json::Value::String(unknown.into()));
2808            assert!(res.is_err(), "expected rejection for {unknown:?}");
2809        }
2810    }
2811
2812    #[test]
2813    fn fetch_source_round_trips() {
2814        let src = format!(
2815            r#"
2816url     = "https://example.invalid/upstream.bin"
2817blake3  = "{HASH_64}"
2818license = "mit"
2819"#
2820        );
2821        let fs: FetchSource = toml::from_str(&src).expect("parse");
2822        assert_eq!(fs.url, "https://example.invalid/upstream.bin");
2823        assert_eq!(fs.blake3.0, HASH_64);
2824        assert_eq!(fs.license, License::Mit);
2825
2826        let back = toml::to_string(&fs).expect("serialize");
2827        let fs2: FetchSource = toml::from_str(&back).expect("re-parse");
2828        assert_eq!(fs, fs2);
2829    }
2830
2831    #[test]
2832    fn fetch_source_rejects_unknown_license() {
2833        let src = format!(
2834            r#"
2835url     = "https://example.invalid/upstream.bin"
2836blake3  = "{HASH_64}"
2837license = "GPL-3.0"
2838"#
2839        );
2840        let res: Result<FetchSource, _> = toml::from_str(&src);
2841        assert!(res.is_err(), "expected non-permissive license to reject");
2842    }
2843
2844    #[test]
2845    fn asset_entry_derive_mode_round_trips() {
2846        let src = format!(
2847            r#"
2848schema_version = "V1"
2849
2850[[asset]]
2851filename = "whisper/distil-large-v3-q5_1.bin"
2852blake3   = "{HASH_64}"
2853
2854[asset.derive.fetch]
2855url     = "https://example.invalid/ggml-distil-large-v3.bin"
2856blake3  = "{HASH_64}"
2857license = "mit"
2858
2859[asset.derive.transform]
2860recipe = "whisper-quantize"
2861params = {{ quant = "q5_1" }}
2862"#
2863        );
2864        let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
2865        assert_eq!(w.assets.len(), 1);
2866        let entry = &w.assets[0];
2867        assert!(entry.source.is_none());
2868        let derive = entry.derive.as_ref().expect("derive present");
2869        assert_eq!(derive.fetch.url, "https://example.invalid/ggml-distil-large-v3.bin");
2870        assert_eq!(derive.fetch.license, License::Mit);
2871        let transform = derive.transform.as_ref().expect("transform present");
2872        assert_eq!(transform.recipe, "whisper-quantize");
2873        assert_eq!(transform.params.get("quant").map(String::as_str), Some("q5_1"));
2874
2875        let back = toml::to_string(&w).expect("serialize");
2876        let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
2877        assert_eq!(w, w2);
2878    }
2879
2880    #[test]
2881    fn legacy_source_only_asset_serializes_without_derive_field() {
2882        // Verify the skip_serializing_if guards keep legacy TOMLs round-tripping
2883        // without ever emitting an empty `derive = ...` line.
2884        let src = format!(
2885            r#"
2886schema_version = "V1"
2887
2888[[asset]]
2889filename = "operator-curated.bin"
2890source   = "sources/operator-curated.bin"
2891blake3   = "{HASH_64}"
2892"#
2893        );
2894        let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
2895        let back = toml::to_string(&w).expect("serialize");
2896        assert!(!back.contains("derive"), "serialized output leaked a derive field: {back}");
2897        let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
2898        assert_eq!(w, w2);
2899    }
2900
2901    /// W212/R518: the `[asset.derive.lock]` block round-trips through TOML, and
2902    /// is omitted from output when absent (so non-derive / unlocked assets stay
2903    /// clean).
2904    #[test]
2905    fn derive_lock_round_trips_through_toml() {
2906        let toml = r#"
2907url     = "https://example.invalid/config.json"
2908blake3  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
2909license = "mit"
2910"#;
2911        let fetch: FetchSource = ::toml::from_str(toml).unwrap();
2912        let derive = AssetDerive {
2913            fetch,
2914            transform: Some(TransformSpec {
2915                recipe: "whisper-bundle-tar".into(),
2916                params: BTreeMap::new(),
2917            }),
2918            lock: Some(DeriveLock {
2919                input_hash: "1111111111111111111111111111111111111111111111111111111111111111".into(),
2920                output_blake3: "2222222222222222222222222222222222222222222222222222222222222222".into(),
2921            }),
2922        };
2923        let s = ::toml::to_string(&derive).unwrap();
2924        assert!(s.contains("[lock]"), "lock serialized: {s}");
2925        let back: AssetDerive = ::toml::from_str(&s).unwrap();
2926        assert_eq!(derive, back);
2927
2928        // Absent lock → no `[lock]` table in the output.
2929        let unlocked = AssetDerive { lock: None, ..derive };
2930        let s2 = ::toml::to_string(&unlocked).unwrap();
2931        assert!(!s2.contains("[lock]"), "unlocked must omit lock: {s2}");
2932    }
2933
2934    #[test]
2935    fn shape_static_asset_rejects_both_source_and_derive() {
2936        use crate::validate::{shape_static_asset, FieldPath, ShapeError};
2937
2938        let entry = AssetEntry {
2939            filename: "ambiguous.bin".into(),
2940            source: Some("sources/ambiguous.bin".into()),
2941            derive: Some(AssetDerive {
2942                fetch: FetchSource {
2943                    url: "https://example.invalid/x".into(),
2944                    blake3: BlakeHash(HASH_64.into()),
2945                    license: License::Mit,
2946                },
2947                transform: None,
2948                lock: None,
2949            }),
2950            blake3: BlakeHash(HASH_64.into()),
2951        };
2952        let w = StaticAssetWorkload {
2953            schema_version: SchemaVersion::V1,
2954            assets: vec![entry],
2955            aliases: BTreeMap::new(),
2956        };
2957        let err = shape_static_asset(&w).expect_err("XOR violated");
2958        match err {
2959            ShapeError::Field { path: FieldPath::Asset(0, "source"), .. } => {}
2960            other => panic!("expected Asset(0, \"source\") shape error, got {other:?}"),
2961        }
2962    }
2963
2964    #[test]
2965    fn shape_static_asset_rejects_neither_source_nor_derive() {
2966        use crate::validate::{shape_static_asset, FieldPath, ShapeError};
2967
2968        let entry = AssetEntry {
2969            filename: "empty.bin".into(),
2970            source: None,
2971            derive: None,
2972            blake3: BlakeHash(HASH_64.into()),
2973        };
2974        let w = StaticAssetWorkload {
2975            schema_version: SchemaVersion::V1,
2976            assets: vec![entry],
2977            aliases: BTreeMap::new(),
2978        };
2979        let err = shape_static_asset(&w).expect_err("XOR violated");
2980        match err {
2981            ShapeError::Field { path: FieldPath::Asset(0, "source"), .. } => {}
2982            other => panic!("expected Asset(0, \"source\") shape error, got {other:?}"),
2983        }
2984    }
2985
2986    #[test]
2987    fn shape_static_asset_accepts_either_mode() {
2988        use crate::validate::shape_static_asset;
2989
2990        let legacy = AssetEntry {
2991            filename: "a.bin".into(),
2992            source: Some("sources/a.bin".into()),
2993            derive: None,
2994            blake3: BlakeHash(HASH_64.into()),
2995        };
2996        let derived = AssetEntry {
2997            filename: "b.bin".into(),
2998            source: None,
2999            derive: Some(AssetDerive {
3000                fetch: FetchSource {
3001                    url: "https://example.invalid/b".into(),
3002                    blake3: BlakeHash(HASH_64.into()),
3003                    license: License::Apache2,
3004                },
3005                transform: None,
3006                lock: None,
3007            }),
3008            blake3: BlakeHash(HASH_64.into()),
3009        };
3010        let w = StaticAssetWorkload {
3011            schema_version: SchemaVersion::V1,
3012            assets: vec![legacy, derived],
3013            aliases: BTreeMap::new(),
3014        };
3015        shape_static_asset(&w).expect("both modes accepted");
3016    }
3017
3018    #[test]
3019    fn image_ref_string_form_rejects_bare_tag() {
3020        let res: Result<ImageRef, _> =
3021            serde_json::from_value(serde_json::Value::String("node:20".into()));
3022        let err = res.expect_err("bare-tag must reject");
3023        let msg = format!("{err}");
3024        assert!(msg.contains("digest"), "error should mention digest: {msg}");
3025    }
3026
3027    #[test]
3028    fn image_ref_string_form_accepts_digest_pinned() {
3029        let pinned = format!("node:20@sha256:{HASH_64}");
3030        let img: ImageRef =
3031            serde_json::from_value(serde_json::Value::String(pinned.clone())).expect("parse");
3032        assert_eq!(img.registry, "docker.io");
3033        assert_eq!(img.repository, "library/node");
3034        assert_eq!(img.tag, "20");
3035        assert_eq!(img.digest, format!("sha256:{HASH_64}"));
3036    }
3037
3038    #[test]
3039    fn image_ref_string_form_accepts_ghcr_with_pin() {
3040        let pinned = format!("ghcr.io/foo/bar:v1.7.4@sha256:{HASH_64}");
3041        let img: ImageRef =
3042            serde_json::from_value(serde_json::Value::String(pinned)).expect("parse");
3043        assert_eq!(img.registry, "ghcr.io");
3044        assert_eq!(img.repository, "foo/bar");
3045        assert_eq!(img.tag, "v1.7.4");
3046        assert!(img.digest.starts_with("sha256:"));
3047    }
3048
3049    #[test]
3050    fn image_ref_string_form_rejects_non_sha256_digest() {
3051        for bad in [
3052            "node:20@md5:abcdef",
3053            "node:20@sha1:abcdef",
3054            "node:20@sha256:",
3055            "node:20@sha256:zzznothex",
3056        ] {
3057            let res: Result<ImageRef, _> =
3058                serde_json::from_value(serde_json::Value::String(bad.into()));
3059            assert!(res.is_err(), "expected reject for {bad:?}");
3060        }
3061    }
3062
3063    #[test]
3064    fn image_ref_struct_form_rejects_missing_digest() {
3065        // Digest is now structurally required (R438-T3). Struct-form payloads
3066        // without `digest` must fail at serde-deserialize.
3067        let v = serde_json::json!({
3068            "registry": "ghcr.io",
3069            "repository": "noisetable/api",
3070            "tag": "v1.4.2",
3071        });
3072        let res: Result<ImageRef, _> = serde_json::from_value(v);
3073        assert!(res.is_err(), "missing digest must reject");
3074    }
3075
3076    #[test]
3077    fn image_ref_struct_form_round_trips_through_toml() {
3078        let img = ImageRef {
3079            registry: "ghcr.io".into(),
3080            repository: "ggerganov/whisper.cpp".into(),
3081            tag: "v1.7.4".into(),
3082            digest: format!("sha256:{HASH_64}"),
3083        };
3084        let toml_doc = toml::to_string(&img).expect("serialize");
3085        let back: ImageRef = toml::from_str(&toml_doc).expect("re-parse");
3086        assert_eq!(img, back);
3087    }
3088
3089    /// R546-B7: assert the shape real files use. This test previously fed the
3090    /// EXTERNALLY-tagged wrapping-table form (`[static-asset]` +
3091    /// `[[static-asset.asset]]`), which no on-disk `workload.toml` has ever
3092    /// used — so it stayed green while `yah cloud apply` was broken for every
3093    /// static-asset component. The flat `kind = "..."` form below is what every
3094    /// workload.toml in the workspace is written in.
3095    #[test]
3096    fn workload_envelope_dispatches_static_asset() {
3097        let src = format!(
3098            r#"
3099kind = "static-asset"
3100schema_version = "V1"
3101
3102[[asset]]
3103filename = "foo/bar.bin"
3104source   = "sources/bar.bin"
3105blake3   = "{HASH_64}"
3106"#
3107        );
3108        let w: Workload = toml::from_str(&src).expect("parse");
3109        assert!(matches!(w, Workload::StaticAsset(_)));
3110    }
3111
3112    /// R546-B7: the format branch, both directions. Human-readable formats get
3113    /// the flat `kind`-tagged shape; postcard keeps the externally-tagged
3114    /// variant-index encoding the kamaji UDS depends on (R590-B3). Regressing
3115    /// either side breaks a different half of the system, so pin both.
3116    #[test]
3117    fn workload_envelope_is_tagged_in_toml_and_external_in_postcard() {
3118        let src = format!(
3119            r#"
3120kind = "static-asset"
3121schema_version = "V1"
3122
3123[[asset]]
3124filename = "foo/bar.bin"
3125source   = "sources/bar.bin"
3126blake3   = "{HASH_64}"
3127"#
3128        );
3129        let w: Workload = toml::from_str(&src).expect("parse flat TOML");
3130
3131        // Human-readable round-trips stay flat — no wrapping table.
3132        let json = serde_json::to_string(&w).expect("serialize json");
3133        assert!(json.contains("\"kind\":\"static-asset\""), "got {json}");
3134        assert!(
3135            !json.contains("{\"static-asset\":"),
3136            "human-readable output must not be externally tagged: {json}"
3137        );
3138        assert_eq!(
3139            serde_json::from_str::<Workload>(&json).expect("re-parse json"),
3140            w
3141        );
3142
3143        // postcard is non-self-describing: it can only round-trip because the
3144        // binary branch never asks for deserialize_any.
3145        let bytes = postcard::to_allocvec(&w).expect("postcard encode");
3146        assert_eq!(
3147            postcard::from_bytes::<Workload>(&bytes).expect("postcard decode"),
3148            w
3149        );
3150    }
3151
3152    // ── R572-F1: lifecycle archetype discriminator ─────────────────────────
3153
3154    fn archetype_test_spec(name: &str) -> WorkloadSpec {
3155        WorkloadSpec::for_forge(
3156            name,
3157            ImageRef {
3158                registry: "ghcr.io".into(),
3159                repository: "yah/test".into(),
3160                tag: "latest".into(),
3161                digest: testing::test_digest(),
3162            },
3163            TierTag("infra".into()),
3164            vec![],
3165        )
3166    }
3167
3168    #[test]
3169    fn explicit_archetype_round_trips_through_json_and_wins_over_inference() {
3170        for archetype in [
3171            LifecycleArchetype::Server,
3172            LifecycleArchetype::Appliance,
3173            LifecycleArchetype::Job,
3174        ] {
3175            let mut spec = archetype_test_spec("explicit");
3176            // Volumes present + restart_policy Always would infer Appliance
3177            // (see effective_archetype_infers_* below) — deliberately
3178            // mismatched against every archetype under test so the
3179            // assertion actually proves the explicit field wins, not that
3180            // it happens to agree with inference.
3181            spec.volumes = vec![VolumeMount {
3182                source: VolumeSource::Named { name: "data".into() },
3183                target: PathBuf::from("/data"),
3184                read_only: false,
3185            }];
3186            spec.restart_policy = RestartPolicy::Always;
3187            spec.archetype = Some(archetype);
3188
3189            let json = serde_json::to_string(&spec).expect("serialize");
3190            assert!(
3191                json.contains("\"archetype\""),
3192                "explicit archetype must be present on the wire"
3193            );
3194            let back: WorkloadSpec = serde_json::from_str(&json).expect("deserialize");
3195            assert_eq!(spec, back, "spec did not survive JSON round-trip");
3196            assert_eq!(back.archetype, Some(archetype));
3197            assert_eq!(
3198                back.effective_archetype(),
3199                archetype,
3200                "explicit archetype must win over the volumes/restart_policy inference"
3201            );
3202        }
3203    }
3204
3205    #[test]
3206    fn archetype_serializes_as_null_when_none() {
3207        let mut spec = archetype_test_spec("omitted");
3208        spec.archetype = None;
3209        let json = serde_json::to_value(&spec).expect("to_value");
3210        // Postcard-native (R590-B3): no `skip_serializing_if` anywhere on the
3211        // graph, so every field is always on the wire — a None Option is an
3212        // explicit `null`, not an absent key. The binary UDS wire is positional
3213        // and requires the slot to be present.
3214        assert_eq!(json.get("archetype"), Some(&serde_json::Value::Null));
3215    }
3216
3217    #[test]
3218    fn spec_without_archetype_field_deserializes_to_none() {
3219        // Simulates an on-disk spec written before R572-F1: no `archetype`
3220        // key at all. Omitting the key must still parse to None (the additive-
3221        // default contract) even though we now always *emit* the field.
3222        let mut spec = archetype_test_spec("pre-existing");
3223        spec.archetype = None;
3224        let mut json = serde_json::to_value(&spec).expect("to_value");
3225        json.as_object_mut().unwrap().remove("archetype");
3226        let back: WorkloadSpec = serde_json::from_value(json).expect("deserialize");
3227        assert_eq!(back.archetype, None);
3228    }
3229
3230    #[test]
3231    fn effective_archetype_infers_appliance_from_volumes_when_field_absent() {
3232        // Pre-R572 behavior: a workload with a volume was understood (by
3233        // convention, never a type) to be stateful/pinned. Confirm that
3234        // meaning is preserved bit-for-bit through effective_archetype().
3235        let mut spec = archetype_test_spec("appliance-inferred");
3236        spec.volumes = vec![VolumeMount {
3237            source: VolumeSource::Named { name: "pgdata".into() },
3238            target: PathBuf::from("/var/lib/postgresql/data"),
3239            read_only: false,
3240        }];
3241        spec.restart_policy = RestartPolicy::Always;
3242        spec.archetype = None;
3243        assert_eq!(spec.effective_archetype(), LifecycleArchetype::Appliance);
3244    }
3245
3246    #[test]
3247    fn effective_archetype_infers_job_from_restart_never_when_field_absent() {
3248        // Pre-R572 behavior: RestartPolicy::Never + no volumes is the forge
3249        // run-once convention (see RestartPolicy::Never's own doc comment) —
3250        // structurally a job. WorkloadSpec::for_forge already produces
3251        // exactly this shape; isolate the pure-inference path by clearing
3252        // the explicit archetype for_forge now sets.
3253        let mut spec = archetype_test_spec("job-inferred");
3254        assert!(spec.volumes.is_empty());
3255        assert!(matches!(spec.restart_policy, RestartPolicy::Never));
3256        spec.archetype = None;
3257        assert_eq!(spec.effective_archetype(), LifecycleArchetype::Job);
3258    }
3259
3260    #[test]
3261    fn effective_archetype_defaults_to_server_as_the_common_case_when_field_absent() {
3262        // Pre-R572 behavior: no volumes + a restartable policy (the common
3263        // stateless-web-server shape) inferred as movable/fungible.
3264        let mut spec = archetype_test_spec("server-inferred");
3265        spec.restart_policy = RestartPolicy::Always;
3266        spec.archetype = None;
3267        assert_eq!(spec.effective_archetype(), LifecycleArchetype::Server);
3268    }
3269
3270    // ── R594-F2: public-ingress appliance (container-shaped, not a new
3271    // Workload variant — see Workload::Container's doc comment) ───────────
3272
3273    #[test]
3274    fn ingress_marked_spec_is_appliance_and_carries_public_ip_placement_requirement() {
3275        let mut spec = archetype_test_spec("public-ingress");
3276        spec.archetype = Some(LifecycleArchetype::Appliance);
3277        spec.annotations.insert(
3278            REQUIRES_TAINT_ANNOTATION.to_string(),
3279            PUBLIC_IP_TAINT.to_string(),
3280        );
3281
3282        assert_eq!(
3283            spec.effective_archetype(),
3284            LifecycleArchetype::Appliance,
3285            "ingress must be pinned-per-node/non-drainable, the R572 appliance sense"
3286        );
3287        assert_eq!(
3288            spec.requires_taint(),
3289            Some(PUBLIC_IP_TAINT),
3290            "ingress must declare it can only land on a public-ip-tainted node"
3291        );
3292
3293        // No taint exists to match against yet (R572-F3) and nothing
3294        // enforces placement yet (R572-F5) — confirm this ticket stays
3295        // declarative-only by checking a spec with no requirement stays
3296        // unaffected.
3297        let unrelated = archetype_test_spec("unrelated");
3298        assert_eq!(unrelated.requires_taint(), None);
3299    }
3300
3301    #[test]
3302    fn ingress_marked_spec_round_trips_through_json_as_a_container_workload() {
3303        // Mirrors the on-disk envelope: the externally-tagged `container`
3304        // variant wrapping the WorkloadSpec, exactly like every other
3305        // container-shaped workload. No new Workload variant, no new
3306        // discriminator.
3307        let mut inner = archetype_test_spec("public-ingress");
3308        inner.archetype = Some(LifecycleArchetype::Appliance);
3309        inner.annotations.insert(
3310            REQUIRES_TAINT_ANNOTATION.to_string(),
3311            PUBLIC_IP_TAINT.to_string(),
3312        );
3313        let workload = Workload::Container(inner.clone());
3314
3315        let json = serde_json::to_string(&workload).expect("serialize");
3316        assert!(json.contains("\"container\""));
3317        assert!(json.contains(REQUIRES_TAINT_ANNOTATION));
3318        assert!(json.contains(PUBLIC_IP_TAINT));
3319
3320        let back: Workload = serde_json::from_str(&json).expect("deserialize");
3321        match back {
3322            Workload::Container(spec) => {
3323                assert_eq!(spec, inner);
3324                assert_eq!(spec.effective_archetype(), LifecycleArchetype::Appliance);
3325                assert_eq!(spec.requires_taint(), Some(PUBLIC_IP_TAINT));
3326            }
3327            other => panic!("expected Workload::Container, got {other:?}"),
3328        }
3329    }
3330
3331    // ── Nested-sandbox grant (R636-B2) ──────────────────────────────────────
3332
3333    #[test]
3334    fn nested_sandbox_marker_is_opt_in_and_reads_back() {
3335        // The half that matters: no workload gets the grant by default, so
3336        // adding the marker cannot widen anything already deployed.
3337        let plain = archetype_test_spec("ordinary-build");
3338        assert!(!plain.wants_nested_sandbox());
3339
3340        let mut buildkit = archetype_test_spec("build-image");
3341        buildkit.annotations.insert(
3342            NESTED_SANDBOX_ANNOTATION.to_string(),
3343            NESTED_SANDBOX_VALUE.to_string(),
3344        );
3345        assert!(buildkit.wants_nested_sandbox());
3346
3347        // Fails closed on any other value, same strictness as
3348        // `wants_host_network` — a typo must not hand out CAP_SETUID.
3349        let mut typo = archetype_test_spec("typo");
3350        typo.annotations
3351            .insert(NESTED_SANDBOX_ANNOTATION.to_string(), "Nested".to_string());
3352        assert!(!typo.wants_nested_sandbox());
3353    }
3354
3355    /// The three markers are independent axes: asking for host networking or
3356    /// native exec must not imply the capability grant, and vice versa.
3357    #[test]
3358    fn nested_sandbox_marker_is_independent_of_the_other_markers() {
3359        let mut host_net = archetype_test_spec("host-net");
3360        host_net.annotations.insert(
3361            HOST_NETWORK_ANNOTATION.to_string(),
3362            HOST_NETWORK_VALUE.to_string(),
3363        );
3364        assert!(host_net.wants_host_network());
3365        assert!(!host_net.wants_nested_sandbox());
3366
3367        let mut nested = archetype_test_spec("nested");
3368        nested.annotations.insert(
3369            NESTED_SANDBOX_ANNOTATION.to_string(),
3370            NESTED_SANDBOX_VALUE.to_string(),
3371        );
3372        assert!(nested.wants_nested_sandbox());
3373        assert!(!nested.wants_host_network());
3374        assert!(!nested.wants_native_exec());
3375    }
3376
3377    // ── Native exec marker (R577-T1 / W254) ─────────────────────────────────
3378
3379    #[test]
3380    fn native_exec_marker_is_opt_in_and_reads_back() {
3381        // Default: every forge workload is a container workload. This is the
3382        // half that matters most — the marker must not silently reroute the
3383        // Linux offload leg proven live on us-west-002.
3384        let plain = archetype_test_spec("linux-build");
3385        assert!(!plain.wants_native_exec());
3386
3387        let mut native = archetype_test_spec("darwin-build");
3388        native.annotations.insert(
3389            NATIVE_EXEC_ANNOTATION.to_string(),
3390            NATIVE_EXEC_VALUE.to_string(),
3391        );
3392        assert!(native.wants_native_exec());
3393
3394        // Any other value is not the opt-in — same strictness as
3395        // `wants_host_network`, so a typo fails closed onto the container
3396        // backend rather than escaping the sandbox.
3397        let mut typo = archetype_test_spec("typo");
3398        typo.annotations
3399            .insert(NATIVE_EXEC_ANNOTATION.to_string(), "Native".to_string());
3400        assert!(!typo.wants_native_exec());
3401    }
3402
3403    #[test]
3404    fn native_marked_spec_round_trips_through_json_as_a_container_workload() {
3405        // The point of the annotation shape: a native workload is still a
3406        // `Workload::Container` on the wire, so kamaji-proto's codec, yubaba
3407        // admission and the mesh-assignment path need no new variant.
3408        let mut inner = archetype_test_spec("darwin-build");
3409        inner.annotations.insert(
3410            NATIVE_EXEC_ANNOTATION.to_string(),
3411            NATIVE_EXEC_VALUE.to_string(),
3412        );
3413        let workload = Workload::Container(inner.clone());
3414
3415        let json = serde_json::to_string(&workload).expect("serialize");
3416        assert!(json.contains(NATIVE_EXEC_ANNOTATION));
3417
3418        let back: Workload = serde_json::from_str(&json).expect("deserialize");
3419        match back {
3420            Workload::Container(spec) => {
3421                assert_eq!(spec, inner);
3422                assert!(spec.wants_native_exec());
3423            }
3424            other => panic!("expected Workload::Container, got {other:?}"),
3425        }
3426    }
3427}