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//! @yah:gotcha("HALF-STALE as of 2026-08-18: the gotcha above says both R658 gaps are pinned in workload_envelope.rs KNOWN_GAPS. Only R658-B2 (`missing field image`) still is. R658-B1 is CLOSED - all four `missing field routes` entries were deleted, every manifest and the `yah cloud site init` scaffold now write routes ABOVE [build], and BuildConfig carries serde(deny_unknown_fields) so the misplacement is a parse error rather than a dropped key. See R658-B1.")
262//!
263//! @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)")
264//! @yah:status(review)
265//! @yah:assignee(agent:bundle-anthropic-glimmerstone)
266//! @yah:at(2026-07-23T17:47:24Z)
267//! @yah:kind(spike)
268//! @yah:phase(P3)
269//! @yah:parent(R626)
270//! @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.")
271//! @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.")
272//! @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.")
273//! @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.")
274//! @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.")
275//! @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.")
276//! @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'.")
277//! @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.")
278//! @yah:next("Wire DesiredStateStore::forget into the undeclare path so the document doesn't accumulate intent for mirrors that no longer exist.")
279//! @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")
280//! @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)")
281//! @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.")
282//! @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.")
283//! @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).")
284//!
285//!
286//! @yah:relay(R658, "workload.toml envelope: two type-vs-reality mismatches R546-B7 uncovered but did not fix")
287//! @yah:at(2026-08-03T00:43:00Z)
288//! @yah:status(open)
289//! @yah:assignee(agent:bundle-anthropic-ashguard)
290//! @yah:parent(R546)
291//!
292//! @yah:ticket(R658-B1, "MesofactStaticWorkload.routes is a required top-level field, but every real file and the CLI scaffold write it inside [build]")
293//! @yah:status(review)
294//! @yah:at(2026-08-19T02:08:02Z)
295//! @yah:assignee(agent:bundle-anthropic-ashguard)
296//! @yah:parent(R658)
297//! @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.")
298//! @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.")
299//! @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.")
300//! @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.")
301//! @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.")
302//! @yah:next("SPREAD, found 2026-08-14 by R715-T2: two MORE files hit this and are NOT in KNOWN_GAPS, so `cargo test -p xtask --test workload_envelope` is RED on a clean tree for everyone. The two are app/yah/web/chat/workload.toml and oss/mesofact/examples/hello/workload.toml, both the same routes-after-[build] shape. Deliberately NOT pinned into KNOWN_GAPS - silently widening the pin is what this ticket exists to stop. Migrate them alongside the other four when the type-vs-scaffold decision lands.")
303//! @yah:handoff("DECIDED: the DATA moved to the type, not the type to the data. `routes` stays a TOP-LEVEL field of MesofactStaticWorkload; all eight on-disk manifests and the CLI scaffold now write it ABOVE [build]. Three reasons the reverse was wrong: (1) MesofactStaticWorkload is a postcard wire type over the kamaji UDS, so moving a field between structs is a wire break needing a lockstep kamaji+yubaba deploy; (2) the field's own doc says it is what the RECONCILER reads to enumerate routes, i.e. deploy-time not build-time, so [build] is the wrong home semantically; (3) the reconciler's own fixtures (mesofact_static.rs), three camp.rs fixtures and the struct literal at cloud.rs:5389 already agreed with the type - only the hand-authored TOML disagreed.")
304//! @yah:verify("cargo test -p xtask --test workload_envelope - GREEN (was RED on a clean tree for everyone). All four `missing field routes` KNOWN_GAPS entries DELETED, not widened; only R658-B2's `missing field image` remains.")
305//! @yah:handoff("ROOT-CAUSE GUARD, the part that makes this not recur: workload_spec::BuildConfig now carries #[serde(deny_unknown_fields)] (oss/yah-base/crates/workload-spec/src/lib.rs). Migrating the files alone would have left the trap armed - serde silently dropping a stray [build] key is WHY a declared-but-dead field survived months unnoticed. Now the misplacement is a parse error naming `routes`, which is the one thing the author needs to move. Inert for the postcard kamaji wire (non-self-describing, positional); only constrains TOML/JSON.")
306//! @yah:verify("cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --all-features - 121 lib (incl. 3 new R658-B1 tests) + 8 integration targets, 0 failed. New: mesofact_static_routes_parse_at_the_top_level, mesofact_static_routes_inside_build_is_rejected_by_name, unknown_build_keys_are_refused_rather_than_ignored.")
307//! @yah:verify("cargo test --manifest-path oss/yubaba/Cargo.toml -p yah-cloud --lib - 878 passed, 0 failed.")
308//! @yah:verify("cargo test -p yah --lib cloud:: - 120 passed, 0 failed, incl. the new site_init_tests::scaffold_workload_toml_parses_through_the_envelope_with_top_level_routes.")
309//! @yah:verify("cargo test -p xtask - all 12 targets green, incl. schema_drift and workload_envelope.")
310//! @yah:verify("./scripts/check-workload-spec-ts.sh - in sync (ts-rs ignores deny_unknown_fields, so no TS churn).")
311//! @yah:handoff("DISCOVERED WORK, wider than the ticket title - deny_unknown_fields immediately caught TWO live files the envelope test structurally CANNOT see. app/yah/web/analytics/workload.toml and app/yah/web/dashboard/workload.toml are kind = mesofact-spa, which is not in the test's MODELLED_KINDS, but mesofact-spa rides the SAME MesofactStaticReconciler (app/yah/cli/src/cloud.rs:5195) and therefore the same read_mesofact_build -> BuildConfig parse. Both had routes under [build]; without migrating them my own guard would have broken deploys for analytics.yah.dev and app.yah.dev. Both migrated. Swept every workload.toml in the camp for stray [build] keys: the only two remaining are crates/yah/cloud-admin (R658-B2's file) and app/yah/workers/yah-cr, and NEITHER goes through workload_spec::BuildConfig - both use reconciler-local raw toml::Value extraction (cloudflare_worker.rs:200), so both are unaffected.")
312//! @yah:handoff("ALSO FIXED IN THIS PASS (docs are canon; these were what a human copies): .yah/docs/guides/host-a-site-and-worker-on-yah.md:102 and .yah/docs/architecture/A031-yah-cloud-config-shape.md:438 both showed routes UNDER [build] - they would have re-seeded the bug into every hand-authored manifest. Also oss/yubaba/crates/cloud/src/config.rs:5890 (web_workload_round_trips fixture) and the bundle_assembly_tests fixture at app/yah/cli/src/cloud.rs.")
313//! @yah:handoff("UNRELATED LANDED BREAKAGE unblocked to verify at all: oss/yubaba/crates/cloud/src/reconciler/lowering_golden.rs:48 failed to compile with E0063 missing field `admission` - TransformRecipe gained admission: Option<RecipeAdmission> (oss/qed/crates/velveteen-exec/src/transforms.rs:70, landed in 8b35b0a9) and this golden was never updated. Whole yah-cloud test binary would not build. Added `admission: None` (correct: the golden is an unsigned local recipe and pins the LOWERING shape, which the signature does not participate in). Both files were committed-clean, not a peer's in-flight edit - checked git status before touching.")
314//! @yah:gotcha("UNCOMMITTED REGEN - .yah/schema/workload.toml.schema.json is REGENERATED in the working tree (cargo run -p xtask -- emit-schemas) and must be committed WITH this change. deny_unknown_fields makes schemars emit additionalProperties: false on BuildConfig. scripts/check-schema-drift.sh compares generated output against the git INDEX, so it stays RED until the regen is committed - that is the script working as designed, not drift. Only workload.toml.schema.json moved; no peer's schema was swept in (git diff --stat -- .yah/schema/ = 1 file).")
315//! @yah:assumes("deny_unknown_fields on BuildConfig trades forward-compat for loudness: a manifest carrying a [build] key an older binary does not know is now a hard parse error, not an ignored key. Deliberate and argued in the type's doc comment. Blast radius outside this monorepo is any site scaffolded by an older `yah cloud site init` - the template shipped routes under [build] for its whole life, so such a site now fails to parse until routes is moved above the header. The only tenant in the tree (scrabcake) was migrated; an external one would need the same one-line move.")
316//! @yah:gotcha("NOW FULLY STALE as of 2026-08-19: the HALF-STALE note above says R658-B2's `missing field image` is the one KNOWN_GAPS entry left. R783-F1/F2 closed that too, so KNOWN_GAPS in xtask/tests/workload_envelope.rs is EMPTY - every modelled on-disk workload.toml parses through the envelope. An entry reappearing means a real file stopped parsing.")
317//!
318//!
319//! @yah:ticket(R743-T4, "workload-spec: 7 test binaries to 1")
320//! @yah:at(2026-08-11T01:18:24Z)
321//! @yah:status(review)
322//! @yah:phase(P2)
323//! @yah:parent(R743)
324//! @yah:next("tests/main.rs mod'ing all 7 siblings + autotests = false and [[test]] name = \"main\" in oss/yah-base/crates/workload-spec/Cargo.toml.")
325//! @yah:next("tests/compose/ and tests/fixtures/ are data/module dirs, not targets — they are unaffected. Confirm the [[bin]] named export-ts in Cargo.toml is untouched by autotests = false (it is a bin, not a test, but read it before editing).")
326//! @yah:verify("cargo test -p yah-workload-spec -- --list count unchanged; three green runs. One commit — oss subtree.")
327//! @yah:tier(Cleric)
328//! @yah:handoff("LANDED: tests/main.rs mods in the 7 former top-level integration-test files (compose_import, mesh_resolver, restart_policy, round_trip, secrets_invariant, semantic, shape_fixtures) as submodules; Cargo.toml gained `autotests = false` on [package] plus a single `[[test]] name = \"main\" path = \"tests/main.rs\"`. tests/compose/ and tests/fixtures/ untouched (data dirs); the export-ts [[bin]] untouched (autotests only scans tests/, not bins). Quick audit found nothing to fix: no std::env::set_var/remove_var, no set_current_dir, no TcpListener/bind/fixed ports in any of the 7 files, and only one inner `mod secrets` (in secrets_invariant.rs) which nests fine under its own file-module with no sibling collision — so no renames were needed.")
329//! @yah:verify("RUSTC_WRAPPER=\"\" cargo test -p yah-workload-spec -- --list (run inside oss/yah-base): BEFORE 8 targets (lib 146 + export-ts bin 0 + 7 integration files summing to 65: compose_import 5, mesh_resolver 8, restart_policy 5, round_trip 16, secrets_invariant 7, semantic 18, shape_fixtures 6) = 211 total. AFTER 4 targets (lib 146 + export-ts bin 0 + single `main` integration binary 65, all 65 test names now module-qualified e.g. round_trip::round_trip_full_spec + doctests 0) = 211 total, unchanged.")
330//! @yah:verify("RUSTC_WRAPPER=\"\" cargo test -p yah-workload-spec (inside oss/yah-base): ok. 146 passed lib + ok. 65 passed main + 0 doctests, 0 failed — run three times, all green, no pre-existing failures to record.")
331//!
332//! @yah:ticket(R783-F1, "ContainerManifest: split the on-disk container manifest from the wire WorkloadSpec, keeping postcard byte-identical")
333//! @yah:status(review)
334//! @yah:at(2026-08-19T07:11:49Z)
335//! @yah:assignee(agent:bundle-anthropic-ashguard)
336//! @yah:parent(R783)
337//! @arch:see(.yah/docs/working/W324-workload-kind-is-not-a-runtime.md)
338//! @yah:next("THE SEAM: introduce `ContainerManifest = Reference(WorkloadSpec) | Recipe(ContainerBuild)` and change Workload::Container's payload to it. WorkloadExternal::Container KEEPS WorkloadSpec so the postcard kamaji wire is byte-identical - verify with the existing round_trip.rs postcard tests, which must pass UNCHANGED.")
339//! @yah:next("WHY a recipe cannot just be a WorkloadSpec (this is the whole design): ImageRef.digest is String, not Option<String> - R438-T3 tightened it deliberately and the string form REJECTS a bare tag at serde-deserialize (lib.rs:187, parser compose_import::parse_pinned_image_ref). The local form's image is `yah-local/yah-cloud-admin:dev`, a bare tag, because the digest does not exist until docker build has run. Preserve that invariant; do not weaken ImageRef to make this easier.")
340//! @yah:next("Encode the invariant in the signature: ContainerBuild::into_spec(self, digest: &str) -> WorkloadSpec. The lowering is only available AFTER a build produced a digest. Serializing a Recipe to postcard must be an Err, not a panic and not a silent empty digest.")
341//! @yah:next("Tier: Wizard - cross-workspace type split with a wire invariant to preserve; the postcard encoding is positional and a mistake decodes silently into the wrong variant.")
342//! @yah:verify("cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --all-features - round_trip.rs postcard tests must pass UNCHANGED (they are the wire-compat gate).")
343//! @yah:verify("cargo test -p xtask --test workload_envelope with the `missing field image` KNOWN_GAPS entry for crates/yah/cloud-admin/workload.toml DELETED - that file is the acceptance case.")
344//! @yah:verify("cargo test --manifest-path oss/kamaji/Cargo.toml -p kamaji-proto codec - deploy_container_round_trip is the exact UDS path.")
345//! @yah:gotcha("VARIANT ORDER IS LOAD-BEARING on WorkloadExternal/WorkloadExternalRef - postcard encodes the external tag as the variant INDEX, so reordering or inserting anywhere but the end silently decodes kamaji UDS frames into the WRONG variant, with no type error. Commented at the definitions in lib.rs.")
346//! @yah:gotcha("BLAST RADIUS ~25 real construction/match sites across FOUR workspaces: oss/kamaji (incl. peer-owned kamaji-proto/src/codec.rs exhaustive matches), oss/yubaba, oss/qed, app/yah/cli, oss/yah-base. R594-F2 deliberately avoided exactly this churn by using an annotation instead of a field (lib.rs:225) - that was right for a marker, and is NOT right here, but read that note before assuming the churn is accidental.")
347//! @yah:gotcha("Consider a Workload::container(spec) constructor + ContainerManifest::as_spec() accessor to keep the ~25 sites one-line mechanical rather than restructured.")
348//! @yah:handoff("LANDED. `ContainerManifest = Reference(WorkloadSpec) | Recipe(ContainerBuild)` is now `Workload::Container`'s payload (oss/yah-base/crates/workload-spec/src/lib.rs). New public types: ContainerManifest, ContainerBuild, ContainerBuildStep, ContainerRunConfig, ContainerMount, plus `Workload::container(spec)` / `Workload::container_spec()` / `Workload::container_manifest()` so the ~25 call sites stayed one-line.")
349//! @yah:verify("cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --all-features - 127 lib + 8 integration targets, 0 failed. round_trip.rs: 16 pass.")
350//! @yah:verify("cargo test -p xtask - all 12 targets green incl. workload_envelope 1/1 with KNOWN_GAPS now EMPTY (R658-B2's `missing field image` entry deleted, not widened) and schema_drift 3/3.")
351//! @yah:verify("cargo test --manifest-path oss/kamaji/Cargo.toml --workspace - green incl. kamaji-proto codec 26/26 (deploy_container_round_trip, the exact UDS path) and kamaji-bin 213/213.")
352//! @yah:verify("cargo test --manifest-path oss/yubaba/Cargo.toml -p yah-cloud --lib 881 pass / -p yubaba --lib 492 pass; cargo test -p yah --lib cloud:: 120 pass.")
353//! @yah:handoff("THE WIRE CLAIM IS NOW A TEST, not an assertion. round_trip.rs::container_postcard_frame_is_the_variant_index_then_the_bare_spec asserts the frame is exactly [1] ++ postcard(WorkloadSpec) - a round-trip alone would still pass if both halves moved together. WorkloadExternal::Container keeps WorkloadSpec; Workload's binary Serialize maps Reference through unchanged and returns Err for Recipe (round_trip.rs::container_recipe_is_refused_by_postcard_rather_than_encoded).")
354//! @yah:handoff("DISCRIMINATOR: presence of a `[build]` table means Recipe; presence of top-level `image` means Reference; NEITHER is its own error naming both forms rather than a misleading `missing field image`. Hand-written Deserialize, not serde(untagged), specifically so a malformed reference still reports `missing field tier` instead of 'data did not match any variant'.")
355//! @yah:gotcha("UNCOMMITTED REGEN - both generated artifacts are regenerated in the working tree and must be committed WITH this change: .yah/schema/workload.toml.schema.json (cargo run -p xtask -- emit-schemas) and packages/yah/workload-spec/index.ts (cargo run --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --bin export-ts). Both scripts/check-schema-drift.sh and scripts/check-workload-spec-ts.sh exit 1 right now because they diff generated output against the git INDEX - that is the scripts working as designed, not drift. cargo test -p xtask schema_drift (which diffs against the working tree) is GREEN.")
356//! @yah:gotcha("SIGNATURE DEVIATION from the ticket text, deliberate: into_spec is `ContainerBuild::into_spec(self, digest: &str, tier: TierTag) -> Result<WorkloadSpec, String>`, not the infallible two-arg form the ticket sketched. Fallible because digest is a caller-supplied string and a malformed one must error rather than mint a spec that lies about being content-addressed - it routes through compose_import::parse_pinned_image_ref, the one home of the R438-T3 digest rule. tier is a parameter because admission control is cluster policy, not a manifest fact. Recorded in W324 under a new 'As shipped (R783-F1)' section.")
357//! @yah:assumes("ContainerBuild::into_spec has NO production caller yet - it is the documented lowering with unit-test coverage only. Its unset-image default is `yah-local/<manifest name>:dev`, which is NOT the same string ContainerReconciler's default_image_tag builds (`yah-local/<service>-<component>:dev`) because the manifest only knows its own name. If a future caller lowers a recipe whose [build].image was left unset and expects to find the image the reconciler built, those two defaults have to be reconciled first.")
358//! @yah:cleanup("LocalProcessReconciler still parses its own private ProcessComponent for the [process] table (oss/yubaba/crates/cloud/src/reconciler/local_process.rs:696). The envelope does not model [process] at all, so that tier is still a second parser over the same file - the exact shape R783-F2 just removed for the container tier. W324 section 1 names it as the third runtime behind kind = container; folding it in is the natural next step and is deliberately NOT in R783.")
359//!
360//! @yah:ticket(R838-B1, "xtask workload_envelope fails on both machines: the template deliberately omits [build] command while workload_spec Workload requires it as a non-Option String")
361//! @yah:status(review)
362//! @yah:at(2026-08-31T00:17:46Z)
363//! @yah:assignee(agent:bundle-anthropic-ashguard)
364//! @yah:parent(R838)
365//! @yah:handoff("LANDED. workload_spec::BuildConfig.command is now Option<String> with #[serde(default)] (oss/yah-base/crates/workload-spec/src/lib.rs:1439). Absent means the project has no external bundler step, which is what mesofact new's scaffold template documents about itself. xtask workload_envelope now passes with KNOWN_GAPS still EMPTY, which was the goal state that test names for itself.")
366//! @yah:handoff("WHY THIS WAS NOT A DECISION AFTER ALL. The sibling ticket R658-B3 filed the same bug as DECISION REQUIRED because it read the deploy path as having no branch for a missing command. It has one, in two of the three readers, and it predates this change: app/yah/cli/src/cloud.rs:3368 read_workload_build has ALWAYS returned Option<String>; assemble_component_bundle_with_sidecars (cloud.rs:3492) needs the command only under --run-build and otherwise assembles from an existing out_dir; deploy_mesofact_bundle (cloud.rs:5841) refuses None with a message that already reads correctly, and cloud.rs:8387 a_missing_build_command_is_reported_not_skipped already tested that refusal. The only reader that made it mandatory was the type. So no in-process build branch had to be invented.")
367//! @yah:handoff("RECONCILER: lower_build_to_forge_spec now returns Option<ForgeSpec> (None when no command) and run_build logs a skip and returns Ok. That is the same outcome rebuild_static already produced for a workload with no workload.toml. Deliberately NOT sh -c with an empty string: that exits 0 having built nothing, so the reconciler would report success and publish stale out_dir bytes.")
368//! @yah:handoff("WIRE: BuildConfig rides the postcard kamaji wire inside Workload::MesofactStatic, so String -> Option<String> adds a leading tag byte. A pre-R838 node decoding a new frame fails loudly (a string length byte is not a valid Option tag) rather than reading a shifted field, which is why this is Option and not a serde(default) empty-String sentinel. NOT a cluster-epoch surface: xtask/src/cluster_epochs.rs hashes the yubaba raft modules and the openraft pin, not workload_spec; all 8 cluster_epoch_drift tests stayed green, so no epoch bump is owed.")
369//! @yah:handoff("CALL SITES (10, four workspaces, all mechanical): kamaji-proto/src/codec.rs:1077, kamaji-bin/src/server.rs x3, yubaba/src/lib.rs:9000, cloud/src/reconciler/lowering_golden.rs x2 (+3 .expect() on the now-Option lowering), cloud/src/reconciler/mesofact_static.rs (revalidate_static's render BuildConfig + 2 fixtures + 3 assertions), app/yah/cli/src/cloud.rs:5725.")
370//! @yah:handoff("GENERATED ARTIFACTS REGENERATED AND MUST BE COMMITTED WITH THIS: .yah/schema/workload.toml.schema.json (command dropped from required, type now [string,null]) and packages/yah/workload-spec/index.ts (command: string | null). Both scripts/check-workload-spec-ts.sh and scripts/check-schema-drift.sh exit 1 until the commit lands because they diff against the git INDEX; the working-tree equivalent, cargo test -p xtask schema_drift, is green. Same shape as the R783-F1 note above.")
371//! @yah:verify("cargo test -p xtask --tests --locked: 54 passed, 0 failed. Includes workload_envelope::every_on_disk_workload_toml_parses_through_the_envelope (was 0 passed / 1 failed with 'missing field command'), schema_drift 3/3, cluster_epoch_drift 8/8.")
372//! @yah:verify("cargo test --manifest-path oss/yah-base/Cargo.toml -p yah-workload-spec --all-features --locked: 146 lib + 68 integration, 0 failed. Three NEW tests in tests/round_trip.rs: mesofact_static_build_table_without_a_command_parses_as_none, an_unknown_build_key_is_still_refused_now_that_command_is_optional (deny_unknown_fields from R658-B1 did not loosen), mesofact_static_build_command_round_trips_through_postcard_both_ways.")
373//! @yah:verify("cargo test --manifest-path oss/yubaba/Cargo.toml -p yah-cloud --lib: 927 passed, 0 failed, 4 ignored. Three NEW tests in reconciler::mesofact_static::tests: read_mesofact_build_accepts_a_build_table_with_no_command, rebuild_static_skips_the_build_step_when_no_command_is_declared (asserts the CaptureExecutor got nothing), lowering_a_build_with_no_command_yields_no_forge_spec.")
374//! @yah:verify("cargo test --manifest-path oss/yubaba/Cargo.toml -p yubaba --lib: 553 passed, 0 failed.")
375//! @yah:verify("cargo test --manifest-path oss/kamaji/Cargo.toml --workspace: all green incl. kamaji-proto codec 26/26 (the UDS round-trip) and kamaji-bin 217/217.")
376//! @yah:verify("cargo check --all-targets --locked on the root workspace: clean (warnings only, all pre-existing).")
377//! @yah:verify("cargo test --locked --no-fail-fast on the root workspace: one failure, yah-log tests::init_noop_without_env, which is NOT this change and is already filed as R840-B1 (it reads process-global env and the camp build rail exports YAH_TASK_RUN + YAH_LOG_PIPE; it passes in CI and under env -u).")
378//! @yah:gotcha("DEAD GENERATED FILE FOUND, not touched: oss/packages/yah/workload-spec/index.ts is tracked, a month stale (last written 2026-07-29, has no ContainerManifest so it predates R783-F1), referenced by nothing, and gated by nothing. It is the fossil of the off-by-one that export-ts.rs:107 documents in its own comment: with ancestors().nth(3) the bin wrote to oss/packages/ instead of the camp root, and the stray output got committed. export-oss.sh exports oss/<name> subtrees, and oss/packages is not one, so it is not even on an export path. Deleting it is a one-line git rm but it is a tracked-file deletion outside this ticket, so it is named here rather than done.")
379//! @yah:gotcha("STALE CLAIM in a neighbouring annotation, disproved but left in place: oss/yubaba/crates/cloud/src/reconciler/mesofact_static.rs:165 (R438-T6) says read_mesofact_build must hand-extract toml::Value subtrees because 'schema_version = 1 (integer) ... the typed envelope rejects'. R546-B7 made SchemaVersion read the bare integer (oss/yah-base/crates/workload-spec/src/version.rs), and the workload_envelope run proves it: the only error reported for the scaffold template was 'missing field command', never schema_version. The subtree reader has other reasons to exist, but that one is gone.")
380//!
381//! @yah:ticket(R658-B3, "mesofact new scaffolds a workload.toml the deploy path cannot execute: BuildConfig.command is required but the template deliberately omits it")
382//! @yah:status(review)
383//! @yah:assignee(agent:bundle-anthropic-ashguard)
384//! @yah:at(2026-09-02T19:08:17Z)
385//! @yah:parent(R658)
386//! @yah:severity(high)
387//! @yah:next("DECISION REQUIRED, do not guess. oss/mesofact/crates/mesofact/src/cli/new/template/workload.toml (new in ef8bd656) declares kind = mesofact-static with no [build] command, and its own header comment says that is deliberate: 'Left unset, mesofact-dev runs the build pipeline in-process — no third binary, no package manager, no Node.' But BuildConfig.command is a required String (oss/yah-base/crates/workload-spec/src/lib.rs:1428), so the file does not parse through workload_spec::Workload.")
388//! @yah:next("The deploy path has no in-process branch. MesofactStaticReconciler uses build.command unconditionally — oss/yubaba/crates/cloud/src/reconciler/mesofact_static.rs:1143 builds vec![sh, -c, build.command.clone()], and 1173/1179/1185 log and execute it. So making command Option<String> is NOT a mechanical type change: it requires deciding what 'yah cloud bundle build' DOES for a manifest with no command. That is the actual open question.")
389//! @yah:next("Three options. (a) command becomes Option<String> and the reconciler gains an in-process build branch — matches the template's documented intent and the W225 s2 'no package manager, no Node' promise, but MesofactStaticWorkload is a postcard wire type over the kamaji UDS, so a shape change is a lockstep kamaji+yubaba deploy (see the R658-B1 handoff, which rejected moving a field for exactly this reason). (b) The template gains a command — contradicts its own comment and the no-Node promise. (c) Add a KNOWN_GAPS entry in xtask/tests/workload_envelope.rs — unblocks check today, records the gap honestly, decides nothing.")
390//! @yah:verify("cargo test -p xtask --test main --locked -- workload_envelope::every_on_disk_workload_toml_parses_through_the_envelope (currently: 0 passed, 1 failed, 'missing field command')")
391//! @yah:gotcha("BLOCKS THE RELEASE GATE. This fails cargo test -p xtask --tests, which is check.toml step 7 (xtask-tests), which release-check runs before oss-publish. It also very likely fails release-check's second sub-pipeline mesofact-new-smoke, whose stated promise is that 'mesofact new' produces a project that builds and serves with no package manager and no Node on PATH — the same scaffold.")
392//! @yah:gotcha("WHY THIS WAS INVISIBLE UNTIL NOW: check.toml's cargo-test step has no --no-fail-fast and died at yah-party, so steps 5-16 never ran this cycle. Separately, R605-S6 documents that xtask is a workspace member but NOT a default-member, so plain cargo test never reaches these tests at all, and workload_envelope was named there as one of eight test binaries that had been dark since being written. KNOWN_GAPS in xtask/tests/workload_envelope.rs is currently empty, so this file DID parse before ef8bd656 introduced the template — it is a regression, not a pre-existing gap.")
393//! @yah:tier(Warrior)
394//! @yah:next("Option (a) was taken. The open question that made this a decision was already answered by the code: yah cloud bundle build does NOT require a command. app/yah/cli/src/cloud.rs:3368 read_workload_build has always typed it Option<String>; assemble_component_bundle_with_sidecars needs it only under --run-build; deploy_mesofact_bundle refuses None by name at cloud.rs:5841. So no in-process build branch had to be invented: the reconciler skips the build step for None, exactly as it already did for a workload with no workload.toml at all.")
395//! @yah:next("TO CLOSE: re-run cargo test -p xtask --test main --locked -- workload_envelope:: (passes now) and archive. Nothing left to build here.")
396//! @yah:handoff("FIXED BY R838-B1 (same bug, filed twice; R838-B1 is the older ID). BuildConfig.command is now Option<String> in oss/yah-base/crates/workload-spec/src/lib.rs. cargo test -p xtask --tests is 54/54 green including workload_envelope, the gate that was failing.")
397//! @yah:handoff("GENERATED ARTIFACTS CONFIRMED LANDED, which R838-B1's handoff flagged as still-uncommitted and therefore red. Both are now committed and clean against the index (git status --porcelain reports nothing for either): .yah/schema/workload.toml.schema.json carries command with \"default\": null and \"type\": [\"string\",\"null\"] under the BuildConfig object, and packages/yah/workload-spec/index.ts carries `command: string | null` at line 452. The unrelated required-\"command\" at schema line 126 is the Almanac/render struct (lib.rs:1661), which is correctly still a bare String. So scripts/check-schema-drift.sh and scripts/check-workload-spec-ts.sh no longer have anything to fail on for this change.")
398//! @yah:verify("cargo test -p xtask --test main --locked -- workload_envelope:: — 1 passed, 0 failed, 55 filtered out. every_on_disk_workload_toml_parses_through_the_envelope is ok; it was 0 passed / 1 failed with \"missing field command\" when this ticket was filed. This is the exact argv named in the ticket's @yah:verify, and it is the criterion this ticket closes on.")
399//! @yah:gotcha("DISPROVED A CLAIM ON R836-B2 IN PASS and recorded it there. Its COVERAGE NOTE said the failing assertion lives in xtask's lib target which \"neither\" check.toml xtask step reaches, and its second @yah:next asked to widen the guard to include --lib. Measured: `--tests` DOES reach the lib target — running check.toml's own xtask-tests argv produced the 43/1 result above and cargo's footer read \"error: test failed, to rerun pass -p xtask --lib\". So that next step is a no-op and the bar already covers the assertion.")
400//! @yah:cleanup("NOT FIXED, out of this ticket's blast radius, flagged for whoever owns the mesofact_static reconciler: oss/yubaba/crates/cloud/src/reconciler/mesofact_static.rs:240-245 has 13 unused imports (EnvVar, ExposeSpec, ImageRef, MeshExpose, Millis, NamespaceId, ResourceLimits, RestartPolicy, SchemaVersion, StopPolicy, TenantId, TierTag, WorkloadSpec, NATIVE_IDENTITY_DIGEST). Warnings only, so nothing is blocked. They are pre-existing on committed main (file is clean in the working tree; last moved in committed 9677282b \"mes\", 2026-09-01), NOT introduced by the R838-B1 change and not a live peer's WIP. Worth a look because that many newly-unused imports usually means a block of code was removed, and it is worth confirming that removal was intended rather than collateral.")
401//! @yah:gotcha("CORRECTION to this ticket's own inherited handoff line \"cargo test -p xtask --tests is 54/54 green including workload_envelope\". That was true when R838-B1 wrote it and is NOT true on main as of 2026-09-02. That argv now gives 43 passed / 1 failed, failing in the LIB target on cluster_epochs::tests::the_declaration_records_current_per_input_digests_for_every_axis (\"state_epoch: recorded digest for `rust-file oss/yubaba/crates/yubaba/src/raft/store.rs` is stale\"). That failure is R836-B2, is unrelated to workload.toml, and is deliberately NOT a regenerate-the-artifact case — it is a mixed-operation compatibility call (bump the state_epoch vs re-record the digest) owned by whoever owns the raft store change, so it was correctly left alone here. It matters for reading this ticket because `--tests` carries no --no-fail-fast: that single lib failure aborts the step before the integration binary runs, so workload_envelope is reported as neither passed nor failed rather than green. That is why this ticket was verified with the narrower `--test main -- workload_envelope::`, which isolates the gate this ticket actually owns. check.toml step xtask-tests stays red camp-wide until R836-B2 is answered.")
402//!
403//! @yah:ticket(R844-F17, "Port names are unwritable in every manifest — the declaration surface F15 built the plumbing for")
404//! @yah:status(review)
405//! @yah:assignee(agent:bundle-anthropic-ashguard)
406//! @yah:at(2026-09-04T01:29:45Z)
407//! @yah:parent(R844)
408//! @yah:depends_on(R844-F15)
409//! @yah:handoff("LANDED. A manifest can name its ports. `MeshExpose.ports` went from `Vec<u16>` to `Vec<MeshPort>` (oss/yah-base/crates/workload-spec/src/lib.rs) and accepts three spellings that mix freely in one array: a bare number `8080` (unnamed — every manifest written before this), a bare string `\\\"http\\\"` (a name whose number the supervisor picks), and a table `{ name = \\\"http\\\", port = 8080 }` (both stated). Read it with `MeshExpose::numbers()`, `named_numbers()`, `names()`; write the old shape with `MeshExpose::anonymous_ports([..])`. There is deliberately NO conversion back to a plain `Vec<u16>`: a name-only entry has no number yet, and a `Vec<u16>` field could not say that its list is shorter than the one the author wrote.")
410//! @yah:handoff("THE NAMES REACH THE RECORD, which is the only thing that makes this worth the blast radius. `kamaji::declared_port_names(&MeshExpose)` (oss/kamaji/crates/kamaji/src/lib.rs) is the new single lowering from manifest to the `name -> port` map every tier below already spoke, and it replaced `name_anonymous_ports(&spec.expose.mesh.ports)` at all five call sites — kamaji's fake/containerd/docker/native backends and yubaba's `ServiceRecordStore::upsert_deployed`. So `ports = [{ name = \\\"http\\\", port = 8080 }, { name = \\\"metrics\\\", port = 9090 }]` now publishes `{\\\"http\\\":8080,\\\"metrics\\\":9090}` in the service record, `ServiceRecordFanout::port_for` resolves `http`, and the ingress planner stops refusing a two-listener workload. That refusal was the ONLY reason a multi-port slot had to keep a `port` pin forever, which is the whole R844 thesis.")
411//! @yah:handoff("THE NAMING RULE, and the care in it — `declared_port_names` does NOT promote an unnamed leftover to `http`. If NOTHING is named the whole list falls through to `name_anonymous_ports` byte-for-byte (sole port -> `http`; several -> their own numbers, none `http`), which is the compatibility property the entire change rests on and is asserted directly against the old function by kamaji::tests::an_unnamed_declaration_resolves_identically_to_the_old_synthesis. If ANYTHING is named, declared names are used verbatim and unnamed siblings become their own number. Rejected the obvious alternative — \\\"the one they left bare must be the default\\\" — for the same reason R844-F15 rejected first-is-http: an author who names one of three ports has shown they name deliberately, so promoting the leftover invents exactly the fact (THIS is the listener the world dials) that naming exists to state. A caller asking for `http` and getting None sends them back to the manifest.")
412//! @yah:handoff("PROTOCOL V7, and it is not the same kind of break as V2/V4/V5/V6 — read the new stanza in oss/kamaji/crates/kamaji-proto/src/version.rs before touching either wire. `WorkloadSpec` rides `Workload::Container` inside the postcard `Deploy` frame, so changing the ELEMENT TYPE of `expose.mesh.ports` (a `Vec<u16>` is len + varints; a `Vec<MeshPort>` is len + two-`Option` structs) does not fail cleanly at the port list — an unbumped peer consumes the wrong byte count and then misreads EVERY FIELD AFTER IT in the spec, i.e. deploys a wrong image or a wrong volume mount instead of erroring. `ProtocolVersion::CURRENT` is now V7. The blast radius is unchanged and unchanged in kind: one node, yubaba+kamaji rolled as a pair, which R844-F15's V6 already requires — so this rides that same paired roll at zero extra operational cost, and T10's recorded ordering does not change.")
413//! @yah:handoff("THE JSON WIRE IS UNAFFECTED, deliberately and by the same split `ImageRef` makes (R590-B3): `MeshPort` branches on `is_human_readable()`, so TOML/JSON get the flexible three-spelling form and postcard gets the plain positional two-`Option` struct. Consequence worth knowing — an UNNAMED port serializes to JSON as the bare number it always was, so `{\\\"ports\\\":[8080]}` is byte-identical in both directions against an un-rolled reader; only a manifest that actually names a port produces JSON an old reader cannot take. Pinned by tests/mesh_ports.rs::the_binary_wire_carries_both_halves_of_every_spelling and ::every_spelling_round_trips_through_toml.")
414//! @yah:handoff("THE NAME-ONLY SPELLING IS ACCEPTED AND WARNS, which is a deliberate choice between two worse ones. `ports = [\\\"http\\\", \\\"wss\\\"]` parses, validates and crosses both wires, but NOTHING BINDS IT: I measured why and it is structural, not an oversight — a container's ports are its image's, the native and bundle tiers WRITE `expose.mesh.ports` from the port they already resolved rather than reading it (native.rs:158 says so in its own words), and `kamaji::ports::PortAllocator::resolve_set`, which R844-F14 built for exactly this, still has ZERO production callers. So `validate::shape` emits a ShapeWarning naming the port and telling the author to state the number. Rejecting the spelling would refuse one the guide and `kamaji::ports`' own module doc both document; accepting it silently would be the inert-config failure this relay exists to eliminate. Filed as R844-F21 with the measurement and the one design question it has to answer first (what a name-only port means on a CONTAINER workload).")
415//! @yah:handoff("DISCOVERED WORK DONE IN THIS PASS, beyond the ticket title. (1) TWO PRODUCERS NOW STATE `http` INSTEAD OF LEAVING IT TO BE RE-DERIVED: kamaji-bin's bundle archetype (server.rs, the `ports` parsed back off `--listen`) and yubaba's mesofact-static reconciler (mesofact_static.rs, the allocator's `spawn_port`) both asked the allocator for the port under `kamaji::ports::HTTP` and then threw the name away; both now write `MeshPort::pinned(HTTP, n)`. Same value today, right value if a bundle ever serves a second listener. (2) `validate::shape` gained the port-list rules it never had — an entry stating neither name nor number, a name that is not a DNS label of at most 15 chars, a repeated name, a repeated number. The two uniqueness rules are load-bearing: a repeated NAME makes `name -> port` ambiguous at the exact moment `ServiceRecord::port(\\\"http\\\")` or `PORT_HTTP` asks for it. (3) Two positional reads in oss/yah-base/crates/local-driver corrected to go through `numbers()` (local_runtime's sim-tier host-port publish, pond_ssr_runtime's container port). (4) The guide's \\\"no manifest schema carries a port-NAME key yet\\\" section in .yah/docs/guides/write-a-service-toml.md is replaced with the real spelling — that paragraph is what this ticket was filed off.")
416//! @yah:gotcha("A WORKSPACE-LOCAL `cargo check --all-targets` DOES NOT SEE oss/yah-base's TEST TARGETS, and this change proved it. `cargo check --workspace --all-targets` from the camp root (exit 0) and the same in oss/kamaji and oss/yubaba (exit 0) all passed while `yah-local-driver`'s LIB TEST target still failed to compile — oss/yah-base is excluded from the root workspace, and the other two consume it as a path dep whose test targets are never built. The break only surfaced under `cargo test --manifest-path oss/yah-base/Cargo.toml --workspace`. If you touch a workload-spec type, that argv is not optional.")
417//! @yah:gotcha("THE TWO DRIFT GATES ARE RED UNTIL THIS COMMITS, and that is the gate working rather than real drift — do not chase it. `scripts/check-schema-drift.sh` and `scripts/check-workload-spec-ts.sh` both REGENERATE and then `git diff --quiet`, so an uncommitted regen always reports drift. I ran both generators (`cargo run -p xtask -- emit-schemas`, `cargo run --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --bin export-ts`) and the artifacts are current on disk: `.yah/schema/workload.toml.schema.json` gained a `MeshPortRepr` definition rendering the union as `anyOf[integer|string|{name, port?}]` and `MeshExpose.ports` now `$ref`s it; `packages/yah/workload-spec/index.ts` carries `ports: (number | string | { name: string, port?: number })[]`. `git diff --stat` on those two paths is 42 + 15 lines and NOTHING ELSE, so no peer's pending regen got swept in.")
418//! @yah:gotcha("`cargo test -p yah --lib` FAILED ONCE MID-VERIFICATION WITH AN IMPOSSIBLE-LOOKING BUILD ERROR AND IT WAS NOT THIS CHANGE — recorded here because the next person will hit it and CLAUDE.md points them at the wrong tool. Signature: `can't find crate for runner / kg_rust / kg_store / kg_ts / kg / party / agent_tools / camp_service` plus `extern location for {serde,tokio,anyhow,...} does not exist`, killing yah-mcp and yah-eval — crates this change never touches. I followed CLAUDE.md's orphan-gc procedure first and IT EXONERATED orphan-gc: `cargo orphan-gc log -n 300` matched none of the missing hashes and every entry in the hour reads `deleted 0 artifacts`. The real cause is R748-B17 (the camp-service stale sweep splitting a unit's .rmeta from its .rlib in deps/), whose own 2026-08-31 gotcha names SIX of those exact crates and whose fix is in source but not in the long-lived CampService processes doing the deleting. A bare re-run with no clean and no edit passed 1360/0/1. Evidence appended to R748-B17.")
419//! @yah:verify("EVERY NUMBER BELOW WAS RUN BY ME, and the last four on a settled tree after the final edit. workload-spec: `cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --all-features` = 162 lib + 87 integration passed / 0 failed (73 integration before, so +14 new in tests/mesh_ports.rs). yah-base workspace: `cargo test --manifest-path oss/yah-base/Cargo.toml --workspace` = every target ok (37/99/34/38/23/28/146/87/1/1, 0 failed). kamaji: `cargo test --manifest-path oss/kamaji/Cargo.toml --workspace --all-features` = every target ok, kamaji lib 51 passed (45 before, +6 for `declared_port_names`), kamaji-bin lib 278 passed, sibling_wire_e2e and docker_backend_e2e 2 passed each — the two suites R844-F15's postcard bug broke, which is the check that matters for a V7 bump.")
420//! @yah:verify("yubaba: `cargo test --manifest-path oss/yubaba/Cargo.toml -p yah-cloud --lib` = 1011 passed / 0 failed / 4 ignored; `-p yubaba --lib` = 632 passed / 0 failed; `-p yubaba --features testing --test testing -- integration_service_records::` = 11 passed / 0 failed (the suite that asserts a deploy publishes a ready dialable record, i.e. the path `declared_port_names` now feeds). Root: `cargo test -p yah --lib` = 1360 passed / 0 failed / 1 ignored. THE R844 PURITY CANARY, run twice and green both times: `cargo test -p xtask --test main mirror_ingress` = 11 passed / 0 failed — plan_ingress still plans the camp's REAL .yah/services tree with no network, no credentials and no CloudConfig.")
421//! @yah:verify("CARGO EXIT CODES CAPTURED DIRECTLY, not inferred from a grep (an earlier run of mine reported `rc=1` which was ripgrep's no-matches status, i.e. a PASS wearing a failure's clothes — re-run to settle it): `cargo check --manifest-path oss/kamaji/Cargo.toml --workspace --all-features --all-targets` cargo-exit=0, zero `^error` lines; `cargo check --workspace --all-targets` cargo-exit=0, zero `^error` lines. SCOPE HELD: `git diff -- .yah/services/` is EMPTY — this change touches no mirror, and the three apex pins R844-T10 owns are untouched at cloud.toml:105/:250/:276.")
422//!
423//! @yah:ticket(R885-B5, "cpu_millis is documented as a request and rendered as a hard quota — split request from limit")
424//! @yah:status(review)
425//! @yah:assignee(agent:bundle-anthropic-glimmerstone)
426//! @yah:at(2026-09-11T07:27:24Z)
427//! @yah:phase(P2)
428//! @yah:parent(R885)
429//! @yah:next("Tier: Cleric. The judgment is made; this applies it.")
430//! @yah:next("ONE NUMBER, THREE MEANINGS. ResourceLimits::cpu_millis (workload-spec/src/lib.rs:4732) documents itself as a REQUEST — \"an allocatable quantity a bin-packer can subtract from a node budget\". containerd (kamaji-containerd-core/src/lib.rs:1216) and docker (kamaji/src/docker.rs:439) render it as a relative WEIGHT via cpu_shares(). cgroup.rs:146-152 renders it as a hard QUOTA in cpu.max. microvm.rs:563 renders it as a vCPU COUNT via div_ceil(1000).max(1).")
431//! @yah:next("THE BUG THIS CAUSES: a workload declaring 250m as its fair share gets throttled at a quarter core even on a completely idle node. A request rendered as a ceiling is a semantic bug, not a missing feature.")
432//! @yah:next("FIX: cpu_millis stays the request and renders as cpu.weight. An OPTIONAL limit, carried as an annotation (not a field — see the postcard wire rule at kamaji-proto/src/version.rs:63), renders as cpu.max when present and omits it when absent. The containerd/docker weight derivation is already correct and must not change. R572-T2 consciously postponed a separate cpu_limit_millis for exactly this; this is that work, done as an annotation instead of a field.")
433//! @yah:next("memory.high is deliberately NOT in scope. No recorded incident asks for a throttle-before-kill tier; file it when something does.")
434//! @yah:verify("The containerd and docker paths still derive the same cpu_shares value they do today — no change in oss/kamaji/crates/kamaji-containerd-core/src/lib.rs or kamaji/src/docker.rs.")
435//! @yah:depends_on(R885-B1)
436//! @yah:gotcha("YOU ARE NO LONGER ADDITIVE — THE THROTTLE IS LIVE AS OF R885-B1 (2026-09-11). Wiring the cgroup driver onto kamaji::native::NativeRuntime shipped the very semantic bug W344 Finding 5 describes: cpu_millis, documented as a REQUEST, is now rendered as a hard `cpu.max` quota on the live fleet path. Measured on us-east-001: four native workloads at `cpu.max=25600 100000`, i.e. hard-capped at 0.256 of a core even on an idle node, where before the wiring they could burst to whatever the box had. The relay's ordering note calls 2/3/5 'independent and additive behind 1'; for this ticket that is now wrong — every node that takes the R885-B1 binaries is throttled until this lands. The split is unchanged (request -> cpu.weight, optional annotation-carried limit -> cpu.max; the containerd/docker weight derivation is already correct and stays); what changed is the urgency. The write site is CgroupV2::create_workload in oss/kamaji/crates/kamaji/src/cgroup.rs — note that file MOVED out of kamaji-bin in R885-B1.")
437//! @yah:handoff("THE THROTTLE IS OFF. cpu_millis is now rendered as cpu.weight (a relative share, no ceiling on an idle node) and cpu.max is written ONLY when the spec declares a ceiling. Three files: (1) oss/yah-base/crates/workload-spec/src/lib.rs — new CPU_LIMIT_ANNOTATION = \"yah.limits.cpu-millis\" and WorkloadSpec::cpu_limit_millis() -> Option<u32>, the exact mirror of memory_request_mb (that one adds the missing REQUEST beside a field that is a ceiling; this one adds the missing CEILING beside a field that is a request). Absent, unparseable, or 0 all mean NO ceiling — the permissive direction, because capping a workload at zero CPU over a typo is the worse failure. (2) oss/kamaji/crates/kamaji/src/cgroup.rs — new pub struct WorkloadLimits {cpu_request_millis, cpu_limit_millis: Option<u32>, memory_max_mb} with from_spec (live path, reads the annotation) and from_request (callers holding only ResourceLimits, so no ceiling); create_workload takes it instead of &ResourceLimits and writes cpu.weight unconditionally, cpu.max conditionally; new format_cpu_weight beside the existing format_cpu_max. (3) oss/kamaji/crates/kamaji/src/native.rs — the live call site now passes WorkloadLimits::from_spec(spec).")
438//! @yah:handoff("THE WEIGHT MAPPING AND ITS ONE-LINE JUSTIFICATION: weight = clamp(millis * 100 / 1000, 1, 10000), i.e. 1000m -> 100. 100 is the cgroup v2 / systemd CPUWeight= default, so \"one core\" lands on the platform default exactly as the containerd and docker backends' cpu_shares() puts 1000m on 1024, cgroup v1's default — a workload's relative standing is then the same whichever backend runs it, which is the property that made the three renderings disagree in the first place. Clamped because the kernel rejects a cpu.weight outside 1..=10000: a sub-10m request still gets a nonzero share rather than a failed write, and an absurd request saturates instead of erroring. cpu_millis == 0 (\"no declared request\") renders as the default 100, so the write is unconditional and explicit rather than relying on the file's initial value.")
439//! @yah:handoff("THE OPERATOR QUESTION, DECIDED: an already-deployed workload on an upgraded node picks the new rendering up ON REDEPLOY ONLY, and nothing needs to reconcile existing leaves. Startup never rewrites a leaf — the only writer in the whole tree is create_workload (verified: grep for cpu.max/cpu.weight across oss/ crates/ app/ scripts/ returns this one write site plus doc comments). deploy_workload does teardown (rmdir) then mkdir, and a freshly created cgroup starts at the kernel default cpu.max = \"max <period>\", so the stale 25600 100000 cannot survive a redeploy and no reconciler is warranted. THE ONE RESIDUE: a leaf whose rmdir failed EBUSY and is therefore reused keeps its stale cpu.max, because an absent ceiling now skips the write rather than clearing the file. That is exactly R885-B4's race, it is narrow, and the manual remedy is one line on the node (`echo \"max 100000\" > <leaf>/cpu.max`). I chose skip-the-write over write-\"max\" because the ticket's acceptance requires cpu.max be written only when the annotation is present, and because clearing a file nobody set is how you paper over B4 instead of fixing it. NOTE FOR WHOEVER SHIPS THIS: us-east-001 is currently running hotship 0.8.38-h5 bytes carrying the R885-B1 confinement (recorded by @Ashguard:coffee on R881-B7), so its four native workloads are throttled right now and need this tree shipped plus one redeploy each.")
440//! @yah:handoff("DISCOVERED WORK DONE IN THIS PASS, beyond the ticket title. (a) THE TWO GENERATED ARTIFACTS HAD TO BE REGENERATED and the drift test caught it: schemars and ts-rs both emit Rust doc comments into their output, so correcting ResourceLimits::cpu_millis's doc (it said `0` means \"no CPU limit\", which is now wrong twice over — 0 means no declared REQUEST, and the field is not a limit at all) broke yah-workload-spec's ts_drift::committed_ts_bindings_match_current_rust_types. Ran both generators per CLAUDE.md; `git diff --stat` on the two artifacts is 2 lines of .yah/schema/workload.toml.schema.json + 13 of packages/yah/workload-spec/index.ts and NOTHING else, so no peer's pending regen was swept in. (b) The stale pointer in this ticket's own Verify block (oss/kamaji/crates/kamaji-bin/src/cgroup.rs — that file was deleted in R885-B1) is removed and replaced with the real path, along with the kamaji-bin test command that went with it. (c) Two module headers corrected where they now lie: kamaji/src/native.rs:8 said the leaf carries \"memory.max and cpu.max\", and kamaji/src/cgroup.rs's summary said the driver translates ResourceLimits into cpu.max + memory.max. cgroup.rs also gains a \"CPU: a request is not a ceiling\" section recording the us-east-001 measurement, so the next person to reach for cpu.max finds the incident rather than rediscovering it.")
441//! @yah:handoff("WHAT I DELIBERATELY DID NOT DO. memory.high — out of scope by the ticket, and no incident asks for a throttle-before-kill tier. microvm.rs:563's third reading (cpu_millis.div_ceil(1000).max(1) as a vCPU COUNT) is untouched: W344 names it as the third disagreeing interpretation but it is not a throttle, a microVM's vCPU count genuinely is a count, and changing it is a separate judgment on a backend with no live native blast radius. No spec in the tree sets yah.limits.cpu-millis, which is correct and deliberate: the fleet's current state (every native workload burstable, weighted by its request) is the pre-R885-B1 behaviour plus proportional fairness under contention, and a ceiling is now something a workload opts into rather than something it gets by accident.")
442//! @yah:verify("THE ACCEPTANCE GREP, on the file that actually exists: `rg -n \"cpu.weight|cpu.max\" oss/kamaji/crates/kamaji/src/cgroup.rs` — cpu.weight is written unconditionally from the request at create_workload (write_file(&path.join(\"cpu.weight\"), &format_cpu_weight(limits.cpu_request_millis))), and cpu.max sits inside `if let Some(ceiling_millis) = limits.cpu_limit_millis`. There is no other cpu.max/cpu.weight write site anywhere in oss/, crates/, app/ or scripts/.")
443//! @yah:verify("BOTH SHAPES ARE COVERED, and the limit-ABSENT one is asserted twice — at the driver and at the live deploy path. cgroup::tests::a_workload_with_no_declared_ceiling_gets_a_weight_and_no_quota (cpu.weight == \"100\" for 1000m, and cpu.max does NOT EXIST); cgroup::tests::a_declared_ceiling_is_written_to_cpu_max_beside_the_weight (250m request + 2000m ceiling -> weight 25 AND cpu.max \"200000 100000\", two different numbers from two different sources); native::tests::deploying_a_workload_mints_a_cgroup_leaf_carrying_its_limits now drives the REAL deploy_workload and asserts cpu.weight == \"12\" with cpu.max absent — that test previously asserted cpu.max == \"12800 100000\", i.e. it pinned the bug; native::tests::a_declared_cpu_ceiling_renders_as_cpu_max drives the real deploy with the annotation set and gets \"50000 100000\". Plus cpu_weight_translates_a_request_to_a_relative_share (clamping both ends, 0 -> 100) and three workload-spec accessor tests including one asserting that garbage and an explicit 0 both mean no ceiling.")
444//! @yah:verify("THE containerd AND docker DERIVATIONS ARE BYTE-IDENTICAL TO TODAY, verified three ways rather than asserted: `git diff -- oss/kamaji/crates/kamaji/src/docker.rs` is EMPTY (0 lines); kamaji-containerd-core/src/lib.rs shows 10 changed lines and ZERO of them are code — `git diff -U0 | grep \"^[+-]\" | grep -v \"^[+-]//!\"` returns 0, they are a peer's @yah: board annotations for R881-B7; and ResourceLimits::cpu_shares itself is untouched in workload-spec (the only cpu_shares lines in that diff are three doc-comment references being re-worded). So spec.resources.cpu_shares() at containerd lib.rs:1373 and docker.rs:440 still derives millis*1024/1000 exactly as before.")
445//! @yah:verify("EVERY NUMBER BELOW WAS RUN BY ME ON THIS TREE. cargo test -p kamaji --features native-integration --lib: 120 pass / 0 fail (baseline 116, R885-B1's recorded figure; +4 = 3 net new cgroup tests and 1 new native call-site test, all seven named above confirmed present by name in the run). cargo test -p kamaji-bin --features native-exec --lib: 237 pass / 0 fail (baseline 237, unchanged — the off-Linux spawn test's create_workload call was updated to WorkloadLimits::from_request). workload-spec --lib: 192 pass / 0 fail (baseline 189, +3 accessor tests). yah-base workspace, the argv a workload-spec change makes non-optional (its test targets are invisible to every other workspace's --all-targets): `cargo test --manifest-path oss/yah-base/Cargo.toml --workspace` — all targets ok, 0 failed, AFTER the regen; before it, yah-workload-spec --test main was 104 pass / 1 FAIL on the ts-drift gate, now 105 / 0. cargo check --manifest-path oss/kamaji/Cargo.toml --workspace --all-features --all-targets exit 0, no errors. cargo check -p camp-identity (the root-workspace consumer of kamaji-bin) exit 0.")
446//! @yah:verify("CROSS-COMPILE, the Linux-path verification this Mac can actually achieve: `cargo zigbuild -p kamaji-bin --features containerd-integration,native-exec --target x86_64-unknown-linux-gnu` — Finished, exit 0. So the Linux path COMPILES. WHAT I DID NOT ACHIEVE, stated plainly: no live reading. Every cgroup assertion here is against a tempdir, where the control files are ordinary files rather than kernel interfaces — which is why the limit-absent test asserts cpu.max does not EXIST, a check that is only equivalent to \"unlimited\" because a real cgroupfs materialises the file at the kernel default. The live acceptance is still owed: on a node running these bytes, after one redeploy, `cat /sys/fs/cgroup/yubaba.slice/kamaji.service/<ident>/{cpu.weight,cpu.max}` must show the weight set and cpu.max reading `max 100000`.")
447//! @yah:gotcha("THE FIX IS PROVEN IN TESTS AND ON A CROSS-COMPILE, NOT ON HARDWARE — and this relay has already been burned by exactly that gap (R885-B1's own first gotcha: R406-T4/T5 sat in review for two months with passing tests and no reachable call site). What is different here is that the call site IS reachable and IS asserted by a test that drives the real deploy_workload. What is still missing is a node. us-east-001 currently runs hotship 0.8.38-h5 bytes with the R885-B1 confinement and NO B5 fix, so its four native workloads are throttled at this moment; shipping this tree plus one redeploy per workload is what closes the incident, and the reading that proves it is cpu.weight set with cpu.max at `max 100000`.")
448//! @yah:cleanup("An absent ceiling SKIPS the cpu.max write rather than clearing the file, so a leaf reused after a failed teardown (R885-B4's EBUSY race) keeps a stale quota. Narrow, and deliberate — the alternative papers over B4 — but if B4 lands a cgroup.kill + ordered teardown, re-read this decision: with a reliable rmdir the residue becomes impossible, and with an unreliable one it might be worth writing \"max <period>\" explicitly.")
449//!
450//! @yah:ticket(R885-T6, "Delete ephemeral_storage_mb from ResourceLimits and give the microVM its own named scratch floor")
451//! @yah:status(review)
452//! @yah:assignee(agent:bundle-anthropic-glimmerstone)
453//! @yah:at(2026-09-11T08:49:25Z)
454//! @yah:phase(P3)
455//! @yah:parent(R885)
456//! @yah:next("Tier: Cleric either way; the operator call is what makes it blocked, not the difficulty.")
457//! @yah:next("THE FIELD LIES TODAY. ResourceLimits::ephemeral_storage_mb (workload-spec/src/lib.rs:4735) documents itself as a cap on the writable layer plus tmpfs footprint. NO backend enforces it: the OCI resources block (kamaji-containerd-core/src/lib.rs:1213-1218) carries only memory and cpu; docker argv carries only --memory and --cpu-shares; cgroup.rs deliberately omits it (not a cgroup v2 control). Its ONE live consumer, microvm.rs:693, uses it as a FLOOR on the scratch disk — the opposite of a cap. WorkloadSpec::for_forge sets 512 MiB, which as a cap would fail every build at first checkout.")
458//! @yah:next("EITHER ANSWER IS A SCHEMA CHANGE and needs the CLAUDE.md regen dance: cargo run -p xtask -- emit-schemas, then cargo run --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --bin export-ts. Gated by schema-drift-guard and workload-spec-drift-guard in .yah/qed/check.toml.")
459//! @yah:verify("rg -rn \"ephemeral_storage_mb\" --type rust — every remaining site agrees with whichever answer was chosen; no site reads it as a cap while another reads it as a floor.")
460//! @yah:verify("scripts/check-schema-drift.sh and scripts/check-workload-spec-ts.sh both clean.")
461//! @yah:gotcha("Deleting the field is a postcard wire change and therefore a ProtocolVersion bump (kamaji-proto/src/version.rs, currently V8) plus a sweep of WorkloadSpec struct literals across four cargo workspaces and two excluded manifests — R860-T1 took 35 call sites and three misses. Blast radius is bounded (node-local UDS, yubaba and kamaji self-install as a pair, so skew is a restart not a rolling upgrade) but it is not free. If R885-F3 also turns out to need a bump, do both in one.")
462//! @yah:next("DECIDED 2026-09-10 (operator): DELETE. Remove ephemeral_storage_mb from ResourceLimits entirely and give the microVM its own explicitly-named scratch-disk floor. Rationale is CLAUDE.md pre-1.0 doctrine — change the design rather than tape it. Do NOT keep the field aliased, defaulted, or read-both-and-prefer-whichever; one shape has to win and it is the one without the field.")
463//! @yah:next("THE MICROVM REPLACEMENT IS THE REAL WORK, not the deletion. microvm.rs:693 currently reads ephemeral_storage_mb as a scratch-disk FLOOR (disk_size_bytes at :1071), and WorkloadSpec::for_forge sets 512 MiB expecting exactly that. Give it a named annotation of its own so the floor is legible as a floor. Do not silently drop the behaviour along with the field — a build that fails at first checkout is the failure mode.")
464//! @yah:gotcha("THE WIRE BUMP IS NOW CERTAIN, not conditional — deleting a ResourceLimits field is a postcard wire change and therefore a ProtocolVersion bump (kamaji-proto/src/version.rs, V8 today). Coordinate with R885-F3 BEFORE bumping: if F3 also needs a new WorkloadState shape, both changes ride one V9 rather than a V9 and a V10. Whichever ticket moves first should say so in its handoff.")
465//! @yah:next("R885-F3 LANDED NODE-LOCAL ONLY AND OWES YOU A WIRE DELTA (2026-09-11). F3 classifies OOM vs plain SIGKILL inside kamaji and names it in the journal + in WorkloadStatus::Failed{reason}, but `reason` dies at the UDS boundary: kamaji-bin/src/server.rs:3866 and :3924 both flatten `WorkloadStatus::Failed { .. } => WireState::Failed`, and kamaji-proto's WorkloadState (messages.rs:158) is a FIELDLESS enum with nowhere to put a string. So yubaba still cannot tell an OOM from a crash. CARRY THIS IN YOUR V9: add `OomKilled` as a new variant of kamaji_proto::WorkloadState (it is already #[non_exhaustive]; postcard encodes the discriminant as a varint, so appending at the END keeps Pending..Failed on 0..5 and only an unbumped peer decoding discriminant 6 breaks - which is exactly what the bump exists to refuse). Then map it at BOTH server.rs sites from a classification kamaji already computes: crate::cgroup::ExitClass::is_oom() is the predicate, and NativeProcess::settle already has the value. The only extra plumbing needed is carrying ExitClass (or a bool) out of Completion, which is crate-internal and not a wire type.")
466//! @yah:gotcha("THE BATCH IS NOW CONFIRMED, NOT CONDITIONAL: R885-F3 is at review having deliberately NOT bumped ProtocolVersion (operator/leader call - land everything below the wire, leave the upward report to T6's certain V9). F3's wire delta is spelled out in this ticket's @yah:next above. Do both in the one V9: deleting ephemeral_storage_mb from ResourceLimits AND adding WorkloadState::OomKilled. F3 touched only oss/kamaji/crates/kamaji/src/{cgroup.rs,native.rs} and did NOT touch workload-spec/src/lib.rs, so it leaves no conflict with your ResourceLimits sweep.")
467//! @yah:next("TIER RE-STAMPED Cleric -> Warrior by the relay leader, 2026-09-11, with the reason recorded because a silent tier bump is exactly what R889-B1 is about. The original `Tier: Cleric` stamp was written when this ticket was 'delete a field + give the microVM a named floor'. Since then R885-F3 landed node-local-only and pushed its entire wire delta onto this ticket: a new `kamaji_proto::WorkloadState::OomKilled` variant, mapped at BOTH server.rs:3866 and :3924, plus carrying `ExitClass` out of `Completion`. Combined with the ProtocolVersion V8->V9 bump, the ~35-call-site WorkloadSpec sweep across four cargo workspaces and two excluded manifests (R860-T1's measured cost, with three misses), and the schema + TS regen, this is now the `class_tiers` Warrior descriptor verbatim - 'tricky implementation with a clear spec, heavy integration'. GENERAL LESSON WORTH CARRYING: a Tier stamp is written at filing time and can be invalidated by a SIBLING ticket pushing scope onto it. Whoever re-reads a stamp before dispatching should check whether the ticket still describes the work it was stamped for.")
468//! @yah:handoff("THE BUMP IS V11, NOT V9 — THE TICKET'S \"V8 TODAY\" WAS TWO BUMPS STALE. kamaji-proto/src/version.rs read ProtocolVersion::CURRENT = V10 on pickup, not V8: R605-T27 landed V9 (microvm: MicroVmHealth on NodeCapabilities) and R850-T4 landed V10 (DeployAck replaces AckKind::Deploy) after this ticket was filed. So both of this ticket's changes ride ONE bump to V11, which is the batching the ticket asked for; only the number moved. version.rs's V11 stanza spells out both halves and why each alone would earn a bump — (a) DELETING a field from a struct on a positional postcard wire shifts every byte after the hole, which is V2/V4/V5/V6/V8 run backwards and no safer for being subtraction; (b) appending WorkloadState::OomKilled is the benign kind, free once a bump is being spent. Tree anchor at dispatch: 494e22fa14b21d0cc72dd8a3132e7f078d1eb5eb.")
469//! @yah:handoff("PIECE 1+2 — THE FIELD IS GONE AND THE FLOOR IS NAMED `floor`. ResourceLimits::ephemeral_storage_mb deleted; successor is annotation `yah.limits.scratch-floor-mb` (SCRATCH_FLOOR_ANNOTATION) read by WorkloadSpec::scratch_floor_mb() -> Option<u32>, following R885-B5's cpu_limit_millis / R885-T2's pids_limit precedent verbatim (annotation not field, because a field costs exactly the wire bump this ticket is paying). microvm::workspace::disk_size_bytes(input_bytes, Option<u32>) and build_disk take the floor; deploy passes spec.scratch_floor_mb(). for_forge declares FORGE_SCRATCH_FLOOR_MB = 512 via the annotation so the old `ephemeral_storage_mb: 512` declaration is RENAMED, not dropped. Naming call I made: kept it in the `yah.limits.*` family beside the two ceilings, with the word `floor` in the key carrying the opposite direction — documented at the const.")
470//! @yah:handoff("MEASURED, AND IT RETIRES THE TICKET'S OWN WORRY: THE `requested` TERM WAS ALREADY DEAD CODE TREE-WIDE. microvm's WORKSPACE_MIN_BYTES floor is 8 GiB and disk_size_bytes is a `max`, while the LARGEST ephemeral_storage_mb anywhere in the tree was 1024 MiB (histogram over every rust literal: 0, 32, 64, 128, 256, 512, 1024 — nothing above). So the spec-supplied term never won for any workload that exists, and for_forge's 512 MiB in particular has always resolved to 8 GiB. Deleting the field is therefore behaviour-preserving by measurement, not by argument. The mechanism is still kept because it is load-bearing ABOVE 8 GiB, which is the only reason to have it at all — pinned by an assertion at 32 GiB.")
471//! @yah:handoff("PIECE 3 — R885-F3'S WIRE DELTA IS CARRIED, PLUS ONE BUG IT WOULD HAVE INTRODUCED. kamaji_proto::WorkloadState::OomKilled appended LAST (messages.rs). Plumbing is a new `oom_killed: bool` on kamaji's own WorkloadStatus::Failed (crate-internal, not a wire type, as F3 specified): native.rs settle() sets it from class.is_oom() — the ExitClass F3 already computes — and every other backend passes false, documented as \"not known to be an OOM\", never \"known not to be one\". Mapped at BOTH server.rs sites (runtime_state_to_entry, docker_workload_to_entry). sibling.rs carries the RETURN leg so the bit survives the round trip instead of being flattened one hop after it crossed. DISCOVERED AND FIXED IN THIS PASS, not filed: server.rs liveness_rank ends in `_ => 2` for states a newer peer might send, so OomKilled would have fallen into it and ranked 2 — ABOVE Failed(1) and level with Pending — letting an OOM-killed row win the List dedupe against a more informative row for the same id. Explicit `WireState::OomKilled => 1` arm plus a test. The catch-all is right for a state this build has never heard of and wrong for one it ships.")
472//! @yah:handoff("THE SWEEP: 75 rg hits across FIVE workspaces (root, oss/kamaji, oss/yah-base, oss/yubaba, oss/qed), all five now compiling with --all-features --all-targets. 38 files had standalone struct-literal lines; the non-mechanical ones were velveteen-exec/src/remote.rs:1101 (the buildkit image-build step set 4096 — a REAL second consumer the ticket did not name, now the annotation), workload-spec/src/validate.rs (capacity error text), and 19 JSON fixtures + app/yah/xlb-node/workload.json (field was the LAST key, so the preceding comma had to go too — all 20 re-validated as parseable JSON). A METHOD NOTE FOR WHOEVER RE-RUNS THE ACCEPTANCE GREP: this ticket's own verify line says `rg -rn \"ephemeral_storage_mb\"`, and `-r` is ripgrep's REPLACE flag — `-rn` parses as `-r n`, so every hit renders as the literal `n` and the output is unreadable. Use `rg -n`. That cost a pass to notice.")
473//! @yah:gotcha("STALE SECOND COPY OF A GENERATED FILE, PRE-EXISTING AND NOT TOUCHED BY THIS TICKET: oss/packages/yah/workload-spec/index.ts is 22040 bytes against the live packages/yah/workload-spec/index.ts at 39216, and it still carries `ephemeral_storage_mb: number`. `export-ts` writes ONLY the root packages/ path, and neither check-schema-drift.sh nor check-workload-spec-ts.sh reads the oss/ copy — both gates pass green with it stale. It was ~17KB behind before this ticket, so hand-patching the one field would make a file that is wrong in dozens of ways LOOK current, which is worse than leaving it. Deliberately left; needs either a generator that targets it or deletion, as its own ticket.")
474//! @yah:cleanup("DOCKER BACKEND CANNOT ANSWER `oom_killed` AND THE DATA IS RIGHT THERE. docker.rs status mapping hardcodes oom_killed: false because DockerState (docker.rs:87) does not parse the `OOMKilled` bool Docker's own /containers/{id}/json State object returns. Contained follow-on: add the field to DockerState, map it at the three WorkloadStatus::Failed arms. Left out deliberately — it is a different backend's classification and this ticket's piece 3 is the native path. Comment at the site names it so the `false` reads as a gap rather than a fact.")
475//! @yah:handoff("ALL FOUR ACCEPTANCE CRITERIA MET. (1) `rg -n \"ephemeral_storage_mb\" --type rust` returns ZERO code reads across all five workspaces — only board prose and deliberate historical references inside doc comments explaining what was deleted. (2) The for_forge scratch floor is proven by test, not inspection: deleting_the_field_did_not_change_what_for_forge_is_given drives the REAL WorkloadSpec::for_forge through the real accessor and asserts disk_size_bytes is identical for an empty tree and a 3 GiB one, that None and a declared-below-8-GiB floor agree, and that a 32 GiB floor still wins. (3) `rg -n \"OomKilled\" oss/kamaji` shows the variant, both server.rs mappings, and both directions tested — an_oom_kill_reaches_the_wire_distinct_from_a_plain_failure pins OOM -> OomKilled AND plain SIGKILL -> Failed. (4) ProtocolVersion is V11 exactly once with both changes in it. Schema + TS regen ran; check-schema-drift.sh and check-workload-spec-ts.sh both report `ok: in sync`.")
476//! @yah:verify("EVERY NUMBER RUN BY ME ON THIS TREE (anchor 494e22fa14b21d0cc72dd8a3132e7f078d1eb5eb), against the baselines the dispatch named. `cargo test -p kamaji --features native-integration --lib`: 153 pass / 0 fail, baseline 153 after B4 — UNCHANGED, because my microVM test needs the microvm feature. With `--features native-integration,microvm-integration --lib`: 195 pass / 0 fail, including the new microvm::tests::deleting_the_field_did_not_change_what_for_forge_is_given and all seven of F3's settle tests untouched. `cargo test -p kamaji-bin --features native-exec --lib`: 239 pass / 0 fail, baseline 237, +2 both mine. `cargo test -p yah-workload-spec --lib`: 196 pass / 0 fail, baseline 196 unchanged; `--tests` adds 105 pass / 0 fail over the 20 edited JSON fixtures. `cargo test -p kamaji-proto`: 34 pass / 0 fail, baseline 33, +1 mine. `cargo check --workspace --all-features --all-targets` exit 0 on oss/kamaji, oss/yah-base, oss/yubaba; `-p velveteen-exec --all-features --all-targets` exit 0 on oss/qed; `cargo check --workspace --all-targets` exit 0 on the root. `cargo clippy -p kamaji --features native-integration --all-targets`: 1 warning, PRE-EXISTING (jit.rs:312 too-many-arguments — the same one B1, F3 and B4 each recorded), none from this change.")
477//! @yah:verify("NOT ACHIEVED, STATED PLAINLY: NO LIVE LINUX NODE READING, and no live UDS exercised between a real yubaba and a real kamaji at V11. This is a macOS dev machine. The Linux-path verification achieved is the cross-compile — `cargo zigbuild -p kamaji-bin --features containerd-integration,native-exec --target x86_64-unknown-linux-gnu`, Finished, exit 0 — plus a byte-level pin of the wire claim in place of a real peer: appending_oom_killed_left_every_existing_discriminant_alone asserts each WorkloadState encodes to its literal postcard byte (Pending..Failed = 0..5 unmoved, OomKilled = 6), against the actual encoder rather than an `as usize` cast, because the cast reads the Rust discriminant and the claim is about the wire. Same macOS limitation B1, B5, T2, F3 and B4 each recorded. STILL OWED ON A NODE: deploy a workload past its memory.max and confirm yubaba now receives OomKilled rather than Failed — the end-to-end that R590-B10's scar is really about; and confirm a yubaba/kamaji pair restarted together negotiate V11 (skew here is a restart, not a rolling upgrade, since the two self-install as a pair).")
478//! @yah:gotcha("T6'S ROLL IS A THREE-NODE PROTOCOL BUMP, NOT A ONE-NODE ONE — and it did NOT ride R885's 2026-09-11 hot ship. That ship carried B4/B9/B10/B11 to us-east-001 only; T6 is still unshipped everywhere. From @Ashguard:coffee (R876), who dated the fleet independently: the live fleet is running PRE-T6 kamaji, established by `grep -c -a` against the deployed binaries — `ephemeral_storage_mb` PRESENT and `scratch-floor-mb` ABSENT. That string-dating trick is worth keeping: it dates a deployed binary by its own contents when `--version` cannot be trusted (hotship.sh:880 warns that `--version` is a build-time string that has agreed with a release the binary did not contain, R746-T3 — confirmed again on 2026-09-11, when us-east-001's yubaba printed 0.8.37 while /health correctly reported the hot-shipped 0.8.38-h5; `/health` is the authority, `--version` is not). WHY THIS MATTERS FOR T6 SPECIFICALLY: deleting ephemeral_storage_mb from ResourceLimits moves the postcard wire, so every node must cross together — unlike R885's capability and cgroup work, which degraded gracefully on a single node. Whoever rolls T6 is planning a fleet-wide coordinated roll of us-west-001, us-east-001 and us-south-001, not a scoped hot ship, and should read R885-B9's hotship gotcha first: hotship ships BINARIES ONLY and writes no unit file, which for T6 is fine but for the CAP_SETPCAP half of B9 is the difference between in-force and inert.")
479
480use std::collections::BTreeMap;
481use std::collections::HashMap;
482use std::fmt;
483use std::path::PathBuf;
484
485use serde::{Deserialize, Serialize};
486use ts_rs::TS;
487
488pub mod admission;
489pub mod compose_import;
490pub mod control_plane_install;
491pub mod export_ts;
492pub mod rollout;
493pub mod secrets;
494pub mod sovereign;
495pub mod validate;
496
497
498// ── Duration ──────────────────────────────────────────────────────────────────
499
500/// Duration expressed as an integer millisecond count.
501///
502/// Used for healthcheck intervals, timeouts, delays, and stop grace periods.
503/// Chosen over `std::time::Duration` to keep serde support dependency-free.
504#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
505#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
506#[ts(type = "number")]
507pub struct Millis(pub u64);
508
509impl Millis {
510 pub fn from_secs(s: u64) -> Self {
511 Self(s * 1000)
512 }
513
514 pub fn from_ms(ms: u64) -> Self {
515 Self(ms)
516 }
517
518 pub fn as_ms(self) -> u64 {
519 self.0
520 }
521
522 pub fn as_secs_f64(self) -> f64 {
523 self.0 as f64 / 1000.0
524 }
525}
526
527// ── Primitive newtypes ────────────────────────────────────────────────────────
528
529/// Opaque identifier for a yubaba-managed machine within the cluster.
530///
531/// Used by the semantic validation layer for admission-control capacity checks.
532/// Yubaba passes its own machine ID when validating a spec before deployment.
533#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TS)]
534#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
535pub struct MachineId(pub String);
536
537/// DNS-segment identity for a workload on the cluster mesh, e.g.
538/// `"noisetable-api.pdx"`. Regex constraint: `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`,
539/// length ≤ 63. Enforced in shape validation (R090-F2).
540#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
541#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
542pub struct MeshIdent(pub String);
543
544/// Tier classification that governs admission control and mesh `allow_from`
545/// filtering. Known values: `"public"`, `"tenant"`, `"private"`, `"infra"`.
546/// Custom tiers are allowed per cluster; shape validation warns on unknowns
547/// rather than rejecting them (R090-F2).
548#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
549#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
550pub struct TierTag(pub String);
551
552/// Default single-tenant identity written to specs that predate the tenant
553/// axis (W206). Its concrete string is arbitrary — what matters is that a
554/// single-tenant cluster only ever sees this one value, so every per-tenant
555/// isolation primitive collapses to a no-op. See [`TenantId::singleton`].
556pub const DEFAULT_TENANT: &str = "default";
557
558/// Default single-namespace identity for specs that predate the namespace
559/// axis (W206). See [`NamespaceId::singleton`].
560pub const DEFAULT_NAMESPACE: &str = "default";
561
562/// Tenant **isolation** axis (W206). Separates one operator's workloads from
563/// another's at the network / DB / mesh-identity level. Orthogonal to
564/// [`NamespaceId`] (routing/naming) and [`TierTag`] (workload class within a
565/// `(tenant, namespace)` pair).
566///
567/// **Degenerate case:** when a yubaba reconciler sees only one `TenantId`
568/// across every workload on a machine, the tenant prefix on mesh identity is
569/// dropped and PostgreSQL role separation is skipped — isolation primitives
570/// become no-ops. You pay only when more than one tenant is present. Container
571/// networking is keyed on the id, not the count: [`TenantId::singleton`]
572/// workloads share the node bridge, and every other tenant gets its own (W343
573/// §Tenant isolation). Specs written before this axis existed deserialize to
574/// [`TenantId::singleton`].
575#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, TS)]
576#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
577pub struct TenantId(pub String);
578
579impl TenantId {
580 /// The singleton tenant used for back-compat with single-tenant (current)
581 /// deployments. Specs written before the tenant axis existed deserialize
582 /// to this value via the `#[serde(default)]` on [`WorkloadSpec::tenant`],
583 /// keeping the whole cluster single-tenant so every isolation primitive
584 /// stays a no-op.
585 pub fn singleton() -> Self {
586 Self(DEFAULT_TENANT.to_string())
587 }
588
589 /// Whether this is the singleton (degenerate single-tenant) identity.
590 pub fn is_singleton(&self) -> bool {
591 self.0 == DEFAULT_TENANT
592 }
593}
594
595/// Namespace **routing/naming** axis (W206). A pure naming key that never
596/// affects isolation: it selects the config root, disambiguates service DNS
597/// names within a tenant, prefixes object-store bucket names within a tenant's
598/// bucket scope, and selects the provider zone (e.g. `noisetable.com` vs
599/// `yah.dev`). Two namespaces in the same tenant share networks, mesh-identity
600/// space, and PG cluster — they simply cannot collide on workload names or
601/// external domains. Specs written before this axis existed deserialize to
602/// [`NamespaceId::singleton`].
603#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, TS)]
604#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
605pub struct NamespaceId(pub String);
606
607impl NamespaceId {
608 /// The singleton namespace used for back-compat with single-namespace
609 /// (current) deployments. Specs written before the namespace axis existed
610 /// deserialize to this value via the `#[serde(default)]` on
611 /// [`WorkloadSpec::namespace`].
612 pub fn singleton() -> Self {
613 Self(DEFAULT_NAMESPACE.to_string())
614 }
615
616 /// Whether this is the singleton (degenerate single-namespace) identity.
617 pub fn is_singleton(&self) -> bool {
618 self.0 == DEFAULT_NAMESPACE
619 }
620}
621
622// ── Workload (on-disk envelope) ──────────────────────────────────────────────
623
624/// On-disk `workload.toml` manifest. Each variant matches one
625/// `ServiceComponent.kind` value; the `kind` field on the wire is the serde
626/// discriminator.
627///
628/// This is the **on-disk** envelope — distinct from [`WorkloadSpec`], the
629/// containerd wire format yubaba receives over RPC. A `kind = "container"`
630/// workload deserializes its remaining fields as a [`ContainerManifest`],
631/// which is *either* a digest-pinned `WorkloadSpec` or a local Dockerfile
632/// recipe (R783-F1 / W324); other kinds carry their own per-reconciler
633/// payload shape.
634///
635/// **Never put `#[serde(skip_serializing_if = "Option::is_none")]` on a field
636/// of this enum or any type it reaches.** These types ride the kamaji-proto
637/// **postcard** wire, which is non-self-describing and positional:
638/// `skip_serializing_if` omits the field's byte on serialize while decode still
639/// expects to read it at that offset, so the byte stream misaligns and the
640/// round-trip fails. Use `#[serde(default)]` + `#[ts(optional = nullable)]`
641/// instead — that still gives TOML/JSON back-compat (missing field → `None`)
642/// while the field is always encoded. `MesofactStaticWorkload::ssr_runtime` and
643/// `::serve_bundle` are the reference shape.
644/// **Two wire shapes, one type (R546-B7).** `Serialize`/`Deserialize` are
645/// hand-written and branch on [`is_human_readable`](serde::Deserializer::is_human_readable):
646///
647/// - **TOML/JSON (human-readable)** → *internally* tagged on `kind`, i.e. the
648/// flat shape every on-disk `workload.toml` actually uses
649/// (`kind = "static-asset"` beside `[[asset]]` and `[aliases]`).
650/// - **postcard (binary)** → *externally* tagged, byte-identical to the derived
651/// representation R590-B3 established for the kamaji UDS.
652///
653/// Why not just `#[serde(tag = "kind")]`: internal tagging buffers through
654/// `deserialize_any`, which postcard (non-self-describing) refuses with
655/// `WontImplement` — that is exactly the failure R590-B3 fixed by flipping this
656/// enum to external tagging. But external tagging wants a single-key map, so
657/// every flat on-disk file then failed with `wanted exactly 1 element, more
658/// than 1 element` and `yah cloud apply` broke for every static-asset
659/// component. Branching on the format satisfies both, and mirrors what
660/// [`ImageRef`] already does for its string-vs-struct form.
661#[derive(Debug, Clone, PartialEq, TS)]
662#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
663#[cfg_attr(
664 feature = "json-schema",
665 schemars(tag = "kind", rename_all = "kebab-case")
666)]
667#[ts(tag = "kind", rename_all = "kebab-case")]
668pub enum Workload {
669 /// Static-site build that publishes an artifact directory to the
670 /// service's `static` provider slot. Reconciled by the
671 /// `mesofact-static` reconciler — does not deploy to yubaba.
672 MesofactStatic(MesofactStaticWorkload),
673
674 /// A container-shaped workload. **Two on-disk forms** (R783-F1 / W324),
675 /// see [`ContainerManifest`]: a digest-pinned [`WorkloadSpec`] reference
676 /// (the form that crosses the kamaji wire) or a local Dockerfile
677 /// [`ContainerBuild`] recipe (which cannot, because it names no digest
678 /// until it has been built).
679 ///
680 /// Construct the wire form with [`Workload::container`] and read it back
681 /// with [`Workload::container_spec`] — most callers only ever mean the
682 /// reference form and should not have to name the manifest enum.
683 ///
684 /// The reference form's inline fields are the full [`WorkloadSpec`] minus
685 /// the `kind` discriminator.
686 ///
687 /// This is also the shape of the W267 sovereign-public-ingress appliance
688 /// (R594-F2): a container-kind workload with `archetype =
689 /// Some(LifecycleArchetype::Appliance)` and
690 /// `requires_taint() == Some(PUBLIC_IP_TAINT)`, **not** a dedicated
691 /// `Workload::ingress(..)` variant. It runs an ordinary OCI image (the
692 /// `passway` proxy, R594-F4) supervised by kamaji exactly like any other
693 /// `Container`, so no admission-list or wire-codec change was needed to
694 /// let kamaji accept it. A new enum variant would have forced an
695 /// exhaustive-match update in every `Workload` consumer, including
696 /// peer-owned `kamaji-proto/src/codec.rs` — the archetype + annotation
697 /// combination expresses "this is the public ingress appliance" without
698 /// that blast radius. See [`WorkloadSpec::requires_taint`] and
699 /// [`LifecycleArchetype::Appliance`].
700 Container(ContainerManifest),
701
702 /// Data-pipeline job with declared I/O and a readiness policy. The
703 /// orchestrator checks all `inputs` are reachable before each run and
704 /// verifies `outputs` afterward. Generalises the OpenRouter JSON-cache
705 /// refresher (`spawn_almanac_refresher`) to the full manifest form.
706 Almanac(AlmanacManifest),
707
708 /// Content-addressed static files uploaded to the mirror's `object_store`
709 /// provider slot. Wave-0 by default — gating mesofact and container waves.
710 /// Rollback is a pointer-flip via `mirror.toml [asset_aliases]`; bytes are
711 /// append-only and never re-pushed on rollback. See W160.
712 StaticAsset(StaticAssetWorkload),
713
714 /// One cold, per-tenant passway serving a single custom domain, forked on
715 /// demand by kamaji's JIT tier (R852-F1 / W267 §"Free-tier ingress at 10k
716 /// domains"). Unlike the `Container`-shaped **node** ingress appliance
717 /// above, this one is native-forked and zero-resident — see
718 /// [`TenantPasswayWorkload`] for why that difference is what made it a
719 /// variant rather than another annotated container.
720 ///
721 /// **Appended last, deliberately.** postcard encodes an external tag as the
722 /// variant *index*, so a variant inserted anywhere but the end renumbers
723 /// every later one and a pre-R852 node silently decodes the wrong shape off
724 /// the kamaji UDS.
725 TenantPassway(TenantPasswayWorkload),
726}
727
728impl Workload {
729 /// The `kind` discriminator this variant serializes as — the same string a
730 /// `workload.toml` writes and a `ServiceComponent.kind` names.
731 ///
732 /// Lives here rather than at a call site because this enum now has FIVE
733 /// places that enumerate its variants (itself plus the four tagging
734 /// mirrors below); a caller-local match would be a sixth, in another crate,
735 /// with nothing to force it to keep up.
736 pub fn kind_str(&self) -> &'static str {
737 match self {
738 Workload::MesofactStatic(_) => "mesofact-static",
739 Workload::Container(_) => "container",
740 Workload::Almanac(_) => "almanac",
741 Workload::StaticAsset(_) => "static-asset",
742 Workload::TenantPassway(_) => "tenant-passway",
743 }
744 }
745
746 /// The per-tenant passway declaration, if this is one.
747 pub fn tenant_passway(&self) -> Option<&TenantPasswayWorkload> {
748 match self {
749 Workload::TenantPassway(w) => Some(w),
750 _ => None,
751 }
752 }
753
754 /// Wrap a digest-pinned [`WorkloadSpec`] as a `kind = "container"`
755 /// workload — the form that crosses the kamaji wire.
756 ///
757 /// Every caller that synthesizes a container workload in code (ingress
758 /// appliances, forge runs, kamaji's own deploy path) means *this* form;
759 /// the [`ContainerManifest::Recipe`] arm only ever arrives by parsing a
760 /// `workload.toml` with a `[build]` table. Keeping the constructor here
761 /// means R783-F1 did not have to teach ~25 call sites the name of a
762 /// manifest enum they have no opinion about.
763 pub fn container(spec: WorkloadSpec) -> Self {
764 Workload::Container(ContainerManifest::Reference(spec))
765 }
766
767 /// The digest-pinned spec of a `kind = "container"` workload, if this is
768 /// a container workload in the reference form.
769 ///
770 /// `None` covers both "not a container" and "a container *recipe*, which
771 /// has no spec until it is built" — a consumer that speaks the wire
772 /// (kamaji, yubaba's deploy path) must treat both as inadmissible, so
773 /// collapsing them into one `None` is deliberate rather than lossy. Use
774 /// [`Workload::container_manifest`] when the two need distinguishing.
775 pub fn container_spec(&self) -> Option<&WorkloadSpec> {
776 match self {
777 Workload::Container(m) => m.as_spec(),
778 _ => None,
779 }
780 }
781
782 /// The container manifest, in whichever on-disk form it was written.
783 pub fn container_manifest(&self) -> Option<&ContainerManifest> {
784 match self {
785 Workload::Container(m) => Some(m),
786 _ => None,
787 }
788 }
789}
790
791/// Internally-tagged mirror of [`Workload`] — the on-disk shape. Only ever
792/// reached on the human-readable branch, so its `deserialize_any` buffering is
793/// never asked of postcard.
794#[derive(Serialize, Deserialize)]
795#[serde(tag = "kind", rename_all = "kebab-case")]
796enum WorkloadTagged {
797 MesofactStatic(MesofactStaticWorkload),
798 Container(ContainerManifest),
799 Almanac(AlmanacManifest),
800 StaticAsset(StaticAssetWorkload),
801 TenantPassway(TenantPasswayWorkload),
802}
803
804/// Borrowing twin of [`WorkloadTagged`] so `Serialize` need not clone the
805/// payload. Variant order must match [`Workload`].
806#[derive(Serialize)]
807#[serde(tag = "kind", rename_all = "kebab-case")]
808enum WorkloadTaggedRef<'a> {
809 MesofactStatic(&'a MesofactStaticWorkload),
810 Container(&'a ContainerManifest),
811 Almanac(&'a AlmanacManifest),
812 StaticAsset(&'a StaticAssetWorkload),
813 TenantPassway(&'a TenantPasswayWorkload),
814}
815
816/// Externally-tagged mirror — the postcard wire shape R590-B3 established.
817/// postcard encodes an external tag as the *variant index*, so the variant
818/// ORDER here is load-bearing: it must match [`Workload`] exactly or the
819/// kamaji UDS silently decodes into the wrong variant.
820///
821/// `Container` deliberately keeps [`WorkloadSpec`], **not**
822/// [`ContainerManifest`] (R783-F1 / W324): the wire carries only the
823/// digest-pinned reference form, so these bytes are unchanged by the on-disk
824/// split, and a [`ContainerManifest::Recipe`] is refused at serialize rather
825/// than encoded as a second variant nothing on the far side can execute.
826#[derive(Serialize, Deserialize)]
827#[serde(rename_all = "kebab-case")]
828enum WorkloadExternal {
829 MesofactStatic(MesofactStaticWorkload),
830 Container(WorkloadSpec),
831 Almanac(AlmanacManifest),
832 StaticAsset(StaticAssetWorkload),
833 TenantPassway(TenantPasswayWorkload),
834}
835
836/// Borrowing twin of [`WorkloadExternal`]. Same order requirement.
837#[derive(Serialize)]
838#[serde(rename_all = "kebab-case")]
839enum WorkloadExternalRef<'a> {
840 MesofactStatic(&'a MesofactStaticWorkload),
841 Container(&'a WorkloadSpec),
842 Almanac(&'a AlmanacManifest),
843 StaticAsset(&'a StaticAssetWorkload),
844 TenantPassway(&'a TenantPasswayWorkload),
845}
846
847impl Serialize for Workload {
848 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
849 where
850 S: serde::Serializer,
851 {
852 if s.is_human_readable() {
853 match self {
854 Workload::MesofactStatic(w) => WorkloadTaggedRef::MesofactStatic(w),
855 Workload::Container(w) => WorkloadTaggedRef::Container(w),
856 Workload::Almanac(w) => WorkloadTaggedRef::Almanac(w),
857 Workload::StaticAsset(w) => WorkloadTaggedRef::StaticAsset(w),
858 Workload::TenantPassway(w) => WorkloadTaggedRef::TenantPassway(w),
859 }
860 .serialize(s)
861 } else {
862 match self {
863 Workload::MesofactStatic(w) => WorkloadExternalRef::MesofactStatic(w),
864 // The wire gate (W324 §5). A recipe names no digest, so there
865 // is nothing for kamaji to pull — refusing here makes "a build
866 // recipe cannot reach kamaji" a fact the type system holds,
867 // rather than a convention someone eventually forgets.
868 Workload::Container(ContainerManifest::Recipe(_)) => {
869 return Err(serde::ser::Error::custom(RECIPE_IS_NOT_A_WIRE_SPEC))
870 }
871 Workload::Container(ContainerManifest::Reference(spec)) => {
872 WorkloadExternalRef::Container(spec)
873 }
874 Workload::Almanac(w) => WorkloadExternalRef::Almanac(w),
875 Workload::StaticAsset(w) => WorkloadExternalRef::StaticAsset(w),
876 Workload::TenantPassway(w) => WorkloadExternalRef::TenantPassway(w),
877 }
878 .serialize(s)
879 }
880 }
881}
882
883impl<'de> Deserialize<'de> for Workload {
884 fn deserialize<D>(de: D) -> Result<Self, D::Error>
885 where
886 D: serde::Deserializer<'de>,
887 {
888 if de.is_human_readable() {
889 Ok(match WorkloadTagged::deserialize(de)? {
890 WorkloadTagged::MesofactStatic(w) => Workload::MesofactStatic(w),
891 WorkloadTagged::Container(w) => Workload::Container(w),
892 WorkloadTagged::Almanac(w) => Workload::Almanac(w),
893 WorkloadTagged::StaticAsset(w) => Workload::StaticAsset(w),
894 WorkloadTagged::TenantPassway(w) => Workload::TenantPassway(w),
895 })
896 } else {
897 Ok(match WorkloadExternal::deserialize(de)? {
898 WorkloadExternal::MesofactStatic(w) => Workload::MesofactStatic(w),
899 // Only the reference form exists on the wire, by construction
900 // of `WorkloadExternal` — see its doc comment.
901 WorkloadExternal::Container(w) => Workload::container(w),
902 WorkloadExternal::Almanac(w) => Workload::Almanac(w),
903 WorkloadExternal::StaticAsset(w) => Workload::StaticAsset(w),
904 WorkloadExternal::TenantPassway(w) => Workload::TenantPassway(w),
905 })
906 }
907 }
908}
909
910// ── Container manifest (R783-F1 / W324) ───────────────────────────────────────
911
912/// Error text used both by the postcard serializer gate and by
913/// [`ContainerManifest::into_spec`]'s doc, so the two cannot drift.
914const RECIPE_IS_NOT_A_WIRE_SPEC: &str = "a kind = \"container\" workload in the RECIPE form \
915 (a [build] table) cannot cross the kamaji wire: it names an image tag, not a digest, and \
916 the digest does not exist until `docker build` has run. Lower it with \
917 `ContainerBuild::into_spec(digest)` after the build, then send the resulting WorkloadSpec.";
918
919/// On-disk payload of `kind = "container"` — **two forms**, one wire type
920/// (W324 §5).
921///
922/// A [`WorkloadSpec`] asserts a content-addressed identity: its
923/// [`ImageRef::digest`] is a required `sha256:<hex>` and the string form
924/// rejects a bare tag at serde-deserialize (R438-T3). A local component built
925/// from a Dockerfile next to its `workload.toml` cannot satisfy that — its
926/// image is `yah-local/<name>:dev`, and the digest does not exist until the
927/// build has run. So a build *recipe* is not a degenerate spec with a missing
928/// field; it is a promise to produce one, and the two are different types.
929///
930/// The discriminator is the presence of a `[build]` table. `WorkloadSpec` has
931/// no `build` field and [`ContainerBuild`] requires one, so the two shapes are
932/// mutually exclusive — and picking the branch explicitly (rather than with
933/// `#[serde(untagged)]`) is what lets a malformed reference still report
934/// `missing field \`image\`` instead of "data did not match any variant".
935///
936/// Only [`Reference`](Self::Reference) crosses the postcard kamaji wire; see
937/// [`WorkloadExternal`]'s doc comment for why that keeps those bytes
938/// byte-identical to the pre-split encoding.
939#[derive(Debug, Clone, PartialEq, TS)]
940#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
941#[cfg_attr(feature = "json-schema", schemars(untagged))]
942#[ts(untagged)]
943pub enum ContainerManifest {
944 /// Digest-pinned image. Crosses the wire as-is.
945 Reference(WorkloadSpec),
946
947 /// Dockerfile recipe. **Local only** — see [`ContainerBuild`].
948 Recipe(ContainerBuild),
949}
950
951impl ContainerManifest {
952 /// The digest-pinned spec, or `None` for the recipe form.
953 pub fn as_spec(&self) -> Option<&WorkloadSpec> {
954 match self {
955 ContainerManifest::Reference(spec) => Some(spec),
956 ContainerManifest::Recipe(_) => None,
957 }
958 }
959
960 /// The build recipe, or `None` for the reference form.
961 pub fn as_recipe(&self) -> Option<&ContainerBuild> {
962 match self {
963 ContainerManifest::Recipe(b) => Some(b),
964 ContainerManifest::Reference(_) => None,
965 }
966 }
967
968 /// Consume the manifest, yielding the digest-pinned spec. `Err` carries
969 /// the recipe back so a caller that *can* build it still has it.
970 pub fn into_spec(self) -> Result<WorkloadSpec, ContainerBuild> {
971 match self {
972 ContainerManifest::Reference(spec) => Ok(spec),
973 ContainerManifest::Recipe(b) => Err(b),
974 }
975 }
976
977 /// `"reference"` or `"recipe"` — for error messages that need to name
978 /// which form was found without matching on the enum at the call site.
979 pub fn form(&self) -> &'static str {
980 match self {
981 ContainerManifest::Reference(_) => "reference",
982 ContainerManifest::Recipe(_) => "recipe",
983 }
984 }
985}
986
987impl Serialize for ContainerManifest {
988 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
989 where
990 S: serde::Serializer,
991 {
992 match self {
993 // Transparent in both directions: the on-disk container form is
994 // the payload's own fields flattened under `kind = "container"`,
995 // exactly as it was before the split.
996 ContainerManifest::Reference(spec) => spec.serialize(s),
997 ContainerManifest::Recipe(recipe) => {
998 if s.is_human_readable() {
999 recipe.serialize(s)
1000 } else {
1001 Err(serde::ser::Error::custom(RECIPE_IS_NOT_A_WIRE_SPEC))
1002 }
1003 }
1004 }
1005 }
1006}
1007
1008impl<'de> Deserialize<'de> for ContainerManifest {
1009 fn deserialize<D>(de: D) -> Result<Self, D::Error>
1010 where
1011 D: serde::Deserializer<'de>,
1012 {
1013 use serde::de::Error as _;
1014
1015 // postcard and friends are non-self-describing, so there is no map to
1016 // probe for `[build]` — and by construction the binary wire only ever
1017 // carries the reference form anyway (`WorkloadExternal::Container`).
1018 if !de.is_human_readable() {
1019 return WorkloadSpec::deserialize(de).map(ContainerManifest::Reference);
1020 }
1021
1022 // Buffer once, then branch explicitly. `serde_json::Value` is the
1023 // buffer rather than `#[serde(untagged)]`'s private `Content` because
1024 // untagged discards the inner error: `missing field \`image\`` — the
1025 // one thing an author needs to see — becomes "data did not match any
1026 // variant of untagged enum ContainerManifest".
1027 let buffered = serde_json::Value::deserialize(de)?;
1028
1029 match (
1030 buffered.get("build").is_some(),
1031 buffered.get("image").is_some(),
1032 ) {
1033 (true, _) => ContainerBuild::deserialize(buffered)
1034 .map(ContainerManifest::Recipe)
1035 .map_err(|e| {
1036 D::Error::custom(format!(
1037 "kind = \"container\" with a [build] table is a local build recipe: {e}"
1038 ))
1039 }),
1040 (false, true) => WorkloadSpec::deserialize(buffered)
1041 .map(ContainerManifest::Reference)
1042 .map_err(|e| {
1043 D::Error::custom(format!(
1044 "kind = \"container\" without a [build] table is a digest-pinned image \
1045 reference: {e}"
1046 ))
1047 }),
1048 // Neither marker. Reporting `missing field \`image\`` here would
1049 // send a recipe author off to add a field their form does not
1050 // have, so name both forms instead — this is the one case where
1051 // the file does not say which of the two it is trying to be.
1052 (false, false) => Err(D::Error::custom(
1053 "kind = \"container\" must declare either a digest-pinned `image` (the wire \
1054 form: a WorkloadSpec yubaba hands to kamaji) or a [build] table (a local \
1055 Dockerfile recipe built on the operator's box) — it declares neither",
1056 )),
1057 }
1058 }
1059}
1060
1061/// `kind = "container"` in the **recipe** form: a Dockerfile next to the
1062/// component's `workload.toml`, built and run on the operator's box.
1063///
1064/// This is the shape `ContainerReconciler` drives (`docker build` from
1065/// [`build`](Self::build), `docker run` with [`run`](Self::run)). It is
1066/// deliberately *not* a `WorkloadSpec` — see [`ContainerManifest`] for why the
1067/// digest invariant makes that impossible, and [`Self::into_spec`] for the one
1068/// lowering that is allowed.
1069///
1070/// **Unknown keys are tolerated on purpose.** `crates/yah/cloud-admin/workload.toml`
1071/// carries a `[process]` table read by `LocalProcessReconciler` on the dev
1072/// mirror — one component file, three tier runtimes (W324 §1). Adding
1073/// `deny_unknown_fields` here would make that file unparseable as a container
1074/// manifest, which is the opposite of the point.
1075#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1076#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1077pub struct ContainerBuild {
1078 /// Component name. Same field the reference form carries, so a manifest
1079 /// identifies itself the same way whichever form it is written in.
1080 pub name: String,
1081
1082 /// How the image is built. Its presence is what makes this a recipe.
1083 pub build: ContainerBuildStep,
1084
1085 /// How the built image is run locally.
1086 #[serde(default)]
1087 pub run: ContainerRunConfig,
1088}
1089
1090impl ContainerBuild {
1091 /// Lower a recipe to the wire type, **once a build has produced a digest**.
1092 ///
1093 /// The signature is the invariant (W324 §5): there is no way to reach a
1094 /// `WorkloadSpec` from a recipe without supplying the `sha256:<hex>` the
1095 /// build emitted, so an unpinned container spec cannot be constructed by
1096 /// accident.
1097 ///
1098 /// Fallible because `digest` is a caller-supplied string: a malformed one
1099 /// must be an error, not a `WorkloadSpec` that lies about being
1100 /// content-addressed. Everything the recipe does not declare
1101 /// (`tier`, `resources`, `restart_policy`, …) takes the same defaults a
1102 /// hand-written local container gets; `tier` is the caller's because
1103 /// admission control is a cluster policy, not a manifest fact.
1104 pub fn into_spec(self, digest: &str, tier: TierTag) -> Result<WorkloadSpec, String> {
1105 let image_tag = self
1106 .build
1107 .image
1108 .clone()
1109 .unwrap_or_else(|| format!("yah-local/{}:dev", self.name));
1110
1111 // Route through the one parser that owns the digest rule (R438-T3) so
1112 // the recipe path cannot grow a second, laxer definition of "pinned".
1113 let image = compose_import::parse_pinned_image_ref(&format!("{image_tag}@{digest}"))
1114 .map_err(|e| format!("lowering container recipe {:?}: {e}", self.name))?;
1115
1116 let ports = MeshExpose::anonymous_ports(self.run.port);
1117
1118 Ok(WorkloadSpec {
1119 name: self.name.clone(),
1120 image,
1121 tier,
1122 tenant: TenantId::singleton(),
1123 namespace: NamespaceId::singleton(),
1124 replicas: 1,
1125 command: None,
1126 entrypoint: None,
1127 workdir: None,
1128 user: None,
1129 env: self
1130 .run
1131 .env
1132 .into_iter()
1133 .map(|(name, value)| EnvVar {
1134 name,
1135 value: EnvValue::Literal { value },
1136 })
1137 .collect(),
1138 secrets: vec![],
1139 volumes: self
1140 .run
1141 .mounts
1142 .into_iter()
1143 .map(|m| VolumeMount {
1144 source: VolumeSource::Bind {
1145 host_path: PathBuf::from(m.host),
1146 },
1147 target: m.container,
1148 read_only: m.read_only,
1149 from_secret_mount: false,
1150 })
1151 .collect(),
1152 resources: ResourceLimits {
1153 memory_mb: 1024,
1154 cpu_millis: 1000,
1155 memory_request_mb: None,
1156 cpu_limit_millis: None,
1157 pids_max: None,
1158 scratch_floor_mb: None,
1159 },
1160 depends_on: vec![],
1161 requires: vec![],
1162 healthcheck: None,
1163 restart_policy: RestartPolicy::Always,
1164 archetype: Some(LifecycleArchetype::Server),
1165 stop_policy: StopPolicy {
1166 signal: 15,
1167 grace_period: Millis::from_secs(10),
1168 },
1169 expose: ExposeSpec {
1170 mesh: MeshExpose {
1171 identity: MeshIdent(self.name),
1172 ports,
1173 allow_from: vec![],
1174 },
1175 public: None,
1176 operator: None,
1177 },
1178 labels: HashMap::new(),
1179 durability: None,
1180 annotations: HashMap::new(),
1181 files: Vec::new(),
1182 })
1183 }
1184}
1185
1186/// The `[build]` table of a container recipe.
1187#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1188#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1189pub struct ContainerBuildStep {
1190 /// Dockerfile path, relative to the component directory.
1191 #[serde(default = "default_dockerfile")]
1192 pub dockerfile: PathBuf,
1193
1194 /// Build context, relative to the workspace root. `None` → the component
1195 /// directory. Workspace crates set `"."` so their path-dependency sources
1196 /// resolve.
1197 #[serde(default)]
1198 #[ts(optional = nullable)]
1199 pub context: Option<PathBuf>,
1200
1201 /// Image tag to build and run. `None` → `yah-local/<name>:dev`.
1202 ///
1203 /// A **tag**, not an [`ImageRef`]: this names an image that does not exist
1204 /// yet, so there is no digest to pin it by.
1205 #[serde(default)]
1206 #[ts(optional = nullable)]
1207 pub image: Option<String>,
1208}
1209
1210fn default_dockerfile() -> PathBuf {
1211 PathBuf::from("Dockerfile")
1212}
1213
1214impl Default for ContainerBuildStep {
1215 fn default() -> Self {
1216 Self {
1217 dockerfile: default_dockerfile(),
1218 context: None,
1219 image: None,
1220 }
1221 }
1222}
1223
1224/// The `[run]` table of a container recipe.
1225#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, TS)]
1226#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1227pub struct ContainerRunConfig {
1228 /// Container port the process listens on.
1229 #[serde(default)]
1230 #[ts(optional = nullable)]
1231 pub port: Option<u16>,
1232
1233 /// Host port to publish it on. `None` → same as [`port`](Self::port).
1234 #[serde(default)]
1235 #[ts(optional = nullable)]
1236 pub host_port: Option<u16>,
1237
1238 /// Environment passed into the container.
1239 #[serde(default)]
1240 pub env: BTreeMap<String, String>,
1241
1242 /// Bind mounts from the workspace into the container.
1243 #[serde(default)]
1244 pub mounts: Vec<ContainerMount>,
1245}
1246
1247/// One `[[run.mounts]]` entry of a container recipe.
1248#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1249#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1250pub struct ContainerMount {
1251 /// Host path. Relative paths resolve against the workspace root — the
1252 /// declaration lives in the repo, so it should read like a repo path and
1253 /// stay valid on whichever machine the operator runs it from.
1254 pub host: String,
1255
1256 /// Absolute path inside the container.
1257 pub container: PathBuf,
1258
1259 /// Default `true`. A workspace mount is config the service *reads*; a
1260 /// writable default would let a container mutate the operator's checkout
1261 /// as a side effect of running, so opting into that has to be explicit.
1262 #[serde(default = "default_true")]
1263 pub read_only: bool,
1264}
1265
1266fn default_true() -> bool {
1267 true
1268}
1269
1270/// `kind = "mesofact-static"` payload — static-site build colocated with the
1271/// frontend it deploys.
1272///
1273/// The two-role model (R256-F7): a build/publish step plus an optional
1274/// SSR/SPA runtime companion. The build step is always transient (runs once,
1275/// publishes, exits). The companion is long-lived and only present when the
1276/// app has dynamic/server-rendered pages; pure static sites leave it `None`.
1277#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1278#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1279pub struct MesofactStaticWorkload {
1280 /// Build command + output directory.
1281 pub build: BuildConfig,
1282
1283 /// Path (relative to the manifest) of the routes module the
1284 /// `mesofact-static` reconciler reads to enumerate routes.
1285 pub routes: PathBuf,
1286
1287 /// Where the build command runs. Default: `HostSide` (mesofact-dev on the
1288 /// host). Set to `InContainer` for cloud/HA where no host watcher is
1289 /// present and CI-fidelity build environments are required.
1290 #[serde(default)]
1291 pub build_mode: BuildMode,
1292
1293 /// Optional SSR/SPA runtime companion container.
1294 ///
1295 /// `None` → pure static site; Caddy (or equivalent CDN) serves all
1296 /// requests directly from the object store. This is the common case for
1297 /// dev-yah today.
1298 ///
1299 /// `Some` → the workload spec describes a long-lived container that
1300 /// handles dynamic/SSR requests. Caddy routes static asset paths to
1301 /// the object store and all other paths to this container. The companion
1302 /// uses `RestartPolicy::Always`; the orchestrator (camp or yubaba)
1303 /// ensures it stays up alongside the Caddy edge.
1304 #[ts(optional = nullable)]
1305 pub ssr_runtime: Option<WorkloadSpec>,
1306
1307 /// Serve-time reference to a published W272 bundle (R599-F4).
1308 ///
1309 /// `Some` → the built app is deployed as a content-addressed bundle that
1310 /// kamaji materializes from the bundle store (R599-F1) and serves via its
1311 /// native backend, instead of (or in addition to) the build reconciler
1312 /// pushing `dist/` to the object-store/CDN. `None` → legacy
1313 /// build-and-publish-only workload — kamaji rejects that form as yubaba's
1314 /// `mesofact-static` reconciler's responsibility.
1315 ///
1316 /// No `skip_serializing_if`: like `ssr_runtime`, this field is always
1317 /// encoded so the postcard wire codec (non-self-describing, positional)
1318 /// round-trips — `skip_serializing_if` would omit the byte on serialize
1319 /// while decode still expects it. `#[serde(default)]` keeps every existing
1320 /// `mesofact-static` TOML/JSON that predates this field parsing to `None`.
1321 #[serde(default)]
1322 #[ts(optional = nullable)]
1323 pub serve_bundle: Option<MesofactServeBundle>,
1324
1325 /// Revalidate receiver for the almanac push model (R330-F12).
1326 ///
1327 /// `Some` → kamaji also forks `mesofact serve --revalidate <workload>`
1328 /// alongside the bundle's static serve (or in place of it when
1329 /// `serve_bundle` is `None`). The receiver is ephemeral-V8: each
1330 /// `POST /dawn` boots a V8 isolate, re-renders the route, republishes to
1331 /// the CDN, then drops the isolate. (`/revalidate` is still served as a
1332 /// transitional alias — yah R752-T10 renamed it so the render stage stops
1333 /// sharing a path with almanac's feed-refetch stage, `POST /freshen`.)
1334 ///
1335 /// Env vars are resolved at deploy time (R2 creds + mirror bearer) so
1336 /// the node never sees keystore slot names.
1337 #[serde(default)]
1338 #[ts(optional = nullable)]
1339 pub revalidate_receiver: Option<MesofactRevalidateReceiver>,
1340}
1341
1342/// Revalidate receiver config (R330-F12) — tells kamaji to fork a second
1343/// `mesofact serve --revalidate` process alongside the static bundle server.
1344///
1345/// The receiver is the almanac push endpoint: a lightweight resident axum
1346/// server mounting `POST /dawn` (plus the legacy `/revalidate` alias) that
1347/// boots V8 on each poke, re-renders the invalidated route, publishes to
1348/// R2/CDN, then drops the isolate.
1349#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1350#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1351pub struct MesofactRevalidateReceiver {
1352 /// Routes the receiver accepts pokes for (allowlist).
1353 /// Empty vec → all routes in the workload's manifest are revalidatable.
1354 #[serde(default)]
1355 pub routes: Vec<String>,
1356
1357 /// Path to `mesofact.config.toml` carrying the `[publish]` block
1358 /// (bucket / zone / env-named credentials). Relative to the workload
1359 /// directory. Default: `"mesofact.config.toml"`.
1360 #[serde(default = "default_publish_config_path")]
1361 pub publish_config: String,
1362
1363 /// Env var name holding the bearer secret for this tenant, resolved
1364 /// at deploy time and set as `MESOFACT_MIRROR_KEY` on the receiver
1365 /// process. `None` → open receiver (no bearer check).
1366 #[ts(optional = nullable)]
1367 pub mirror_key_env: Option<String>,
1368
1369 /// Environment variables set on the revalidate process by kamaji.
1370 /// Keys are the canonical env var names (`MESOFACT_S3_ACCESS_KEY_ID`,
1371 /// `MESOFACT_S3_SECRET_ACCESS_KEY`, `CLOUDFLARE_API_TOKEN`,
1372 /// `MESOFACT_MIRROR_KEY`). Values are resolved from the keystore at
1373 /// deploy time — the node never sees slot names.
1374 #[serde(default)]
1375 pub env: std::collections::BTreeMap<String, String>,
1376
1377 /// Feed-fetch tier (R330-F31) — the almanac feeds whose artifacts must be
1378 /// refreshed **on the node** for a poke to have anything new to render.
1379 ///
1380 /// Empty → no fetcher; the receiver re-renders whatever data the bundle was
1381 /// built with (correct for a site whose data only changes at build time,
1382 /// silently stale for one whose data is a live feed). Non-empty → kamaji
1383 /// forks a third resident process, the `almanac-feed` fetcher, next to the
1384 /// receiver — resolved from the bundle's `bins/<triple>/almanac-feed` when
1385 /// it carries one, else from [`feed_runtime`](Self::feed_runtime).
1386 #[serde(default)]
1387 pub feeds: Vec<AlmanacFeed>,
1388
1389 /// Runtime ref the `almanac-feed` fetcher resolves from the node's shared
1390 /// runtime-asset cache when the bundle carries no `bins/` (R746-T3), e.g.
1391 /// `"almanac-feed/0.8.22"`.
1392 ///
1393 /// This is what lets a **vanilla** bundle have a feed tier at all. A
1394 /// self-contained bundle stages the fetcher into `bins/` and stays closed
1395 /// over it; a vanilla bundle carries no binaries by construction, so the
1396 /// fetcher has to be a node-level asset for the same reason `serve` is —
1397 /// otherwise a templates-only sync would still need a cross-built musl
1398 /// binary sitting on the syncing machine's disk.
1399 ///
1400 /// `None` with `feeds` non-empty and no sidecar in the bundle is a deploy
1401 /// failure, named at the node. It is not a silent skip: "the site serves
1402 /// but its data is frozen" is the exact state R330-F31 exists to make
1403 /// observable.
1404 #[serde(default)]
1405 #[ts(optional = nullable)]
1406 pub feed_runtime: Option<String>,
1407
1408 /// Seconds between feed-fetch ticks. Ignored when `feeds` is empty.
1409 ///
1410 /// This is the site's freshness bound: a release lands, and the next tick
1411 /// refreshes + pokes. `FeedRunner`'s change-suppression means an idle tick
1412 /// costs one conditional fetch, so a short interval is affordable.
1413 #[serde(default = "default_feed_interval_secs")]
1414 pub feed_interval_secs: u64,
1415
1416 /// Workspace-relative path of the component whose build produced this
1417 /// bundle, e.g. `app/yah/web/marketing` (R330-F31).
1418 ///
1419 /// Reconciles two roots for one file: a feed declares `emit.artifact`
1420 /// workspace-relative (that is where it is authored), while the route
1421 /// declares the same file project-relative (that is what the bundle
1422 /// carries). The fetcher strips this prefix to get from one to the other.
1423 /// `None` → the two already coincide.
1424 #[serde(default)]
1425 #[ts(optional = nullable)]
1426 pub feed_project_prefix: Option<String>,
1427
1428 /// Secrets materialized to fixed host paths before the receiver process
1429 /// forks (R876-B16). Bundle-tier deploys are native (no container
1430 /// namespace, see `deploy_non_container`), so only `SecretTarget::File`
1431 /// is meaningful here — there is no env-var form, because the whole point
1432 /// is that the plaintext credential value never becomes part of this
1433 /// spec or of kamaji's in-memory/on-disk deploy record. Replaces the
1434 /// prior design where the CLI resolved R2/Cloudflare credentials itself
1435 /// and inlined them as literal values into `env` above.
1436 #[serde(default)]
1437 pub secrets: Vec<SecretMount>,
1438}
1439
1440/// One almanac feed handed to the on-node fetcher (R330-F31).
1441///
1442/// The definition travels **by value**, not by path: the node has no copy of
1443/// the camp's `.yah/almanac/` tree, and staging one into the content-addressed
1444/// bundle would put a mutable-by-nature config inside an immutable artifact.
1445/// The fetcher parses `config_toml` with the same `FeedConfig` type that reads
1446/// the file at the source, so there is one schema and no drift.
1447#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1448#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1449pub struct AlmanacFeed {
1450 /// Feed name — the `.yah/almanac/<name>.toml` stem. Logs/diagnostics only;
1451 /// `config_toml` is authoritative.
1452 pub name: String,
1453
1454 /// Verbatim contents of the feed definition TOML.
1455 pub config_toml: String,
1456}
1457
1458fn default_publish_config_path() -> String {
1459 "mesofact.config.toml".to_string()
1460}
1461
1462/// Five minutes: fast enough that a release is live on yah.dev before anyone
1463/// goes looking, slow enough to be invisible against a source API's rate limit.
1464fn default_feed_interval_secs() -> u64 {
1465 300
1466}
1467
1468/// Serve-time reference to a published W272 bundle (R599-F4) — the
1469/// `{bundle_digest, runtime, lifecycle}` triple a `mesofact-static` workload
1470/// carries when kamaji, not the build reconciler, serves it.
1471///
1472/// @yah:ticket(R870-B6, "Bundle origin is node-wide, so a second tenant's bundle can never be materialized")
1473/// @yah:status(review)
1474/// @yah:at(2026-09-09T03:23:08Z)
1475/// @yah:assignee(agent:bundle-anthropic-ashguard)
1476/// @yah:parent(R870)
1477/// @yah:gotcha("REPORTED BY THE NOISETABLE CAMP, which is R870's second tenant made concrete. Its bundle deploy gets ALL THE WAY to admission and then fails: 'materialize bundle f75c6940...: missing blob manifests/f75c6940... for path \"manifest.toml\"'. Ident and placement were correct — the CLI printed 'noisetable admitted by us-east-001 (http://100.64.0.3:7443)' — so this is not a discovery or placement bug. The node accepted a digest it has no way to fetch.")
1478/// @yah:gotcha("ROOT CAUSE, read not guessed: MesofactServeBundle is { digest, runtime, lifecycle, port, env } and carries NEITHER a bucket NOR an origin (oss/yah-base/crates/workload-spec/src/lib.rs). BundleSlot::serve_bundle constructs it from the slot and drops the slot's `bucket` on the floor (oss/yubaba/crates/cloud/src/reconciler/mesofact_bundle.rs). kamaji then fetches from the NODE-WIDE KAMAJI_BUNDLE_ORIGIN (oss/kamaji/crates/kamaji-bin/src/main.rs:86). So the publish side is per-service and the fetch side is per-node, and they only agree while the fleet has exactly one tenant.")
1479/// @yah:gotcha("MEASURED, WITH A NEGATIVE CONTROL — all four taken 2026-09-08. (1) our manifest blob IS in the noisetable-marketing bucket: `yah cloud bucket ls --bucket noisetable-marketing` lists manifests/f75c6940.... (2) https://cdn.noisetable.com/manifests/f75c6940... = 200. (3) https://cdn.yah.dev/manifests/f75c6940... = 404 — the node's origin, where it looked. (4) POSITIVE CONTROL, so 404 is not just a broken URL shape: https://cdn.yah.dev/manifests/0279f43e... (a real yah bundle) = 200. yah-dev holds 49 manifests/ objects; noisetable-marketing holds 1, and it is ours.")
1480/// @yah:gotcha("THE RUNTIME ASSET HALF FAILS IDENTICALLY AND IS A SECOND BLOCKER, not the same one twice — fixing only the manifest fetch leaves the deploy failing one step later. runtimes/mesofact/0.8.32/x86_64-unknown-linux-musl.toml exists in yah-dev and in NO other bucket, and the apply printed 'no mesofact/0.8.32 runtime asset is published yet' against the tenant's own bucket. Whatever carries the origin must cover manifests, blobs AND the runtime-asset lookup.")
1481/// @yah:gotcha("THE WORKAROUND WAS CONSIDERED AND REFUSED BY THE OPERATOR, recorded so it is not re-proposed as a shortcut: point the tenant's bundle slot at bucket = \"yah-dev\" so it lands in the store the node already reads. It works today and is one word. It also puts a tenant's build output in yah's bucket, which makes the tenant boundary fictional for bundle content in exactly the way reusing the account-scoped ACME token would have for DNS — the same call R870-F1 already made the other way when it minted a noisetable-only token instead. Noisetable's mirror still declares bucket = \"noisetable-marketing\" and is correct as written; it is yah that cannot consume it.")
1482/// @yah:next("THE PRECEDENT IS IN THE SAME STRUCT AND SHOULD BE COPIED RATHER THAN REDESIGNED. MesofactServeBundle::port's own doc records that it 'used to mean fall back to kamaji's node-wide default (KAMAJI_BUNDLE_PORT, else 8080), which was a single node-wide slot wearing the word default — correct only while a node hosted one'. R844-F2 fixed that axis by making the value travel with the workload and letting the node-wide setting be a fallback. KAMAJI_BUNDLE_ORIGIN is the same defect on the store axis, unfixed. Do the same thing: an Option<String> origin (or bucket) on MesofactServeBundle, threaded from BundleSlot::serve_bundle, with KAMAJI_BUNDLE_ORIGIN demoted to the fallback so every existing single-tenant deploy is byte-identical.")
1483/// @yah:next("SCOPE IS THREE EDIT SITES, all named: (a) MesofactServeBundle gains the field (oss/yah-base/crates/workload-spec/src/lib.rs) — wire type, so schema + TS export + drift test move with it; (b) BundleSlot::serve_bundle stops discarding the slot's `bucket` (oss/yubaba/crates/cloud/src/reconciler/mesofact_bundle.rs); (c) kamaji resolves the per-workload origin ahead of KAMAJI_BUNDLE_ORIGIN for manifest, blob AND runtime-asset fetches (oss/kamaji/crates/kamaji-bin). A bucket name is not directly fetchable, so decide deliberately whether the field carries a public origin URL or a bucket that the node maps to one — the tenant's blobs are reachable at https://cdn.noisetable.com today, so an origin URL needs no new credential on the node and keeps kamaji credential-free, which is the property worth preserving.")
1484/// @yah:next("DO NOT SOLVE THIS BY GIVING KAMAJI R2 CREDENTIALS PER TENANT. It fetches over plain HTTP from a public origin today and holds no bucket credential at all; adding one would put a tenant-scoped R2 key on three public boxes for content that is already world-readable. The W295 warning about the account-wide R2 write pair is the adjacent precedent.")
1485/// @yah:verify("End to end, from the noisetable camp: `yah cloud apply --service noisetable-marketing --env cloud` with bucket = \"noisetable-marketing\" unchanged reaches Running rather than Failed on us-east-001, and https://noisetable.com/ serves the site instead of the R870-F5 holding page.")
1486/// @yah:verify("Regression, single-tenant: yah-marketing's own deploy is unchanged with no edit to its mirror — it declares bucket = \"yah-dev\" and the node's KAMAJI_BUNDLE_ORIGIN already points there, so the fallback path must produce a byte-identical spec. Assert it at the wire type, not just by observing yah.dev stay up.")
1487/// @yah:verify("The runtime-asset half specifically: the deploy must NOT print 'no mesofact/<ver> runtime asset is published yet' when the tenant's own origin serves one, and must still resolve the stock runtime for a tenant that publishes none.")
1488/// @yah:handoff("FIXED, AND THE FIX IS THE PRECEDENT THE TICKET NAMED. `MesofactServeBundle` gains `origin: Option<String>` — the public HTTPS origin serving the bucket the workload was published to — appended after `env` (postcard is positional; no skip_serializing_if), `#[serde(default)]` + `#[ts(optional = nullable)]`. `None` means the node's own `KAMAJI_BUNDLE_ORIGIN`, so every existing single-tenant deploy is byte-identical and no yah-owned mirror needs an edit.")
1489/// @yah:handoff("A URL, NOT A BUCKET, decided rather than defaulted. A bucket name is not fetchable: resolving one would need a node-side bucket→origin table (the same node-wide defect one level down) or R2 credentials on every box, for content that is world-readable and against a node that deliberately holds none. `providers.static.asset_origin` already makes this call one tier over. Declared rather than derived from `zone`, because `https://cdn.<zone>` is a guess about an R2 custom-domain binding that may not exist.")
1490/// @yah:handoff("PUBLISHER SIDE (oss/yubaba/crates/cloud/src/reconciler/mesofact_bundle.rs): `BundleSlot` gains `origin`, `origin` joins ALLOWED_SLOT_KEYS, and `serve_bundle` stops dropping the store on the floor. Parsed strictly — a schemeless value (`cdn.noisetable.com`, or a bucket name) is REFUSED at parse with the reason, because its only consumer joins keys onto it as path segments, so accepting one would deploy clean and fail on the node at materialize time. A trailing slash is trimmed once, here.")
1491/// @yah:handoff("NODE SIDE (oss/kamaji/crates/kamaji-bin/src/server.rs): new `BundleBackend::store_for(origin)`. `None` returns the node's store itself, unwrapped. `Some` builds an `HttpReadOnlyObjectStore` (on the blocking pool — a reqwest blocking client panics if constructed inside a tokio runtime) and puts it in FRONT of the node's, not in place of it. All three fetch sites take it: the manifest+blob materialize, the serve runtime-asset resolve, and the feed-tier fetcher (threaded through `fork_revalidate_receiver` → `fork_feed_tier`).")
1492/// @yah:handoff("THE READ-THROUGH IS WHAT MAKES THE RUNTIME-ASSET HALF WORK, and it is why this is a chain and not a swap. The fleet publishes the stock `mesofact/<ver>` serve runtime once, to its own origin; a tenant has no reason to mirror ~70MB of it. Tenant origin answers for the tenant's bundle, node origin answers for the stock runtime, and which is which is not knowable per key. New `yah_object_store::FallbackObjectStore` (oss/yah-base/crates/object-store/src/fallback.rs) does exactly that and nothing else: reads chain, writes are REFUSED (a chain has no principled answer to which member a `put` lands in — guessing would put one tenant's bytes in another's store, the boundary this ticket exists to draw). Safe because every key on this path is content-addressed and blake3-verified after the fetch, so a fallback can return the wrong store's bytes only if they are the right bytes. A primary ERROR is not laundered into a miss: an unreachable tenant origin fails loudly instead of quietly serving yah's copy.")
1493/// @yah:handoff("APPLY-TIME NOTE CORRECTED (app/yah/cli/src/cloud.rs): 'no mesofact/<ver> runtime asset is published yet … or the node will have nothing to fork' was true for a single-tenant fleet and is now false — for a tenant bucket it is the NORMAL state. It names the bucket it checked and says the node reads through to its own origin for the stock one.")
1494/// @yah:handoff("REGENERATED: `cargo run -p xtask -- emit-schemas` + export-ts. `.yah/schema/workload.toml.schema.json` and `packages/yah/workload-spec/index.ts` carry the new field. (`.yah/schema/machine.toml.schema.json` also moved — it was already dirty in this shared tree when this session started, not something this ticket authored.)")
1495/// @yah:verify("cargo test -p yah-object-store -p yah-workload-spec (oss/yah-base): 101 pass, 0 fail — includes 7 new FallbackObjectStore tests (primary wins, clean miss falls through, head chains, a primary error is NOT a miss, locate names both members, writes refused, list_prefix unions).")
1496/// @yah:verify("cargo test -p yah-cloud --lib (oss/yubaba): 1134 pass, 0 fail — 4 new: a declared origin reaches the serve_bundle; NO declared origin leaves the node-wide one in charge (the single-tenant regression, asserted at the wire type, not by watching yah.dev stay up); trailing slash trimmed; a schemeless origin is refused naming the shape.")
1497/// @yah:verify("cargo test -p kamaji-bin --features bundle-serving --lib (oss/kamaji): 270 pass, 0 fail (was 267). THE END-TO-END ONE IS `a_second_tenants_bundle_materializes_from_its_own_origin`: the tenant's bundle is published to a store served over a REAL loopback HTTP/1.1 origin (not a second injected ObjectStore — a test that handed the backend an in-memory store would pass with the URL ignored, which is the bug), the node's store holds ONLY the fleet's stock runtime, and the deploy comes up: tenant content from the tenant's origin, stock runtime read through to the node's. `without_an_origin_a_tenant_bundle_fails_after_a_clean_admission` is its negative control — the reported bug verbatim. `an_undeclared_origin_resolves_to_the_node_store_unchanged` pins Arc::ptr_eq on the None path so the single-tenant case pays nothing for the mechanism.")
1498/// @yah:verify("cargo check -p kamaji-bin with NO features (the bundle-serving-off build) and cargo test -p kamaji-proto (33 pass, the postcard round-trip): both clean.")
1499/// @yah:verify("cargo check --workspace: exit 0. Every remaining warning is pre-existing in files this ticket did not touch (board.rs, runner/sessions.rs, mesofact_static.rs, agent-tools/shared_pool.rs).")
1500/// @yah:verify("NOT VERIFIED LIVE, AND CANNOT BE FROM HERE: the ticket's first verify is a `yah cloud apply` from the noisetable camp reaching Running. That needs the fleet to be RUNNING this kamaji — us-east-001 still runs the pre-change binary, which ignores the field. Filed as R870-T9 (roll kamaji, then add the one `origin` line to noisetable's mirror), which depends_on this ticket.")
1501/// @yah:gotcha("ROLL ORDER DOES NOT BITE, checked rather than assumed: `origin` reaches yubaba as JSON and `MesofactServeBundle` carries no `deny_unknown_fields`, so a mirror declaring an origin against an un-rolled node has the field ignored and fails exactly as it does today — not a parse error. Only kamaji has to move.")
1502/// @yah:gotcha("THE NODE CACHE IS SHARED ACROSS ORIGINS AND THAT IS FINE FOR BUNDLES, LESS OBVIOUSLY SO FOR RUNTIME ASSETS. Materialized bundles are keyed by blake3 digest, so two tenants cannot collide. Runtime assets are keyed by `<runtime>/<version>/<triple>` — so two tenants publishing DIFFERENT bytes under the same `mesofact/<ver>` would share one node cache entry, first writer wins. Not reachable today (nobody but the fleet publishes a mesofact runtime, and `publish_runtime_asset`'s own docs already say publish a new version rather than repointing one), but it is the next thing this axis will need if a tenant ever ships its own build of a stock runtime name.")
1503#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1504#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1505pub struct MesofactServeBundle {
1506 /// BLAKE3 digest of the published bundle manifest — the content-address
1507 /// kamaji materializes from the bundle store (`yah_mesofact_bundle`,
1508 /// R599-F1). Same 64-hex shape the bundle crate's `BundleHash` validates.
1509 pub digest: BlakeHash,
1510
1511 /// Runtime that serves the bundle: `"self"` (bundle ships its own
1512 /// `bins/<triple>/serve`) or `"mesofact/<version>"` (resolve the stock
1513 /// serve runtime asset from the node cache). Wire-mirrors
1514 /// `yah_mesofact_bundle::BundleRuntime`; kept as a plain `String` here so
1515 /// workload-spec stays free of the bundle crate and its non-TS/schema
1516 /// newtypes.
1517 pub runtime: String,
1518
1519 /// How kamaji supervises the served bundle. Default: keep-alive.
1520 #[serde(default)]
1521 pub lifecycle: BundleLifecycle,
1522
1523 /// Port the served bundle listens on (R599-F12). This is the bundle-tier
1524 /// analogue of a container's `expose.mesh.ports`: the *declared* serving
1525 /// port, which a proxy pairs with the workload's mesh IP to get a dialable
1526 /// address.
1527 ///
1528 /// `None` → **the supervisor allocates one** (R844-F2), and reports the
1529 /// port it bound back to yubaba on the next workload listing, where it
1530 /// lands in the service record an ingress upstream is rendered from. This
1531 /// is the normal case: a mirror should not have to name a port at all.
1532 ///
1533 /// It used to mean "fall back to kamaji's node-wide default
1534 /// (`KAMAJI_BUNDLE_PORT`, else 8080)", which was a single node-wide slot
1535 /// wearing the word *default* — correct only while a node hosted one
1536 /// bundle, and a silent collision for the second. R599-F12 added this field
1537 /// so a workload could opt out of that; R844-F2 removed the default itself,
1538 /// so opting out is no longer something anyone has to remember to do.
1539 ///
1540 /// Declaring a port still pins it exactly, for a workload that must be
1541 /// reachable at a known number.
1542 ///
1543 /// No `skip_serializing_if` — see `serve_bundle`'s note: the postcard wire
1544 /// codec is positional, so an omitted byte shifts every later field.
1545 #[serde(default)]
1546 #[ts(optional = nullable)]
1547 pub port: Option<u16>,
1548
1549 /// Environment the serve process is forked with (R556-T12) — already
1550 /// **resolved** values, `NAME → value`.
1551 ///
1552 /// This is what makes an SSR route that reads a private source deployable
1553 /// at all: `mesofact serve` resolves a source's credentials from its own
1554 /// process environment at request time, and before this field the static /
1555 /// SSR serve process was forked with `env: vec![]` while only the
1556 /// `revalidate_receiver` sub-slot carried any. A declared-authed SSR site
1557 /// therefore deployed clean and failed *per request* on the node.
1558 ///
1559 /// Resolution happens deploy-side, exactly like
1560 /// [`MesofactRevalidateReceiver::env`]: the mirror declares source URIs
1561 /// (`vault:<slot>` / `env:<VAR>`), `yah cloud apply` resolves them against
1562 /// the operator's vault, and the node receives values. Keystore slot names
1563 /// never cross the wire.
1564 ///
1565 /// Appended **after** `port` — see `port`'s note: the postcard wire codec
1566 /// is positional, so a new field goes last and never carries
1567 /// `skip_serializing_if`.
1568 #[serde(default)]
1569 pub env: BTreeMap<String, String>,
1570
1571 /// Public HTTPS origin serving the bundle store this workload was published
1572 /// to (R870-B6) — e.g. `"https://cdn.noisetable.com"`, the R2 custom domain
1573 /// bound to the tenant's own bucket.
1574 ///
1575 /// `None` → the node's own `KAMAJI_BUNDLE_ORIGIN`, which is the shape every
1576 /// yah-owned mirror uses and the reason this is optional rather than
1577 /// required.
1578 ///
1579 /// # Why the store has to travel with the workload
1580 ///
1581 /// This is `port`'s defect one axis over, and it was found the same way: by
1582 /// a second tenant. Publishing is per-service — `providers.bundle.bucket`
1583 /// names the tenant's own R2 bucket — while fetching was per-*node*, from
1584 /// the single `KAMAJI_BUNDLE_ORIGIN` the systemd drop-in sets. Those two
1585 /// agree only while the fleet hosts one tenant. The noisetable deploy got
1586 /// all the way to admission and then failed with `missing blob
1587 /// manifests/<digest>`: its manifest was in `noisetable-marketing`, and the
1588 /// node looked in `cdn.yah.dev`.
1589 ///
1590 /// Pointing the tenant's slot at yah's bucket would also have worked, and
1591 /// was refused — a tenant's build output in yah's store makes the tenant
1592 /// boundary fictional for bundle content.
1593 ///
1594 /// # Why a URL and not the bucket name
1595 ///
1596 /// A bucket name is not fetchable. Resolving one would need either a
1597 /// node-side bucket→origin map (another node-wide table, the same defect
1598 /// again) or R2 credentials on every box — for content that is already
1599 /// world-readable, and against a node that deliberately holds no bucket
1600 /// credential at all (see `HttpReadOnlyObjectStore`). Integrity comes from
1601 /// the content address, not the transport, so an unauthenticated origin is
1602 /// exactly as safe here as an authenticated one.
1603 ///
1604 /// The declared origin does not *replace* the node's: kamaji reads through
1605 /// to `KAMAJI_BUNDLE_ORIGIN` on a miss, which is what lets a tenant fetch
1606 /// the stock `mesofact/<ver>` serve runtime the fleet publishes once
1607 /// without republishing ~70MB into their own bucket.
1608 ///
1609 /// Appended **after** `env` — see `port`'s note on the positional codec.
1610 #[serde(default)]
1611 #[ts(optional = nullable)]
1612 pub origin: Option<String>,
1613}
1614
1615/// Lifecycle mode for a served bundle (W272 §3).
1616#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1617#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1618#[serde(rename_all = "snake_case")]
1619pub enum BundleLifecycle {
1620 /// Fork at deploy, keep resident, restart per policy — today's server
1621 /// archetype. Memory is resident for the workload's lifetime.
1622 KeepAlive,
1623
1624 /// Kamaji owns the listen socket, forks the runtime on the first connection
1625 /// (fd-passing), and reaps it after `idle_ttl` with zero connections — the
1626 /// "serverless" tier (zero memory when idle). The JIT fork/reap mechanics
1627 /// land in R599-F6; this variant only declares the intent + budget.
1628 OnDemand {
1629 /// Idle time with no live connections before kamaji reaps the process.
1630 idle_ttl: Millis,
1631 },
1632}
1633
1634impl Default for BundleLifecycle {
1635 /// Keep-alive — the resident server archetype — matches the current
1636 /// deploy-and-supervise default.
1637 fn default() -> Self {
1638 BundleLifecycle::KeepAlive
1639 }
1640}
1641
1642// ── Per-tenant passway (R852-F1 / W267 §Free-tier ingress at 10k domains) ─────
1643
1644/// Container-side / node-side path a per-tenant passway reads its PEM chain
1645/// from, when the declaration does not name one. Deliberately per-domain: two
1646/// tenants sharing a path is two tenants sharing a certificate.
1647pub const DEFAULT_TENANT_PASSWAY_CERT_DIR: &str = "/run/yah/passway/tenants";
1648
1649/// Node path of the passway binary a per-tenant passway forks, when the
1650/// declaration does not name one. Matches the path the passway image installs
1651/// to, which is what `local-driver`'s node-appliance spec also runs.
1652pub const DEFAULT_PASSWAY_COMMAND: &str = "/usr/local/bin/passway";
1653
1654/// One **cold, per-tenant passway** — a TLS terminator that serves exactly one
1655/// custom tenant domain, forked on demand by kamaji's JIT tier
1656/// (`kamaji::jit::JitRuntime`) and self-reaped when idle.
1657///
1658/// This is the declaration W267's free-tier ingress design was missing. R779
1659/// shipped every mechanism — the SNI demux that splices `:443` by ClientHello
1660/// without terminating TLS, passway's fd-3 adoption + idle self-reap, the
1661/// R2-backed cert store off raft, the per-domain DNS-01 issuer — but nothing
1662/// could *say* "there is a passway for `shop.tenant.io` at `127.0.0.1:8443`",
1663/// because kamaji's on-demand tier was reachable only through
1664/// [`MesofactServeBundle`], a mesofact-specific carrier.
1665///
1666/// ## Why a variant and not an annotated [`Workload::Container`]
1667///
1668/// The W267 **node appliance** is a container (see `Workload::Container`'s doc
1669/// comment): one resident passway per public-IP node, image-pulled, supervised
1670/// like anything else, so an archetype + annotation expressed it with no wire
1671/// change. A per-tenant passway is the opposite on every axis that decides the
1672/// question. It is **native-forked, not containerized** — kamaji's JIT tier
1673/// hands the child an inherited fd, and that path (`kamaji::jit`) forks a
1674/// process, not a container. It is **zero-resident**, so the deploy Ack means
1675/// "socket bound and armed", not "a process is running". And there are ten
1676/// thousand of them, generated from the enrollment set rather than written by
1677/// hand. Squeezing that into `Container` would mean a spec whose image is a
1678/// lie and whose supervision arm is chosen by an annotation nobody reading the
1679/// type would look for.
1680///
1681/// ## The bind string is the fd-table key
1682///
1683/// [`listen`](Self::listen) is **declared, never allocated.** It is the address
1684/// the tenant's enrollment record already names as its demux backend
1685/// (`yubaba::cert_store::Enrollment::tls_backend`), so kamaji must bind exactly
1686/// it — an allocator picking a port here would arm a socket the demux never
1687/// routes to, and the tenant's domain would resolve, handshake, and hang.
1688///
1689/// The same string is also passway's `PASSWAY_LISTEN`, and it must match **byte
1690/// for byte**: passway's socket-activation path (on by default) *panics* rather
1691/// than binding fresh when `LISTEN_FDS` is set and the seed does not take, so a
1692/// drifted string is a workload that forks and immediately dies on every
1693/// connection. [`jit_spec`](Self::jit_spec) is the reason that cannot happen —
1694/// it renders `PASSWAY_LISTEN` from this one field rather than asking a caller
1695/// to restate it, the same "derive, never re-state" rule
1696/// `yubaba::domain_admin` applies to the DNS-01 record name.
1697#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1698#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1699// R896-F2: `deny_unknown_fields` was REMOVED here. It used to be inert on the
1700// kamaji wire — R658-B1 said so explicitly, "only constrains TOML/JSON", which
1701// was true while that wire was positional postcard with no field names to
1702// reject. It is not true any more: `Workload::TenantPassway` now crosses the UDS
1703// as name-keyed JSON (`kamaji_proto::tolerant`), so this attribute would refuse
1704// a spec from a peer one field ahead — defeating the whole envelope on exactly
1705// the workload kind `yubaba::tenant_passway` reconciles most often, and turning
1706// an additive field change back into a paired fleet ship.
1707//
1708// `BuildConfig` keeps its copy: that one is authoring-only and the loudness it
1709// buys (a misplaced `routes` key is a parse error naming the key, not a silently
1710// dropped one) is the reason R658-B1 added it.
1711pub struct TenantPasswayWorkload {
1712 /// The single custom domain this passway terminates TLS for — the SNI the
1713 /// demux matched to route here, and the hostname
1714 /// [`jit_spec`](Self::jit_spec) keys the rendered `PASSWAY_UPSTREAMS`
1715 /// entries on.
1716 pub domain: String,
1717
1718 /// `host:port` kamaji binds and holds in custody, and the address the demux
1719 /// splices this domain's bytes to. See the type doc: declared, not
1720 /// allocated, and byte-identical to `PASSWAY_LISTEN`.
1721 pub listen: String,
1722
1723 /// Plaintext backends passway forwards to after terminating TLS, as bare
1724 /// `host:port`. Rendered as `<domain>=<addr>` entries — repeated entries
1725 /// load-balance (R844-F3), which is why this is a list and not one address.
1726 ///
1727 /// Empty is legal and means "no backend yet": passway answers 503 rather
1728 /// than refusing to start, so a domain can be enrolled and issued before
1729 /// the tenant's app is placed.
1730 #[serde(default)]
1731 pub upstreams: Vec<String>,
1732
1733 /// Where the per-domain PEM pair the R2 cert store holds
1734 /// (`yubaba::cert_store`) has been materialized on the node.
1735 pub tls: TenantPasswayTls,
1736
1737 /// Idle time with no in-flight request before the process exits, leaving
1738 /// kamaji holding the socket and re-forking on the next connection.
1739 ///
1740 /// `None` means **never reap** — a long-running per-tenant passway. That is
1741 /// the shape the free tier exists to avoid (10k resident processes is the
1742 /// number W267 §"Scaling B to a free tier" set out to dissolve), and it also
1743 /// re-opens a rotation gap a cold passway does not have: a cold one re-reads
1744 /// [`tls`](Self::tls) at every cold start, while a resident one holds the
1745 /// chain it started with. Sub-second values round **up** to one second, and
1746 /// zero is not "never" — see [`idle_ttl_secs`](Self::idle_ttl_secs).
1747 ///
1748 /// No `skip_serializing_if`: this rides the positional postcard wire.
1749 #[serde(default)]
1750 #[ts(optional = nullable)]
1751 pub idle_ttl: Option<Millis>,
1752
1753 /// Node path of the passway binary to fork. `None` →
1754 /// [`DEFAULT_PASSWAY_COMMAND`].
1755 #[serde(default)]
1756 #[ts(optional = nullable)]
1757 pub command: Option<String>,
1758
1759 /// Extra environment for the forked process — the ACME/auth/health knobs
1760 /// passway reads that this type has no opinion about.
1761 ///
1762 /// **Cannot override the derived keys.** [`jit_spec`](Self::jit_spec)
1763 /// applies this map *first* and the derived
1764 /// (`PASSWAY_LISTEN`/`LISTEN_FDS`/`PASSWAY_IDLE_TTL_SECS`/
1765 /// `PASSWAY_UPSTREAMS`/`PASSWAY_TLS_*`) keys last, so an escape hatch cannot
1766 /// silently break the fd handoff — which would surface as a domain that
1767 /// hangs, not as a config error.
1768 #[serde(default)]
1769 pub env: BTreeMap<String, String>,
1770
1771 /// Discover the backends from yubaba's service records instead of the
1772 /// static [`upstreams`](Self::upstreams) list (R910-F2).
1773 ///
1774 /// `None` is the free tier's shape: the door knows where TLS terminates
1775 /// and nothing about where the tenant's app runs, so it answers 503 until
1776 /// something fills `upstreams`. `Some` is a door that fronts a workload
1777 /// this fleet places, which is what a tunnel-fronted door is: the
1778 /// placement moves, so a pinned address list would go stale.
1779 ///
1780 /// A field rather than three `env` entries because `PASSWAY_UPSTREAM_SOURCE`
1781 /// is a derived key [`jit_spec`](Self::jit_spec) writes last — the escape
1782 /// hatch cannot change it, by design.
1783 #[serde(default)]
1784 #[ts(optional = nullable)]
1785 pub discover: Option<TenantPasswayDiscovery>,
1786}
1787
1788/// Where a per-tenant passway polls for its backends (R910-F2): the yubaba
1789/// nodes holding the fronted workload's service records, and that workload's
1790/// ident.
1791#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1792#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1793pub struct TenantPasswayDiscovery {
1794 /// Base URLs of the yubabas to poll, e.g. `http://100.64.0.10:7443`. Every
1795 /// entry is polled and the records unioned (R844-F23).
1796 pub urls: Vec<String>,
1797 /// The fronted workload's ident, as its service records carry it.
1798 pub ident: String,
1799}
1800
1801/// Node-side paths of one tenant's materialized certificate pair.
1802///
1803/// Paths rather than [`SecretMount`]s: the JIT tier forks a *process*, not a
1804/// container, so there is no mount namespace to project a secret into — the
1805/// files are read from the node filesystem by the forked passway. Whoever
1806/// materializes them out of `yubaba::cert_store` owns their permissions.
1807#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1808#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1809// R896-F2: removed for the same reason as [`TenantPasswayWorkload`] above —
1810// this type rides inside it across the now name-keyed kamaji wire.
1811pub struct TenantPasswayTls {
1812 /// PEM chain path (`PASSWAY_TLS_CERT`).
1813 pub cert: String,
1814 /// PEM private-key path (`PASSWAY_TLS_KEY`).
1815 pub key: String,
1816}
1817
1818impl TenantPasswayTls {
1819 /// The conventional per-domain pair under
1820 /// [`DEFAULT_TENANT_PASSWAY_CERT_DIR`]: `<dir>/<domain>/{tls.crt,tls.key}`.
1821 pub fn for_domain(domain: &str) -> Self {
1822 Self {
1823 cert: format!("{DEFAULT_TENANT_PASSWAY_CERT_DIR}/{domain}/tls.crt"),
1824 key: format!("{DEFAULT_TENANT_PASSWAY_CERT_DIR}/{domain}/tls.key"),
1825 }
1826 }
1827}
1828
1829impl TenantPasswayWorkload {
1830 /// A cold per-tenant passway for `domain` on `listen`, with the
1831 /// conventional cert paths and a one-minute idle TTL.
1832 pub fn cold(domain: impl Into<String>, listen: impl Into<String>) -> Self {
1833 let domain = domain.into();
1834 Self {
1835 tls: TenantPasswayTls::for_domain(&domain),
1836 domain,
1837 listen: listen.into(),
1838 upstreams: Vec::new(),
1839 idle_ttl: Some(Millis::from_secs(60)),
1840 command: None,
1841 env: BTreeMap::new(),
1842 discover: None,
1843 }
1844 }
1845
1846 /// Point this passway at `addrs` (bare `host:port`).
1847 pub fn with_upstreams<S: Into<String>>(mut self, addrs: impl IntoIterator<Item = S>) -> Self {
1848 self.upstreams = addrs.into_iter().map(Into::into).collect();
1849 self
1850 }
1851
1852 /// The passway binary this workload forks.
1853 pub fn command_path(&self) -> &str {
1854 self.command.as_deref().unwrap_or(DEFAULT_PASSWAY_COMMAND)
1855 }
1856
1857 /// `PASSWAY_IDLE_TTL_SECS`, or `None` for "never reap".
1858 ///
1859 /// Rounds **up** to one second, for the reason the bundle JIT path rounds
1860 /// up: passway reads this as an integer number of seconds, so a 500 ms TTL
1861 /// would truncate to `0` — and `0` there does not mean "reap immediately",
1862 /// it means the reap never fires. Rounding down would turn a declared cold
1863 /// workload resident without any error to read.
1864 pub fn idle_ttl_secs(&self) -> Option<u64> {
1865 self.idle_ttl.map(|t| t.as_ms().div_ceil(1000).max(1))
1866 }
1867
1868 /// `PASSWAY_UPSTREAMS` for this domain: `<domain>=<addr>` per backend,
1869 /// comma-joined. Empty when no backend is declared, which passway reads as
1870 /// "fail ready with 503".
1871 pub fn passway_upstreams(&self) -> String {
1872 self.upstreams
1873 .iter()
1874 .map(|a| format!("{}={}", self.domain, a))
1875 .collect::<Vec<_>>()
1876 .join(",")
1877 }
1878
1879 /// The [`WorkloadSpec`] kamaji's JIT runtime forks for this tenant.
1880 ///
1881 /// `id` is the kamaji workload identity (also the mesh ident and the
1882 /// custodian key). Everything else is derived from `self` — see the type
1883 /// doc for why no caller is allowed to restate `PASSWAY_LISTEN`.
1884 ///
1885 /// - `entrypoint` is the passway binary; `command` is empty, because passway
1886 /// is configured entirely by environment (it has no config-file parser).
1887 /// - `restart_policy` is [`RestartPolicy::Never`]: the JIT supervisor owns
1888 /// re-forking on the next connection, and an idle self-reap is an expected
1889 /// exit, not a crash.
1890 /// - `expose.mesh.ports` is parsed back off [`listen`](Self::listen) rather
1891 /// than carried separately, so the declared port cannot drift from the
1892 /// bound one.
1893 /// - `LISTEN_FDS=1` is set here as well as by the JIT supervisor. That is
1894 /// deliberate redundancy, not a duplicate: it makes the spec truthful
1895 /// about how this process expects to get its socket to anyone reading the
1896 /// spec alone, and setting it twice to the same value is inert.
1897 pub fn jit_spec(&self, id: &str) -> WorkloadSpec {
1898 let mut env: BTreeMap<String, String> = self.env.clone();
1899 // Derived keys go last: an `env` escape hatch must not be able to break
1900 // the fd handoff (see the field doc).
1901 env.insert("PASSWAY_LISTEN".into(), self.listen.clone());
1902 env.insert("LISTEN_FDS".into(), "1".into());
1903 env.insert("PASSWAY_TLS_MODE".into(), "manual".into());
1904 env.insert("PASSWAY_TLS_CERT".into(), self.tls.cert.clone());
1905 env.insert("PASSWAY_TLS_KEY".into(), self.tls.key.clone());
1906 env.insert("PASSWAY_UPSTREAMS".into(), self.passway_upstreams());
1907 match &self.discover {
1908 // R910-F2: poll yubaba for the fronted workload's records. The
1909 // URLs carry no `<hostname>=` prefix — this door serves exactly
1910 // one domain, so the catch-all form is the whole instruction.
1911 Some(d) => {
1912 env.insert("PASSWAY_UPSTREAM_SOURCE".into(), "yubaba".into());
1913 env.insert("PASSWAY_YUBABA_URL".into(), d.urls.join(","));
1914 env.insert(
1915 "PASSWAY_YUBABA_IDENT".into(),
1916 format!("{}={}", self.domain, d.ident),
1917 );
1918 }
1919 None => {
1920 env.insert("PASSWAY_UPSTREAM_SOURCE".into(), "static".into());
1921 env.remove("PASSWAY_YUBABA_URL");
1922 env.remove("PASSWAY_YUBABA_IDENT");
1923 }
1924 }
1925 match self.idle_ttl_secs() {
1926 Some(secs) => {
1927 env.insert("PASSWAY_IDLE_TTL_SECS".into(), secs.to_string());
1928 }
1929 // Unset, not `0` — passway reads an absent variable as "never
1930 // reap", and `0` as a zero-second timer that fires immediately.
1931 None => {
1932 env.remove("PASSWAY_IDLE_TTL_SECS");
1933 }
1934 }
1935
1936 WorkloadSpec {
1937 name: id.to_string(),
1938 image: ImageRef {
1939 // Identity metadata only — the JIT tier forks a node binary and
1940 // pulls nothing, exactly like the bundle-serving native path.
1941 registry: "passway".into(),
1942 repository: format!("tenant/{}", self.domain),
1943 tag: "jit".into(),
1944 digest: "sha256:0000000000000000000000000000000000000000000000000000000000000000"
1945 .into(),
1946 },
1947 tier: TierTag("infra".into()),
1948 tenant: TenantId::singleton(),
1949 namespace: NamespaceId::singleton(),
1950 replicas: 1,
1951 entrypoint: Some(vec![self.command_path().to_string()]),
1952 command: Some(vec![]),
1953 workdir: None,
1954 user: None,
1955 env: env
1956 .into_iter()
1957 .map(|(name, value)| EnvVar {
1958 name,
1959 value: EnvValue::Literal { value },
1960 })
1961 .collect(),
1962 secrets: vec![],
1963 volumes: vec![],
1964 resources: ResourceLimits {
1965 memory_mb: 64,
1966 cpu_millis: 256,
1967 memory_request_mb: None,
1968 cpu_limit_millis: None,
1969 pids_max: None,
1970 scratch_floor_mb: None,
1971 },
1972 depends_on: vec![],
1973 requires: vec![],
1974 // No probe: a `TcpConnect` probe would dial the held socket and
1975 // fork the process on every interval, defeating the idle reap. The
1976 // JIT bundle path refuses one for the same reason.
1977 healthcheck: None,
1978 restart_policy: RestartPolicy::Never,
1979 archetype: None,
1980 stop_policy: StopPolicy {
1981 signal: 15,
1982 grace_period: Millis::from_secs(5),
1983 },
1984 expose: ExposeSpec {
1985 mesh: MeshExpose {
1986 identity: MeshIdent(id.to_string()),
1987 ports: MeshExpose::anonymous_ports(self.listen_port()),
1988 allow_from: vec![],
1989 },
1990 public: None,
1991 operator: None,
1992 },
1993 labels: Default::default(),
1994 durability: None,
1995 annotations: Default::default(),
1996 files: Vec::new(),
1997 }
1998 }
1999
2000 /// Port half of [`listen`](Self::listen), when it parses.
2001 pub fn listen_port(&self) -> Option<u16> {
2002 self.listen
2003 .rsplit_once(':')
2004 .and_then(|(_, p)| p.parse::<u16>().ok())
2005 }
2006}
2007
2008/// Build step that produces the static artifact published by a
2009/// `mesofact-static` workload.
2010///
2011/// **`deny_unknown_fields` is load-bearing (R658-B1).** TOML scopes every key
2012/// written after a table header into that table, so a manifest that puts a
2013/// top-level `MesofactStaticWorkload` field — `routes` was the one that
2014/// actually happened — below `[build]` silently produces `build.routes`
2015/// instead. Without this attribute serde discards the stray key, the
2016/// top-level field falls back to its default (or fails with a `missing field`
2017/// error pointing at the wrong place), and the manifest deploys with a
2018/// declaration nobody honours. Every real `workload.toml` in the camp and the
2019/// CLI's own `yah cloud site init` scaffold carried exactly that shape for
2020/// months without a single reader noticing.
2021///
2022/// The cost is forward-compat: a manifest carrying a `[build]` key this binary
2023/// doesn't know is a hard parse error, not an ignored key. That is deliberate.
2024/// A build config is a small, slow-moving, load-bearing table — a key that
2025/// silently does nothing is worse here than one that refuses to load, because
2026/// the failure surfaces as a wrong artifact rather than an error.
2027///
2028/// Note `deny_unknown_fields` is inert for the postcard kamaji wire, which is
2029/// non-self-describing and positional — this only constrains TOML/JSON.
2030///
2031/// @yah:relay(R905, "Per-environment build override — a mirror cannot change what its component builds with")
2032/// @yah:status(review)
2033/// @yah:at(2026-09-14T20:25:20Z)
2034/// @yah:assignee(agent:bundle-anthropic-ashguard)
2035/// @yah:next("THE GAP, stated structurally. A `[build]` block lives on the COMPONENT's workload.toml (BuildConfig here: `command`, `out_dir`, `render_command` — nothing else), and a component is declared once in `.yah/services/<svc>/service.toml`. The MIRROR is the only per-environment surface, and `.yah/schema/mirror.toml.schema.json`'s top-level keys are exactly `asset_aliases, drivers, ingress, ingress_machines, providers, schema_version, shape` — no `build`, no `env`. So a service that deploys the SAME component to two environments has no way to build it differently for each, and `mesofact_static`'s build step passes no environment either (`run_build` -> `ExecContext::default().with_cwd()`).")
2036/// @yah:next("FIRST CONSUMER, CONFIRMED LIVE, NOT HYPOTHETICAL — noisetable's staging origin (noisetable camp R704-T4). `web/landing/workload.toml:39` hardcodes `command = \"bun run build:cloud\"`, and `build:cloud` is `NOISETABLE_API_ORIGIN=https://api.noisetable.com bun run build` (web/landing/package.json:17). A sibling `build:staging` pointing at `https://api-staging.noisetable.com` EXISTS at package.json:18 and `rg build:staging` over the whole repo returns exactly that one definition line — nothing can reference it, because there is nowhere to put the reference. Measured consequence: `curl -sS https://staging.noisetable.com/account` serves `\"api_base\":\"https://api.noisetable.com/api/v1\"`, production CORS allows only `https://noisetable.com`, so the staging account page's every API call is blocked and the page renders \"unreachable\".")
2037/// @yah:next("TWO SHAPES, both plausible; the ticket does not pick one. (a) An `env` table on BuildConfig plus a mirror-level override of it — most direct, but puts per-env data on a per-component struct. (b) A `[build]` override block on the mirror that replaces `command` for that environment — keeps the environment axis where every other environment fact already lives (`providers`, `ingress`), at the cost of a second place a build command can come from. (b) is the one this filer leans toward, precisely because the mirror is ALREADY the per-environment surface and (a) invents a second one.")
2038/// @yah:next("DO NOT 'SOLVE' THIS CONSUMER-SIDE WITH HOSTNAME SNIFFING. The tempting workaround is deriving the API origin from `location.hostname` in the browser (staging.noisetable.com -> api-staging.noisetable.com). It was considered and rejected by the noisetable operator: it is one-off string-match control flow, and it converts a deploy-time fact into client-bundle logic that no build can validate. The absence of that workaround is why this ticket exists — do not close it by suggesting one.")
2039/// @yah:verify("`rg -n \"build:staging\" --glob '!node_modules'` inside the noisetable checkout returns MORE than the single package.json definition line — i.e. something now references it. Today it returns exactly one hit, which is the whole defect.")
2040/// @yah:verify("`curl -sS https://staging.noisetable.com/account | grep -o '\"api_base\":\"[^\"]*\"'` prints `https://api-staging.noisetable.com/api/v1`. It printed `https://api.noisetable.com/api/v1` on 2026-09-13 when this was filed.")
2041/// @yah:gotcha("THE NOISETABLE SIDE IS NOT ALL OF THIS TICKET'S BLAST RADIUS, and shipping the override does not by itself fix that page. `api-staging.noisetable.com` currently resolves and serves 200 on `/health` from the PRODUCTION passway edge, whose `upstreams_by_host` lists `api.noisetable.com` and `*` with no `api-staging` entry — so a staging bundle pointed at it would 404 until noisetable R704-T4's own door (a cloudflare-tunnel on the NAT'd dev node us-west-011) is up. The two are independent and both are required; neither is a reason to defer the other.")
2042/// @yah:handoff("SHAPE (b) SHIPPED, widened by an env table. MirrorConfig.build: BTreeMap<component id, MirrorBuildOverride{command, render_command, env}> (oss/yubaba/crates/cloud/src/config.rs). Field-wise override of the workload's [build]; out_dir deliberately not overridable. Wired into BOTH build paths: MesofactStaticReconciler::rebuild_static/revalidate_static (env via ExecContext::with_env) and the bundle tier in app/yah/cli/src/cloud.rs (effective_build_command + spawn_component_build shared by single- and multi-component assembly; deploy_mesofact_bundle's provenance/refusal resolve through it too). noisetable staging uses providers.bundle, so the CLI path is the one its first consumer hits. cross_ref_validate refuses [build.<id>] naming an undeclared component. mirror.toml.schema.json regenerated.")
2043/// @yah:handoff("LANDED (yah side). Mirrors take `[build.<component id>]` with `command`, `render_command` and `env` (MirrorBuildOverride, oss/yubaba/crates/cloud/src/config.rs). Each field overrides the matching field of the workload's [build]; `out_dir` cannot be overridden. Honoured by BOTH build paths: MesofactStaticReconciler::rebuild_static/revalidate_static (env via ExecContext::with_env) and the bundle tier (app/yah/cli/src/cloud.rs effective_build_command + spawn_component_build, shared by single- and multi-component assembly and by deploy_mesofact_bundle's provenance/refusal). cross_ref_validate refuses a key naming an undeclared component. Schema regenerated; it was swept into peer commit 30c2c02c.")
2044/// @yah:verify("cd oss/yubaba && cargo test -p yah-cloud --lib -- reconciler::mesofact_static build_override → 56 + 5 pass, including rebuild_static_runs_the_mirrors_overridden_command_with_its_env, revalidate_static_honours_the_mirrors_render_override_and_env, and cloud_config_cross_ref_fails_on_a_build_override_for_an_unknown_component")
2045/// @yah:gotcha("An OLD yah binary silently ignores `[build.<id>]`, because MirrorConfig has no deny_unknown_fields. Install a yah that includes R905 before relying on the override, or staging keeps building with build:cloud and nothing errors.")
2046/// @yah:assumes("The noisetable consumer edit is noisetable-camp work (R704-T4), not done here. Add `[build.site] command = \"bun run build:staging\"` (or `[build.site.env] NOISETABLE_API_ORIGIN = ...`) to .yah/services/noisetable-marketing/mirrors/staging.toml, then apply. The two filing-time verify lines (rg build:staging, curl api_base) are only satisfiable after that step and after R704-T4's api-staging door is up.")
2047/// @yah:handoff("NOISETABLE LOCKSTEP (operator-authorized 2026-09-14): (1) noisetable .yah/services/noisetable-marketing/mirrors/staging.toml gains `[build.site] command = \"bun run build:staging\"` with the why + old-binary warning inline. (2) web/landing/workload.toml's comment claiming the reconciler passes no build env replaced — it now says build:cloud is production's default and staging overrides it from its mirror. (3) noisetable .yah/schema/mirror.toml.schema.json refreshed from yah's regenerated copy (+35 lines). external/yah there is a symlink to this monorepo, so the mesofact-build side needs no separate bump.")
2048/// @yah:gotcha("~/.local/bin/yah (0.8.39+e0530813-dirty) does NOT carry R905 (strings check for the new cross_ref_validate message: 0 hits). Until an R905 yah is installed, noisetable's `[build.site]` is silently ignored and staging still builds with build:cloud. Also: the filing-time verify `rg build:staging` skips hidden dirs, so it never sees .yah/ — its hits come from web/landing/workload.toml's comment, not from the mirror itself.")
2049/// @yah:verify("Against the REAL noisetable tree with an R905 build (target/debug/yah, 2026-09-14 13:33): `yah cloud validate --path ~/ss/noisetable` → ok. Negative: the same .yah copied to /tmp with the key changed to `[build.sight]` → `Error: loading workspace declarations: services/noisetable-marketing/mirrors/staging.toml: [build.sight] — no component \"sight\" in services/noisetable-marketing/service.toml (declared: [\"site\", \"app\"])`. CLI: cargo test -p yah --lib (6 bundle_assembly_tests incl. a_mirror_build_override_decides_what_a_component_builds_with) pass.")
2050#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2051#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2052#[serde(deny_unknown_fields)]
2053pub struct BuildConfig {
2054 /// Shell command run from the manifest's directory, e.g. `"bun run build"`.
2055 ///
2056 /// **Absent means "this project has no external bundler step" (R838-B1)**,
2057 /// not "run nothing by accident". `mesofact new`'s scaffold deliberately
2058 /// omits it — the in-process pipeline (`mesofact-dev` / `mesofact-build`)
2059 /// produces `out_dir` with no third binary, no package manager and no Node
2060 /// — so requiring it here made every scaffolded project's manifest fail to
2061 /// load through this envelope. Setting it opts back out to a shell command,
2062 /// which is what a project with its own bundler wants.
2063 ///
2064 /// Consumers were already written for this: `read_workload_build`
2065 /// (app/yah/cli/src/cloud.rs) has always typed it `Option<String>` and
2066 /// `yah cloud bundle build` only needs it under `--run-build`; the bundle
2067 /// sync arm refuses `None` by name. `MesofactStaticReconciler::
2068 /// rebuild_static` skips the build step for `None` — the same thing it
2069 /// already did for a workload with no `workload.toml` at all.
2070 ///
2071 /// WIRE NOTE: this is `Option<String>` on the postcard kamaji wire, so it
2072 /// costs a leading `0x00`/`0x01` tag byte that the bare `String` did not
2073 /// have. A pre-R838 node decoding a new frame fails loudly (the string's
2074 /// length byte is not a valid `Option` tag) rather than silently reading a
2075 /// shifted field — which is why this is `Option` and not a `#[serde(default)]`
2076 /// empty `String` sentinel. Not a `cluster_epochs` surface: those hash the
2077 /// raft modules and the openraft pin, not `workload_spec`.
2078 #[serde(default)]
2079 pub command: Option<String>,
2080
2081 /// Output directory (relative to the manifest) the reconciler uploads.
2082 pub out_dir: PathBuf,
2083
2084 /// Data-only re-render command (W225 §3 "revalidate"), run from the
2085 /// manifest's directory against the **already-built** `out_dir` — no
2086 /// bundler. `{route}` is substituted with the invalidated route pattern,
2087 /// e.g. `"../../../../scripts/mesofact-build.sh render . --route {route}
2088 /// --all"` (R746-F9 — resolves a prebuilt binary rather than shelling to
2089 /// cargo, which cannot even find the package from a site's own dir).
2090 /// Absent → a revalidate dispatch republishes `out_dir` as-is.
2091 #[serde(default)]
2092 pub render_command: Option<String>,
2093}
2094
2095// ── BuildMode ─────────────────────────────────────────────────────────────────
2096
2097/// Where the build command runs for a `mesofact-static` workload.
2098///
2099/// The two-role split encodes the F7 design decision: build/publish is a
2100/// **transient job** (runs once, exits, GC'd); SSR/SPA serving is a separate
2101/// **long-lived companion container** (optional, only for dynamic pages). A
2102/// single merged "mesofact container" is the trap — in cloud, CI builds the
2103/// artifact, R2+CDN serve it, and a distinct worker handles any SSR.
2104///
2105/// Default: `HostSide` — mesofact-dev runs the build on the host and publishes
2106/// to the tier's object store. No container overhead; compatible with dev and
2107/// sim tiers.
2108#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, TS)]
2109#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2110#[serde(rename_all = "snake_case")]
2111pub enum BuildMode {
2112 /// Build command runs on the host (mesofact-dev watcher). The watcher
2113 /// publishes the output to the tier's object store (DistPointer for dev,
2114 /// MinIO for sim). Compatible with all tiers; zero container overhead.
2115 #[default]
2116 HostSide,
2117
2118 /// Build runs inside a transient container matching the CI image. Higher
2119 /// fidelity (environment matches CI exactly); costs image pull +
2120 /// container cold-start. Required for cloud/HA where no mesofact-dev
2121 /// watcher is running on the host.
2122 InContainer {
2123 /// Container image that runs the build (e.g. `"ghcr.io/org/app-build:v1.2"`).
2124 /// Must have the build toolchain installed. The container is started with
2125 /// the workspace root bind-mounted, runs `build.command`, uploads
2126 /// `build.out_dir` to the object store, then exits.
2127 image: ImageRef,
2128 },
2129}
2130
2131// ── AlmanacManifest ───────────────────────────────────────────────────────────
2132
2133/// An observable endpoint the almanac scheduler probes to check readiness.
2134///
2135/// Used for both inputs (checked before the run) and outputs (verified after
2136/// a successful run to confirm the job produced something reachable).
2137/// The probe is intentionally lightweight — no S3 SigV4, no xlb-net discovery
2138/// required; a simple TCP connect or HTTP GET is enough for the dev/sim tier.
2139#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2140#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2141#[serde(rename_all = "snake_case")]
2142pub enum AlmanacTarget {
2143 /// Issue an HTTP GET to `url`; ready when the server responds with
2144 /// `expect_status` (default: any 2xx).
2145 Http {
2146 url: String,
2147 #[ts(optional = nullable)]
2148 expect_status: Option<u16>,
2149 },
2150
2151 /// Establish a TCP connection to `host:port`; ready when the connect
2152 /// succeeds. Used for non-HTTP services (e.g. MinIO API on port 9000)
2153 /// and as a lighter probe when an HTTP endpoint isn't stable yet.
2154 Tcp { host: String, port: u16 },
2155}
2156
2157/// What the almanac scheduler does when a precondition check fails.
2158///
2159/// The F9 design decision: `WaitWithTimeout` is the default. Fail-fast is
2160/// too brittle for the sim tier (containers may still be cold-starting);
2161/// requeue-with-no-ceiling can block the scheduler indefinitely. The
2162/// recommended timeout for sim is the container spinup budget (~5 s cold,
2163/// ~1 s warm): set `timeout` to a few seconds, then let the retry cadence
2164/// handle transient glitches.
2165#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2166#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2167#[serde(rename_all = "snake_case")]
2168pub enum NotReadyPolicy {
2169 /// Wait up to `timeout` for all preconditions to pass before aborting
2170 /// the run. The run is skipped (not rescheduled); the next cadence tick
2171 /// will retry. Suitable when targets occasionally lag at startup.
2172 WaitWithTimeout {
2173 /// How long to wait for each precondition to become reachable. The
2174 /// scheduler polls with a short sleep between attempts.
2175 timeout: Millis,
2176 },
2177
2178 /// Abort immediately if any precondition check fails. Suitable for
2179 /// integration-test harnesses where a missing dependency is always a
2180 /// hard error.
2181 FailFast,
2182
2183 /// Requeue with exponential backoff up to `max_attempts` times. After
2184 /// exhaustion the run is marked failed. Suitable for cloud/HA where
2185 /// transient dependency outages are expected.
2186 Requeue {
2187 /// Maximum number of requeue attempts before the run is marked failed.
2188 max_attempts: u32,
2189 /// Initial backoff between attempts, in milliseconds.
2190 backoff: Millis,
2191 },
2192}
2193
2194impl Default for NotReadyPolicy {
2195 /// Default is `WaitWithTimeout { timeout: 5 seconds }` — matches the
2196 /// container spinup budget for the sim tier (few-second cold, sub-second warm).
2197 fn default() -> Self {
2198 Self::WaitWithTimeout { timeout: Millis::from_secs(5) }
2199 }
2200}
2201
2202/// When the almanac scheduler triggers a run.
2203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2204#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2205#[serde(rename_all = "snake_case")]
2206pub enum Cadence {
2207 /// Run once at first opportunity, then never again.
2208 Once,
2209
2210 /// Run repeatedly with a fixed interval between the end of one run and
2211 /// the start of the next. Equivalent to `sleep N && run` in a loop.
2212 Every {
2213 /// Minimum time between consecutive run completions.
2214 interval: Millis,
2215 },
2216
2217 /// Run on a UTC cron schedule (standard 5-field expression, e.g.
2218 /// `"0 */6 * * *"` for every 6 hours). The scheduler evaluates the
2219 /// expression relative to UTC midnight.
2220 Cron { expression: String },
2221}
2222
2223/// `kind = "almanac"` manifest — a declared data-pipeline job.
2224///
2225/// An almanac job is the generalisation of the OpenRouter refresher
2226/// (`spawn_almanac_refresher`): it declares its I/O contract explicitly so
2227/// the orchestrator can enforce preconditions before each run and verify
2228/// outputs afterward. The degenerate case (no inputs, no app target, cron
2229/// schedule) is exactly the OpenRouter JSON-cache refresher.
2230///
2231/// Lifecycle:
2232/// 1. Cadence tick fires.
2233/// 2. Scheduler probes every `inputs` target. If any fail → apply
2234/// `not_ready_policy`.
2235/// 3. Command runs (`sh -c command` from the workload directory).
2236/// 4. Scheduler probes every `outputs` target. Failure → mark run as
2237/// failed but do not retry.
2238/// 5. Any workloads listed in `invalidates` receive a cache-bust signal
2239/// (implementation detail of the orchestrator; in camp this is a
2240/// rebuild trigger on the mesofact-dev watcher).
2241#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2242#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2243pub struct AlmanacManifest {
2244 /// Shell command executed via `sh -c` from the workload directory.
2245 pub command: String,
2246
2247 /// When to run.
2248 pub cadence: Cadence,
2249
2250 /// Input targets that must be reachable before the command runs.
2251 /// Empty list → no precondition checks (degenerate case).
2252 #[serde(default)]
2253 pub inputs: Vec<AlmanacTarget>,
2254
2255 /// Output targets verified after a successful run.
2256 /// Empty list → no post-run verification.
2257 #[serde(default)]
2258 pub outputs: Vec<AlmanacTarget>,
2259
2260 /// What to do when a precondition check fails.
2261 /// Default: `WaitWithTimeout { timeout: 5000ms }`.
2262 #[serde(default)]
2263 pub not_ready_policy: NotReadyPolicy,
2264
2265 /// Mesh identities of workloads to notify after a successful run.
2266 /// The orchestrator sends a cache-bust signal to each entry so
2267 /// downstream consumers can reload their data (e.g. mesofact-dev
2268 /// triggers a rebuild when the OpenRouter cache refreshes).
2269 /// Empty list → no downstream invalidation.
2270 #[serde(default)]
2271 pub invalidates: Vec<MeshIdent>,
2272}
2273
2274// ── StaticAssetWorkload ───────────────────────────────────────────────────────
2275
2276/// BLAKE3 content hash expressed as exactly 64 ASCII hex digits.
2277///
2278/// This is the content-address key for every file in the static-asset catalog.
2279/// Deserialization rejects values that do not conform — 64 hex chars, case
2280/// insensitive. Mismatch between the recorded hash and the source file halts
2281/// the upload step in the reconciler.
2282#[derive(Debug, Clone, PartialEq, Eq, Serialize, TS)]
2283#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2284#[ts(type = "string")]
2285pub struct BlakeHash(pub String);
2286
2287impl<'de> Deserialize<'de> for BlakeHash {
2288 fn deserialize<D>(de: D) -> Result<Self, D::Error>
2289 where
2290 D: serde::Deserializer<'de>,
2291 {
2292 let s = String::deserialize(de)?;
2293 if s.len() != 64 || !s.bytes().all(|b| b.is_ascii_hexdigit()) {
2294 return Err(serde::de::Error::custom(format!(
2295 "blake3 hash must be exactly 64 hex digits, got {:?}",
2296 s
2297 )));
2298 }
2299 Ok(BlakeHash(s))
2300 }
2301}
2302
2303// ── License & FetchSource (W164) ──────────────────────────────────────────────
2304
2305/// Closed-set, parse-time-enforced license tag. Mirrors the workspace
2306/// permissive-license rule (MIT / Apache-2.0 / BSD-2/3-Clause / ISC). Adding a
2307/// variant is an explicit schema change — non-permissive strings
2308/// (`"GPL-3.0"`, `"AGPL"`, etc.) fail at serde-deserialize before any shape
2309/// validator runs.
2310///
2311/// Shared between `asset.derive.fetch.license` (W164, required) and a future
2312/// `almanac::ReleaseSource.license` migration (R438-F10, optional).
2313#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2314#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2315#[serde(rename_all = "kebab-case")]
2316pub enum License {
2317 Mit,
2318 Apache2,
2319 Bsd2Clause,
2320 Bsd3Clause,
2321 Isc,
2322}
2323
2324/// Shared fetch primitive — usable by `asset.derive` today, and by Almanac's
2325/// `ReleaseSource` after a follow-up migration (R438-F10). Defined once in
2326/// workload-spec so both consumers reject the same set of non-permissive
2327/// licenses.
2328///
2329/// The `blake3` hash pins the upstream bytes; mismatch at fetch time is a hard
2330/// error in the reconciler. The `license` field is **required** here — every
2331/// derived asset must declare its upstream license. If/when Almanac adopts
2332/// `FetchSource`, the Almanac side may wrap this in a struct with
2333/// `Option<License>` since release manifests have no distribution license per
2334/// se.
2335#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2336#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2337pub struct FetchSource {
2338 /// Upstream URL fetched verbatim. Reconciler retry policy is configured
2339 /// elsewhere (R438-F11); the URL itself is opaque to workload-spec.
2340 pub url: String,
2341
2342 /// Expected BLAKE3 hash of the fetched bytes (64 hex characters). The
2343 /// reconciler verifies this after download and aborts on mismatch.
2344 pub blake3: BlakeHash,
2345
2346 /// Upstream license. Closed-set, parse-time enforced.
2347 pub license: License,
2348}
2349
2350/// Optional transform applied after a [`FetchSource`] download, lowering to a
2351/// `ForgeCommand::Subprocess` via the recipe loader (R438-T4). The transform's
2352/// output is content-addressed by the entry's `blake3` (the recipe runs only
2353/// when the cache misses).
2354#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2355#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2356pub struct TransformSpec {
2357 /// Named recipe under `.yah/qed/transforms/<recipe>.toml`. Loader rejects
2358 /// missing recipes at materialize time.
2359 pub recipe: String,
2360
2361 /// `{{key}}` substitutions passed to the recipe argv at element
2362 /// granularity (no shell, no string concat). Empty when the recipe is
2363 /// fully parameterless.
2364 #[serde(default)]
2365 pub params: BTreeMap<String, String>,
2366}
2367
2368/// W212/R518: the committed derivation lock — the in-tree action-cache
2369/// receipt. `input_hash` is the input-addressed derivation key computed over
2370/// the complete declared input set (fetched-input pin ⊕ recipe-file bytes ⊕
2371/// invocation params ⊕ schema version); `output_blake3` is what those inputs
2372/// produced (== the entry's `blake3`). The reconciler skips the entire build
2373/// (no fetch, no transform, no PUT) when the lock matches the inputs recomputed
2374/// from the current pins and the bucket already holds the output — the
2375/// Nix-substituter / Bazel-remote-cache behaviour. Written by the R510 bind
2376/// path from the reconciler's `discovered_input_hash:<filename>` output; the
2377/// `git diff` on this block is the receipt that the derivation rolled.
2378#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2379#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2380pub struct DeriveLock {
2381 /// Input-addressed derivation key (BLAKE3 hex). A change to any declared
2382 /// input flips this, so a stale lock never produces a false skip.
2383 pub input_hash: String,
2384 /// Output the locked inputs produced (BLAKE3 hex; equals the entry's
2385 /// `blake3`). Carried so the lock is a self-contained action-cache entry.
2386 pub output_blake3: String,
2387}
2388
2389/// Provenance chain for a derived asset: required `fetch` step, optional
2390/// `transform` step. Materialized bytes replace `AssetEntry.source` for the
2391/// rest of the static-asset reconcile loop.
2392#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2393#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2394pub struct AssetDerive {
2395 /// Upstream fetch — URL + content-pin + license.
2396 pub fetch: FetchSource,
2397
2398 /// Post-fetch transform. `None` → the fetched bytes ARE the asset
2399 /// (entry `blake3` must match fetch `blake3`).
2400 #[serde(default)]
2401 #[ts(optional = nullable)]
2402 pub transform: Option<TransformSpec>,
2403
2404 /// W212/R518: committed derivation lock (input-addressed action-cache
2405 /// receipt). Absent until the first successful build writes it via the
2406 /// bind path. When present and current, enables the substituter-style
2407 /// build skip.
2408 #[serde(default)]
2409 #[ts(optional = nullable)]
2410 pub lock: Option<DeriveLock>,
2411}
2412
2413/// A single file entry in the static-asset catalog.
2414///
2415/// One `[[asset]]` row per bucket object. Multiple rows for different variants
2416/// (e.g. q5 and q4 whisper models) are fine — each declares its own filename
2417/// and hash. The reconciler treats the catalog as exhaustive and append-only:
2418/// new rows trigger a PUT; removed rows surface as drift (never a DELETE).
2419///
2420/// **Source-vs-derive XOR.** Exactly one of `source` or `derive` must be set.
2421/// Legacy local-bytes assets keep `source = "..."`; W164 derived assets set
2422/// `[asset.derive]` instead. [`validate::shape_static_asset`] enforces the
2423/// XOR; both-set and neither-set are hard `ShapeError::Field`.
2424#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2425#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2426pub struct AssetEntry {
2427 /// Destination path within the bucket, e.g.
2428 /// `"whisper/distil-large-v3-q5_1.bin"`. Must be unique in the catalog.
2429 /// Used as the S3 object key by the reconciler.
2430 pub filename: String,
2431
2432 /// Path to a local source file, relative to the `workload.toml` directory.
2433 /// Mutually exclusive with `derive`.
2434 #[serde(default)]
2435 #[ts(optional = nullable)]
2436 pub source: Option<PathBuf>,
2437
2438 /// Declared fetch (+ optional transform) provenance chain. The reconciler
2439 /// materializes the bytes into a content-addressed cache; the cache path
2440 /// then replaces `source` for the rest of the upload pipeline. Mutually
2441 /// exclusive with `source`.
2442 #[serde(default)]
2443 #[ts(optional = nullable)]
2444 pub derive: Option<AssetDerive>,
2445
2446 /// Expected BLAKE3 hash of the *final* asset bytes (64 hex characters).
2447 /// For `source` mode, this is hashed before upload. For `derive` mode,
2448 /// it's the post-transform (or post-fetch when no transform) output.
2449 /// Mismatch aborts the upload.
2450 pub blake3: BlakeHash,
2451}
2452
2453/// `kind = "static-asset"` payload — content-addressed bucket catalog.
2454///
2455/// The reconciler makes the bucket match the `[[asset]]` list exactly
2456/// (append-only: new rows → PUT; removed rows → drift report, not DELETE).
2457/// Rollback is pointer-flip via `mirror.toml [asset_aliases]` — bytes never
2458/// move during rollback.
2459///
2460/// **Closed-catalog invariant**: every value in `[aliases]` must be a
2461/// `filename` that exists in `[[asset]]`. Enforced by
2462/// [`validate::shape_static_asset`]. Mirror overrides (`[asset_aliases]` in
2463/// `mirror.toml`) are bound by the same rule — the alias graph can only
2464/// resolve to filenames already in the catalog.
2465#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2466#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2467pub struct StaticAssetWorkload {
2468 /// Exhaustive catalog of files this component manages in the bucket.
2469 ///
2470 /// Named `asset` on disk (TOML `[[asset]]` array-of-tables) to follow TOML
2471 /// convention; accessed as `.assets` in Rust code.
2472 #[serde(rename = "asset", default)]
2473 pub assets: Vec<AssetEntry>,
2474
2475 /// Canonical logical-name → filename mappings for this component.
2476 ///
2477 /// Values must be filenames present in `assets` — validated by
2478 /// [`validate::shape_static_asset`]. Mirror files may override individual
2479 /// entries via `[asset_aliases]` but may never reference filenames absent
2480 /// from this catalog.
2481 #[serde(default)]
2482 pub aliases: BTreeMap<String, String>,
2483}
2484
2485// ── Lifecycle archetype (R572-F1 / W244) ───────────────────────────────────────
2486
2487/// Explicit lifecycle archetype for a `kind = "container"` workload (W244).
2488///
2489/// The question that actually matters to a scheduler: *"can I kill this and
2490/// recreate it somewhere else?"* Before this field existed, the answer was
2491/// inferred per-spec from `volumes.is_empty()` + `restart_policy` — fragile
2492/// absence-as-policy, the same trap W243 calls out on the node-taint side.
2493/// This type makes the answer structural instead of guessed.
2494///
2495/// This ticket (R572-F1) adds the discriminator only. The reconciler does not
2496/// yet branch on it (R572-F4) and neither does the scheduler (R572-F5).
2497#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2498#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2499#[serde(rename_all = "kebab-case")]
2500pub enum LifecycleArchetype {
2501 /// k8s analogue: Deployment. Stateless and fungible — the scheduler may
2502 /// move it, scale it to N replicas, or restart it on a different node
2503 /// with zero consequence. Drainable.
2504 Server,
2505
2506 /// k8s analogue: StatefulSet. Stable identity + a volume that must
2507 /// follow it; at most one live instance. Not drainable — the reconciler
2508 /// must not schedule it onto a different node. Example: a postgres peer,
2509 /// headscale (W267/R591).
2510 Appliance,
2511
2512 /// k8s analogue: Job. Runs to completion with declared inputs/outputs,
2513 /// then is gone — no steady-state identity. `almanac` is the first
2514 /// job-family member; forge runs (`WorkloadSpec::for_forge`, used by QED)
2515 /// are the `container`-kind instance of this archetype.
2516 Job,
2517}
2518
2519impl LifecycleArchetype {
2520 /// Every variant, in declaration order. Exists so a consumer can enumerate
2521 /// the archetypes without hand-maintaining a parallel list — the taint
2522 /// vocabulary in `cloud::config::taint_effect` is built from this, so
2523 /// adding a fourth archetype extends the set of live repel keys for free.
2524 pub const ALL: [LifecycleArchetype; 3] = [Self::Server, Self::Appliance, Self::Job];
2525
2526 /// The repel-taint key for this archetype (R572-F5). A node carrying the
2527 /// taint `"no-<key>"` **absolutely** rejects workloads of this class.
2528 ///
2529 /// Examples: `Server` → `"server"` (repelled by `"no-server"`);
2530 /// `Appliance` → `"appliance"` (repelled by `"no-appliance"`).
2531 ///
2532 /// W305/R742-T4: there is no toleration. Earlier prose here and in
2533 /// `cloud::config` called this "repel-unless-tolerate"; the `unless` was
2534 /// never built, and reading it as a preference is what made `no-appliance`
2535 /// on the dev Pis look advisory when it was an unconditional block.
2536 pub fn taint_key(&self) -> &'static str {
2537 match self {
2538 Self::Server => "server",
2539 Self::Appliance => "appliance",
2540 Self::Job => "job",
2541 }
2542 }
2543
2544 /// The pre-R572 inference this field replaces, kept only to give
2545 /// `WorkloadSpec::effective_archetype` a behavior-preserving fallback for
2546 /// specs written before this field existed (`archetype: None`).
2547 ///
2548 /// A volume that must follow the workload is the strongest signal of
2549 /// durable state → [`Self::Appliance`]. Absent that, `RestartPolicy::Never`
2550 /// is the existing forge/run-once convention (see
2551 /// [`RestartPolicy::Never`]'s doc comment) → [`Self::Job`]. Everything
2552 /// else defaults to the common case, [`Self::Server`].
2553 fn infer(volumes: &[VolumeMount], restart_policy: &RestartPolicy) -> Self {
2554 if !volumes.is_empty() {
2555 LifecycleArchetype::Appliance
2556 } else if matches!(restart_policy, RestartPolicy::Never) {
2557 LifecycleArchetype::Job
2558 } else {
2559 LifecycleArchetype::Server
2560 }
2561 }
2562}
2563
2564/// Whether a placement group may be drained off its node (W338 §"Placement
2565/// consequences" 2): false as soon as **any** member is an Appliance.
2566///
2567/// The set-valued form of the per-workload question. A `Server` bound to an
2568/// Appliance by a `local` edge has to move with it or not at all, so draining it
2569/// alone breaks the group the same way placing it alone would.
2570///
2571/// # Why this lives here and not in `cloud`
2572///
2573/// R860-T4 landed it in `cloud::config`, which is the right layer for the
2574/// *scheduler* — but R860-T6 needs the identical predicate on the **node** side,
2575/// in `drain_workloads`, and yubaba deliberately has no runtime dependency on
2576/// cloud (R374-F3 moved `local-driver` out of cloud precisely to avoid that
2577/// reverse edge; `cloud` is a dev-dependency of yubaba only). Placement and
2578/// drain disagreeing about drainability is exactly the drift this predicate
2579/// exists to prevent, so it belongs in the crate they both already depend on.
2580/// `cloud::config::group_is_drainable` delegates here and keeps its signature.
2581pub fn group_is_drainable(members: &[WorkloadSpec]) -> bool {
2582 !members
2583 .iter()
2584 .any(|m| m.effective_archetype() == LifecycleArchetype::Appliance)
2585}
2586
2587// ── Requirements (R860-T1 / W338) ─────────────────────────────────────────────
2588
2589/// Which providers count as satisfying a [`Requirement`] (W338).
2590///
2591/// One of the two independent axes a requirement carries. `depends_on` could
2592/// only ever say "someone, somewhere, is Ready" — which is the wrong answer for
2593/// a provider that must open the *same file on the same filesystem* as its
2594/// requirer (the headscale sqlite replicator, W338's motivating case). Locality
2595/// makes co-location a declared property instead of something arranged outside
2596/// the spec by a systemd unit.
2597#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2598#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2599#[serde(rename_all = "kebab-case")]
2600pub enum Locality {
2601 /// Any Ready provider service discovery can reach, anywhere in the mesh.
2602 /// Exactly what a [`WorkloadSpec::depends_on`] entry means today, which is
2603 /// why it is the default — folding `depends_on` into `requires` must not
2604 /// change any existing spec's meaning.
2605 Anywhere,
2606
2607 /// A provider on this node satisfies it; otherwise a remote one does.
2608 ///
2609 /// **Never blocks placement.** This is the "at least one wherever this app
2610 /// runs" shape — a local replica is preferred, a remote one is acceptable,
2611 /// and nothing is refused for want of either.
2612 PreferLocal,
2613
2614 /// Only a provider on **this node** satisfies it. A true sidecar edge: the
2615 /// requirer and the provider form a placement group that must be placed
2616 /// together and must move together.
2617 Local,
2618}
2619
2620impl Default for Locality {
2621 fn default() -> Self {
2622 Locality::Anywhere
2623 }
2624}
2625
2626/// What to do when nothing satisfies a [`Requirement`] (W338).
2627///
2628/// The second axis, deliberately independent of [`Locality`]: all six
2629/// combinations are meaningful, and `prefer-local` + `self` is where a
2630/// DaemonSet falls out as a consequence rather than as a fourth archetype.
2631///
2632/// Kept a plain two-value enum rather than a data-carrying variant precisely so
2633/// the two axes stay independent — the provider's spec rides on
2634/// [`Requirement::provides`] instead.
2635#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2636#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2637#[serde(rename_all = "kebab-case")]
2638pub enum Supply {
2639 /// Someone else declares and deploys the provider; block until it appears,
2640 /// under the existing healthcheck-sum deadline. Today's `depends_on`
2641 /// behaviour, and the default.
2642 Wait,
2643
2644 /// This workload carries the provider's spec in [`Requirement::provides`]
2645 /// and stands one up where the locality demands. Torn down with its
2646 /// requirer.
2647 ///
2648 /// Wire value is `"self"` — `Self` is a Rust keyword, so the variant is
2649 /// spelled `SelfProvision` and renamed on the wire.
2650 #[serde(rename = "self")]
2651 SelfProvision,
2652}
2653
2654impl Default for Supply {
2655 fn default() -> Self {
2656 Supply::Wait
2657 }
2658}
2659
2660/// One thing a workload needs before it can run (W338).
2661///
2662/// Widens [`WorkloadSpec::depends_on`] rather than adding a second concept
2663/// beside it: a requirement names an identity and answers the two questions the
2664/// bare ident list cannot — *which providers count* ([`Locality`]) and *what to
2665/// do when none exists* ([`Supply`]).
2666///
2667/// Each member of a group keeps its own mesh identity. A provider that may be
2668/// satisfied remotely must be independently discoverable, so a requirement is
2669/// an *edge between two identities*, never a way to collapse several workloads
2670/// under one. Nothing about addressing, teardown-by-identity or the
2671/// service-record rail changes.
2672#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2673#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2674pub struct Requirement {
2675 /// Mesh identity of the provider. The same currency a
2676 /// [`WorkloadSpec::depends_on`] entry is written in.
2677 pub ident: MeshIdent,
2678
2679 /// Which providers count as satisfying this. Defaults to
2680 /// [`Locality::Anywhere`], the `depends_on` meaning.
2681 #[serde(default)]
2682 pub locality: Locality,
2683
2684 /// What to do when nothing satisfies it. Defaults to [`Supply::Wait`], the
2685 /// `depends_on` meaning.
2686 #[serde(default)]
2687 pub supply: Supply,
2688
2689 /// The provider's own spec, carried here when `supply = "self"`.
2690 ///
2691 /// Required for [`Supply::SelfProvision`] and forbidden for
2692 /// [`Supply::Wait`] — a `wait` requirement names a provider someone else
2693 /// declares, so a spec here would have no owner. Both directions are
2694 /// enforced by [`validate::shape`].
2695 ///
2696 /// Boxed because this makes [`WorkloadSpec`] recursive. The recursion is
2697 /// bounded at **depth 1**: a `provides` spec may not itself carry a
2698 /// `self`-supplied requirement (also enforced in [`validate::shape`]), so
2699 /// composition stays a requirer plus its immediate providers rather than an
2700 /// arbitrarily deep tree.
2701 #[serde(default)]
2702 #[ts(optional = nullable)]
2703 pub provides: Option<Box<WorkloadSpec>>,
2704}
2705
2706// ── WorkloadSpec ──────────────────────────────────────────────────────────────
2707
2708/// Complete typed description of a containerd workload handed to yubaba over
2709/// RPC. This is also the payload of the `kind = "container"` variant of
2710/// [`Workload`] on disk.
2711///
2712/// Yubaba never accepts compose YAML on its RPC surface — agents, the desktop,
2713/// and operator CLIs all hand yubaba `WorkloadSpec` values. See the arch doc
2714/// for the validation layers and evolution rules.
2715///
2716/// @yah:ticket(R860-T1, "Spec: Requirement { ident, locality, supply } + `requires` on WorkloadSpec, depends_on as back-compat projection")
2717/// @yah:status(review)
2718/// @yah:phase(P1)
2719/// @yah:at(2026-09-05T18:28:59Z)
2720/// @yah:assignee(agent:bundle-anthropic-ashguard)
2721/// @yah:parent(R860)
2722/// @yah:next("Regenerate the derived artifacts and commit them — they are generated, not owned (CLAUDE.md \\\"Generated artifacts do NOT regenerate on commit anymore\\\"): `cargo run -p xtask -- emit-schemas`, then `cargo run --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --bin export-ts`.")
2723/// @yah:verify("bash scripts/check-schema-drift.sh && bash scripts/check-workload-spec-ts.sh && cargo test -p workload-spec")
2724/// @yah:gotcha("Vocabulary ONLY — nothing reads `requires` yet. Deliberate, and it mirrors how `archetype` landed in R572-F1 (\\\"this field alone changes no runtime behavior\\\"). Enforcement is R860-T2 (deploy gate) and R860-T3 (placement group).")
2725/// @arch:see(.yah/docs/working/W338-workload-dependencies-and-appliance-composition.md)
2726/// @yah:gotcha("Adding `requires` to WorkloadSpec is NOT a one-file change in practice: a new struct field makes every `WorkloadSpec { .. }` literal in the tree an E0063, across all four workspaces (root, oss/yah-base, oss/kamaji, oss/yubaba). 22 call sites needed a mechanical `requires: vec![],`. One of them is `headscale_spec()` in oss/yubaba/crates/yubaba/src/headscale_appliance.rs, a file @Ashguard:eclipse (session:83093d9d) is live in on R858 — left it in rather than break the camp build, notified both channels (party.chat + @yah:notify_on on R858).")
2727/// @yah:gotcha("R860-T3 does not exist (board_show: \"ticket 'R860-T3' not found\"). The first gotcha's \"R860-T3 (placement group)\" is really R860-T4 (\"Admission: place the transitive closure of `local` edges as one group\"), and supply=self enforcement is R860-T6. The doc comments landed in lib.rs cite T2/T4/T6, not T3.")
2728/// @yah:verify("Baseline recorded BEFORE any edit (`cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml`): lib 156 passed / 0 failed, integration \"main\" 98 passed / 0 failed. NB `cargo test -p workload-spec` does NOT work — the package is `yah-workload-spec` and it lives in the excluded oss/yah-base workspace, so `-p` from the camp root fails with \"not a member of the workspace\". Use --manifest-path.")
2729/// @yah:handoff("Decision made without asking (brief said to decide and record): \"a `provides` spec's own name/mesh ident must match its Requirement::ident\" is enforced against `expose.mesh.identity`, NOT `name`. A requirement is written in mesh idents (same currency as depends_on) and the mesh identity is what makes the provider independently discoverable — W338's \"each member keeps its own mesh identity\". The error message still prints the provider's `name` so a mismatch is diagnosable from either side.")
2730/// @yah:handoff("Second decision: `tests/round_trip.rs::full_spec()` (\"every field family populated\") now populates `requires` with BOTH a bare prefer-local/wait entry and a local/self entry carrying a nested provider (new `sidecar_spec()` helper). That makes the three existing round-trip tests — JSON, postcard, and Workload::Container-over-postcard — carry the recursive `Option<Box<WorkloadSpec>>` rather than only the flat shape, which is the thing most likely to break silently on the kamaji UDS (cf. R590-B3).")
2731/// @yah:handoff("Third decision: `Locality`/`Supply` get hand-written `impl Default` rather than `#[derive(Default)]` + `#[default]`. Three derive macros (TS, JsonSchema, Serialize) sit on the same item and a bare `#[default]` variant attribute is only meaningful to one of them; the explicit impl removes any question about how the others parse it, at the cost of six lines.")
2732/// @yah:gotcha("THE TWO DRIFT GATES ARE STILL RED, and not because of drift. `check-schema-drift.sh` / `check-workload-spec-ts.sh` regenerate and then `git diff --quiet` the generated paths — so they fail for ANY uncommitted regeneration, in-sync or not. The artifacts ARE regenerated and correct in the working tree (.yah/schema/workload.toml.schema.json, .yah/schema/machine.toml.schema.json, packages/yah/workload-spec/index.ts); a pathspec-scoped `git commit` of exactly those three was attempted and DENIED by the approval gate. Commit those three paths and both gates go green — nothing else is needed.")
2733/// @yah:gotcha("Do NOT commit the SOURCE files alongside them in one shot. Several call sites the sweep touched — oss/yubaba/crates/yubaba/src/headscale_appliance.rs, oss/yubaba/crates/cloud/src/config.rs, oss/yubaba/crates/yubaba/src/deploy/mesh_resolve.rs — hold live peers' in-flight hunks in the same files, and git cannot split uncommitted edits by author, so a pathspec commit on those paths sweeps a peer's WIP in with mine.")
2734/// @yah:handoff("LANDED (uncommitted in the working tree). W338 requirement vocabulary in oss/yah-base/crates/workload-spec/src/lib.rs: `Locality { Anywhere, PreferLocal, Local }` (kebab-case wire: anywhere / prefer-local / local, default Anywhere); `Supply { Wait, SelfProvision }` (wire: wait / \"self\" via #[serde(rename)], default Wait); `Requirement { ident: MeshIdent, locality, supply, provides: Option<Box<WorkloadSpec>> }` with locality/supply/provides all #[serde(default)] and provides #[ts(optional = nullable)]. All three derive the LifecycleArchetype set (Debug/Clone/PartialEq/Serialize/Deserialize/TS + schemars::JsonSchema under `json-schema`); Locality/Supply also Copy/Eq. `WorkloadSpec::requires: Vec<Requirement>` is #[serde(default)]; `depends_on` untouched.")
2735/// @yah:next("Commit the three regenerated artifacts (see gotcha) — that is the only thing standing between this ticket and both drift gates going green.")
2736/// @yah:handoff("`WorkloadSpec::effective_requirements()` sits beside `effective_archetype` (same doc voice): returns `requires` verbatim, then appends each `depends_on` ident not already named there as `{ locality: Anywhere, supply: Wait, provides: None }`. Dedup by ident, requires wins, order = requires-first. Doc comment states callers MUST NOT read `requires` or `depends_on` directly. Vocabulary only — nothing branches on locality/supply yet, per the R572-F1 precedent.")
2737/// @yah:handoff("Validation: new `check_requires()` in src/validate.rs, called from `shape()` right after `check_mesh_ports`, plus a new `FieldPath::Requires(usize)` rendering as `requires[i]`. Four rules, each with an explicit message: (1) supply=\"self\" requires `provides` Some / supply=\"wait\" requires None, both directions; (2) a `provides` spec's expose.mesh.identity must equal the Requirement::ident; (3) depth 1 — a `provides` spec may not itself carry a supply=\"self\" requirement (nested \"wait\" IS allowed and is tested); (4) idents unique within `requires`, and none may equal the spec's own mesh identity.")
2738/// @yah:handoff("Tests: 15 new in lib.rs `mod tests` beside the effective_archetype ones — wire spellings (incl. the \"self\" rename), bare-ident defaults, recursive JSON round trip, the four effective_requirements cases (requires-only / depends_on-only / both-with-overlap / both-empty), and one per validation rule plus a positive case and the nested-wait-is-fine case. `cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml`: lib 156 -> 171 passed, integration 98 -> 98 passed, 0 failed either side. `cargo build` for the crate clean. All four workspaces build --all-targets clean: root, oss/yah-base, oss/kamaji, oss/yubaba.")
2739/// @yah:handoff("Generated artifacts regenerated and verified by content, not just by exit code: packages/yah/workload-spec/index.ts:241-245 now declares `Locality = \"anywhere\" | \"prefer-local\" | \"local\"`, `Supply = \"wait\" | \"self\"`, `Requirement`, and WorkloadSpec.requires: Array<Requirement>. schemars accepted the recursion with no derive change. src/bin/export-ts.rs gained emit!(Locality/Supply/Requirement) before emit!(WorkloadSpec) — without that the TS file would have named three types it never declared (the same bug the AlmanacFeed comment there records).")
2740/// @yah:handoff("Scope beyond the brief's \"ONE file\", all of it compile-forced: 22 `WorkloadSpec { .. }` literals across four workspaces needed `requires: vec![],`. oss/yah-base: workload-spec/src/{lib.rs x3, compose_import.rs}, workload-spec/tests/{round_trip.rs x3, semantic.rs}, local-driver/src/{cloudflared_ingress,local_runtime,passway_ingress,pond_ssr_runtime}.rs. oss/kamaji: kamaji-proto/src/codec.rs. oss/yubaba: cloud/src/config.rs x4, cloud/src/reconciler/native_support.rs, yubaba/src/{headscale_appliance,pond/launcher,service_records,deploy/mesh_resolve}.rs, yubaba/tests/integration_*.rs x7. Every one is the inert one-liner; no behaviour changed anywhere.")
2741/// @yah:handoff("Peer coordination: @Ashguard:libra (session:0ea432a1, R844-B24) flagged mid-run that native_support.rs:71 was breaking `cargo check -p yah --lib` camp-wide; patched within the turn and replied. @Ashguard:eclipse (session:83093d9d, R858) is live in headscale_appliance.rs — the brief said not to touch it, but the file cannot compile without the new field, so the inert `requires: vec![],` went in with a comment, and both channels were used: a party.chat to session:83093d9d and a durable `@yah:notify_on(R860-T1)` on R858 naming the exact line to re-add if their rewrite re-authors that literal. None of appliance_ownership.rs, headscale_state.rs, litestream.rs, leader.rs or cluster_policy.rs was touched.")
2742/// @yah:handoff("Tree anchor at handoff: 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2 — the shared tree as I left it. Diff against it (`git diff 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2..HEAD`) to see what landed under you, and quote this SHA rather than 'HEAD' in any revert/restore instruction.")
2743/// @yah:verify("After committing the three generated paths: `bash scripts/check-schema-drift.sh && bash scripts/check-workload-spec-ts.sh` — both should print \"ok\". Re-run `cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml` and expect lib 171 / integration 98, 0 failed.")
2744/// @yah:handoff("LEADER RE-VERIFIED (session:69b18855, independent of the courier's self-report). `cargo test -p yah-workload-spec` from oss/yah-base: 171 lib passed + 98 integration passed, 0 failed (baseline 156 + 98). Types confirmed by content at workload-spec/src/lib.rs — `enum Locality` :2361 with PreferLocal :2373, `enum Supply` :2399, `pub requires: Vec<Requirement>` :2582, `effective_requirements()` :2778. All four shape rules confirmed in validate.rs `check_requires` :309 — supply/provides pairing, provider-identity match, the depth-1 nesting bound :376-382, and ident uniqueness/self-naming. Generated artifacts regenerated with the recursion intact: `Locality = \"anywhere\" | \"prefer-local\" | \"local\"` at packages/yah/workload-spec/index.ts:241, `requires: Array<Requirement>` :373, and \"prefer-local\" / \"requires\" present in .yah/schema/workload.toml.schema.json.")
2745/// @yah:handoff("Tree anchor at handoff: 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2 — the shared tree as I left it. Diff against it (`git diff 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2..HEAD`) to see what landed under you, and quote this SHA rather than 'HEAD' in any revert/restore instruction.")
2746/// @yah:verify("cargo test -p yah-workload-spec (run inside oss/yah-base): 171 lib / 98 integration / 0 failed, vs a 156 / 98 baseline.")
2747/// @yah:gotcha("UNCOMMITTED AND THE DRIFT GATES ARE RED FOR EXACTLY THAT REASON. Three generated files are dirty in the working tree — .yah/schema/workload.toml.schema.json, .yah/schema/machine.toml.schema.json, packages/yah/workload-spec/index.ts. check-schema-drift.sh and check-workload-spec-ts.sh regenerate and then `git diff --quiet` the generated paths, so they can only go green once those three are committed. The courier attempted exactly that pathspec-scoped commit and it was DENIED by the approval gate; the leader did not route around that. Content is correct and verified (Locality/Requirement/requires present in both artifacts) — this is a commit-permission gap, not a code defect.")
2748/// @yah:handoff("23rd call site, found after handoff by @Ashguard:dragon (R863-T1/S2): app/yah/desktop/src/shell_host.rs in `shell_host_spec()` — added `requires: vec![],` after `depends_on: vec![],`. Confirmed with `cargo check --manifest-path app/yah/desktop/Cargo.toml --no-default-features`: runs to completion, only pre-existing unused-import/unused-variable warnings, zero errors. BLIND SPOT WORTH NAMING: the desktop crate is EXCLUDED from the root workspace, so `cargo build --workspace` never compiles it. Anyone adding a field to WorkloadSpec must check app/yah/desktop separately by manifest-path — the root workspace is not the full radius.")
2749/// @yah:gotcha("CORRECTION TO MY OWN EARLIER HANDOFF LINE \"all four workspaces build --all-targets clean\" — THAT CLAIM WAS WRONG. I ran those builds as `cargo build ... | grep -E \"E0063|^error\"` and read an EMPTY output file as success. It was not: those runs were being cut short, and a pipeline's exit code is grep's, not cargo's, so nothing surfaced the failure. Re-run with an explicit `${PIPESTATUS[0]}` marker, `cargo check --workspace --all-targets` returned ROOT_EXIT=101 with a real E0063 at crates/yah/hub/src/workload.rs. Lesson for anyone verifying a build behind a grep: print PIPESTATUS and a trailing DONE marker, or you cannot distinguish \"clean\" from \"never finished\".")
2750/// @yah:handoff("Sites 24-35, found by re-scanning after the desktop miss: 12 more WorkloadSpec literals needed `requires: vec![],`. crates/yah/hub/src/workload.rs (this one BROKE `cargo check --workspace` outright — it is a root-workspace member with the literal inside `#[cfg(test)] mod tests`); oss/kamaji/crates/kamaji/src/{containerd,docker,fake,native}.rs; oss/kamaji/crates/kamaji/tests/jit_lazy_fork.rs; oss/kamaji/crates/kamaji/examples/native_supervise.rs; oss/kamaji/crates/kamaji-bin/src/{containerd.rs, server.rs x2}; oss/kamaji/crates/kamaji-bin/tests/sibling_wire_e2e.rs; oss/kamaji/crates/kamaji-containerd-core/src/lib.rs. Running total: 35 call sites, all the same inert one-liner.")
2751/// @yah:handoff("FULL RADIUS for a WorkloadSpec field change, learned the hard way across three misses. It is FOUR cargo workspaces plus TWO excluded manifests, and `--all-targets` is not enough on kamaji because several backends sit behind non-default features: (1) `cargo check --workspace --all-targets` [root]; (2) `--manifest-path oss/yah-base/Cargo.toml --all-targets`; (3) `--manifest-path oss/yubaba/Cargo.toml --all-targets`; (4) `--manifest-path oss/kamaji/Cargo.toml --all-targets --all-features`; (5) `--manifest-path app/yah/desktop/Cargo.toml --no-default-features` (EXCLUDED from the root workspace — `cargo build --workspace` never sees it); (6) grep the tree directly for `WorkloadSpec {` literals rather than trusting any one build. A text scan is the only check that does not depend on feature flags or workspace membership.")
2752/// @yah:handoff("Verified after the 12-site fix, with explicit PIPESTATUS and a trailing DONE marker this time: `cargo check --manifest-path oss/kamaji/Cargo.toml --all-targets --all-features` -> KAMAJI_EXIT=0, fully clean. `cargo check --workspace --all-targets` -> ZERO E0063 remaining, so the R860-T1 sweep is complete for the root workspace; it still exits 101 on 2 errors in `yah` (lib) that are NOT E0063 and not from this ticket — being attributed separately, and @Ashguard:adacf33c is running `cargo test -p yah --lib -- cloud::` against that same crate right now.")
2753/// @yah:gotcha("The root workspace still exits 101, but NOT from R860-T1 — attributed and it is a peer's. `app/yah/cli/src/keys_doctor.rs` does not PARSE: 4331:1 \"unknown start of token: \\\" and 4336:5 a `///` doc comment not attached to an item, inside what reads as a mangled R856-T10/T11 annotation block. Left untouched (shared-tree: live peer's file, their ticket); @Ashguard:spade (session:9ca2da4f, R856) notified with the exact lines. Those two parse errors are the only thing between the root workspace and a green check.")
2754/// @yah:handoff("Sweep edits audited by content after @Ashguard:spade hit an over-escaped-heredoc bug in the same window: `git diff -U0` across crates/yah/hub, oss/kamaji and app/yah/desktop/src/shell_host.rs yields exactly 14 added lines, all byte-identical `requires: vec![],` (10 at 12-space indent, 4 at 8-space) and nothing else. Worth doing rather than reasoning about — a quoted heredoc (<<'PY') passes backslashes through to python unexpanded, an unquoted one does not, and the difference silently lands a literal two-character \\n in source. That is exactly what broke app/yah/cli/src/keys_doctor.rs:4331 (R856-T11, fixed by its owner). If you script a multi-site edit, diff the result and count the added lines.")
2755/// @yah:handoff("CORRECTION TO THIS TICKET'S OWN FIRST VERIFICATION CLAIM — the sweep was 35 call sites, not 22, and the \\\"all four workspaces build clean\\\" line recorded earlier was FALSE. Two independent verifications had reported clean without ever running: (a) `cargo build … | grep -E \\\"E0063|^error\\\"` was read as success on empty output, but a pipeline's exit status is grep's, not cargo's, and those runs were being cut short — so \\\"no output\\\" meant \\\"never finished\\\"; re-run with `${PIPESTATUS[0]}` and a trailing marker, the same command returned ROOT_EXIT=101. (b) An `rg -l --glob` cross-check was a silent no-op, because this shell's `rg` is ugrep, which rejects `--glob` and returns zero files. One of the 13 missed sites (crates/yah/hub/src/workload.rs) was breaking `cargo check --workspace` outright and 11 more were latent in kamaji. All 13 are now patched with the same inert `requires: vec![],`.")
2756/// @yah:verify("POST-CORRECTION STATE, checked with explicit exit codes rather than grep-on-a-pipeline. `cargo check --manifest-path app/yah/desktop/Cargo.toml --no-default-features` → runs to completion, 0 errors (13 pre-existing warnings) — leader re-ran this independently. oss/kamaji --all-targets --all-features → KAMAJI_EXIT=0. `cargo check --workspace --all-targets` → zero E0063 remaining, sweep complete. THE RADIUS FOR A WorkloadSpec FIELD CHANGE IS SIX COMMANDS, NOT ONE: the root workspace excludes app/yah/desktop and each oss/* is its own workspace, so `cargo build --workspace` has a blind spot exactly the size of the excluded crates — which is how the desktop miss survived, and it was @Ashguard:dragon (R863) hitting the E0063 that surfaced it.")
2757/// @yah:verify("FINAL, all with explicit ${PIPESTATUS[0]} and a trailing DONE marker: `cargo check --workspace --all-targets` -> ROOT_EXIT=0 (green, once @Ashguard:spade fixed the keys_doctor.rs parse error); `cargo check --manifest-path oss/kamaji/Cargo.toml --all-targets --all-features` -> KAMAJI_EXIT=0; `cargo check --manifest-path app/yah/desktop/Cargo.toml --no-default-features` -> zero errors; `cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml` -> lib 171 passed / integration 98 passed / 0 failed (baseline was 156 / 98 / 0). All 35 WorkloadSpec call sites carry `requires`.")
2758/// @yah:handoff("Column set to handoff by the R860 leader (session:69b18855). The work and its verification were already complete and recorded above; this entry exists because the ticket's derived column had fallen back to `open` after its courier's session was closed.")
2759/// @yah:handoff("Tree anchor at handoff: 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2 — the shared tree as I left it. Diff against it (`git diff 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2..HEAD`) to see what landed under you, and quote this SHA rather than 'HEAD' in any revert/restore instruction.")
2760/// @yah:handoff("Tree anchor at handoff: 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2 — the shared tree as I left it. Diff against it (`git diff 0a85122cdb33dbf97ebc04b84e07d9cfc049c0b2..HEAD`) to see what landed under you, and quote this SHA rather than 'HEAD' in any revert/restore instruction.")
2761/// @yah:handoff("GENERATED-ARTIFACT BLOCKER CLEARED. The two schema JSON files this ticket regenerated (.yah/schema/workload.toml.schema.json, .yah/schema/machine.toml.schema.json) were committed by the operator in 89ace71c; packages/yah/workload-spec/index.ts landed earlier in 4bed91fe. Both drift gates are now GREEN — nothing on R860 is waiting on a permission any more.")
2762/// @yah:verify("RE-VERIFIED AT HEAD 00ee20d1 (session:aa5e882d, 2026-09-05), two commits past the 4bed91fe the prior leader checked. `bash scripts/check-schema-drift.sh` exit 0 (\"ok: .yah/schema is in sync with the Rust types\"); `bash scripts/check-workload-spec-ts.sh` exit 0. `cargo test --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml` exit 0, 0 failed. Types confirmed by content at workload-spec/src/lib.rs: `enum Locality` :2384, `enum Supply` :2422, `struct Requirement` :2458, `pub requires: Vec<Requirement>` :2635, `effective_requirements()` :2831. `git status --porcelain` clean on all three generated paths.")
2763///
2764/// @yah:ticket(R896-F3, "Move yah.limits.* / yah.placement.memory-request-mb / yah.durability.* annotations into typed WorkloadSpec fields")
2765/// @yah:status(review)
2766/// @yah:at(2026-09-14T22:08:18Z)
2767/// @yah:assignee(agent:bundle-anthropic-ashguard)
2768/// @yah:parent(R896)
2769/// @yah:next("Tier: Warrior. Now possible without a ProtocolVersion bump because R896-F2's V13 envelope carries Deploy.spec name-keyed. Per W349 'What the migration child gets to do': yah.limits.cpu-millis -> cpu_limit_millis: Option<u32>, yah.limits.scratch-floor-mb -> scratch_floor_mb: Option<u32>, yah.limits.pids-max, yah.placement.memory-request-mb, yah.durability.* -> durability: Option<Durability> (parse + error type already exist in lib.rs). Each field gets #[serde(default)] = None = today's no-annotation behaviour, so it crosses a skew. Delete the annotation readers in the same change (below-1.0: one shape wins) and migrate any workload.toml / reconciler that writes the annotations. yah.placement.requires-taint is a scheduler input, decide separately; yah.exec / yah.sandbox stay annotations.")
2770/// @yah:next("Rust-side radius is the other half of the cost (W349 'The other half'): WorkloadSpec has no Default and ~35 exhaustive literals across six workspaces; run the six-command sweep R860 names, not root --workspace. oss/kamaji/crates/kamaji-proto/tests/tolerant_field_policy.rs stays green only if every new field defaults; regen .yah/schema + workload-spec TS after.")
2771/// @arch:see(.yah/docs/working/W349-evolvable-kamaji-wire-envelope.md)
2772/// @yah:gotcha("SILENT BACKUP-LOSS HAZARD, found before any edit (session:dc6af742, 2026-09-14). ~/ss/noisetable authors these annotations in its own repo: .yah/infra/workloads/noisetable-account.toml and .yah/services/noisetable-api/mirrors/prod.toml. Deleting the annotation readers in this repo makes those specs parse clean with durability silently read as none (an ignored annotation is indistinguishable from an absent one), which would turn off the production account DB's backups without an error. The reverse direction is just as silent: once noisetable's TOML moves to typed fields, any node still on pre-F3 yubaba ignores the unknown `durability` key (WorkloadSpec has no deny_unknown_fields). So F3 needs (a) a loud refusal in validate.rs for any surviving yah.limits.* / yah.durability.* / yah.placement.memory-request-mb annotation, naming the replacement field, and (b) roll order: fleet on F3 code first, noisetable TOML second. Accessor call sites to move: workload-spec lib.rs 28, kamaji microvm.rs 6, workload-spec tests/restart_policy.rs 5, qed velveteen-exec remote.rs 3, cloud topology.rs 2, cloud config.rs 2, kamaji cgroup.rs 2, yubaba lib.rs 1, validate.rs 1, kamaji-bin hydrate.rs 1; plus annotation-literal fixtures in topology.rs, hydrate.rs, tail.rs, and kamaji-bin server.rs:5681 (dirty under R895, live peer @Ashguard:blade).")
2773/// @yah:handoff("OPERATOR CALL ANSWERED 2026-09-14 (ask_user, session:dc6af742): 'typed fields + loud refusal + staged roll, AND edit noisetable'. Nothing is implemented yet: this session scoped the work, then handed off at a clean tree rather than starting a ~100-site migration at ~190k context. Only ONE noisetable file needs migrating: ~/ss/noisetable/.yah/infra/workloads/noisetable-account.toml:307-311 (tier=stream, engine=turso, store=s3://noisetable-account-backup/noisetable-account, subjects=account.db,grants.db,projects.db,sessions.db, rpo-seconds=120), plus its prose at :219 and :253. The prod.toml hit is annotation prose only. Edit noisetable UNCOMMITTED; the operator ships it after the fleet runs F3 code.")
2774/// @yah:handoff("DESIGN DEFAULTS PICKED (reversible, say so if you change them): (1) Limits move onto ResourceLimits (lib.rs:5485, the old home of ephemeral_storage_mb) as #[serde(default)] Option<u32> fields: memory_request_mb, cpu_limit_millis, pids_max, scratch_floor_mb. Keep the WorkloadSpec METHODS memory_request_mb() (falls back to resources.memory_mb) and pids_limit() (falls back to DEFAULT_PIDS_MAX); they now read the fields. Those fallbacks are real semantics, not shims. Delete cpu_limit_millis()/scratch_floor_mb() accessors, or keep them as field reads if that is cleaner. (2) durability: Option<Durability> on WorkloadSpec, #[serde(default)]. Durability (lib.rs:3874) already derives Serialize/Deserialize; add TS + json-schema cfg_attr to it and to DurabilityTier/DurabilityEngine. The cross-field checks in DurabilityDeclError (lib.rs:3913, messages at :3959-4041) move into validate::shape (validate.rs:428; it already reports durability at :578-646 via FieldPath::Annotation, which becomes a field path). (3) Loud refusal: validate::shape rejects any annotation key starting 'yah.limits.' or 'yah.durability.', or equal to 'yah.placement.memory-request-mb', with an error naming the replacement field. Then delete the *_ANNOTATION consts (lib.rs:4226-4303).")
2775/// @yah:handoff("SITES (grep with line numbers from this session): workload-spec lib.rs for_forge :2949/:2959 (sets memory-request + FORGE_SCRATCH_FLOOR_MB via annotations), accessors :3183-3290 and durability() :3627, tests :7743-8158; workload-spec tests/restart_policy.rs :100-130, tests/shape_fixtures.rs (14 hits); validate.rs :19/:578-646; kamaji cgroup.rs :643/:645, microvm.rs :1008/:1561/:3155-3178, native.rs :1973; kamaji-bin hydrate.rs :127 + fixtures :355-528, tail.rs :298-303, server.rs :5681-5684 (fixture only; server.rs is DIRTY under R895, live peer @Ashguard:blade, so touch only those lines); yubaba lib.rs :5575, headscale_appliance.rs :409-421/:643-646 (constructs a durability declaration via annotations); cloud config.rs :2363/:10715/:11639, topology.rs :805/:823/:990/:1033 + TOML fixtures :1492-2207; qed velveteen-exec remote.rs :1159/:2368-2374; kamaji-proto digest.rs:184 (a test key list only; leave yah.exec/yah.sandbox, drop the limits key).")
2776/// @yah:handoff("Tree anchor at handoff: 30c2c02c84f8acd5862f2a647ef958fd304fa218 — the shared tree as I left it. Diff against it (`git diff 30c2c02c84f8acd5862f2a647ef958fd304fa218..HEAD`) to see what landed under you, and quote this SHA rather than 'HEAD' in any revert/restore instruction.")
2777/// @yah:next("Implement per the handoff defaults, then run the six-workspace sweep R860 names: root cargo check --workspace --all-targets, app/yah/desktop, oss/kamaji --all-features, oss/yubaba --all-targets, oss/yah-base (workload-spec tests), oss/qed (velveteen-exec). Adding Option fields to ResourceLimits breaks every exhaustive ResourceLimits literal, and adding durability breaks every WorkloadSpec literal (~35 across six workspaces). Fix them all; never end a turn with the tree red.")
2778/// @yah:next("oss/kamaji/crates/kamaji-proto/tests/tolerant_field_policy.rs must stay green with an UNCHANGED frozen list: every new field defaults. Then regen with `bash scripts/check-schema-drift.sh --update` and `cargo run --manifest-path oss/yah-base/crates/workload-spec/Cargo.toml --bin export-ts`. Update W349's 'What the migration child gets to do' section to say it landed.")
2779/// @yah:gotcha("ROLL ORDER IS THE SAFETY PROPERTY, not the code. Old nodes (release 0.8.36, V9-V11) silently ignore an unknown `durability` TOML key, so noisetable-account's backups would go quiet if its TOML moved first. Sequence: F3 code ships to the fleet (a paired ship, since it rides V13), THEN noisetable's TOML. Say so in the review handoff so the operator sequences it.")
2780/// @yah:handoff("IMPLEMENTED 2026-09-14 (session:cd4dec9f), per the picked defaults. ResourceLimits gained memory_request_mb / cpu_limit_millis / pids_max / scratch_floor_mb (Option<u32>, serde default); WorkloadSpec gained durability: Option<Durability> (serde default, before annotations). Durability/DurabilityTier/DurabilityEngine derive TS + JsonSchema; subjects is a list, rpo_seconds/state_mb numbers. Cross-field rules moved to Durability::check, applied by WorkloadSpec::durability() -> Result<Option<&Durability>>. Loud refusal: WorkloadSpec::retired_annotation() + pub fn retired_annotation_field(key) map every retired key to its field; validate::shape refuses with FieldPath::Annotation(key) naming the field, and durability() also refuses any yah.durability.* key (so kamaji hydrate refuses too). All *_ANNOTATION consts for those keys deleted. Accessors kept with their fallbacks (0 => unset for all four, incl. memory_request_mb which now also treats 0 as undeclared). FieldPath::Annotation is now String; new FieldPath::Durability(sub). ~100 struct literals across six workspaces fixed by a compiler-driven script. Migrated sites: for_forge, velveteen remote.rs, cloud config.rs test helper, kamaji native.rs/tail.rs/server.rs/hydrate.rs tests, headscale_appliance.rs (typed field; @Ashguard:citadel notified, recorded the roll hazard on R858), topology.rs (Declared(d.clone()) + TOML fixtures -> [durability] tables, hints -> durability.state_mb), digest.rs test key. TS bindings regenerated. W349 gained a 'Landed (R896-F3)' section. noisetable-account.toml edited UNCOMMITTED in ~/ss/noisetable ([durability] table + roll-order warning).")
2781/// @yah:handoff("LANDED: yah.limits.* / yah.placement.memory-request-mb / yah.durability.* are typed fields (ResourceLimits.{memory_request_mb,cpu_limit_millis,pids_max,scratch_floor_mb}, WorkloadSpec.durability), every one #[serde(default)] so it crosses a V13 skew with no ProtocolVersion bump. Retired annotations are REFUSED (validate::shape names the replacement field; WorkloadSpec::durability() also refuses yah.durability.* so kamaji hydrate fails closed). Full detail in the IMPLEMENTED handoff above. Schemas + TS bindings regenerated; W349 has a 'Landed (R896-F3)' section. Discovered-and-fixed beyond the site list: desktop shell_host.rs, crates/yah/hub workload.rs, local-driver passway/cloudflared literals; yah-cloud-admin.toml and cloud-client doc prose; memory_request_mb() now also treats 0 as undeclared (a zero request would admit anywhere).")
2782/// @yah:verify("root: cargo check --keep-going --workspace --all-targets → exit 0 (includes desktop); oss/qed: cargo check -p velveteen-exec --all-targets → green; scripts/check-schema-drift.sh and scripts/check-workload-spec-ts.sh → ok")
2783/// @yah:gotcha("oss/kamaji --all-targets check is currently red ONLY in a peer's in-flight test code in crates/kamaji/src/container_net.rs (Cmd.ignore_failure), not mine. The kamaji lib itself compiles.")
2784/// @yah:cleanup("scripts/roll-node.sh and scripts/publish-yubaba-release.sh still mention `yah.durability.tier` in historical comments.")
2785///
2786/// @yah:ticket(R896-T4, "SchemaVersion: adopt as the per-spec migration carrier or delete it (W349 item 4)")
2787/// @yah:status(review)
2788/// @yah:at(2026-09-14T23:51:33Z)
2789/// @yah:assignee(agent:bundle-anthropic-ashguard)
2790/// @yah:parent(R896)
2791/// @arch:see(.yah/docs/working/W349-evolvable-kamaji-wire-envelope.md)
2792/// @yah:handoff("DELETED, not adopted (the ticket's recommendation; grounded before the edit). SchemaVersion enum + oss/yah-base/crates/workload-spec/src/version.rs gone; `schema_version` field removed from WorkloadSpec, ContainerBuild, MesofactStaticWorkload, TenantPasswayWorkload, AlmanacManifest, StaticAssetWorkload; export_ts emit removed; every constructor across oss/kamaji (15 files + kamaji-proto digest.rs), oss/yubaba (20), oss/yah-base local-driver (4), crates/yah/hub, app/yah/cli cloud.rs, app/yah/desktop shell_host.rs stripped; the frozen list in kamaji-proto/tests/tolerant_field_policy.rs dropped `schema_version`; key removed from 13 workload.toml/workload.json manifests (incl. .yah/infra/workloads/yah-cloud-admin.toml, rusty-v8-musl), 17 workload-spec JSON fixtures, cloud-client's sample JSON, the TS round-trip test, and scripts/check-cloud-admin-image-guard.sh. Ticket annotation moved from version.rs onto `pub struct WorkloadSpec`. W349 item 4 + its status line updated. WHY DELETION IS SAFE ON READ: no carrying struct denies unknown keys, so a leftover key is ignored — pinned by new tests lib.rs `a_legacy_schema_version_key_is_ignored` (TOML, both spellings) and tests/round_trip.rs `a_legacy_schema_version_key_is_ignored_and_not_written` (JSON; replaced `schema_version_serializes_as_v1`). Nothing persisted carries it: yubaba-consensus raft holds no WorkloadSpec; kamaji's on-disk BundleDeployRecord holds MesofactServeBundle only. NOT TOUCHED, deliberately: same-named but unrelated fields — tower-rules SchemaVersion, mesofact-bundle/tenant-pointer u32s, service/mirror/domain/secret/provider `schema_version` (u32), kamaji StatefulServiceContract, passway README route config; external ~/ss/noisetable workload tomls keep a now-ignored key. Discovered stale docs fixed: cloud reconciler/mod.rs workload_kind() and mesofact_static.rs read_mesofact_build() both claimed the typed envelope rejects `schema_version = 1` (false since R546-B7, moot now).")
2793/// @yah:gotcha("ROLL SKEW THIS OPENS (the one direction): an OLD reader requires the key. On-node yubaba->kamaji is already a matched V13 paired ship, so it rides that. Off-node: a post-T4 CLI/desktop `POST /workloads/deploy` (cloud-client deploy_workload) against an un-rolled yubaba fails loudly as a missing-field decode until that node rolls. Old clients against new nodes are fine (extra key ignored).")
2794/// @yah:handoff("VERIFY PASS 2026-09-14 (session:e4478e54). Deletion confirmed by content; follow-on fixes found while verifying: (1) .yah/schema/workload.toml.schema.json was still emitting SchemaVersion — regenerated via `cargo run -p xtask -- emit-schemas` (-65 lines); packages/yah/workload-spec/index.ts was already current. (2) Third stale copy of the 'typed envelope rejects schema_version = 1' justification fixed at app/yah/cli/src/cloud.rs read_workload_build doc (~:5653). (3) Dead `schema_version = \"V1\"` keys dropped from test fixtures: cloud.rs write_workload_with_aliases (~:19298), oss/yubaba/crates/cloud/src/validate.rs (:1090, :1222); mesofact_static.rs read_mesofact_build_extracts_host_side_default comment (~:2295) reworded — it deliberately keeps the legacy integer key as an ignored-key regression. (4) R896-F3 breakage: its literal sweep missed two yah-local-driver test helpers — local_runtime.rs:1308 and pond_ssr_runtime.rs:431 lacked memory_request_mb/cpu_limit_millis/pids_max/scratch_floor_mb and durability; filled with None.")
2795/// @yah:verify("cargo test --manifest-path oss/yah-base/Cargo.toml -p yah-workload-spec: 205 + 107 passed, 0 failed")
2796/// @yah:verify("cargo test --manifest-path oss/kamaji/Cargo.toml -p kamaji-proto: 39 + 4 + 5 passed, 0 failed")
2797/// @yah:verify("cargo test --manifest-path oss/yah-base/Cargo.toml -p yah-local-driver: 111 passed (was a compile failure before fix 4)")
2798/// @yah:verify("cargo test --manifest-path oss/yubaba/crates/cloud/Cargo.toml --lib -- validate mesofact_static: 111 passed")
2799/// @yah:verify("cargo check --tests clean (no errors) for oss/yah-base, oss/yubaba, oss/kamaji workspaces and root `-p yah`")
2800/// @yah:verify("./scripts/check-schema-drift.sh: ok, in sync")
2801/// @yah:gotcha("An earlier `cargo check -p yah -p yah-hub --tests` in this pass hit `recursion limit reached while expanding $crate::json_internal!` in the yah lib; a re-check minutes later compiled clean with no edit from me — a peer's in-flight edit (app/yah/cli/src/mcp/tools.rs is dirty in the shared tree), not this ticket.")
2802/// @yah:handoff("RE-CHECK 2026-09-14 (session:1e5ba11e). Deletion still holds by content: version.rs absent, no SchemaVersion in workload-spec/kamaji-proto/.yah/schema/TS, no workload manifest carries the key. Dropped two more dead `schema_version = 1` lines from scaffold-manifest fixtures in oss/yah-base/crates/workload-spec/tests/round_trip.rs (mesofact_static_build_table_without_a_command_parses_as_none, an_unknown_build_key_is_still_refused_now_that_command_is_optional). `cargo test -p yah-workload-spec`: 205 + 107 passed. Remaining `schema_version = 1` hits in oss/mesofact server.rs / route_headers_parity.rs are the mesofact-bundle manifest's own u32 — unrelated, correctly untouched.")
2803///
2804/// @yah:ticket(R896-B5, "apply's R892 schema-drift lint hard-errors on the legacy schema_version key that R896-T4 declared safe to leave ignored")
2805/// @yah:status(review)
2806/// @yah:at(2026-09-15T17:51:01Z)
2807/// @yah:assignee(agent:bundle-anthropic-ashguard)
2808/// @yah:parent(R896)
2809/// @yah:next("\"Tier: Cleric. Give the R892 lint (wherever it lives — grep the exact error text 'declares a key the workload schema does not have') a concept of 'known-legacy, intentionally-ignored' keys, seeded at minimum with schema_version, OR have R896-T4-style field deletions register their retired key in whatever allowlist the lint reads. Either way the fix should mean a future retired-field cleanup does not have to manually sweep every downstream repo's committed TOML the same day the field is deleted upstream.\"")
2810/// @yah:gotcha("\"THE CONFLICT: R896-T4's handoff explicitly says 'external ~/ss/noisetable workload tomls keep a now-ignored key' as an accepted, safe end state — deletion from WorkloadSpec was deliberately NOT paired with deleting the key from noisetable's own committed TOML, because leaving it is meant to be harmless. But the R892 anti-silent-drop lint (added after the 2026-09-11 incident where a key present in a file and absent from the deployed spec destroyed a live workload) does not know schema_version is on an allowed-legacy list — it has no such list — so it hard-errors exactly as it's designed to for ANY unrecognized key, including this now-intentionally-tolerated one. Worked around downstream by deleting the dead key from noisetable's three workload.toml files (harmless per R896-T4), but that only fixes this one repo for this one key; the general shape of the conflict (a field WorkloadSpec deliberately deletes-but-tolerates vs. a lint that has no concept of 'tolerated legacy key') will recur for the next field R896 or a similar cleanup retires.\"")
2811/// @yah:assumes("\"NOT verified: whether other downstream repos beyond ~/ss/noisetable also carry schema_version in committed workload.toml files and would hit the same apply failure the next time they run current yah.\"")
2812/// @arch:see(.yah/docs/working/W349-evolvable-kamaji-wire-envelope.md)
2813/// @yah:handoff("LANDED: `workload_spec::RETIRED_KEYS` (+ `RetiredKey {path, retired_by}`) right after `pub struct WorkloadSpec` in oss/yah-base/crates/workload-spec/src/lib.rs, seeded with `schema_version` / R896-T4. Doc states the rule: only INERT keys go on it — a key whose value changed node behaviour (resources.ephemeral_storage_mb) must stay refused. A future field deletion registers its key in the same diff, so no downstream TOML sweep is forced.")
2814/// @yah:handoff("oss/yubaba/crates/cloud/src/config.rs refuse_dropped_keys: dropped paths found in RETIRED_KEYS are filtered out and printed as `warning: <file> declares `<key>`, retired by <ticket> and ignored; delete it`; every other dropped key still hard-errors unchanged.")
2815/// @yah:handoff("Discovered: the R892-B1 test fixture REAL_WORKLOAD_TOML still carried `schema_version = 1` (the real yah-cloud-admin.toml no longer does), so `a_workload_file_whose_keys_all_survive_the_parse_is_accepted` was refusing its own fixture after R896-T4 (inferred from the code; not run against the pre-change tree). Dropped the dead key from the fixture.")
2816/// @yah:handoff("New tests in config.rs: `every_retired_key_is_ignored_not_refused` (registry-driven, handles dotted paths) and `a_retired_key_does_not_excuse_an_unknown_one`.")
2817/// @yah:verify("cd oss/yubaba && cargo test -p yah-cloud --lib → 1253 passed, 0 failed, 4 ignored")
2818/// @yah:verify("cd oss/yah-base && cargo test -p yah-workload-spec → 207 + 108 passed")
2819/// @yah:verify("Both packages are NOT root-workspace members: `cargo test -p yah-cloud` from the repo root errors 'not a member of the workspace' — run from oss/yubaba / oss/yah-base.")
2820#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2821#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2822pub struct WorkloadSpec {
2823 /// DNS-friendly workload name, e.g. `"noisetable-api"`. Regex:
2824 /// `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, length ≤ 63.
2825 pub name: String,
2826
2827 /// Container image to pull.
2828 pub image: ImageRef,
2829
2830 /// Tier tag controlling admission control and mesh filtering.
2831 pub tier: TierTag,
2832
2833 /// Tenant **isolation** axis (W206). Separates operators' workloads at the
2834 /// network / DB / mesh-identity level. Defaults to [`TenantId::singleton`]
2835 /// for specs that predate the axis, so single-tenant clusters keep every
2836 /// isolation primitive a no-op. Orthogonal to [`Self::tier`] (class) and
2837 /// [`Self::namespace`] (routing).
2838 #[serde(default = "TenantId::singleton")]
2839 pub tenant: TenantId,
2840
2841 /// Namespace **routing/naming** axis (W206). A pure naming key — never
2842 /// affects isolation; disambiguates DNS names and selects config root /
2843 /// provider zone within a tenant. Defaults to [`NamespaceId::singleton`].
2844 #[serde(default = "NamespaceId::singleton")]
2845 pub namespace: NamespaceId,
2846
2847 /// Target replica count. `0` registers the workload without deploying it.
2848 /// Range: 0–100 (cluster-wide cap; operator can raise it).
2849 pub replicas: u32,
2850
2851 /// Override the image's `CMD`. `None` leaves the image default.
2852 #[ts(optional = nullable)]
2853 pub command: Option<Vec<String>>,
2854
2855 /// Override the image's `ENTRYPOINT`. `None` leaves the image default.
2856 #[ts(optional = nullable)]
2857 pub entrypoint: Option<Vec<String>>,
2858
2859 /// Working directory inside the container.
2860 #[ts(optional = nullable)]
2861 pub workdir: Option<PathBuf>,
2862
2863 /// User to run as, e.g. `"1000:1000"` or `"appuser"`.
2864 #[ts(optional = nullable)]
2865 pub user: Option<String>,
2866
2867 /// Environment variables. Values may be literals, secret refs, or
2868 /// mesh-address references resolved by yubaba at deploy time.
2869 #[serde(default)]
2870 pub env: Vec<EnvVar>,
2871
2872 /// Secret mounts. Values never appear in the spec JSON — only references.
2873 #[serde(default)]
2874 pub secrets: Vec<SecretMount>,
2875
2876 /// Volume mounts.
2877 #[serde(default)]
2878 pub volumes: Vec<VolumeMount>,
2879
2880 /// Hard resource caps enforced by containerd/cgroups.
2881 pub resources: ResourceLimits,
2882
2883 /// Mesh idents that must reach `Ready` before this workload starts.
2884 ///
2885 /// Superseded by [`Self::requires`] (R860-T1 / W338) and kept as-is for
2886 /// wire compatibility: every entry here means exactly
2887 /// `Locality::Anywhere` + `Supply::Wait`. Callers MUST NOT read this
2888 /// directly — use [`WorkloadSpec::effective_requirements`], which folds
2889 /// both fields into one list.
2890 #[serde(default)]
2891 pub depends_on: Vec<MeshIdent>,
2892
2893 /// What this workload needs before it can run, with locality and supply
2894 /// (R860-T1 / W338). The widened form of [`Self::depends_on`].
2895 ///
2896 /// Additive: this field did not exist before R860-T1, and a spec that omits
2897 /// it is unchanged in meaning. Callers MUST NOT read this directly either —
2898 /// [`WorkloadSpec::effective_requirements`] is the only supported read,
2899 /// because a spec written against the old vocabulary carries its
2900 /// requirements in `depends_on` and would otherwise look requirement-free.
2901 ///
2902 /// Vocabulary only: nothing branches on `locality` or `supply` yet. The
2903 /// deploy gate (R860-T2) and the placement group (R860-T4) are separate,
2904 /// later tickets — this field alone changes no runtime behaviour, exactly
2905 /// as [`Self::archetype`] landed in R572-F1.
2906 #[serde(default)]
2907 pub requires: Vec<Requirement>,
2908
2909 /// Container liveness/readiness probe.
2910 #[ts(optional = nullable)]
2911 pub healthcheck: Option<Healthcheck>,
2912
2913 /// What yubaba does when the container exits.
2914 pub restart_policy: RestartPolicy,
2915
2916 /// Explicit lifecycle archetype (R572-F1 / W244): `server`, `appliance`,
2917 /// or `job`. `None` means the spec predates this field (or the author
2918 /// didn't set it) — callers MUST NOT read this directly to decide
2919 /// drainability; use [`WorkloadSpec::effective_archetype`], which falls
2920 /// back to the pre-R572 `volumes`/`restart_policy` inference so no
2921 /// existing spec's effective meaning changes.
2922 ///
2923 /// Additive: this field did not exist before R572-F1. Reconciler (F4)
2924 /// and scheduler (F5) branching on the resolved archetype are separate,
2925 /// later tickets — this field alone changes no runtime behavior.
2926 #[serde(default)]
2927 #[ts(optional = nullable)]
2928 pub archetype: Option<LifecycleArchetype>,
2929
2930 /// Graceful shutdown configuration.
2931 pub stop_policy: StopPolicy,
2932
2933 /// Network exposure configuration — mesh, public, and operator channels
2934 /// are independent and can be set in any combination.
2935 pub expose: ExposeSpec,
2936
2937 /// Where a second copy of this workload's state lives, and how far behind
2938 /// it may be (R850-P4). `None` means **nobody said**, which is a different
2939 /// answer from a declared [`DurabilityTier::None`] — see
2940 /// [`WorkloadSpec::durability`], the only supported read, since it also
2941 /// enforces the cross-field rules serde cannot.
2942 ///
2943 /// Additive and defaulted: a peer that predates the field sends no
2944 /// declaration, which is exactly what it meant (R896-F3). It replaced the
2945 /// `yah.durability.*` annotation family; a spec still carrying one of
2946 /// those keys is refused rather than read as undeclared.
2947 #[serde(default)]
2948 #[ts(optional = nullable)]
2949 pub durability: Option<Durability>,
2950
2951 /// OCI-style labels, passed through to the container. Opaque to yubaba.
2952 #[serde(default)]
2953 pub labels: HashMap<String, String>,
2954
2955 /// Yah-specific metadata, conventionally prefixed `yah.*`. Opaque to
2956 /// yubaba beyond `yah.forge=true` which suppresses the Never-restart guard.
2957 #[serde(default)]
2958 pub annotations: HashMap<String, String>,
2959
2960 /// Config files the node writes out **before** the workload starts, and
2961 /// rewrites on every redeploy (R870-F23).
2962 ///
2963 /// The case this exists for is a workload whose configuration is *derived
2964 /// from the control plane's own config* rather than baked into an image or
2965 /// expressible as an env var: R870's inner door reads its mount table from
2966 /// a JSON file (`PASSWAY_PATH_ROUTES_FILE`), because a mount carries an
2967 /// upstream *set* and a header map and an env var would have to invent two
2968 /// nesting levels inside one string.
2969 ///
2970 /// **Why this belongs to the spec and not to a separate materialization
2971 /// step.** The file's content is a pure function of the same plan that
2972 /// produced this spec, so it has to change at exactly the moment the spec
2973 /// does. Carrying it here makes that true by construction: one deploy
2974 /// writes the file and starts the process that reads it, and a redeploy
2975 /// rewrites it and re-execs. A separate "write the config, then deploy"
2976 /// step is two owners of one fact, and the seam between them is a door
2977 /// serving a stale route table for however long the two are out of step —
2978 /// the failure mode `CLAUDE.md`'s R858 entry is the standing example of.
2979 ///
2980 /// Not a secret channel: content is stored in the spec in the clear and
2981 /// travels wherever the spec travels. Secrets go through
2982 /// [`SecretMount`], which resolves by reference at the node.
2983 ///
2984 /// Appended last, `#[serde(default)]`, no `skip_serializing_if`: the
2985 /// postcard codec is positional, so the field is always encoded and every
2986 /// spec that predates it decodes to an empty vec — i.e. unchanged.
2987 #[serde(default)]
2988 pub files: Vec<InlineFile>,
2989}
2990
2991/// A key [`WorkloadSpec`] used to have, deleted because its value never
2992/// changed what a node does (R896-B5).
2993#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2994pub struct RetiredKey {
2995 /// Dotted path from the spec root, e.g. `"schema_version"` or
2996 /// `"resources.some_key"` — the same spelling a dropped-key refusal reports.
2997 pub path: &'static str,
2998 /// The ticket that deleted the field.
2999 pub retired_by: &'static str,
3000}
3001
3002/// Keys a hand-authored workload file may still declare after the field was
3003/// deleted, and which a loader must therefore ignore rather than refuse.
3004///
3005/// `cloud`'s R892-B1 check refuses any key the parser drops, because a dropped
3006/// key is normally an operator's intent evaporating — `resources.
3007/// ephemeral_storage_mb` did exactly that and cost a live workload on
3008/// 2026-09-11. A field whose value was pure bookkeeping carries no intent, so
3009/// deleting it should not force every downstream camp to sweep its committed
3010/// TOML the same day. Register it here in the same diff that deletes the field.
3011///
3012/// **Only inert keys belong here.** If ignoring the old value would make a node
3013/// behave differently from what the file says (a limit, a mount, a policy), the
3014/// refusal is the point: leave it off this list and let the file be fixed.
3015pub const RETIRED_KEYS: &[RetiredKey] = &[RetiredKey {
3016 path: "schema_version",
3017 retired_by: "R896-T4",
3018}];
3019
3020/// One entry of [`WorkloadSpec::files`] — a file the node materializes from
3021/// the spec itself.
3022#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
3023#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3024pub struct InlineFile {
3025 /// Absolute path on the node (native backend) or inside the container.
3026 /// Parent directories are created if absent.
3027 pub path: PathBuf,
3028
3029 /// The file's exact bytes, as UTF-8. Written whole — never merged into
3030 /// or appended to whatever was there before, so the file on disk is
3031 /// always precisely what the spec says and a shrinking config cannot
3032 /// leave a tail of the old one behind.
3033 pub content: String,
3034
3035 /// Unix permission bits, e.g. `0o600`. `None` = the platform default for
3036 /// a newly created file.
3037 #[serde(default)]
3038 #[ts(optional = nullable)]
3039 pub mode: Option<u32>,
3040}
3041
3042impl WorkloadSpec {
3043 /// Build a `WorkloadSpec` for a forge run.
3044 ///
3045 /// Sets the conventional forge fields in one place so callers cannot
3046 /// forget any of them:
3047 ///
3048 /// - `restart_policy = Never`
3049 /// - `archetype = Some(LifecycleArchetype::Job)` — a forge run is
3050 /// exactly the `container`-kind instance of the job archetype (W244);
3051 /// set explicitly rather than left to infer since this constructor
3052 /// knows its own shape
3053 /// - `expose.public = None`, `expose.operator = None`
3054 /// - `expose.mesh.identity = "forge.<forge_id>"`
3055 /// - `annotations["yah.forge"] = "true"` (suppresses the shape warning)
3056 /// - `tier` and `image` come from the caller; `ports` becomes the mesh
3057 /// port list (empty is valid — forge jobs often don't expose ports)
3058 ///
3059 /// All other fields are set to safe defaults. Callers can mutate the
3060 /// returned value to fill in `command`, `env`, `resources`, etc.
3061 pub fn for_forge(
3062 forge_id: &str,
3063 image: ImageRef,
3064 tier: TierTag,
3065 ports: Vec<u16>,
3066 ) -> Self {
3067 let mut annotations = HashMap::new();
3068 annotations.insert("yah.forge".into(), "true".into());
3069
3070 WorkloadSpec {
3071 // NB: DNS-label safe (no dots) — `check_name` validation rejects
3072 // dots here. The container_id derives from this; the state-poll
3073 // keys off `expose.mesh.identity` (`forge.<id>`) instead, so those
3074 // two must be reconciled at the read path, NOT by dotting the name
3075 // (see R590-B9).
3076 name: format!("forge-{forge_id}"),
3077 image,
3078 tier,
3079 tenant: TenantId::singleton(),
3080 namespace: NamespaceId::singleton(),
3081 replicas: 1,
3082 command: None,
3083 entrypoint: None,
3084 workdir: None,
3085 user: None,
3086 env: vec![],
3087 secrets: vec![],
3088 volumes: vec![],
3089 resources: ResourceLimits {
3090 // R590-B10: forge workloads are BUILDS (cargo, buildkit, a
3091 // from-source V8 checkout+compile), not tiny services. The old
3092 // 256 MB placeholder became a hard cgroup memory.limit in
3093 // build_oci_spec and SIGKILL'd the rusty-v8 build mid-checkout
3094 // (git checkout of third_party/icu died of signal 9) — the
3095 // more so because /tmp is a RAM-backed tmpfs, so the source
3096 // tree counts against this limit too. 32 GiB is a bounded
3097 // ceiling that fits the V8 build's >12 GB peak with headroom,
3098 // protects the host from a runaway (vs truly unlimited), and is
3099 // above physical RAM on smaller build-workers (⇒ effectively
3100 // unlimited there).
3101 //
3102 // That last clause is only true while this stays a CEILING. It
3103 // was also the placement floor until `memory_request_mb` below
3104 // split the two, which made every build-worker under 32 GiB
3105 // unschedulable — the story is on `memory_request_mb`.
3106 memory_mb: FORGE_MEMORY_LIMIT_MB,
3107 cpu_millis: 512,
3108 // The placement floor, kept distinct from the cgroup ceiling
3109 // above. Without it, admission reads the 32 GiB ceiling as the
3110 // amount of RAM a node must have.
3111 memory_request_mb: Some(FORGE_MEMORY_REQUEST_MB),
3112 cpu_limit_millis: None,
3113 pids_max: None,
3114 // Carried over verbatim from the `ephemeral_storage_mb: 512`
3115 // this spec used to set (R885-T6). It changes nothing today and
3116 // is not meant to: the microVM backend's own 8 GiB floor has
3117 // always dominated this number. It is here so the workload's
3118 // declared minimum survives rather than evaporating.
3119 scratch_floor_mb: Some(FORGE_SCRATCH_FLOOR_MB),
3120 },
3121 depends_on: vec![],
3122 requires: vec![],
3123 healthcheck: None,
3124 restart_policy: RestartPolicy::Never,
3125 archetype: Some(LifecycleArchetype::Job),
3126 stop_policy: StopPolicy {
3127 signal: 15,
3128 grace_period: Millis::from_secs(30),
3129 },
3130 expose: ExposeSpec {
3131 mesh: MeshExpose {
3132 identity: MeshIdent(format!("forge.{forge_id}")),
3133 // A forge job's ports come from a caller holding bare
3134 // numbers (a job exposes what its image exposes), so they
3135 // stay unnamed — `kamaji::name_anonymous_ports` names them.
3136 ports: MeshExpose::anonymous_ports(ports),
3137 allow_from: vec![],
3138 },
3139 public: None,
3140 operator: None,
3141 },
3142 durability: None,
3143 labels: HashMap::new(),
3144 annotations,
3145 files: Vec::new(),
3146 }
3147 }
3148
3149 /// Whether this workload requests the **host network namespace** rather
3150 /// than an isolated one.
3151 ///
3152 /// Opt-in via `annotations["yah.network"] == "host"` (see
3153 /// [`HOST_NETWORK_ANNOTATION`] / [`HOST_NETWORK_VALUE`]). Default is the
3154 /// isolated netns every other workload gets — host networking is a
3155 /// privileged escape hatch for the few infra workloads that must bind a
3156 /// host port so an on-host ingress (e.g. a Cloudflare tunnel reaching
3157 /// `127.0.0.1:<port>`) can route to them without CNI/bridge plumbing.
3158 ///
3159 /// The backend (kamaji) is responsible for **guarding** this: host
3160 /// networking is only honoured for `tier == "infra"` workloads; a
3161 /// non-infra workload that sets the annotation is rejected at deploy. See
3162 /// `validate_spec_for_constable`.
3163 pub fn wants_host_network(&self) -> bool {
3164 self.annotations
3165 .get(HOST_NETWORK_ANNOTATION)
3166 .map(|v| v == HOST_NETWORK_VALUE)
3167 .unwrap_or(false)
3168 }
3169
3170 /// Resolve the lifecycle archetype (R572-F1 / W244): the explicit
3171 /// [`Self::archetype`] if set, otherwise the pre-R572 inference from
3172 /// `volumes`/`restart_policy` this field replaces.
3173 ///
3174 /// This is the one seam callers should use to ask "can I kill and
3175 /// reschedule this?" — it is intentionally the *only* place that
3176 /// implements the fallback, so behavior for pre-existing specs (no
3177 /// `archetype` on disk) is identical to what it was before this field
3178 /// existed. Consumers (reconciler R572-F4, scheduler R572-F5) branch on
3179 /// the return value; this crate does not itself change any reconciler or
3180 /// scheduler behavior.
3181 pub fn effective_archetype(&self) -> LifecycleArchetype {
3182 self.archetype
3183 .unwrap_or_else(|| LifecycleArchetype::infer(&self.volumes, &self.restart_policy))
3184 }
3185
3186 /// Resolve what this workload needs (R860-T1 / W338): [`Self::requires`],
3187 /// then every [`Self::depends_on`] ident not already named there, folded
3188 /// into the `Anywhere` + `Wait` requirement that a bare `depends_on` entry
3189 /// has always meant.
3190 ///
3191 /// This is the one seam callers should use to ask "what does this workload
3192 /// need?" — it is intentionally the *only* place that implements the fold,
3193 /// so a spec written before `requires` existed keeps its exact previous
3194 /// meaning. Callers MUST NOT read [`Self::requires`] or
3195 /// [`Self::depends_on`] directly: reading either alone silently drops half
3196 /// the requirements of any spec that uses both.
3197 ///
3198 /// Deduplicated by ident, and `requires` wins — an ident named in both is
3199 /// the author restating a dependency with a locality, not two separate
3200 /// edges. Consumers (the deploy gate R860-T2, the placement group R860-T4)
3201 /// branch on the return value; this crate does not itself change any
3202 /// deploy or placement behaviour.
3203 pub fn effective_requirements(&self) -> Vec<Requirement> {
3204 let mut out = self.requires.clone();
3205 for ident in &self.depends_on {
3206 if out.iter().any(|req| &req.ident == ident) {
3207 continue;
3208 }
3209 out.push(Requirement {
3210 ident: ident.clone(),
3211 locality: Locality::Anywhere,
3212 supply: Supply::Wait,
3213 provides: None,
3214 });
3215 }
3216 out
3217 }
3218
3219 /// Fully-qualified mesh identity `<tenant>/<namespace>/<name>` (W206 /
3220 /// R558-F3), where `<name>` is this workload's [`MeshExpose::identity`].
3221 ///
3222 /// Within a tenant, workloads still address each other by the short
3223 /// identity (namespace disambiguates only on collision); the FQN is what
3224 /// makes the identity unambiguous across tenants and is exactly what a
3225 /// [`MeshPeer::CrossTenant`] grant names.
3226 pub fn fq_mesh_identity(&self) -> String {
3227 format!(
3228 "{}/{}/{}",
3229 self.tenant.0, self.namespace.0, self.expose.mesh.identity.0
3230 )
3231 }
3232
3233 /// The taint this workload requires its node to carry, if any (R594-F2 /
3234 /// W267 sovereign public ingress).
3235 ///
3236 /// Opt-in via `annotations["yah.placement.requires-taint"] = "<taint
3237 /// name>"` (see [`REQUIRES_TAINT_ANNOTATION`]) — same annotation-based,
3238 /// zero-blast-radius shape as [`Self::wants_host_network`], chosen so
3239 /// declaring this requirement does not force a struct-literal edit at
3240 /// every existing `WorkloadSpec { .. }` construction site the way a new
3241 /// plain field would (see R572-F1's handoff: ~26 sites for one field).
3242 ///
3243 /// Both halves have since landed: `MachineConfig.taints` (R572-F3) and the
3244 /// scheduler's affinity check in `cloud::config::RequiredSpec::matches`
3245 /// (R572-F5), which requires the key in the node's `taints` **or**
3246 /// `mesh_tags`.
3247 ///
3248 /// A key named here is one of only two ways a node taint can influence
3249 /// placement — the other is the `no-<archetype>` repulsion form. W305/
3250 /// R742-T4 makes `yah cloud validate` reject any node taint that is
3251 /// neither, so a new affinity key must be added to
3252 /// `cloud::config::AFFINITY_TAINT_KEYS` alongside the workload that
3253 /// requires it.
3254 ///
3255 /// The public-ingress appliance (W267) is the first user: a
3256 /// `kind = "container"` workload with `archetype =
3257 /// Some(LifecycleArchetype::Appliance)` and
3258 /// `requires_taint() == Some(PUBLIC_IP_TAINT)`, so yubaba may one day
3259 /// place it only on machines carrying the `"public-ip"` taint and kamaji
3260 /// supervises it like any other container (no new `Workload` variant —
3261 /// see [`Workload::Container`]'s doc comment).
3262 pub fn requires_taint(&self) -> Option<&str> {
3263 self.annotations
3264 .get(REQUIRES_TAINT_ANNOTATION)
3265 .map(String::as_str)
3266 }
3267
3268 /// The memory (MiB) a scheduler must find on a node before placing this
3269 /// workload — its **request**, as distinct from [`ResourceLimits::memory_mb`],
3270 /// which is a **ceiling** the backend turns into a cgroup `memory.max`.
3271 ///
3272 /// Opt-in via [`ResourceLimits::memory_request_mb`]; absent falls back to
3273 /// `resources.memory_mb`, so every spec that does not set it is admitted
3274 /// exactly as it was before the request existed.
3275 ///
3276 /// # Why the two numbers must not be the same one
3277 ///
3278 /// A limit answers "kill it past here"; a request answers "don't start it
3279 /// somewhere smaller than here". Generous is the safe direction for the
3280 /// first and the unschedulable direction for the second, so one field
3281 /// serving both makes a deliberately-roomy ceiling into an admission floor.
3282 ///
3283 /// That is not hypothetical: [`WorkloadSpec::for_forge`] sets a 32 GiB
3284 /// ceiling explicitly reasoned as "above physical RAM on smaller
3285 /// build-workers ⇒ effectively unlimited there" (R590-B10), and
3286 /// `CloudConfig::admit_workload` fed that same 32768 in as the R572-F5
3287 /// capacity floor. Every build-worker under 32 GiB — the three 8 GiB Pi-5s
3288 /// and the 16 GiB us-west-003 — became structurally unadmittable for *any*
3289 /// offloaded qed step, leaving one 47 GiB node as the fleet's only legal
3290 /// target for remote CI. This is R590-B10's own recorded follow-up
3291 /// ("thread a per-step memory request … instead of a blanket forge
3292 /// default"), reduced to the seam that closes the bug.
3293 pub fn memory_request_mb(&self) -> u32 {
3294 // `0` falls back too: a zero request would admit the workload on any
3295 // node at all, which is never what a declared request meant.
3296 self.resources
3297 .memory_request_mb
3298 .filter(|mb| *mb > 0)
3299 .unwrap_or(self.resources.memory_mb)
3300 }
3301
3302 /// The hard CPU ceiling (millicores) a backend may enforce, if the workload
3303 /// declares one — the exact mirror of [`Self::memory_request_mb`], which
3304 /// adds the missing *request* beside a field that is a ceiling. Here the
3305 /// field ([`ResourceLimits::cpu_millis`]) is the *request* and this adds the
3306 /// missing ceiling.
3307 ///
3308 /// Opt-in via [`ResourceLimits::cpu_limit_millis`]. Absent or `0` means **no
3309 /// ceiling**: the workload gets its declared share of a contended node and
3310 /// may burst to the whole box on an idle one. That is the default because
3311 /// `cpu_millis` is documented as a request, and every backend but one has
3312 /// always rendered it as a relative weight
3313 /// ([`ResourceLimits::cpu_shares`]).
3314 ///
3315 /// # Why this exists (R885-B5 / W344 Finding 5)
3316 ///
3317 /// R885-B1 wired the cgroup v2 driver onto the live native deploy path, and
3318 /// that driver rendered `cpu_millis` into `cpu.max` — a hard quota. Measured
3319 /// on us-east-001 on 2026-09-11: four native workloads at
3320 /// `cpu.max = 25600 100000`, i.e. capped at 0.256 of a core even on an idle
3321 /// node, where before the wiring they could burst to the whole machine. A
3322 /// request rendered as a ceiling is a semantic bug, not a missing feature.
3323 pub fn cpu_limit_millis(&self) -> Option<u32> {
3324 self.resources.cpu_limit_millis.filter(|millis| *millis > 0)
3325 }
3326
3327 /// The hard process-count ceiling (cgroup v2 `pids.max`) a backend
3328 /// enforces on this workload's leaf — R885-T2 (W344 Finding 3: "no bound
3329 /// on a fork bomb, accidental or otherwise").
3330 ///
3331 /// Shaped like [`Self::cpu_limit_millis`] (an opt-in override), but its
3332 /// default runs the OPPOSITE direction on purpose. An absent
3333 /// `cpu_limit_millis` correctly means "no ceiling", because `cpu_millis`
3334 /// already has a well-defined request-only meaning without it. There is no
3335 /// such fallback for pids: "unbounded" is precisely the bug R885-T2 closed,
3336 /// not a feature to preserve, so absent or `0` both fall back to
3337 /// [`DEFAULT_PIDS_MAX`] instead of to "unset".
3338 ///
3339 /// [`DEFAULT_PIDS_MAX`]'s doc comment records where the number comes
3340 /// from and why it does not need to be tight to be useful.
3341 ///
3342 /// Opt-in override via [`ResourceLimits::pids_max`] for a workload that
3343 /// legitimately needs a different bound.
3344 pub fn pids_limit(&self) -> u32 {
3345 self.resources
3346 .pids_max
3347 .filter(|pids| *pids > 0)
3348 .unwrap_or(DEFAULT_PIDS_MAX)
3349 }
3350
3351 /// A **floor** on the microVM scratch disk in MiB — the smallest workspace
3352 /// this workload is willing to be given, or `None` for "the backend's own
3353 /// floor is fine".
3354 ///
3355 /// Opt-in via [`ResourceLimits::scratch_floor_mb`]. Absent or `0` both mean
3356 /// **no declared floor**; the microVM backend still applies its own
3357 /// (`microvm::WORKSPACE_MIN_BYTES`), which is what actually sizes every
3358 /// workload in this tree today.
3359 ///
3360 /// # Why this replaced a field (R885-T6 / W344)
3361 ///
3362 /// It is the successor to `ResourceLimits::ephemeral_storage_mb`, which was
3363 /// deleted rather than renamed because the field **lied**. Its doc comment
3364 /// called it a "cap on the writable layer + tmpfs footprint" and no backend
3365 /// ever enforced it as one — not the OCI resources block, not docker's
3366 /// argv, and deliberately not the cgroup v2 driver. Its single live
3367 /// consumer, [`microvm::workspace::disk_size_bytes`], read it as a
3368 /// **floor**, i.e. the exact opposite. `for_forge` then set it to 512 MiB,
3369 /// a number that as a cap would have failed every build at its first
3370 /// checkout and as a floor was simply ignored.
3371 ///
3372 /// A field that means one thing at its definition and the reverse at its
3373 /// only use is not a field to keep compatible with, so per the pre-1.0
3374 /// doctrine in `CLAUDE.md` the design changed instead of being taped: the
3375 /// floor is now spelled `floor`.
3376 ///
3377 /// [`microvm::workspace::disk_size_bytes`]: https://docs.rs/kamaji
3378 pub fn scratch_floor_mb(&self) -> Option<u32> {
3379 self.resources.scratch_floor_mb.filter(|mb| *mb > 0)
3380 }
3381
3382 /// Whether this workload must be run by kamaji's **native** (fork+exec)
3383 /// backend on the node's own userland, rather than by a container backend
3384 /// (R577-T1 / W254).
3385 ///
3386 /// Opt-in via `annotations["yah.exec"] == "native"` (see
3387 /// [`NATIVE_EXEC_ANNOTATION`] / [`NATIVE_EXEC_VALUE`]) — the same
3388 /// annotation-shaped, zero-blast-radius marker as
3389 /// [`Self::wants_host_network`] and [`Self::requires_taint`], chosen over
3390 /// a new plain field for the reason R572-F1 recorded: a field forces a
3391 /// struct-literal edit at every existing construction site and an
3392 /// exhaustive-match update in `kamaji-proto`'s codec, and this marker
3393 /// needs neither.
3394 ///
3395 /// # Why an annotation and not a runtime enum on the wire
3396 ///
3397 /// The remote-execution wire already carries exactly one workload shape —
3398 /// `Workload::Container(WorkloadSpec)` — and every layer between the
3399 /// dispatcher and the node (yubaba admission, mesh assignment, log
3400 /// ingest, produced-file retrieval, teardown) is written against it. A
3401 /// Darwin build differs from a Linux build in *one* respect: there is no
3402 /// container that can host it, because you cannot containerize the Darwin
3403 /// kernel. Marking that one difference keeps the rest of the path shared
3404 /// instead of growing a parallel `exec_native` RPC that would have to
3405 /// re-implement all of it.
3406 ///
3407 /// `image` stays populated for a native workload and is **identity
3408 /// metadata only** — nothing is pulled; the native backend resolves argv
3409 /// from `entrypoint` + `command` (container semantics) and execs it on
3410 /// the host.
3411 pub fn wants_native_exec(&self) -> bool {
3412 self.annotations
3413 .get(NATIVE_EXEC_ANNOTATION)
3414 .map(|v| v == NATIVE_EXEC_VALUE)
3415 .unwrap_or(false)
3416 }
3417
3418 /// Whether this workload must be run by kamaji's **microVM** backend —
3419 /// booted in its own KVM guest with its own kernel, rather than sharing the
3420 /// host kernel with every other workload on the node (R605-F8 / W325 §5).
3421 ///
3422 /// Opt-in via `annotations["yah.exec"] == "microvm"` (see
3423 /// [`NATIVE_EXEC_ANNOTATION`] / [`MICROVM_EXEC_VALUE`]).
3424 ///
3425 /// # Why the *same* key as native exec, not a new one
3426 ///
3427 /// W325's Shape A calls this "a sibling branch on a new annotation value",
3428 /// and the value — not the key — is the whole point. `yah.exec` names the
3429 /// execution substrate, and a workload has exactly one:
3430 ///
3431 /// | `yah.exec` | substrate | kernel | isolation |
3432 /// |---|---|---|---|
3433 /// | *(absent)* | container backend | host's | namespaces + cgroup |
3434 /// | `native` | fork+exec on the host | host's | **none** |
3435 /// | `microvm` | KVM guest | **its own** | hardware |
3436 ///
3437 /// A second key (`yah.isolation = microvm`, say) would make
3438 /// `yah.exec = native` + `yah.isolation = microvm` *expressible*, and
3439 /// therefore something a dispatcher could emit and a backend would have to
3440 /// refuse — exactly the refusal `validate_native_exec_spec` already has to
3441 /// carry for the `yah.sandbox` pair, and for the same avoidable reason. A
3442 /// map key holds one value, so on this key the three substrates are
3443 /// mutually exclusive *by construction*: there is no spec on which both
3444 /// this and [`Self::wants_native_exec`] return `true`, and
3445 /// `exec_substrate_markers_are_mutually_exclusive_by_construction` pins
3446 /// that.
3447 ///
3448 /// # What the marker does and does not promise
3449 ///
3450 /// Like every marker on this struct it is **inert metadata** — it declares
3451 /// intent and nothing more. Whether a node can honour it is a node
3452 /// capability question (`/dev/kvm`, a guest kernel, a rootfs; see W325 §4),
3453 /// and a node whose kamaji has no microVM backend configured **refuses**
3454 /// the deploy rather than falling back to a container. That refusal is
3455 /// deliberate and mirrors R577-T1's: a caller asking for microVM isolation
3456 /// is asking for the one property a container cannot provide, so silently
3457 /// downgrading it would return success while delivering the thing the
3458 /// caller specifically declined.
3459 ///
3460 /// `image` is identity metadata only, as it is for native exec — nothing is
3461 /// pulled. The guest's root filesystem comes from the node's configured
3462 /// rootfs image, and argv is resolved from `entrypoint` + `command` with
3463 /// container semantics, so one spec shape drives all three substrates.
3464 pub fn wants_microvm(&self) -> bool {
3465 self.annotations
3466 .get(NATIVE_EXEC_ANNOTATION)
3467 .map(|v| v == MICROVM_EXEC_VALUE)
3468 .unwrap_or(false)
3469 }
3470
3471 /// The substrate this spec selects, as an *ordered* value (R894-F1).
3472 ///
3473 /// The same reading [`Self::wants_native_exec`] and [`Self::wants_microvm`]
3474 /// perform, collapsed into one total function so that "which substrate is
3475 /// this" has a single answer rather than two booleans a caller re-combines.
3476 /// Every existing `if wants_native_exec() … else if wants_microvm() …`
3477 /// ladder in the tree is that re-combination, and R605-F8's own doc notes
3478 /// that a duplicated ladder is exactly what drifts when a fourth substrate
3479 /// arrives.
3480 ///
3481 /// An unrecognised `yah.exec` value reads as [`ExecSubstrate::Container`],
3482 /// preserving what both predicates already do. That is the fail-*closed*
3483 /// direction for the trust check in [`Self::trust`]'s consumers: a typo'd
3484 /// `microvm` on an untrusted spec reads as a container and is refused,
3485 /// rather than reading as the microVM the author meant to ask for.
3486 pub fn exec_substrate(&self) -> ExecSubstrate {
3487 match self
3488 .annotations
3489 .get(NATIVE_EXEC_ANNOTATION)
3490 .map(String::as_str)
3491 {
3492 Some(v) if v == NATIVE_EXEC_VALUE => ExecSubstrate::Native,
3493 Some(v) if v == MICROVM_EXEC_VALUE => ExecSubstrate::MicroVm,
3494 _ => ExecSubstrate::Container,
3495 }
3496 }
3497
3498 /// How far this workload's code is trusted, from [`TRUST_ANNOTATION`]
3499 /// (R894-F1).
3500 ///
3501 /// Absent means [`TrustLevel::Trusted`] — see that type's docs for why that
3502 /// default is safe only because of *where* the untrusted marker is stamped.
3503 /// An unrecognised value is an `Err`, never a fallback to either side.
3504 pub fn trust(&self) -> std::result::Result<TrustLevel, TrustDeclError> {
3505 match self.annotations.get(TRUST_ANNOTATION).map(|v| v.trim()) {
3506 None => Ok(TrustLevel::Trusted),
3507 Some(v) if v == TRUST_TRUSTED_VALUE => Ok(TrustLevel::Trusted),
3508 Some(v) if v == TRUST_UNTRUSTED_VALUE => Ok(TrustLevel::Untrusted),
3509 Some(other) => Err(TrustDeclError::UnknownLevel(other.to_string())),
3510 }
3511 }
3512
3513 /// Stamp this spec as carrying code the operator did not write, and raise
3514 /// its substrate request to the minimum that trust level requires.
3515 ///
3516 /// **This is the choke-point verb.** Any constructor that turns third-party
3517 /// bytes (a tenant image, a vended camp, a user-supplied argv) into a
3518 /// `WorkloadSpec` calls it *in the same function that takes those bytes*, so
3519 /// the marker cannot be lost by a caller who forgets. R823 is the first such
3520 /// path.
3521 ///
3522 /// It raises the substrate rather than only marking trust because the two
3523 /// halves belong to the same decision and splitting them across two call
3524 /// sites is how one of them goes missing. Raising is one-directional: a
3525 /// caller that already asked for something at least as strong keeps its own
3526 /// request, so stamping a spec that explicitly wants a microVM is a no-op
3527 /// and stamping is idempotent.
3528 ///
3529 /// A spec that had asked for `native` becomes `microvm`. That is not a
3530 /// silent downgrade — it is a *widening* of isolation, the safe direction —
3531 /// and it happens at the moment the untrusted origin is established, not at
3532 /// admission, where the same mismatch is a refusal.
3533 pub fn stamp_untrusted(&mut self) {
3534 self.annotations.insert(
3535 TRUST_ANNOTATION.to_string(),
3536 TRUST_UNTRUSTED_VALUE.to_string(),
3537 );
3538 let floor = TrustLevel::Untrusted.minimum_substrate();
3539 if self.exec_substrate() < floor {
3540 match floor.annotation_value() {
3541 Some(v) => {
3542 self.annotations
3543 .insert(NATIVE_EXEC_ANNOTATION.to_string(), v.to_string());
3544 }
3545 None => {
3546 self.annotations.remove(NATIVE_EXEC_ANNOTATION);
3547 }
3548 }
3549 }
3550 }
3551
3552 /// Whether this workload builds its **own unprivileged container sandbox**
3553 /// inside the one the backend gives it, and therefore needs the two
3554 /// capabilities plus the `no_new_privs` relaxation that setting up a
3555 /// user namespace requires (R636-B2).
3556 ///
3557 /// Opt-in via `annotations["yah.sandbox"] == "nested"` (see
3558 /// [`NESTED_SANDBOX_ANNOTATION`] / [`NESTED_SANDBOX_VALUE`]) — the same
3559 /// annotation-shaped, zero-blast-radius marker as
3560 /// [`Self::wants_host_network`] and [`Self::wants_native_exec`].
3561 ///
3562 /// # What it actually grants, and why exactly that
3563 ///
3564 /// Rootless BuildKit (the only user today: remote `build-image` steps
3565 /// dispatch `moby/buildkit:*-rootless`) boots through `rootlesskit`, which
3566 /// must map a range of sub-uids into a fresh user namespace. It does that
3567 /// by exec'ing the **setuid-root** helpers `newuidmap` / `newgidmap`, so
3568 /// it needs `CAP_SETUID` + `CAP_SETGID` in the bounding set *and*
3569 /// `noNewPrivileges = false` (with `no_new_privs` on, the kernel silently
3570 /// strips the setuid bit and the helper fails with "Could not set caps").
3571 ///
3572 /// Each of those three was measured on us-west-002 to be **individually
3573 /// necessary** — dropping any one of them puts `rootlesskit` back to
3574 /// failing before the first layer:
3575 ///
3576 /// | grant | `rootlesskit` result |
3577 /// |---|---|
3578 /// | baseline (`CAP_NET_BIND_SERVICE` only, `nnp` on) | `fork/exec /usr/bin/newuidmap: operation not permitted` |
3579 /// | `+CAP_SETUID` only, `nnp` off | `fork/exec /usr/bin/newgidmap: operation not permitted` |
3580 /// | `+CAP_SETUID +CAP_SETGID`, `nnp` **on** | `newuidmap: Could not set caps` |
3581 /// | `+CAP_SETUID +CAP_SETGID`, `nnp` off | starts; build runs to completion |
3582 ///
3583 /// It is deliberately *not* `CAP_SYS_ADMIN`: a non-rootless buildkitd
3584 /// would need that instead, which is a far wider grant. Emptying
3585 /// `/etc/subuid` to force `rootlesskit`'s single-mapping path does not
3586 /// avoid the helpers either — it just fails earlier with "No subuid
3587 /// ranges found".
3588 ///
3589 /// **The backend guards this.** Like host networking, it is honoured only
3590 /// for `tier == "infra"` workloads; a non-infra workload that sets the
3591 /// annotation is rejected at deploy. Every other workload keeps the
3592 /// `CAP_NET_BIND_SERVICE`-only, `no_new_privs` baseline.
3593 ///
3594 /// # Mutually exclusive with [`Self::wants_native_exec`]
3595 ///
3596 /// This grant is defined in terms of an **OCI process spec** — a
3597 /// capability set and a `noNewPrivileges` bit. A native (fork+exec)
3598 /// workload has no OCI spec, so there is nothing to apply it to; kamaji
3599 /// refuses a spec carrying both markers rather than accepting a request
3600 /// for widened privileges and silently dropping it (R577-T1 owns that
3601 /// refusal). The two are independent *annotations* — neither implies the
3602 /// other, which is what
3603 /// `nested_sandbox_marker_is_independent_of_the_other_markers` pins — but
3604 /// they are not a legal *pair*.
3605 ///
3606 /// If a future runtime does have a sandbox worth widening (a MacVM under
3607 /// W254, say), give it its own annotation rather than relaxing that
3608 /// refusal. The grant this marker names is `CAP_SETUID` + `CAP_SETGID` +
3609 /// `no_new_privs` off and nothing else; letting it mean a different
3610 /// privilege set per backend would make "what does `yah.sandbox=nested`
3611 /// grant?" unanswerable without knowing which backend received it, which
3612 /// is precisely what a security-relevant marker must not be.
3613 pub fn wants_nested_sandbox(&self) -> bool {
3614 self.annotations
3615 .get(NESTED_SANDBOX_ANNOTATION)
3616 .map(|v| v == NESTED_SANDBOX_VALUE)
3617 .unwrap_or(false)
3618 }
3619
3620 /// The host paths this workload declares it **writes to** (R885-B11 / W344),
3621 /// from [`WRITABLE_PATHS_ANNOTATION`]. Empty when nothing is declared.
3622 ///
3623 /// This is the input to kamaji's native filesystem confinement: a native
3624 /// workload is a plain `fork`+`exec` on the host, so the only description of
3625 /// what it may write is the one its spec carries. `kamaji::sandbox` unions
3626 /// these with the spec's `Bind` volumes and denies writes everywhere else —
3627 /// and confines **only** a workload that declares one or the other, so a
3628 /// spec that says nothing about its writes is not silently guessed at.
3629 ///
3630 /// # Why an annotation rather than a field
3631 ///
3632 /// It qualifies the substrate markers (`yah.exec`, `yah.sandbox`) that
3633 /// already ride the annotation map, and it is a policy hint for the
3634 /// sandbox rather than a fact about the workload's shape. (The original
3635 /// reason — a positional postcard wire on which every new field was a
3636 /// protocol bump — stopped holding at kamaji-proto V13, R896-F2.)
3637 ///
3638 /// ```toml
3639 /// [annotations]
3640 /// "yah.writable-paths" = "/var/lib/yah/qed,/tmp"
3641 /// ```
3642 ///
3643 /// # What is refused, and why refused rather than normalized
3644 ///
3645 /// A relative entry, a `.`/`..` component, an empty entry (a stray comma),
3646 /// or a duplicate. Each of these would otherwise turn into a *grant* — a
3647 /// landlock rule is `PathBeneath`, so a mis-resolved path does not fail
3648 /// closed, it opens a subtree nobody asked for. Normalizing silently is how
3649 /// you grant write access to the wrong directory and never hear about it.
3650 ///
3651 /// Note that a declared path is a **ceiling, not a mount**: nothing here
3652 /// creates a directory. A path that does not exist on the node is skipped
3653 /// (with a warning) when the ruleset is built.
3654 pub fn writable_paths(&self) -> Result<Vec<PathBuf>, WritablePathsDeclError> {
3655 match self.annotations.get(WRITABLE_PATHS_ANNOTATION) {
3656 None => Ok(Vec::new()),
3657 Some(raw) => parse_writable_paths(raw),
3658 }
3659 }
3660
3661 /// The durability tier this workload declares for its own state, if it
3662 /// declares one at all (R850-P4).
3663 ///
3664 /// `Ok(None)` and `Ok(Some(tier: DurabilityTier::None))` are **different
3665 /// answers and must stay different**: the first is "nobody said", the
3666 /// second is "somebody looked and decided not to". A named volume with no
3667 /// declaration is the shape that loses every byte when its node dies, and
3668 /// collapsing the two would let the analyzer report that case in the same
3669 /// words as a deliberately-ephemeral cache.
3670 ///
3671 /// ```toml
3672 /// [durability]
3673 /// tier = "stream" # none|snapshot|dedup|stream
3674 /// engine = "turso" # required by every tier but "none"
3675 /// store = "s3://yah-backups/noisetable-account"
3676 /// subjects = ["accounts.db", "passkeys.db", "sessions.db"]
3677 /// rpo_seconds = 120 # stream only
3678 /// ```
3679 ///
3680 /// # Read this, not the field
3681 ///
3682 /// Serde refuses an unknown tier or engine and a non-numeric RPO, but it
3683 /// cannot see the rules that span fields — a shipping tier with no store,
3684 /// an RPO on a snapshot tier, a subject that escapes its volume. Those are
3685 /// [`Durability::check`], and this accessor is the one read that applies
3686 /// it, so a malformed declaration refuses at every consumer rather than only
3687 /// at the ones that remembered to validate.
3688 ///
3689 /// # The retired annotations (R896-F3)
3690 ///
3691 /// This declaration was the `yah.durability.*` annotation family until
3692 /// kamaji-proto V13 made a field addition cross a skew. Any surviving key
3693 /// of that family is [`DurabilityDeclError::RetiredAnnotation`], never
3694 /// ignored: an ignored annotation is indistinguishable from an absent one,
3695 /// and "absent" here means "this database has no backup".
3696 ///
3697 /// # Why `engine` and `subjects` are not optional (R850-F1)
3698 ///
3699 /// The tier vocabulary is `turso-backup`-shaped, and P4 shipped it on a
3700 /// *generic* `WorkloadSpec` — so a Postgres appliance could declare `tier =
3701 /// "stream"` and mean something no code in this tree can do. `engine` makes
3702 /// that claim explicit and refusable at parse time rather than at 3am.
3703 ///
3704 /// `subjects` exists because a restore has a *file* as its unit and a
3705 /// workload has a *volume*. The driving case (R850) is one process with
3706 /// three turso databases inside one named volume; "restore the volume" is
3707 /// not a thing turso-backup can do, and guessing which files in a directory
3708 /// are databases is guessing about the only copy of somebody's data. Paths
3709 /// are volume-relative — the same string the analyzer prints and the
3710 /// hydrate helper joins onto the host volume root — and are validated
3711 /// against traversal, because they name a host path something will write to.
3712 ///
3713 /// # What is and is not wired
3714 ///
3715 /// This accessor plus [`validate::shape`]'s check on it is the whole of the
3716 /// runtime effect today: **declaring a tier does not yet cause a backup to
3717 /// happen.** `turso-backup` implements all three tiers
3718 /// ([`DurabilityTier::Snapshot`] = its tier 1a, [`DurabilityTier::Dedup`] =
3719 /// 1b, [`DurabilityTier::Stream`] = 2 with restore-by-frame-replay) and,
3720 /// since R850-F1, the fencing epoch a hydrate must hold
3721 /// (`turso_backup::claim`). Nothing in yubaba's reconciler calls into any of
3722 /// it yet.
3723 ///
3724 /// Until that lands, the declaration's value is exactly that
3725 /// `cloud::topology` can tell an operator, *before* the topology is
3726 /// committed, which of their stateful workloads has no second copy of its
3727 /// bytes anywhere.
3728 pub fn durability(&self) -> Result<Option<&Durability>, DurabilityDeclError> {
3729 if let Some(key) = self
3730 .annotations
3731 .keys()
3732 .filter(|k| k.starts_with("yah.durability."))
3733 .min()
3734 {
3735 return Err(DurabilityDeclError::RetiredAnnotation {
3736 key: key.clone(),
3737 field: retired_annotation_field(key).unwrap_or("durability"),
3738 });
3739 }
3740 let Some(d) = &self.durability else {
3741 return Ok(None);
3742 };
3743 d.check()?;
3744 Ok(Some(d))
3745 }
3746
3747 /// The first annotation (in key order) this spec still carries from a
3748 /// family R896-F3 moved into typed fields, paired with the field that
3749 /// replaced it. `None` for every spec written against the fields.
3750 ///
3751 /// [`validate::shape`] refuses on it. It exists because nothing reads
3752 /// those keys any more, and a key nothing reads fails silently: a
3753 /// `yah.limits.pids-max` would quietly fall back to the default bound, and
3754 /// a `yah.durability.tier` would quietly mean "no backup".
3755 pub fn retired_annotation(&self) -> Option<(&str, &'static str)> {
3756 self.annotations
3757 .keys()
3758 .filter_map(|k| retired_annotation_field(k).map(|field| (k.as_str(), field)))
3759 .min()
3760 }
3761}
3762
3763/// The typed field that replaced a retired annotation key (R896-F3), or `None`
3764/// for a key that was never retired.
3765///
3766/// Unrecognised keys under a retired prefix map to their parent field rather
3767/// than to `None`: a misspelled `yah.durability.teir` is still an attempt at a
3768/// durability declaration, and passing it would be the silent drop this exists
3769/// to prevent.
3770pub fn retired_annotation_field(key: &str) -> Option<&'static str> {
3771 Some(match key {
3772 "yah.placement.memory-request-mb" => "resources.memory_request_mb",
3773 "yah.limits.cpu-millis" => "resources.cpu_limit_millis",
3774 "yah.limits.pids-max" => "resources.pids_max",
3775 "yah.limits.scratch-floor-mb" => "resources.scratch_floor_mb",
3776 "yah.durability.tier" => "durability.tier",
3777 "yah.durability.engine" => "durability.engine",
3778 "yah.durability.store" => "durability.store",
3779 "yah.durability.subjects" => "durability.subjects",
3780 "yah.durability.rpo-seconds" => "durability.rpo_seconds",
3781 "yah.durability.state-mb" => "durability.state_mb",
3782 k if k.starts_with("yah.limits.") => "resources",
3783 k if k.starts_with("yah.durability.") => "durability",
3784 _ => return None,
3785 })
3786}
3787
3788/// Validate [`Durability::subjects`].
3789///
3790/// Every rule here exists because each subject is joined onto a host directory
3791/// (`/var/lib/yah/kamaji/volumes/<name>`) by something that then *writes* to
3792/// it. An absolute path or a `..` component would put a restore outside the
3793/// volume it was scoped to, so those are refused by name rather than
3794/// normalized — silently rewriting a path a human typed is how you restore the
3795/// right bytes to the wrong place.
3796fn check_durability_subjects(subjects: &[String]) -> Result<(), DurabilityDeclError> {
3797 for (i, s) in subjects.iter().enumerate() {
3798 if s.trim().is_empty() {
3799 return Err(DurabilityDeclError::EmptySubject);
3800 }
3801 if s.starts_with('/') || s.starts_with('\\') || s.contains(':') {
3802 return Err(DurabilityDeclError::AbsoluteSubject { subject: s.clone() });
3803 }
3804 if s.split('/').any(|c| c == "." || c == "..") {
3805 return Err(DurabilityDeclError::TraversingSubject { subject: s.clone() });
3806 }
3807 if subjects[..i].contains(s) {
3808 return Err(DurabilityDeclError::DuplicateSubject { subject: s.clone() });
3809 }
3810 }
3811 Ok(())
3812}
3813
3814/// Which database engine a [`DurabilityTier`]'s three tier names refer to
3815/// (R850-F1).
3816///
3817/// One variant today, and that is the point: the tier vocabulary was minted
3818/// from `turso-backup`'s implementation, so an appliance running anything else
3819/// gets a refusal at parse time instead of a tier nothing can honour. Adding an
3820/// engine means adding a restore path, not adding a string.
3821#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
3822#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3823#[serde(rename_all = "snake_case")]
3824pub enum DurabilityEngine {
3825 /// Turso / libSQL, via `turso-backup`. `snapshot` is its tier 1a `VACUUM
3826 /// INTO`, `dedup` its tier 1b page-dedup, `stream` its tier 2 WAL-frame
3827 /// streaming with restore-by-frame-replay.
3828 Turso,
3829}
3830
3831impl DurabilityEngine {
3832 /// The wire/TOML spelling, so a diagnostic and the file it points at agree.
3833 pub fn as_str(&self) -> &'static str {
3834 match self {
3835 Self::Turso => "turso",
3836 }
3837 }
3838}
3839
3840impl fmt::Display for DurabilityEngine {
3841 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3842 f.write_str(self.as_str())
3843 }
3844}
3845
3846/// A workload's declared durability tier — where a second copy of its state
3847/// lives, and how far behind that copy is allowed to be (R850-P4).
3848///
3849/// The three non-`None` variants name `turso-backup`'s three implemented
3850/// tiers. They are spelled here rather than imported because `workload-spec`
3851/// is a leaf crate every fleet node links and `turso-backup` is a service-side
3852/// dependency; the coupling that matters is the vocabulary, not the types.
3853#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
3854#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3855#[serde(rename_all = "snake_case")]
3856pub enum DurabilityTier {
3857 /// Deliberately no second copy. State lives only where the container runs
3858 /// and is gone when that node is. Legitimate for caches and scratch — and
3859 /// it is a *statement*, which is why it is not the same as declaring
3860 /// nothing (see [`WorkloadSpec::durability`]).
3861 None,
3862
3863 /// `turso-backup` tier 1a — periodic full `VACUUM INTO` snapshot to the
3864 /// object store. Recovery point is the last snapshot, so the loss window is
3865 /// the snapshot interval, which this declaration does not carry: a
3866 /// snapshot-tier workload's RPO is whatever schedules it.
3867 Snapshot,
3868
3869 /// `turso-backup` tier 1b — incremental page-dedup snapshot. Same recovery
3870 /// *point* semantics as [`Self::Snapshot`]; cheaper per run, so in practice
3871 /// a shorter interval.
3872 Dedup,
3873
3874 /// `turso-backup` tier 2 — WAL-frame streaming with restore by frame
3875 /// replay. The only tier with a *bounded, declarable* loss window; see
3876 /// [`Durability::rpo_seconds`] and
3877 /// `turso_backup::stream::DEFAULT_RPO_TARGET` (120 s), which is what an
3878 /// undeclared RPO means in practice.
3879 Stream,
3880}
3881
3882impl DurabilityTier {
3883 /// The wire/TOML spelling, so a diagnostic and the file it points at agree.
3884 pub fn as_str(&self) -> &'static str {
3885 match self {
3886 Self::None => "none",
3887 Self::Snapshot => "snapshot",
3888 Self::Dedup => "dedup",
3889 Self::Stream => "stream",
3890 }
3891 }
3892
3893 /// Whether this tier puts bytes in an object store — i.e. whether there is
3894 /// a copy to hydrate from after the node is gone.
3895 pub fn ships_bytes(&self) -> bool {
3896 !matches!(self, Self::None)
3897 }
3898}
3899
3900impl fmt::Display for DurabilityTier {
3901 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3902 f.write_str(self.as_str())
3903 }
3904}
3905
3906/// A workload's durability declaration — [`WorkloadSpec::durability`].
3907///
3908/// Every field but `tier` defaults, because which of them are required depends
3909/// on the tier, and that is a rule serde cannot express. [`Self::check`] is
3910/// where it is enforced; read the declaration through
3911/// [`WorkloadSpec::durability`], which applies it.
3912#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
3913#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3914pub struct Durability {
3915 pub tier: DurabilityTier,
3916 /// Which engine's tier vocabulary this is. `Some` exactly when
3917 /// [`DurabilityTier::ships_bytes`] — enforced by [`Self::check`] (R850-F1).
3918 #[serde(default)]
3919 #[ts(optional = nullable)]
3920 pub engine: Option<DurabilityEngine>,
3921 /// Object-store URL the copy lives at. `Some` (and non-blank) exactly when
3922 /// [`DurabilityTier::ships_bytes`] — enforced by [`Self::check`].
3923 #[serde(default)]
3924 #[ts(optional = nullable)]
3925 pub store: Option<String>,
3926 /// Volume-relative paths of the database files this tier covers, in
3927 /// declaration order. Non-empty exactly when
3928 /// [`DurabilityTier::ships_bytes`] — enforced by [`Self::check`] (R850-F1).
3929 ///
3930 /// Volume-relative, never absolute: the same string is joined onto the
3931 /// container's mount target when read as documentation and onto
3932 /// `/var/lib/yah/kamaji/volumes/<name>` when a hydrate writes it. Each is
3933 /// also the object-store key suffix under [`Self::store`], so the layout an
3934 /// operator sees in the bucket mirrors the layout on the volume.
3935 #[serde(default)]
3936 pub subjects: Vec<String>,
3937 /// Declared recovery-point objective in seconds. [`DurabilityTier::Stream`]
3938 /// only; `None` there means `turso_backup::stream::DEFAULT_RPO_TARGET`.
3939 #[serde(default)]
3940 #[ts(optional = nullable)]
3941 pub rpo_seconds: Option<u32>,
3942 /// Expected steady-state size of this workload's state, in MiB — the input
3943 /// a cold-start-from-object-store estimate needs and cannot get anywhere
3944 /// else. The microVM scratch floor ([`WorkloadSpec::scratch_floor_mb`]) is
3945 /// not it: that sizes a job's ephemeral workspace, and a named volume is
3946 /// neither ephemeral nor a workspace.
3947 ///
3948 /// **Declared, never measured.** Any recovery-time figure derived from it
3949 /// inherits that, and must say so at the point it is printed.
3950 #[serde(default)]
3951 #[ts(optional = nullable)]
3952 pub state_mb: Option<u32>,
3953}
3954
3955impl Durability {
3956 /// The rules that span fields, which serde cannot enforce. Checked in the
3957 /// order a human would fix them: where the copy goes, when, what engine,
3958 /// which files.
3959 pub fn check(&self) -> Result<(), DurabilityDeclError> {
3960 let tier = self.tier;
3961 let ships = tier.ships_bytes();
3962
3963 // A tier that ships bytes needs to name the somewhere. Defaulting it
3964 // would put the only copy of a database in a bucket nobody chose.
3965 let store = self.store.as_deref().filter(|s| !s.trim().is_empty());
3966 if ships && store.is_none() {
3967 return Err(DurabilityDeclError::MissingStore { tier });
3968 }
3969 if !ships && self.store.is_some() {
3970 return Err(DurabilityDeclError::StoreWithoutTier);
3971 }
3972
3973 if self.rpo_seconds.is_some() && tier != DurabilityTier::Stream {
3974 return Err(DurabilityDeclError::RpoOnNonStreamTier { tier });
3975 }
3976
3977 // R850-F1: the engine axis. Required by every tier that ships bytes,
3978 // because the three tier names are turso-backup's and a spec that means
3979 // something else must say so rather than be discovered at restore time.
3980 match (ships, self.engine) {
3981 (true, None) => return Err(DurabilityDeclError::MissingEngine { tier }),
3982 (false, Some(_)) => return Err(DurabilityDeclError::EngineWithoutTier),
3983 _ => {}
3984 }
3985
3986 match (ships, self.subjects.is_empty()) {
3987 (true, true) => return Err(DurabilityDeclError::MissingSubjects { tier }),
3988 (false, false) => return Err(DurabilityDeclError::SubjectsWithoutTier),
3989 _ => {}
3990 }
3991 check_durability_subjects(&self.subjects)
3992 }
3993}
3994
3995/// A durability declaration that cannot be acted on.
3996///
3997/// Every variant is a *refusal to guess*. The alternative — falling back to
3998/// "undeclared" on a malformed value, the way [`WorkloadSpec::memory_request_mb`]
3999/// falls back to its ceiling — is safe there and unsafe here: a mistyped memory
4000/// request costs a placement, a mistyped durability tier costs the database.
4001/// (An unknown tier or engine, or a non-numeric RPO, no longer reaches this
4002/// type: those are serde refusals on the typed field.)
4003#[derive(Debug, Clone, PartialEq, Eq)]
4004pub enum DurabilityDeclError {
4005 /// R896-F3: the spec still declares durability through the retired
4006 /// `yah.durability.*` annotations. Nothing reads them, so passing the spec
4007 /// would silently mean "no backup".
4008 RetiredAnnotation { key: String, field: &'static str },
4009 /// A tier that ships bytes with nowhere to ship them.
4010 MissingStore { tier: DurabilityTier },
4011 /// `tier = "none"` with a store — contradictory, and the reader cannot
4012 /// tell which half is the mistake.
4013 StoreWithoutTier,
4014 /// An RPO on a tier that has no bounded loss window to state.
4015 RpoOnNonStreamTier { tier: DurabilityTier },
4016 /// R850-F1: a bytes-shipping tier with no engine. The tier names are
4017 /// turso-backup's; a spec that means a different engine has to say so.
4018 MissingEngine { tier: DurabilityTier },
4019 /// R850-F1: an engine alongside `tier = "none"` — nothing ships, so there
4020 /// is nothing for an engine to be the engine *of*.
4021 EngineWithoutTier,
4022 /// R850-F1: a bytes-shipping tier that names no database files.
4023 MissingSubjects { tier: DurabilityTier },
4024 /// R850-F1: subjects alongside `tier = "none"`.
4025 SubjectsWithoutTier,
4026 /// R850-F1: a blank entry in the subject list. Skipping it silently would
4027 /// hide a truncated list.
4028 EmptySubject,
4029 /// R850-F1: a subject that is not volume-relative.
4030 AbsoluteSubject { subject: String },
4031 /// R850-F1: a subject containing a `.` or `..` component.
4032 TraversingSubject { subject: String },
4033 /// R850-F1: the same subject listed twice — it would be backed up twice
4034 /// under one key and restored twice over itself.
4035 DuplicateSubject { subject: String },
4036}
4037
4038impl fmt::Display for DurabilityDeclError {
4039 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4040 match self {
4041 Self::RetiredAnnotation { key, field } => write!(
4042 f,
4043 "annotation {key:?} is no longer read — durability is the typed `{field}` \
4044 field since R896-F3; move the declaration into a [durability] table, because \
4045 an ignored annotation would silently mean this workload has no backup"
4046 ),
4047 Self::MissingStore { tier } => write!(
4048 f,
4049 "durability.tier = \"{tier}\" needs durability.store — there is no default \
4050 bucket, because a default would put the only copy of this workload's state \
4051 somewhere nobody chose"
4052 ),
4053 Self::StoreWithoutTier => write!(
4054 f,
4055 "durability.store is set alongside durability.tier = \"none\"; drop one — \
4056 either the state is backed up or it is deliberately not"
4057 ),
4058 Self::RpoOnNonStreamTier { tier } => write!(
4059 f,
4060 "durability.rpo_seconds applies only to durability.tier = \"stream\", not \
4061 \"{tier}\" — a snapshot tier's recovery point is set by whatever schedules \
4062 the snapshot, not by the spec"
4063 ),
4064 Self::MissingEngine { tier } => write!(
4065 f,
4066 "durability.tier = \"{tier}\" needs durability.engine = \"turso\" — the tier \
4067 vocabulary is turso-backup's, and a declaration that does not say so cannot \
4068 be acted on"
4069 ),
4070 Self::EngineWithoutTier => write!(
4071 f,
4072 "durability.engine is set alongside durability.tier = \"none\"; nothing \
4073 ships, so drop one"
4074 ),
4075 Self::MissingSubjects { tier } => write!(
4076 f,
4077 "durability.tier = \"{tier}\" needs durability.subjects — a restore's unit is \
4078 a database file, not a volume, and guessing which files in the volume are \
4079 databases is guessing about the only copy of this workload's state"
4080 ),
4081 Self::SubjectsWithoutTier => write!(
4082 f,
4083 "durability.subjects is set alongside durability.tier = \"none\"; nothing \
4084 ships, so drop one"
4085 ),
4086 Self::EmptySubject => write!(
4087 f,
4088 "durability.subjects has a blank entry; every entry must name a database file"
4089 ),
4090 Self::AbsoluteSubject { subject } => write!(
4091 f,
4092 "durability.subjects entry {subject:?} must be relative to the workload's \
4093 volume — an absolute path would restore outside it"
4094 ),
4095 Self::TraversingSubject { subject } => write!(
4096 f,
4097 "durability.subjects entry {subject:?} contains a \".\" or \"..\" component; \
4098 it would restore outside the volume it is scoped to"
4099 ),
4100 Self::DuplicateSubject { subject } => {
4101 write!(f, "durability.subjects names {subject:?} twice")
4102 }
4103 }
4104 }
4105}
4106
4107impl std::error::Error for DurabilityDeclError {}
4108
4109/// Split and validate [`WRITABLE_PATHS_ANNOTATION`].
4110///
4111/// Mirrors [`check_durability_subjects`] in shape and in temperament, with the
4112/// polarity of the absolute-path rule flipped: a subject is joined onto a volume
4113/// root and must therefore be relative, while a writable path names a host
4114/// directory outright and must therefore be absolute. Both refuse rather than
4115/// normalize, for the same reason — the result is handed to something that
4116/// grants access at that path.
4117fn parse_writable_paths(raw: &str) -> Result<Vec<PathBuf>, WritablePathsDeclError> {
4118 let mut out: Vec<PathBuf> = Vec::new();
4119 for part in raw.split(',') {
4120 let s = part.trim();
4121 if s.is_empty() {
4122 return Err(WritablePathsDeclError::EmptyPath);
4123 }
4124 if !s.starts_with('/') {
4125 return Err(WritablePathsDeclError::RelativePath {
4126 path: s.to_string(),
4127 });
4128 }
4129 if s.split('/').any(|c| c == "." || c == "..") {
4130 return Err(WritablePathsDeclError::TraversingPath {
4131 path: s.to_string(),
4132 });
4133 }
4134 let path = PathBuf::from(s);
4135 if out.contains(&path) {
4136 return Err(WritablePathsDeclError::DuplicatePath {
4137 path: s.to_string(),
4138 });
4139 }
4140 out.push(path);
4141 }
4142 Ok(out)
4143}
4144
4145/// Why a [`WRITABLE_PATHS_ANNOTATION`] value could not be read (R885-B11).
4146///
4147/// Every variant is a refusal to guess, for the reason
4148/// [`WorkloadSpec::writable_paths`] records: each entry becomes a *grant*, so a
4149/// value nobody can read unambiguously must fail admission rather than resolve
4150/// to whichever subtree the ambiguity happened to point at.
4151#[derive(Debug, Clone, PartialEq, Eq)]
4152pub enum WritablePathsDeclError {
4153 /// An empty entry — a stray or trailing comma. Skipping it silently would
4154 /// hide a truncated list.
4155 EmptyPath,
4156 /// A relative entry. There is no directory for it to be relative *to*: the
4157 /// declaration is read on the node, by a daemon whose own working directory
4158 /// is whatever systemd gave it.
4159 RelativePath { path: String },
4160 /// A `.` or `..` component. The resolved path is not the written one.
4161 TraversingPath { path: String },
4162 /// The same path twice — one of the two is a mistake, and the reader cannot
4163 /// tell which.
4164 DuplicatePath { path: String },
4165}
4166
4167impl fmt::Display for WritablePathsDeclError {
4168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4169 match self {
4170 Self::EmptyPath => write!(
4171 f,
4172 "{WRITABLE_PATHS_ANNOTATION} has an empty entry (a stray or trailing comma)"
4173 ),
4174 Self::RelativePath { path } => write!(
4175 f,
4176 "{WRITABLE_PATHS_ANNOTATION} entry {path:?} must be an absolute host path"
4177 ),
4178 Self::TraversingPath { path } => write!(
4179 f,
4180 "{WRITABLE_PATHS_ANNOTATION} entry {path:?} contains a \".\" or \"..\" component; \
4181 it would grant write access to a directory other than the one written down"
4182 ),
4183 Self::DuplicatePath { path } => {
4184 write!(f, "{WRITABLE_PATHS_ANNOTATION} names {path:?} twice")
4185 }
4186 }
4187 }
4188}
4189
4190impl std::error::Error for WritablePathsDeclError {}
4191
4192#[cfg(test)]
4193mod writable_paths_tests {
4194 use super::*;
4195
4196 fn spec_with(annotation: Option<&str>) -> WorkloadSpec {
4197 let image = ImageRef {
4198 registry: "localhost".into(),
4199 repository: "native/svc".into(),
4200 tag: "dev".into(),
4201 digest: crate::testing::test_digest(),
4202 };
4203 let mut spec = WorkloadSpec::for_forge("w1", image, TierTag("infra".into()), vec![]);
4204 if let Some(v) = annotation {
4205 spec.annotations
4206 .insert(WRITABLE_PATHS_ANNOTATION.to_string(), v.to_string());
4207 }
4208 spec
4209 }
4210
4211 #[test]
4212 fn an_undeclared_spec_yields_no_writable_paths() {
4213 assert_eq!(
4214 spec_with(None).writable_paths().unwrap(),
4215 Vec::<PathBuf>::new()
4216 );
4217 }
4218
4219 #[test]
4220 fn a_comma_separated_list_is_split_and_trimmed() {
4221 let spec = spec_with(Some("/var/lib/yah/qed, /tmp"));
4222 assert_eq!(
4223 spec.writable_paths().unwrap(),
4224 vec![PathBuf::from("/var/lib/yah/qed"), PathBuf::from("/tmp")]
4225 );
4226 }
4227
4228 #[test]
4229 fn a_relative_entry_is_refused() {
4230 let err = spec_with(Some("var/lib/yah/qed"))
4231 .writable_paths()
4232 .unwrap_err();
4233 assert!(matches!(err, WritablePathsDeclError::RelativePath { .. }));
4234 }
4235
4236 #[test]
4237 fn a_traversing_entry_is_refused() {
4238 let err = spec_with(Some("/var/lib/yah/qed/../../.."))
4239 .writable_paths()
4240 .unwrap_err();
4241 assert!(matches!(err, WritablePathsDeclError::TraversingPath { .. }));
4242 }
4243
4244 #[test]
4245 fn a_trailing_comma_is_refused_rather_than_skipped() {
4246 let err = spec_with(Some("/tmp,")).writable_paths().unwrap_err();
4247 assert_eq!(err, WritablePathsDeclError::EmptyPath);
4248 }
4249
4250 #[test]
4251 fn a_duplicate_entry_is_refused() {
4252 let err = spec_with(Some("/tmp,/tmp")).writable_paths().unwrap_err();
4253 assert!(matches!(err, WritablePathsDeclError::DuplicatePath { .. }));
4254 }
4255
4256 #[test]
4257 fn the_declaration_is_independent_of_the_substrate_marker() {
4258 // The two are orthogonal by construction: declaring writes says nothing
4259 // about which backend runs the workload, and a native marker does not
4260 // imply a declaration (that asymmetry is the whole of the "confine what
4261 // describes itself" rule).
4262 let spec = spec_with(Some("/tmp"));
4263 assert!(!spec.wants_native_exec());
4264 assert!(!spec_with(None)
4265 .annotations
4266 .contains_key(WRITABLE_PATHS_ANNOTATION));
4267 }
4268}
4269
4270/// Annotation key requesting a workload share the host network namespace.
4271/// See [`WorkloadSpec::wants_host_network`].
4272pub const HOST_NETWORK_ANNOTATION: &str = "yah.network";
4273
4274/// Annotation value (for [`HOST_NETWORK_ANNOTATION`]) selecting host
4275/// networking. Any other value leaves the workload in an isolated netns.
4276pub const HOST_NETWORK_VALUE: &str = "host";
4277
4278/// Annotation key declaring that a workload must land only on a node
4279/// carrying a specific taint. See [`WorkloadSpec::requires_taint`].
4280pub const REQUIRES_TAINT_ANNOTATION: &str = "yah.placement.requires-taint";
4281
4282/// Default `pids.max` for a workload that declares no
4283/// [`ResourceLimits::pids_max`] — R885-T2.
4284///
4285/// `15%` of `4_194_304`, i.e. `629_145`. Both halves are grounded, not
4286/// invented:
4287///
4288/// - **The percentage** is systemd's own `DefaultTasksMax=`, the share of
4289/// `pid_max` systemd applies to any unit that does not set `TasksMax=`
4290/// explicitly (`systemd-system.conf(5)`). `kamaji.service` is one such
4291/// unit — it sets `Delegate=yes` but no `TasksMax=` — so this constant
4292/// gives native workloads the same *proportion* systemd would already give
4293/// the service itself.
4294/// - **The base** is `4_194_304`, the `pid_max` systemd has shipped since
4295/// v243 (2019) on every 64-bit host via `/usr/lib/sysctl.d/50-pid-max.conf`
4296/// — the fleet's own baseline, since every node here runs kamaji as a
4297/// systemd unit.
4298///
4299/// It does not need to be tight to be useful. A fork bomb today can exhaust
4300/// the *entire node's* pid space, denying PIDs to every other cgroup on the
4301/// box — sshd, monitoring, kamaji itself. A per-workload ceiling at 15% of
4302/// that space turns "the node is down" into "this workload's cgroup is
4303/// full", which is the actual blast-radius reduction R885-T2 asks for; it
4304/// does not need to also be a tight budget. It is also nowhere near tight
4305/// enough to strangle a legitimate workload — the forge build leg
4306/// (`cargo build -jN`) fans out to at most a few hundred rustc/linker
4307/// processes, several orders of magnitude below this ceiling.
4308pub const DEFAULT_PIDS_MAX: u32 = 629_145;
4309
4310/// The memory request [`WorkloadSpec::for_forge`] declares (MiB).
4311///
4312/// A forge run is a build, and a build's *ceiling* is deliberately roomy
4313/// (`FORGE_MEMORY_LIMIT_MB`); this is the much smaller floor a node must have
4314/// free to be a legal target for one. 2 GiB is what the heaviest forge shape
4315/// in the tree already asks for by hand — `velveteen_exec::remote`'s buildkit
4316/// image-build step overrides `resources.memory_mb` to exactly this — so it is
4317/// a measured number rather than a guess, and it keeps the fleet's 8 GiB
4318/// build-workers schedulable.
4319pub const FORGE_MEMORY_REQUEST_MB: u32 = 2048;
4320
4321/// The microVM scratch floor [`WorkloadSpec::for_forge`] declares (MiB) — see
4322/// [`ResourceLimits::scratch_floor_mb`].
4323///
4324/// 512 MiB, carried over unchanged from the `ephemeral_storage_mb` R885-T6
4325/// deleted, and **inert by construction**: the microVM backend's own
4326/// `WORKSPACE_MIN_BYTES` floor is 8 GiB, sixteen times this, and a floor is a
4327/// `max`. It is preserved rather than dropped so the deletion of the field is a
4328/// pure rename of a declaration and not a silent behaviour change, and so the
4329/// number an operator would have to raise is visible in one place if a forge
4330/// workspace ever needs to be bigger than the backend default.
4331pub const FORGE_SCRATCH_FLOOR_MB: u32 = 512;
4332
4333/// The cgroup memory ceiling [`WorkloadSpec::for_forge`] sets (MiB).
4334///
4335/// Bounded rather than unlimited so a runaway build cannot take the host
4336/// down, and large enough for the V8 build's >12 GB peak (R590-B10). It is
4337/// **not** a placement input — see [`FORGE_MEMORY_REQUEST_MB`].
4338pub const FORGE_MEMORY_LIMIT_MB: u32 = 32768;
4339
4340/// Taint name (for [`REQUIRES_TAINT_ANNOTATION`]) identifying machines with
4341/// a publicly-routable IP — the W267 sovereign-ingress placement
4342/// requirement. `MachineConfig.taints` (R572-F3) is the matching node-side
4343/// field and `RequiredSpec::matches` (R572-F5) is the consumer, so this is a
4344/// live key on both sides: a node may carry it, and the cloudflared/passway
4345/// ingress specs require it.
4346pub const PUBLIC_IP_TAINT: &str = "public-ip";
4347
4348/// Annotation key selecting which **execution substrate** kamaji runs a
4349/// workload on. Absent (or unrecognised) means a container backend; see
4350/// [`NATIVE_EXEC_VALUE`] and [`MICROVM_EXEC_VALUE`] for the two opt-outs.
4351///
4352/// The name is historical — R577-T1 introduced it for native exec alone — but
4353/// the key has always been the substrate selector, and R605-F8 added the
4354/// second alternative rather than a second key. See
4355/// [`WorkloadSpec::wants_microvm`] for why one key matters.
4356pub const NATIVE_EXEC_ANNOTATION: &str = "yah.exec";
4357
4358/// Annotation value (for [`NATIVE_EXEC_ANNOTATION`]) selecting native
4359/// host execution. Any other value leaves the workload on a container
4360/// backend.
4361pub const NATIVE_EXEC_VALUE: &str = "native";
4362
4363/// Annotation value (for [`NATIVE_EXEC_ANNOTATION`]) selecting a **microVM**:
4364/// the workload boots in its own KVM guest rather than sharing the host
4365/// kernel. See [`WorkloadSpec::wants_microvm`].
4366pub const MICROVM_EXEC_VALUE: &str = "microvm";
4367
4368/// Annotation key requesting the capabilities a workload needs to stand up an
4369/// unprivileged container sandbox of its own.
4370/// See [`WorkloadSpec::wants_nested_sandbox`].
4371pub const NESTED_SANDBOX_ANNOTATION: &str = "yah.sandbox";
4372
4373/// Annotation value (for [`NESTED_SANDBOX_ANNOTATION`]) requesting the
4374/// nested-sandbox grant (`CAP_SETUID` + `CAP_SETGID`, `no_new_privs` off).
4375/// Any other value leaves the workload on the baseline sandbox.
4376pub const NESTED_SANDBOX_VALUE: &str = "nested";
4377
4378/// Annotation key declaring the **host paths a workload writes to**, as a
4379/// comma-separated list of absolute paths. See [`WorkloadSpec::writable_paths`].
4380///
4381/// A top-level key rather than `yah.sandbox.writable-paths`, deliberately:
4382/// [`NESTED_SANDBOX_ANNOTATION`] is itself the bare key `yah.sandbox`, and two
4383/// keys sharing that prefix while naming policies for two *different* backends
4384/// (a container capability grant and a native filesystem confinement) is the
4385/// kind of near-collision a reader has to keep straight by memory.
4386pub const WRITABLE_PATHS_ANNOTATION: &str = "yah.writable-paths";
4387
4388/// Annotation key declaring **how much this workload's code is trusted**, from
4389/// which admission derives the weakest isolation substrate it may run on
4390/// (R894-F1). See [`WorkloadSpec::trust`] and [`TrustLevel`].
4391///
4392/// # Why a separate key from [`NATIVE_EXEC_ANNOTATION`]
4393///
4394/// `yah.exec` is a *request*: what the dispatcher wants. `yah.trust` is a
4395/// *fact about the code*: where it came from. Folding them together — a fourth
4396/// `yah.exec` value meaning "untrusted, so microvm" — would make the fact
4397/// unstateable whenever the request is stricter than the minimum, and would
4398/// silently discard it if a later ticket widened the substrate set. They are
4399/// two different questions and a workload answers both.
4400///
4401/// The pairing is checked, not merely recorded: `cloud::config::admission_spec`
4402/// refuses any spec whose declared trust exceeds what its requested substrate
4403/// provides, so a `yah.trust = untrusted` workload cannot reach a node on the
4404/// host kernel.
4405pub const TRUST_ANNOTATION: &str = "yah.trust";
4406
4407/// Annotation value (for [`TRUST_ANNOTATION`]) declaring that this workload's
4408/// code is **not** trusted to share a kernel with the fleet. Stamped by
4409/// whatever ingests the code, never by the code's own author.
4410pub const TRUST_UNTRUSTED_VALUE: &str = "untrusted";
4411
4412/// Annotation value (for [`TRUST_ANNOTATION`]) declaring operator-authored
4413/// code. Identical in effect to omitting the key; it exists so an ingest path
4414/// can state the fact positively instead of by silence.
4415pub const TRUST_TRUSTED_VALUE: &str = "trusted";
4416
4417/// The execution substrate a workload runs on, **ordered by the isolation it
4418/// provides** — `Native < Container < MicroVm`.
4419///
4420/// The ordering is the type's whole reason to exist, and it is the one kamaji's
4421/// `Backend` enum documents widest-first: a native workload is fork+exec'd on
4422/// the host with no boundary at all, a container gets namespaces and a cgroup
4423/// on the host's kernel, and a microVM gets its own kernel behind hardware
4424/// virtualization. `Ord` is derived, so *declaration order below is the
4425/// security ordering* — do not reorder these variants, and insert a new one at
4426/// the position its isolation actually places it.
4427///
4428/// This is the same three-way distinction [`crate::admission::GrantRuntime`]
4429/// carries; that type stays separate because a grant names a substrate it
4430/// *admits* (an unordered label in a signed document) while this one answers
4431/// "is what I got at least as strong as what I need". `GrantRuntime::of_spec`
4432/// delegates here so the two cannot disagree about how a spec reads.
4433#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4434pub enum ExecSubstrate {
4435 /// fork+exec on the host's own userland. Host kernel, no namespaces, no
4436 /// image — [`WorkloadSpec::wants_native_exec`].
4437 Native,
4438 /// A container backend: host kernel, namespaces + cgroup, OCI rootfs. The
4439 /// default when [`NATIVE_EXEC_ANNOTATION`] is absent or unrecognised.
4440 Container,
4441 /// A KVM guest with its own kernel — [`WorkloadSpec::wants_microvm`].
4442 MicroVm,
4443}
4444
4445impl ExecSubstrate {
4446 /// The annotation value that selects this substrate, or `None` for
4447 /// [`Self::Container`] (which is selected by saying nothing).
4448 pub fn annotation_value(self) -> Option<&'static str> {
4449 match self {
4450 ExecSubstrate::Native => Some(NATIVE_EXEC_VALUE),
4451 ExecSubstrate::MicroVm => Some(MICROVM_EXEC_VALUE),
4452 ExecSubstrate::Container => None,
4453 }
4454 }
4455
4456 /// How a refusal should name this substrate to an operator. Matches the
4457 /// annotation spelling for the two opt-outs; `container` is what every
4458 /// message in the tree already calls the default.
4459 pub fn as_str(self) -> &'static str {
4460 match self {
4461 ExecSubstrate::Native => NATIVE_EXEC_VALUE,
4462 ExecSubstrate::Container => "container",
4463 ExecSubstrate::MicroVm => MICROVM_EXEC_VALUE,
4464 }
4465 }
4466}
4467
4468/// How far a workload's code is trusted — the declared input from which
4469/// admission derives a **minimum** [`ExecSubstrate`] (R894).
4470///
4471/// # Absent means trusted, and that is only safe because of where it is stamped
4472///
4473/// The default direction is the trap this axis exists to close, so it is worth
4474/// stating exactly. Every `WorkloadSpec` in the tree today is built by operator
4475/// code — a reconciler, an appliance builder, a qed dispatcher — and none of
4476/// them declares trust. Making the absent key mean *untrusted* would refuse the
4477/// entire fleet on the day this lands; making it mean *trusted* is correct for
4478/// exactly that population and wrong for any other.
4479///
4480/// So the rule is not "absent means trusted". It is: **the single choke point
4481/// that ingests code the operator did not write stamps
4482/// [`TRUST_UNTRUSTED_VALUE`] as it builds the spec**
4483/// ([`WorkloadSpec::stamp_untrusted`]), and a spec that reaches admission
4484/// without having passed through operator-authored construction cannot exist.
4485/// A tenant does not hand yah a `WorkloadSpec`; it hands yah an image and an
4486/// argv, which yah's own code puts into a spec. That is why the marker is not
4487/// self-attestable: the untrusted party never holds the pen.
4488///
4489/// R823 (untrusted camp vending on microVMs) is the first such choke point. If
4490/// a second one appears, it stamps too — and the rule to apply is that any
4491/// constructor taking third-party bytes calls
4492/// [`stamp_untrusted`](WorkloadSpec::stamp_untrusted) in the same function that
4493/// takes them, not in a caller that might be forgotten.
4494#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
4495pub enum TrustLevel {
4496 /// Operator-authored code. No substrate floor.
4497 #[default]
4498 Trusted,
4499 /// Third-party code. Must not share a kernel with the fleet.
4500 Untrusted,
4501}
4502
4503impl TrustLevel {
4504 /// The weakest substrate this trust level may run on.
4505 ///
4506 /// [`TrustLevel::Untrusted`] maps to [`ExecSubstrate::MicroVm`] because a
4507 /// container shares the node's kernel, and "untrusted code never shares a
4508 /// kernel with the fleet" is the rule this axis makes checkable. W344's
4509 /// isolation table says "containerd or microVM" for higher-risk code; the
4510 /// 2026-09-11 operator call resolved that disjunction to the strict side
4511 /// for code the operator did not write.
4512 ///
4513 /// [`TrustLevel::Trusted`] maps to [`ExecSubstrate::Native`], the bottom of
4514 /// the ordering — i.e. no constraint, which is what every workload running
4515 /// today has.
4516 pub fn minimum_substrate(self) -> ExecSubstrate {
4517 match self {
4518 TrustLevel::Trusted => ExecSubstrate::Native,
4519 TrustLevel::Untrusted => ExecSubstrate::MicroVm,
4520 }
4521 }
4522
4523 /// The annotation value spelling this level.
4524 pub fn as_str(self) -> &'static str {
4525 match self {
4526 TrustLevel::Trusted => TRUST_TRUSTED_VALUE,
4527 TrustLevel::Untrusted => TRUST_UNTRUSTED_VALUE,
4528 }
4529 }
4530}
4531
4532/// Why a [`TRUST_ANNOTATION`] value could not be read (R894-F1).
4533///
4534/// Manual `Display` + `Error` impls in the same shape as
4535/// [`WritablePathsDeclError`], so `workload-spec` keeps its no-`thiserror` lib
4536/// surface.
4537#[derive(Debug, Clone, PartialEq, Eq)]
4538pub enum TrustDeclError {
4539 /// The value is neither [`TRUST_TRUSTED_VALUE`] nor
4540 /// [`TRUST_UNTRUSTED_VALUE`].
4541 ///
4542 /// Refused rather than normalised to either side. Reading it as trusted
4543 /// would let a typo (`untrused`) turn a microVM requirement off; reading it
4544 /// as untrusted would refuse a fleet workload over a typo nobody can see.
4545 /// Neither is a guess worth making about a security floor.
4546 UnknownLevel(String),
4547}
4548
4549impl std::fmt::Display for TrustDeclError {
4550 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4551 match self {
4552 TrustDeclError::UnknownLevel(v) => write!(
4553 f,
4554 "{TRUST_ANNOTATION} = {v:?} is not a trust level \
4555 (expected {TRUST_TRUSTED_VALUE:?} or {TRUST_UNTRUSTED_VALUE:?}); \
4556 omit the key for operator-authored code"
4557 ),
4558 }
4559 }
4560}
4561
4562impl std::error::Error for TrustDeclError {}
4563
4564// ── ImageRef ─────────────────────────────────────────────────────────────────
4565
4566/// Container image reference identifying a specific image to pull.
4567///
4568/// **Digest is required.** Every executable image reference in the workspace
4569/// is content-addressed by `sha256:<hex>`. The `tag` is preserved as a
4570/// human-readable identifier but is not the source of truth — registries
4571/// return mutable `tag → digest` mappings and we don't trust them for
4572/// reproducibility. R438-T3 tightened `digest: Option<String> → String` to
4573/// make unpinned-image bugs impossible by construction.
4574///
4575/// **Two deserialize shapes.** The struct form
4576/// (`registry`/`repository`/`tag`/`digest` fields) is the on-disk envelope.
4577/// A **string form** (`image = "ghcr.io/foo/bar:v1@sha256:<hex>"`) is also
4578/// accepted and is the shape W164 transform recipes (R438-T4) and W165
4579/// `BuildMode::InContainer` (R438-T6) use. Both shapes go through a single
4580/// parser ([`compose_import::parse_pinned_image_ref`]) that rejects
4581/// bare-tag references at serde-deserialize.
4582#[derive(Debug, Clone, PartialEq, Eq, Serialize, TS)]
4583#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4584pub struct ImageRef {
4585 /// Registry hostname, e.g. `"ghcr.io"` or `"localhost:5000"`.
4586 pub registry: String,
4587
4588 /// Repository path, e.g. `"noisetable/api"`.
4589 pub repository: String,
4590
4591 /// Tag, e.g. `"v1.4.2"` or `"latest"`. Informational — the digest is
4592 /// the source of truth for image identity.
4593 pub tag: String,
4594
4595 /// Content-addressed pinned identity, e.g. `"sha256:abc..."`. Required.
4596 pub digest: String,
4597}
4598
4599impl<'de> Deserialize<'de> for ImageRef {
4600 fn deserialize<D>(de: D) -> Result<Self, D::Error>
4601 where
4602 D: serde::Deserializer<'de>,
4603 {
4604 #[derive(Deserialize)]
4605 struct Fields {
4606 registry: String,
4607 repository: String,
4608 tag: String,
4609 digest: String,
4610 }
4611
4612 // The string-or-struct `untagged` probe requires `deserialize_any`,
4613 // which only self-describing formats support. Postcard — the binary
4614 // wire behind the kamaji UDS — returns `WontImplement` for it, so a
4615 // `Workload::Container(WorkloadSpec)` carrying a nested `ImageRef`
4616 // failed to decode and every container deploy 500'd (R590-B3).
4617 //
4618 // The string form is purely an authoring convenience in human-readable
4619 // configs (`image = "ghcr.io/…@sha256:…"` in recipe/workload TOML and
4620 // JSON); the binary wire only ever carries the derived struct form
4621 // (Serialize is a plain struct derive). So branch on the format: text
4622 // keeps the string-or-struct convenience via `untagged`; binary decodes
4623 // the plain positional struct with no `deserialize_any`.
4624 if de.is_human_readable() {
4625 #[derive(Deserialize)]
4626 #[serde(untagged)]
4627 enum Repr {
4628 // Order matters for `untagged`: try the string form first so
4629 // explicit strings don't get coerced into a struct error.
4630 Pinned(String),
4631 Struct(Fields),
4632 }
4633
4634 match Repr::deserialize(de)? {
4635 Repr::Pinned(s) => {
4636 compose_import::parse_pinned_image_ref(&s).map_err(serde::de::Error::custom)
4637 }
4638 Repr::Struct(f) => Ok(ImageRef {
4639 registry: f.registry,
4640 repository: f.repository,
4641 tag: f.tag,
4642 digest: f.digest,
4643 }),
4644 }
4645 } else {
4646 let f = Fields::deserialize(de)?;
4647 Ok(ImageRef {
4648 registry: f.registry,
4649 repository: f.repository,
4650 tag: f.tag,
4651 digest: f.digest,
4652 })
4653 }
4654 }
4655}
4656
4657// ── testing helpers ───────────────────────────────────────────────────────────
4658
4659/// Fixture helpers for test code that needs to construct types whose schemas
4660/// would otherwise demand operator-pinned values (digests, hashes). Doc-hidden
4661/// to discourage misuse from non-test code — production paths must source
4662/// digests from registry resolution or compile-time injection.
4663#[doc(hidden)]
4664pub mod testing {
4665 /// Fixed valid-format sha256 digest for test fixtures. All-zeros marker
4666 /// is impossible for any real image, so a leaked test fixture in a
4667 /// production code-path surfaces obviously.
4668 ///
4669 /// Aliases [`super::ImageRef::UNPINNED_DIGEST`] — the two are deliberately
4670 /// the same value: the fixture sentinel and the production "unpinned"
4671 /// marker must agree so [`super::ImageRef::pull_ref`]'s tag-fallback fires
4672 /// on exactly the digest `catalog_image` writes.
4673 pub const TEST_DIGEST: &str = super::ImageRef::UNPINNED_DIGEST;
4674
4675 /// Owned `String` form of [`TEST_DIGEST`] for fixture constructors.
4676 pub fn test_digest() -> String {
4677 TEST_DIGEST.to_string()
4678 }
4679}
4680
4681// ── EnvVar ────────────────────────────────────────────────────────────────────
4682
4683/// A single environment variable injected into the container.
4684#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4685#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4686pub struct EnvVar {
4687 /// Variable name, conventionally `SCREAMING_SNAKE_CASE`.
4688 pub name: String,
4689
4690 /// Value source.
4691 pub value: EnvValue,
4692}
4693
4694/// Value source for an environment variable.
4695#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4696#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4697#[serde(rename_all = "snake_case")]
4698pub enum EnvValue {
4699 /// Static string baked into the spec.
4700 Literal { value: String },
4701
4702 /// Resolved from a yubaba secret at deploy time; the secret value never
4703 /// appears in the spec JSON.
4704 FromSecret { secret: String, key: String },
4705
4706 /// Resolved from another workload's mesh address at deploy time by yubaba.
4707 /// Lets workloads reference each other symbolically without IP pinning.
4708 FromMesh { ident: MeshIdent, kind: MeshLookup },
4709}
4710
4711/// Which aspect of a mesh peer's address to inject.
4712///
4713/// ## Which port, when the peer has several (R844-B22)
4714///
4715/// [`Self::Url`] and [`Self::Port`] used to mean "the *first* entry in the
4716/// peer's `expose.mesh.ports`". That was a positional guess — the same one
4717/// `kamaji::name_anonymous_ports` refuses to make and that R844-F15 removed
4718/// from the service-record fanout — and it could hand a dependent workload a
4719/// metrics listener's number in its environment while looking entirely
4720/// successful. It survived only because, before R844-F17, a manifest had no way
4721/// to *name* a port, so "first" was the only selector that existed.
4722///
4723/// They now resolve by the same rule everything else in this workspace uses:
4724/// one port resolves to that port; several resolve to the one named `http`;
4725/// several with no `http` is an **error**, not a pick. The error is the feature
4726/// — it sends the author back to the manifest to say which listener they meant,
4727/// instead of handing a dependent a plausible wrong number.
4728///
4729/// [`Self::UrlNamed`] / [`Self::PortNamed`] say it outright and are the
4730/// spelling to prefer for any peer with more than one listener.
4731///
4732/// The named variants are **appended** rather than added as fields on the
4733/// existing ones: `MeshLookup` rides `EnvValue::FromMesh` inside a
4734/// [`WorkloadSpec`] across the postcard kamaji UDS, where an enum is encoded by
4735/// variant index, so appending leaves every existing encoding byte-identical
4736/// while adding a field to `Url` would not.
4737#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4738#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4739#[serde(rename_all = "snake_case")]
4740pub enum MeshLookup {
4741 /// Full URL, e.g. `"http://noisetable-db.pdx:5432"`. See the type docs for
4742 /// which port this picks when the peer has several.
4743 Url,
4744 /// Hostname only, e.g. `"noisetable-db.pdx"`.
4745 Host,
4746 /// Port only, e.g. `"5432"`. See the type docs for which port this picks
4747 /// when the peer has several.
4748 Port,
4749 /// Full URL at the peer's port called `name`, e.g. `"http://api.pdx:8443"`
4750 /// for `name = "wss"`. Errors when the peer has no port by that name.
4751 UrlNamed { name: String },
4752 /// The peer's port called `name`, stringified. Errors when the peer has no
4753 /// port by that name.
4754 PortNamed { name: String },
4755}
4756
4757impl MeshLookup {
4758 /// The port name this lookup selects, or `None` when it takes the default
4759 /// (see the type docs) or needs no port at all.
4760 pub fn port_name(&self) -> Option<&str> {
4761 match self {
4762 MeshLookup::UrlNamed { name } | MeshLookup::PortNamed { name } => Some(name),
4763 MeshLookup::Url | MeshLookup::Host | MeshLookup::Port => None,
4764 }
4765 }
4766
4767 /// Whether this lookup needs a port at all — `Host` is the one that does
4768 /// not, and it must keep resolving for a portless peer.
4769 pub fn needs_port(&self) -> bool {
4770 !matches!(self, MeshLookup::Host)
4771 }
4772}
4773
4774// ── Secrets ───────────────────────────────────────────────────────────────────
4775
4776/// A secret value mounted into the container as an env var or file.
4777///
4778/// The secret value never appears in the spec JSON — only the reference.
4779/// Yubaba audits secret access per workload from these references.
4780#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4781#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4782pub struct SecretMount {
4783 /// Where yubaba reads the secret value from.
4784 pub source: SecretRef,
4785
4786 /// How the secret is surfaced inside the container.
4787 pub target: SecretTarget,
4788}
4789
4790/// Where yubaba resolves the secret value from.
4791#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4792#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4793#[serde(rename_all = "snake_case")]
4794pub enum SecretRef {
4795 /// Per-machine yubaba secret store at `/var/lib/yah/yubaba/secrets/`.
4796 LocalFile { path: PathBuf },
4797
4798 /// Raft-replicated cluster secret spanning all machines (planned; not in
4799 /// V1 deployment). Sketch preserved for wire compatibility.
4800 Cluster { name: String },
4801}
4802
4803/// How the secret is surfaced inside the container.
4804#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4805#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4806#[serde(rename_all = "snake_case")]
4807pub enum SecretTarget {
4808 /// Injected as an environment variable. Value never appears in spec JSON.
4809 /// Prefer `File` — env vars leak through subprocess env and log dumps.
4810 EnvVar { name: String },
4811
4812 /// Mounted as a file inside the container at `path` with `mode` (octal).
4813 File { path: PathBuf, mode: u32 },
4814}
4815
4816// ── Volumes ───────────────────────────────────────────────────────────────────
4817
4818/// A volume mount inside the container.
4819#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4820#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4821pub struct VolumeMount {
4822 /// Backing volume source.
4823 pub source: VolumeSource,
4824
4825 /// Absolute path inside the container.
4826 pub target: PathBuf,
4827
4828 /// Whether the container sees the volume as read-only.
4829 pub read_only: bool,
4830
4831 /// True when this mount was synthesized by yubaba's deploy-time secret
4832 /// materializer (`secret_mount::materialize_file_secrets`) rather than
4833 /// declared by the operator (R858-B26).
4834 ///
4835 /// `yah.durability.*`'s "exactly one named-or-bind volume" count
4836 /// (`validate::shape`, `kamaji::hydrate::plan`) skips any mount with this
4837 /// set, for the same reason it already skips `Tmpfs`: a secret bind is by
4838 /// construction not where durable subjects live, and counting it made
4839 /// every workload with both durable state and a file secret un-declarable.
4840 /// Kamaji still mounts it exactly like any other `Bind` — this flag is
4841 /// read only by the two durability-counting sites, nowhere else.
4842 #[serde(default)]
4843 pub from_secret_mount: bool,
4844}
4845
4846/// Backing source for a volume mount.
4847#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4848#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4849#[serde(rename_all = "snake_case")]
4850pub enum VolumeSource {
4851 /// Yubaba-managed named volume; created on first use.
4852 Named { name: String },
4853
4854 /// Operator-managed host path. Yubaba rejects bind mounts unless
4855 /// `WorkloadSpec.tier == "infra"`; shape validation enforces this.
4856 Bind { host_path: PathBuf },
4857
4858 /// In-memory tmpfs; discarded on container stop. `size_mb` caps space
4859 /// consumed by the writable layer.
4860 Tmpfs { size_mb: u32 },
4861}
4862
4863// ── Durable forge produced-artifact convention (R603-T5) ──────────────────────
4864
4865/// Convention for a remote forge step's durable produced artifacts.
4866///
4867/// A remote build (e.g. the rusty_v8 musl build on a build-worker) writes its
4868/// output tarball to a path *inside* the container. The container's rootfs is
4869/// destroyed when kamaji reaps the EXITED container — so if the camp daemon is
4870/// down when the build finishes, the artifact is gone before boot-reconcile can
4871/// retrieve it (R603-T4 surfaced this as `Success`-but-`UNPUBLISHED`).
4872///
4873/// The fix (R603-T5) is a **host-persistent bind mount**: forge Subprocess
4874/// workloads mount [`HOST_ROOT`]`/<forge_id>` onto [`CONTAINER_DIR`], so a
4875/// build that writes its `produces` under `/yah/produced` lands the bytes on
4876/// the worker's host filesystem. yubaba then reads them back from the host path
4877/// ([`host_path`]) — which outlives container reaping — instead of the
4878/// unreachable container rootfs.
4879///
4880/// The container-side path and the host root are a shared convention between
4881/// three crates: the qed `build_workload_spec` that adds the mount, kamaji that
4882/// binds it, and the yubaba handler that reads + reaps it. Keeping it here (the
4883/// crate all three already depend on) is the single source of truth.
4884pub mod forge_produced {
4885 use std::path::{Path, PathBuf};
4886
4887 /// Conventional container-side directory a remote forge step writes its
4888 /// durable produced artifacts to. Bind-mounted onto a host-persistent dir.
4889 pub const CONTAINER_DIR: &str = "/yah/produced";
4890
4891 /// Host root under which each forge's durable produced dir lives, one
4892 /// subdir per run: `<HOST_ROOT>/<forge_id>/`. yubaba owns this directory —
4893 /// it creates the per-forge subdir at deploy, serves reads from it, and
4894 /// reaps it on teardown / TTL sweep.
4895 pub const HOST_ROOT: &str = "/var/lib/yah/qed/produced";
4896
4897 /// Forge mesh idents are `forge.<id>` (see [`WorkloadSpec::for_forge`]).
4898 /// Extract the bare `<id>`, or `None` for a non-forge ident.
4899 ///
4900 /// [`WorkloadSpec::for_forge`]: super::WorkloadSpec::for_forge
4901 pub fn forge_id_from_ident(ident: &str) -> Option<&str> {
4902 ident.strip_prefix("forge.")
4903 }
4904
4905 /// The host-persistent produced directory for one forge run.
4906 pub fn host_dir(forge_id: &str) -> PathBuf {
4907 PathBuf::from(HOST_ROOT).join(forge_id)
4908 }
4909
4910 /// Translate a container-side produced path to its durable host path for a
4911 /// given forge run. Returns `None` when `container_path` is not under
4912 /// [`CONTAINER_DIR`] (the caller then knows the artifact was not written to
4913 /// the durable location and won't survive reaping), or when the relative
4914 /// path contains a `..` component (a traversal attempt that could escape the
4915 /// per-forge dir — the reader must never serve a file outside it).
4916 pub fn host_path(forge_id: &str, container_path: &Path) -> Option<PathBuf> {
4917 host_path_under(&host_dir(forge_id), container_path)
4918 }
4919
4920 /// The same translation against an ARBITRARY host directory, for a produced
4921 /// dir that is not one of yubaba's (R560-B12).
4922 ///
4923 /// A LOCAL container step has the identical problem a remote one has and no
4924 /// [`HOST_ROOT`] to solve it with: `docker run --rm` throws the container's
4925 /// writable layer away on exit, so a build that writes its `produces` under
4926 /// [`CONTAINER_DIR`] and exits 0 leaves the caller reading an absent file —
4927 /// exactly the remote failure this module was created for. The caller binds
4928 /// a host dir of its own choosing (qed uses a run-scoped dir under the
4929 /// camp's cache) and maps declared container paths through this.
4930 ///
4931 /// Split out rather than duplicated because the `..` guard is the whole
4932 /// safety content of both: `host_path` must never serve a file outside the
4933 /// per-forge dir, and this one must never write outside the caller's. Two
4934 /// copies of that check is one copy that can be fixed alone.
4935 pub fn host_path_under(host_dir: &Path, container_path: &Path) -> Option<PathBuf> {
4936 let rel = container_path.strip_prefix(CONTAINER_DIR).ok()?;
4937 if rel
4938 .components()
4939 .any(|c| matches!(c, std::path::Component::ParentDir))
4940 {
4941 return None;
4942 }
4943 Some(host_dir.join(rel))
4944 }
4945
4946 /// The durable produced-dir bind mount for a forge run: host
4947 /// `<HOST_ROOT>/<forge_id>` → container [`CONTAINER_DIR`], writable.
4948 pub fn durable_mount(forge_id: &str) -> super::VolumeMount {
4949 super::VolumeMount {
4950 source: super::VolumeSource::Bind {
4951 host_path: host_dir(forge_id),
4952 },
4953 target: PathBuf::from(CONTAINER_DIR),
4954 read_only: false,
4955 from_secret_mount: false,
4956 }
4957 }
4958
4959 /// True when `path` is (or is under) the conventional durable produced dir
4960 /// — the guard qed uses to enforce that declared `produces` land somewhere
4961 /// reap-durable.
4962 pub fn is_durable_path(path: &Path) -> bool {
4963 path.starts_with(CONTAINER_DIR)
4964 }
4965}
4966
4967// ── Forge host-state root (R636-B1) ───────────────────────────────────────────
4968
4969/// The one host directory tree a QED forge step's bind mounts may live under.
4970///
4971/// # Why this is a named root rather than a list of paths
4972///
4973/// runc refuses a bind whose source is missing, and the OCI mapper never
4974/// mkdirs one — so *something* has to create each host dir before deploy.
4975/// yubaba does, but only for paths it recognizes, and "recognizes" was
4976/// originally a hardcoded match on the produced dir. Every new forge mount then
4977/// re-learned the lesson the expensive way, on a real box, minutes into a
4978/// build: R603-B6 for `produced/`, then R636-B1 for `build-out/`, each
4979/// surfacing as the same opaque `failed to fulfil mount request: … no such file
4980/// or directory` from deep inside containerd.
4981///
4982/// Naming the *root* makes the rule checkable instead of enumerable: yubaba
4983/// creates any forge bind under [`HOST_ROOT`], and `yubaba.service` grants the
4984/// root once via `StateDirectory=yah/qed`. A third mount needs no new code and
4985/// no unit-file edit — it only has to live here.
4986///
4987/// The prefix bound is load-bearing in the other direction too: it is what
4988/// keeps a workload spec from asking yubaba to mkdir an arbitrary host path.
4989pub mod forge_state {
4990 use std::path::Path;
4991
4992 /// Root of the forge's host-persistent state. Both
4993 /// [`super::forge_produced::HOST_ROOT`] and [`BUILD_OUT_DIR`] are under it.
4994 pub const HOST_ROOT: &str = "/var/lib/yah/qed";
4995
4996 /// Host directory a `build-image` step's OCI archive is written to, bound
4997 /// at `/yah/build/out` in the BuildKit container. Shared (rather than
4998 /// per-forge like `produced/`) because the archive is named after the image
4999 /// tag, which is already unique per build.
5000 pub const BUILD_OUT_DIR: &str = "/var/lib/yah/qed/build-out";
5001
5002 /// Whether yubaba may create `host_path` on behalf of a forge workload.
5003 ///
5004 /// Rejects anything outside [`HOST_ROOT`], and anything with a `..`
5005 /// component — `/var/lib/yah/qed/../../../etc` starts with the root as a
5006 /// string and is nowhere near it as a path.
5007 pub fn is_forge_state_path(host_path: &Path) -> bool {
5008 !host_path
5009 .components()
5010 .any(|c| matches!(c, std::path::Component::ParentDir))
5011 && host_path.starts_with(HOST_ROOT)
5012 }
5013}
5014
5015// ── Materialized-secret path contract (R555-F5) ───────────────────────────────
5016
5017/// Where yubaba writes a `File`-target secret it has resolved, and how the host
5018/// path is derived from the container path.
5019///
5020/// # Why the derivation lives here and not in yubaba
5021///
5022/// yubaba resolves a [`SecretMount`] and rewrites it into a read-only [`Bind`]
5023/// volume before the spec reaches the backend, so the spec kamaji admits is not
5024/// the spec the dispatcher signed: one mount has become one bind. Admission has
5025/// to be able to recognise that rewrite — otherwise a signed recipe carrying a
5026/// secret is refused by [`admission::AdmissionGrant::covers`]'s bind rule, which
5027/// only knows about [`forge_state::HOST_ROOT`], with a message about a forge
5028/// state root that has nothing to do with what happened.
5029///
5030/// Recognising it means recomputing the host path, which means the derivation
5031/// has to be visible to both sides. It was private to yubaba's
5032/// `deploy::secret_mount`; it lives here now, and yubaba calls in. `forge_state`
5033/// is the same shape for the same reason.
5034///
5035/// [`Bind`]: VolumeSource::Bind
5036pub mod secret_mount {
5037 use std::path::{Path, PathBuf};
5038
5039 /// RAM-backed root for materialized secret files. `/run` is a tmpfs on
5040 /// systemd nodes, so decrypted PEM never touches disk. Each workload gets a
5041 /// `<root>/<ident>/` subdir, reaped on workload destroy.
5042 pub const HOST_ROOT: &str = "/run/yah/secrets";
5043
5044 /// Collapse a value into a single safe path component: every char outside
5045 /// `[A-Za-z0-9_-]` becomes `_` (dots included, so `.` / `..` can never
5046 /// traverse). Empty input maps to `_`.
5047 pub fn sanitize_component(s: &str) -> String {
5048 let mapped: String = s
5049 .chars()
5050 .map(|c| {
5051 if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
5052 c
5053 } else {
5054 '_'
5055 }
5056 })
5057 .collect();
5058 if mapped.is_empty() {
5059 "_".into()
5060 } else {
5061 mapped
5062 }
5063 }
5064
5065 /// Derive a collision-free host filename from a container target path: strip
5066 /// the leading `/`, keep `.` for extensions, and replace path separators (and
5067 /// any other non-`[A-Za-z0-9_.-]` char) with `_`. A target that reduces to
5068 /// nothing or a dots-only name falls back to `secret`. The result is always a
5069 /// single flat filename (no separators), so it cannot traverse out of the
5070 /// per-workload dir.
5071 pub fn host_file_name(target: &Path) -> String {
5072 let raw = target.to_string_lossy();
5073 let trimmed = raw.trim_start_matches('/');
5074 let mapped: String = trimmed
5075 .chars()
5076 .map(|c| {
5077 if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.') {
5078 c
5079 } else {
5080 '_'
5081 }
5082 })
5083 .collect();
5084 if mapped.is_empty() || mapped.chars().all(|c| c == '.') {
5085 "secret".into()
5086 } else {
5087 mapped
5088 }
5089 }
5090
5091 /// The per-workload directory materialized secrets are written to.
5092 pub fn workload_dir(root: &Path, ident: &str) -> PathBuf {
5093 root.join(sanitize_component(ident))
5094 }
5095
5096 /// The host path a `File`-target secret at container path `target` is
5097 /// materialized to for workload `ident`.
5098 ///
5099 /// Deterministic in exactly those three inputs, which is what lets admission
5100 /// recompute it from the spec alone and match a bind against it.
5101 pub fn materialized_host_path(root: &Path, ident: &str, target: &Path) -> PathBuf {
5102 workload_dir(root, ident).join(host_file_name(target))
5103 }
5104}
5105
5106#[cfg(test)]
5107mod secret_mount_tests {
5108 use super::secret_mount::*;
5109 use std::path::{Path, PathBuf};
5110
5111 #[test]
5112 fn the_host_path_is_a_pure_function_of_root_ident_and_target() {
5113 let p = materialized_host_path(
5114 Path::new(HOST_ROOT),
5115 "forge.abc-123",
5116 Path::new("/etc/yah/r2.json"),
5117 );
5118 assert_eq!(
5119 p,
5120 PathBuf::from("/run/yah/secrets/forge_abc-123/etc_yah_r2.json")
5121 );
5122 }
5123
5124 /// The two collapses exist to keep a hostile ident or target from steering
5125 /// the write out of the per-workload dir. Pinned here because admission now
5126 /// depends on them being total.
5127 #[test]
5128 fn neither_component_can_traverse() {
5129 for ident in ["..", "../../etc", "a/b", ""] {
5130 let dir = workload_dir(Path::new(HOST_ROOT), ident);
5131 assert_eq!(dir.components().count(), 5, "{ident:?} escaped {dir:?}");
5132 assert!(dir.starts_with(HOST_ROOT));
5133 }
5134 for target in ["/../../etc/shadow", "..", "/", "/a/../b"] {
5135 let name = host_file_name(Path::new(target));
5136 assert!(!name.contains('/'), "{target:?} kept a separator: {name}");
5137 assert_ne!(name, "..");
5138 }
5139 }
5140}
5141
5142// ── Durable forge build-cache convention (R876-F4) ────────────────────────────
5143
5144/// Convention for a remote forge step's host-persistent **build cache**.
5145///
5146/// # The gap this closes
5147///
5148/// A remote subprocess gets image + argv + the [`forge_produced`] mount and
5149/// nothing else, so a step that compiles a source tree compiles it from scratch
5150/// every single run: the container's writable layer (where `CARGO_TARGET_DIR`
5151/// lands by default) is thrown away when kamaji reaps the exited container, and
5152/// `/yah/produced` is per-run and reaped on destroy. `mesofact-musl`'s x86_64
5153/// leg measured 9m56s / 9m57s / 11m10s across its successful runs and every one
5154/// of those was a cold full release build.
5155///
5156/// This is the third mount under [`forge_state::HOST_ROOT`], and — exactly as
5157/// R603-B6's handoff promised — it needs neither new yubaba code nor a
5158/// `yubaba.service` edit: `ensure_forge_state_dirs` already mkdirs *any* forge
5159/// bind under that root.
5160///
5161/// # Why the key is derived, not caller-supplied
5162///
5163/// A shared target dir keyed by nothing is a correctness bug, not merely a
5164/// race. `mesofact-musl` carries `concurrency_key = "mesofact-musl"`, but that
5165/// is *camp-side scheduling*: it does not constrain a second camp, or a
5166/// hand-rolled dispatch, aiming at the same worker. The key is therefore
5167/// derived by the dispatcher from **pipeline + step + target triple**
5168/// ([`key_from_parts`]) rather than written in a TOML, so two different
5169/// pipelines — or the same pipeline's two triples — cannot land on one target
5170/// dir however the run was started.
5171///
5172/// Two runs of the *same* pipeline+step+triple DO share, and that is the whole
5173/// point: cargo is designed for exactly that reuse, and its own `.cargo-lock`
5174/// in the target dir serializes two builds that overlap in time.
5175///
5176/// # Eviction is explicit
5177///
5178/// An unbounded cache on a worker rootfs is R702's subject. Both holders of a
5179/// cache root — yubaba on the worker, qed for the local-container placement —
5180/// sweep it with [`evict_plan`]: anything idle past [`RETENTION`] goes, and if
5181/// the filesystem is below [`FREE_FLOOR_BYTES`] the least-recently-used dirs go
5182/// too, until it is not. The cache can therefore never consume the last few GB
5183/// of a build worker's `/var`.
5184pub mod forge_cache {
5185 use std::path::{Path, PathBuf};
5186 use std::time::{Duration, SystemTime};
5187
5188 /// Conventional container-side directory a cached forge step's build
5189 /// scratch lives in. Bind-mounted onto a host-persistent, key-scoped dir.
5190 ///
5191 /// The step's argv points its own toolchain at this (mesofact-musl exports
5192 /// `CARGO_TARGET_DIR="$YAH_CACHE_DIR/target"`), the same way it does the
5193 /// source-context fetch — the mount is toolchain-agnostic and the TOML
5194 /// keeps describing what actually runs.
5195 pub const CONTAINER_DIR: &str = "/yah/cache";
5196
5197 /// Environment variable carrying [`CONTAINER_DIR`] into the step, so an
5198 /// argv never has to hardcode the convention.
5199 pub const CACHE_DIR_ENV: &str = "YAH_CACHE_DIR";
5200
5201 /// Host root under which each cache key gets a directory:
5202 /// `<HOST_ROOT>/<key>/`. Under [`super::forge_state::HOST_ROOT`], so
5203 /// yubaba's `ensure_forge_state_dirs` creates it and `yubaba.service`
5204 /// already grants write access to it.
5205 pub const HOST_ROOT: &str = "/var/lib/yah/qed/cache";
5206
5207 /// A cache dir untouched for this long is evicted. Long enough that a
5208 /// weekly release still hits a warm cache; short enough that a renamed
5209 /// step's orphan does not sit on the disk forever.
5210 pub const RETENTION: Duration = Duration::from_secs(60 * 60 * 24 * 14);
5211
5212 /// Below this much free space on the filesystem holding a cache root,
5213 /// least-recently-used cache dirs are evicted until it is above it again.
5214 /// This is the bound that matters on a build worker: us-west-003's `/var`
5215 /// is a 60 GB LV, and a release target dir is multiple GB.
5216 pub const FREE_FLOOR_BYTES: u64 = 10 * 1024 * 1024 * 1024;
5217
5218 /// Longest derived key kept verbatim; longer ones are truncated and
5219 /// disambiguated with a digest by [`key_from_parts`].
5220 pub const MAX_KEY_LEN: usize = 96;
5221
5222 /// Whether `key` is safe to use as a single path component under
5223 /// [`HOST_ROOT`]. Deliberately narrow: alphanumerics plus `.`, `-`, `_`,
5224 /// non-empty, length-capped, and never a bare `.`/`..`. Everything a
5225 /// dispatcher derives passes; nothing a hostile spec could write escapes.
5226 pub fn is_valid_key(key: &str) -> bool {
5227 !key.is_empty()
5228 && key.len() <= MAX_KEY_LEN + 24
5229 && key != "."
5230 && key != ".."
5231 && key
5232 .chars()
5233 .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_'))
5234 }
5235
5236 /// Derive the sharing key from the parts that must not collide: the
5237 /// pipeline, the step within it, and the target triple.
5238 ///
5239 /// Characters outside the [`is_valid_key`] alphabet collapse to `-`. A key
5240 /// that would exceed [`MAX_KEY_LEN`] is truncated and suffixed with a
5241 /// digest of the *full* string, so shortening can never merge two distinct
5242 /// keys into one.
5243 ///
5244 /// The digest is FNV-1a rather than blake3: this crate is deliberately a
5245 /// zero-dependency schema crate (see its `seal` / `admission-verify`
5246 /// features — even the cipher is opt-in), the inputs are pipeline and step
5247 /// names from the camp's own TOMLs rather than anything adversarial, and
5248 /// the property needed is "two long keys differ", not preimage resistance.
5249 pub fn key_from_parts(pipeline: &str, step: &str, triple: &str) -> String {
5250 let raw = format!("{pipeline}.{step}.{triple}");
5251 let mut safe: String = raw
5252 .chars()
5253 .map(|c| {
5254 if c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_') {
5255 c
5256 } else {
5257 '-'
5258 }
5259 })
5260 .collect();
5261 if safe.len() > MAX_KEY_LEN {
5262 let digest = fnv1a64(raw.as_bytes());
5263 safe.truncate(MAX_KEY_LEN);
5264 safe.push('.');
5265 safe.push_str(&format!("{digest:016x}"));
5266 }
5267 safe
5268 }
5269
5270 /// FNV-1a, 64-bit. See [`key_from_parts`] for why this and not a real hash.
5271 fn fnv1a64(bytes: &[u8]) -> u64 {
5272 let mut h: u64 = 0xcbf2_9ce4_8422_2325;
5273 for b in bytes {
5274 h ^= *b as u64;
5275 h = h.wrapping_mul(0x0000_0100_0000_01b3);
5276 }
5277 h
5278 }
5279
5280 /// The host-persistent cache directory for one key, under [`HOST_ROOT`].
5281 pub fn host_dir(key: &str) -> Option<PathBuf> {
5282 cache_dir_under(Path::new(HOST_ROOT), key)
5283 }
5284
5285 /// The same derivation against an arbitrary root — for the local-container
5286 /// placement, whose cache lives under the camp's own `.yah/cache` and has
5287 /// no [`HOST_ROOT`] to hang off. Split out for the same reason
5288 /// [`super::forge_produced::host_path_under`] is: the key validation is the
5289 /// whole safety content of both, and two copies is one copy that can be
5290 /// fixed alone.
5291 pub fn cache_dir_under(root: &Path, key: &str) -> Option<PathBuf> {
5292 is_valid_key(key).then(|| root.join(key))
5293 }
5294
5295 /// The build-cache bind mount for one key: host `<HOST_ROOT>/<key>` →
5296 /// container [`CONTAINER_DIR`], writable. `None` for an invalid key —
5297 /// the caller must refuse rather than mount something else.
5298 pub fn durable_mount(key: &str) -> Option<super::VolumeMount> {
5299 Some(super::VolumeMount {
5300 source: super::VolumeSource::Bind {
5301 host_path: host_dir(key)?,
5302 },
5303 target: PathBuf::from(CONTAINER_DIR),
5304 read_only: false,
5305 from_secret_mount: false,
5306 })
5307 }
5308
5309 /// Which cache dirs to delete, given every dir in a cache root with its
5310 /// last-modified time, the current free space on that filesystem, and the
5311 /// floor to hold.
5312 ///
5313 /// Pure so the policy is one testable function shared by both holders of a
5314 /// cache root (yubaba on the worker, qed for local containers) instead of
5315 /// two drifting copies. The callers own the `read_dir` / `df` / `remove`.
5316 ///
5317 /// Two rules, in order: everything idle past `retention` goes
5318 /// unconditionally; then, while `free_bytes` is under `floor_bytes`, the
5319 /// least-recently-used survivor goes — LRU because the dir a build just
5320 /// touched is the one whose loss costs the next run the most.
5321 ///
5322 /// `free_bytes` is what the caller measured BEFORE any deletion, so the
5323 /// count of extra evictions is a heuristic (this function cannot know a
5324 /// dir's size without walking it). It is bounded and monotone: under
5325 /// sustained pressure each sweep drops one more dir, and a root that is
5326 /// entirely evicted simply rebuilds cold — the failure mode is a slow
5327 /// build, never a full disk.
5328 pub fn evict_plan(
5329 entries: &[(PathBuf, SystemTime)],
5330 now: SystemTime,
5331 retention: Duration,
5332 free_bytes: u64,
5333 floor_bytes: u64,
5334 ) -> Vec<PathBuf> {
5335 let mut evict = Vec::new();
5336 let mut live: Vec<&(PathBuf, SystemTime)> = Vec::new();
5337 for entry in entries {
5338 let idle = now
5339 .duration_since(entry.1)
5340 .map(|age| age > retention)
5341 .unwrap_or(false);
5342 if idle {
5343 evict.push(entry.0.clone());
5344 } else {
5345 live.push(entry);
5346 }
5347 }
5348 if free_bytes < floor_bytes && !live.is_empty() {
5349 live.sort_by_key(|(_, mtime)| *mtime);
5350 evict.push(live[0].0.clone());
5351 }
5352 evict
5353 }
5354}
5355
5356#[cfg(test)]
5357mod forge_cache_tests {
5358 use super::forge_cache::*;
5359 use std::path::{Path, PathBuf};
5360 use std::time::{Duration, SystemTime};
5361
5362 #[test]
5363 fn the_cache_root_is_under_the_forge_state_root() {
5364 assert!(super::forge_state::is_forge_state_path(Path::new(
5365 HOST_ROOT
5366 )));
5367 assert!(super::forge_state::is_forge_state_path(
5368 &host_dir("mesofact-musl.build.x86_64-unknown-linux-musl").unwrap()
5369 ));
5370 }
5371
5372 #[test]
5373 fn the_key_separates_pipelines_steps_and_triples() {
5374 let a = key_from_parts("mesofact-musl", "build", "x86_64-unknown-linux-musl");
5375 let b = key_from_parts("mesofact-musl", "build", "aarch64-unknown-linux-musl");
5376 let c = key_from_parts("other-pipeline", "build", "x86_64-unknown-linux-musl");
5377 let d = key_from_parts("mesofact-musl", "other-step", "x86_64-unknown-linux-musl");
5378 assert_ne!(a, b);
5379 assert_ne!(a, c);
5380 assert_ne!(a, d);
5381 assert!(is_valid_key(&a), "{a}");
5382 }
5383
5384 /// Shortening must never merge two distinct keys — the whole point of the
5385 /// key is that a collision is impossible.
5386 #[test]
5387 fn an_over_long_key_is_digest_disambiguated_not_merely_truncated() {
5388 let long = "p".repeat(MAX_KEY_LEN);
5389 let a = key_from_parts(&long, "step-one", "x86_64-unknown-linux-musl");
5390 let b = key_from_parts(&long, "step-two", "x86_64-unknown-linux-musl");
5391 assert_ne!(a, b);
5392 assert!(is_valid_key(&a) && is_valid_key(&b));
5393 }
5394
5395 #[test]
5396 fn a_key_that_could_escape_the_root_is_refused_rather_than_sanitized() {
5397 for bad in ["", ".", "..", "../etc", "a/b", "a\0b"] {
5398 assert!(!is_valid_key(bad), "{bad:?} must not be a cache key");
5399 assert!(host_dir(bad).is_none(), "{bad:?}");
5400 assert!(durable_mount(bad).is_none(), "{bad:?}");
5401 }
5402 // …and a derived key from hostile parts is sanitized into the alphabet.
5403 let k = key_from_parts("../../etc", "x/y", "t");
5404 assert!(is_valid_key(&k), "{k}");
5405 assert_eq!(host_dir(&k).unwrap().parent().unwrap(), Path::new(HOST_ROOT));
5406 }
5407
5408 #[test]
5409 fn durable_mount_shape() {
5410 let m = durable_mount("k").expect("valid key");
5411 assert_eq!(m.target, PathBuf::from(CONTAINER_DIR));
5412 assert!(!m.read_only, "a build cache the step cannot write is useless");
5413 match &m.source {
5414 super::VolumeSource::Bind { host_path } => {
5415 assert_eq!(host_path, &PathBuf::from(HOST_ROOT).join("k"));
5416 }
5417 other => panic!("expected a bind, got {other:?}"),
5418 }
5419 }
5420
5421 #[test]
5422 fn idle_dirs_are_evicted_and_fresh_ones_are_kept_when_there_is_room() {
5423 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
5424 let entries = vec![
5425 (PathBuf::from("/c/old"), now - RETENTION - Duration::from_secs(1)),
5426 (PathBuf::from("/c/fresh"), now - Duration::from_secs(60)),
5427 ];
5428 let plan = evict_plan(&entries, now, RETENTION, FREE_FLOOR_BYTES * 2, FREE_FLOOR_BYTES);
5429 assert_eq!(plan, vec![PathBuf::from("/c/old")]);
5430 }
5431
5432 #[test]
5433 fn disk_pressure_evicts_the_least_recently_used_survivor() {
5434 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
5435 let entries = vec![
5436 (PathBuf::from("/c/hot"), now - Duration::from_secs(60)),
5437 (PathBuf::from("/c/cool"), now - Duration::from_secs(6000)),
5438 ];
5439 let plan = evict_plan(&entries, now, RETENTION, 1, FREE_FLOOR_BYTES);
5440 assert_eq!(plan, vec![PathBuf::from("/c/cool")]);
5441 }
5442
5443 #[test]
5444 fn an_empty_root_under_disk_pressure_plans_nothing() {
5445 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
5446 assert!(evict_plan(&[], now, RETENTION, 0, FREE_FLOOR_BYTES).is_empty());
5447 }
5448}
5449
5450#[cfg(test)]
5451mod forge_state_tests {
5452 use super::forge_state::*;
5453 use std::path::Path;
5454
5455 #[test]
5456 fn both_known_forge_roots_are_under_the_state_root() {
5457 assert!(is_forge_state_path(Path::new(
5458 super::forge_produced::HOST_ROOT
5459 )));
5460 assert!(is_forge_state_path(Path::new(BUILD_OUT_DIR)));
5461 assert!(is_forge_state_path(&super::forge_produced::host_dir(
5462 "abc-123"
5463 )));
5464 }
5465
5466 /// A spec must not be able to steer yubaba's mkdir anywhere it likes —
5467 /// neither by naming an unrelated absolute path nor by climbing out with
5468 /// `..`, which a plain string prefix check would wave through.
5469 #[test]
5470 fn paths_outside_the_root_are_refused() {
5471 for bad in [
5472 "/var/lib/yah/yubaba",
5473 "/etc/systemd/system",
5474 "/var/lib/yah/qed/../../../etc",
5475 "relative/path",
5476 ] {
5477 assert!(
5478 !is_forge_state_path(Path::new(bad)),
5479 "{bad} must not be creatable by a forge spec"
5480 );
5481 }
5482 }
5483}
5484
5485// ── Resources ─────────────────────────────────────────────────────────────────
5486
5487/// Hard resource caps enforced by containerd/cgroups at runtime.
5488#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5489#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5490pub struct ResourceLimits {
5491 /// Maximum RAM the container may allocate, in MiB. The container is OOM-
5492 /// killed if it exceeds this.
5493 ///
5494 /// A **ceiling**, not a request: setting it generously is the safe
5495 /// direction here and the unschedulable direction for placement, so
5496 /// schedulers must read [`WorkloadSpec::memory_request_mb`] instead of
5497 /// this field. (`cpu_millis` below is the opposite — a request by
5498 /// definition — which is why the two are not symmetric.)
5499 pub memory_mb: u32,
5500
5501 /// CPU **request** in millicores (k8s convention): `1000` = one full core,
5502 /// `250` = `.25 CPU`. Unlike a Docker relative weight this is an
5503 /// allocatable quantity a bin-packer can subtract from a node's budget.
5504 /// `0` means "no declared request" — the workload gets the node's default
5505 /// share. Backends that speak a relative weight derive it via
5506 /// [`ResourceLimits::cpu_shares`].
5507 ///
5508 /// **It is not a ceiling.** A request answers "what share of a contended
5509 /// node is mine"; nothing here says "stop at this much". A workload
5510 /// declaring `250m` must still be able to burst to the whole box when the
5511 /// box is idle. The hard ceiling is optional and rides
5512 /// [`Self::cpu_limit_millis`] — R885-B5 / W344 Finding 5, filed after this
5513 /// field was rendered into a cgroup `cpu.max` and throttled four live
5514 /// workloads to a quarter core apiece.
5515 pub cpu_millis: u32,
5516
5517 // The four below were `yah.placement.memory-request-mb` and the
5518 // `yah.limits.*` annotations until R896-F3. Each defaults to `None`, which
5519 // is exactly what a peer that predates the field meant, so adding them
5520 // crossed a kamaji-proto V13 skew without a protocol bump. Read them
5521 // through the `WorkloadSpec` accessors named on each: those own the
5522 // fallback each one needs.
5523 /// Memory **request** in MiB — what a scheduler must find free on a node,
5524 /// separate from the [`Self::memory_mb`] ceiling. Read via
5525 /// [`WorkloadSpec::memory_request_mb`], which falls back to the ceiling.
5526 #[serde(default)]
5527 #[ts(optional = nullable)]
5528 pub memory_request_mb: Option<u32>,
5529
5530 /// Optional **hard CPU ceiling** in millicores — the mirror image of
5531 /// `memory_request_mb`: [`Self::cpu_millis`] is the request, this is the
5532 /// limit. Read via [`WorkloadSpec::cpu_limit_millis`]; `0` means none.
5533 #[serde(default)]
5534 #[ts(optional = nullable)]
5535 pub cpu_limit_millis: Option<u32>,
5536
5537 /// Hard **process-count ceiling** — cgroup v2 `pids.max`. Read via
5538 /// [`WorkloadSpec::pids_limit`], which falls back to [`DEFAULT_PIDS_MAX`]
5539 /// rather than to "unbounded".
5540 #[serde(default)]
5541 #[ts(optional = nullable)]
5542 pub pids_max: Option<u32>,
5543
5544 /// A **floor** on the microVM scratch disk in MiB — the smallest workspace
5545 /// this workload is willing to be given. Note the direction: the other
5546 /// limits here are ceilings a backend enforces downward, this is a floor a
5547 /// backend raises *up* to, and `floor` is in the name for exactly that
5548 /// reason (R885-T6 deleted `ephemeral_storage_mb` because it was documented
5549 /// as a cap and read as a floor). Read via [`WorkloadSpec::scratch_floor_mb`].
5550 #[serde(default)]
5551 #[ts(optional = nullable)]
5552 pub scratch_floor_mb: Option<u32>,
5553}
5554
5555impl ResourceLimits {
5556 /// The Docker/OCI relative CPU weight (`cpu.shares`, where `1024` ≈ one
5557 /// core) equivalent to this millicore request. The containerd and docker
5558 /// backends express CPU as a weight rather than a millicore request, so
5559 /// they derive it here instead of storing shares: `1000m` ⇒ `1024`.
5560 pub fn cpu_shares(&self) -> u64 {
5561 (u64::from(self.cpu_millis) * 1024) / 1000
5562 }
5563}
5564
5565// ── Healthcheck ───────────────────────────────────────────────────────────────
5566
5567/// Container health probe configuration.
5568#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5569#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5570pub struct Healthcheck {
5571 /// The probe executed to determine container health.
5572 pub probe: HealthProbe,
5573
5574 /// How often the probe runs.
5575 pub interval: Millis,
5576
5577 /// Per-probe timeout; a slow response counts as failure.
5578 pub timeout: Millis,
5579
5580 /// Time to wait after container start before the first probe. Shape
5581 /// validation warns (not errors) if this is less than
5582 /// `stop_policy.grace_period * 2`.
5583 pub initial_delay: Millis,
5584
5585 /// Number of consecutive failures before the container is marked
5586 /// `Unhealthy`.
5587 pub failure_threshold: u32,
5588}
5589
5590/// Mechanism used to check container health.
5591#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5592#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5593#[serde(rename_all = "snake_case")]
5594pub enum HealthProbe {
5595 /// HTTP GET to `path` on `port`. A 2xx (or `expect_status` if set)
5596 /// response counts as healthy.
5597 HttpGet {
5598 path: String,
5599 port: u16,
5600 #[ts(optional = nullable)]
5601 expect_status: Option<u16>,
5602 },
5603
5604 /// Run `argv` inside the container; exit-0 counts as healthy.
5605 Exec { argv: Vec<String> },
5606
5607 /// TCP connection to `port`; a successful connect counts as healthy.
5608 TcpConnect { port: u16 },
5609}
5610
5611// ── Restart / Stop ────────────────────────────────────────────────────────────
5612
5613/// What yubaba does when the container exits.
5614#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
5615#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5616#[serde(rename_all = "snake_case")]
5617pub enum RestartPolicy {
5618 /// Restart unconditionally on any exit.
5619 Always,
5620
5621 /// Restart on non-zero exit, up to `max_attempts` times with exponential
5622 /// backoff. After exhaustion, the workload is marked `Failed`.
5623 OnFailure {
5624 max_attempts: u32,
5625 backoff: BackoffPolicy,
5626 },
5627
5628 /// Do not restart. The container runs once and exits.
5629 ///
5630 /// **Forge convention.** Forge runs (R094) synthesize a `WorkloadSpec`
5631 /// using [`WorkloadSpec::for_forge`] which sets all the conventional fields
5632 /// together:
5633 ///
5634 /// - `restart_policy = Never`
5635 /// - `expose.public = None`, `expose.operator = None`
5636 /// - `expose.mesh.identity = "forge.<forge_id>"` — distinguishable from
5637 /// persistent mirror identities at the mesh layer
5638 /// - `tier = "infra"` (or the forge-spec's effective tier)
5639 /// - `annotations["yah.forge"] = "true"` — suppresses the shape warning
5640 ///
5641 /// Using `Never` on a persistent mirror (not a forge run) means the mirror
5642 /// stays dead after any exit — a likely misconfiguration. Shape validation
5643 /// emits a soft warning unless `annotations["yah.forge"] == "true"` is
5644 /// present. See R094 forge.
5645 Never,
5646}
5647
5648/// Exponential backoff parameters for `RestartPolicy::OnFailure`.
5649#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
5650#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5651pub struct BackoffPolicy {
5652 /// Initial delay before the first restart, in milliseconds.
5653 pub initial_ms: u32,
5654
5655 /// Maximum delay between retries, in milliseconds.
5656 pub max_ms: u32,
5657
5658 /// Backoff multiplier applied to each successive delay.
5659 pub multiplier: f32,
5660}
5661
5662/// Graceful shutdown configuration for yubaba's stop sequence.
5663#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5664#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5665pub struct StopPolicy {
5666 /// Signal number sent first, e.g. `15` (SIGTERM) or `2` (SIGINT).
5667 pub signal: i32,
5668
5669 /// Time yubaba waits after sending `signal` before issuing SIGKILL.
5670 pub grace_period: Millis,
5671}
5672
5673// ── Expose ────────────────────────────────────────────────────────────────────
5674
5675/// Network exposure configuration. The three channels are independent; any
5676/// combination is valid.
5677#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5678#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5679pub struct ExposeSpec {
5680 /// Mesh-internal exposure. Required; every workload must have a mesh
5681 /// identity even if no other workload currently reaches it.
5682 pub mesh: MeshExpose,
5683
5684 /// Public internet exposure via a Cloudflare tunnel route. `None` means
5685 /// the workload is not internet-reachable.
5686 #[ts(optional = nullable)]
5687 pub public: Option<PublicExpose>,
5688
5689 /// Operator-facing exposure via a Tailscale ACL tag. `None` means the
5690 /// workload is not operator-reachable via Tailscale.
5691 #[ts(optional = nullable)]
5692 pub operator: Option<OperatorExpose>,
5693}
5694
5695/// A peer permitted to initiate mesh connections to a workload (W206 / R558-F3).
5696///
5697/// Cross-tenant access is **deny-by-default**: a workload accepts inter-tenant
5698/// traffic only from peers it lists explicitly as [`MeshPeer::CrossTenant`].
5699/// Same-tenant access stays tier-based ([`MeshPeer::Tier`]) — the pre-R558
5700/// model — and an `allow_from` with no `Tier` entries still admits every
5701/// same-tenant peer (the historical "empty = allow all" default).
5702///
5703/// External serde tagging keeps this postcard-safe (R590-B3): no internal tag,
5704/// no untagged, no `skip_serializing_if`.
5705#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5706#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5707#[serde(rename_all = "snake_case")]
5708pub enum MeshPeer {
5709 /// Any **same-tenant** workload whose `tier` matches this tag. This is the
5710 /// pre-R558 `allow_from` semantics.
5711 Tier(TierTag),
5712
5713 /// A specific workload in **another tenant**, addressed by its fully
5714 /// qualified mesh identity `<tenant>/<namespace>/<name>`. There is no
5715 /// cross-tenant tier wildcard — each cross-tenant peer is granted
5716 /// individually, so a shared fleet stays isolated unless an operator opts
5717 /// in here.
5718 CrossTenant {
5719 tenant: TenantId,
5720 namespace: NamespaceId,
5721 /// Peer's mesh identity (its [`MeshExpose::identity`]).
5722 name: MeshIdent,
5723 },
5724}
5725
5726/// One port a workload listens on, as its manifest declares it (R844-F17).
5727///
5728/// Before this, `expose.mesh.ports` was an array of bare numbers and a port
5729/// name was unwritable anywhere in the workspace — names were real at every
5730/// tier *below* the manifest (kamaji's allocator resolves `name -> port`, a
5731/// service record publishes `{"http": 8080, "wss": 8443}`, the sibling wire
5732/// carries `named_ports`, `PORT_<NAME>` reaches the process) and synthesised
5733/// from nothing at the top by [`crate::MeshExpose`]'s number list. This is the
5734/// declaration surface that had to exist for any of that to be *stated* rather
5735/// than guessed.
5736///
5737/// ## Three spellings, one type
5738///
5739/// ```toml
5740/// ports = [8080] # a number, unnamed
5741/// ports = ["http", "wss"] # names; the supervisor picks the numbers
5742/// ports = [{ name = "https", port = 443 }] # both stated
5743/// ```
5744///
5745/// They mix freely in one array (`ports = [{ name = "http", port = 8080 },
5746/// "metrics"]`), because the two facts are independent: a container's ports are
5747/// fixed by its image and still want names, while a native workload's numbers
5748/// are the allocator's to choose and only the names are the author's.
5749///
5750/// ## What each spelling means downstream
5751///
5752/// - **A number** is a request to listen there. On a container backend that is
5753/// simply the container-side port. On the published (fleet) tier a number
5754/// outside `kamaji::ports::WORLD_FIXED_PORTS` is refused at bring-up rather
5755/// than honoured (R844-F14) — a stale pin is how one workload lands on the
5756/// port a co-tenant already holds.
5757/// - **A name** is what a consumer asks for: `ServiceRecord::port("wss")`, the
5758/// ingress planner resolving which listener a hostname fronts, the
5759/// `PORT_<NAME>` variable the process reads. A workload declaring several
5760/// ports and naming none has nothing called `http`, and the front door
5761/// refuses to resolve rather than publish a hostname at whichever listener
5762/// sorted first (`kamaji::name_anonymous_ports`). Naming them is how you
5763/// answer that question instead of being asked it.
5764///
5765/// ## Wire shapes
5766///
5767/// Human-readable formats (TOML/JSON) accept all three spellings and
5768/// round-trip back to the most compact faithful one. The binary wire (postcard,
5769/// behind the kamaji UDS) carries the plain two-`Option` struct: `untagged`
5770/// needs `deserialize_any`, which postcard refuses — the same split
5771/// [`ImageRef`] makes, and for the same reason (R590-B3).
5772///
5773/// Deliberately NOT `Default`: the all-`None` value is the one shape no accepted
5774/// spelling produces and `validate::shape` rejects, so a `..Default::default()`
5775/// would hand a caller exactly the invalid port.
5776#[derive(Debug, Clone, PartialEq, Eq)]
5777pub struct MeshPort {
5778 /// The name this port is known by — `http`, `wss`, `metrics`. `None` when
5779 /// the manifest wrote a bare number; `kamaji::name_anonymous_ports` then
5780 /// decides what to call it, which is deliberately *not* `http` when there
5781 /// is more than one.
5782 pub name: Option<String>,
5783
5784 /// The port number, when the manifest states one. `None` means the
5785 /// supervisor allocates it and tells the workload via `PORT_<NAME>`.
5786 pub number: Option<u16>,
5787}
5788
5789impl MeshPort {
5790 /// A bare number, unnamed — the pre-R844-F17 spelling, still valid.
5791 pub fn anonymous(number: u16) -> Self {
5792 Self {
5793 name: None,
5794 number: Some(number),
5795 }
5796 }
5797
5798 /// A named port whose number the supervisor allocates.
5799 pub fn named(name: impl Into<String>) -> Self {
5800 Self {
5801 name: Some(name.into()),
5802 number: None,
5803 }
5804 }
5805
5806 /// A named port whose number the manifest states.
5807 pub fn pinned(name: impl Into<String>, number: u16) -> Self {
5808 Self {
5809 name: Some(name.into()),
5810 number: Some(number),
5811 }
5812 }
5813}
5814
5815impl From<u16> for MeshPort {
5816 fn from(number: u16) -> Self {
5817 Self::anonymous(number)
5818 }
5819}
5820
5821impl From<&str> for MeshPort {
5822 fn from(name: &str) -> Self {
5823 Self::named(name)
5824 }
5825}
5826
5827impl From<String> for MeshPort {
5828 fn from(name: String) -> Self {
5829 Self::named(name)
5830 }
5831}
5832
5833/// The self-describing spelling of a [`MeshPort`] — the shape a TOML/JSON
5834/// author writes, and the one the generated JSON schema and TS bindings
5835/// advertise.
5836///
5837/// Kept as its own type rather than folded into `MeshPort` because it is only
5838/// half the story: the binary wire never sees it (see [`MeshPort`]'s docs), and
5839/// a struct with two `Option`s is the shape every *consumer* wants regardless
5840/// of which of the three forms the author picked.
5841#[derive(Serialize, Deserialize)]
5842#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5843#[serde(untagged)]
5844enum MeshPortRepr {
5845 /// `8080` — a number with no name.
5846 Number(u16),
5847 /// `"http"` — a name whose number the supervisor allocates.
5848 Name(String),
5849 /// `{ name = "https", port = 443 }` — both stated. `port` may be omitted,
5850 /// which is the table spelling of the bare-name form.
5851 Both {
5852 name: String,
5853 #[serde(default)]
5854 port: Option<u16>,
5855 },
5856}
5857
5858impl Serialize for MeshPort {
5859 fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
5860 where
5861 S: serde::Serializer,
5862 {
5863 if !ser.is_human_readable() {
5864 // Postcard and friends: the plain positional struct, every field
5865 // always encoded. See the V6 stanza in `kamaji_proto::version` —
5866 // there is no `skip_serializing_if` that is safe here.
5867 #[derive(Serialize)]
5868 struct Fields<'a> {
5869 name: &'a Option<String>,
5870 number: &'a Option<u16>,
5871 }
5872 return Fields {
5873 name: &self.name,
5874 number: &self.number,
5875 }
5876 .serialize(ser);
5877 }
5878
5879 match (&self.name, self.number) {
5880 (Some(name), Some(port)) => MeshPortRepr::Both {
5881 name: name.clone(),
5882 port: Some(port),
5883 },
5884 (Some(name), None) => MeshPortRepr::Name(name.clone()),
5885 (None, Some(port)) => MeshPortRepr::Number(port),
5886 // Not constructible from any accepted spelling; `validate::shape`
5887 // rejects it too. Emitted as an empty table rather than silently
5888 // becoming something else.
5889 (None, None) => MeshPortRepr::Both {
5890 name: String::new(),
5891 port: None,
5892 },
5893 }
5894 .serialize(ser)
5895 }
5896}
5897
5898impl<'de> Deserialize<'de> for MeshPort {
5899 fn deserialize<D>(de: D) -> Result<Self, D::Error>
5900 where
5901 D: serde::Deserializer<'de>,
5902 {
5903 if !de.is_human_readable() {
5904 #[derive(Deserialize)]
5905 struct Fields {
5906 name: Option<String>,
5907 number: Option<u16>,
5908 }
5909 let f = Fields::deserialize(de)?;
5910 return Ok(MeshPort {
5911 name: f.name,
5912 number: f.number,
5913 });
5914 }
5915
5916 Ok(match MeshPortRepr::deserialize(de)? {
5917 MeshPortRepr::Number(port) => MeshPort::anonymous(port),
5918 MeshPortRepr::Name(name) => MeshPort::named(name),
5919 MeshPortRepr::Both { name, port } => MeshPort {
5920 name: Some(name),
5921 number: port,
5922 },
5923 })
5924 }
5925}
5926
5927/// Mesh-internal port exposure and peer access control.
5928#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5929#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5930pub struct MeshExpose {
5931 /// DNS-segment mesh identity for this workload. Must be unique in the
5932 /// cluster. Regex: `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, length ≤ 63.
5933 pub identity: MeshIdent,
5934
5935 /// Ports this workload listens on, each optionally named (R844-F17). Other
5936 /// workloads reach it at `<identity>:<port>` on the mesh.
5937 ///
5938 /// See [`MeshPort`] for the three accepted spellings. Read the numbers with
5939 /// [`MeshExpose::numbers`] and the names with
5940 /// [`MeshExpose::named_numbers`] — there is deliberately no way to read
5941 /// this as a plain `Vec<u16>`, because a name-only entry has no number yet
5942 /// and a conversion that dropped it would be exactly the silent loss named
5943 /// ports exist to prevent.
5944 #[ts(type = "(number | string | { name: string, port?: number })[]")]
5945 #[cfg_attr(feature = "json-schema", schemars(with = "Vec<MeshPortRepr>"))]
5946 pub ports: Vec<MeshPort>,
5947
5948 /// Peers permitted to initiate connections to this workload on the mesh
5949 /// (W206 / R558-F3). Same-tenant tier rules and explicit cross-tenant
5950 /// grants share this one list. With **no** [`MeshPeer::Tier`] entries every
5951 /// same-tenant peer is admitted (the historical "empty = allow all"
5952 /// default); cross-tenant peers are always denied unless named by a
5953 /// [`MeshPeer::CrossTenant`] entry. See [`MeshExpose::admits_peer`].
5954 #[serde(default)]
5955 pub allow_from: Vec<MeshPeer>,
5956}
5957
5958impl MeshExpose {
5959 /// Every port *number* this workload declares, in declaration order.
5960 ///
5961 /// Name-only entries (`ports = ["http"]`) carry no number and are simply
5962 /// absent here — they do not have one until a supervisor allocates it. That
5963 /// is why this is a method rather than the field: a caller reading numbers
5964 /// has to be able to see that the list it got is shorter than the list the
5965 /// author wrote, and a `Vec<u16>` field could not say so.
5966 pub fn numbers(&self) -> Vec<u16> {
5967 self.ports.iter().filter_map(|p| p.number).collect()
5968 }
5969
5970 /// Whether `port` appears as a declared number.
5971 pub fn declares_number(&self, port: u16) -> bool {
5972 self.ports.iter().any(|p| p.number == Some(port))
5973 }
5974
5975 /// The `name -> number` map for every port the manifest declares *both*
5976 /// for. Name-only ports are absent (no number yet) and unnamed ports are
5977 /// absent (no name); `kamaji::name_anonymous_ports` is what fills the
5978 /// second gap once numbers are known.
5979 pub fn named_numbers(&self) -> BTreeMap<String, u16> {
5980 self.ports
5981 .iter()
5982 .filter_map(|p| Some((p.name.clone()?, p.number?)))
5983 .collect()
5984 }
5985
5986 /// Every port name the manifest states, in declaration order.
5987 pub fn names(&self) -> Vec<&str> {
5988 self.ports
5989 .iter()
5990 .filter_map(|p| p.name.as_deref())
5991 .collect()
5992 }
5993
5994 /// The pre-R844-F17 spelling as a value: a list of unnamed numbers. Kept
5995 /// because most call sites — and every test fixture — genuinely mean
5996 /// "these numbers, names irrelevant".
5997 pub fn anonymous_ports(numbers: impl IntoIterator<Item = u16>) -> Vec<MeshPort> {
5998 numbers.into_iter().map(MeshPort::anonymous).collect()
5999 }
6000
6001 /// Whether a peer may initiate a mesh connection to a workload whose mesh
6002 /// exposure is `self`. `own_tenant` is the tenant of the workload being
6003 /// protected; the remaining arguments identify the connecting peer.
6004 ///
6005 /// Deny-by-default across tenants (W206 / R558-F3):
6006 /// - **Same tenant** (`own_tenant == peer_tenant`): admitted when the
6007 /// peer's tier matches a [`MeshPeer::Tier`] rule, or when there are no
6008 /// `Tier` rules at all (historical "empty `allow_from` = allow all
6009 /// same-tenant").
6010 /// - **Cross tenant**: admitted only when an explicit
6011 /// [`MeshPeer::CrossTenant`] entry matches the peer's
6012 /// `(tenant, namespace, name)`.
6013 pub fn admits_peer(
6014 &self,
6015 own_tenant: &TenantId,
6016 peer_tenant: &TenantId,
6017 peer_namespace: &NamespaceId,
6018 peer_name: &MeshIdent,
6019 peer_tier: &TierTag,
6020 ) -> bool {
6021 if own_tenant == peer_tenant {
6022 let mut has_tier_rule = false;
6023 for peer in &self.allow_from {
6024 if let MeshPeer::Tier(t) = peer {
6025 has_tier_rule = true;
6026 if t == peer_tier {
6027 return true;
6028 }
6029 }
6030 }
6031 // No same-tenant tier restriction declared → admit all same-tenant.
6032 !has_tier_rule
6033 } else {
6034 self.allow_from.iter().any(|peer| {
6035 matches!(
6036 peer,
6037 MeshPeer::CrossTenant { tenant, namespace, name }
6038 if tenant == peer_tenant
6039 && namespace == peer_namespace
6040 && name == peer_name
6041 )
6042 })
6043 }
6044 }
6045}
6046
6047/// The name by which a workload is addressed **within its own tenant** (W206 /
6048/// R558-F3), given every `(namespace, identity)` pair present in that tenant.
6049///
6050/// Within a tenant, a workload is reached by its short mesh `identity` when that
6051/// identity is unique across the tenant's namespaces. When two namespaces
6052/// expose the same identity, the name is ambiguous, so both are disambiguated
6053/// by a namespace prefix — `<namespace>.<identity>` (e.g. `yah.runner` vs
6054/// `noisetable.runner`). Cross-tenant addressing always uses the full FQN
6055/// ([`WorkloadSpec::fq_mesh_identity`]) and is out of scope here.
6056pub fn intra_tenant_address(
6057 namespace: &NamespaceId,
6058 identity: &MeshIdent,
6059 tenant_workloads: &[(NamespaceId, MeshIdent)],
6060) -> String {
6061 let collides = tenant_workloads
6062 .iter()
6063 .any(|(ns, id)| id == identity && ns != namespace);
6064 if collides {
6065 format!("{}.{}", namespace.0, identity.0)
6066 } else {
6067 identity.0.clone()
6068 }
6069}
6070
6071/// Public internet exposure via a Cloudflare tunnel route.
6072#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
6073#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
6074pub struct PublicExpose {
6075 /// Public hostname to route, e.g. `"api.noisetable.io"`. Semantic
6076 /// validation checks that this hostname is owned by a configured CF zone.
6077 pub hostname: String,
6078
6079 /// Container-side port to route traffic to. Shape validation requires this
6080 /// port to appear in `expose.mesh.ports`.
6081 pub port: u16,
6082
6083 /// TLS configuration for the public endpoint.
6084 pub tls: PublicTls,
6085}
6086
6087/// TLS mode for a public endpoint.
6088#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
6089#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
6090#[serde(rename_all = "snake_case")]
6091pub enum PublicTls {
6092 /// Cloudflare manages the TLS certificate (default; requires a proxied DNS
6093 /// record in the configured zone).
6094 CfManaged,
6095
6096 /// User-supplied certificate referenced by name in the yubaba secret store.
6097 UserCertRef { name: String },
6098}
6099
6100/// Operator-facing exposure via a Tailscale ACL tag.
6101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
6102#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
6103pub struct OperatorExpose {
6104 /// Tailscale ACL tag granting access, e.g. `"tag:noisetable-ops"`. Semantic
6105 /// validation checks that this tag exists in the cluster's Tailscale ACL.
6106 pub tailscale_tag: String,
6107
6108 /// Container-side port to expose to Tailscale-authorized operators.
6109 pub port: u16,
6110}
6111
6112// ── ImageRef helpers ──────────────────────────────────────────────────────────
6113
6114impl ImageRef {
6115 /// The all-zeros sha256 digest that marks an image reference as **not
6116 /// content-pinned**. No real image can carry it, so a build that never
6117 /// injected a compile-time digest (dev builds) or a catalog image that
6118 /// isn't published-and-pinned yet lands on this sentinel. This is the
6119 /// single source of truth both the catalog emitter
6120 /// (`task::default_image::catalog_image`, which writes it) and the
6121 /// container-runtime resolvers ([`Self::pull_ref`], via kamaji) agree on —
6122 /// keeping them here means they cannot drift. [`testing::TEST_DIGEST`] is
6123 /// the same value re-exported for fixtures.
6124 pub const UNPINNED_DIGEST: &'static str =
6125 "sha256:0000000000000000000000000000000000000000000000000000000000000000";
6126
6127 /// Parse a full digest-pinned image reference —
6128 /// `[registry/]repo[:tag]@sha256:<hex>` — into its parts.
6129 ///
6130 /// This is the public door onto the same parser the `ImageRef` string-form
6131 /// `Deserialize` arm uses, so a config that spells an image as one string
6132 /// (a qed `step.image`, a transform recipe) and a config that spells it as
6133 /// a struct land on identical semantics. A bare tag is rejected: the whole
6134 /// point of the string form is that it carries the digest.
6135 pub fn parse_pinned(s: &str) -> Result<Self, String> {
6136 compose_import::parse_pinned_image_ref(s)
6137 }
6138
6139 /// Format this reference as a Docker-compatible image string,
6140 /// `{registry}/{repository}:{tag}@{digest}`. Tag is included for human
6141 /// readability; the digest is what the pull resolves against. Always emits
6142 /// the digest — this is the display/logging form; use [`Self::pull_ref`]
6143 /// for the string handed to a container runtime.
6144 pub fn docker_ref(&self) -> String {
6145 format!("{}/{}:{}@{}", self.registry, self.repository, self.tag, self.digest)
6146 }
6147
6148 /// True when this reference carries a real content-addressed digest, i.e.
6149 /// its digest is not the all-zeros [`Self::UNPINNED_DIGEST`] sentinel.
6150 pub fn is_pinned(&self) -> bool {
6151 self.digest != Self::UNPINNED_DIGEST
6152 }
6153
6154 /// The reference string to hand a container runtime for pull/resolve.
6155 ///
6156 /// - **Pinned** (real digest): `{registry}/{repository}:{tag}@{digest}` —
6157 /// content-addressed, the reproducible path.
6158 /// - **Unpinned** (all-zeros [`Self::UNPINNED_DIGEST`]): `{registry}/{repository}:{tag}`
6159 /// — tag-only. No registry or local store holds an image under the
6160 /// sentinel digest, so `…@sha256:0000…` can never resolve; a
6161 /// tag-pulled or locally-built image is keyed by `registry/repo:tag`.
6162 /// This is the tag-fallback path that lets a not-yet-published catalog
6163 /// image (e.g. a from-source build-worker image) still pull by tag.
6164 pub fn pull_ref(&self) -> String {
6165 if self.is_pinned() {
6166 format!("{}/{}:{}@{}", self.registry, self.repository, self.tag, self.digest)
6167 } else {
6168 format!("{}/{}:{}", self.registry, self.repository, self.tag)
6169 }
6170 }
6171}
6172
6173// ── WorkloadRuntime trait ─────────────────────────────────────────────────────
6174
6175/// Shared interface for deploying and managing `WorkloadSpec` containers.
6176///
6177/// This is the keystone abstraction (R256-F10) that makes sim and cloud
6178/// literally interchangeable at the container level:
6179///
6180/// - **Camp/sim tier**: `LocalDockerRuntime` in `cloud` implements this trait
6181/// via the docker CLI pointed at OrbStack (or any Docker-compatible socket).
6182/// No mesh — containers communicate over OrbStack's bridge network.
6183///
6184/// - **Yubaba/cloud-HA tier**: `yubaba::runtime::ContainerRuntime` (gRPC to
6185/// containerd) will implement this trait. Mesh assignment is a separate
6186/// orchestration step on top (handled by yubaba's raft layer), not part
6187/// of the shared deploy/supervise interface.
6188///
6189/// Callers that type against `WorkloadRuntime` automatically work with both
6190/// backends. Reconcilers in `cloud` use it today; yubaba wires its own impl
6191/// when R276 Tier-3 lands.
6192#[async_trait::async_trait]
6193pub trait WorkloadRuntime: Send + Sync {
6194 /// Deploy a workload described by `spec`. Pulls the image if needed,
6195 /// creates and starts the container, and returns an opaque workload ID
6196 /// (typically the container name derived from `spec.name`).
6197 ///
6198 /// Idempotent: re-deploying a running workload replaces it cleanly.
6199 async fn deploy_workload(&self, spec: &WorkloadSpec) -> anyhow::Result<String>;
6200
6201 /// Tear down a deployed workload — stop the process and remove all
6202 /// associated state. No-op when the workload is already gone.
6203 async fn teardown_workload(&self, name: &str) -> anyhow::Result<()>;
6204
6205 /// Returns `true` when the named workload is currently running (i.e.
6206 /// the container process is alive and has not exited).
6207 async fn is_running(&self, name: &str) -> anyhow::Result<bool>;
6208
6209 /// Probe the runtime backend. Returns `true` when the backend socket is
6210 /// reachable and healthy (e.g. docker daemon up, containerd gRPC up).
6211 /// Used by health endpoints and startup checks.
6212 async fn runtime_health(&self) -> anyhow::Result<bool>;
6213}
6214
6215// ── Tests ─────────────────────────────────────────────────────────────────────
6216
6217#[cfg(test)]
6218mod tests {
6219 use super::*;
6220
6221 // ── R658-B1 `routes` belongs at the top level, not inside [build] ─────────
6222
6223 /// The canonical `mesofact-static` manifest shape: `routes` above the
6224 /// `[build]` header, where TOML keeps it top-level.
6225 #[test]
6226 fn mesofact_static_routes_parse_at_the_top_level() {
6227 let src = r#"
6228kind = "mesofact-static"
6229routes = "./mesofact.routes.ts"
6230
6231[build]
6232command = "bun run build"
6233out_dir = "dist"
6234"#;
6235 let Workload::MesofactStatic(site) =
6236 toml::from_str::<Workload>(src).expect("canonical shape must parse")
6237 else {
6238 panic!("kind = \"mesofact-static\" must select MesofactStatic");
6239 };
6240 assert_eq!(site.routes, PathBuf::from("./mesofact.routes.ts"));
6241 assert_eq!(site.build.out_dir, PathBuf::from("dist"));
6242 }
6243
6244 /// The bug R658-B1 exists for: `routes` written *below* `[build]` is
6245 /// `build.routes` as far as TOML is concerned. `BuildConfig` used to
6246 /// discard the stray key, so this manifest parsed as far as the missing
6247 /// top-level field and blamed the wrong line — or, once `routes` had a
6248 /// default, would have deployed a site that enumerated no routes at all.
6249 ///
6250 /// `deny_unknown_fields` makes the misplacement itself the error, and the
6251 /// message names `routes`, which is the one thing the author needs to move.
6252 #[test]
6253 fn mesofact_static_routes_inside_build_is_rejected_by_name() {
6254 let src = r#"
6255kind = "mesofact-static"
6256
6257[build]
6258command = "bun run build"
6259out_dir = "dist"
6260routes = "./mesofact.routes.ts"
6261"#;
6262 let err = toml::from_str::<Workload>(src)
6263 .expect_err("`routes` under [build] must not parse silently")
6264 .to_string();
6265 assert!(
6266 err.contains("routes"),
6267 "the error must name the misplaced key so the fix is obvious; got: {err}"
6268 );
6269 }
6270
6271 /// Guard the general case, not just the one key that bit us: any unknown
6272 /// `[build]` key is refused rather than dropped on the floor.
6273 #[test]
6274 fn unknown_build_keys_are_refused_rather_than_ignored() {
6275 let src = r#"
6276command = "bun run build"
6277out_dir = "dist"
6278outdir = "dist"
6279"#;
6280 let err = toml::from_str::<BuildConfig>(src)
6281 .expect_err("a typo'd build key must not be silently ignored")
6282 .to_string();
6283 assert!(err.contains("outdir"), "got: {err}");
6284
6285 // …and the keys that ARE modelled still round-trip.
6286 let ok: BuildConfig = toml::from_str(
6287 r#"
6288command = "bun run build"
6289out_dir = "dist"
6290render_command = "mesofact-build render . --route {route}"
6291"#,
6292 )
6293 .expect("modelled keys must still parse");
6294 assert_eq!(ok.render_command.as_deref(), Some("mesofact-build render . --route {route}"));
6295 }
6296
6297 // ── R783-F1 / W324: container manifest vs wire spec ────────────────────────
6298
6299 /// The acceptance case. `crates/yah/cloud-admin/workload.toml` is the file
6300 /// that could not parse through the envelope at all (R658-B2 pinned it in
6301 /// `xtask/tests/workload_envelope.rs` as `missing field \`image\``): it is a
6302 /// Dockerfile recipe, and the envelope only knew digest-pinned specs.
6303 ///
6304 /// The `[process]` table is deliberately present — that file is read by
6305 /// `LocalProcessReconciler` on the dev mirror *and* `ContainerReconciler`
6306 /// on pond, so the container form must tolerate the other tier's table
6307 /// rather than reject the file (W324 §1).
6308 #[test]
6309 fn container_recipe_parses_including_the_other_tier_s_table() {
6310 let src = r#"
6311name = "yah-cloud-admin"
6312kind = "container"
6313
6314[build]
6315dockerfile = "Dockerfile"
6316context = "."
6317image = "yah-local/yah-cloud-admin:dev"
6318
6319[run]
6320port = 4325
6321host_port = 4326
6322
6323[run.env]
6324YAH_CLOUD_ADMIN_ADDR = "0.0.0.0:4325"
6325
6326[[run.mounts]]
6327host = ".yah/infra"
6328container = "/workspace/.yah/infra"
6329
6330[process]
6331cargo_package = "yah-cloud-admin"
6332port = 4325
6333"#;
6334 let workload = toml::from_str::<Workload>(src).expect("the recipe form must parse");
6335 assert_eq!(workload.kind_str(), "container");
6336
6337 let recipe = workload
6338 .container_manifest()
6339 .and_then(ContainerManifest::as_recipe)
6340 .expect("a [build] table selects the recipe form");
6341 assert_eq!(recipe.name, "yah-cloud-admin");
6342 assert_eq!(recipe.build.dockerfile, PathBuf::from("Dockerfile"));
6343 assert_eq!(recipe.build.context, Some(PathBuf::from(".")));
6344 assert_eq!(
6345 recipe.build.image.as_deref(),
6346 Some("yah-local/yah-cloud-admin:dev")
6347 );
6348 assert_eq!(recipe.run.port, Some(4325));
6349 assert_eq!(recipe.run.host_port, Some(4326));
6350 assert_eq!(
6351 recipe.run.env.get("YAH_CLOUD_ADMIN_ADDR").map(String::as_str),
6352 Some("0.0.0.0:4325")
6353 );
6354 assert_eq!(recipe.run.mounts.len(), 1);
6355 assert!(recipe.run.mounts[0].read_only, "mounts default to read-only");
6356
6357 // The recipe has no spec — that is the whole point of the split.
6358 assert!(workload.container_spec().is_none());
6359 }
6360
6361 /// The other branch: no `[build]` table means the flat fields are a
6362 /// digest-pinned `WorkloadSpec`, exactly as before the split.
6363 #[test]
6364 fn container_reference_still_parses_as_a_workload_spec() {
6365 let spec = archetype_test_spec("noisetable-api");
6366 let toml_src = toml::to_string(&Workload::container(spec.clone())).expect("serialize");
6367 assert!(
6368 toml_src.contains("kind = \"container\""),
6369 "the on-disk form stays flat + internally tagged: {toml_src}"
6370 );
6371
6372 let back = toml::from_str::<Workload>(&toml_src).expect("deserialize");
6373 assert_eq!(back.container_spec(), Some(&spec));
6374 }
6375
6376 /// Explicit-branch deserialize exists so this error survives. Under
6377 /// `#[serde(untagged)]` it would read "data did not match any variant of
6378 /// untagged enum ContainerManifest", which tells an author nothing.
6379 #[test]
6380 fn a_malformed_container_reference_still_names_the_missing_field() {
6381 let src = r#"
6382kind = "container"
6383name = "noisetable-api"
6384image = "ghcr.io/noisetable/api:v1@sha256:0000000000000000000000000000000000000000000000000000000000000000"
6385replicas = 1
6386"#;
6387 let err = toml::from_str::<Workload>(src)
6388 .expect_err("a reference missing a required field must not parse")
6389 .to_string();
6390 assert!(err.contains("missing field `tier`"), "got: {err}");
6391 }
6392
6393 /// The one file that names neither marker. `missing field \`image\`` would
6394 /// send a recipe author off to add a field their form does not have, so
6395 /// the error names both forms instead.
6396 #[test]
6397 fn a_container_with_neither_image_nor_build_names_both_forms() {
6398 let src = r#"
6399kind = "container"
6400name = "yah-cloud-admin"
6401
6402[run]
6403port = 4325
6404"#;
6405 let err = toml::from_str::<Workload>(src)
6406 .expect_err("neither form is declared")
6407 .to_string();
6408 assert!(err.contains("image"), "got: {err}");
6409 assert!(err.contains("[build]"), "got: {err}");
6410 }
6411
6412 /// W324 §5's invariant, as a signature: there is no path from a recipe to
6413 /// a `WorkloadSpec` that does not name a digest.
6414 #[test]
6415 fn a_recipe_lowers_only_once_a_build_has_produced_a_digest() {
6416 let recipe = ContainerBuild {
6417 name: "yah-cloud-admin".into(),
6418 build: ContainerBuildStep {
6419 dockerfile: "Dockerfile".into(),
6420 context: Some(".".into()),
6421 image: Some("yah-local/yah-cloud-admin:dev".into()),
6422 },
6423 run: ContainerRunConfig {
6424 port: Some(4325),
6425 host_port: Some(4326),
6426 env: BTreeMap::from([("A".to_string(), "b".to_string())]),
6427 mounts: vec![ContainerMount {
6428 host: ".yah/infra".into(),
6429 container: "/workspace/.yah/infra".into(),
6430 read_only: true,
6431 }],
6432 },
6433 };
6434
6435 let digest = testing::test_digest();
6436 let spec = recipe
6437 .clone()
6438 .into_spec(&digest, TierTag("private".into()))
6439 .expect("a well-formed digest lowers");
6440 assert_eq!(spec.name, "yah-cloud-admin");
6441 assert_eq!(spec.image.digest, digest);
6442 assert_eq!(spec.image.repository, "yah-local/yah-cloud-admin");
6443 assert_eq!(spec.image.tag, "dev");
6444 assert_eq!(spec.expose.mesh.numbers(), vec![4325]);
6445 assert_eq!(spec.env.len(), 1);
6446 assert_eq!(spec.volumes.len(), 1);
6447
6448 // A bare tag is not a digest. Lowering must fail rather than mint a
6449 // spec that lies about being content-addressed (R438-T3).
6450 let err = recipe
6451 .into_spec("dev", TierTag("private".into()))
6452 .expect_err("an unpinned digest must not lower");
6453 assert!(err.contains("sha256"), "got: {err}");
6454 }
6455
6456 /// A recipe is a first-class on-disk value: it survives a write/read of
6457 /// the manifest unchanged. The other half of the gate — that the same
6458 /// value is *refused* by postcard — is in `tests/round_trip.rs`, which
6459 /// also pins the reference form's byte layout.
6460 #[test]
6461 fn a_recipe_round_trips_on_disk_under_the_container_kind() {
6462 let recipe = Workload::Container(ContainerManifest::Recipe(ContainerBuild {
6463 name: "yah-cloud-admin".into(),
6464 build: ContainerBuildStep::default(),
6465 run: ContainerRunConfig::default(),
6466 }));
6467 assert_eq!(recipe.kind_str(), "container");
6468
6469 let src = toml::to_string(&recipe).expect("a recipe serializes to disk");
6470 assert!(src.contains("kind = \"container\""), "{src}");
6471 let back: Workload = toml::from_str(&src).expect("and parses back");
6472 assert_eq!(back, recipe);
6473 }
6474
6475 // ── R603-T5 durable forge produced convention ──────────────────────────────
6476
6477 #[test]
6478 fn forge_produced_ident_parse() {
6479 assert_eq!(forge_produced::forge_id_from_ident("forge.abc123"), Some("abc123"));
6480 assert_eq!(forge_produced::forge_id_from_ident("svc.web"), None);
6481 assert_eq!(forge_produced::forge_id_from_ident("abc123"), None);
6482 }
6483
6484 #[test]
6485 fn forge_produced_host_path_translates_under_convention_dir() {
6486 let hp = forge_produced::host_path(
6487 "fid",
6488 std::path::Path::new("/yah/produced/librusty_v8.tar.gz"),
6489 )
6490 .expect("path under the convention dir translates");
6491 assert_eq!(
6492 hp,
6493 PathBuf::from("/var/lib/yah/qed/produced/fid/librusty_v8.tar.gz")
6494 );
6495 }
6496
6497 #[test]
6498 fn forge_produced_host_path_rejects_paths_outside_convention_dir() {
6499 assert_eq!(
6500 forge_produced::host_path("fid", std::path::Path::new("/tmp/x.tar.gz")),
6501 None,
6502 "a path outside /yah/produced has no durable host mapping"
6503 );
6504 }
6505
6506 #[test]
6507 fn forge_produced_host_path_rejects_traversal() {
6508 // A `..` component must never let a read escape the per-forge dir.
6509 assert_eq!(
6510 forge_produced::host_path(
6511 "fid",
6512 std::path::Path::new("/yah/produced/../../etc/passwd")
6513 ),
6514 None,
6515 "traversal out of the per-forge dir must be refused"
6516 );
6517 }
6518
6519 #[test]
6520 fn forge_produced_durable_mount_shape() {
6521 let m = forge_produced::durable_mount("fid");
6522 assert_eq!(m.target, PathBuf::from("/yah/produced"));
6523 assert!(!m.read_only, "the build must be able to write to it");
6524 assert_eq!(
6525 m.source,
6526 VolumeSource::Bind {
6527 host_path: PathBuf::from("/var/lib/yah/qed/produced/fid"),
6528 }
6529 );
6530 }
6531
6532 const HASH_64: &str = "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890";
6533
6534 #[test]
6535 fn blake_hash_accepts_64_hex() {
6536 let h: BlakeHash = toml::from_str(&format!("x = \"{HASH_64}\""))
6537 .map(|t: toml::Table| t["x"].as_str().unwrap().to_owned())
6538 .map(|s| serde_json::from_value(serde_json::Value::String(s)).unwrap())
6539 .unwrap();
6540 assert_eq!(h.0, HASH_64);
6541 }
6542
6543 #[test]
6544 fn blake_hash_rejects_wrong_length() {
6545 let short = "abcdef";
6546 let res: Result<BlakeHash, _> =
6547 serde_json::from_value(serde_json::Value::String(short.into()));
6548 assert!(res.is_err());
6549 }
6550
6551 #[test]
6552 fn blake_hash_rejects_non_hex() {
6553 let bad = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
6554 let res: Result<BlakeHash, _> =
6555 serde_json::from_value(serde_json::Value::String(bad.into()));
6556 assert!(res.is_err());
6557 }
6558
6559 fn image_ref(digest: &str) -> ImageRef {
6560 ImageRef {
6561 registry: "ghcr.io".into(),
6562 repository: "yah-ai/rusty-v8-musl-builder".into(),
6563 tag: "latest".into(),
6564 digest: digest.into(),
6565 }
6566 }
6567
6568 #[test]
6569 fn is_pinned_distinguishes_real_digest_from_sentinel() {
6570 assert!(!image_ref(ImageRef::UNPINNED_DIGEST).is_pinned());
6571 assert!(!image_ref(&testing::test_digest()).is_pinned());
6572 assert!(image_ref("sha256:deadbeef").is_pinned());
6573 }
6574
6575 #[test]
6576 fn pull_ref_pinned_carries_tag_and_digest() {
6577 assert_eq!(
6578 image_ref("sha256:deadbeef").pull_ref(),
6579 "ghcr.io/yah-ai/rusty-v8-musl-builder:latest@sha256:deadbeef",
6580 );
6581 }
6582
6583 #[test]
6584 fn pull_ref_unpinned_falls_back_to_tag_only() {
6585 // An unpinned catalog image (all-zeros sentinel) resolves by tag —
6586 // no store holds `…@sha256:0000…`, so the tag is the only usable key.
6587 assert_eq!(
6588 image_ref(ImageRef::UNPINNED_DIGEST).pull_ref(),
6589 "ghcr.io/yah-ai/rusty-v8-musl-builder:latest",
6590 );
6591 }
6592
6593 #[test]
6594 fn test_digest_alias_is_the_unpinned_sentinel() {
6595 assert_eq!(testing::TEST_DIGEST, ImageRef::UNPINNED_DIGEST);
6596 }
6597
6598 #[test]
6599 fn static_asset_workload_round_trips() {
6600 let src = format!(
6601 r#"
6602
6603[[asset]]
6604filename = "whisper/distil-large-v3-q5_1.bin"
6605source = "sources/distil-large-v3-q5_1.bin"
6606blake3 = "{HASH_64}"
6607
6608[[asset]]
6609filename = "whisper/distil-large-v3-q4_0.bin"
6610source = "sources/distil-large-v3-q4_0.bin"
6611blake3 = "{HASH_64}"
6612
6613[aliases]
6614"whisper-default" = "whisper/distil-large-v3-q5_1.bin"
6615"#
6616 );
6617 let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
6618 assert_eq!(w.assets.len(), 2);
6619 assert_eq!(w.assets[0].filename, "whisper/distil-large-v3-q5_1.bin");
6620 assert_eq!(w.assets[0].blake3.0, HASH_64);
6621 assert_eq!(w.aliases["whisper-default"], "whisper/distil-large-v3-q5_1.bin");
6622
6623 let back = toml::to_string(&w).expect("serialize");
6624 let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
6625 assert_eq!(w, w2);
6626 }
6627
6628 #[test]
6629 fn license_round_trip_each_variant() {
6630 // Wire format is whatever serde's `rename_all = "kebab-case"` emits.
6631 // heck's kebab-case keeps letter→digit attached but splits digit→uppercase,
6632 // so `Apache2 → "apache2"` and `Bsd2Clause → "bsd2-clause"`.
6633 for (variant, on_wire) in [
6634 (License::Mit, "mit"),
6635 (License::Apache2, "apache2"),
6636 (License::Bsd2Clause, "bsd2-clause"),
6637 (License::Bsd3Clause, "bsd3-clause"),
6638 (License::Isc, "isc"),
6639 ] {
6640 let ser = serde_json::to_value(variant).expect("serialize");
6641 assert_eq!(ser, serde_json::Value::String(on_wire.into()));
6642 let back: License = serde_json::from_value(ser).expect("deserialize");
6643 assert_eq!(back, variant);
6644 }
6645 }
6646
6647 #[test]
6648 fn license_rejects_non_permissive_variants() {
6649 for unknown in ["GPL-3.0", "AGPL", "lgpl-2.1", "unknown", "MIT"] {
6650 let res: Result<License, _> =
6651 serde_json::from_value(serde_json::Value::String(unknown.into()));
6652 assert!(res.is_err(), "expected rejection for {unknown:?}");
6653 }
6654 }
6655
6656 #[test]
6657 fn fetch_source_round_trips() {
6658 let src = format!(
6659 r#"
6660url = "https://example.invalid/upstream.bin"
6661blake3 = "{HASH_64}"
6662license = "mit"
6663"#
6664 );
6665 let fs: FetchSource = toml::from_str(&src).expect("parse");
6666 assert_eq!(fs.url, "https://example.invalid/upstream.bin");
6667 assert_eq!(fs.blake3.0, HASH_64);
6668 assert_eq!(fs.license, License::Mit);
6669
6670 let back = toml::to_string(&fs).expect("serialize");
6671 let fs2: FetchSource = toml::from_str(&back).expect("re-parse");
6672 assert_eq!(fs, fs2);
6673 }
6674
6675 #[test]
6676 fn fetch_source_rejects_unknown_license() {
6677 let src = format!(
6678 r#"
6679url = "https://example.invalid/upstream.bin"
6680blake3 = "{HASH_64}"
6681license = "GPL-3.0"
6682"#
6683 );
6684 let res: Result<FetchSource, _> = toml::from_str(&src);
6685 assert!(res.is_err(), "expected non-permissive license to reject");
6686 }
6687
6688 #[test]
6689 fn asset_entry_derive_mode_round_trips() {
6690 let src = format!(
6691 r#"
6692
6693[[asset]]
6694filename = "whisper/distil-large-v3-q5_1.bin"
6695blake3 = "{HASH_64}"
6696
6697[asset.derive.fetch]
6698url = "https://example.invalid/ggml-distil-large-v3.bin"
6699blake3 = "{HASH_64}"
6700license = "mit"
6701
6702[asset.derive.transform]
6703recipe = "whisper-quantize"
6704params = {{ quant = "q5_1" }}
6705"#
6706 );
6707 let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
6708 assert_eq!(w.assets.len(), 1);
6709 let entry = &w.assets[0];
6710 assert!(entry.source.is_none());
6711 let derive = entry.derive.as_ref().expect("derive present");
6712 assert_eq!(derive.fetch.url, "https://example.invalid/ggml-distil-large-v3.bin");
6713 assert_eq!(derive.fetch.license, License::Mit);
6714 let transform = derive.transform.as_ref().expect("transform present");
6715 assert_eq!(transform.recipe, "whisper-quantize");
6716 assert_eq!(transform.params.get("quant").map(String::as_str), Some("q5_1"));
6717
6718 let back = toml::to_string(&w).expect("serialize");
6719 let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
6720 assert_eq!(w, w2);
6721 }
6722
6723 #[test]
6724 fn legacy_source_only_asset_serializes_without_derive_field() {
6725 // Verify the skip_serializing_if guards keep legacy TOMLs round-tripping
6726 // without ever emitting an empty `derive = ...` line.
6727 let src = format!(
6728 r#"
6729
6730[[asset]]
6731filename = "operator-curated.bin"
6732source = "sources/operator-curated.bin"
6733blake3 = "{HASH_64}"
6734"#
6735 );
6736 let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
6737 let back = toml::to_string(&w).expect("serialize");
6738 assert!(!back.contains("derive"), "serialized output leaked a derive field: {back}");
6739 let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
6740 assert_eq!(w, w2);
6741 }
6742
6743 /// W212/R518: the `[asset.derive.lock]` block round-trips through TOML, and
6744 /// is omitted from output when absent (so non-derive / unlocked assets stay
6745 /// clean).
6746 #[test]
6747 fn derive_lock_round_trips_through_toml() {
6748 let toml = r#"
6749url = "https://example.invalid/config.json"
6750blake3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6751license = "mit"
6752"#;
6753 let fetch: FetchSource = ::toml::from_str(toml).unwrap();
6754 let derive = AssetDerive {
6755 fetch,
6756 transform: Some(TransformSpec {
6757 recipe: "whisper-bundle-tar".into(),
6758 params: BTreeMap::new(),
6759 }),
6760 lock: Some(DeriveLock {
6761 input_hash: "1111111111111111111111111111111111111111111111111111111111111111".into(),
6762 output_blake3: "2222222222222222222222222222222222222222222222222222222222222222".into(),
6763 }),
6764 };
6765 let s = ::toml::to_string(&derive).unwrap();
6766 assert!(s.contains("[lock]"), "lock serialized: {s}");
6767 let back: AssetDerive = ::toml::from_str(&s).unwrap();
6768 assert_eq!(derive, back);
6769
6770 // Absent lock → no `[lock]` table in the output.
6771 let unlocked = AssetDerive { lock: None, ..derive };
6772 let s2 = ::toml::to_string(&unlocked).unwrap();
6773 assert!(!s2.contains("[lock]"), "unlocked must omit lock: {s2}");
6774 }
6775
6776 #[test]
6777 fn shape_static_asset_rejects_both_source_and_derive() {
6778 use crate::validate::{shape_static_asset, FieldPath, ShapeError};
6779
6780 let entry = AssetEntry {
6781 filename: "ambiguous.bin".into(),
6782 source: Some("sources/ambiguous.bin".into()),
6783 derive: Some(AssetDerive {
6784 fetch: FetchSource {
6785 url: "https://example.invalid/x".into(),
6786 blake3: BlakeHash(HASH_64.into()),
6787 license: License::Mit,
6788 },
6789 transform: None,
6790 lock: None,
6791 }),
6792 blake3: BlakeHash(HASH_64.into()),
6793 };
6794 let w = StaticAssetWorkload {
6795 assets: vec![entry],
6796 aliases: BTreeMap::new(),
6797 };
6798 let err = shape_static_asset(&w).expect_err("XOR violated");
6799 match err {
6800 ShapeError::Field { path: FieldPath::Asset(0, "source"), .. } => {}
6801 other => panic!("expected Asset(0, \"source\") shape error, got {other:?}"),
6802 }
6803 }
6804
6805 #[test]
6806 fn shape_static_asset_rejects_neither_source_nor_derive() {
6807 use crate::validate::{shape_static_asset, FieldPath, ShapeError};
6808
6809 let entry = AssetEntry {
6810 filename: "empty.bin".into(),
6811 source: None,
6812 derive: None,
6813 blake3: BlakeHash(HASH_64.into()),
6814 };
6815 let w = StaticAssetWorkload {
6816 assets: vec![entry],
6817 aliases: BTreeMap::new(),
6818 };
6819 let err = shape_static_asset(&w).expect_err("XOR violated");
6820 match err {
6821 ShapeError::Field { path: FieldPath::Asset(0, "source"), .. } => {}
6822 other => panic!("expected Asset(0, \"source\") shape error, got {other:?}"),
6823 }
6824 }
6825
6826 #[test]
6827 fn shape_static_asset_accepts_either_mode() {
6828 use crate::validate::shape_static_asset;
6829
6830 let legacy = AssetEntry {
6831 filename: "a.bin".into(),
6832 source: Some("sources/a.bin".into()),
6833 derive: None,
6834 blake3: BlakeHash(HASH_64.into()),
6835 };
6836 let derived = AssetEntry {
6837 filename: "b.bin".into(),
6838 source: None,
6839 derive: Some(AssetDerive {
6840 fetch: FetchSource {
6841 url: "https://example.invalid/b".into(),
6842 blake3: BlakeHash(HASH_64.into()),
6843 license: License::Apache2,
6844 },
6845 transform: None,
6846 lock: None,
6847 }),
6848 blake3: BlakeHash(HASH_64.into()),
6849 };
6850 let w = StaticAssetWorkload {
6851 assets: vec![legacy, derived],
6852 aliases: BTreeMap::new(),
6853 };
6854 shape_static_asset(&w).expect("both modes accepted");
6855 }
6856
6857 #[test]
6858 fn image_ref_string_form_rejects_bare_tag() {
6859 let res: Result<ImageRef, _> =
6860 serde_json::from_value(serde_json::Value::String("node:20".into()));
6861 let err = res.expect_err("bare-tag must reject");
6862 let msg = format!("{err}");
6863 assert!(msg.contains("digest"), "error should mention digest: {msg}");
6864 }
6865
6866 #[test]
6867 fn image_ref_string_form_accepts_digest_pinned() {
6868 let pinned = format!("node:20@sha256:{HASH_64}");
6869 let img: ImageRef =
6870 serde_json::from_value(serde_json::Value::String(pinned.clone())).expect("parse");
6871 assert_eq!(img.registry, "docker.io");
6872 assert_eq!(img.repository, "library/node");
6873 assert_eq!(img.tag, "20");
6874 assert_eq!(img.digest, format!("sha256:{HASH_64}"));
6875 }
6876
6877 #[test]
6878 fn image_ref_string_form_accepts_ghcr_with_pin() {
6879 let pinned = format!("ghcr.io/foo/bar:v1.7.4@sha256:{HASH_64}");
6880 let img: ImageRef =
6881 serde_json::from_value(serde_json::Value::String(pinned)).expect("parse");
6882 assert_eq!(img.registry, "ghcr.io");
6883 assert_eq!(img.repository, "foo/bar");
6884 assert_eq!(img.tag, "v1.7.4");
6885 assert!(img.digest.starts_with("sha256:"));
6886 }
6887
6888 #[test]
6889 fn image_ref_string_form_rejects_non_sha256_digest() {
6890 for bad in [
6891 "node:20@md5:abcdef",
6892 "node:20@sha1:abcdef",
6893 "node:20@sha256:",
6894 "node:20@sha256:zzznothex",
6895 ] {
6896 let res: Result<ImageRef, _> =
6897 serde_json::from_value(serde_json::Value::String(bad.into()));
6898 assert!(res.is_err(), "expected reject for {bad:?}");
6899 }
6900 }
6901
6902 #[test]
6903 fn image_ref_struct_form_rejects_missing_digest() {
6904 // Digest is now structurally required (R438-T3). Struct-form payloads
6905 // without `digest` must fail at serde-deserialize.
6906 let v = serde_json::json!({
6907 "registry": "ghcr.io",
6908 "repository": "noisetable/api",
6909 "tag": "v1.4.2",
6910 });
6911 let res: Result<ImageRef, _> = serde_json::from_value(v);
6912 assert!(res.is_err(), "missing digest must reject");
6913 }
6914
6915 #[test]
6916 fn image_ref_struct_form_round_trips_through_toml() {
6917 let img = ImageRef {
6918 registry: "ghcr.io".into(),
6919 repository: "ggerganov/whisper.cpp".into(),
6920 tag: "v1.7.4".into(),
6921 digest: format!("sha256:{HASH_64}"),
6922 };
6923 let toml_doc = toml::to_string(&img).expect("serialize");
6924 let back: ImageRef = toml::from_str(&toml_doc).expect("re-parse");
6925 assert_eq!(img, back);
6926 }
6927
6928 /// R546-B7: assert the shape real files use. This test previously fed the
6929 /// EXTERNALLY-tagged wrapping-table form (`[static-asset]` +
6930 /// `[[static-asset.asset]]`), which no on-disk `workload.toml` has ever
6931 /// used — so it stayed green while `yah cloud apply` was broken for every
6932 /// static-asset component. The flat `kind = "..."` form below is what every
6933 /// workload.toml in the workspace is written in.
6934 #[test]
6935 fn workload_envelope_dispatches_static_asset() {
6936 let src = format!(
6937 r#"
6938kind = "static-asset"
6939
6940[[asset]]
6941filename = "foo/bar.bin"
6942source = "sources/bar.bin"
6943blake3 = "{HASH_64}"
6944"#
6945 );
6946 let w: Workload = toml::from_str(&src).expect("parse");
6947 assert!(matches!(w, Workload::StaticAsset(_)));
6948 }
6949
6950 /// R896-T4 deleted `SchemaVersion`. Manifests written before that — any
6951 /// camp that has not re-synced, e.g. `~/ss/noisetable`'s workload tomls —
6952 /// still carry the key in both spellings it used to accept, and must load
6953 /// with it ignored rather than refused.
6954 #[test]
6955 fn a_legacy_schema_version_key_is_ignored() {
6956 for line in ["schema_version = 1", r#"schema_version = "V1""#] {
6957 let src = format!(
6958 r#"
6959{line}
6960kind = "static-asset"
6961
6962[[asset]]
6963filename = "foo/bar.bin"
6964source = "sources/bar.bin"
6965blake3 = "{HASH_64}"
6966"#
6967 );
6968 let w: Workload = toml::from_str(&src).unwrap_or_else(|e| panic!("{line}: {e}"));
6969 assert!(matches!(w, Workload::StaticAsset(_)));
6970 }
6971 }
6972
6973 /// R546-B7: the format branch, both directions. Human-readable formats get
6974 /// the flat `kind`-tagged shape; postcard keeps the externally-tagged
6975 /// variant-index encoding the kamaji UDS depends on (R590-B3). Regressing
6976 /// either side breaks a different half of the system, so pin both.
6977 #[test]
6978 fn workload_envelope_is_tagged_in_toml_and_external_in_postcard() {
6979 let src = format!(
6980 r#"
6981kind = "static-asset"
6982
6983[[asset]]
6984filename = "foo/bar.bin"
6985source = "sources/bar.bin"
6986blake3 = "{HASH_64}"
6987"#
6988 );
6989 let w: Workload = toml::from_str(&src).expect("parse flat TOML");
6990
6991 // Human-readable round-trips stay flat — no wrapping table.
6992 let json = serde_json::to_string(&w).expect("serialize json");
6993 assert!(json.contains("\"kind\":\"static-asset\""), "got {json}");
6994 assert!(
6995 !json.contains("{\"static-asset\":"),
6996 "human-readable output must not be externally tagged: {json}"
6997 );
6998 assert_eq!(
6999 serde_json::from_str::<Workload>(&json).expect("re-parse json"),
7000 w
7001 );
7002
7003 // postcard is non-self-describing: it can only round-trip because the
7004 // binary branch never asks for deserialize_any.
7005 let bytes = postcard::to_allocvec(&w).expect("postcard encode");
7006 assert_eq!(
7007 postcard::from_bytes::<Workload>(&bytes).expect("postcard decode"),
7008 w
7009 );
7010 }
7011
7012 // ── R572-F1: lifecycle archetype discriminator ─────────────────────────
7013
7014 fn archetype_test_spec(name: &str) -> WorkloadSpec {
7015 WorkloadSpec::for_forge(
7016 name,
7017 ImageRef {
7018 registry: "ghcr.io".into(),
7019 repository: "yah/test".into(),
7020 tag: "latest".into(),
7021 digest: testing::test_digest(),
7022 },
7023 TierTag("infra".into()),
7024 vec![],
7025 )
7026 }
7027
7028 #[test]
7029 fn explicit_archetype_round_trips_through_json_and_wins_over_inference() {
7030 for archetype in [
7031 LifecycleArchetype::Server,
7032 LifecycleArchetype::Appliance,
7033 LifecycleArchetype::Job,
7034 ] {
7035 let mut spec = archetype_test_spec("explicit");
7036 // Volumes present + restart_policy Always would infer Appliance
7037 // (see effective_archetype_infers_* below) — deliberately
7038 // mismatched against every archetype under test so the
7039 // assertion actually proves the explicit field wins, not that
7040 // it happens to agree with inference.
7041 spec.volumes = vec![VolumeMount {
7042 source: VolumeSource::Named { name: "data".into() },
7043 target: PathBuf::from("/data"),
7044 read_only: false,
7045 from_secret_mount: false,
7046 }];
7047 spec.restart_policy = RestartPolicy::Always;
7048 spec.archetype = Some(archetype);
7049
7050 let json = serde_json::to_string(&spec).expect("serialize");
7051 assert!(
7052 json.contains("\"archetype\""),
7053 "explicit archetype must be present on the wire"
7054 );
7055 let back: WorkloadSpec = serde_json::from_str(&json).expect("deserialize");
7056 assert_eq!(spec, back, "spec did not survive JSON round-trip");
7057 assert_eq!(back.archetype, Some(archetype));
7058 assert_eq!(
7059 back.effective_archetype(),
7060 archetype,
7061 "explicit archetype must win over the volumes/restart_policy inference"
7062 );
7063 }
7064 }
7065
7066 #[test]
7067 fn archetype_serializes_as_null_when_none() {
7068 let mut spec = archetype_test_spec("omitted");
7069 spec.archetype = None;
7070 let json = serde_json::to_value(&spec).expect("to_value");
7071 // Postcard-native (R590-B3): no `skip_serializing_if` anywhere on the
7072 // graph, so every field is always on the wire — a None Option is an
7073 // explicit `null`, not an absent key. The binary UDS wire is positional
7074 // and requires the slot to be present.
7075 assert_eq!(json.get("archetype"), Some(&serde_json::Value::Null));
7076 }
7077
7078 #[test]
7079 fn spec_without_archetype_field_deserializes_to_none() {
7080 // Simulates an on-disk spec written before R572-F1: no `archetype`
7081 // key at all. Omitting the key must still parse to None (the additive-
7082 // default contract) even though we now always *emit* the field.
7083 let mut spec = archetype_test_spec("pre-existing");
7084 spec.archetype = None;
7085 let mut json = serde_json::to_value(&spec).expect("to_value");
7086 json.as_object_mut().unwrap().remove("archetype");
7087 let back: WorkloadSpec = serde_json::from_value(json).expect("deserialize");
7088 assert_eq!(back.archetype, None);
7089 }
7090
7091 #[test]
7092 fn effective_archetype_infers_appliance_from_volumes_when_field_absent() {
7093 // Pre-R572 behavior: a workload with a volume was understood (by
7094 // convention, never a type) to be stateful/pinned. Confirm that
7095 // meaning is preserved bit-for-bit through effective_archetype().
7096 let mut spec = archetype_test_spec("appliance-inferred");
7097 spec.volumes = vec![VolumeMount {
7098 source: VolumeSource::Named { name: "pgdata".into() },
7099 target: PathBuf::from("/var/lib/postgresql/data"),
7100 read_only: false,
7101 from_secret_mount: false,
7102 }];
7103 spec.restart_policy = RestartPolicy::Always;
7104 spec.archetype = None;
7105 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Appliance);
7106 }
7107
7108 #[test]
7109 fn effective_archetype_infers_job_from_restart_never_when_field_absent() {
7110 // Pre-R572 behavior: RestartPolicy::Never + no volumes is the forge
7111 // run-once convention (see RestartPolicy::Never's own doc comment) —
7112 // structurally a job. WorkloadSpec::for_forge already produces
7113 // exactly this shape; isolate the pure-inference path by clearing
7114 // the explicit archetype for_forge now sets.
7115 let mut spec = archetype_test_spec("job-inferred");
7116 assert!(spec.volumes.is_empty());
7117 assert!(matches!(spec.restart_policy, RestartPolicy::Never));
7118 spec.archetype = None;
7119 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Job);
7120 }
7121
7122 #[test]
7123 fn effective_archetype_defaults_to_server_as_the_common_case_when_field_absent() {
7124 // Pre-R572 behavior: no volumes + a restartable policy (the common
7125 // stateless-web-server shape) inferred as movable/fungible.
7126 let mut spec = archetype_test_spec("server-inferred");
7127 spec.restart_policy = RestartPolicy::Always;
7128 spec.archetype = None;
7129 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Server);
7130 }
7131
7132 // ── R860-T1 / W338: requirement vocabulary ──────────────────────────────
7133
7134 /// An ordinary `anywhere` + `wait` requirement — what a `depends_on` entry
7135 /// has always meant, written the long way.
7136 fn wait_requirement(ident: &str) -> Requirement {
7137 Requirement {
7138 ident: MeshIdent(ident.into()),
7139 locality: Locality::Anywhere,
7140 supply: Supply::Wait,
7141 provides: None,
7142 }
7143 }
7144
7145 /// A `local` + `self` requirement carrying its provider — the sidecar
7146 /// shape, W338's motivating case. `ident` must be the provider's own mesh
7147 /// identity, which `archetype_test_spec` spells `forge.<name>`.
7148 fn self_requirement(provider_name: &str) -> Requirement {
7149 let provider = archetype_test_spec(provider_name);
7150 Requirement {
7151 ident: provider.expose.mesh.identity.clone(),
7152 locality: Locality::Local,
7153 supply: Supply::SelfProvision,
7154 provides: Some(Box::new(provider)),
7155 }
7156 }
7157
7158 #[test]
7159 fn locality_and_supply_use_the_wire_spellings_the_design_names() {
7160 // The TOML in W338 is written against these strings; a rename here is a
7161 // silent break of every manifest on disk. `self` in particular cannot
7162 // be the variant name (Rust keyword), so it is a serde rename and needs
7163 // guarding rather than trusting rename_all.
7164 assert_eq!(
7165 serde_json::to_string(&Locality::Anywhere).unwrap(),
7166 "\"anywhere\""
7167 );
7168 assert_eq!(
7169 serde_json::to_string(&Locality::PreferLocal).unwrap(),
7170 "\"prefer-local\""
7171 );
7172 assert_eq!(serde_json::to_string(&Locality::Local).unwrap(), "\"local\"");
7173 assert_eq!(serde_json::to_string(&Supply::Wait).unwrap(), "\"wait\"");
7174 assert_eq!(
7175 serde_json::to_string(&Supply::SelfProvision).unwrap(),
7176 "\"self\""
7177 );
7178
7179 assert_eq!(
7180 serde_json::from_str::<Supply>("\"self\"").unwrap(),
7181 Supply::SelfProvision
7182 );
7183 assert_eq!(
7184 serde_json::from_str::<Locality>("\"prefer-local\"").unwrap(),
7185 Locality::PreferLocal
7186 );
7187 }
7188
7189 #[test]
7190 fn a_requirement_omitting_locality_and_supply_defaults_to_the_depends_on_meaning() {
7191 // Folding `depends_on` into `requires` must not change any existing
7192 // spec's meaning, which is only true if the defaults are exactly the
7193 // old behaviour.
7194 let req: Requirement =
7195 serde_json::from_str(r#"{"ident":"headscale-db"}"#).expect("bare ident must parse");
7196 assert_eq!(req.locality, Locality::Anywhere);
7197 assert_eq!(req.supply, Supply::Wait);
7198 assert_eq!(req.provides, None);
7199 }
7200
7201 #[test]
7202 fn a_self_provisioned_requirement_round_trips_its_nested_provider_spec() {
7203 // `provides` makes WorkloadSpec recursive. Confirm the box survives a
7204 // JSON round trip rather than trusting the derive.
7205 let mut spec = archetype_test_spec("headscale");
7206 spec.requires = vec![self_requirement("replicator")];
7207
7208 let json = serde_json::to_string(&spec).expect("serialize");
7209 let back: WorkloadSpec = serde_json::from_str(&json).expect("deserialize");
7210 assert_eq!(back, spec);
7211
7212 let provided = back.requires[0]
7213 .provides
7214 .as_ref()
7215 .expect("the nested provider spec must survive the round trip");
7216 assert_eq!(provided.expose.mesh.identity, back.requires[0].ident);
7217 }
7218
7219 #[test]
7220 fn effective_requirements_returns_requires_verbatim_when_depends_on_is_empty() {
7221 let mut spec = archetype_test_spec("requires-only");
7222 spec.depends_on = vec![];
7223 spec.requires = vec![
7224 Requirement {
7225 ident: MeshIdent("headscale-db".into()),
7226 locality: Locality::PreferLocal,
7227 supply: Supply::Wait,
7228 provides: None,
7229 },
7230 self_requirement("replicator"),
7231 ];
7232
7233 assert_eq!(spec.effective_requirements(), spec.requires);
7234 }
7235
7236 #[test]
7237 fn effective_requirements_folds_depends_on_into_anywhere_wait() {
7238 // The back-compat projection: a pre-R860 spec carries everything in
7239 // `depends_on`, and reading `requires` alone would call it
7240 // requirement-free.
7241 let mut spec = archetype_test_spec("depends-on-only");
7242 spec.depends_on = vec![MeshIdent("noisetable-db".into()), MeshIdent("redis".into())];
7243 spec.requires = vec![];
7244
7245 assert_eq!(
7246 spec.effective_requirements(),
7247 vec![
7248 wait_requirement("noisetable-db"),
7249 wait_requirement("redis"),
7250 ]
7251 );
7252 }
7253
7254 #[test]
7255 fn effective_requirements_dedups_by_ident_and_requires_wins() {
7256 // An ident in both fields is the author restating one dependency with a
7257 // locality, not two edges — so the richer entry survives and the folded
7258 // `depends_on` projection is dropped, order following `requires` first.
7259 let mut spec = archetype_test_spec("overlap");
7260 spec.depends_on = vec![
7261 MeshIdent("headscale-db".into()),
7262 MeshIdent("only-in-depends-on".into()),
7263 ];
7264 spec.requires = vec![Requirement {
7265 ident: MeshIdent("headscale-db".into()),
7266 locality: Locality::Local,
7267 supply: Supply::Wait,
7268 provides: None,
7269 }];
7270
7271 let effective = spec.effective_requirements();
7272 assert_eq!(
7273 effective,
7274 vec![
7275 Requirement {
7276 ident: MeshIdent("headscale-db".into()),
7277 locality: Locality::Local,
7278 supply: Supply::Wait,
7279 provides: None,
7280 },
7281 wait_requirement("only-in-depends-on"),
7282 ],
7283 "the `requires` entry must win and the ident must appear exactly once"
7284 );
7285 }
7286
7287 #[test]
7288 fn effective_requirements_is_empty_when_neither_field_is_set() {
7289 let mut spec = archetype_test_spec("neither");
7290 spec.depends_on = vec![];
7291 spec.requires = vec![];
7292 assert!(spec.effective_requirements().is_empty());
7293 }
7294
7295 // ── R860-T1: `requires` shape validation ────────────────────────────────
7296
7297 /// Assert `shape` rejects `spec` with an error naming `requires[0]` and
7298 /// mentioning `needle`, so a failure points at the rule that fired.
7299 fn assert_requires_rejected(spec: &WorkloadSpec, needle: &str) {
7300 let err = validate::shape(spec).expect_err("shape must reject this spec");
7301 let rendered = err.to_string();
7302 assert!(
7303 rendered.contains("requires[0]"),
7304 "error must name the offending requirement; got: {rendered}"
7305 );
7306 assert!(
7307 rendered.contains(needle),
7308 "error must explain the rule ({needle:?}); got: {rendered}"
7309 );
7310 }
7311
7312 #[test]
7313 fn a_valid_requires_list_passes_shape_validation() {
7314 let mut spec = archetype_test_spec("valid-requires");
7315 spec.requires = vec![
7316 wait_requirement("headscale-db"),
7317 self_requirement("replicator"),
7318 ];
7319 validate::shape(&spec).expect("a well-formed requires list must pass");
7320 }
7321
7322 #[test]
7323 fn self_supply_without_a_provides_spec_is_rejected() {
7324 let mut spec = archetype_test_spec("self-without-provides");
7325 spec.requires = vec![Requirement {
7326 ident: MeshIdent("replicator".into()),
7327 locality: Locality::Local,
7328 supply: Supply::SelfProvision,
7329 provides: None,
7330 }];
7331 assert_requires_rejected(&spec, "no `provides` spec");
7332 }
7333
7334 #[test]
7335 fn wait_supply_carrying_a_provides_spec_is_rejected() {
7336 // The other direction matters just as much: a spec attached to a
7337 // `wait` requirement has no owner, so nothing would ever deploy it and
7338 // the author's intent is silently lost.
7339 let mut spec = archetype_test_spec("wait-with-provides");
7340 let mut req = self_requirement("replicator");
7341 req.supply = Supply::Wait;
7342 spec.requires = vec![req];
7343 assert_requires_rejected(&spec, "would have no owner");
7344 }
7345
7346 #[test]
7347 fn a_provides_spec_naming_a_different_identity_is_rejected() {
7348 // Each member keeps its own mesh identity (W338), and it has to be the
7349 // identity the edge points at — otherwise the provider is not the thing
7350 // the requirer asked for and is not discoverable as it.
7351 let mut spec = archetype_test_spec("identity-mismatch");
7352 let mut req = self_requirement("replicator");
7353 req.ident = MeshIdent("something-else".into());
7354 spec.requires = vec![req];
7355 assert_requires_rejected(&spec, "the provider keeps its own mesh identity");
7356 }
7357
7358 #[test]
7359 fn a_provides_spec_that_itself_self_provisions_is_rejected() {
7360 // The depth bound. Without it, `provides` is an arbitrarily deep tree
7361 // that placement would have to flatten before scheduling anything.
7362 let mut spec = archetype_test_spec("too-deep");
7363 let mut req = self_requirement("replicator");
7364 req.provides
7365 .as_mut()
7366 .expect("self_requirement always carries a provider")
7367 .requires = vec![self_requirement("replicator-of-the-replicator")];
7368 spec.requires = vec![req];
7369 assert_requires_rejected(&spec, "bounded at one");
7370 }
7371
7372 #[test]
7373 fn a_provides_spec_that_only_waits_is_accepted_at_depth_one() {
7374 // The bound is on `self` supply, not on nesting a `requires` list at
7375 // all — a provider may still name things it does not deploy.
7376 let mut spec = archetype_test_spec("nested-wait-ok");
7377 let mut req = self_requirement("replicator");
7378 req.provides
7379 .as_mut()
7380 .expect("self_requirement always carries a provider")
7381 .requires = vec![wait_requirement("object-storage")];
7382 spec.requires = vec![req];
7383 validate::shape(&spec).expect("a nested `wait` requirement is within the depth bound");
7384 }
7385
7386 #[test]
7387 fn a_repeated_requirement_ident_is_rejected() {
7388 let mut spec = archetype_test_spec("repeated-ident");
7389 spec.requires = vec![
7390 wait_requirement("headscale-db"),
7391 Requirement {
7392 ident: MeshIdent("headscale-db".into()),
7393 locality: Locality::Local,
7394 supply: Supply::Wait,
7395 provides: None,
7396 },
7397 ];
7398 let err = validate::shape(&spec)
7399 .expect_err("a duplicate ident must be rejected")
7400 .to_string();
7401 assert!(err.contains("requires[1]"), "got: {err}");
7402 assert!(err.contains("declared twice"), "got: {err}");
7403 }
7404
7405 #[test]
7406 fn a_requirement_naming_the_spec_itself_is_rejected() {
7407 let mut spec = archetype_test_spec("self-naming");
7408 spec.requires = vec![wait_requirement(&spec.expose.mesh.identity.0.clone())];
7409 assert_requires_rejected(&spec, "cannot be its own provider");
7410 }
7411
7412 // ── R594-F2: public-ingress appliance (container-shaped, not a new
7413 // Workload variant — see Workload::Container's doc comment) ───────────
7414
7415 #[test]
7416 fn ingress_marked_spec_is_appliance_and_carries_public_ip_placement_requirement() {
7417 let mut spec = archetype_test_spec("public-ingress");
7418 spec.archetype = Some(LifecycleArchetype::Appliance);
7419 spec.annotations.insert(
7420 REQUIRES_TAINT_ANNOTATION.to_string(),
7421 PUBLIC_IP_TAINT.to_string(),
7422 );
7423
7424 assert_eq!(
7425 spec.effective_archetype(),
7426 LifecycleArchetype::Appliance,
7427 "ingress must be pinned-per-node/non-drainable, the R572 appliance sense"
7428 );
7429 assert_eq!(
7430 spec.requires_taint(),
7431 Some(PUBLIC_IP_TAINT),
7432 "ingress must declare it can only land on a public-ip-tainted node"
7433 );
7434
7435 // No taint exists to match against yet (R572-F3) and nothing
7436 // enforces placement yet (R572-F5) — confirm this ticket stays
7437 // declarative-only by checking a spec with no requirement stays
7438 // unaffected.
7439 let unrelated = archetype_test_spec("unrelated");
7440 assert_eq!(unrelated.requires_taint(), None);
7441 }
7442
7443 #[test]
7444 fn ingress_marked_spec_round_trips_through_json_as_a_container_workload() {
7445 // Mirrors the on-disk envelope: the externally-tagged `container`
7446 // variant wrapping the WorkloadSpec, exactly like every other
7447 // container-shaped workload. No new Workload variant, no new
7448 // discriminator.
7449 let mut inner = archetype_test_spec("public-ingress");
7450 inner.archetype = Some(LifecycleArchetype::Appliance);
7451 inner.annotations.insert(
7452 REQUIRES_TAINT_ANNOTATION.to_string(),
7453 PUBLIC_IP_TAINT.to_string(),
7454 );
7455 let workload = Workload::container(inner.clone());
7456
7457 let json = serde_json::to_string(&workload).expect("serialize");
7458 assert!(json.contains("\"container\""));
7459 assert!(json.contains(REQUIRES_TAINT_ANNOTATION));
7460 assert!(json.contains(PUBLIC_IP_TAINT));
7461
7462 let back: Workload = serde_json::from_str(&json).expect("deserialize");
7463 match back.container_spec() {
7464 Some(spec) => {
7465 assert_eq!(spec, &inner);
7466 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Appliance);
7467 assert_eq!(spec.requires_taint(), Some(PUBLIC_IP_TAINT));
7468 }
7469 None => panic!("expected a container reference workload, got {back:?}"),
7470 }
7471 }
7472
7473 // ── Nested-sandbox grant (R636-B2) ──────────────────────────────────────
7474
7475 #[test]
7476 fn nested_sandbox_marker_is_opt_in_and_reads_back() {
7477 // The half that matters: no workload gets the grant by default, so
7478 // adding the marker cannot widen anything already deployed.
7479 let plain = archetype_test_spec("ordinary-build");
7480 assert!(!plain.wants_nested_sandbox());
7481
7482 let mut buildkit = archetype_test_spec("build-image");
7483 buildkit.annotations.insert(
7484 NESTED_SANDBOX_ANNOTATION.to_string(),
7485 NESTED_SANDBOX_VALUE.to_string(),
7486 );
7487 assert!(buildkit.wants_nested_sandbox());
7488
7489 // Fails closed on any other value, same strictness as
7490 // `wants_host_network` — a typo must not hand out CAP_SETUID.
7491 let mut typo = archetype_test_spec("typo");
7492 typo.annotations
7493 .insert(NESTED_SANDBOX_ANNOTATION.to_string(), "Nested".to_string());
7494 assert!(!typo.wants_nested_sandbox());
7495 }
7496
7497 /// The three markers are independent axes: asking for host networking or
7498 /// native exec must not imply the capability grant, and vice versa.
7499 #[test]
7500 fn nested_sandbox_marker_is_independent_of_the_other_markers() {
7501 let mut host_net = archetype_test_spec("host-net");
7502 host_net.annotations.insert(
7503 HOST_NETWORK_ANNOTATION.to_string(),
7504 HOST_NETWORK_VALUE.to_string(),
7505 );
7506 assert!(host_net.wants_host_network());
7507 assert!(!host_net.wants_nested_sandbox());
7508
7509 let mut nested = archetype_test_spec("nested");
7510 nested.annotations.insert(
7511 NESTED_SANDBOX_ANNOTATION.to_string(),
7512 NESTED_SANDBOX_VALUE.to_string(),
7513 );
7514 assert!(nested.wants_nested_sandbox());
7515 assert!(!nested.wants_host_network());
7516 assert!(!nested.wants_native_exec());
7517 }
7518
7519 // ── Native exec marker (R577-T1 / W254) ─────────────────────────────────
7520
7521 #[test]
7522 fn native_exec_marker_is_opt_in_and_reads_back() {
7523 // Default: every forge workload is a container workload. This is the
7524 // half that matters most — the marker must not silently reroute the
7525 // Linux offload leg proven live on us-west-002.
7526 let plain = archetype_test_spec("linux-build");
7527 assert!(!plain.wants_native_exec());
7528
7529 let mut native = archetype_test_spec("darwin-build");
7530 native.annotations.insert(
7531 NATIVE_EXEC_ANNOTATION.to_string(),
7532 NATIVE_EXEC_VALUE.to_string(),
7533 );
7534 assert!(native.wants_native_exec());
7535
7536 // Any other value is not the opt-in — same strictness as
7537 // `wants_host_network`, so a typo fails closed onto the container
7538 // backend rather than escaping the sandbox.
7539 let mut typo = archetype_test_spec("typo");
7540 typo.annotations
7541 .insert(NATIVE_EXEC_ANNOTATION.to_string(), "Native".to_string());
7542 assert!(!typo.wants_native_exec());
7543 }
7544
7545 #[test]
7546 fn native_marked_spec_round_trips_through_json_as_a_container_workload() {
7547 // The point of the annotation shape: a native workload is still a
7548 // `Workload::Container` on the wire, so kamaji-proto's codec, yubaba
7549 // admission and the mesh-assignment path need no new variant.
7550 let mut inner = archetype_test_spec("darwin-build");
7551 inner.annotations.insert(
7552 NATIVE_EXEC_ANNOTATION.to_string(),
7553 NATIVE_EXEC_VALUE.to_string(),
7554 );
7555 let workload = Workload::container(inner.clone());
7556
7557 let json = serde_json::to_string(&workload).expect("serialize");
7558 assert!(json.contains(NATIVE_EXEC_ANNOTATION));
7559
7560 let back: Workload = serde_json::from_str(&json).expect("deserialize");
7561 match back.container_spec() {
7562 Some(spec) => {
7563 assert_eq!(spec, &inner);
7564 assert!(spec.wants_native_exec());
7565 }
7566 None => panic!("expected a container reference workload, got {back:?}"),
7567 }
7568 }
7569
7570 // ── MicroVM marker (R605-F8 / W325 §5) ──────────────────────────────────
7571
7572 #[test]
7573 fn microvm_marker_is_opt_in_and_reads_back() {
7574 let plain = archetype_test_spec("linux-build");
7575 assert!(!plain.wants_microvm());
7576
7577 let mut vm = archetype_test_spec("isolated-build");
7578 vm.annotations.insert(
7579 NATIVE_EXEC_ANNOTATION.to_string(),
7580 MICROVM_EXEC_VALUE.to_string(),
7581 );
7582 assert!(vm.wants_microvm());
7583
7584 // Fails closed onto the container backend, like every other marker: a
7585 // typo must not be read as "boot a VM", because the deploy that would
7586 // then be refused for lack of a microVM backend is a *worse* failure
7587 // than the container run the author actually spelled.
7588 let mut typo = archetype_test_spec("typo");
7589 typo.annotations
7590 .insert(NATIVE_EXEC_ANNOTATION.to_string(), "MicroVM".to_string());
7591 assert!(!typo.wants_microvm());
7592 assert!(!typo.wants_native_exec());
7593 }
7594
7595 #[test]
7596 fn exec_substrate_markers_are_mutually_exclusive_by_construction() {
7597 // This is the property that buys R605-F8 out of a refusal branch: the
7598 // three substrates share one annotation key, so no spec can ask for two
7599 // of them. Pinned because a later "let's give microVM its own key"
7600 // refactor would silently re-open the incoherent-pair case that
7601 // `yah.sandbox` + `yah.exec = native` still has to be refused for.
7602 assert_eq!(
7603 NATIVE_EXEC_ANNOTATION, NATIVE_EXEC_ANNOTATION,
7604 "both substrate values must live on the same key"
7605 );
7606 assert_ne!(NATIVE_EXEC_VALUE, MICROVM_EXEC_VALUE);
7607
7608 for value in [NATIVE_EXEC_VALUE, MICROVM_EXEC_VALUE, "", "container"] {
7609 let mut spec = archetype_test_spec("substrate");
7610 spec.annotations
7611 .insert(NATIVE_EXEC_ANNOTATION.to_string(), value.to_string());
7612 assert!(
7613 !(spec.wants_native_exec() && spec.wants_microvm()),
7614 "yah.exec={value:?} selected two substrates at once"
7615 );
7616 }
7617 }
7618
7619 // ─── R894-F1: trust as a declared axis ───────────────────────────────────
7620
7621 /// The security ordering is the derived `Ord`, so it has to be pinned
7622 /// explicitly: a reorder of the variants would silently invert the whole
7623 /// trust check in `cloud::config::check_trust_substrate` while compiling
7624 /// cleanly.
7625 #[test]
7626 fn substrates_are_ordered_by_the_isolation_they_provide() {
7627 assert!(ExecSubstrate::Native < ExecSubstrate::Container);
7628 assert!(ExecSubstrate::Container < ExecSubstrate::MicroVm);
7629
7630 assert_eq!(
7631 TrustLevel::Trusted.minimum_substrate(),
7632 ExecSubstrate::Native
7633 );
7634 assert_eq!(
7635 TrustLevel::Untrusted.minimum_substrate(),
7636 ExecSubstrate::MicroVm
7637 );
7638 // Trusted's floor is the bottom of the ordering — i.e. no constraint,
7639 // which is what every workload in the fleet has today.
7640 for s in [
7641 ExecSubstrate::Native,
7642 ExecSubstrate::Container,
7643 ExecSubstrate::MicroVm,
7644 ] {
7645 assert!(s >= TrustLevel::Trusted.minimum_substrate());
7646 }
7647 }
7648
7649 /// `exec_substrate` must agree with the two booleans it replaces on every
7650 /// value, including the unrecognised one — that equivalence is what lets
7651 /// `GrantRuntime::of_spec` delegate to it.
7652 #[test]
7653 fn exec_substrate_agrees_with_the_two_predicates_it_replaces() {
7654 for (value, expected) in [
7655 (None, ExecSubstrate::Container),
7656 (Some(NATIVE_EXEC_VALUE), ExecSubstrate::Native),
7657 (Some(MICROVM_EXEC_VALUE), ExecSubstrate::MicroVm),
7658 // A typo fails CLOSED: it reads as a container, which an untrusted
7659 // spec is refused for, rather than as the microVM it meant.
7660 (Some("micro-vm"), ExecSubstrate::Container),
7661 (Some(""), ExecSubstrate::Container),
7662 ] {
7663 let mut spec = archetype_test_spec("substrate");
7664 if let Some(v) = value {
7665 spec.annotations
7666 .insert(NATIVE_EXEC_ANNOTATION.to_string(), v.to_string());
7667 }
7668 assert_eq!(spec.exec_substrate(), expected, "yah.exec={value:?}");
7669 assert_eq!(
7670 spec.wants_native_exec(),
7671 expected == ExecSubstrate::Native,
7672 "yah.exec={value:?}"
7673 );
7674 assert_eq!(
7675 spec.wants_microvm(),
7676 expected == ExecSubstrate::MicroVm,
7677 "yah.exec={value:?}"
7678 );
7679 }
7680 }
7681
7682 /// Absent means trusted (the fleet's population today); an unrecognised
7683 /// value is an error rather than a silent fallback to either side.
7684 #[test]
7685 fn trust_defaults_to_trusted_and_refuses_anything_it_cannot_read() {
7686 let spec = archetype_test_spec("trust");
7687 assert_eq!(spec.trust().unwrap(), TrustLevel::Trusted);
7688
7689 for (value, expected) in [
7690 (TRUST_TRUSTED_VALUE, TrustLevel::Trusted),
7691 (TRUST_UNTRUSTED_VALUE, TrustLevel::Untrusted),
7692 // Whitespace around the value is trimmed, not refused.
7693 (" untrusted ", TrustLevel::Untrusted),
7694 ] {
7695 let mut spec = archetype_test_spec("trust");
7696 spec.annotations
7697 .insert(TRUST_ANNOTATION.to_string(), value.to_string());
7698 assert_eq!(spec.trust().unwrap(), expected, "yah.trust={value:?}");
7699 }
7700
7701 for bad in ["untrused", "UNTRUSTED", "yes", ""] {
7702 let mut spec = archetype_test_spec("trust");
7703 spec.annotations
7704 .insert(TRUST_ANNOTATION.to_string(), bad.to_string());
7705 let err = spec.trust().unwrap_err();
7706 assert_eq!(err, TrustDeclError::UnknownLevel(bad.trim().to_string()));
7707 assert!(
7708 err.to_string().contains(bad.trim()),
7709 "the error must quote the unreadable value"
7710 );
7711 }
7712 }
7713
7714 /// The choke-point verb stamps *and* raises, is idempotent, and never
7715 /// lowers a substrate a caller already asked for.
7716 #[test]
7717 fn stamp_untrusted_raises_the_substrate_and_never_lowers_it() {
7718 // No marker (container) → raised to microvm.
7719 let mut spec = archetype_test_spec("tenant");
7720 spec.stamp_untrusted();
7721 assert_eq!(spec.trust().unwrap(), TrustLevel::Untrusted);
7722 assert_eq!(spec.exec_substrate(), ExecSubstrate::MicroVm);
7723
7724 // Idempotent: stamping twice changes nothing.
7725 let once = spec.annotations.clone();
7726 spec.stamp_untrusted();
7727 assert_eq!(spec.annotations, once);
7728
7729 // `native` is a WIDENING when raised — the safe direction — and it
7730 // happens here rather than being left for admission to refuse.
7731 let mut native = archetype_test_spec("tenant");
7732 native.annotations.insert(
7733 NATIVE_EXEC_ANNOTATION.to_string(),
7734 NATIVE_EXEC_VALUE.to_string(),
7735 );
7736 native.stamp_untrusted();
7737 assert_eq!(native.exec_substrate(), ExecSubstrate::MicroVm);
7738
7739 // A caller that already asked for at least the floor keeps its request.
7740 let mut vm = archetype_test_spec("tenant");
7741 vm.annotations.insert(
7742 NATIVE_EXEC_ANNOTATION.to_string(),
7743 MICROVM_EXEC_VALUE.to_string(),
7744 );
7745 vm.stamp_untrusted();
7746 assert_eq!(vm.exec_substrate(), ExecSubstrate::MicroVm);
7747
7748 // And the post-condition the whole axis rests on: a stamped spec always
7749 // satisfies its own floor, so the choke point cannot emit a spec that
7750 // admission will refuse.
7751 for spec in [&spec, &native, &vm] {
7752 let trust = spec.trust().unwrap();
7753 assert!(spec.exec_substrate() >= trust.minimum_substrate());
7754 }
7755 }
7756
7757 #[test]
7758 fn microvm_marked_spec_round_trips_through_json_as_a_container_workload() {
7759 // Same zero-blast-radius claim as the native case: a microVM workload
7760 // is still `Workload::Container` on the wire, so kamaji-proto's codec
7761 // gains no variant and its positional postcard encoding does not move.
7762 let mut inner = archetype_test_spec("isolated-build");
7763 inner.annotations.insert(
7764 NATIVE_EXEC_ANNOTATION.to_string(),
7765 MICROVM_EXEC_VALUE.to_string(),
7766 );
7767 let workload = Workload::container(inner.clone());
7768
7769 let json = serde_json::to_string(&workload).expect("serialize");
7770 assert!(json.contains(MICROVM_EXEC_VALUE));
7771
7772 let back: Workload = serde_json::from_str(&json).expect("deserialize");
7773 match back.container_spec() {
7774 Some(spec) => {
7775 assert_eq!(spec, &inner);
7776 assert!(spec.wants_microvm());
7777 assert!(!spec.wants_native_exec());
7778 }
7779 None => panic!("expected a container reference workload, got {back:?}"),
7780 }
7781 }
7782
7783 // ── R885: resource ceilings ───────────────────────────────────────────────
7784
7785 /// R885-B5 / W344 Finding 5. The default has to be "no ceiling": every spec
7786 /// in the tree predates the ceiling, and reading the request as a ceiling
7787 /// is exactly the bug that throttled four live workloads to a quarter core
7788 /// apiece on us-east-001.
7789 #[test]
7790 fn a_spec_that_declares_no_cpu_ceiling_has_none() {
7791 let spec = archetype_test_spec("uncapped");
7792 assert!(spec.resources.cpu_millis > 0, "premise: it has a request");
7793 assert_eq!(spec.cpu_limit_millis(), None);
7794 }
7795
7796 #[test]
7797 fn a_declared_cpu_ceiling_is_read_independently_of_the_request() {
7798 let mut spec = archetype_test_spec("capped");
7799 spec.resources.cpu_limit_millis = Some(2000);
7800 assert_eq!(spec.cpu_limit_millis(), Some(2000));
7801 // Two independent numbers: the ceiling is not derived from the request,
7802 // and reading one must not move the other.
7803 assert_ne!(spec.cpu_limit_millis(), Some(spec.resources.cpu_millis));
7804 }
7805
7806 /// An explicit `0` means "no ceiling" rather than "cap it at nothing".
7807 /// Capping a workload at zero CPU is the worse failure of the two.
7808 #[test]
7809 fn a_zero_cpu_ceiling_is_no_ceiling() {
7810 let mut spec = archetype_test_spec("odd");
7811 spec.resources.cpu_limit_millis = Some(0);
7812 assert_eq!(spec.cpu_limit_millis(), None);
7813 }
7814
7815 /// R885-T2 / W344 Finding 3. Opposite default direction from the CPU
7816 /// ceiling above: absent must fall back to a real bound, not to "no
7817 /// ceiling" — an unbounded `pids.max` is exactly the fork-bomb bug this
7818 /// ticket exists to close.
7819 #[test]
7820 fn a_spec_that_declares_no_pids_limit_gets_the_default() {
7821 let spec = archetype_test_spec("uncapped-pids");
7822 assert_eq!(spec.pids_limit(), DEFAULT_PIDS_MAX);
7823 }
7824
7825 #[test]
7826 fn a_declared_pids_limit_overrides_the_default() {
7827 let mut spec = archetype_test_spec("capped-pids");
7828 spec.resources.pids_max = Some(256);
7829 assert_eq!(spec.pids_limit(), 256);
7830 }
7831
7832 /// Unlike the CPU ceiling, a `0` falls back to the default bound, not to
7833 /// "no ceiling".
7834 #[test]
7835 fn a_zero_pids_limit_falls_back_to_the_default() {
7836 let mut spec = archetype_test_spec("odd-pids");
7837 spec.resources.pids_max = Some(0);
7838 assert_eq!(spec.pids_limit(), DEFAULT_PIDS_MAX);
7839 }
7840
7841 // ── R896-F3: the retired annotations ─────────────────────────────────────
7842
7843 /// Every key the fields replaced maps to the field that replaced it, and
7844 /// the annotations that stayed annotations do not.
7845 #[test]
7846 fn every_retired_annotation_names_its_replacement_and_the_markers_are_not_retired() {
7847 for (key, field) in [
7848 ("yah.placement.memory-request-mb", "resources.memory_request_mb"),
7849 ("yah.limits.cpu-millis", "resources.cpu_limit_millis"),
7850 ("yah.limits.pids-max", "resources.pids_max"),
7851 ("yah.limits.scratch-floor-mb", "resources.scratch_floor_mb"),
7852 ("yah.durability.tier", "durability.tier"),
7853 ("yah.durability.rpo-seconds", "durability.rpo_seconds"),
7854 // A typo under a retired prefix is still an attempt at the family.
7855 ("yah.durability.teir", "durability"),
7856 ("yah.limits.memory", "resources"),
7857 ] {
7858 assert_eq!(retired_annotation_field(key), Some(field), "{key}");
7859 }
7860 for key in [
7861 "yah.exec",
7862 "yah.sandbox",
7863 "yah.forge",
7864 "yah.placement.requires-taint",
7865 "yah.writable-paths",
7866 ] {
7867 assert_eq!(retired_annotation_field(key), None, "{key}");
7868 }
7869 }
7870
7871 /// The backup-loss hazard R896-F3 was filed around: a spec still written
7872 /// against the annotation family must not read as "undeclared", even when
7873 /// it also carries a valid typed declaration.
7874 #[test]
7875 fn a_durability_annotation_is_refused_rather_than_read_as_undeclared() {
7876 let mut spec = archetype_test_spec("legacy");
7877 spec.annotations
7878 .insert("yah.durability.tier".into(), "stream".into());
7879 let err = spec.durability().unwrap_err();
7880 assert_eq!(
7881 err,
7882 DurabilityDeclError::RetiredAnnotation {
7883 key: "yah.durability.tier".into(),
7884 field: "durability.tier",
7885 }
7886 );
7887 assert!(err.to_string().contains("no backup"), "{err}");
7888
7889 spec.durability = Some(stream(&["a.db"]));
7890 assert!(matches!(
7891 spec.durability(),
7892 Err(DurabilityDeclError::RetiredAnnotation { .. })
7893 ));
7894 assert_eq!(spec.retired_annotation(), Some(("yah.durability.tier", "durability.tier")));
7895 }
7896
7897 // ── R850-P4: durability declaration ──────────────────────────────────────
7898
7899 fn stream(subjects: &[&str]) -> Durability {
7900 Durability {
7901 tier: DurabilityTier::Stream,
7902 engine: Some(DurabilityEngine::Turso),
7903 store: Some("s3://backups/db".into()),
7904 subjects: subjects.iter().map(|s| s.to_string()).collect(),
7905 rpo_seconds: None,
7906 state_mb: None,
7907 }
7908 }
7909
7910 fn none_tier() -> Durability {
7911 Durability {
7912 tier: DurabilityTier::None,
7913 engine: None,
7914 store: None,
7915 subjects: vec![],
7916 rpo_seconds: None,
7917 state_mb: None,
7918 }
7919 }
7920
7921 fn check(d: Durability) -> Result<(), DurabilityDeclError> {
7922 d.check()
7923 }
7924
7925 /// The distinction the whole surface rests on. Every spec in the tree
7926 /// predates the declaration, so `None` has to keep meaning "nobody said" —
7927 /// and a workload that says `tier = "none"` has to be distinguishable from
7928 /// one that never considered the question, because only one of those is a
7929 /// finding.
7930 #[test]
7931 fn an_absent_declaration_and_a_declared_none_are_different_answers() {
7932 let mut spec = archetype_test_spec("durable");
7933 assert_eq!(spec.durability().unwrap(), None);
7934
7935 spec.durability = Some(none_tier());
7936 let declared = spec
7937 .durability()
7938 .unwrap()
7939 .expect("tier = none is a declaration");
7940 assert_eq!(declared.tier, DurabilityTier::None);
7941 assert_eq!(declared.store, None);
7942 }
7943
7944 /// The authoring shape, parsed the way a workload.toml/JSON spec is: a
7945 /// table with snake_case keys and real numbers and lists — no more
7946 /// comma-joined strings.
7947 #[test]
7948 fn a_stream_tier_carries_its_store_rpo_and_state_size() {
7949 let d: Durability = serde_json::from_value(serde_json::json!({
7950 "tier": "stream",
7951 "engine": "turso",
7952 "store": "s3://backups/db",
7953 "subjects": ["accounts.db"],
7954 "rpo_seconds": 30,
7955 "state_mb": 100,
7956 }))
7957 .expect("a complete declaration decodes");
7958 d.check().expect("and is valid");
7959 assert_eq!(d.tier, DurabilityTier::Stream);
7960 assert_eq!(d.engine, Some(DurabilityEngine::Turso));
7961 assert_eq!(d.store.as_deref(), Some("s3://backups/db"));
7962 assert_eq!(d.subjects, vec!["accounts.db".to_string()]);
7963 assert_eq!(d.rpo_seconds, Some(30));
7964 assert_eq!(d.state_mb, Some(100));
7965 }
7966
7967 /// A misspelled tier or engine, a non-numeric RPO, and a declaration with
7968 /// no tier at all are serde refusals now — the vocabulary checks the
7969 /// annotation parser used to hand-roll. Pinned so nobody "helpfully" adds a
7970 /// fallback variant and turns `streem` into "no backups".
7971 #[test]
7972 fn a_malformed_declaration_is_refused_at_decode() {
7973 for bad in [
7974 serde_json::json!({ "tier": "streem" }),
7975 serde_json::json!({ "tier": "stream", "engine": "postgres" }),
7976 serde_json::json!({ "tier": "stream", "rpo_seconds": "2m" }),
7977 serde_json::json!({ "tier": "none", "state_mb": "100MB" }),
7978 serde_json::json!({ "store": "s3://backups/db" }),
7979 ] {
7980 assert!(
7981 serde_json::from_value::<Durability>(bad.clone()).is_err(),
7982 "{bad} must not decode"
7983 );
7984 }
7985 }
7986
7987 #[test]
7988 fn a_tier_that_ships_bytes_must_name_where() {
7989 for store in [None, Some(" ".to_string())] {
7990 let err = check(Durability {
7991 store,
7992 ..stream(&["a.db"])
7993 })
7994 .unwrap_err();
7995 assert_eq!(
7996 err,
7997 DurabilityDeclError::MissingStore {
7998 tier: DurabilityTier::Stream
7999 }
8000 );
8001 // The refusal has to say why there is no default, or the next
8002 // reader adds one.
8003 assert!(err.to_string().contains("nobody chose"));
8004 }
8005 }
8006
8007 #[test]
8008 fn a_store_alongside_tier_none_is_contradictory_and_refused() {
8009 let err = check(Durability {
8010 store: Some("s3://backups/db".into()),
8011 ..none_tier()
8012 })
8013 .unwrap_err();
8014 assert_eq!(err, DurabilityDeclError::StoreWithoutTier);
8015 }
8016
8017 /// Only tier 2 has a recovery point the spec can state. Accepting an RPO on
8018 /// a snapshot tier would let a report print a bound nothing enforces.
8019 #[test]
8020 fn an_rpo_on_a_snapshot_tier_is_refused() {
8021 let err = check(Durability {
8022 tier: DurabilityTier::Snapshot,
8023 rpo_seconds: Some(30),
8024 ..stream(&["a.db"])
8025 })
8026 .unwrap_err();
8027 assert_eq!(
8028 err,
8029 DurabilityDeclError::RpoOnNonStreamTier {
8030 tier: DurabilityTier::Snapshot
8031 }
8032 );
8033 }
8034
8035 /// The declaration is a typed field now, carried name-keyed by the V13
8036 /// envelope; the annotation map stays empty of it.
8037 #[test]
8038 fn a_durability_declaration_round_trips_as_a_typed_field() {
8039 let mut spec = archetype_test_spec("durable");
8040 spec.durability = Some(stream(&["accounts.db"]));
8041 let json = serde_json::to_string(&spec).expect("serialize");
8042 assert!(json.contains("\"durability\""), "{json}");
8043 assert!(!json.contains("yah.durability"), "{json}");
8044 let back: WorkloadSpec = serde_json::from_str(&json).expect("deserialize");
8045 assert_eq!(back.durability().unwrap(), spec.durability().unwrap());
8046 }
8047
8048 // ── R850-F1: the engine and subject axes ─────────────────────────────────
8049
8050 /// The driving shape from R850: one appliance, one named volume, three
8051 /// turso databases inside it. The declaration has to carry all three by
8052 /// name, because a restore's unit is a file and "the volume" is not one.
8053 #[test]
8054 fn three_databases_in_one_volume_are_three_named_subjects() {
8055 let d = stream(&["accounts.db", "passkeys.db", "sessions.db"]);
8056 d.check().expect("three distinct relative subjects are valid");
8057 assert_eq!(d.subjects, vec!["accounts.db", "passkeys.db", "sessions.db"]);
8058 }
8059
8060 /// Gotcha (c) on R850-F1, closed: the three tier names are turso-backup's,
8061 /// so a Postgres appliance saying `tier = "stream"` was declaring something
8062 /// no code in this tree can do. It cannot say it without also naming an
8063 /// engine, and the only engine that decodes is the one with a restore path
8064 /// (see `a_malformed_declaration_is_refused_at_decode`).
8065 #[test]
8066 fn a_bytes_shipping_tier_must_name_an_engine() {
8067 let err = check(Durability {
8068 engine: None,
8069 ..stream(&["a.db"])
8070 })
8071 .unwrap_err();
8072 assert_eq!(
8073 err,
8074 DurabilityDeclError::MissingEngine {
8075 tier: DurabilityTier::Stream
8076 }
8077 );
8078 }
8079
8080 #[test]
8081 fn a_bytes_shipping_tier_must_name_its_databases() {
8082 let err = check(Durability {
8083 tier: DurabilityTier::Snapshot,
8084 ..stream(&[])
8085 })
8086 .unwrap_err();
8087 assert_eq!(
8088 err,
8089 DurabilityDeclError::MissingSubjects {
8090 tier: DurabilityTier::Snapshot
8091 }
8092 );
8093 assert!(err.to_string().contains("guessing"), "{err}");
8094 }
8095
8096 /// `tier = "none"` ships nothing, so an engine or a subject list beside it
8097 /// is a half-edited declaration — the same shape `StoreWithoutTier`
8098 /// already refuses, and refused for the same reason: the reader cannot tell
8099 /// which half is the mistake.
8100 #[test]
8101 fn an_engine_or_subject_list_alongside_tier_none_is_refused() {
8102 assert_eq!(
8103 check(Durability {
8104 engine: Some(DurabilityEngine::Turso),
8105 ..none_tier()
8106 })
8107 .unwrap_err(),
8108 DurabilityDeclError::EngineWithoutTier
8109 );
8110 assert_eq!(
8111 check(Durability {
8112 subjects: vec!["a.db".into()],
8113 ..none_tier()
8114 })
8115 .unwrap_err(),
8116 DurabilityDeclError::SubjectsWithoutTier
8117 );
8118 }
8119
8120 /// A subject is joined onto a host directory by something that then writes
8121 /// to it, so traversal is refused by name rather than normalized away.
8122 /// Silently rewriting a path a human typed is how the right bytes land in
8123 /// the wrong place.
8124 #[test]
8125 fn a_subject_cannot_escape_the_volume_it_is_scoped_to() {
8126 let bad = |subjects: &[&str]| check(stream(subjects)).unwrap_err();
8127 assert_eq!(
8128 bad(&["/etc/passwd"]),
8129 DurabilityDeclError::AbsoluteSubject {
8130 subject: "/etc/passwd".into()
8131 }
8132 );
8133 assert_eq!(
8134 bad(&["../../../etc/passwd"]),
8135 DurabilityDeclError::TraversingSubject {
8136 subject: "../../../etc/passwd".into()
8137 }
8138 );
8139 assert_eq!(
8140 bad(&["data/./a.db"]),
8141 DurabilityDeclError::TraversingSubject {
8142 subject: "data/./a.db".into()
8143 }
8144 );
8145 // A blank entry truncates a list without looking like it did.
8146 assert_eq!(bad(&["a.db", " "]), DurabilityDeclError::EmptySubject);
8147 assert_eq!(
8148 bad(&["a.db", "a.db"]),
8149 DurabilityDeclError::DuplicateSubject {
8150 subject: "a.db".into()
8151 }
8152 );
8153 }
8154
8155 /// Nested subjects are legal — a workload is free to keep its databases in
8156 /// a subdirectory of the volume — so the traversal guard must reject `..`
8157 /// without rejecting every path containing a slash.
8158 #[test]
8159 fn a_subject_may_sit_in_a_subdirectory_of_the_volume() {
8160 let d = Durability {
8161 tier: DurabilityTier::Dedup,
8162 ..stream(&["db/accounts.db"])
8163 };
8164 d.check().expect("a nested relative subject is valid");
8165 }
8166}