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
423use std::collections::BTreeMap;
424use std::collections::HashMap;
425use std::fmt;
426use std::path::PathBuf;
427
428use serde::{Deserialize, Serialize};
429use ts_rs::TS;
430
431pub mod admission;
432pub mod compose_import;
433pub mod control_plane_install;
434pub mod rollout;
435pub mod secrets;
436pub mod sovereign;
437pub mod validate;
438mod version;
439
440pub use version::SchemaVersion;
441
442// ── Duration ──────────────────────────────────────────────────────────────────
443
444/// Duration expressed as an integer millisecond count.
445///
446/// Used for healthcheck intervals, timeouts, delays, and stop grace periods.
447/// Chosen over `std::time::Duration` to keep serde support dependency-free.
448#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
449#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
450#[ts(type = "number")]
451pub struct Millis(pub u64);
452
453impl Millis {
454 pub fn from_secs(s: u64) -> Self {
455 Self(s * 1000)
456 }
457
458 pub fn from_ms(ms: u64) -> Self {
459 Self(ms)
460 }
461
462 pub fn as_ms(self) -> u64 {
463 self.0
464 }
465
466 pub fn as_secs_f64(self) -> f64 {
467 self.0 as f64 / 1000.0
468 }
469}
470
471// ── Primitive newtypes ────────────────────────────────────────────────────────
472
473/// Opaque identifier for a yubaba-managed machine within the cluster.
474///
475/// Used by the semantic validation layer for admission-control capacity checks.
476/// Yubaba passes its own machine ID when validating a spec before deployment.
477#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TS)]
478#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
479pub struct MachineId(pub String);
480
481/// DNS-segment identity for a workload on the cluster mesh, e.g.
482/// `"noisetable-api.pdx"`. Regex constraint: `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`,
483/// length ≤ 63. Enforced in shape validation (R090-F2).
484#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
485#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
486pub struct MeshIdent(pub String);
487
488/// Tier classification that governs admission control and mesh `allow_from`
489/// filtering. Known values: `"public"`, `"tenant"`, `"private"`, `"infra"`.
490/// Custom tiers are allowed per cluster; shape validation warns on unknowns
491/// rather than rejecting them (R090-F2).
492#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
493#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
494pub struct TierTag(pub String);
495
496/// Default single-tenant identity written to specs that predate the tenant
497/// axis (W206). Its concrete string is arbitrary — what matters is that a
498/// single-tenant cluster only ever sees this one value, so every per-tenant
499/// isolation primitive collapses to a no-op. See [`TenantId::singleton`].
500pub const DEFAULT_TENANT: &str = "default";
501
502/// Default single-namespace identity for specs that predate the namespace
503/// axis (W206). See [`NamespaceId::singleton`].
504pub const DEFAULT_NAMESPACE: &str = "default";
505
506/// Tenant **isolation** axis (W206). Separates one operator's workloads from
507/// another's at the network / DB / mesh-identity level. Orthogonal to
508/// [`NamespaceId`] (routing/naming) and [`TierTag`] (workload class within a
509/// `(tenant, namespace)` pair).
510///
511/// **Degenerate case:** when a yubaba reconciler sees only one `TenantId`
512/// across every workload on a machine, per-tenant Podman networks collapse
513/// into the shared tier networks, the tenant prefix on mesh identity is
514/// dropped, and PostgreSQL role separation is skipped — isolation primitives
515/// become no-ops. You pay only when more than one tenant is present. Specs
516/// written before this axis existed deserialize to [`TenantId::singleton`].
517#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, TS)]
518#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
519pub struct TenantId(pub String);
520
521impl TenantId {
522 /// The singleton tenant used for back-compat with single-tenant (current)
523 /// deployments. Specs written before the tenant axis existed deserialize
524 /// to this value via the `#[serde(default)]` on [`WorkloadSpec::tenant`],
525 /// keeping the whole cluster single-tenant so every isolation primitive
526 /// stays a no-op.
527 pub fn singleton() -> Self {
528 Self(DEFAULT_TENANT.to_string())
529 }
530
531 /// Whether this is the singleton (degenerate single-tenant) identity.
532 pub fn is_singleton(&self) -> bool {
533 self.0 == DEFAULT_TENANT
534 }
535}
536
537/// Namespace **routing/naming** axis (W206). A pure naming key that never
538/// affects isolation: it selects the config root, disambiguates service DNS
539/// names within a tenant, prefixes object-store bucket names within a tenant's
540/// bucket scope, and selects the provider zone (e.g. `noisetable.com` vs
541/// `yah.dev`). Two namespaces in the same tenant share networks, mesh-identity
542/// space, and PG cluster — they simply cannot collide on workload names or
543/// external domains. Specs written before this axis existed deserialize to
544/// [`NamespaceId::singleton`].
545#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, TS)]
546#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
547pub struct NamespaceId(pub String);
548
549impl NamespaceId {
550 /// The singleton namespace used for back-compat with single-namespace
551 /// (current) deployments. Specs written before the namespace axis existed
552 /// deserialize to this value via the `#[serde(default)]` on
553 /// [`WorkloadSpec::namespace`].
554 pub fn singleton() -> Self {
555 Self(DEFAULT_NAMESPACE.to_string())
556 }
557
558 /// Whether this is the singleton (degenerate single-namespace) identity.
559 pub fn is_singleton(&self) -> bool {
560 self.0 == DEFAULT_NAMESPACE
561 }
562}
563
564// ── Workload (on-disk envelope) ──────────────────────────────────────────────
565
566/// On-disk `workload.toml` manifest. Each variant matches one
567/// `ServiceComponent.kind` value; the `kind` field on the wire is the serde
568/// discriminator.
569///
570/// This is the **on-disk** envelope — distinct from [`WorkloadSpec`], the
571/// containerd wire format yubaba receives over RPC. A `kind = "container"`
572/// workload deserializes its remaining fields as a [`ContainerManifest`],
573/// which is *either* a digest-pinned `WorkloadSpec` or a local Dockerfile
574/// recipe (R783-F1 / W324); other kinds carry their own per-reconciler
575/// payload shape.
576///
577/// **Never put `#[serde(skip_serializing_if = "Option::is_none")]` on a field
578/// of this enum or any type it reaches.** These types ride the kamaji-proto
579/// **postcard** wire, which is non-self-describing and positional:
580/// `skip_serializing_if` omits the field's byte on serialize while decode still
581/// expects to read it at that offset, so the byte stream misaligns and the
582/// round-trip fails. Use `#[serde(default)]` + `#[ts(optional = nullable)]`
583/// instead — that still gives TOML/JSON back-compat (missing field → `None`)
584/// while the field is always encoded. `MesofactStaticWorkload::ssr_runtime` and
585/// `::serve_bundle` are the reference shape.
586/// **Two wire shapes, one type (R546-B7).** `Serialize`/`Deserialize` are
587/// hand-written and branch on [`is_human_readable`](serde::Deserializer::is_human_readable):
588///
589/// - **TOML/JSON (human-readable)** → *internally* tagged on `kind`, i.e. the
590/// flat shape every on-disk `workload.toml` actually uses
591/// (`kind = "static-asset"` beside `schema_version`, `[[asset]]`, `[aliases]`).
592/// - **postcard (binary)** → *externally* tagged, byte-identical to the derived
593/// representation R590-B3 established for the kamaji UDS.
594///
595/// Why not just `#[serde(tag = "kind")]`: internal tagging buffers through
596/// `deserialize_any`, which postcard (non-self-describing) refuses with
597/// `WontImplement` — that is exactly the failure R590-B3 fixed by flipping this
598/// enum to external tagging. But external tagging wants a single-key map, so
599/// every flat on-disk file then failed with `wanted exactly 1 element, more
600/// than 1 element` and `yah cloud apply` broke for every static-asset
601/// component. Branching on the format satisfies both, and mirrors what
602/// [`ImageRef`] already does for its string-vs-struct form.
603#[derive(Debug, Clone, PartialEq, TS)]
604#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
605#[cfg_attr(
606 feature = "json-schema",
607 schemars(tag = "kind", rename_all = "kebab-case")
608)]
609#[ts(tag = "kind", rename_all = "kebab-case")]
610pub enum Workload {
611 /// Static-site build that publishes an artifact directory to the
612 /// service's `static` provider slot. Reconciled by the
613 /// `mesofact-static` reconciler — does not deploy to yubaba.
614 MesofactStatic(MesofactStaticWorkload),
615
616 /// A container-shaped workload. **Two on-disk forms** (R783-F1 / W324),
617 /// see [`ContainerManifest`]: a digest-pinned [`WorkloadSpec`] reference
618 /// (the form that crosses the kamaji wire) or a local Dockerfile
619 /// [`ContainerBuild`] recipe (which cannot, because it names no digest
620 /// until it has been built).
621 ///
622 /// Construct the wire form with [`Workload::container`] and read it back
623 /// with [`Workload::container_spec`] — most callers only ever mean the
624 /// reference form and should not have to name the manifest enum.
625 ///
626 /// The reference form's inline fields are the full [`WorkloadSpec`] minus
627 /// the `kind` discriminator.
628 ///
629 /// This is also the shape of the W267 sovereign-public-ingress appliance
630 /// (R594-F2): a container-kind workload with `archetype =
631 /// Some(LifecycleArchetype::Appliance)` and
632 /// `requires_taint() == Some(PUBLIC_IP_TAINT)`, **not** a dedicated
633 /// `Workload::ingress(..)` variant. It runs an ordinary OCI image (the
634 /// `passway` proxy, R594-F4) supervised by kamaji exactly like any other
635 /// `Container`, so no admission-list or wire-codec change was needed to
636 /// let kamaji accept it. A new enum variant would have forced an
637 /// exhaustive-match update in every `Workload` consumer, including
638 /// peer-owned `kamaji-proto/src/codec.rs` — the archetype + annotation
639 /// combination expresses "this is the public ingress appliance" without
640 /// that blast radius. See [`WorkloadSpec::requires_taint`] and
641 /// [`LifecycleArchetype::Appliance`].
642 Container(ContainerManifest),
643
644 /// Data-pipeline job with declared I/O and a readiness policy. The
645 /// orchestrator checks all `inputs` are reachable before each run and
646 /// verifies `outputs` afterward. Generalises the OpenRouter JSON-cache
647 /// refresher (`spawn_almanac_refresher`) to the full manifest form.
648 Almanac(AlmanacManifest),
649
650 /// Content-addressed static files uploaded to the mirror's `object_store`
651 /// provider slot. Wave-0 by default — gating mesofact and container waves.
652 /// Rollback is a pointer-flip via `mirror.toml [asset_aliases]`; bytes are
653 /// append-only and never re-pushed on rollback. See W160.
654 StaticAsset(StaticAssetWorkload),
655
656 /// One cold, per-tenant passway serving a single custom domain, forked on
657 /// demand by kamaji's JIT tier (R852-F1 / W267 §"Free-tier ingress at 10k
658 /// domains"). Unlike the `Container`-shaped **node** ingress appliance
659 /// above, this one is native-forked and zero-resident — see
660 /// [`TenantPasswayWorkload`] for why that difference is what made it a
661 /// variant rather than another annotated container.
662 ///
663 /// **Appended last, deliberately.** postcard encodes an external tag as the
664 /// variant *index*, so a variant inserted anywhere but the end renumbers
665 /// every later one and a pre-R852 node silently decodes the wrong shape off
666 /// the kamaji UDS.
667 TenantPassway(TenantPasswayWorkload),
668}
669
670impl Workload {
671 /// The `kind` discriminator this variant serializes as — the same string a
672 /// `workload.toml` writes and a `ServiceComponent.kind` names.
673 ///
674 /// Lives here rather than at a call site because this enum now has FIVE
675 /// places that enumerate its variants (itself plus the four tagging
676 /// mirrors below); a caller-local match would be a sixth, in another crate,
677 /// with nothing to force it to keep up.
678 pub fn kind_str(&self) -> &'static str {
679 match self {
680 Workload::MesofactStatic(_) => "mesofact-static",
681 Workload::Container(_) => "container",
682 Workload::Almanac(_) => "almanac",
683 Workload::StaticAsset(_) => "static-asset",
684 Workload::TenantPassway(_) => "tenant-passway",
685 }
686 }
687
688 /// The per-tenant passway declaration, if this is one.
689 pub fn tenant_passway(&self) -> Option<&TenantPasswayWorkload> {
690 match self {
691 Workload::TenantPassway(w) => Some(w),
692 _ => None,
693 }
694 }
695
696 /// Wrap a digest-pinned [`WorkloadSpec`] as a `kind = "container"`
697 /// workload — the form that crosses the kamaji wire.
698 ///
699 /// Every caller that synthesizes a container workload in code (ingress
700 /// appliances, forge runs, kamaji's own deploy path) means *this* form;
701 /// the [`ContainerManifest::Recipe`] arm only ever arrives by parsing a
702 /// `workload.toml` with a `[build]` table. Keeping the constructor here
703 /// means R783-F1 did not have to teach ~25 call sites the name of a
704 /// manifest enum they have no opinion about.
705 pub fn container(spec: WorkloadSpec) -> Self {
706 Workload::Container(ContainerManifest::Reference(spec))
707 }
708
709 /// The digest-pinned spec of a `kind = "container"` workload, if this is
710 /// a container workload in the reference form.
711 ///
712 /// `None` covers both "not a container" and "a container *recipe*, which
713 /// has no spec until it is built" — a consumer that speaks the wire
714 /// (kamaji, yubaba's deploy path) must treat both as inadmissible, so
715 /// collapsing them into one `None` is deliberate rather than lossy. Use
716 /// [`Workload::container_manifest`] when the two need distinguishing.
717 pub fn container_spec(&self) -> Option<&WorkloadSpec> {
718 match self {
719 Workload::Container(m) => m.as_spec(),
720 _ => None,
721 }
722 }
723
724 /// The container manifest, in whichever on-disk form it was written.
725 pub fn container_manifest(&self) -> Option<&ContainerManifest> {
726 match self {
727 Workload::Container(m) => Some(m),
728 _ => None,
729 }
730 }
731}
732
733/// Internally-tagged mirror of [`Workload`] — the on-disk shape. Only ever
734/// reached on the human-readable branch, so its `deserialize_any` buffering is
735/// never asked of postcard.
736#[derive(Serialize, Deserialize)]
737#[serde(tag = "kind", rename_all = "kebab-case")]
738enum WorkloadTagged {
739 MesofactStatic(MesofactStaticWorkload),
740 Container(ContainerManifest),
741 Almanac(AlmanacManifest),
742 StaticAsset(StaticAssetWorkload),
743 TenantPassway(TenantPasswayWorkload),
744}
745
746/// Borrowing twin of [`WorkloadTagged`] so `Serialize` need not clone the
747/// payload. Variant order must match [`Workload`].
748#[derive(Serialize)]
749#[serde(tag = "kind", rename_all = "kebab-case")]
750enum WorkloadTaggedRef<'a> {
751 MesofactStatic(&'a MesofactStaticWorkload),
752 Container(&'a ContainerManifest),
753 Almanac(&'a AlmanacManifest),
754 StaticAsset(&'a StaticAssetWorkload),
755 TenantPassway(&'a TenantPasswayWorkload),
756}
757
758/// Externally-tagged mirror — the postcard wire shape R590-B3 established.
759/// postcard encodes an external tag as the *variant index*, so the variant
760/// ORDER here is load-bearing: it must match [`Workload`] exactly or the
761/// kamaji UDS silently decodes into the wrong variant.
762///
763/// `Container` deliberately keeps [`WorkloadSpec`], **not**
764/// [`ContainerManifest`] (R783-F1 / W324): the wire carries only the
765/// digest-pinned reference form, so these bytes are unchanged by the on-disk
766/// split, and a [`ContainerManifest::Recipe`] is refused at serialize rather
767/// than encoded as a second variant nothing on the far side can execute.
768#[derive(Serialize, Deserialize)]
769#[serde(rename_all = "kebab-case")]
770enum WorkloadExternal {
771 MesofactStatic(MesofactStaticWorkload),
772 Container(WorkloadSpec),
773 Almanac(AlmanacManifest),
774 StaticAsset(StaticAssetWorkload),
775 TenantPassway(TenantPasswayWorkload),
776}
777
778/// Borrowing twin of [`WorkloadExternal`]. Same order requirement.
779#[derive(Serialize)]
780#[serde(rename_all = "kebab-case")]
781enum WorkloadExternalRef<'a> {
782 MesofactStatic(&'a MesofactStaticWorkload),
783 Container(&'a WorkloadSpec),
784 Almanac(&'a AlmanacManifest),
785 StaticAsset(&'a StaticAssetWorkload),
786 TenantPassway(&'a TenantPasswayWorkload),
787}
788
789impl Serialize for Workload {
790 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
791 where
792 S: serde::Serializer,
793 {
794 if s.is_human_readable() {
795 match self {
796 Workload::MesofactStatic(w) => WorkloadTaggedRef::MesofactStatic(w),
797 Workload::Container(w) => WorkloadTaggedRef::Container(w),
798 Workload::Almanac(w) => WorkloadTaggedRef::Almanac(w),
799 Workload::StaticAsset(w) => WorkloadTaggedRef::StaticAsset(w),
800 Workload::TenantPassway(w) => WorkloadTaggedRef::TenantPassway(w),
801 }
802 .serialize(s)
803 } else {
804 match self {
805 Workload::MesofactStatic(w) => WorkloadExternalRef::MesofactStatic(w),
806 // The wire gate (W324 §5). A recipe names no digest, so there
807 // is nothing for kamaji to pull — refusing here makes "a build
808 // recipe cannot reach kamaji" a fact the type system holds,
809 // rather than a convention someone eventually forgets.
810 Workload::Container(ContainerManifest::Recipe(_)) => {
811 return Err(serde::ser::Error::custom(RECIPE_IS_NOT_A_WIRE_SPEC))
812 }
813 Workload::Container(ContainerManifest::Reference(spec)) => {
814 WorkloadExternalRef::Container(spec)
815 }
816 Workload::Almanac(w) => WorkloadExternalRef::Almanac(w),
817 Workload::StaticAsset(w) => WorkloadExternalRef::StaticAsset(w),
818 Workload::TenantPassway(w) => WorkloadExternalRef::TenantPassway(w),
819 }
820 .serialize(s)
821 }
822 }
823}
824
825impl<'de> Deserialize<'de> for Workload {
826 fn deserialize<D>(de: D) -> Result<Self, D::Error>
827 where
828 D: serde::Deserializer<'de>,
829 {
830 if de.is_human_readable() {
831 Ok(match WorkloadTagged::deserialize(de)? {
832 WorkloadTagged::MesofactStatic(w) => Workload::MesofactStatic(w),
833 WorkloadTagged::Container(w) => Workload::Container(w),
834 WorkloadTagged::Almanac(w) => Workload::Almanac(w),
835 WorkloadTagged::StaticAsset(w) => Workload::StaticAsset(w),
836 WorkloadTagged::TenantPassway(w) => Workload::TenantPassway(w),
837 })
838 } else {
839 Ok(match WorkloadExternal::deserialize(de)? {
840 WorkloadExternal::MesofactStatic(w) => Workload::MesofactStatic(w),
841 // Only the reference form exists on the wire, by construction
842 // of `WorkloadExternal` — see its doc comment.
843 WorkloadExternal::Container(w) => Workload::container(w),
844 WorkloadExternal::Almanac(w) => Workload::Almanac(w),
845 WorkloadExternal::StaticAsset(w) => Workload::StaticAsset(w),
846 WorkloadExternal::TenantPassway(w) => Workload::TenantPassway(w),
847 })
848 }
849 }
850}
851
852// ── Container manifest (R783-F1 / W324) ───────────────────────────────────────
853
854/// Error text used both by the postcard serializer gate and by
855/// [`ContainerManifest::into_spec`]'s doc, so the two cannot drift.
856const RECIPE_IS_NOT_A_WIRE_SPEC: &str = "a kind = \"container\" workload in the RECIPE form \
857 (a [build] table) cannot cross the kamaji wire: it names an image tag, not a digest, and \
858 the digest does not exist until `docker build` has run. Lower it with \
859 `ContainerBuild::into_spec(digest)` after the build, then send the resulting WorkloadSpec.";
860
861/// On-disk payload of `kind = "container"` — **two forms**, one wire type
862/// (W324 §5).
863///
864/// A [`WorkloadSpec`] asserts a content-addressed identity: its
865/// [`ImageRef::digest`] is a required `sha256:<hex>` and the string form
866/// rejects a bare tag at serde-deserialize (R438-T3). A local component built
867/// from a Dockerfile next to its `workload.toml` cannot satisfy that — its
868/// image is `yah-local/<name>:dev`, and the digest does not exist until the
869/// build has run. So a build *recipe* is not a degenerate spec with a missing
870/// field; it is a promise to produce one, and the two are different types.
871///
872/// The discriminator is the presence of a `[build]` table. `WorkloadSpec` has
873/// no `build` field and [`ContainerBuild`] requires one, so the two shapes are
874/// mutually exclusive — and picking the branch explicitly (rather than with
875/// `#[serde(untagged)]`) is what lets a malformed reference still report
876/// `missing field \`image\`` instead of "data did not match any variant".
877///
878/// Only [`Reference`](Self::Reference) crosses the postcard kamaji wire; see
879/// [`WorkloadExternal`]'s doc comment for why that keeps those bytes
880/// byte-identical to the pre-split encoding.
881#[derive(Debug, Clone, PartialEq, TS)]
882#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
883#[cfg_attr(feature = "json-schema", schemars(untagged))]
884#[ts(untagged)]
885pub enum ContainerManifest {
886 /// Digest-pinned image. Crosses the wire as-is.
887 Reference(WorkloadSpec),
888
889 /// Dockerfile recipe. **Local only** — see [`ContainerBuild`].
890 Recipe(ContainerBuild),
891}
892
893impl ContainerManifest {
894 /// The digest-pinned spec, or `None` for the recipe form.
895 pub fn as_spec(&self) -> Option<&WorkloadSpec> {
896 match self {
897 ContainerManifest::Reference(spec) => Some(spec),
898 ContainerManifest::Recipe(_) => None,
899 }
900 }
901
902 /// The build recipe, or `None` for the reference form.
903 pub fn as_recipe(&self) -> Option<&ContainerBuild> {
904 match self {
905 ContainerManifest::Recipe(b) => Some(b),
906 ContainerManifest::Reference(_) => None,
907 }
908 }
909
910 /// Consume the manifest, yielding the digest-pinned spec. `Err` carries
911 /// the recipe back so a caller that *can* build it still has it.
912 pub fn into_spec(self) -> Result<WorkloadSpec, ContainerBuild> {
913 match self {
914 ContainerManifest::Reference(spec) => Ok(spec),
915 ContainerManifest::Recipe(b) => Err(b),
916 }
917 }
918
919 /// `"reference"` or `"recipe"` — for error messages that need to name
920 /// which form was found without matching on the enum at the call site.
921 pub fn form(&self) -> &'static str {
922 match self {
923 ContainerManifest::Reference(_) => "reference",
924 ContainerManifest::Recipe(_) => "recipe",
925 }
926 }
927}
928
929impl Serialize for ContainerManifest {
930 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
931 where
932 S: serde::Serializer,
933 {
934 match self {
935 // Transparent in both directions: the on-disk container form is
936 // the payload's own fields flattened under `kind = "container"`,
937 // exactly as it was before the split.
938 ContainerManifest::Reference(spec) => spec.serialize(s),
939 ContainerManifest::Recipe(recipe) => {
940 if s.is_human_readable() {
941 recipe.serialize(s)
942 } else {
943 Err(serde::ser::Error::custom(RECIPE_IS_NOT_A_WIRE_SPEC))
944 }
945 }
946 }
947 }
948}
949
950impl<'de> Deserialize<'de> for ContainerManifest {
951 fn deserialize<D>(de: D) -> Result<Self, D::Error>
952 where
953 D: serde::Deserializer<'de>,
954 {
955 use serde::de::Error as _;
956
957 // postcard and friends are non-self-describing, so there is no map to
958 // probe for `[build]` — and by construction the binary wire only ever
959 // carries the reference form anyway (`WorkloadExternal::Container`).
960 if !de.is_human_readable() {
961 return WorkloadSpec::deserialize(de).map(ContainerManifest::Reference);
962 }
963
964 // Buffer once, then branch explicitly. `serde_json::Value` is the
965 // buffer rather than `#[serde(untagged)]`'s private `Content` because
966 // untagged discards the inner error: `missing field \`image\`` — the
967 // one thing an author needs to see — becomes "data did not match any
968 // variant of untagged enum ContainerManifest".
969 let buffered = serde_json::Value::deserialize(de)?;
970
971 match (
972 buffered.get("build").is_some(),
973 buffered.get("image").is_some(),
974 ) {
975 (true, _) => ContainerBuild::deserialize(buffered)
976 .map(ContainerManifest::Recipe)
977 .map_err(|e| {
978 D::Error::custom(format!(
979 "kind = \"container\" with a [build] table is a local build recipe: {e}"
980 ))
981 }),
982 (false, true) => WorkloadSpec::deserialize(buffered)
983 .map(ContainerManifest::Reference)
984 .map_err(|e| {
985 D::Error::custom(format!(
986 "kind = \"container\" without a [build] table is a digest-pinned image \
987 reference: {e}"
988 ))
989 }),
990 // Neither marker. Reporting `missing field \`image\`` here would
991 // send a recipe author off to add a field their form does not
992 // have, so name both forms instead — this is the one case where
993 // the file does not say which of the two it is trying to be.
994 (false, false) => Err(D::Error::custom(
995 "kind = \"container\" must declare either a digest-pinned `image` (the wire \
996 form: a WorkloadSpec yubaba hands to kamaji) or a [build] table (a local \
997 Dockerfile recipe built on the operator's box) — it declares neither",
998 )),
999 }
1000 }
1001}
1002
1003/// `kind = "container"` in the **recipe** form: a Dockerfile next to the
1004/// component's `workload.toml`, built and run on the operator's box.
1005///
1006/// This is the shape `ContainerReconciler` drives (`docker build` from
1007/// [`build`](Self::build), `docker run` with [`run`](Self::run)). It is
1008/// deliberately *not* a `WorkloadSpec` — see [`ContainerManifest`] for why the
1009/// digest invariant makes that impossible, and [`Self::into_spec`] for the one
1010/// lowering that is allowed.
1011///
1012/// **Unknown keys are tolerated on purpose.** `crates/yah/cloud-admin/workload.toml`
1013/// carries a `[process]` table read by `LocalProcessReconciler` on the dev
1014/// mirror — one component file, three tier runtimes (W324 §1). Adding
1015/// `deny_unknown_fields` here would make that file unparseable as a container
1016/// manifest, which is the opposite of the point.
1017#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1018#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1019pub struct ContainerBuild {
1020 /// Wire-format version. Always `V1` today.
1021 pub schema_version: SchemaVersion,
1022
1023 /// Component name. Same field the reference form carries, so a manifest
1024 /// identifies itself the same way whichever form it is written in.
1025 pub name: String,
1026
1027 /// How the image is built. Its presence is what makes this a recipe.
1028 pub build: ContainerBuildStep,
1029
1030 /// How the built image is run locally.
1031 #[serde(default)]
1032 pub run: ContainerRunConfig,
1033}
1034
1035impl ContainerBuild {
1036 /// Lower a recipe to the wire type, **once a build has produced a digest**.
1037 ///
1038 /// The signature is the invariant (W324 §5): there is no way to reach a
1039 /// `WorkloadSpec` from a recipe without supplying the `sha256:<hex>` the
1040 /// build emitted, so an unpinned container spec cannot be constructed by
1041 /// accident.
1042 ///
1043 /// Fallible because `digest` is a caller-supplied string: a malformed one
1044 /// must be an error, not a `WorkloadSpec` that lies about being
1045 /// content-addressed. Everything the recipe does not declare
1046 /// (`tier`, `resources`, `restart_policy`, …) takes the same defaults a
1047 /// hand-written local container gets; `tier` is the caller's because
1048 /// admission control is a cluster policy, not a manifest fact.
1049 pub fn into_spec(self, digest: &str, tier: TierTag) -> Result<WorkloadSpec, String> {
1050 let image_tag = self
1051 .build
1052 .image
1053 .clone()
1054 .unwrap_or_else(|| format!("yah-local/{}:dev", self.name));
1055
1056 // Route through the one parser that owns the digest rule (R438-T3) so
1057 // the recipe path cannot grow a second, laxer definition of "pinned".
1058 let image = compose_import::parse_pinned_image_ref(&format!("{image_tag}@{digest}"))
1059 .map_err(|e| format!("lowering container recipe {:?}: {e}", self.name))?;
1060
1061 let ports = MeshExpose::anonymous_ports(self.run.port);
1062
1063 Ok(WorkloadSpec {
1064 schema_version: self.schema_version,
1065 name: self.name.clone(),
1066 image,
1067 tier,
1068 tenant: TenantId::singleton(),
1069 namespace: NamespaceId::singleton(),
1070 replicas: 1,
1071 command: None,
1072 entrypoint: None,
1073 workdir: None,
1074 user: None,
1075 env: self
1076 .run
1077 .env
1078 .into_iter()
1079 .map(|(name, value)| EnvVar {
1080 name,
1081 value: EnvValue::Literal { value },
1082 })
1083 .collect(),
1084 secrets: vec![],
1085 volumes: self
1086 .run
1087 .mounts
1088 .into_iter()
1089 .map(|m| VolumeMount {
1090 source: VolumeSource::Bind {
1091 host_path: PathBuf::from(m.host),
1092 },
1093 target: m.container,
1094 read_only: m.read_only,
1095 })
1096 .collect(),
1097 resources: ResourceLimits {
1098 memory_mb: 1024,
1099 cpu_millis: 1000,
1100 ephemeral_storage_mb: 1024,
1101 },
1102 depends_on: vec![],
1103 requires: vec![],
1104 healthcheck: None,
1105 restart_policy: RestartPolicy::Always,
1106 archetype: Some(LifecycleArchetype::Server),
1107 stop_policy: StopPolicy {
1108 signal: 15,
1109 grace_period: Millis::from_secs(10),
1110 },
1111 expose: ExposeSpec {
1112 mesh: MeshExpose {
1113 identity: MeshIdent(self.name),
1114 ports,
1115 allow_from: vec![],
1116 },
1117 public: None,
1118 operator: None,
1119 },
1120 labels: HashMap::new(),
1121 annotations: HashMap::new(),
1122 })
1123 }
1124}
1125
1126/// The `[build]` table of a container recipe.
1127#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1128#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1129pub struct ContainerBuildStep {
1130 /// Dockerfile path, relative to the component directory.
1131 #[serde(default = "default_dockerfile")]
1132 pub dockerfile: PathBuf,
1133
1134 /// Build context, relative to the workspace root. `None` → the component
1135 /// directory. Workspace crates set `"."` so their path-dependency sources
1136 /// resolve.
1137 #[serde(default)]
1138 #[ts(optional = nullable)]
1139 pub context: Option<PathBuf>,
1140
1141 /// Image tag to build and run. `None` → `yah-local/<name>:dev`.
1142 ///
1143 /// A **tag**, not an [`ImageRef`]: this names an image that does not exist
1144 /// yet, so there is no digest to pin it by.
1145 #[serde(default)]
1146 #[ts(optional = nullable)]
1147 pub image: Option<String>,
1148}
1149
1150fn default_dockerfile() -> PathBuf {
1151 PathBuf::from("Dockerfile")
1152}
1153
1154impl Default for ContainerBuildStep {
1155 fn default() -> Self {
1156 Self {
1157 dockerfile: default_dockerfile(),
1158 context: None,
1159 image: None,
1160 }
1161 }
1162}
1163
1164/// The `[run]` table of a container recipe.
1165#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, TS)]
1166#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1167pub struct ContainerRunConfig {
1168 /// Container port the process listens on.
1169 #[serde(default)]
1170 #[ts(optional = nullable)]
1171 pub port: Option<u16>,
1172
1173 /// Host port to publish it on. `None` → same as [`port`](Self::port).
1174 #[serde(default)]
1175 #[ts(optional = nullable)]
1176 pub host_port: Option<u16>,
1177
1178 /// Environment passed into the container.
1179 #[serde(default)]
1180 pub env: BTreeMap<String, String>,
1181
1182 /// Bind mounts from the workspace into the container.
1183 #[serde(default)]
1184 pub mounts: Vec<ContainerMount>,
1185}
1186
1187/// One `[[run.mounts]]` entry of a container recipe.
1188#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1189#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1190pub struct ContainerMount {
1191 /// Host path. Relative paths resolve against the workspace root — the
1192 /// declaration lives in the repo, so it should read like a repo path and
1193 /// stay valid on whichever machine the operator runs it from.
1194 pub host: String,
1195
1196 /// Absolute path inside the container.
1197 pub container: PathBuf,
1198
1199 /// Default `true`. A workspace mount is config the service *reads*; a
1200 /// writable default would let a container mutate the operator's checkout
1201 /// as a side effect of running, so opting into that has to be explicit.
1202 #[serde(default = "default_true")]
1203 pub read_only: bool,
1204}
1205
1206fn default_true() -> bool {
1207 true
1208}
1209
1210/// `kind = "mesofact-static"` payload — static-site build colocated with the
1211/// frontend it deploys.
1212///
1213/// The two-role model (R256-F7): a build/publish step plus an optional
1214/// SSR/SPA runtime companion. The build step is always transient (runs once,
1215/// publishes, exits). The companion is long-lived and only present when the
1216/// app has dynamic/server-rendered pages; pure static sites leave it `None`.
1217#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
1218#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1219pub struct MesofactStaticWorkload {
1220 /// Wire-format version. Always `V1` today.
1221 pub schema_version: SchemaVersion,
1222
1223 /// Build command + output directory.
1224 pub build: BuildConfig,
1225
1226 /// Path (relative to the manifest) of the routes module the
1227 /// `mesofact-static` reconciler reads to enumerate routes.
1228 pub routes: PathBuf,
1229
1230 /// Where the build command runs. Default: `HostSide` (mesofact-dev on the
1231 /// host). Set to `InContainer` for cloud/HA where no host watcher is
1232 /// present and CI-fidelity build environments are required.
1233 #[serde(default)]
1234 pub build_mode: BuildMode,
1235
1236 /// Optional SSR/SPA runtime companion container.
1237 ///
1238 /// `None` → pure static site; Caddy (or equivalent CDN) serves all
1239 /// requests directly from the object store. This is the common case for
1240 /// dev-yah today.
1241 ///
1242 /// `Some` → the workload spec describes a long-lived container that
1243 /// handles dynamic/SSR requests. Caddy routes static asset paths to
1244 /// the object store and all other paths to this container. The companion
1245 /// uses `RestartPolicy::Always`; the orchestrator (camp or yubaba)
1246 /// ensures it stays up alongside the Caddy edge.
1247 #[ts(optional = nullable)]
1248 pub ssr_runtime: Option<WorkloadSpec>,
1249
1250 /// Serve-time reference to a published W272 bundle (R599-F4).
1251 ///
1252 /// `Some` → the built app is deployed as a content-addressed bundle that
1253 /// kamaji materializes from the bundle store (R599-F1) and serves via its
1254 /// native backend, instead of (or in addition to) the build reconciler
1255 /// pushing `dist/` to the object-store/CDN. `None` → legacy
1256 /// build-and-publish-only workload — kamaji rejects that form as yubaba's
1257 /// `mesofact-static` reconciler's responsibility.
1258 ///
1259 /// No `skip_serializing_if`: like `ssr_runtime`, this field is always
1260 /// encoded so the postcard wire codec (non-self-describing, positional)
1261 /// round-trips — `skip_serializing_if` would omit the byte on serialize
1262 /// while decode still expects it. `#[serde(default)]` keeps every existing
1263 /// `mesofact-static` TOML/JSON that predates this field parsing to `None`.
1264 #[serde(default)]
1265 #[ts(optional = nullable)]
1266 pub serve_bundle: Option<MesofactServeBundle>,
1267
1268 /// Revalidate receiver for the almanac push model (R330-F12).
1269 ///
1270 /// `Some` → kamaji also forks `mesofact serve --revalidate <workload>`
1271 /// alongside the bundle's static serve (or in place of it when
1272 /// `serve_bundle` is `None`). The receiver is ephemeral-V8: each
1273 /// `POST /dawn` boots a V8 isolate, re-renders the route, republishes to
1274 /// the CDN, then drops the isolate. (`/revalidate` is still served as a
1275 /// transitional alias — yah R752-T10 renamed it so the render stage stops
1276 /// sharing a path with almanac's feed-refetch stage, `POST /freshen`.)
1277 ///
1278 /// Env vars are resolved at deploy time (R2 creds + mirror bearer) so
1279 /// the node never sees keystore slot names.
1280 #[serde(default)]
1281 #[ts(optional = nullable)]
1282 pub revalidate_receiver: Option<MesofactRevalidateReceiver>,
1283}
1284
1285/// Revalidate receiver config (R330-F12) — tells kamaji to fork a second
1286/// `mesofact serve --revalidate` process alongside the static bundle server.
1287///
1288/// The receiver is the almanac push endpoint: a lightweight resident axum
1289/// server mounting `POST /dawn` (plus the legacy `/revalidate` alias) that
1290/// boots V8 on each poke, re-renders the invalidated route, publishes to
1291/// R2/CDN, then drops the isolate.
1292#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1293#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1294pub struct MesofactRevalidateReceiver {
1295 /// Routes the receiver accepts pokes for (allowlist).
1296 /// Empty vec → all routes in the workload's manifest are revalidatable.
1297 #[serde(default)]
1298 pub routes: Vec<String>,
1299
1300 /// Path to `mesofact.config.toml` carrying the `[publish]` block
1301 /// (bucket / zone / env-named credentials). Relative to the workload
1302 /// directory. Default: `"mesofact.config.toml"`.
1303 #[serde(default = "default_publish_config_path")]
1304 pub publish_config: String,
1305
1306 /// Env var name holding the bearer secret for this tenant, resolved
1307 /// at deploy time and set as `MESOFACT_MIRROR_KEY` on the receiver
1308 /// process. `None` → open receiver (no bearer check).
1309 #[ts(optional = nullable)]
1310 pub mirror_key_env: Option<String>,
1311
1312 /// Environment variables set on the revalidate process by kamaji.
1313 /// Keys are the canonical env var names (`MESOFACT_S3_ACCESS_KEY_ID`,
1314 /// `MESOFACT_S3_SECRET_ACCESS_KEY`, `CLOUDFLARE_API_TOKEN`,
1315 /// `MESOFACT_MIRROR_KEY`). Values are resolved from the keystore at
1316 /// deploy time — the node never sees slot names.
1317 #[serde(default)]
1318 pub env: std::collections::BTreeMap<String, String>,
1319
1320 /// Feed-fetch tier (R330-F31) — the almanac feeds whose artifacts must be
1321 /// refreshed **on the node** for a poke to have anything new to render.
1322 ///
1323 /// Empty → no fetcher; the receiver re-renders whatever data the bundle was
1324 /// built with (correct for a site whose data only changes at build time,
1325 /// silently stale for one whose data is a live feed). Non-empty → kamaji
1326 /// forks a third resident process, the `almanac-feed` fetcher, next to the
1327 /// receiver — resolved from the bundle's `bins/<triple>/almanac-feed` when
1328 /// it carries one, else from [`feed_runtime`](Self::feed_runtime).
1329 #[serde(default)]
1330 pub feeds: Vec<AlmanacFeed>,
1331
1332 /// Runtime ref the `almanac-feed` fetcher resolves from the node's shared
1333 /// runtime-asset cache when the bundle carries no `bins/` (R746-T3), e.g.
1334 /// `"almanac-feed/0.8.22"`.
1335 ///
1336 /// This is what lets a **vanilla** bundle have a feed tier at all. A
1337 /// self-contained bundle stages the fetcher into `bins/` and stays closed
1338 /// over it; a vanilla bundle carries no binaries by construction, so the
1339 /// fetcher has to be a node-level asset for the same reason `serve` is —
1340 /// otherwise a templates-only sync would still need a cross-built musl
1341 /// binary sitting on the syncing machine's disk.
1342 ///
1343 /// `None` with `feeds` non-empty and no sidecar in the bundle is a deploy
1344 /// failure, named at the node. It is not a silent skip: "the site serves
1345 /// but its data is frozen" is the exact state R330-F31 exists to make
1346 /// observable.
1347 #[serde(default)]
1348 #[ts(optional = nullable)]
1349 pub feed_runtime: Option<String>,
1350
1351 /// Seconds between feed-fetch ticks. Ignored when `feeds` is empty.
1352 ///
1353 /// This is the site's freshness bound: a release lands, and the next tick
1354 /// refreshes + pokes. `FeedRunner`'s change-suppression means an idle tick
1355 /// costs one conditional fetch, so a short interval is affordable.
1356 #[serde(default = "default_feed_interval_secs")]
1357 pub feed_interval_secs: u64,
1358
1359 /// Workspace-relative path of the component whose build produced this
1360 /// bundle, e.g. `app/yah/web/marketing` (R330-F31).
1361 ///
1362 /// Reconciles two roots for one file: a feed declares `emit.artifact`
1363 /// workspace-relative (that is where it is authored), while the route
1364 /// declares the same file project-relative (that is what the bundle
1365 /// carries). The fetcher strips this prefix to get from one to the other.
1366 /// `None` → the two already coincide.
1367 #[serde(default)]
1368 #[ts(optional = nullable)]
1369 pub feed_project_prefix: Option<String>,
1370}
1371
1372/// One almanac feed handed to the on-node fetcher (R330-F31).
1373///
1374/// The definition travels **by value**, not by path: the node has no copy of
1375/// the camp's `.yah/almanac/` tree, and staging one into the content-addressed
1376/// bundle would put a mutable-by-nature config inside an immutable artifact.
1377/// The fetcher parses `config_toml` with the same `FeedConfig` type that reads
1378/// the file at the source, so there is one schema and no drift.
1379#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1380#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1381pub struct AlmanacFeed {
1382 /// Feed name — the `.yah/almanac/<name>.toml` stem. Logs/diagnostics only;
1383 /// `config_toml` is authoritative.
1384 pub name: String,
1385
1386 /// Verbatim contents of the feed definition TOML.
1387 pub config_toml: String,
1388}
1389
1390fn default_publish_config_path() -> String {
1391 "mesofact.config.toml".to_string()
1392}
1393
1394/// Five minutes: fast enough that a release is live on yah.dev before anyone
1395/// goes looking, slow enough to be invisible against a source API's rate limit.
1396fn default_feed_interval_secs() -> u64 {
1397 300
1398}
1399
1400/// Serve-time reference to a published W272 bundle (R599-F4) — the
1401/// `{bundle_digest, runtime, lifecycle}` triple a `mesofact-static` workload
1402/// carries when kamaji, not the build reconciler, serves it.
1403///
1404/// @yah:ticket(R870-B6, "Bundle origin is node-wide, so a second tenant's bundle can never be materialized")
1405/// @yah:status(review)
1406/// @yah:at(2026-09-09T03:23:08Z)
1407/// @yah:assignee(agent:bundle-anthropic-ashguard)
1408/// @yah:parent(R870)
1409/// @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.")
1410/// @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.")
1411/// @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.")
1412/// @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.")
1413/// @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.")
1414/// @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.")
1415/// @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.")
1416/// @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.")
1417/// @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.")
1418/// @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.")
1419/// @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.")
1420/// @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.")
1421/// @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.")
1422/// @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.")
1423/// @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`).")
1424/// @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.")
1425/// @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.")
1426/// @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.)")
1427/// @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).")
1428/// @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.")
1429/// @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.")
1430/// @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.")
1431/// @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).")
1432/// @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.")
1433/// @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.")
1434/// @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.")
1435#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1436#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1437pub struct MesofactServeBundle {
1438 /// BLAKE3 digest of the published bundle manifest — the content-address
1439 /// kamaji materializes from the bundle store (`yah_mesofact_bundle`,
1440 /// R599-F1). Same 64-hex shape the bundle crate's `BundleHash` validates.
1441 pub digest: BlakeHash,
1442
1443 /// Runtime that serves the bundle: `"self"` (bundle ships its own
1444 /// `bins/<triple>/serve`) or `"mesofact/<version>"` (resolve the stock
1445 /// serve runtime asset from the node cache). Wire-mirrors
1446 /// `yah_mesofact_bundle::BundleRuntime`; kept as a plain `String` here so
1447 /// workload-spec stays free of the bundle crate and its non-TS/schema
1448 /// newtypes.
1449 pub runtime: String,
1450
1451 /// How kamaji supervises the served bundle. Default: keep-alive.
1452 #[serde(default)]
1453 pub lifecycle: BundleLifecycle,
1454
1455 /// Port the served bundle listens on (R599-F12). This is the bundle-tier
1456 /// analogue of a container's `expose.mesh.ports`: the *declared* serving
1457 /// port, which a proxy pairs with the workload's mesh IP to get a dialable
1458 /// address.
1459 ///
1460 /// `None` → **the supervisor allocates one** (R844-F2), and reports the
1461 /// port it bound back to yubaba on the next workload listing, where it
1462 /// lands in the service record an ingress upstream is rendered from. This
1463 /// is the normal case: a mirror should not have to name a port at all.
1464 ///
1465 /// It used to mean "fall back to kamaji's node-wide default
1466 /// (`KAMAJI_BUNDLE_PORT`, else 8080)", which was a single node-wide slot
1467 /// wearing the word *default* — correct only while a node hosted one
1468 /// bundle, and a silent collision for the second. R599-F12 added this field
1469 /// so a workload could opt out of that; R844-F2 removed the default itself,
1470 /// so opting out is no longer something anyone has to remember to do.
1471 ///
1472 /// Declaring a port still pins it exactly, for a workload that must be
1473 /// reachable at a known number.
1474 ///
1475 /// No `skip_serializing_if` — see `serve_bundle`'s note: the postcard wire
1476 /// codec is positional, so an omitted byte shifts every later field.
1477 #[serde(default)]
1478 #[ts(optional = nullable)]
1479 pub port: Option<u16>,
1480
1481 /// Environment the serve process is forked with (R556-T12) — already
1482 /// **resolved** values, `NAME → value`.
1483 ///
1484 /// This is what makes an SSR route that reads a private source deployable
1485 /// at all: `mesofact serve` resolves a source's credentials from its own
1486 /// process environment at request time, and before this field the static /
1487 /// SSR serve process was forked with `env: vec![]` while only the
1488 /// `revalidate_receiver` sub-slot carried any. A declared-authed SSR site
1489 /// therefore deployed clean and failed *per request* on the node.
1490 ///
1491 /// Resolution happens deploy-side, exactly like
1492 /// [`MesofactRevalidateReceiver::env`]: the mirror declares source URIs
1493 /// (`vault:<slot>` / `env:<VAR>`), `yah cloud apply` resolves them against
1494 /// the operator's vault, and the node receives values. Keystore slot names
1495 /// never cross the wire.
1496 ///
1497 /// Appended **after** `port` — see `port`'s note: the postcard wire codec
1498 /// is positional, so a new field goes last and never carries
1499 /// `skip_serializing_if`.
1500 #[serde(default)]
1501 pub env: BTreeMap<String, String>,
1502
1503 /// Public HTTPS origin serving the bundle store this workload was published
1504 /// to (R870-B6) — e.g. `"https://cdn.noisetable.com"`, the R2 custom domain
1505 /// bound to the tenant's own bucket.
1506 ///
1507 /// `None` → the node's own `KAMAJI_BUNDLE_ORIGIN`, which is the shape every
1508 /// yah-owned mirror uses and the reason this is optional rather than
1509 /// required.
1510 ///
1511 /// # Why the store has to travel with the workload
1512 ///
1513 /// This is `port`'s defect one axis over, and it was found the same way: by
1514 /// a second tenant. Publishing is per-service — `providers.bundle.bucket`
1515 /// names the tenant's own R2 bucket — while fetching was per-*node*, from
1516 /// the single `KAMAJI_BUNDLE_ORIGIN` the systemd drop-in sets. Those two
1517 /// agree only while the fleet hosts one tenant. The noisetable deploy got
1518 /// all the way to admission and then failed with `missing blob
1519 /// manifests/<digest>`: its manifest was in `noisetable-marketing`, and the
1520 /// node looked in `cdn.yah.dev`.
1521 ///
1522 /// Pointing the tenant's slot at yah's bucket would also have worked, and
1523 /// was refused — a tenant's build output in yah's store makes the tenant
1524 /// boundary fictional for bundle content.
1525 ///
1526 /// # Why a URL and not the bucket name
1527 ///
1528 /// A bucket name is not fetchable. Resolving one would need either a
1529 /// node-side bucket→origin map (another node-wide table, the same defect
1530 /// again) or R2 credentials on every box — for content that is already
1531 /// world-readable, and against a node that deliberately holds no bucket
1532 /// credential at all (see `HttpReadOnlyObjectStore`). Integrity comes from
1533 /// the content address, not the transport, so an unauthenticated origin is
1534 /// exactly as safe here as an authenticated one.
1535 ///
1536 /// The declared origin does not *replace* the node's: kamaji reads through
1537 /// to `KAMAJI_BUNDLE_ORIGIN` on a miss, which is what lets a tenant fetch
1538 /// the stock `mesofact/<ver>` serve runtime the fleet publishes once
1539 /// without republishing ~70MB into their own bucket.
1540 ///
1541 /// Appended **after** `env` — see `port`'s note on the positional codec.
1542 #[serde(default)]
1543 #[ts(optional = nullable)]
1544 pub origin: Option<String>,
1545}
1546
1547/// Lifecycle mode for a served bundle (W272 §3).
1548#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1549#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1550#[serde(rename_all = "snake_case")]
1551pub enum BundleLifecycle {
1552 /// Fork at deploy, keep resident, restart per policy — today's server
1553 /// archetype. Memory is resident for the workload's lifetime.
1554 KeepAlive,
1555
1556 /// Kamaji owns the listen socket, forks the runtime on the first connection
1557 /// (fd-passing), and reaps it after `idle_ttl` with zero connections — the
1558 /// "serverless" tier (zero memory when idle). The JIT fork/reap mechanics
1559 /// land in R599-F6; this variant only declares the intent + budget.
1560 OnDemand {
1561 /// Idle time with no live connections before kamaji reaps the process.
1562 idle_ttl: Millis,
1563 },
1564}
1565
1566impl Default for BundleLifecycle {
1567 /// Keep-alive — the resident server archetype — matches the current
1568 /// deploy-and-supervise default.
1569 fn default() -> Self {
1570 BundleLifecycle::KeepAlive
1571 }
1572}
1573
1574// ── Per-tenant passway (R852-F1 / W267 §Free-tier ingress at 10k domains) ─────
1575
1576/// Container-side / node-side path a per-tenant passway reads its PEM chain
1577/// from, when the declaration does not name one. Deliberately per-domain: two
1578/// tenants sharing a path is two tenants sharing a certificate.
1579pub const DEFAULT_TENANT_PASSWAY_CERT_DIR: &str = "/run/yah/passway/tenants";
1580
1581/// Node path of the passway binary a per-tenant passway forks, when the
1582/// declaration does not name one. Matches the path the passway image installs
1583/// to, which is what `local-driver`'s node-appliance spec also runs.
1584pub const DEFAULT_PASSWAY_COMMAND: &str = "/usr/local/bin/passway";
1585
1586/// One **cold, per-tenant passway** — a TLS terminator that serves exactly one
1587/// custom tenant domain, forked on demand by kamaji's JIT tier
1588/// (`kamaji::jit::JitRuntime`) and self-reaped when idle.
1589///
1590/// This is the declaration W267's free-tier ingress design was missing. R779
1591/// shipped every mechanism — the SNI demux that splices `:443` by ClientHello
1592/// without terminating TLS, passway's fd-3 adoption + idle self-reap, the
1593/// R2-backed cert store off raft, the per-domain DNS-01 issuer — but nothing
1594/// could *say* "there is a passway for `shop.tenant.io` at `127.0.0.1:8443`",
1595/// because kamaji's on-demand tier was reachable only through
1596/// [`MesofactServeBundle`], a mesofact-specific carrier.
1597///
1598/// ## Why a variant and not an annotated [`Workload::Container`]
1599///
1600/// The W267 **node appliance** is a container (see `Workload::Container`'s doc
1601/// comment): one resident passway per public-IP node, image-pulled, supervised
1602/// like anything else, so an archetype + annotation expressed it with no wire
1603/// change. A per-tenant passway is the opposite on every axis that decides the
1604/// question. It is **native-forked, not containerized** — kamaji's JIT tier
1605/// hands the child an inherited fd, and that path (`kamaji::jit`) forks a
1606/// process, not a container. It is **zero-resident**, so the deploy Ack means
1607/// "socket bound and armed", not "a process is running". And there are ten
1608/// thousand of them, generated from the enrollment set rather than written by
1609/// hand. Squeezing that into `Container` would mean a spec whose image is a
1610/// lie and whose supervision arm is chosen by an annotation nobody reading the
1611/// type would look for.
1612///
1613/// ## The bind string is the fd-table key
1614///
1615/// [`listen`](Self::listen) is **declared, never allocated.** It is the address
1616/// the tenant's enrollment record already names as its demux backend
1617/// (`yubaba::cert_store::Enrollment::tls_backend`), so kamaji must bind exactly
1618/// it — an allocator picking a port here would arm a socket the demux never
1619/// routes to, and the tenant's domain would resolve, handshake, and hang.
1620///
1621/// The same string is also passway's `PASSWAY_LISTEN`, and it must match **byte
1622/// for byte**: passway's socket-activation path (on by default) *panics* rather
1623/// than binding fresh when `LISTEN_FDS` is set and the seed does not take, so a
1624/// drifted string is a workload that forks and immediately dies on every
1625/// connection. [`jit_spec`](Self::jit_spec) is the reason that cannot happen —
1626/// it renders `PASSWAY_LISTEN` from this one field rather than asking a caller
1627/// to restate it, the same "derive, never re-state" rule
1628/// `yubaba::domain_admin` applies to the DNS-01 record name.
1629#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1630#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1631#[serde(deny_unknown_fields)]
1632pub struct TenantPasswayWorkload {
1633 #[serde(default)]
1634 pub schema_version: SchemaVersion,
1635
1636 /// The single custom domain this passway terminates TLS for — the SNI the
1637 /// demux matched to route here, and the hostname
1638 /// [`jit_spec`](Self::jit_spec) keys the rendered `PASSWAY_UPSTREAMS`
1639 /// entries on.
1640 pub domain: String,
1641
1642 /// `host:port` kamaji binds and holds in custody, and the address the demux
1643 /// splices this domain's bytes to. See the type doc: declared, not
1644 /// allocated, and byte-identical to `PASSWAY_LISTEN`.
1645 pub listen: String,
1646
1647 /// Plaintext backends passway forwards to after terminating TLS, as bare
1648 /// `host:port`. Rendered as `<domain>=<addr>` entries — repeated entries
1649 /// load-balance (R844-F3), which is why this is a list and not one address.
1650 ///
1651 /// Empty is legal and means "no backend yet": passway answers 503 rather
1652 /// than refusing to start, so a domain can be enrolled and issued before
1653 /// the tenant's app is placed.
1654 #[serde(default)]
1655 pub upstreams: Vec<String>,
1656
1657 /// Where the per-domain PEM pair the R2 cert store holds
1658 /// (`yubaba::cert_store`) has been materialized on the node.
1659 pub tls: TenantPasswayTls,
1660
1661 /// Idle time with no in-flight request before the process exits, leaving
1662 /// kamaji holding the socket and re-forking on the next connection.
1663 ///
1664 /// `None` means **never reap** — a long-running per-tenant passway. That is
1665 /// the shape the free tier exists to avoid (10k resident processes is the
1666 /// number W267 §"Scaling B to a free tier" set out to dissolve), and it also
1667 /// re-opens a rotation gap a cold passway does not have: a cold one re-reads
1668 /// [`tls`](Self::tls) at every cold start, while a resident one holds the
1669 /// chain it started with. Sub-second values round **up** to one second, and
1670 /// zero is not "never" — see [`idle_ttl_secs`](Self::idle_ttl_secs).
1671 ///
1672 /// No `skip_serializing_if`: this rides the positional postcard wire.
1673 #[serde(default)]
1674 #[ts(optional = nullable)]
1675 pub idle_ttl: Option<Millis>,
1676
1677 /// Node path of the passway binary to fork. `None` →
1678 /// [`DEFAULT_PASSWAY_COMMAND`].
1679 #[serde(default)]
1680 #[ts(optional = nullable)]
1681 pub command: Option<String>,
1682
1683 /// Extra environment for the forked process — the ACME/auth/health knobs
1684 /// passway reads that this type has no opinion about.
1685 ///
1686 /// **Cannot override the derived keys.** [`jit_spec`](Self::jit_spec)
1687 /// applies this map *first* and the derived
1688 /// (`PASSWAY_LISTEN`/`LISTEN_FDS`/`PASSWAY_IDLE_TTL_SECS`/
1689 /// `PASSWAY_UPSTREAMS`/`PASSWAY_TLS_*`) keys last, so an escape hatch cannot
1690 /// silently break the fd handoff — which would surface as a domain that
1691 /// hangs, not as a config error.
1692 #[serde(default)]
1693 pub env: BTreeMap<String, String>,
1694}
1695
1696/// Node-side paths of one tenant's materialized certificate pair.
1697///
1698/// Paths rather than [`SecretMount`]s: the JIT tier forks a *process*, not a
1699/// container, so there is no mount namespace to project a secret into — the
1700/// files are read from the node filesystem by the forked passway. Whoever
1701/// materializes them out of `yubaba::cert_store` owns their permissions.
1702#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1703#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1704#[serde(deny_unknown_fields)]
1705pub struct TenantPasswayTls {
1706 /// PEM chain path (`PASSWAY_TLS_CERT`).
1707 pub cert: String,
1708 /// PEM private-key path (`PASSWAY_TLS_KEY`).
1709 pub key: String,
1710}
1711
1712impl TenantPasswayTls {
1713 /// The conventional per-domain pair under
1714 /// [`DEFAULT_TENANT_PASSWAY_CERT_DIR`]: `<dir>/<domain>/{tls.crt,tls.key}`.
1715 pub fn for_domain(domain: &str) -> Self {
1716 Self {
1717 cert: format!("{DEFAULT_TENANT_PASSWAY_CERT_DIR}/{domain}/tls.crt"),
1718 key: format!("{DEFAULT_TENANT_PASSWAY_CERT_DIR}/{domain}/tls.key"),
1719 }
1720 }
1721}
1722
1723impl TenantPasswayWorkload {
1724 /// A cold per-tenant passway for `domain` on `listen`, with the
1725 /// conventional cert paths and a one-minute idle TTL.
1726 pub fn cold(domain: impl Into<String>, listen: impl Into<String>) -> Self {
1727 let domain = domain.into();
1728 Self {
1729 schema_version: SchemaVersion::V1,
1730 tls: TenantPasswayTls::for_domain(&domain),
1731 domain,
1732 listen: listen.into(),
1733 upstreams: Vec::new(),
1734 idle_ttl: Some(Millis::from_secs(60)),
1735 command: None,
1736 env: BTreeMap::new(),
1737 }
1738 }
1739
1740 /// Point this passway at `addrs` (bare `host:port`).
1741 pub fn with_upstreams<S: Into<String>>(mut self, addrs: impl IntoIterator<Item = S>) -> Self {
1742 self.upstreams = addrs.into_iter().map(Into::into).collect();
1743 self
1744 }
1745
1746 /// The passway binary this workload forks.
1747 pub fn command_path(&self) -> &str {
1748 self.command.as_deref().unwrap_or(DEFAULT_PASSWAY_COMMAND)
1749 }
1750
1751 /// `PASSWAY_IDLE_TTL_SECS`, or `None` for "never reap".
1752 ///
1753 /// Rounds **up** to one second, for the reason the bundle JIT path rounds
1754 /// up: passway reads this as an integer number of seconds, so a 500 ms TTL
1755 /// would truncate to `0` — and `0` there does not mean "reap immediately",
1756 /// it means the reap never fires. Rounding down would turn a declared cold
1757 /// workload resident without any error to read.
1758 pub fn idle_ttl_secs(&self) -> Option<u64> {
1759 self.idle_ttl.map(|t| t.as_ms().div_ceil(1000).max(1))
1760 }
1761
1762 /// `PASSWAY_UPSTREAMS` for this domain: `<domain>=<addr>` per backend,
1763 /// comma-joined. Empty when no backend is declared, which passway reads as
1764 /// "fail ready with 503".
1765 pub fn passway_upstreams(&self) -> String {
1766 self.upstreams
1767 .iter()
1768 .map(|a| format!("{}={}", self.domain, a))
1769 .collect::<Vec<_>>()
1770 .join(",")
1771 }
1772
1773 /// The [`WorkloadSpec`] kamaji's JIT runtime forks for this tenant.
1774 ///
1775 /// `id` is the kamaji workload identity (also the mesh ident and the
1776 /// custodian key). Everything else is derived from `self` — see the type
1777 /// doc for why no caller is allowed to restate `PASSWAY_LISTEN`.
1778 ///
1779 /// - `entrypoint` is the passway binary; `command` is empty, because passway
1780 /// is configured entirely by environment (it has no config-file parser).
1781 /// - `restart_policy` is [`RestartPolicy::Never`]: the JIT supervisor owns
1782 /// re-forking on the next connection, and an idle self-reap is an expected
1783 /// exit, not a crash.
1784 /// - `expose.mesh.ports` is parsed back off [`listen`](Self::listen) rather
1785 /// than carried separately, so the declared port cannot drift from the
1786 /// bound one.
1787 /// - `LISTEN_FDS=1` is set here as well as by the JIT supervisor. That is
1788 /// deliberate redundancy, not a duplicate: it makes the spec truthful
1789 /// about how this process expects to get its socket to anyone reading the
1790 /// spec alone, and setting it twice to the same value is inert.
1791 pub fn jit_spec(&self, id: &str) -> WorkloadSpec {
1792 let mut env: BTreeMap<String, String> = self.env.clone();
1793 // Derived keys go last: an `env` escape hatch must not be able to break
1794 // the fd handoff (see the field doc).
1795 env.insert("PASSWAY_LISTEN".into(), self.listen.clone());
1796 env.insert("LISTEN_FDS".into(), "1".into());
1797 env.insert("PASSWAY_TLS_MODE".into(), "manual".into());
1798 env.insert("PASSWAY_TLS_CERT".into(), self.tls.cert.clone());
1799 env.insert("PASSWAY_TLS_KEY".into(), self.tls.key.clone());
1800 env.insert("PASSWAY_UPSTREAM_SOURCE".into(), "static".into());
1801 env.insert("PASSWAY_UPSTREAMS".into(), self.passway_upstreams());
1802 match self.idle_ttl_secs() {
1803 Some(secs) => {
1804 env.insert("PASSWAY_IDLE_TTL_SECS".into(), secs.to_string());
1805 }
1806 // Unset, not `0` — passway reads an absent variable as "never
1807 // reap", and `0` as a zero-second timer that fires immediately.
1808 None => {
1809 env.remove("PASSWAY_IDLE_TTL_SECS");
1810 }
1811 }
1812
1813 WorkloadSpec {
1814 schema_version: SchemaVersion::V1,
1815 name: id.to_string(),
1816 image: ImageRef {
1817 // Identity metadata only — the JIT tier forks a node binary and
1818 // pulls nothing, exactly like the bundle-serving native path.
1819 registry: "passway".into(),
1820 repository: format!("tenant/{}", self.domain),
1821 tag: "jit".into(),
1822 digest: "sha256:0000000000000000000000000000000000000000000000000000000000000000"
1823 .into(),
1824 },
1825 tier: TierTag("infra".into()),
1826 tenant: TenantId::singleton(),
1827 namespace: NamespaceId::singleton(),
1828 replicas: 1,
1829 entrypoint: Some(vec![self.command_path().to_string()]),
1830 command: Some(vec![]),
1831 workdir: None,
1832 user: None,
1833 env: env
1834 .into_iter()
1835 .map(|(name, value)| EnvVar {
1836 name,
1837 value: EnvValue::Literal { value },
1838 })
1839 .collect(),
1840 secrets: vec![],
1841 volumes: vec![],
1842 resources: ResourceLimits {
1843 memory_mb: 64,
1844 cpu_millis: 256,
1845 ephemeral_storage_mb: 64,
1846 },
1847 depends_on: vec![],
1848 requires: vec![],
1849 // No probe: a `TcpConnect` probe would dial the held socket and
1850 // fork the process on every interval, defeating the idle reap. The
1851 // JIT bundle path refuses one for the same reason.
1852 healthcheck: None,
1853 restart_policy: RestartPolicy::Never,
1854 archetype: None,
1855 stop_policy: StopPolicy {
1856 signal: 15,
1857 grace_period: Millis::from_secs(5),
1858 },
1859 expose: ExposeSpec {
1860 mesh: MeshExpose {
1861 identity: MeshIdent(id.to_string()),
1862 ports: MeshExpose::anonymous_ports(self.listen_port()),
1863 allow_from: vec![],
1864 },
1865 public: None,
1866 operator: None,
1867 },
1868 labels: Default::default(),
1869 annotations: Default::default(),
1870 }
1871 }
1872
1873 /// Port half of [`listen`](Self::listen), when it parses.
1874 pub fn listen_port(&self) -> Option<u16> {
1875 self.listen
1876 .rsplit_once(':')
1877 .and_then(|(_, p)| p.parse::<u16>().ok())
1878 }
1879}
1880
1881/// Build step that produces the static artifact published by a
1882/// `mesofact-static` workload.
1883///
1884/// **`deny_unknown_fields` is load-bearing (R658-B1).** TOML scopes every key
1885/// written after a table header into that table, so a manifest that puts a
1886/// top-level `MesofactStaticWorkload` field — `routes` was the one that
1887/// actually happened — below `[build]` silently produces `build.routes`
1888/// instead. Without this attribute serde discards the stray key, the
1889/// top-level field falls back to its default (or fails with a `missing field`
1890/// error pointing at the wrong place), and the manifest deploys with a
1891/// declaration nobody honours. Every real `workload.toml` in the camp and the
1892/// CLI's own `yah cloud site init` scaffold carried exactly that shape for
1893/// months without a single reader noticing.
1894///
1895/// The cost is forward-compat: a manifest carrying a `[build]` key this binary
1896/// doesn't know is a hard parse error, not an ignored key. That is deliberate.
1897/// A build config is a small, slow-moving, load-bearing table — a key that
1898/// silently does nothing is worse here than one that refuses to load, because
1899/// the failure surfaces as a wrong artifact rather than an error.
1900///
1901/// Note `deny_unknown_fields` is inert for the postcard kamaji wire, which is
1902/// non-self-describing and positional — this only constrains TOML/JSON.
1903#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1904#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1905#[serde(deny_unknown_fields)]
1906pub struct BuildConfig {
1907 /// Shell command run from the manifest's directory, e.g. `"bun run build"`.
1908 ///
1909 /// **Absent means "this project has no external bundler step" (R838-B1)**,
1910 /// not "run nothing by accident". `mesofact new`'s scaffold deliberately
1911 /// omits it — the in-process pipeline (`mesofact-dev` / `mesofact-build`)
1912 /// produces `out_dir` with no third binary, no package manager and no Node
1913 /// — so requiring it here made every scaffolded project's manifest fail to
1914 /// load through this envelope. Setting it opts back out to a shell command,
1915 /// which is what a project with its own bundler wants.
1916 ///
1917 /// Consumers were already written for this: `read_workload_build`
1918 /// (app/yah/cli/src/cloud.rs) has always typed it `Option<String>` and
1919 /// `yah cloud bundle build` only needs it under `--run-build`; the bundle
1920 /// sync arm refuses `None` by name. `MesofactStaticReconciler::
1921 /// rebuild_static` skips the build step for `None` — the same thing it
1922 /// already did for a workload with no `workload.toml` at all.
1923 ///
1924 /// WIRE NOTE: this is `Option<String>` on the postcard kamaji wire, so it
1925 /// costs a leading `0x00`/`0x01` tag byte that the bare `String` did not
1926 /// have. A pre-R838 node decoding a new frame fails loudly (the string's
1927 /// length byte is not a valid `Option` tag) rather than silently reading a
1928 /// shifted field — which is why this is `Option` and not a `#[serde(default)]`
1929 /// empty `String` sentinel. Not a `cluster_epochs` surface: those hash the
1930 /// raft modules and the openraft pin, not `workload_spec`.
1931 #[serde(default)]
1932 pub command: Option<String>,
1933
1934 /// Output directory (relative to the manifest) the reconciler uploads.
1935 pub out_dir: PathBuf,
1936
1937 /// Data-only re-render command (W225 §3 "revalidate"), run from the
1938 /// manifest's directory against the **already-built** `out_dir` — no
1939 /// bundler. `{route}` is substituted with the invalidated route pattern,
1940 /// e.g. `"../../../../scripts/mesofact-build.sh render . --route {route}
1941 /// --all"` (R746-F9 — resolves a prebuilt binary rather than shelling to
1942 /// cargo, which cannot even find the package from a site's own dir).
1943 /// Absent → a revalidate dispatch republishes `out_dir` as-is.
1944 #[serde(default)]
1945 pub render_command: Option<String>,
1946}
1947
1948// ── BuildMode ─────────────────────────────────────────────────────────────────
1949
1950/// Where the build command runs for a `mesofact-static` workload.
1951///
1952/// The two-role split encodes the F7 design decision: build/publish is a
1953/// **transient job** (runs once, exits, GC'd); SSR/SPA serving is a separate
1954/// **long-lived companion container** (optional, only for dynamic pages). A
1955/// single merged "mesofact container" is the trap — in cloud, CI builds the
1956/// artifact, R2+CDN serve it, and a distinct worker handles any SSR.
1957///
1958/// Default: `HostSide` — mesofact-dev runs the build on the host and publishes
1959/// to the tier's object store. No container overhead; compatible with dev and
1960/// sim tiers.
1961#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, TS)]
1962#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1963#[serde(rename_all = "snake_case")]
1964pub enum BuildMode {
1965 /// Build command runs on the host (mesofact-dev watcher). The watcher
1966 /// publishes the output to the tier's object store (DistPointer for dev,
1967 /// MinIO for sim). Compatible with all tiers; zero container overhead.
1968 #[default]
1969 HostSide,
1970
1971 /// Build runs inside a transient container matching the CI image. Higher
1972 /// fidelity (environment matches CI exactly); costs image pull +
1973 /// container cold-start. Required for cloud/HA where no mesofact-dev
1974 /// watcher is running on the host.
1975 InContainer {
1976 /// Container image that runs the build (e.g. `"ghcr.io/org/app-build:v1.2"`).
1977 /// Must have the build toolchain installed. The container is started with
1978 /// the workspace root bind-mounted, runs `build.command`, uploads
1979 /// `build.out_dir` to the object store, then exits.
1980 image: ImageRef,
1981 },
1982}
1983
1984// ── AlmanacManifest ───────────────────────────────────────────────────────────
1985
1986/// An observable endpoint the almanac scheduler probes to check readiness.
1987///
1988/// Used for both inputs (checked before the run) and outputs (verified after
1989/// a successful run to confirm the job produced something reachable).
1990/// The probe is intentionally lightweight — no S3 SigV4, no xlb-net discovery
1991/// required; a simple TCP connect or HTTP GET is enough for the dev/sim tier.
1992#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
1993#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
1994#[serde(rename_all = "snake_case")]
1995pub enum AlmanacTarget {
1996 /// Issue an HTTP GET to `url`; ready when the server responds with
1997 /// `expect_status` (default: any 2xx).
1998 Http {
1999 url: String,
2000 #[ts(optional = nullable)]
2001 expect_status: Option<u16>,
2002 },
2003
2004 /// Establish a TCP connection to `host:port`; ready when the connect
2005 /// succeeds. Used for non-HTTP services (e.g. MinIO API on port 9000)
2006 /// and as a lighter probe when an HTTP endpoint isn't stable yet.
2007 Tcp { host: String, port: u16 },
2008}
2009
2010/// What the almanac scheduler does when a precondition check fails.
2011///
2012/// The F9 design decision: `WaitWithTimeout` is the default. Fail-fast is
2013/// too brittle for the sim tier (containers may still be cold-starting);
2014/// requeue-with-no-ceiling can block the scheduler indefinitely. The
2015/// recommended timeout for sim is the container spinup budget (~5 s cold,
2016/// ~1 s warm): set `timeout` to a few seconds, then let the retry cadence
2017/// handle transient glitches.
2018#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2019#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2020#[serde(rename_all = "snake_case")]
2021pub enum NotReadyPolicy {
2022 /// Wait up to `timeout` for all preconditions to pass before aborting
2023 /// the run. The run is skipped (not rescheduled); the next cadence tick
2024 /// will retry. Suitable when targets occasionally lag at startup.
2025 WaitWithTimeout {
2026 /// How long to wait for each precondition to become reachable. The
2027 /// scheduler polls with a short sleep between attempts.
2028 timeout: Millis,
2029 },
2030
2031 /// Abort immediately if any precondition check fails. Suitable for
2032 /// integration-test harnesses where a missing dependency is always a
2033 /// hard error.
2034 FailFast,
2035
2036 /// Requeue with exponential backoff up to `max_attempts` times. After
2037 /// exhaustion the run is marked failed. Suitable for cloud/HA where
2038 /// transient dependency outages are expected.
2039 Requeue {
2040 /// Maximum number of requeue attempts before the run is marked failed.
2041 max_attempts: u32,
2042 /// Initial backoff between attempts, in milliseconds.
2043 backoff: Millis,
2044 },
2045}
2046
2047impl Default for NotReadyPolicy {
2048 /// Default is `WaitWithTimeout { timeout: 5 seconds }` — matches the
2049 /// container spinup budget for the sim tier (few-second cold, sub-second warm).
2050 fn default() -> Self {
2051 Self::WaitWithTimeout { timeout: Millis::from_secs(5) }
2052 }
2053}
2054
2055/// When the almanac scheduler triggers a run.
2056#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2057#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2058#[serde(rename_all = "snake_case")]
2059pub enum Cadence {
2060 /// Run once at first opportunity, then never again.
2061 Once,
2062
2063 /// Run repeatedly with a fixed interval between the end of one run and
2064 /// the start of the next. Equivalent to `sleep N && run` in a loop.
2065 Every {
2066 /// Minimum time between consecutive run completions.
2067 interval: Millis,
2068 },
2069
2070 /// Run on a UTC cron schedule (standard 5-field expression, e.g.
2071 /// `"0 */6 * * *"` for every 6 hours). The scheduler evaluates the
2072 /// expression relative to UTC midnight.
2073 Cron { expression: String },
2074}
2075
2076/// `kind = "almanac"` manifest — a declared data-pipeline job.
2077///
2078/// An almanac job is the generalisation of the OpenRouter refresher
2079/// (`spawn_almanac_refresher`): it declares its I/O contract explicitly so
2080/// the orchestrator can enforce preconditions before each run and verify
2081/// outputs afterward. The degenerate case (no inputs, no app target, cron
2082/// schedule) is exactly the OpenRouter JSON-cache refresher.
2083///
2084/// Lifecycle:
2085/// 1. Cadence tick fires.
2086/// 2. Scheduler probes every `inputs` target. If any fail → apply
2087/// `not_ready_policy`.
2088/// 3. Command runs (`sh -c command` from the workload directory).
2089/// 4. Scheduler probes every `outputs` target. Failure → mark run as
2090/// failed but do not retry.
2091/// 5. Any workloads listed in `invalidates` receive a cache-bust signal
2092/// (implementation detail of the orchestrator; in camp this is a
2093/// rebuild trigger on the mesofact-dev watcher).
2094#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2095#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2096pub struct AlmanacManifest {
2097 /// Wire-format version. Always `V1` today.
2098 pub schema_version: SchemaVersion,
2099
2100 /// Shell command executed via `sh -c` from the workload directory.
2101 pub command: String,
2102
2103 /// When to run.
2104 pub cadence: Cadence,
2105
2106 /// Input targets that must be reachable before the command runs.
2107 /// Empty list → no precondition checks (degenerate case).
2108 #[serde(default)]
2109 pub inputs: Vec<AlmanacTarget>,
2110
2111 /// Output targets verified after a successful run.
2112 /// Empty list → no post-run verification.
2113 #[serde(default)]
2114 pub outputs: Vec<AlmanacTarget>,
2115
2116 /// What to do when a precondition check fails.
2117 /// Default: `WaitWithTimeout { timeout: 5000ms }`.
2118 #[serde(default)]
2119 pub not_ready_policy: NotReadyPolicy,
2120
2121 /// Mesh identities of workloads to notify after a successful run.
2122 /// The orchestrator sends a cache-bust signal to each entry so
2123 /// downstream consumers can reload their data (e.g. mesofact-dev
2124 /// triggers a rebuild when the OpenRouter cache refreshes).
2125 /// Empty list → no downstream invalidation.
2126 #[serde(default)]
2127 pub invalidates: Vec<MeshIdent>,
2128}
2129
2130// ── StaticAssetWorkload ───────────────────────────────────────────────────────
2131
2132/// BLAKE3 content hash expressed as exactly 64 ASCII hex digits.
2133///
2134/// This is the content-address key for every file in the static-asset catalog.
2135/// Deserialization rejects values that do not conform — 64 hex chars, case
2136/// insensitive. Mismatch between the recorded hash and the source file halts
2137/// the upload step in the reconciler.
2138#[derive(Debug, Clone, PartialEq, Eq, Serialize, TS)]
2139#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2140#[ts(type = "string")]
2141pub struct BlakeHash(pub String);
2142
2143impl<'de> Deserialize<'de> for BlakeHash {
2144 fn deserialize<D>(de: D) -> Result<Self, D::Error>
2145 where
2146 D: serde::Deserializer<'de>,
2147 {
2148 let s = String::deserialize(de)?;
2149 if s.len() != 64 || !s.bytes().all(|b| b.is_ascii_hexdigit()) {
2150 return Err(serde::de::Error::custom(format!(
2151 "blake3 hash must be exactly 64 hex digits, got {:?}",
2152 s
2153 )));
2154 }
2155 Ok(BlakeHash(s))
2156 }
2157}
2158
2159// ── License & FetchSource (W164) ──────────────────────────────────────────────
2160
2161/// Closed-set, parse-time-enforced license tag. Mirrors the workspace
2162/// permissive-license rule (MIT / Apache-2.0 / BSD-2/3-Clause / ISC). Adding a
2163/// variant is an explicit schema change — non-permissive strings
2164/// (`"GPL-3.0"`, `"AGPL"`, etc.) fail at serde-deserialize before any shape
2165/// validator runs.
2166///
2167/// Shared between `asset.derive.fetch.license` (W164, required) and a future
2168/// `almanac::ReleaseSource.license` migration (R438-F10, optional).
2169#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2170#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2171#[serde(rename_all = "kebab-case")]
2172pub enum License {
2173 Mit,
2174 Apache2,
2175 Bsd2Clause,
2176 Bsd3Clause,
2177 Isc,
2178}
2179
2180/// Shared fetch primitive — usable by `asset.derive` today, and by Almanac's
2181/// `ReleaseSource` after a follow-up migration (R438-F10). Defined once in
2182/// workload-spec so both consumers reject the same set of non-permissive
2183/// licenses.
2184///
2185/// The `blake3` hash pins the upstream bytes; mismatch at fetch time is a hard
2186/// error in the reconciler. The `license` field is **required** here — every
2187/// derived asset must declare its upstream license. If/when Almanac adopts
2188/// `FetchSource`, the Almanac side may wrap this in a struct with
2189/// `Option<License>` since release manifests have no distribution license per
2190/// se.
2191#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2192#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2193pub struct FetchSource {
2194 /// Upstream URL fetched verbatim. Reconciler retry policy is configured
2195 /// elsewhere (R438-F11); the URL itself is opaque to workload-spec.
2196 pub url: String,
2197
2198 /// Expected BLAKE3 hash of the fetched bytes (64 hex characters). The
2199 /// reconciler verifies this after download and aborts on mismatch.
2200 pub blake3: BlakeHash,
2201
2202 /// Upstream license. Closed-set, parse-time enforced.
2203 pub license: License,
2204}
2205
2206/// Optional transform applied after a [`FetchSource`] download, lowering to a
2207/// `ForgeCommand::Subprocess` via the recipe loader (R438-T4). The transform's
2208/// output is content-addressed by the entry's `blake3` (the recipe runs only
2209/// when the cache misses).
2210#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2211#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2212pub struct TransformSpec {
2213 /// Named recipe under `.yah/qed/transforms/<recipe>.toml`. Loader rejects
2214 /// missing recipes at materialize time.
2215 pub recipe: String,
2216
2217 /// `{{key}}` substitutions passed to the recipe argv at element
2218 /// granularity (no shell, no string concat). Empty when the recipe is
2219 /// fully parameterless.
2220 #[serde(default)]
2221 pub params: BTreeMap<String, String>,
2222}
2223
2224/// W212/R518: the committed derivation lock — the in-tree action-cache
2225/// receipt. `input_hash` is the input-addressed derivation key computed over
2226/// the complete declared input set (fetched-input pin ⊕ recipe-file bytes ⊕
2227/// invocation params ⊕ schema version); `output_blake3` is what those inputs
2228/// produced (== the entry's `blake3`). The reconciler skips the entire build
2229/// (no fetch, no transform, no PUT) when the lock matches the inputs recomputed
2230/// from the current pins and the bucket already holds the output — the
2231/// Nix-substituter / Bazel-remote-cache behaviour. Written by the R510 bind
2232/// path from the reconciler's `discovered_input_hash:<filename>` output; the
2233/// `git diff` on this block is the receipt that the derivation rolled.
2234#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2235#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2236pub struct DeriveLock {
2237 /// Input-addressed derivation key (BLAKE3 hex). A change to any declared
2238 /// input flips this, so a stale lock never produces a false skip.
2239 pub input_hash: String,
2240 /// Output the locked inputs produced (BLAKE3 hex; equals the entry's
2241 /// `blake3`). Carried so the lock is a self-contained action-cache entry.
2242 pub output_blake3: String,
2243}
2244
2245/// Provenance chain for a derived asset: required `fetch` step, optional
2246/// `transform` step. Materialized bytes replace `AssetEntry.source` for the
2247/// rest of the static-asset reconcile loop.
2248#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2249#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2250pub struct AssetDerive {
2251 /// Upstream fetch — URL + content-pin + license.
2252 pub fetch: FetchSource,
2253
2254 /// Post-fetch transform. `None` → the fetched bytes ARE the asset
2255 /// (entry `blake3` must match fetch `blake3`).
2256 #[serde(default)]
2257 #[ts(optional = nullable)]
2258 pub transform: Option<TransformSpec>,
2259
2260 /// W212/R518: committed derivation lock (input-addressed action-cache
2261 /// receipt). Absent until the first successful build writes it via the
2262 /// bind path. When present and current, enables the substituter-style
2263 /// build skip.
2264 #[serde(default)]
2265 #[ts(optional = nullable)]
2266 pub lock: Option<DeriveLock>,
2267}
2268
2269/// A single file entry in the static-asset catalog.
2270///
2271/// One `[[asset]]` row per bucket object. Multiple rows for different variants
2272/// (e.g. q5 and q4 whisper models) are fine — each declares its own filename
2273/// and hash. The reconciler treats the catalog as exhaustive and append-only:
2274/// new rows trigger a PUT; removed rows surface as drift (never a DELETE).
2275///
2276/// **Source-vs-derive XOR.** Exactly one of `source` or `derive` must be set.
2277/// Legacy local-bytes assets keep `source = "..."`; W164 derived assets set
2278/// `[asset.derive]` instead. [`validate::shape_static_asset`] enforces the
2279/// XOR; both-set and neither-set are hard `ShapeError::Field`.
2280#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
2281#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2282pub struct AssetEntry {
2283 /// Destination path within the bucket, e.g.
2284 /// `"whisper/distil-large-v3-q5_1.bin"`. Must be unique in the catalog.
2285 /// Used as the S3 object key by the reconciler.
2286 pub filename: String,
2287
2288 /// Path to a local source file, relative to the `workload.toml` directory.
2289 /// Mutually exclusive with `derive`.
2290 #[serde(default)]
2291 #[ts(optional = nullable)]
2292 pub source: Option<PathBuf>,
2293
2294 /// Declared fetch (+ optional transform) provenance chain. The reconciler
2295 /// materializes the bytes into a content-addressed cache; the cache path
2296 /// then replaces `source` for the rest of the upload pipeline. Mutually
2297 /// exclusive with `source`.
2298 #[serde(default)]
2299 #[ts(optional = nullable)]
2300 pub derive: Option<AssetDerive>,
2301
2302 /// Expected BLAKE3 hash of the *final* asset bytes (64 hex characters).
2303 /// For `source` mode, this is hashed before upload. For `derive` mode,
2304 /// it's the post-transform (or post-fetch when no transform) output.
2305 /// Mismatch aborts the upload.
2306 pub blake3: BlakeHash,
2307}
2308
2309/// `kind = "static-asset"` payload — content-addressed bucket catalog.
2310///
2311/// The reconciler makes the bucket match the `[[asset]]` list exactly
2312/// (append-only: new rows → PUT; removed rows → drift report, not DELETE).
2313/// Rollback is pointer-flip via `mirror.toml [asset_aliases]` — bytes never
2314/// move during rollback.
2315///
2316/// **Closed-catalog invariant**: every value in `[aliases]` must be a
2317/// `filename` that exists in `[[asset]]`. Enforced by
2318/// [`validate::shape_static_asset`]. Mirror overrides (`[asset_aliases]` in
2319/// `mirror.toml`) are bound by the same rule — the alias graph can only
2320/// resolve to filenames already in the catalog.
2321#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2322#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2323pub struct StaticAssetWorkload {
2324 /// Wire-format version. Always `V1` today.
2325 pub schema_version: SchemaVersion,
2326
2327 /// Exhaustive catalog of files this component manages in the bucket.
2328 ///
2329 /// Named `asset` on disk (TOML `[[asset]]` array-of-tables) to follow TOML
2330 /// convention; accessed as `.assets` in Rust code.
2331 #[serde(rename = "asset", default)]
2332 pub assets: Vec<AssetEntry>,
2333
2334 /// Canonical logical-name → filename mappings for this component.
2335 ///
2336 /// Values must be filenames present in `assets` — validated by
2337 /// [`validate::shape_static_asset`]. Mirror files may override individual
2338 /// entries via `[asset_aliases]` but may never reference filenames absent
2339 /// from this catalog.
2340 #[serde(default)]
2341 pub aliases: BTreeMap<String, String>,
2342}
2343
2344// ── Lifecycle archetype (R572-F1 / W244) ───────────────────────────────────────
2345
2346/// Explicit lifecycle archetype for a `kind = "container"` workload (W244).
2347///
2348/// The question that actually matters to a scheduler: *"can I kill this and
2349/// recreate it somewhere else?"* Before this field existed, the answer was
2350/// inferred per-spec from `volumes.is_empty()` + `restart_policy` — fragile
2351/// absence-as-policy, the same trap W243 calls out on the node-taint side.
2352/// This type makes the answer structural instead of guessed.
2353///
2354/// This ticket (R572-F1) adds the discriminator only. The reconciler does not
2355/// yet branch on it (R572-F4) and neither does the scheduler (R572-F5).
2356#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2357#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2358#[serde(rename_all = "kebab-case")]
2359pub enum LifecycleArchetype {
2360 /// k8s analogue: Deployment. Stateless and fungible — the scheduler may
2361 /// move it, scale it to N replicas, or restart it on a different node
2362 /// with zero consequence. Drainable.
2363 Server,
2364
2365 /// k8s analogue: StatefulSet. Stable identity + a volume that must
2366 /// follow it; at most one live instance. Not drainable — the reconciler
2367 /// must not schedule it onto a different node. Example: a postgres peer,
2368 /// headscale (W267/R591).
2369 Appliance,
2370
2371 /// k8s analogue: Job. Runs to completion with declared inputs/outputs,
2372 /// then is gone — no steady-state identity. `almanac` is the first
2373 /// job-family member; forge runs (`WorkloadSpec::for_forge`, used by QED)
2374 /// are the `container`-kind instance of this archetype.
2375 Job,
2376}
2377
2378impl LifecycleArchetype {
2379 /// Every variant, in declaration order. Exists so a consumer can enumerate
2380 /// the archetypes without hand-maintaining a parallel list — the taint
2381 /// vocabulary in `cloud::config::taint_effect` is built from this, so
2382 /// adding a fourth archetype extends the set of live repel keys for free.
2383 pub const ALL: [LifecycleArchetype; 3] = [Self::Server, Self::Appliance, Self::Job];
2384
2385 /// The repel-taint key for this archetype (R572-F5). A node carrying the
2386 /// taint `"no-<key>"` **absolutely** rejects workloads of this class.
2387 ///
2388 /// Examples: `Server` → `"server"` (repelled by `"no-server"`);
2389 /// `Appliance` → `"appliance"` (repelled by `"no-appliance"`).
2390 ///
2391 /// W305/R742-T4: there is no toleration. Earlier prose here and in
2392 /// `cloud::config` called this "repel-unless-tolerate"; the `unless` was
2393 /// never built, and reading it as a preference is what made `no-appliance`
2394 /// on the dev Pis look advisory when it was an unconditional block.
2395 pub fn taint_key(&self) -> &'static str {
2396 match self {
2397 Self::Server => "server",
2398 Self::Appliance => "appliance",
2399 Self::Job => "job",
2400 }
2401 }
2402
2403 /// The pre-R572 inference this field replaces, kept only to give
2404 /// `WorkloadSpec::effective_archetype` a behavior-preserving fallback for
2405 /// specs written before this field existed (`archetype: None`).
2406 ///
2407 /// A volume that must follow the workload is the strongest signal of
2408 /// durable state → [`Self::Appliance`]. Absent that, `RestartPolicy::Never`
2409 /// is the existing forge/run-once convention (see
2410 /// [`RestartPolicy::Never`]'s doc comment) → [`Self::Job`]. Everything
2411 /// else defaults to the common case, [`Self::Server`].
2412 fn infer(volumes: &[VolumeMount], restart_policy: &RestartPolicy) -> Self {
2413 if !volumes.is_empty() {
2414 LifecycleArchetype::Appliance
2415 } else if matches!(restart_policy, RestartPolicy::Never) {
2416 LifecycleArchetype::Job
2417 } else {
2418 LifecycleArchetype::Server
2419 }
2420 }
2421}
2422
2423/// Whether a placement group may be drained off its node (W338 §"Placement
2424/// consequences" 2): false as soon as **any** member is an Appliance.
2425///
2426/// The set-valued form of the per-workload question. A `Server` bound to an
2427/// Appliance by a `local` edge has to move with it or not at all, so draining it
2428/// alone breaks the group the same way placing it alone would.
2429///
2430/// # Why this lives here and not in `cloud`
2431///
2432/// R860-T4 landed it in `cloud::config`, which is the right layer for the
2433/// *scheduler* — but R860-T6 needs the identical predicate on the **node** side,
2434/// in `drain_workloads`, and yubaba deliberately has no runtime dependency on
2435/// cloud (R374-F3 moved `local-driver` out of cloud precisely to avoid that
2436/// reverse edge; `cloud` is a dev-dependency of yubaba only). Placement and
2437/// drain disagreeing about drainability is exactly the drift this predicate
2438/// exists to prevent, so it belongs in the crate they both already depend on.
2439/// `cloud::config::group_is_drainable` delegates here and keeps its signature.
2440pub fn group_is_drainable(members: &[WorkloadSpec]) -> bool {
2441 !members
2442 .iter()
2443 .any(|m| m.effective_archetype() == LifecycleArchetype::Appliance)
2444}
2445
2446// ── Requirements (R860-T1 / W338) ─────────────────────────────────────────────
2447
2448/// Which providers count as satisfying a [`Requirement`] (W338).
2449///
2450/// One of the two independent axes a requirement carries. `depends_on` could
2451/// only ever say "someone, somewhere, is Ready" — which is the wrong answer for
2452/// a provider that must open the *same file on the same filesystem* as its
2453/// requirer (the headscale sqlite replicator, W338's motivating case). Locality
2454/// makes co-location a declared property instead of something arranged outside
2455/// the spec by a systemd unit.
2456#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2457#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2458#[serde(rename_all = "kebab-case")]
2459pub enum Locality {
2460 /// Any Ready provider service discovery can reach, anywhere in the mesh.
2461 /// Exactly what a [`WorkloadSpec::depends_on`] entry means today, which is
2462 /// why it is the default — folding `depends_on` into `requires` must not
2463 /// change any existing spec's meaning.
2464 Anywhere,
2465
2466 /// A provider on this node satisfies it; otherwise a remote one does.
2467 ///
2468 /// **Never blocks placement.** This is the "at least one wherever this app
2469 /// runs" shape — a local replica is preferred, a remote one is acceptable,
2470 /// and nothing is refused for want of either.
2471 PreferLocal,
2472
2473 /// Only a provider on **this node** satisfies it. A true sidecar edge: the
2474 /// requirer and the provider form a placement group that must be placed
2475 /// together and must move together.
2476 Local,
2477}
2478
2479impl Default for Locality {
2480 fn default() -> Self {
2481 Locality::Anywhere
2482 }
2483}
2484
2485/// What to do when nothing satisfies a [`Requirement`] (W338).
2486///
2487/// The second axis, deliberately independent of [`Locality`]: all six
2488/// combinations are meaningful, and `prefer-local` + `self` is where a
2489/// DaemonSet falls out as a consequence rather than as a fourth archetype.
2490///
2491/// Kept a plain two-value enum rather than a data-carrying variant precisely so
2492/// the two axes stay independent — the provider's spec rides on
2493/// [`Requirement::provides`] instead.
2494#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
2495#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2496#[serde(rename_all = "kebab-case")]
2497pub enum Supply {
2498 /// Someone else declares and deploys the provider; block until it appears,
2499 /// under the existing healthcheck-sum deadline. Today's `depends_on`
2500 /// behaviour, and the default.
2501 Wait,
2502
2503 /// This workload carries the provider's spec in [`Requirement::provides`]
2504 /// and stands one up where the locality demands. Torn down with its
2505 /// requirer.
2506 ///
2507 /// Wire value is `"self"` — `Self` is a Rust keyword, so the variant is
2508 /// spelled `SelfProvision` and renamed on the wire.
2509 #[serde(rename = "self")]
2510 SelfProvision,
2511}
2512
2513impl Default for Supply {
2514 fn default() -> Self {
2515 Supply::Wait
2516 }
2517}
2518
2519/// One thing a workload needs before it can run (W338).
2520///
2521/// Widens [`WorkloadSpec::depends_on`] rather than adding a second concept
2522/// beside it: a requirement names an identity and answers the two questions the
2523/// bare ident list cannot — *which providers count* ([`Locality`]) and *what to
2524/// do when none exists* ([`Supply`]).
2525///
2526/// Each member of a group keeps its own mesh identity. A provider that may be
2527/// satisfied remotely must be independently discoverable, so a requirement is
2528/// an *edge between two identities*, never a way to collapse several workloads
2529/// under one. Nothing about addressing, teardown-by-identity or the
2530/// service-record rail changes.
2531#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2532#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2533pub struct Requirement {
2534 /// Mesh identity of the provider. The same currency a
2535 /// [`WorkloadSpec::depends_on`] entry is written in.
2536 pub ident: MeshIdent,
2537
2538 /// Which providers count as satisfying this. Defaults to
2539 /// [`Locality::Anywhere`], the `depends_on` meaning.
2540 #[serde(default)]
2541 pub locality: Locality,
2542
2543 /// What to do when nothing satisfies it. Defaults to [`Supply::Wait`], the
2544 /// `depends_on` meaning.
2545 #[serde(default)]
2546 pub supply: Supply,
2547
2548 /// The provider's own spec, carried here when `supply = "self"`.
2549 ///
2550 /// Required for [`Supply::SelfProvision`] and forbidden for
2551 /// [`Supply::Wait`] — a `wait` requirement names a provider someone else
2552 /// declares, so a spec here would have no owner. Both directions are
2553 /// enforced by [`validate::shape`].
2554 ///
2555 /// Boxed because this makes [`WorkloadSpec`] recursive. The recursion is
2556 /// bounded at **depth 1**: a `provides` spec may not itself carry a
2557 /// `self`-supplied requirement (also enforced in [`validate::shape`]), so
2558 /// composition stays a requirer plus its immediate providers rather than an
2559 /// arbitrarily deep tree.
2560 #[serde(default)]
2561 #[ts(optional = nullable)]
2562 pub provides: Option<Box<WorkloadSpec>>,
2563}
2564
2565// ── WorkloadSpec ──────────────────────────────────────────────────────────────
2566
2567/// Complete typed description of a containerd workload handed to yubaba over
2568/// RPC. This is also the payload of the `kind = "container"` variant of
2569/// [`Workload`] on disk.
2570///
2571/// Yubaba never accepts compose YAML on its RPC surface — agents, the desktop,
2572/// and operator CLIs all hand yubaba `WorkloadSpec` values. See the arch doc
2573/// for the validation layers and evolution rules.
2574///
2575/// @yah:ticket(R860-T1, "Spec: Requirement { ident, locality, supply } + `requires` on WorkloadSpec, depends_on as back-compat projection")
2576/// @yah:status(review)
2577/// @yah:phase(P1)
2578/// @yah:at(2026-09-05T18:28:59Z)
2579/// @yah:assignee(agent:bundle-anthropic-ashguard)
2580/// @yah:parent(R860)
2581/// @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`.")
2582/// @yah:verify("bash scripts/check-schema-drift.sh && bash scripts/check-workload-spec-ts.sh && cargo test -p workload-spec")
2583/// @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).")
2584/// @arch:see(.yah/docs/working/W338-workload-dependencies-and-appliance-composition.md)
2585/// @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).")
2586/// @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.")
2587/// @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.")
2588/// @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.")
2589/// @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).")
2590/// @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.")
2591/// @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.")
2592/// @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.")
2593/// @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.")
2594/// @yah:next("Commit the three regenerated artifacts (see gotcha) — that is the only thing standing between this ticket and both drift gates going green.")
2595/// @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.")
2596/// @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.")
2597/// @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.")
2598/// @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).")
2599/// @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.")
2600/// @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.")
2601/// @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.")
2602/// @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.")
2603/// @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.")
2604/// @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.")
2605/// @yah:verify("cargo test -p yah-workload-spec (run inside oss/yah-base): 171 lib / 98 integration / 0 failed, vs a 156 / 98 baseline.")
2606/// @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.")
2607/// @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.")
2608/// @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\".")
2609/// @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.")
2610/// @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.")
2611/// @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.")
2612/// @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.")
2613/// @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.")
2614/// @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![],`.")
2615/// @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.")
2616/// @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`.")
2617/// @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.")
2618/// @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.")
2619/// @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.")
2620/// @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.")
2621/// @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.")
2622#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
2623#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
2624pub struct WorkloadSpec {
2625 /// Wire-format version; always `V1` today. Present at the top level so
2626 /// rolling clusters can detect and migrate across schema generations.
2627 pub schema_version: SchemaVersion,
2628
2629 /// DNS-friendly workload name, e.g. `"noisetable-api"`. Regex:
2630 /// `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, length ≤ 63.
2631 pub name: String,
2632
2633 /// Container image to pull.
2634 pub image: ImageRef,
2635
2636 /// Tier tag controlling admission control and mesh filtering.
2637 pub tier: TierTag,
2638
2639 /// Tenant **isolation** axis (W206). Separates operators' workloads at the
2640 /// network / DB / mesh-identity level. Defaults to [`TenantId::singleton`]
2641 /// for specs that predate the axis, so single-tenant clusters keep every
2642 /// isolation primitive a no-op. Orthogonal to [`Self::tier`] (class) and
2643 /// [`Self::namespace`] (routing).
2644 #[serde(default = "TenantId::singleton")]
2645 pub tenant: TenantId,
2646
2647 /// Namespace **routing/naming** axis (W206). A pure naming key — never
2648 /// affects isolation; disambiguates DNS names and selects config root /
2649 /// provider zone within a tenant. Defaults to [`NamespaceId::singleton`].
2650 #[serde(default = "NamespaceId::singleton")]
2651 pub namespace: NamespaceId,
2652
2653 /// Target replica count. `0` registers the workload without deploying it.
2654 /// Range: 0–100 (cluster-wide cap; operator can raise it).
2655 pub replicas: u32,
2656
2657 /// Override the image's `CMD`. `None` leaves the image default.
2658 #[ts(optional = nullable)]
2659 pub command: Option<Vec<String>>,
2660
2661 /// Override the image's `ENTRYPOINT`. `None` leaves the image default.
2662 #[ts(optional = nullable)]
2663 pub entrypoint: Option<Vec<String>>,
2664
2665 /// Working directory inside the container.
2666 #[ts(optional = nullable)]
2667 pub workdir: Option<PathBuf>,
2668
2669 /// User to run as, e.g. `"1000:1000"` or `"appuser"`.
2670 #[ts(optional = nullable)]
2671 pub user: Option<String>,
2672
2673 /// Environment variables. Values may be literals, secret refs, or
2674 /// mesh-address references resolved by yubaba at deploy time.
2675 #[serde(default)]
2676 pub env: Vec<EnvVar>,
2677
2678 /// Secret mounts. Values never appear in the spec JSON — only references.
2679 #[serde(default)]
2680 pub secrets: Vec<SecretMount>,
2681
2682 /// Volume mounts.
2683 #[serde(default)]
2684 pub volumes: Vec<VolumeMount>,
2685
2686 /// Hard resource caps enforced by containerd/cgroups.
2687 pub resources: ResourceLimits,
2688
2689 /// Mesh idents that must reach `Ready` before this workload starts.
2690 ///
2691 /// Superseded by [`Self::requires`] (R860-T1 / W338) and kept as-is for
2692 /// wire compatibility: every entry here means exactly
2693 /// `Locality::Anywhere` + `Supply::Wait`. Callers MUST NOT read this
2694 /// directly — use [`WorkloadSpec::effective_requirements`], which folds
2695 /// both fields into one list.
2696 #[serde(default)]
2697 pub depends_on: Vec<MeshIdent>,
2698
2699 /// What this workload needs before it can run, with locality and supply
2700 /// (R860-T1 / W338). The widened form of [`Self::depends_on`].
2701 ///
2702 /// Additive: this field did not exist before R860-T1, and a spec that omits
2703 /// it is unchanged in meaning. Callers MUST NOT read this directly either —
2704 /// [`WorkloadSpec::effective_requirements`] is the only supported read,
2705 /// because a spec written against the old vocabulary carries its
2706 /// requirements in `depends_on` and would otherwise look requirement-free.
2707 ///
2708 /// Vocabulary only: nothing branches on `locality` or `supply` yet. The
2709 /// deploy gate (R860-T2) and the placement group (R860-T4) are separate,
2710 /// later tickets — this field alone changes no runtime behaviour, exactly
2711 /// as [`Self::archetype`] landed in R572-F1.
2712 #[serde(default)]
2713 pub requires: Vec<Requirement>,
2714
2715 /// Container liveness/readiness probe.
2716 #[ts(optional = nullable)]
2717 pub healthcheck: Option<Healthcheck>,
2718
2719 /// What yubaba does when the container exits.
2720 pub restart_policy: RestartPolicy,
2721
2722 /// Explicit lifecycle archetype (R572-F1 / W244): `server`, `appliance`,
2723 /// or `job`. `None` means the spec predates this field (or the author
2724 /// didn't set it) — callers MUST NOT read this directly to decide
2725 /// drainability; use [`WorkloadSpec::effective_archetype`], which falls
2726 /// back to the pre-R572 `volumes`/`restart_policy` inference so no
2727 /// existing spec's effective meaning changes.
2728 ///
2729 /// Additive: this field did not exist before R572-F1. Reconciler (F4)
2730 /// and scheduler (F5) branching on the resolved archetype are separate,
2731 /// later tickets — this field alone changes no runtime behavior.
2732 #[serde(default)]
2733 #[ts(optional = nullable)]
2734 pub archetype: Option<LifecycleArchetype>,
2735
2736 /// Graceful shutdown configuration.
2737 pub stop_policy: StopPolicy,
2738
2739 /// Network exposure configuration — mesh, public, and operator channels
2740 /// are independent and can be set in any combination.
2741 pub expose: ExposeSpec,
2742
2743 /// OCI-style labels, passed through to the container. Opaque to yubaba.
2744 #[serde(default)]
2745 pub labels: HashMap<String, String>,
2746
2747 /// Yah-specific metadata, conventionally prefixed `yah.*`. Opaque to
2748 /// yubaba beyond `yah.forge=true` which suppresses the Never-restart guard.
2749 #[serde(default)]
2750 pub annotations: HashMap<String, String>,
2751}
2752
2753impl WorkloadSpec {
2754 /// Build a `WorkloadSpec` for a forge run.
2755 ///
2756 /// Sets the conventional forge fields in one place so callers cannot
2757 /// forget any of them:
2758 ///
2759 /// - `restart_policy = Never`
2760 /// - `archetype = Some(LifecycleArchetype::Job)` — a forge run is
2761 /// exactly the `container`-kind instance of the job archetype (W244);
2762 /// set explicitly rather than left to infer since this constructor
2763 /// knows its own shape
2764 /// - `expose.public = None`, `expose.operator = None`
2765 /// - `expose.mesh.identity = "forge.<forge_id>"`
2766 /// - `annotations["yah.forge"] = "true"` (suppresses the shape warning)
2767 /// - `tier` and `image` come from the caller; `ports` becomes the mesh
2768 /// port list (empty is valid — forge jobs often don't expose ports)
2769 ///
2770 /// All other fields are set to safe defaults. Callers can mutate the
2771 /// returned value to fill in `command`, `env`, `resources`, etc.
2772 pub fn for_forge(
2773 forge_id: &str,
2774 image: ImageRef,
2775 tier: TierTag,
2776 ports: Vec<u16>,
2777 ) -> Self {
2778 let mut annotations = HashMap::new();
2779 annotations.insert("yah.forge".into(), "true".into());
2780 // The placement floor, kept distinct from the cgroup ceiling below.
2781 // Without this, admission reads the 32 GiB ceiling as the amount of
2782 // RAM a node must have — see `memory_request_mb` for what that cost.
2783 annotations.insert(
2784 MEMORY_REQUEST_ANNOTATION.into(),
2785 FORGE_MEMORY_REQUEST_MB.to_string(),
2786 );
2787
2788 WorkloadSpec {
2789 schema_version: SchemaVersion::V1,
2790 // NB: DNS-label safe (no dots) — `check_name` validation rejects
2791 // dots here. The container_id derives from this; the state-poll
2792 // keys off `expose.mesh.identity` (`forge.<id>`) instead, so those
2793 // two must be reconciled at the read path, NOT by dotting the name
2794 // (see R590-B9).
2795 name: format!("forge-{forge_id}"),
2796 image,
2797 tier,
2798 tenant: TenantId::singleton(),
2799 namespace: NamespaceId::singleton(),
2800 replicas: 1,
2801 command: None,
2802 entrypoint: None,
2803 workdir: None,
2804 user: None,
2805 env: vec![],
2806 secrets: vec![],
2807 volumes: vec![],
2808 resources: ResourceLimits {
2809 // R590-B10: forge workloads are BUILDS (cargo, buildkit, a
2810 // from-source V8 checkout+compile), not tiny services. The old
2811 // 256 MB placeholder became a hard cgroup memory.limit in
2812 // build_oci_spec and SIGKILL'd the rusty-v8 build mid-checkout
2813 // (git checkout of third_party/icu died of signal 9) — the
2814 // more so because /tmp is a RAM-backed tmpfs, so the source
2815 // tree counts against this limit too. 32 GiB is a bounded
2816 // ceiling that fits the V8 build's >12 GB peak with headroom,
2817 // protects the host from a runaway (vs truly unlimited), and is
2818 // above physical RAM on smaller build-workers (⇒ effectively
2819 // unlimited there).
2820 //
2821 // That last clause is only true while this stays a CEILING. It
2822 // was also the placement floor until the annotation set above
2823 // split the two, which made every build-worker under 32 GiB
2824 // unschedulable — the story is on `memory_request_mb`.
2825 memory_mb: FORGE_MEMORY_LIMIT_MB,
2826 cpu_millis: 512,
2827 ephemeral_storage_mb: 512,
2828 },
2829 depends_on: vec![],
2830 requires: vec![],
2831 healthcheck: None,
2832 restart_policy: RestartPolicy::Never,
2833 archetype: Some(LifecycleArchetype::Job),
2834 stop_policy: StopPolicy {
2835 signal: 15,
2836 grace_period: Millis::from_secs(30),
2837 },
2838 expose: ExposeSpec {
2839 mesh: MeshExpose {
2840 identity: MeshIdent(format!("forge.{forge_id}")),
2841 // A forge job's ports come from a caller holding bare
2842 // numbers (a job exposes what its image exposes), so they
2843 // stay unnamed — `kamaji::name_anonymous_ports` names them.
2844 ports: MeshExpose::anonymous_ports(ports),
2845 allow_from: vec![],
2846 },
2847 public: None,
2848 operator: None,
2849 },
2850 labels: HashMap::new(),
2851 annotations,
2852 }
2853 }
2854
2855 /// Whether this workload requests the **host network namespace** rather
2856 /// than an isolated one.
2857 ///
2858 /// Opt-in via `annotations["yah.network"] == "host"` (see
2859 /// [`HOST_NETWORK_ANNOTATION`] / [`HOST_NETWORK_VALUE`]). Default is the
2860 /// isolated netns every other workload gets — host networking is a
2861 /// privileged escape hatch for the few infra workloads that must bind a
2862 /// host port so an on-host ingress (e.g. a Cloudflare tunnel reaching
2863 /// `127.0.0.1:<port>`) can route to them without CNI/bridge plumbing.
2864 ///
2865 /// The backend (kamaji) is responsible for **guarding** this: host
2866 /// networking is only honoured for `tier == "infra"` workloads; a
2867 /// non-infra workload that sets the annotation is rejected at deploy. See
2868 /// `validate_spec_for_constable`.
2869 pub fn wants_host_network(&self) -> bool {
2870 self.annotations
2871 .get(HOST_NETWORK_ANNOTATION)
2872 .map(|v| v == HOST_NETWORK_VALUE)
2873 .unwrap_or(false)
2874 }
2875
2876 /// Resolve the lifecycle archetype (R572-F1 / W244): the explicit
2877 /// [`Self::archetype`] if set, otherwise the pre-R572 inference from
2878 /// `volumes`/`restart_policy` this field replaces.
2879 ///
2880 /// This is the one seam callers should use to ask "can I kill and
2881 /// reschedule this?" — it is intentionally the *only* place that
2882 /// implements the fallback, so behavior for pre-existing specs (no
2883 /// `archetype` on disk) is identical to what it was before this field
2884 /// existed. Consumers (reconciler R572-F4, scheduler R572-F5) branch on
2885 /// the return value; this crate does not itself change any reconciler or
2886 /// scheduler behavior.
2887 pub fn effective_archetype(&self) -> LifecycleArchetype {
2888 self.archetype
2889 .unwrap_or_else(|| LifecycleArchetype::infer(&self.volumes, &self.restart_policy))
2890 }
2891
2892 /// Resolve what this workload needs (R860-T1 / W338): [`Self::requires`],
2893 /// then every [`Self::depends_on`] ident not already named there, folded
2894 /// into the `Anywhere` + `Wait` requirement that a bare `depends_on` entry
2895 /// has always meant.
2896 ///
2897 /// This is the one seam callers should use to ask "what does this workload
2898 /// need?" — it is intentionally the *only* place that implements the fold,
2899 /// so a spec written before `requires` existed keeps its exact previous
2900 /// meaning. Callers MUST NOT read [`Self::requires`] or
2901 /// [`Self::depends_on`] directly: reading either alone silently drops half
2902 /// the requirements of any spec that uses both.
2903 ///
2904 /// Deduplicated by ident, and `requires` wins — an ident named in both is
2905 /// the author restating a dependency with a locality, not two separate
2906 /// edges. Consumers (the deploy gate R860-T2, the placement group R860-T4)
2907 /// branch on the return value; this crate does not itself change any
2908 /// deploy or placement behaviour.
2909 pub fn effective_requirements(&self) -> Vec<Requirement> {
2910 let mut out = self.requires.clone();
2911 for ident in &self.depends_on {
2912 if out.iter().any(|req| &req.ident == ident) {
2913 continue;
2914 }
2915 out.push(Requirement {
2916 ident: ident.clone(),
2917 locality: Locality::Anywhere,
2918 supply: Supply::Wait,
2919 provides: None,
2920 });
2921 }
2922 out
2923 }
2924
2925 /// Fully-qualified mesh identity `<tenant>/<namespace>/<name>` (W206 /
2926 /// R558-F3), where `<name>` is this workload's [`MeshExpose::identity`].
2927 ///
2928 /// Within a tenant, workloads still address each other by the short
2929 /// identity (namespace disambiguates only on collision); the FQN is what
2930 /// makes the identity unambiguous across tenants and is exactly what a
2931 /// [`MeshPeer::CrossTenant`] grant names.
2932 pub fn fq_mesh_identity(&self) -> String {
2933 format!(
2934 "{}/{}/{}",
2935 self.tenant.0, self.namespace.0, self.expose.mesh.identity.0
2936 )
2937 }
2938
2939 /// The taint this workload requires its node to carry, if any (R594-F2 /
2940 /// W267 sovereign public ingress).
2941 ///
2942 /// Opt-in via `annotations["yah.placement.requires-taint"] = "<taint
2943 /// name>"` (see [`REQUIRES_TAINT_ANNOTATION`]) — same annotation-based,
2944 /// zero-blast-radius shape as [`Self::wants_host_network`], chosen so
2945 /// declaring this requirement does not force a struct-literal edit at
2946 /// every existing `WorkloadSpec { .. }` construction site the way a new
2947 /// plain field would (see R572-F1's handoff: ~26 sites for one field).
2948 ///
2949 /// Both halves have since landed: `MachineConfig.taints` (R572-F3) and the
2950 /// scheduler's affinity check in `cloud::config::RequiredSpec::matches`
2951 /// (R572-F5), which requires the key in the node's `taints` **or**
2952 /// `mesh_tags`.
2953 ///
2954 /// A key named here is one of only two ways a node taint can influence
2955 /// placement — the other is the `no-<archetype>` repulsion form. W305/
2956 /// R742-T4 makes `yah cloud validate` reject any node taint that is
2957 /// neither, so a new affinity key must be added to
2958 /// `cloud::config::AFFINITY_TAINT_KEYS` alongside the workload that
2959 /// requires it.
2960 ///
2961 /// The public-ingress appliance (W267) is the first user: a
2962 /// `kind = "container"` workload with `archetype =
2963 /// Some(LifecycleArchetype::Appliance)` and
2964 /// `requires_taint() == Some(PUBLIC_IP_TAINT)`, so yubaba may one day
2965 /// place it only on machines carrying the `"public-ip"` taint and kamaji
2966 /// supervises it like any other container (no new `Workload` variant —
2967 /// see [`Workload::Container`]'s doc comment).
2968 pub fn requires_taint(&self) -> Option<&str> {
2969 self.annotations
2970 .get(REQUIRES_TAINT_ANNOTATION)
2971 .map(String::as_str)
2972 }
2973
2974 /// The memory (MiB) a scheduler must find on a node before placing this
2975 /// workload — its **request**, as distinct from [`ResourceLimits::memory_mb`],
2976 /// which is a **ceiling** the backend turns into a cgroup `memory.max`.
2977 ///
2978 /// Opt-in via `annotations["yah.placement.memory-request-mb"]` (see
2979 /// [`MEMORY_REQUEST_ANNOTATION`]); absent or unparseable falls back to
2980 /// `resources.memory_mb`, so every spec that does not set it is admitted
2981 /// exactly as it was before this accessor existed.
2982 ///
2983 /// # Why the two numbers must not be the same one
2984 ///
2985 /// A limit answers "kill it past here"; a request answers "don't start it
2986 /// somewhere smaller than here". Generous is the safe direction for the
2987 /// first and the unschedulable direction for the second, so one field
2988 /// serving both makes a deliberately-roomy ceiling into an admission floor.
2989 ///
2990 /// That is not hypothetical: [`WorkloadSpec::for_forge`] sets a 32 GiB
2991 /// ceiling explicitly reasoned as "above physical RAM on smaller
2992 /// build-workers ⇒ effectively unlimited there" (R590-B10), and
2993 /// `CloudConfig::admit_workload` fed that same 32768 in as the R572-F5
2994 /// capacity floor. Every build-worker under 32 GiB — the three 8 GiB Pi-5s
2995 /// and the 16 GiB us-west-003 — became structurally unadmittable for *any*
2996 /// offloaded qed step, leaving one 47 GiB node as the fleet's only legal
2997 /// target for remote CI. This is R590-B10's own recorded follow-up
2998 /// ("thread a per-step memory request … instead of a blanket forge
2999 /// default"), reduced to the seam that closes the bug.
3000 ///
3001 /// An annotation rather than a new `ResourceLimits` field on purpose:
3002 /// `WorkloadSpec` crosses a postcard wire that is positional and
3003 /// carries no field names (R590-B3), so adding a field would break decode
3004 /// on every fleet node still running an older kamaji. `annotations` is an
3005 /// existing map — an extra key rides it safely, and admission already
3006 /// reads placement inputs from exactly there
3007 /// ([`Self::requires_taint`], the R594 node-selector).
3008 pub fn memory_request_mb(&self) -> u32 {
3009 self.annotations
3010 .get(MEMORY_REQUEST_ANNOTATION)
3011 .and_then(|v| v.trim().parse::<u32>().ok())
3012 .unwrap_or(self.resources.memory_mb)
3013 }
3014
3015 /// Whether this workload must be run by kamaji's **native** (fork+exec)
3016 /// backend on the node's own userland, rather than by a container backend
3017 /// (R577-T1 / W254).
3018 ///
3019 /// Opt-in via `annotations["yah.exec"] == "native"` (see
3020 /// [`NATIVE_EXEC_ANNOTATION`] / [`NATIVE_EXEC_VALUE`]) — the same
3021 /// annotation-shaped, zero-blast-radius marker as
3022 /// [`Self::wants_host_network`] and [`Self::requires_taint`], chosen over
3023 /// a new plain field for the reason R572-F1 recorded: a field forces a
3024 /// struct-literal edit at every existing construction site and an
3025 /// exhaustive-match update in `kamaji-proto`'s codec, and this marker
3026 /// needs neither.
3027 ///
3028 /// # Why an annotation and not a runtime enum on the wire
3029 ///
3030 /// The remote-execution wire already carries exactly one workload shape —
3031 /// `Workload::Container(WorkloadSpec)` — and every layer between the
3032 /// dispatcher and the node (yubaba admission, mesh assignment, log
3033 /// ingest, produced-file retrieval, teardown) is written against it. A
3034 /// Darwin build differs from a Linux build in *one* respect: there is no
3035 /// container that can host it, because you cannot containerize the Darwin
3036 /// kernel. Marking that one difference keeps the rest of the path shared
3037 /// instead of growing a parallel `exec_native` RPC that would have to
3038 /// re-implement all of it.
3039 ///
3040 /// `image` stays populated for a native workload and is **identity
3041 /// metadata only** — nothing is pulled; the native backend resolves argv
3042 /// from `entrypoint` + `command` (container semantics) and execs it on
3043 /// the host.
3044 pub fn wants_native_exec(&self) -> bool {
3045 self.annotations
3046 .get(NATIVE_EXEC_ANNOTATION)
3047 .map(|v| v == NATIVE_EXEC_VALUE)
3048 .unwrap_or(false)
3049 }
3050
3051 /// Whether this workload must be run by kamaji's **microVM** backend —
3052 /// booted in its own KVM guest with its own kernel, rather than sharing the
3053 /// host kernel with every other workload on the node (R605-F8 / W325 §5).
3054 ///
3055 /// Opt-in via `annotations["yah.exec"] == "microvm"` (see
3056 /// [`NATIVE_EXEC_ANNOTATION`] / [`MICROVM_EXEC_VALUE`]).
3057 ///
3058 /// # Why the *same* key as native exec, not a new one
3059 ///
3060 /// W325's Shape A calls this "a sibling branch on a new annotation value",
3061 /// and the value — not the key — is the whole point. `yah.exec` names the
3062 /// execution substrate, and a workload has exactly one:
3063 ///
3064 /// | `yah.exec` | substrate | kernel | isolation |
3065 /// |---|---|---|---|
3066 /// | *(absent)* | container backend | host's | namespaces + cgroup |
3067 /// | `native` | fork+exec on the host | host's | **none** |
3068 /// | `microvm` | KVM guest | **its own** | hardware |
3069 ///
3070 /// A second key (`yah.isolation = microvm`, say) would make
3071 /// `yah.exec = native` + `yah.isolation = microvm` *expressible*, and
3072 /// therefore something a dispatcher could emit and a backend would have to
3073 /// refuse — exactly the refusal `validate_native_exec_spec` already has to
3074 /// carry for the `yah.sandbox` pair, and for the same avoidable reason. A
3075 /// map key holds one value, so on this key the three substrates are
3076 /// mutually exclusive *by construction*: there is no spec on which both
3077 /// this and [`Self::wants_native_exec`] return `true`, and
3078 /// `exec_substrate_markers_are_mutually_exclusive_by_construction` pins
3079 /// that.
3080 ///
3081 /// # What the marker does and does not promise
3082 ///
3083 /// Like every marker on this struct it is **inert metadata** — it declares
3084 /// intent and nothing more. Whether a node can honour it is a node
3085 /// capability question (`/dev/kvm`, a guest kernel, a rootfs; see W325 §4),
3086 /// and a node whose kamaji has no microVM backend configured **refuses**
3087 /// the deploy rather than falling back to a container. That refusal is
3088 /// deliberate and mirrors R577-T1's: a caller asking for microVM isolation
3089 /// is asking for the one property a container cannot provide, so silently
3090 /// downgrading it would return success while delivering the thing the
3091 /// caller specifically declined.
3092 ///
3093 /// `image` is identity metadata only, as it is for native exec — nothing is
3094 /// pulled. The guest's root filesystem comes from the node's configured
3095 /// rootfs image, and argv is resolved from `entrypoint` + `command` with
3096 /// container semantics, so one spec shape drives all three substrates.
3097 pub fn wants_microvm(&self) -> bool {
3098 self.annotations
3099 .get(NATIVE_EXEC_ANNOTATION)
3100 .map(|v| v == MICROVM_EXEC_VALUE)
3101 .unwrap_or(false)
3102 }
3103
3104 /// Whether this workload builds its **own unprivileged container sandbox**
3105 /// inside the one the backend gives it, and therefore needs the two
3106 /// capabilities plus the `no_new_privs` relaxation that setting up a
3107 /// user namespace requires (R636-B2).
3108 ///
3109 /// Opt-in via `annotations["yah.sandbox"] == "nested"` (see
3110 /// [`NESTED_SANDBOX_ANNOTATION`] / [`NESTED_SANDBOX_VALUE`]) — the same
3111 /// annotation-shaped, zero-blast-radius marker as
3112 /// [`Self::wants_host_network`] and [`Self::wants_native_exec`].
3113 ///
3114 /// # What it actually grants, and why exactly that
3115 ///
3116 /// Rootless BuildKit (the only user today: remote `build-image` steps
3117 /// dispatch `moby/buildkit:*-rootless`) boots through `rootlesskit`, which
3118 /// must map a range of sub-uids into a fresh user namespace. It does that
3119 /// by exec'ing the **setuid-root** helpers `newuidmap` / `newgidmap`, so
3120 /// it needs `CAP_SETUID` + `CAP_SETGID` in the bounding set *and*
3121 /// `noNewPrivileges = false` (with `no_new_privs` on, the kernel silently
3122 /// strips the setuid bit and the helper fails with "Could not set caps").
3123 ///
3124 /// Each of those three was measured on us-west-002 to be **individually
3125 /// necessary** — dropping any one of them puts `rootlesskit` back to
3126 /// failing before the first layer:
3127 ///
3128 /// | grant | `rootlesskit` result |
3129 /// |---|---|
3130 /// | baseline (`CAP_NET_BIND_SERVICE` only, `nnp` on) | `fork/exec /usr/bin/newuidmap: operation not permitted` |
3131 /// | `+CAP_SETUID` only, `nnp` off | `fork/exec /usr/bin/newgidmap: operation not permitted` |
3132 /// | `+CAP_SETUID +CAP_SETGID`, `nnp` **on** | `newuidmap: Could not set caps` |
3133 /// | `+CAP_SETUID +CAP_SETGID`, `nnp` off | starts; build runs to completion |
3134 ///
3135 /// It is deliberately *not* `CAP_SYS_ADMIN`: a non-rootless buildkitd
3136 /// would need that instead, which is a far wider grant. Emptying
3137 /// `/etc/subuid` to force `rootlesskit`'s single-mapping path does not
3138 /// avoid the helpers either — it just fails earlier with "No subuid
3139 /// ranges found".
3140 ///
3141 /// **The backend guards this.** Like host networking, it is honoured only
3142 /// for `tier == "infra"` workloads; a non-infra workload that sets the
3143 /// annotation is rejected at deploy. Every other workload keeps the
3144 /// `CAP_NET_BIND_SERVICE`-only, `no_new_privs` baseline.
3145 ///
3146 /// # Mutually exclusive with [`Self::wants_native_exec`]
3147 ///
3148 /// This grant is defined in terms of an **OCI process spec** — a
3149 /// capability set and a `noNewPrivileges` bit. A native (fork+exec)
3150 /// workload has no OCI spec, so there is nothing to apply it to; kamaji
3151 /// refuses a spec carrying both markers rather than accepting a request
3152 /// for widened privileges and silently dropping it (R577-T1 owns that
3153 /// refusal). The two are independent *annotations* — neither implies the
3154 /// other, which is what
3155 /// `nested_sandbox_marker_is_independent_of_the_other_markers` pins — but
3156 /// they are not a legal *pair*.
3157 ///
3158 /// If a future runtime does have a sandbox worth widening (a MacVM under
3159 /// W254, say), give it its own annotation rather than relaxing that
3160 /// refusal. The grant this marker names is `CAP_SETUID` + `CAP_SETGID` +
3161 /// `no_new_privs` off and nothing else; letting it mean a different
3162 /// privilege set per backend would make "what does `yah.sandbox=nested`
3163 /// grant?" unanswerable without knowing which backend received it, which
3164 /// is precisely what a security-relevant marker must not be.
3165 pub fn wants_nested_sandbox(&self) -> bool {
3166 self.annotations
3167 .get(NESTED_SANDBOX_ANNOTATION)
3168 .map(|v| v == NESTED_SANDBOX_VALUE)
3169 .unwrap_or(false)
3170 }
3171
3172 /// The durability tier this workload declares for its own state, if it
3173 /// declares one at all (R850-P4).
3174 ///
3175 /// `Ok(None)` and `Ok(Some(tier: DurabilityTier::None))` are **different
3176 /// answers and must stay different**: the first is "nobody said", the
3177 /// second is "somebody looked and decided not to". A named volume with no
3178 /// declaration is the shape that loses every byte when its node dies, and
3179 /// collapsing the two would let the analyzer report that case in the same
3180 /// words as a deliberately-ephemeral cache.
3181 ///
3182 /// Declared as annotations rather than fields, for the reason
3183 /// [`Self::requires_taint`] and [`Self::memory_request_mb`] already record:
3184 /// `WorkloadSpec` crosses a positional postcard wire carrying no field
3185 /// names (R590-B3), so a new field breaks decode on every fleet node still
3186 /// running an older kamaji, and forces a struct-literal edit at every
3187 /// construction site.
3188 ///
3189 /// ```toml
3190 /// [annotations]
3191 /// "yah.durability.tier" = "stream" # none|snapshot|dedup|stream
3192 /// "yah.durability.engine" = "turso" # required by every tier but "none"
3193 /// "yah.durability.store" = "s3://yah-backups/noisetable-account"
3194 /// "yah.durability.subjects" = "accounts.db,passkeys.db,sessions.db"
3195 /// "yah.durability.rpo-seconds" = "120" # stream only
3196 /// ```
3197 ///
3198 /// # Why `engine` and `subjects` are not optional (R850-F1)
3199 ///
3200 /// The tier vocabulary is `turso-backup`-shaped, and P4 shipped it on a
3201 /// *generic* `WorkloadSpec` — so a Postgres appliance could declare `tier =
3202 /// "stream"` and mean something no code in this tree can do. `engine` makes
3203 /// that claim explicit and refusable at parse time rather than at 3am.
3204 ///
3205 /// `subjects` exists because a restore has a *file* as its unit and a
3206 /// workload has a *volume*. The driving case (R850) is one process with
3207 /// three turso databases inside one named volume; "restore the volume" is
3208 /// not a thing turso-backup can do, and guessing which files in a directory
3209 /// are databases is guessing about the only copy of somebody's data. Paths
3210 /// are volume-relative — the same string the analyzer prints and the
3211 /// hydrate helper joins onto the host volume root — and are validated
3212 /// against traversal, because they name a host path something will write to.
3213 ///
3214 /// # What is and is not wired
3215 ///
3216 /// This accessor plus [`validate::shape`]'s check on it is the whole of the
3217 /// runtime effect today: **declaring a tier does not yet cause a backup to
3218 /// happen.** `turso-backup` implements all three tiers
3219 /// ([`DurabilityTier::Snapshot`] = its tier 1a, [`DurabilityTier::Dedup`] =
3220 /// 1b, [`DurabilityTier::Stream`] = 2 with restore-by-frame-replay) and,
3221 /// since R850-F1, the fencing epoch a hydrate must hold
3222 /// (`turso_backup::claim`). Nothing in yubaba's reconciler calls into any of
3223 /// it yet.
3224 ///
3225 /// Until that lands, the declaration's value is exactly that
3226 /// `cloud::topology` can tell an operator, *before* the topology is
3227 /// committed, which of their stateful workloads has no second copy of its
3228 /// bytes anywhere.
3229 pub fn durability(&self) -> Result<Option<Durability>, DurabilityDeclError> {
3230 let Some(raw) = self.annotations.get(DURABILITY_TIER_ANNOTATION) else {
3231 // A store or an RPO without a tier is a half-written declaration,
3232 // and reading it as "undeclared" is how a typo'd tier key becomes
3233 // silent data loss.
3234 for orphan in [
3235 DURABILITY_STORE_ANNOTATION,
3236 DURABILITY_RPO_ANNOTATION,
3237 DURABILITY_STATE_MB_ANNOTATION,
3238 DURABILITY_ENGINE_ANNOTATION,
3239 DURABILITY_SUBJECTS_ANNOTATION,
3240 ] {
3241 if self.annotations.contains_key(orphan) {
3242 return Err(DurabilityDeclError::OrphanKey { key: orphan });
3243 }
3244 }
3245 return Ok(None);
3246 };
3247
3248 let tier = DurabilityTier::parse(raw.trim()).ok_or_else(|| {
3249 DurabilityDeclError::UnknownTier {
3250 value: raw.clone(),
3251 }
3252 })?;
3253
3254 let store = self
3255 .annotations
3256 .get(DURABILITY_STORE_ANNOTATION)
3257 .map(|s| s.trim().to_string())
3258 .filter(|s| !s.is_empty());
3259
3260 // A tier that ships bytes somewhere needs to name the somewhere.
3261 // Defaulting it would put the only copy of a database in a bucket
3262 // nobody chose.
3263 if tier.ships_bytes() && store.is_none() {
3264 return Err(DurabilityDeclError::MissingStore { tier });
3265 }
3266 if !tier.ships_bytes() && store.is_some() {
3267 return Err(DurabilityDeclError::StoreWithoutTier);
3268 }
3269
3270 let rpo_seconds = match self.annotations.get(DURABILITY_RPO_ANNOTATION) {
3271 None => None,
3272 Some(v) => {
3273 if tier != DurabilityTier::Stream {
3274 return Err(DurabilityDeclError::RpoOnNonStreamTier { tier });
3275 }
3276 Some(v.trim().parse::<u32>().map_err(|_| {
3277 DurabilityDeclError::UnparseableRpo { value: v.clone() }
3278 })?)
3279 }
3280 };
3281
3282 let state_mb = match self.annotations.get(DURABILITY_STATE_MB_ANNOTATION) {
3283 None => None,
3284 Some(v) => Some(v.trim().parse::<u32>().map_err(|_| {
3285 DurabilityDeclError::UnparseableStateMb { value: v.clone() }
3286 })?),
3287 };
3288
3289 // R850-F1: the engine axis. Required by every tier that ships bytes,
3290 // because the three tier names are turso-backup's and a spec that means
3291 // something else must say so rather than be discovered at restore time.
3292 let engine = match self.annotations.get(DURABILITY_ENGINE_ANNOTATION) {
3293 Some(v) => {
3294 let e = DurabilityEngine::parse(v.trim()).ok_or_else(|| {
3295 DurabilityDeclError::UnknownEngine {
3296 value: v.clone(),
3297 }
3298 })?;
3299 if !tier.ships_bytes() {
3300 return Err(DurabilityDeclError::EngineWithoutTier);
3301 }
3302 Some(e)
3303 }
3304 None if tier.ships_bytes() => return Err(DurabilityDeclError::MissingEngine { tier }),
3305 None => None,
3306 };
3307
3308 let subjects = match self.annotations.get(DURABILITY_SUBJECTS_ANNOTATION) {
3309 Some(v) => {
3310 if !tier.ships_bytes() {
3311 return Err(DurabilityDeclError::SubjectsWithoutTier);
3312 }
3313 parse_durability_subjects(v)?
3314 }
3315 None if tier.ships_bytes() => {
3316 return Err(DurabilityDeclError::MissingSubjects { tier })
3317 }
3318 None => Vec::new(),
3319 };
3320
3321 Ok(Some(Durability {
3322 tier,
3323 engine,
3324 store,
3325 subjects,
3326 rpo_seconds,
3327 state_mb,
3328 }))
3329 }
3330}
3331
3332/// Split and validate [`DURABILITY_SUBJECTS_ANNOTATION`].
3333///
3334/// Every rule here exists because the result is joined onto a host directory
3335/// (`/var/lib/yah/kamaji/volumes/<name>`) by something that then *writes* to
3336/// it. An absolute path or a `..` component would put a restore outside the
3337/// volume it was scoped to, so those are refused by name rather than
3338/// normalized — silently rewriting a path a human typed is how you restore the
3339/// right bytes to the wrong place.
3340fn parse_durability_subjects(raw: &str) -> Result<Vec<String>, DurabilityDeclError> {
3341 let mut out = Vec::new();
3342 for part in raw.split(',') {
3343 let s = part.trim();
3344 if s.is_empty() {
3345 return Err(DurabilityDeclError::EmptySubject);
3346 }
3347 if s.starts_with('/') || s.starts_with('\\') || s.contains(':') {
3348 return Err(DurabilityDeclError::AbsoluteSubject {
3349 subject: s.to_string(),
3350 });
3351 }
3352 if s.split('/').any(|c| c == "." || c == "..") {
3353 return Err(DurabilityDeclError::TraversingSubject {
3354 subject: s.to_string(),
3355 });
3356 }
3357 if out.contains(&s.to_string()) {
3358 return Err(DurabilityDeclError::DuplicateSubject {
3359 subject: s.to_string(),
3360 });
3361 }
3362 out.push(s.to_string());
3363 }
3364 Ok(out)
3365}
3366
3367/// Which database engine a [`DurabilityTier`]'s three tier names refer to
3368/// (R850-F1).
3369///
3370/// One variant today, and that is the point: the tier vocabulary was minted
3371/// from `turso-backup`'s implementation, so an appliance running anything else
3372/// gets a refusal at parse time instead of a tier nothing can honour. Adding an
3373/// engine means adding a restore path, not adding a string.
3374#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
3375#[serde(rename_all = "snake_case")]
3376pub enum DurabilityEngine {
3377 /// Turso / libSQL, via `turso-backup`. `snapshot` is its tier 1a `VACUUM
3378 /// INTO`, `dedup` its tier 1b page-dedup, `stream` its tier 2 WAL-frame
3379 /// streaming with restore-by-frame-replay.
3380 Turso,
3381}
3382
3383impl DurabilityEngine {
3384 fn parse(raw: &str) -> Option<Self> {
3385 match raw {
3386 "turso" => Some(Self::Turso),
3387 _ => None,
3388 }
3389 }
3390
3391 /// The wire/TOML spelling, so a diagnostic and the file it points at agree.
3392 pub fn as_str(&self) -> &'static str {
3393 match self {
3394 Self::Turso => "turso",
3395 }
3396 }
3397}
3398
3399impl fmt::Display for DurabilityEngine {
3400 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3401 f.write_str(self.as_str())
3402 }
3403}
3404
3405/// A workload's declared durability tier — where a second copy of its state
3406/// lives, and how far behind that copy is allowed to be (R850-P4).
3407///
3408/// The three non-`None` variants name `turso-backup`'s three implemented
3409/// tiers. They are spelled here rather than imported because `workload-spec`
3410/// is a leaf crate every fleet node links and `turso-backup` is a service-side
3411/// dependency; the coupling that matters is the vocabulary, not the types.
3412#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
3413#[serde(rename_all = "snake_case")]
3414pub enum DurabilityTier {
3415 /// Deliberately no second copy. State lives only where the container runs
3416 /// and is gone when that node is. Legitimate for caches and scratch — and
3417 /// it is a *statement*, which is why it is not the same as declaring
3418 /// nothing (see [`WorkloadSpec::durability`]).
3419 None,
3420
3421 /// `turso-backup` tier 1a — periodic full `VACUUM INTO` snapshot to the
3422 /// object store. Recovery point is the last snapshot, so the loss window is
3423 /// the snapshot interval, which this declaration does not carry: a
3424 /// snapshot-tier workload's RPO is whatever schedules it.
3425 Snapshot,
3426
3427 /// `turso-backup` tier 1b — incremental page-dedup snapshot. Same recovery
3428 /// *point* semantics as [`Self::Snapshot`]; cheaper per run, so in practice
3429 /// a shorter interval.
3430 Dedup,
3431
3432 /// `turso-backup` tier 2 — WAL-frame streaming with restore by frame
3433 /// replay. The only tier with a *bounded, declarable* loss window; see
3434 /// [`WorkloadSpec::durability`]'s `rpo-seconds` and
3435 /// `turso_backup::stream::DEFAULT_RPO_TARGET` (120 s), which is what an
3436 /// undeclared RPO means in practice.
3437 Stream,
3438}
3439
3440impl DurabilityTier {
3441 fn parse(raw: &str) -> Option<Self> {
3442 match raw {
3443 "none" => Some(Self::None),
3444 "snapshot" => Some(Self::Snapshot),
3445 "dedup" => Some(Self::Dedup),
3446 "stream" => Some(Self::Stream),
3447 _ => None,
3448 }
3449 }
3450
3451 /// The wire/TOML spelling, so a diagnostic and the file it points at agree.
3452 pub fn as_str(&self) -> &'static str {
3453 match self {
3454 Self::None => "none",
3455 Self::Snapshot => "snapshot",
3456 Self::Dedup => "dedup",
3457 Self::Stream => "stream",
3458 }
3459 }
3460
3461 /// Whether this tier puts bytes in an object store — i.e. whether there is
3462 /// a copy to hydrate from after the node is gone.
3463 pub fn ships_bytes(&self) -> bool {
3464 !matches!(self, Self::None)
3465 }
3466}
3467
3468impl fmt::Display for DurabilityTier {
3469 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3470 f.write_str(self.as_str())
3471 }
3472}
3473
3474/// A parsed `yah.durability.*` declaration. See [`WorkloadSpec::durability`].
3475#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
3476pub struct Durability {
3477 pub tier: DurabilityTier,
3478 /// Which engine's tier vocabulary this is. `Some` exactly when
3479 /// [`DurabilityTier::ships_bytes`] — enforced by the accessor (R850-F1).
3480 pub engine: Option<DurabilityEngine>,
3481 /// Object-store URL the copy lives at. `Some` exactly when
3482 /// [`DurabilityTier::ships_bytes`] — enforced by the accessor.
3483 pub store: Option<String>,
3484 /// Volume-relative paths of the database files this tier covers, in
3485 /// declaration order. Non-empty exactly when
3486 /// [`DurabilityTier::ships_bytes`] — enforced by the accessor (R850-F1).
3487 ///
3488 /// Volume-relative, never absolute: the same string is joined onto the
3489 /// container's mount target when read as documentation and onto
3490 /// `/var/lib/yah/kamaji/volumes/<name>` when a hydrate writes it. Each is
3491 /// also the object-store key suffix under [`Self::store`], so the layout an
3492 /// operator sees in the bucket mirrors the layout on the volume.
3493 pub subjects: Vec<String>,
3494 /// Declared recovery-point objective in seconds. [`DurabilityTier::Stream`]
3495 /// only; `None` there means `turso_backup::stream::DEFAULT_RPO_TARGET`.
3496 pub rpo_seconds: Option<u32>,
3497 /// Expected steady-state size of this workload's state, in MiB — the input
3498 /// a cold-start-from-object-store estimate needs and cannot get anywhere
3499 /// else. `resources.ephemeral_storage_mb` is not it: that caps the writable
3500 /// layer and tmpfs, and a named volume is neither.
3501 ///
3502 /// **Declared, never measured.** Any recovery-time figure derived from it
3503 /// inherits that, and must say so at the point it is printed.
3504 pub state_mb: Option<u32>,
3505}
3506
3507/// A `yah.durability.*` declaration that cannot be read as one.
3508///
3509/// Every variant is a *refusal to guess*. The alternative — falling back to
3510/// "undeclared" on a malformed value, the way [`WorkloadSpec::memory_request_mb`]
3511/// falls back to its ceiling — is safe there and unsafe here: a mistyped memory
3512/// request costs a placement, a mistyped durability tier costs the database.
3513#[derive(Debug, Clone, PartialEq, Eq)]
3514pub enum DurabilityDeclError {
3515 /// `yah.durability.tier` holds something outside the vocabulary.
3516 UnknownTier { value: String },
3517 /// A `store`/`rpo-seconds` key with no `tier` key beside it — most often
3518 /// `tier` spelled wrong.
3519 OrphanKey { key: &'static str },
3520 /// A tier that ships bytes with nowhere to ship them.
3521 MissingStore { tier: DurabilityTier },
3522 /// `tier = "none"` with a store — contradictory, and the reader cannot
3523 /// tell which half is the mistake.
3524 StoreWithoutTier,
3525 /// An RPO on a tier that has no bounded loss window to state.
3526 RpoOnNonStreamTier { tier: DurabilityTier },
3527 /// `rpo-seconds` is not a number of seconds.
3528 UnparseableRpo { value: String },
3529 /// `state-mb` is not a number of mebibytes.
3530 UnparseableStateMb { value: String },
3531 /// R850-F1: `yah.durability.engine` holds something with no restore path.
3532 UnknownEngine { value: String },
3533 /// R850-F1: a bytes-shipping tier with no engine. The tier names are
3534 /// turso-backup's; a spec that means a different engine has to say so.
3535 MissingEngine { tier: DurabilityTier },
3536 /// R850-F1: an engine alongside `tier = "none"` — nothing ships, so there
3537 /// is nothing for an engine to be the engine *of*.
3538 EngineWithoutTier,
3539 /// R850-F1: a bytes-shipping tier that names no database files.
3540 MissingSubjects { tier: DurabilityTier },
3541 /// R850-F1: subjects alongside `tier = "none"`.
3542 SubjectsWithoutTier,
3543 /// R850-F1: an empty entry in the comma-separated subject list — a stray
3544 /// or trailing comma. Skipping it silently would hide a truncated list.
3545 EmptySubject,
3546 /// R850-F1: a subject that is not volume-relative.
3547 AbsoluteSubject { subject: String },
3548 /// R850-F1: a subject containing a `.` or `..` component.
3549 TraversingSubject { subject: String },
3550 /// R850-F1: the same subject listed twice — it would be backed up twice
3551 /// under one key and restored twice over itself.
3552 DuplicateSubject { subject: String },
3553}
3554
3555impl fmt::Display for DurabilityDeclError {
3556 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3557 match self {
3558 Self::UnknownTier { value } => write!(
3559 f,
3560 "{DURABILITY_TIER_ANNOTATION} = {value:?} is not a known tier \
3561 (none|snapshot|dedup|stream)"
3562 ),
3563 Self::OrphanKey { key } => write!(
3564 f,
3565 "{key} is set but {DURABILITY_TIER_ANNOTATION} is not — a store or an RPO \
3566 with no tier backs up nothing; check the spelling of the tier key"
3567 ),
3568 Self::MissingStore { tier } => write!(
3569 f,
3570 "{DURABILITY_TIER_ANNOTATION} = \"{tier}\" needs \
3571 {DURABILITY_STORE_ANNOTATION} — there is no default bucket, because a \
3572 default would put the only copy of this workload's state somewhere \
3573 nobody chose"
3574 ),
3575 Self::StoreWithoutTier => write!(
3576 f,
3577 "{DURABILITY_STORE_ANNOTATION} is set alongside \
3578 {DURABILITY_TIER_ANNOTATION} = \"none\"; drop one — either the state is \
3579 backed up or it is deliberately not"
3580 ),
3581 Self::RpoOnNonStreamTier { tier } => write!(
3582 f,
3583 "{DURABILITY_RPO_ANNOTATION} applies only to \
3584 {DURABILITY_TIER_ANNOTATION} = \"stream\", not \"{tier}\" — a snapshot \
3585 tier's recovery point is set by whatever schedules the snapshot, not by \
3586 the spec"
3587 ),
3588 Self::UnparseableRpo { value } => write!(
3589 f,
3590 "{DURABILITY_RPO_ANNOTATION} = {value:?} is not a whole number of seconds"
3591 ),
3592 Self::UnparseableStateMb { value } => write!(
3593 f,
3594 "{DURABILITY_STATE_MB_ANNOTATION} = {value:?} is not a whole number of MiB"
3595 ),
3596 Self::UnknownEngine { value } => write!(
3597 f,
3598 "{DURABILITY_ENGINE_ANNOTATION} = {value:?} has no restore path in this tree \
3599 (turso) — the tier names are turso-backup's, so another engine needs its own \
3600 implementation before it can name one"
3601 ),
3602 Self::MissingEngine { tier } => write!(
3603 f,
3604 "{DURABILITY_TIER_ANNOTATION} = \"{tier}\" needs \
3605 {DURABILITY_ENGINE_ANNOTATION} = \"turso\" — the tier vocabulary is \
3606 turso-backup's, and a declaration that does not say so cannot be acted on"
3607 ),
3608 Self::EngineWithoutTier => write!(
3609 f,
3610 "{DURABILITY_ENGINE_ANNOTATION} is set alongside \
3611 {DURABILITY_TIER_ANNOTATION} = \"none\"; nothing ships, so drop one"
3612 ),
3613 Self::MissingSubjects { tier } => write!(
3614 f,
3615 "{DURABILITY_TIER_ANNOTATION} = \"{tier}\" needs \
3616 {DURABILITY_SUBJECTS_ANNOTATION} — a restore's unit is a database file, not a \
3617 volume, and guessing which files in the volume are databases is guessing \
3618 about the only copy of this workload's state"
3619 ),
3620 Self::SubjectsWithoutTier => write!(
3621 f,
3622 "{DURABILITY_SUBJECTS_ANNOTATION} is set alongside \
3623 {DURABILITY_TIER_ANNOTATION} = \"none\"; nothing ships, so drop one"
3624 ),
3625 Self::EmptySubject => write!(
3626 f,
3627 "{DURABILITY_SUBJECTS_ANNOTATION} has an empty entry (a stray or trailing \
3628 comma); every entry must name a database file"
3629 ),
3630 Self::AbsoluteSubject { subject } => write!(
3631 f,
3632 "{DURABILITY_SUBJECTS_ANNOTATION} entry {subject:?} must be relative to the \
3633 workload's named volume — an absolute path would restore outside it"
3634 ),
3635 Self::TraversingSubject { subject } => write!(
3636 f,
3637 "{DURABILITY_SUBJECTS_ANNOTATION} entry {subject:?} contains a \".\" or \"..\" \
3638 component; it would restore outside the volume it is scoped to"
3639 ),
3640 Self::DuplicateSubject { subject } => write!(
3641 f,
3642 "{DURABILITY_SUBJECTS_ANNOTATION} names {subject:?} twice"
3643 ),
3644 }
3645 }
3646}
3647
3648impl std::error::Error for DurabilityDeclError {}
3649
3650/// Annotation key requesting a workload share the host network namespace.
3651/// See [`WorkloadSpec::wants_host_network`].
3652pub const HOST_NETWORK_ANNOTATION: &str = "yah.network";
3653
3654/// Annotation value (for [`HOST_NETWORK_ANNOTATION`]) selecting host
3655/// networking. Any other value leaves the workload in an isolated netns.
3656pub const HOST_NETWORK_VALUE: &str = "host";
3657
3658/// Annotation key declaring that a workload must land only on a node
3659/// carrying a specific taint. See [`WorkloadSpec::requires_taint`].
3660pub const REQUIRES_TAINT_ANNOTATION: &str = "yah.placement.requires-taint";
3661
3662/// Annotation key carrying a workload's memory **request** in MiB — what a
3663/// scheduler must find free on a node — separate from the `memory_mb`
3664/// **ceiling** the backend enforces as a cgroup limit. See
3665/// [`WorkloadSpec::memory_request_mb`].
3666pub const MEMORY_REQUEST_ANNOTATION: &str = "yah.placement.memory-request-mb";
3667
3668/// Annotation key declaring where a workload's state is copied to, and how far
3669/// behind that copy may be. See [`WorkloadSpec::durability`].
3670pub const DURABILITY_TIER_ANNOTATION: &str = "yah.durability.tier";
3671
3672/// Annotation key naming the object store a [`DurabilityTier`] ships to.
3673/// Required for every tier except [`DurabilityTier::None`].
3674pub const DURABILITY_STORE_ANNOTATION: &str = "yah.durability.store";
3675
3676/// Annotation key carrying the declared recovery-point objective in seconds.
3677/// [`DurabilityTier::Stream`] only.
3678pub const DURABILITY_RPO_ANNOTATION: &str = "yah.durability.rpo-seconds";
3679
3680/// Annotation key naming which engine's tier vocabulary a declaration uses
3681/// (R850-F1). Required for every tier except [`DurabilityTier::None`]. See
3682/// [`DurabilityEngine`].
3683pub const DURABILITY_ENGINE_ANNOTATION: &str = "yah.durability.engine";
3684
3685/// Annotation key listing the volume-relative database files a tier covers,
3686/// comma-separated (R850-F1). Required for every tier except
3687/// [`DurabilityTier::None`]. See [`Durability::subjects`].
3688pub const DURABILITY_SUBJECTS_ANNOTATION: &str = "yah.durability.subjects";
3689
3690/// Annotation key carrying the expected size of a workload's state in MiB —
3691/// the only declared input a cold-start-from-object-store estimate has. See
3692/// [`Durability::state_mb`].
3693pub const DURABILITY_STATE_MB_ANNOTATION: &str = "yah.durability.state-mb";
3694
3695/// The memory request [`WorkloadSpec::for_forge`] declares (MiB).
3696///
3697/// A forge run is a build, and a build's *ceiling* is deliberately roomy
3698/// (`FORGE_MEMORY_LIMIT_MB`); this is the much smaller floor a node must have
3699/// free to be a legal target for one. 2 GiB is what the heaviest forge shape
3700/// in the tree already asks for by hand — `velveteen_exec::remote`'s buildkit
3701/// image-build step overrides `resources.memory_mb` to exactly this — so it is
3702/// a measured number rather than a guess, and it keeps the fleet's 8 GiB
3703/// build-workers schedulable.
3704pub const FORGE_MEMORY_REQUEST_MB: u32 = 2048;
3705
3706/// The cgroup memory ceiling [`WorkloadSpec::for_forge`] sets (MiB).
3707///
3708/// Bounded rather than unlimited so a runaway build cannot take the host
3709/// down, and large enough for the V8 build's >12 GB peak (R590-B10). It is
3710/// **not** a placement input — see [`FORGE_MEMORY_REQUEST_MB`].
3711pub const FORGE_MEMORY_LIMIT_MB: u32 = 32768;
3712
3713/// Taint name (for [`REQUIRES_TAINT_ANNOTATION`]) identifying machines with
3714/// a publicly-routable IP — the W267 sovereign-ingress placement
3715/// requirement. `MachineConfig.taints` (R572-F3) is the matching node-side
3716/// field and `RequiredSpec::matches` (R572-F5) is the consumer, so this is a
3717/// live key on both sides: a node may carry it, and the cloudflared/passway
3718/// ingress specs require it.
3719pub const PUBLIC_IP_TAINT: &str = "public-ip";
3720
3721/// Annotation key selecting which **execution substrate** kamaji runs a
3722/// workload on. Absent (or unrecognised) means a container backend; see
3723/// [`NATIVE_EXEC_VALUE`] and [`MICROVM_EXEC_VALUE`] for the two opt-outs.
3724///
3725/// The name is historical — R577-T1 introduced it for native exec alone — but
3726/// the key has always been the substrate selector, and R605-F8 added the
3727/// second alternative rather than a second key. See
3728/// [`WorkloadSpec::wants_microvm`] for why one key matters.
3729pub const NATIVE_EXEC_ANNOTATION: &str = "yah.exec";
3730
3731/// Annotation value (for [`NATIVE_EXEC_ANNOTATION`]) selecting native
3732/// host execution. Any other value leaves the workload on a container
3733/// backend.
3734pub const NATIVE_EXEC_VALUE: &str = "native";
3735
3736/// Annotation value (for [`NATIVE_EXEC_ANNOTATION`]) selecting a **microVM**:
3737/// the workload boots in its own KVM guest rather than sharing the host
3738/// kernel. See [`WorkloadSpec::wants_microvm`].
3739pub const MICROVM_EXEC_VALUE: &str = "microvm";
3740
3741/// Annotation key requesting the capabilities a workload needs to stand up an
3742/// unprivileged container sandbox of its own.
3743/// See [`WorkloadSpec::wants_nested_sandbox`].
3744pub const NESTED_SANDBOX_ANNOTATION: &str = "yah.sandbox";
3745
3746/// Annotation value (for [`NESTED_SANDBOX_ANNOTATION`]) requesting the
3747/// nested-sandbox grant (`CAP_SETUID` + `CAP_SETGID`, `no_new_privs` off).
3748/// Any other value leaves the workload on the baseline sandbox.
3749pub const NESTED_SANDBOX_VALUE: &str = "nested";
3750
3751// ── ImageRef ─────────────────────────────────────────────────────────────────
3752
3753/// Container image reference identifying a specific image to pull.
3754///
3755/// **Digest is required.** Every executable image reference in the workspace
3756/// is content-addressed by `sha256:<hex>`. The `tag` is preserved as a
3757/// human-readable identifier but is not the source of truth — registries
3758/// return mutable `tag → digest` mappings and we don't trust them for
3759/// reproducibility. R438-T3 tightened `digest: Option<String> → String` to
3760/// make unpinned-image bugs impossible by construction.
3761///
3762/// **Two deserialize shapes.** The struct form
3763/// (`registry`/`repository`/`tag`/`digest` fields) is the on-disk envelope.
3764/// A **string form** (`image = "ghcr.io/foo/bar:v1@sha256:<hex>"`) is also
3765/// accepted and is the shape W164 transform recipes (R438-T4) and W165
3766/// `BuildMode::InContainer` (R438-T6) use. Both shapes go through a single
3767/// parser ([`compose_import::parse_pinned_image_ref`]) that rejects
3768/// bare-tag references at serde-deserialize.
3769#[derive(Debug, Clone, PartialEq, Eq, Serialize, TS)]
3770#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3771pub struct ImageRef {
3772 /// Registry hostname, e.g. `"ghcr.io"` or `"localhost:5000"`.
3773 pub registry: String,
3774
3775 /// Repository path, e.g. `"noisetable/api"`.
3776 pub repository: String,
3777
3778 /// Tag, e.g. `"v1.4.2"` or `"latest"`. Informational — the digest is
3779 /// the source of truth for image identity.
3780 pub tag: String,
3781
3782 /// Content-addressed pinned identity, e.g. `"sha256:abc..."`. Required.
3783 pub digest: String,
3784}
3785
3786impl<'de> Deserialize<'de> for ImageRef {
3787 fn deserialize<D>(de: D) -> Result<Self, D::Error>
3788 where
3789 D: serde::Deserializer<'de>,
3790 {
3791 #[derive(Deserialize)]
3792 struct Fields {
3793 registry: String,
3794 repository: String,
3795 tag: String,
3796 digest: String,
3797 }
3798
3799 // The string-or-struct `untagged` probe requires `deserialize_any`,
3800 // which only self-describing formats support. Postcard — the binary
3801 // wire behind the kamaji UDS — returns `WontImplement` for it, so a
3802 // `Workload::Container(WorkloadSpec)` carrying a nested `ImageRef`
3803 // failed to decode and every container deploy 500'd (R590-B3).
3804 //
3805 // The string form is purely an authoring convenience in human-readable
3806 // configs (`image = "ghcr.io/…@sha256:…"` in recipe/workload TOML and
3807 // JSON); the binary wire only ever carries the derived struct form
3808 // (Serialize is a plain struct derive). So branch on the format: text
3809 // keeps the string-or-struct convenience via `untagged`; binary decodes
3810 // the plain positional struct with no `deserialize_any`.
3811 if de.is_human_readable() {
3812 #[derive(Deserialize)]
3813 #[serde(untagged)]
3814 enum Repr {
3815 // Order matters for `untagged`: try the string form first so
3816 // explicit strings don't get coerced into a struct error.
3817 Pinned(String),
3818 Struct(Fields),
3819 }
3820
3821 match Repr::deserialize(de)? {
3822 Repr::Pinned(s) => {
3823 compose_import::parse_pinned_image_ref(&s).map_err(serde::de::Error::custom)
3824 }
3825 Repr::Struct(f) => Ok(ImageRef {
3826 registry: f.registry,
3827 repository: f.repository,
3828 tag: f.tag,
3829 digest: f.digest,
3830 }),
3831 }
3832 } else {
3833 let f = Fields::deserialize(de)?;
3834 Ok(ImageRef {
3835 registry: f.registry,
3836 repository: f.repository,
3837 tag: f.tag,
3838 digest: f.digest,
3839 })
3840 }
3841 }
3842}
3843
3844// ── testing helpers ───────────────────────────────────────────────────────────
3845
3846/// Fixture helpers for test code that needs to construct types whose schemas
3847/// would otherwise demand operator-pinned values (digests, hashes). Doc-hidden
3848/// to discourage misuse from non-test code — production paths must source
3849/// digests from registry resolution or compile-time injection.
3850#[doc(hidden)]
3851pub mod testing {
3852 /// Fixed valid-format sha256 digest for test fixtures. All-zeros marker
3853 /// is impossible for any real image, so a leaked test fixture in a
3854 /// production code-path surfaces obviously.
3855 ///
3856 /// Aliases [`super::ImageRef::UNPINNED_DIGEST`] — the two are deliberately
3857 /// the same value: the fixture sentinel and the production "unpinned"
3858 /// marker must agree so [`super::ImageRef::pull_ref`]'s tag-fallback fires
3859 /// on exactly the digest `catalog_image` writes.
3860 pub const TEST_DIGEST: &str = super::ImageRef::UNPINNED_DIGEST;
3861
3862 /// Owned `String` form of [`TEST_DIGEST`] for fixture constructors.
3863 pub fn test_digest() -> String {
3864 TEST_DIGEST.to_string()
3865 }
3866}
3867
3868// ── EnvVar ────────────────────────────────────────────────────────────────────
3869
3870/// A single environment variable injected into the container.
3871#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
3872#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3873pub struct EnvVar {
3874 /// Variable name, conventionally `SCREAMING_SNAKE_CASE`.
3875 pub name: String,
3876
3877 /// Value source.
3878 pub value: EnvValue,
3879}
3880
3881/// Value source for an environment variable.
3882#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
3883#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3884#[serde(rename_all = "snake_case")]
3885pub enum EnvValue {
3886 /// Static string baked into the spec.
3887 Literal { value: String },
3888
3889 /// Resolved from a yubaba secret at deploy time; the secret value never
3890 /// appears in the spec JSON.
3891 FromSecret { secret: String, key: String },
3892
3893 /// Resolved from another workload's mesh address at deploy time by yubaba.
3894 /// Lets workloads reference each other symbolically without IP pinning.
3895 FromMesh { ident: MeshIdent, kind: MeshLookup },
3896}
3897
3898/// Which aspect of a mesh peer's address to inject.
3899///
3900/// ## Which port, when the peer has several (R844-B22)
3901///
3902/// [`Self::Url`] and [`Self::Port`] used to mean "the *first* entry in the
3903/// peer's `expose.mesh.ports`". That was a positional guess — the same one
3904/// `kamaji::name_anonymous_ports` refuses to make and that R844-F15 removed
3905/// from the service-record fanout — and it could hand a dependent workload a
3906/// metrics listener's number in its environment while looking entirely
3907/// successful. It survived only because, before R844-F17, a manifest had no way
3908/// to *name* a port, so "first" was the only selector that existed.
3909///
3910/// They now resolve by the same rule everything else in this workspace uses:
3911/// one port resolves to that port; several resolve to the one named `http`;
3912/// several with no `http` is an **error**, not a pick. The error is the feature
3913/// — it sends the author back to the manifest to say which listener they meant,
3914/// instead of handing a dependent a plausible wrong number.
3915///
3916/// [`Self::UrlNamed`] / [`Self::PortNamed`] say it outright and are the
3917/// spelling to prefer for any peer with more than one listener.
3918///
3919/// The named variants are **appended** rather than added as fields on the
3920/// existing ones: `MeshLookup` rides `EnvValue::FromMesh` inside a
3921/// [`WorkloadSpec`] across the postcard kamaji UDS, where an enum is encoded by
3922/// variant index, so appending leaves every existing encoding byte-identical
3923/// while adding a field to `Url` would not.
3924#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
3925#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3926#[serde(rename_all = "snake_case")]
3927pub enum MeshLookup {
3928 /// Full URL, e.g. `"http://noisetable-db.pdx:5432"`. See the type docs for
3929 /// which port this picks when the peer has several.
3930 Url,
3931 /// Hostname only, e.g. `"noisetable-db.pdx"`.
3932 Host,
3933 /// Port only, e.g. `"5432"`. See the type docs for which port this picks
3934 /// when the peer has several.
3935 Port,
3936 /// Full URL at the peer's port called `name`, e.g. `"http://api.pdx:8443"`
3937 /// for `name = "wss"`. Errors when the peer has no port by that name.
3938 UrlNamed { name: String },
3939 /// The peer's port called `name`, stringified. Errors when the peer has no
3940 /// port by that name.
3941 PortNamed { name: String },
3942}
3943
3944impl MeshLookup {
3945 /// The port name this lookup selects, or `None` when it takes the default
3946 /// (see the type docs) or needs no port at all.
3947 pub fn port_name(&self) -> Option<&str> {
3948 match self {
3949 MeshLookup::UrlNamed { name } | MeshLookup::PortNamed { name } => Some(name),
3950 MeshLookup::Url | MeshLookup::Host | MeshLookup::Port => None,
3951 }
3952 }
3953
3954 /// Whether this lookup needs a port at all — `Host` is the one that does
3955 /// not, and it must keep resolving for a portless peer.
3956 pub fn needs_port(&self) -> bool {
3957 !matches!(self, MeshLookup::Host)
3958 }
3959}
3960
3961// ── Secrets ───────────────────────────────────────────────────────────────────
3962
3963/// A secret value mounted into the container as an env var or file.
3964///
3965/// The secret value never appears in the spec JSON — only the reference.
3966/// Yubaba audits secret access per workload from these references.
3967#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
3968#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3969pub struct SecretMount {
3970 /// Where yubaba reads the secret value from.
3971 pub source: SecretRef,
3972
3973 /// How the secret is surfaced inside the container.
3974 pub target: SecretTarget,
3975}
3976
3977/// Where yubaba resolves the secret value from.
3978#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
3979#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3980#[serde(rename_all = "snake_case")]
3981pub enum SecretRef {
3982 /// Per-machine yubaba secret store at `/var/lib/yah/yubaba/secrets/`.
3983 LocalFile { path: PathBuf },
3984
3985 /// Raft-replicated cluster secret spanning all machines (planned; not in
3986 /// V1 deployment). Sketch preserved for wire compatibility.
3987 Cluster { name: String },
3988}
3989
3990/// How the secret is surfaced inside the container.
3991#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
3992#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
3993#[serde(rename_all = "snake_case")]
3994pub enum SecretTarget {
3995 /// Injected as an environment variable. Value never appears in spec JSON.
3996 /// Prefer `File` — env vars leak through subprocess env and log dumps.
3997 EnvVar { name: String },
3998
3999 /// Mounted as a file inside the container at `path` with `mode` (octal).
4000 File { path: PathBuf, mode: u32 },
4001}
4002
4003// ── Volumes ───────────────────────────────────────────────────────────────────
4004
4005/// A volume mount inside the container.
4006#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4007#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4008pub struct VolumeMount {
4009 /// Backing volume source.
4010 pub source: VolumeSource,
4011
4012 /// Absolute path inside the container.
4013 pub target: PathBuf,
4014
4015 /// Whether the container sees the volume as read-only.
4016 pub read_only: bool,
4017}
4018
4019/// Backing source for a volume mount.
4020#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4021#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4022#[serde(rename_all = "snake_case")]
4023pub enum VolumeSource {
4024 /// Yubaba-managed named volume; created on first use.
4025 Named { name: String },
4026
4027 /// Operator-managed host path. Yubaba rejects bind mounts unless
4028 /// `WorkloadSpec.tier == "infra"`; shape validation enforces this.
4029 Bind { host_path: PathBuf },
4030
4031 /// In-memory tmpfs; discarded on container stop. `size_mb` caps space
4032 /// consumed by the writable layer.
4033 Tmpfs { size_mb: u32 },
4034}
4035
4036// ── Durable forge produced-artifact convention (R603-T5) ──────────────────────
4037
4038/// Convention for a remote forge step's durable produced artifacts.
4039///
4040/// A remote build (e.g. the rusty_v8 musl build on a build-worker) writes its
4041/// output tarball to a path *inside* the container. The container's rootfs is
4042/// destroyed when kamaji reaps the EXITED container — so if the camp daemon is
4043/// down when the build finishes, the artifact is gone before boot-reconcile can
4044/// retrieve it (R603-T4 surfaced this as `Success`-but-`UNPUBLISHED`).
4045///
4046/// The fix (R603-T5) is a **host-persistent bind mount**: forge Subprocess
4047/// workloads mount [`HOST_ROOT`]`/<forge_id>` onto [`CONTAINER_DIR`], so a
4048/// build that writes its `produces` under `/yah/produced` lands the bytes on
4049/// the worker's host filesystem. yubaba then reads them back from the host path
4050/// ([`host_path`]) — which outlives container reaping — instead of the
4051/// unreachable container rootfs.
4052///
4053/// The container-side path and the host root are a shared convention between
4054/// three crates: the qed `build_workload_spec` that adds the mount, kamaji that
4055/// binds it, and the yubaba handler that reads + reaps it. Keeping it here (the
4056/// crate all three already depend on) is the single source of truth.
4057pub mod forge_produced {
4058 use std::path::{Path, PathBuf};
4059
4060 /// Conventional container-side directory a remote forge step writes its
4061 /// durable produced artifacts to. Bind-mounted onto a host-persistent dir.
4062 pub const CONTAINER_DIR: &str = "/yah/produced";
4063
4064 /// Host root under which each forge's durable produced dir lives, one
4065 /// subdir per run: `<HOST_ROOT>/<forge_id>/`. yubaba owns this directory —
4066 /// it creates the per-forge subdir at deploy, serves reads from it, and
4067 /// reaps it on teardown / TTL sweep.
4068 pub const HOST_ROOT: &str = "/var/lib/yah/qed/produced";
4069
4070 /// Forge mesh idents are `forge.<id>` (see [`WorkloadSpec::for_forge`]).
4071 /// Extract the bare `<id>`, or `None` for a non-forge ident.
4072 ///
4073 /// [`WorkloadSpec::for_forge`]: super::WorkloadSpec::for_forge
4074 pub fn forge_id_from_ident(ident: &str) -> Option<&str> {
4075 ident.strip_prefix("forge.")
4076 }
4077
4078 /// The host-persistent produced directory for one forge run.
4079 pub fn host_dir(forge_id: &str) -> PathBuf {
4080 PathBuf::from(HOST_ROOT).join(forge_id)
4081 }
4082
4083 /// Translate a container-side produced path to its durable host path for a
4084 /// given forge run. Returns `None` when `container_path` is not under
4085 /// [`CONTAINER_DIR`] (the caller then knows the artifact was not written to
4086 /// the durable location and won't survive reaping), or when the relative
4087 /// path contains a `..` component (a traversal attempt that could escape the
4088 /// per-forge dir — the reader must never serve a file outside it).
4089 pub fn host_path(forge_id: &str, container_path: &Path) -> Option<PathBuf> {
4090 host_path_under(&host_dir(forge_id), container_path)
4091 }
4092
4093 /// The same translation against an ARBITRARY host directory, for a produced
4094 /// dir that is not one of yubaba's (R560-B12).
4095 ///
4096 /// A LOCAL container step has the identical problem a remote one has and no
4097 /// [`HOST_ROOT`] to solve it with: `docker run --rm` throws the container's
4098 /// writable layer away on exit, so a build that writes its `produces` under
4099 /// [`CONTAINER_DIR`] and exits 0 leaves the caller reading an absent file —
4100 /// exactly the remote failure this module was created for. The caller binds
4101 /// a host dir of its own choosing (qed uses a run-scoped dir under the
4102 /// camp's cache) and maps declared container paths through this.
4103 ///
4104 /// Split out rather than duplicated because the `..` guard is the whole
4105 /// safety content of both: `host_path` must never serve a file outside the
4106 /// per-forge dir, and this one must never write outside the caller's. Two
4107 /// copies of that check is one copy that can be fixed alone.
4108 pub fn host_path_under(host_dir: &Path, container_path: &Path) -> Option<PathBuf> {
4109 let rel = container_path.strip_prefix(CONTAINER_DIR).ok()?;
4110 if rel
4111 .components()
4112 .any(|c| matches!(c, std::path::Component::ParentDir))
4113 {
4114 return None;
4115 }
4116 Some(host_dir.join(rel))
4117 }
4118
4119 /// The durable produced-dir bind mount for a forge run: host
4120 /// `<HOST_ROOT>/<forge_id>` → container [`CONTAINER_DIR`], writable.
4121 pub fn durable_mount(forge_id: &str) -> super::VolumeMount {
4122 super::VolumeMount {
4123 source: super::VolumeSource::Bind {
4124 host_path: host_dir(forge_id),
4125 },
4126 target: PathBuf::from(CONTAINER_DIR),
4127 read_only: false,
4128 }
4129 }
4130
4131 /// True when `path` is (or is under) the conventional durable produced dir
4132 /// — the guard qed uses to enforce that declared `produces` land somewhere
4133 /// reap-durable.
4134 pub fn is_durable_path(path: &Path) -> bool {
4135 path.starts_with(CONTAINER_DIR)
4136 }
4137}
4138
4139// ── Forge host-state root (R636-B1) ───────────────────────────────────────────
4140
4141/// The one host directory tree a QED forge step's bind mounts may live under.
4142///
4143/// # Why this is a named root rather than a list of paths
4144///
4145/// runc refuses a bind whose source is missing, and the OCI mapper never
4146/// mkdirs one — so *something* has to create each host dir before deploy.
4147/// yubaba does, but only for paths it recognizes, and "recognizes" was
4148/// originally a hardcoded match on the produced dir. Every new forge mount then
4149/// re-learned the lesson the expensive way, on a real box, minutes into a
4150/// build: R603-B6 for `produced/`, then R636-B1 for `build-out/`, each
4151/// surfacing as the same opaque `failed to fulfil mount request: … no such file
4152/// or directory` from deep inside containerd.
4153///
4154/// Naming the *root* makes the rule checkable instead of enumerable: yubaba
4155/// creates any forge bind under [`HOST_ROOT`], and `yubaba.service` grants the
4156/// root once via `StateDirectory=yah/qed`. A third mount needs no new code and
4157/// no unit-file edit — it only has to live here.
4158///
4159/// The prefix bound is load-bearing in the other direction too: it is what
4160/// keeps a workload spec from asking yubaba to mkdir an arbitrary host path.
4161pub mod forge_state {
4162 use std::path::Path;
4163
4164 /// Root of the forge's host-persistent state. Both
4165 /// [`super::forge_produced::HOST_ROOT`] and [`BUILD_OUT_DIR`] are under it.
4166 pub const HOST_ROOT: &str = "/var/lib/yah/qed";
4167
4168 /// Host directory a `build-image` step's OCI archive is written to, bound
4169 /// at `/yah/build/out` in the BuildKit container. Shared (rather than
4170 /// per-forge like `produced/`) because the archive is named after the image
4171 /// tag, which is already unique per build.
4172 pub const BUILD_OUT_DIR: &str = "/var/lib/yah/qed/build-out";
4173
4174 /// Whether yubaba may create `host_path` on behalf of a forge workload.
4175 ///
4176 /// Rejects anything outside [`HOST_ROOT`], and anything with a `..`
4177 /// component — `/var/lib/yah/qed/../../../etc` starts with the root as a
4178 /// string and is nowhere near it as a path.
4179 pub fn is_forge_state_path(host_path: &Path) -> bool {
4180 !host_path
4181 .components()
4182 .any(|c| matches!(c, std::path::Component::ParentDir))
4183 && host_path.starts_with(HOST_ROOT)
4184 }
4185}
4186
4187// ── Materialized-secret path contract (R555-F5) ───────────────────────────────
4188
4189/// Where yubaba writes a `File`-target secret it has resolved, and how the host
4190/// path is derived from the container path.
4191///
4192/// # Why the derivation lives here and not in yubaba
4193///
4194/// yubaba resolves a [`SecretMount`] and rewrites it into a read-only [`Bind`]
4195/// volume before the spec reaches the backend, so the spec kamaji admits is not
4196/// the spec the dispatcher signed: one mount has become one bind. Admission has
4197/// to be able to recognise that rewrite — otherwise a signed recipe carrying a
4198/// secret is refused by [`admission::AdmissionGrant::covers`]'s bind rule, which
4199/// only knows about [`forge_state::HOST_ROOT`], with a message about a forge
4200/// state root that has nothing to do with what happened.
4201///
4202/// Recognising it means recomputing the host path, which means the derivation
4203/// has to be visible to both sides. It was private to yubaba's
4204/// `deploy::secret_mount`; it lives here now, and yubaba calls in. `forge_state`
4205/// is the same shape for the same reason.
4206///
4207/// [`Bind`]: VolumeSource::Bind
4208pub mod secret_mount {
4209 use std::path::{Path, PathBuf};
4210
4211 /// RAM-backed root for materialized secret files. `/run` is a tmpfs on
4212 /// systemd nodes, so decrypted PEM never touches disk. Each workload gets a
4213 /// `<root>/<ident>/` subdir, reaped on workload destroy.
4214 pub const HOST_ROOT: &str = "/run/yah/secrets";
4215
4216 /// Collapse a value into a single safe path component: every char outside
4217 /// `[A-Za-z0-9_-]` becomes `_` (dots included, so `.` / `..` can never
4218 /// traverse). Empty input maps to `_`.
4219 pub fn sanitize_component(s: &str) -> String {
4220 let mapped: String = s
4221 .chars()
4222 .map(|c| {
4223 if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
4224 c
4225 } else {
4226 '_'
4227 }
4228 })
4229 .collect();
4230 if mapped.is_empty() {
4231 "_".into()
4232 } else {
4233 mapped
4234 }
4235 }
4236
4237 /// Derive a collision-free host filename from a container target path: strip
4238 /// the leading `/`, keep `.` for extensions, and replace path separators (and
4239 /// any other non-`[A-Za-z0-9_.-]` char) with `_`. A target that reduces to
4240 /// nothing or a dots-only name falls back to `secret`. The result is always a
4241 /// single flat filename (no separators), so it cannot traverse out of the
4242 /// per-workload dir.
4243 pub fn host_file_name(target: &Path) -> String {
4244 let raw = target.to_string_lossy();
4245 let trimmed = raw.trim_start_matches('/');
4246 let mapped: String = trimmed
4247 .chars()
4248 .map(|c| {
4249 if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.') {
4250 c
4251 } else {
4252 '_'
4253 }
4254 })
4255 .collect();
4256 if mapped.is_empty() || mapped.chars().all(|c| c == '.') {
4257 "secret".into()
4258 } else {
4259 mapped
4260 }
4261 }
4262
4263 /// The per-workload directory materialized secrets are written to.
4264 pub fn workload_dir(root: &Path, ident: &str) -> PathBuf {
4265 root.join(sanitize_component(ident))
4266 }
4267
4268 /// The host path a `File`-target secret at container path `target` is
4269 /// materialized to for workload `ident`.
4270 ///
4271 /// Deterministic in exactly those three inputs, which is what lets admission
4272 /// recompute it from the spec alone and match a bind against it.
4273 pub fn materialized_host_path(root: &Path, ident: &str, target: &Path) -> PathBuf {
4274 workload_dir(root, ident).join(host_file_name(target))
4275 }
4276}
4277
4278#[cfg(test)]
4279mod secret_mount_tests {
4280 use super::secret_mount::*;
4281 use std::path::{Path, PathBuf};
4282
4283 #[test]
4284 fn the_host_path_is_a_pure_function_of_root_ident_and_target() {
4285 let p = materialized_host_path(
4286 Path::new(HOST_ROOT),
4287 "forge.abc-123",
4288 Path::new("/etc/yah/r2.json"),
4289 );
4290 assert_eq!(
4291 p,
4292 PathBuf::from("/run/yah/secrets/forge_abc-123/etc_yah_r2.json")
4293 );
4294 }
4295
4296 /// The two collapses exist to keep a hostile ident or target from steering
4297 /// the write out of the per-workload dir. Pinned here because admission now
4298 /// depends on them being total.
4299 #[test]
4300 fn neither_component_can_traverse() {
4301 for ident in ["..", "../../etc", "a/b", ""] {
4302 let dir = workload_dir(Path::new(HOST_ROOT), ident);
4303 assert_eq!(dir.components().count(), 5, "{ident:?} escaped {dir:?}");
4304 assert!(dir.starts_with(HOST_ROOT));
4305 }
4306 for target in ["/../../etc/shadow", "..", "/", "/a/../b"] {
4307 let name = host_file_name(Path::new(target));
4308 assert!(!name.contains('/'), "{target:?} kept a separator: {name}");
4309 assert_ne!(name, "..");
4310 }
4311 }
4312}
4313
4314// ── Durable forge build-cache convention (R876-F4) ────────────────────────────
4315
4316/// Convention for a remote forge step's host-persistent **build cache**.
4317///
4318/// # The gap this closes
4319///
4320/// A remote subprocess gets image + argv + the [`forge_produced`] mount and
4321/// nothing else, so a step that compiles a source tree compiles it from scratch
4322/// every single run: the container's writable layer (where `CARGO_TARGET_DIR`
4323/// lands by default) is thrown away when kamaji reaps the exited container, and
4324/// `/yah/produced` is per-run and reaped on destroy. `mesofact-musl`'s x86_64
4325/// leg measured 9m56s / 9m57s / 11m10s across its successful runs and every one
4326/// of those was a cold full release build.
4327///
4328/// This is the third mount under [`forge_state::HOST_ROOT`], and — exactly as
4329/// R603-B6's handoff promised — it needs neither new yubaba code nor a
4330/// `yubaba.service` edit: `ensure_forge_state_dirs` already mkdirs *any* forge
4331/// bind under that root.
4332///
4333/// # Why the key is derived, not caller-supplied
4334///
4335/// A shared target dir keyed by nothing is a correctness bug, not merely a
4336/// race. `mesofact-musl` carries `concurrency_key = "mesofact-musl"`, but that
4337/// is *camp-side scheduling*: it does not constrain a second camp, or a
4338/// hand-rolled dispatch, aiming at the same worker. The key is therefore
4339/// derived by the dispatcher from **pipeline + step + target triple**
4340/// ([`key_from_parts`]) rather than written in a TOML, so two different
4341/// pipelines — or the same pipeline's two triples — cannot land on one target
4342/// dir however the run was started.
4343///
4344/// Two runs of the *same* pipeline+step+triple DO share, and that is the whole
4345/// point: cargo is designed for exactly that reuse, and its own `.cargo-lock`
4346/// in the target dir serializes two builds that overlap in time.
4347///
4348/// # Eviction is explicit
4349///
4350/// An unbounded cache on a worker rootfs is R702's subject. Both holders of a
4351/// cache root — yubaba on the worker, qed for the local-container placement —
4352/// sweep it with [`evict_plan`]: anything idle past [`RETENTION`] goes, and if
4353/// the filesystem is below [`FREE_FLOOR_BYTES`] the least-recently-used dirs go
4354/// too, until it is not. The cache can therefore never consume the last few GB
4355/// of a build worker's `/var`.
4356pub mod forge_cache {
4357 use std::path::{Path, PathBuf};
4358 use std::time::{Duration, SystemTime};
4359
4360 /// Conventional container-side directory a cached forge step's build
4361 /// scratch lives in. Bind-mounted onto a host-persistent, key-scoped dir.
4362 ///
4363 /// The step's argv points its own toolchain at this (mesofact-musl exports
4364 /// `CARGO_TARGET_DIR="$YAH_CACHE_DIR/target"`), the same way it does the
4365 /// source-context fetch — the mount is toolchain-agnostic and the TOML
4366 /// keeps describing what actually runs.
4367 pub const CONTAINER_DIR: &str = "/yah/cache";
4368
4369 /// Environment variable carrying [`CONTAINER_DIR`] into the step, so an
4370 /// argv never has to hardcode the convention.
4371 pub const CACHE_DIR_ENV: &str = "YAH_CACHE_DIR";
4372
4373 /// Host root under which each cache key gets a directory:
4374 /// `<HOST_ROOT>/<key>/`. Under [`super::forge_state::HOST_ROOT`], so
4375 /// yubaba's `ensure_forge_state_dirs` creates it and `yubaba.service`
4376 /// already grants write access to it.
4377 pub const HOST_ROOT: &str = "/var/lib/yah/qed/cache";
4378
4379 /// A cache dir untouched for this long is evicted. Long enough that a
4380 /// weekly release still hits a warm cache; short enough that a renamed
4381 /// step's orphan does not sit on the disk forever.
4382 pub const RETENTION: Duration = Duration::from_secs(60 * 60 * 24 * 14);
4383
4384 /// Below this much free space on the filesystem holding a cache root,
4385 /// least-recently-used cache dirs are evicted until it is above it again.
4386 /// This is the bound that matters on a build worker: us-west-003's `/var`
4387 /// is a 60 GB LV, and a release target dir is multiple GB.
4388 pub const FREE_FLOOR_BYTES: u64 = 10 * 1024 * 1024 * 1024;
4389
4390 /// Longest derived key kept verbatim; longer ones are truncated and
4391 /// disambiguated with a digest by [`key_from_parts`].
4392 pub const MAX_KEY_LEN: usize = 96;
4393
4394 /// Whether `key` is safe to use as a single path component under
4395 /// [`HOST_ROOT`]. Deliberately narrow: alphanumerics plus `.`, `-`, `_`,
4396 /// non-empty, length-capped, and never a bare `.`/`..`. Everything a
4397 /// dispatcher derives passes; nothing a hostile spec could write escapes.
4398 pub fn is_valid_key(key: &str) -> bool {
4399 !key.is_empty()
4400 && key.len() <= MAX_KEY_LEN + 24
4401 && key != "."
4402 && key != ".."
4403 && key
4404 .chars()
4405 .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_'))
4406 }
4407
4408 /// Derive the sharing key from the parts that must not collide: the
4409 /// pipeline, the step within it, and the target triple.
4410 ///
4411 /// Characters outside the [`is_valid_key`] alphabet collapse to `-`. A key
4412 /// that would exceed [`MAX_KEY_LEN`] is truncated and suffixed with a
4413 /// digest of the *full* string, so shortening can never merge two distinct
4414 /// keys into one.
4415 ///
4416 /// The digest is FNV-1a rather than blake3: this crate is deliberately a
4417 /// zero-dependency schema crate (see its `seal` / `admission-verify`
4418 /// features — even the cipher is opt-in), the inputs are pipeline and step
4419 /// names from the camp's own TOMLs rather than anything adversarial, and
4420 /// the property needed is "two long keys differ", not preimage resistance.
4421 pub fn key_from_parts(pipeline: &str, step: &str, triple: &str) -> String {
4422 let raw = format!("{pipeline}.{step}.{triple}");
4423 let mut safe: String = raw
4424 .chars()
4425 .map(|c| {
4426 if c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_') {
4427 c
4428 } else {
4429 '-'
4430 }
4431 })
4432 .collect();
4433 if safe.len() > MAX_KEY_LEN {
4434 let digest = fnv1a64(raw.as_bytes());
4435 safe.truncate(MAX_KEY_LEN);
4436 safe.push('.');
4437 safe.push_str(&format!("{digest:016x}"));
4438 }
4439 safe
4440 }
4441
4442 /// FNV-1a, 64-bit. See [`key_from_parts`] for why this and not a real hash.
4443 fn fnv1a64(bytes: &[u8]) -> u64 {
4444 let mut h: u64 = 0xcbf2_9ce4_8422_2325;
4445 for b in bytes {
4446 h ^= *b as u64;
4447 h = h.wrapping_mul(0x0000_0100_0000_01b3);
4448 }
4449 h
4450 }
4451
4452 /// The host-persistent cache directory for one key, under [`HOST_ROOT`].
4453 pub fn host_dir(key: &str) -> Option<PathBuf> {
4454 cache_dir_under(Path::new(HOST_ROOT), key)
4455 }
4456
4457 /// The same derivation against an arbitrary root — for the local-container
4458 /// placement, whose cache lives under the camp's own `.yah/cache` and has
4459 /// no [`HOST_ROOT`] to hang off. Split out for the same reason
4460 /// [`super::forge_produced::host_path_under`] is: the key validation is the
4461 /// whole safety content of both, and two copies is one copy that can be
4462 /// fixed alone.
4463 pub fn cache_dir_under(root: &Path, key: &str) -> Option<PathBuf> {
4464 is_valid_key(key).then(|| root.join(key))
4465 }
4466
4467 /// The build-cache bind mount for one key: host `<HOST_ROOT>/<key>` →
4468 /// container [`CONTAINER_DIR`], writable. `None` for an invalid key —
4469 /// the caller must refuse rather than mount something else.
4470 pub fn durable_mount(key: &str) -> Option<super::VolumeMount> {
4471 Some(super::VolumeMount {
4472 source: super::VolumeSource::Bind {
4473 host_path: host_dir(key)?,
4474 },
4475 target: PathBuf::from(CONTAINER_DIR),
4476 read_only: false,
4477 })
4478 }
4479
4480 /// Which cache dirs to delete, given every dir in a cache root with its
4481 /// last-modified time, the current free space on that filesystem, and the
4482 /// floor to hold.
4483 ///
4484 /// Pure so the policy is one testable function shared by both holders of a
4485 /// cache root (yubaba on the worker, qed for local containers) instead of
4486 /// two drifting copies. The callers own the `read_dir` / `df` / `remove`.
4487 ///
4488 /// Two rules, in order: everything idle past `retention` goes
4489 /// unconditionally; then, while `free_bytes` is under `floor_bytes`, the
4490 /// least-recently-used survivor goes — LRU because the dir a build just
4491 /// touched is the one whose loss costs the next run the most.
4492 ///
4493 /// `free_bytes` is what the caller measured BEFORE any deletion, so the
4494 /// count of extra evictions is a heuristic (this function cannot know a
4495 /// dir's size without walking it). It is bounded and monotone: under
4496 /// sustained pressure each sweep drops one more dir, and a root that is
4497 /// entirely evicted simply rebuilds cold — the failure mode is a slow
4498 /// build, never a full disk.
4499 pub fn evict_plan(
4500 entries: &[(PathBuf, SystemTime)],
4501 now: SystemTime,
4502 retention: Duration,
4503 free_bytes: u64,
4504 floor_bytes: u64,
4505 ) -> Vec<PathBuf> {
4506 let mut evict = Vec::new();
4507 let mut live: Vec<&(PathBuf, SystemTime)> = Vec::new();
4508 for entry in entries {
4509 let idle = now
4510 .duration_since(entry.1)
4511 .map(|age| age > retention)
4512 .unwrap_or(false);
4513 if idle {
4514 evict.push(entry.0.clone());
4515 } else {
4516 live.push(entry);
4517 }
4518 }
4519 if free_bytes < floor_bytes && !live.is_empty() {
4520 live.sort_by_key(|(_, mtime)| *mtime);
4521 evict.push(live[0].0.clone());
4522 }
4523 evict
4524 }
4525}
4526
4527#[cfg(test)]
4528mod forge_cache_tests {
4529 use super::forge_cache::*;
4530 use std::path::{Path, PathBuf};
4531 use std::time::{Duration, SystemTime};
4532
4533 #[test]
4534 fn the_cache_root_is_under_the_forge_state_root() {
4535 assert!(super::forge_state::is_forge_state_path(Path::new(
4536 HOST_ROOT
4537 )));
4538 assert!(super::forge_state::is_forge_state_path(
4539 &host_dir("mesofact-musl.build.x86_64-unknown-linux-musl").unwrap()
4540 ));
4541 }
4542
4543 #[test]
4544 fn the_key_separates_pipelines_steps_and_triples() {
4545 let a = key_from_parts("mesofact-musl", "build", "x86_64-unknown-linux-musl");
4546 let b = key_from_parts("mesofact-musl", "build", "aarch64-unknown-linux-musl");
4547 let c = key_from_parts("other-pipeline", "build", "x86_64-unknown-linux-musl");
4548 let d = key_from_parts("mesofact-musl", "other-step", "x86_64-unknown-linux-musl");
4549 assert_ne!(a, b);
4550 assert_ne!(a, c);
4551 assert_ne!(a, d);
4552 assert!(is_valid_key(&a), "{a}");
4553 }
4554
4555 /// Shortening must never merge two distinct keys — the whole point of the
4556 /// key is that a collision is impossible.
4557 #[test]
4558 fn an_over_long_key_is_digest_disambiguated_not_merely_truncated() {
4559 let long = "p".repeat(MAX_KEY_LEN);
4560 let a = key_from_parts(&long, "step-one", "x86_64-unknown-linux-musl");
4561 let b = key_from_parts(&long, "step-two", "x86_64-unknown-linux-musl");
4562 assert_ne!(a, b);
4563 assert!(is_valid_key(&a) && is_valid_key(&b));
4564 }
4565
4566 #[test]
4567 fn a_key_that_could_escape_the_root_is_refused_rather_than_sanitized() {
4568 for bad in ["", ".", "..", "../etc", "a/b", "a\0b"] {
4569 assert!(!is_valid_key(bad), "{bad:?} must not be a cache key");
4570 assert!(host_dir(bad).is_none(), "{bad:?}");
4571 assert!(durable_mount(bad).is_none(), "{bad:?}");
4572 }
4573 // …and a derived key from hostile parts is sanitized into the alphabet.
4574 let k = key_from_parts("../../etc", "x/y", "t");
4575 assert!(is_valid_key(&k), "{k}");
4576 assert_eq!(host_dir(&k).unwrap().parent().unwrap(), Path::new(HOST_ROOT));
4577 }
4578
4579 #[test]
4580 fn durable_mount_shape() {
4581 let m = durable_mount("k").expect("valid key");
4582 assert_eq!(m.target, PathBuf::from(CONTAINER_DIR));
4583 assert!(!m.read_only, "a build cache the step cannot write is useless");
4584 match &m.source {
4585 super::VolumeSource::Bind { host_path } => {
4586 assert_eq!(host_path, &PathBuf::from(HOST_ROOT).join("k"));
4587 }
4588 other => panic!("expected a bind, got {other:?}"),
4589 }
4590 }
4591
4592 #[test]
4593 fn idle_dirs_are_evicted_and_fresh_ones_are_kept_when_there_is_room() {
4594 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
4595 let entries = vec![
4596 (PathBuf::from("/c/old"), now - RETENTION - Duration::from_secs(1)),
4597 (PathBuf::from("/c/fresh"), now - Duration::from_secs(60)),
4598 ];
4599 let plan = evict_plan(&entries, now, RETENTION, FREE_FLOOR_BYTES * 2, FREE_FLOOR_BYTES);
4600 assert_eq!(plan, vec![PathBuf::from("/c/old")]);
4601 }
4602
4603 #[test]
4604 fn disk_pressure_evicts_the_least_recently_used_survivor() {
4605 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
4606 let entries = vec![
4607 (PathBuf::from("/c/hot"), now - Duration::from_secs(60)),
4608 (PathBuf::from("/c/cool"), now - Duration::from_secs(6000)),
4609 ];
4610 let plan = evict_plan(&entries, now, RETENTION, 1, FREE_FLOOR_BYTES);
4611 assert_eq!(plan, vec![PathBuf::from("/c/cool")]);
4612 }
4613
4614 #[test]
4615 fn an_empty_root_under_disk_pressure_plans_nothing() {
4616 let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000);
4617 assert!(evict_plan(&[], now, RETENTION, 0, FREE_FLOOR_BYTES).is_empty());
4618 }
4619}
4620
4621#[cfg(test)]
4622mod forge_state_tests {
4623 use super::forge_state::*;
4624 use std::path::Path;
4625
4626 #[test]
4627 fn both_known_forge_roots_are_under_the_state_root() {
4628 assert!(is_forge_state_path(Path::new(
4629 super::forge_produced::HOST_ROOT
4630 )));
4631 assert!(is_forge_state_path(Path::new(BUILD_OUT_DIR)));
4632 assert!(is_forge_state_path(&super::forge_produced::host_dir(
4633 "abc-123"
4634 )));
4635 }
4636
4637 /// A spec must not be able to steer yubaba's mkdir anywhere it likes —
4638 /// neither by naming an unrelated absolute path nor by climbing out with
4639 /// `..`, which a plain string prefix check would wave through.
4640 #[test]
4641 fn paths_outside_the_root_are_refused() {
4642 for bad in [
4643 "/var/lib/yah/yubaba",
4644 "/etc/systemd/system",
4645 "/var/lib/yah/qed/../../../etc",
4646 "relative/path",
4647 ] {
4648 assert!(
4649 !is_forge_state_path(Path::new(bad)),
4650 "{bad} must not be creatable by a forge spec"
4651 );
4652 }
4653 }
4654}
4655
4656// ── Resources ─────────────────────────────────────────────────────────────────
4657
4658/// Hard resource caps enforced by containerd/cgroups at runtime.
4659#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4660#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4661pub struct ResourceLimits {
4662 /// Maximum RAM the container may allocate, in MiB. The container is OOM-
4663 /// killed if it exceeds this.
4664 ///
4665 /// A **ceiling**, not a request: setting it generously is the safe
4666 /// direction here and the unschedulable direction for placement, so
4667 /// schedulers must read [`WorkloadSpec::memory_request_mb`] instead of
4668 /// this field. (`cpu_millis` below is the opposite — a request by
4669 /// definition — which is why the two are not symmetric.)
4670 pub memory_mb: u32,
4671
4672 /// CPU **request** in millicores (k8s convention): `1000` = one full core,
4673 /// `250` = `.25 CPU`. Unlike a Docker relative weight this is an
4674 /// allocatable quantity a bin-packer can subtract from a node's budget.
4675 /// `0` means "no CPU limit". Backends that speak a relative weight derive
4676 /// it via [`ResourceLimits::cpu_shares`].
4677 pub cpu_millis: u32,
4678
4679 /// Cap on the writable layer + tmpfs footprint, in MiB.
4680 pub ephemeral_storage_mb: u32,
4681}
4682
4683impl ResourceLimits {
4684 /// The Docker/OCI relative CPU weight (`cpu.shares`, where `1024` ≈ one
4685 /// core) equivalent to this millicore request. The containerd and docker
4686 /// backends express CPU as a weight rather than a millicore request, so
4687 /// they derive it here instead of storing shares: `1000m` ⇒ `1024`.
4688 pub fn cpu_shares(&self) -> u64 {
4689 (u64::from(self.cpu_millis) * 1024) / 1000
4690 }
4691}
4692
4693// ── Healthcheck ───────────────────────────────────────────────────────────────
4694
4695/// Container health probe configuration.
4696#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4697#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4698pub struct Healthcheck {
4699 /// The probe executed to determine container health.
4700 pub probe: HealthProbe,
4701
4702 /// How often the probe runs.
4703 pub interval: Millis,
4704
4705 /// Per-probe timeout; a slow response counts as failure.
4706 pub timeout: Millis,
4707
4708 /// Time to wait after container start before the first probe. Shape
4709 /// validation warns (not errors) if this is less than
4710 /// `stop_policy.grace_period * 2`.
4711 pub initial_delay: Millis,
4712
4713 /// Number of consecutive failures before the container is marked
4714 /// `Unhealthy`.
4715 pub failure_threshold: u32,
4716}
4717
4718/// Mechanism used to check container health.
4719#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4720#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4721#[serde(rename_all = "snake_case")]
4722pub enum HealthProbe {
4723 /// HTTP GET to `path` on `port`. A 2xx (or `expect_status` if set)
4724 /// response counts as healthy.
4725 HttpGet {
4726 path: String,
4727 port: u16,
4728 #[ts(optional = nullable)]
4729 expect_status: Option<u16>,
4730 },
4731
4732 /// Run `argv` inside the container; exit-0 counts as healthy.
4733 Exec { argv: Vec<String> },
4734
4735 /// TCP connection to `port`; a successful connect counts as healthy.
4736 TcpConnect { port: u16 },
4737}
4738
4739// ── Restart / Stop ────────────────────────────────────────────────────────────
4740
4741/// What yubaba does when the container exits.
4742#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
4743#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4744#[serde(rename_all = "snake_case")]
4745pub enum RestartPolicy {
4746 /// Restart unconditionally on any exit.
4747 Always,
4748
4749 /// Restart on non-zero exit, up to `max_attempts` times with exponential
4750 /// backoff. After exhaustion, the workload is marked `Failed`.
4751 OnFailure {
4752 max_attempts: u32,
4753 backoff: BackoffPolicy,
4754 },
4755
4756 /// Do not restart. The container runs once and exits.
4757 ///
4758 /// **Forge convention.** Forge runs (R094) synthesize a `WorkloadSpec`
4759 /// using [`WorkloadSpec::for_forge`] which sets all the conventional fields
4760 /// together:
4761 ///
4762 /// - `restart_policy = Never`
4763 /// - `expose.public = None`, `expose.operator = None`
4764 /// - `expose.mesh.identity = "forge.<forge_id>"` — distinguishable from
4765 /// persistent mirror identities at the mesh layer
4766 /// - `tier = "infra"` (or the forge-spec's effective tier)
4767 /// - `annotations["yah.forge"] = "true"` — suppresses the shape warning
4768 ///
4769 /// Using `Never` on a persistent mirror (not a forge run) means the mirror
4770 /// stays dead after any exit — a likely misconfiguration. Shape validation
4771 /// emits a soft warning unless `annotations["yah.forge"] == "true"` is
4772 /// present. See R094 forge.
4773 Never,
4774}
4775
4776/// Exponential backoff parameters for `RestartPolicy::OnFailure`.
4777#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
4778#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4779pub struct BackoffPolicy {
4780 /// Initial delay before the first restart, in milliseconds.
4781 pub initial_ms: u32,
4782
4783 /// Maximum delay between retries, in milliseconds.
4784 pub max_ms: u32,
4785
4786 /// Backoff multiplier applied to each successive delay.
4787 pub multiplier: f32,
4788}
4789
4790/// Graceful shutdown configuration for yubaba's stop sequence.
4791#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4792#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4793pub struct StopPolicy {
4794 /// Signal number sent first, e.g. `15` (SIGTERM) or `2` (SIGINT).
4795 pub signal: i32,
4796
4797 /// Time yubaba waits after sending `signal` before issuing SIGKILL.
4798 pub grace_period: Millis,
4799}
4800
4801// ── Expose ────────────────────────────────────────────────────────────────────
4802
4803/// Network exposure configuration. The three channels are independent; any
4804/// combination is valid.
4805#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4806#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4807pub struct ExposeSpec {
4808 /// Mesh-internal exposure. Required; every workload must have a mesh
4809 /// identity even if no other workload currently reaches it.
4810 pub mesh: MeshExpose,
4811
4812 /// Public internet exposure via a Cloudflare tunnel route. `None` means
4813 /// the workload is not internet-reachable.
4814 #[ts(optional = nullable)]
4815 pub public: Option<PublicExpose>,
4816
4817 /// Operator-facing exposure via a Tailscale ACL tag. `None` means the
4818 /// workload is not operator-reachable via Tailscale.
4819 #[ts(optional = nullable)]
4820 pub operator: Option<OperatorExpose>,
4821}
4822
4823/// A peer permitted to initiate mesh connections to a workload (W206 / R558-F3).
4824///
4825/// Cross-tenant access is **deny-by-default**: a workload accepts inter-tenant
4826/// traffic only from peers it lists explicitly as [`MeshPeer::CrossTenant`].
4827/// Same-tenant access stays tier-based ([`MeshPeer::Tier`]) — the pre-R558
4828/// model — and an `allow_from` with no `Tier` entries still admits every
4829/// same-tenant peer (the historical "empty = allow all" default).
4830///
4831/// External serde tagging keeps this postcard-safe (R590-B3): no internal tag,
4832/// no untagged, no `skip_serializing_if`.
4833#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
4834#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4835#[serde(rename_all = "snake_case")]
4836pub enum MeshPeer {
4837 /// Any **same-tenant** workload whose `tier` matches this tag. This is the
4838 /// pre-R558 `allow_from` semantics.
4839 Tier(TierTag),
4840
4841 /// A specific workload in **another tenant**, addressed by its fully
4842 /// qualified mesh identity `<tenant>/<namespace>/<name>`. There is no
4843 /// cross-tenant tier wildcard — each cross-tenant peer is granted
4844 /// individually, so a shared fleet stays isolated unless an operator opts
4845 /// in here.
4846 CrossTenant {
4847 tenant: TenantId,
4848 namespace: NamespaceId,
4849 /// Peer's mesh identity (its [`MeshExpose::identity`]).
4850 name: MeshIdent,
4851 },
4852}
4853
4854/// One port a workload listens on, as its manifest declares it (R844-F17).
4855///
4856/// Before this, `expose.mesh.ports` was an array of bare numbers and a port
4857/// name was unwritable anywhere in the workspace — names were real at every
4858/// tier *below* the manifest (kamaji's allocator resolves `name -> port`, a
4859/// service record publishes `{"http": 8080, "wss": 8443}`, the sibling wire
4860/// carries `named_ports`, `PORT_<NAME>` reaches the process) and synthesised
4861/// from nothing at the top by [`crate::MeshExpose`]'s number list. This is the
4862/// declaration surface that had to exist for any of that to be *stated* rather
4863/// than guessed.
4864///
4865/// ## Three spellings, one type
4866///
4867/// ```toml
4868/// ports = [8080] # a number, unnamed
4869/// ports = ["http", "wss"] # names; the supervisor picks the numbers
4870/// ports = [{ name = "https", port = 443 }] # both stated
4871/// ```
4872///
4873/// They mix freely in one array (`ports = [{ name = "http", port = 8080 },
4874/// "metrics"]`), because the two facts are independent: a container's ports are
4875/// fixed by its image and still want names, while a native workload's numbers
4876/// are the allocator's to choose and only the names are the author's.
4877///
4878/// ## What each spelling means downstream
4879///
4880/// - **A number** is a request to listen there. On a container backend that is
4881/// simply the container-side port. On the published (fleet) tier a number
4882/// outside `kamaji::ports::WORLD_FIXED_PORTS` is refused at bring-up rather
4883/// than honoured (R844-F14) — a stale pin is how one workload lands on the
4884/// port a co-tenant already holds.
4885/// - **A name** is what a consumer asks for: `ServiceRecord::port("wss")`, the
4886/// ingress planner resolving which listener a hostname fronts, the
4887/// `PORT_<NAME>` variable the process reads. A workload declaring several
4888/// ports and naming none has nothing called `http`, and the front door
4889/// refuses to resolve rather than publish a hostname at whichever listener
4890/// sorted first (`kamaji::name_anonymous_ports`). Naming them is how you
4891/// answer that question instead of being asked it.
4892///
4893/// ## Wire shapes
4894///
4895/// Human-readable formats (TOML/JSON) accept all three spellings and
4896/// round-trip back to the most compact faithful one. The binary wire (postcard,
4897/// behind the kamaji UDS) carries the plain two-`Option` struct: `untagged`
4898/// needs `deserialize_any`, which postcard refuses — the same split
4899/// [`ImageRef`] makes, and for the same reason (R590-B3).
4900///
4901/// Deliberately NOT `Default`: the all-`None` value is the one shape no accepted
4902/// spelling produces and `validate::shape` rejects, so a `..Default::default()`
4903/// would hand a caller exactly the invalid port.
4904#[derive(Debug, Clone, PartialEq, Eq)]
4905pub struct MeshPort {
4906 /// The name this port is known by — `http`, `wss`, `metrics`. `None` when
4907 /// the manifest wrote a bare number; `kamaji::name_anonymous_ports` then
4908 /// decides what to call it, which is deliberately *not* `http` when there
4909 /// is more than one.
4910 pub name: Option<String>,
4911
4912 /// The port number, when the manifest states one. `None` means the
4913 /// supervisor allocates it and tells the workload via `PORT_<NAME>`.
4914 pub number: Option<u16>,
4915}
4916
4917impl MeshPort {
4918 /// A bare number, unnamed — the pre-R844-F17 spelling, still valid.
4919 pub fn anonymous(number: u16) -> Self {
4920 Self {
4921 name: None,
4922 number: Some(number),
4923 }
4924 }
4925
4926 /// A named port whose number the supervisor allocates.
4927 pub fn named(name: impl Into<String>) -> Self {
4928 Self {
4929 name: Some(name.into()),
4930 number: None,
4931 }
4932 }
4933
4934 /// A named port whose number the manifest states.
4935 pub fn pinned(name: impl Into<String>, number: u16) -> Self {
4936 Self {
4937 name: Some(name.into()),
4938 number: Some(number),
4939 }
4940 }
4941}
4942
4943impl From<u16> for MeshPort {
4944 fn from(number: u16) -> Self {
4945 Self::anonymous(number)
4946 }
4947}
4948
4949impl From<&str> for MeshPort {
4950 fn from(name: &str) -> Self {
4951 Self::named(name)
4952 }
4953}
4954
4955impl From<String> for MeshPort {
4956 fn from(name: String) -> Self {
4957 Self::named(name)
4958 }
4959}
4960
4961/// The self-describing spelling of a [`MeshPort`] — the shape a TOML/JSON
4962/// author writes, and the one the generated JSON schema and TS bindings
4963/// advertise.
4964///
4965/// Kept as its own type rather than folded into `MeshPort` because it is only
4966/// half the story: the binary wire never sees it (see [`MeshPort`]'s docs), and
4967/// a struct with two `Option`s is the shape every *consumer* wants regardless
4968/// of which of the three forms the author picked.
4969#[derive(Serialize, Deserialize)]
4970#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
4971#[serde(untagged)]
4972enum MeshPortRepr {
4973 /// `8080` — a number with no name.
4974 Number(u16),
4975 /// `"http"` — a name whose number the supervisor allocates.
4976 Name(String),
4977 /// `{ name = "https", port = 443 }` — both stated. `port` may be omitted,
4978 /// which is the table spelling of the bare-name form.
4979 Both {
4980 name: String,
4981 #[serde(default)]
4982 port: Option<u16>,
4983 },
4984}
4985
4986impl Serialize for MeshPort {
4987 fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
4988 where
4989 S: serde::Serializer,
4990 {
4991 if !ser.is_human_readable() {
4992 // Postcard and friends: the plain positional struct, every field
4993 // always encoded. See the V6 stanza in `kamaji_proto::version` —
4994 // there is no `skip_serializing_if` that is safe here.
4995 #[derive(Serialize)]
4996 struct Fields<'a> {
4997 name: &'a Option<String>,
4998 number: &'a Option<u16>,
4999 }
5000 return Fields {
5001 name: &self.name,
5002 number: &self.number,
5003 }
5004 .serialize(ser);
5005 }
5006
5007 match (&self.name, self.number) {
5008 (Some(name), Some(port)) => MeshPortRepr::Both {
5009 name: name.clone(),
5010 port: Some(port),
5011 },
5012 (Some(name), None) => MeshPortRepr::Name(name.clone()),
5013 (None, Some(port)) => MeshPortRepr::Number(port),
5014 // Not constructible from any accepted spelling; `validate::shape`
5015 // rejects it too. Emitted as an empty table rather than silently
5016 // becoming something else.
5017 (None, None) => MeshPortRepr::Both {
5018 name: String::new(),
5019 port: None,
5020 },
5021 }
5022 .serialize(ser)
5023 }
5024}
5025
5026impl<'de> Deserialize<'de> for MeshPort {
5027 fn deserialize<D>(de: D) -> Result<Self, D::Error>
5028 where
5029 D: serde::Deserializer<'de>,
5030 {
5031 if !de.is_human_readable() {
5032 #[derive(Deserialize)]
5033 struct Fields {
5034 name: Option<String>,
5035 number: Option<u16>,
5036 }
5037 let f = Fields::deserialize(de)?;
5038 return Ok(MeshPort {
5039 name: f.name,
5040 number: f.number,
5041 });
5042 }
5043
5044 Ok(match MeshPortRepr::deserialize(de)? {
5045 MeshPortRepr::Number(port) => MeshPort::anonymous(port),
5046 MeshPortRepr::Name(name) => MeshPort::named(name),
5047 MeshPortRepr::Both { name, port } => MeshPort {
5048 name: Some(name),
5049 number: port,
5050 },
5051 })
5052 }
5053}
5054
5055/// Mesh-internal port exposure and peer access control.
5056#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5057#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5058pub struct MeshExpose {
5059 /// DNS-segment mesh identity for this workload. Must be unique in the
5060 /// cluster. Regex: `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, length ≤ 63.
5061 pub identity: MeshIdent,
5062
5063 /// Ports this workload listens on, each optionally named (R844-F17). Other
5064 /// workloads reach it at `<identity>:<port>` on the mesh.
5065 ///
5066 /// See [`MeshPort`] for the three accepted spellings. Read the numbers with
5067 /// [`MeshExpose::numbers`] and the names with
5068 /// [`MeshExpose::named_numbers`] — there is deliberately no way to read
5069 /// this as a plain `Vec<u16>`, because a name-only entry has no number yet
5070 /// and a conversion that dropped it would be exactly the silent loss named
5071 /// ports exist to prevent.
5072 #[ts(type = "(number | string | { name: string, port?: number })[]")]
5073 #[cfg_attr(feature = "json-schema", schemars(with = "Vec<MeshPortRepr>"))]
5074 pub ports: Vec<MeshPort>,
5075
5076 /// Peers permitted to initiate connections to this workload on the mesh
5077 /// (W206 / R558-F3). Same-tenant tier rules and explicit cross-tenant
5078 /// grants share this one list. With **no** [`MeshPeer::Tier`] entries every
5079 /// same-tenant peer is admitted (the historical "empty = allow all"
5080 /// default); cross-tenant peers are always denied unless named by a
5081 /// [`MeshPeer::CrossTenant`] entry. See [`MeshExpose::admits_peer`].
5082 #[serde(default)]
5083 pub allow_from: Vec<MeshPeer>,
5084}
5085
5086impl MeshExpose {
5087 /// Every port *number* this workload declares, in declaration order.
5088 ///
5089 /// Name-only entries (`ports = ["http"]`) carry no number and are simply
5090 /// absent here — they do not have one until a supervisor allocates it. That
5091 /// is why this is a method rather than the field: a caller reading numbers
5092 /// has to be able to see that the list it got is shorter than the list the
5093 /// author wrote, and a `Vec<u16>` field could not say so.
5094 pub fn numbers(&self) -> Vec<u16> {
5095 self.ports.iter().filter_map(|p| p.number).collect()
5096 }
5097
5098 /// Whether `port` appears as a declared number.
5099 pub fn declares_number(&self, port: u16) -> bool {
5100 self.ports.iter().any(|p| p.number == Some(port))
5101 }
5102
5103 /// The `name -> number` map for every port the manifest declares *both*
5104 /// for. Name-only ports are absent (no number yet) and unnamed ports are
5105 /// absent (no name); `kamaji::name_anonymous_ports` is what fills the
5106 /// second gap once numbers are known.
5107 pub fn named_numbers(&self) -> BTreeMap<String, u16> {
5108 self.ports
5109 .iter()
5110 .filter_map(|p| Some((p.name.clone()?, p.number?)))
5111 .collect()
5112 }
5113
5114 /// Every port name the manifest states, in declaration order.
5115 pub fn names(&self) -> Vec<&str> {
5116 self.ports
5117 .iter()
5118 .filter_map(|p| p.name.as_deref())
5119 .collect()
5120 }
5121
5122 /// The pre-R844-F17 spelling as a value: a list of unnamed numbers. Kept
5123 /// because most call sites — and every test fixture — genuinely mean
5124 /// "these numbers, names irrelevant".
5125 pub fn anonymous_ports(numbers: impl IntoIterator<Item = u16>) -> Vec<MeshPort> {
5126 numbers.into_iter().map(MeshPort::anonymous).collect()
5127 }
5128
5129 /// Whether a peer may initiate a mesh connection to a workload whose mesh
5130 /// exposure is `self`. `own_tenant` is the tenant of the workload being
5131 /// protected; the remaining arguments identify the connecting peer.
5132 ///
5133 /// Deny-by-default across tenants (W206 / R558-F3):
5134 /// - **Same tenant** (`own_tenant == peer_tenant`): admitted when the
5135 /// peer's tier matches a [`MeshPeer::Tier`] rule, or when there are no
5136 /// `Tier` rules at all (historical "empty `allow_from` = allow all
5137 /// same-tenant").
5138 /// - **Cross tenant**: admitted only when an explicit
5139 /// [`MeshPeer::CrossTenant`] entry matches the peer's
5140 /// `(tenant, namespace, name)`.
5141 pub fn admits_peer(
5142 &self,
5143 own_tenant: &TenantId,
5144 peer_tenant: &TenantId,
5145 peer_namespace: &NamespaceId,
5146 peer_name: &MeshIdent,
5147 peer_tier: &TierTag,
5148 ) -> bool {
5149 if own_tenant == peer_tenant {
5150 let mut has_tier_rule = false;
5151 for peer in &self.allow_from {
5152 if let MeshPeer::Tier(t) = peer {
5153 has_tier_rule = true;
5154 if t == peer_tier {
5155 return true;
5156 }
5157 }
5158 }
5159 // No same-tenant tier restriction declared → admit all same-tenant.
5160 !has_tier_rule
5161 } else {
5162 self.allow_from.iter().any(|peer| {
5163 matches!(
5164 peer,
5165 MeshPeer::CrossTenant { tenant, namespace, name }
5166 if tenant == peer_tenant
5167 && namespace == peer_namespace
5168 && name == peer_name
5169 )
5170 })
5171 }
5172 }
5173}
5174
5175/// The name by which a workload is addressed **within its own tenant** (W206 /
5176/// R558-F3), given every `(namespace, identity)` pair present in that tenant.
5177///
5178/// Within a tenant, a workload is reached by its short mesh `identity` when that
5179/// identity is unique across the tenant's namespaces. When two namespaces
5180/// expose the same identity, the name is ambiguous, so both are disambiguated
5181/// by a namespace prefix — `<namespace>.<identity>` (e.g. `yah.runner` vs
5182/// `noisetable.runner`). Cross-tenant addressing always uses the full FQN
5183/// ([`WorkloadSpec::fq_mesh_identity`]) and is out of scope here.
5184pub fn intra_tenant_address(
5185 namespace: &NamespaceId,
5186 identity: &MeshIdent,
5187 tenant_workloads: &[(NamespaceId, MeshIdent)],
5188) -> String {
5189 let collides = tenant_workloads
5190 .iter()
5191 .any(|(ns, id)| id == identity && ns != namespace);
5192 if collides {
5193 format!("{}.{}", namespace.0, identity.0)
5194 } else {
5195 identity.0.clone()
5196 }
5197}
5198
5199/// Public internet exposure via a Cloudflare tunnel route.
5200#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5201#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5202pub struct PublicExpose {
5203 /// Public hostname to route, e.g. `"api.noisetable.io"`. Semantic
5204 /// validation checks that this hostname is owned by a configured CF zone.
5205 pub hostname: String,
5206
5207 /// Container-side port to route traffic to. Shape validation requires this
5208 /// port to appear in `expose.mesh.ports`.
5209 pub port: u16,
5210
5211 /// TLS configuration for the public endpoint.
5212 pub tls: PublicTls,
5213}
5214
5215/// TLS mode for a public endpoint.
5216#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5217#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5218#[serde(rename_all = "snake_case")]
5219pub enum PublicTls {
5220 /// Cloudflare manages the TLS certificate (default; requires a proxied DNS
5221 /// record in the configured zone).
5222 CfManaged,
5223
5224 /// User-supplied certificate referenced by name in the yubaba secret store.
5225 UserCertRef { name: String },
5226}
5227
5228/// Operator-facing exposure via a Tailscale ACL tag.
5229#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
5230#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
5231pub struct OperatorExpose {
5232 /// Tailscale ACL tag granting access, e.g. `"tag:noisetable-ops"`. Semantic
5233 /// validation checks that this tag exists in the cluster's Tailscale ACL.
5234 pub tailscale_tag: String,
5235
5236 /// Container-side port to expose to Tailscale-authorized operators.
5237 pub port: u16,
5238}
5239
5240// ── ImageRef helpers ──────────────────────────────────────────────────────────
5241
5242impl ImageRef {
5243 /// The all-zeros sha256 digest that marks an image reference as **not
5244 /// content-pinned**. No real image can carry it, so a build that never
5245 /// injected a compile-time digest (dev builds) or a catalog image that
5246 /// isn't published-and-pinned yet lands on this sentinel. This is the
5247 /// single source of truth both the catalog emitter
5248 /// (`task::default_image::catalog_image`, which writes it) and the
5249 /// container-runtime resolvers ([`Self::pull_ref`], via kamaji) agree on —
5250 /// keeping them here means they cannot drift. [`testing::TEST_DIGEST`] is
5251 /// the same value re-exported for fixtures.
5252 pub const UNPINNED_DIGEST: &'static str =
5253 "sha256:0000000000000000000000000000000000000000000000000000000000000000";
5254
5255 /// Parse a full digest-pinned image reference —
5256 /// `[registry/]repo[:tag]@sha256:<hex>` — into its parts.
5257 ///
5258 /// This is the public door onto the same parser the `ImageRef` string-form
5259 /// `Deserialize` arm uses, so a config that spells an image as one string
5260 /// (a qed `step.image`, a transform recipe) and a config that spells it as
5261 /// a struct land on identical semantics. A bare tag is rejected: the whole
5262 /// point of the string form is that it carries the digest.
5263 pub fn parse_pinned(s: &str) -> Result<Self, String> {
5264 compose_import::parse_pinned_image_ref(s)
5265 }
5266
5267 /// Format this reference as a Docker-compatible image string,
5268 /// `{registry}/{repository}:{tag}@{digest}`. Tag is included for human
5269 /// readability; the digest is what the pull resolves against. Always emits
5270 /// the digest — this is the display/logging form; use [`Self::pull_ref`]
5271 /// for the string handed to a container runtime.
5272 pub fn docker_ref(&self) -> String {
5273 format!("{}/{}:{}@{}", self.registry, self.repository, self.tag, self.digest)
5274 }
5275
5276 /// True when this reference carries a real content-addressed digest, i.e.
5277 /// its digest is not the all-zeros [`Self::UNPINNED_DIGEST`] sentinel.
5278 pub fn is_pinned(&self) -> bool {
5279 self.digest != Self::UNPINNED_DIGEST
5280 }
5281
5282 /// The reference string to hand a container runtime for pull/resolve.
5283 ///
5284 /// - **Pinned** (real digest): `{registry}/{repository}:{tag}@{digest}` —
5285 /// content-addressed, the reproducible path.
5286 /// - **Unpinned** (all-zeros [`Self::UNPINNED_DIGEST`]): `{registry}/{repository}:{tag}`
5287 /// — tag-only. No registry or local store holds an image under the
5288 /// sentinel digest, so `…@sha256:0000…` can never resolve; a
5289 /// tag-pulled or locally-built image is keyed by `registry/repo:tag`.
5290 /// This is the tag-fallback path that lets a not-yet-published catalog
5291 /// image (e.g. a from-source build-worker image) still pull by tag.
5292 pub fn pull_ref(&self) -> String {
5293 if self.is_pinned() {
5294 format!("{}/{}:{}@{}", self.registry, self.repository, self.tag, self.digest)
5295 } else {
5296 format!("{}/{}:{}", self.registry, self.repository, self.tag)
5297 }
5298 }
5299}
5300
5301// ── WorkloadRuntime trait ─────────────────────────────────────────────────────
5302
5303/// Shared interface for deploying and managing `WorkloadSpec` containers.
5304///
5305/// This is the keystone abstraction (R256-F10) that makes sim and cloud
5306/// literally interchangeable at the container level:
5307///
5308/// - **Camp/sim tier**: `LocalDockerRuntime` in `cloud` implements this trait
5309/// via the docker CLI pointed at OrbStack (or any Docker-compatible socket).
5310/// No mesh — containers communicate over OrbStack's bridge network.
5311///
5312/// - **Yubaba/cloud-HA tier**: `yubaba::runtime::ContainerRuntime` (gRPC to
5313/// containerd) will implement this trait. Mesh assignment is a separate
5314/// orchestration step on top (handled by yubaba's raft layer), not part
5315/// of the shared deploy/supervise interface.
5316///
5317/// Callers that type against `WorkloadRuntime` automatically work with both
5318/// backends. Reconcilers in `cloud` use it today; yubaba wires its own impl
5319/// when R276 Tier-3 lands.
5320#[async_trait::async_trait]
5321pub trait WorkloadRuntime: Send + Sync {
5322 /// Deploy a workload described by `spec`. Pulls the image if needed,
5323 /// creates and starts the container, and returns an opaque workload ID
5324 /// (typically the container name derived from `spec.name`).
5325 ///
5326 /// Idempotent: re-deploying a running workload replaces it cleanly.
5327 async fn deploy_workload(&self, spec: &WorkloadSpec) -> anyhow::Result<String>;
5328
5329 /// Tear down a deployed workload — stop the process and remove all
5330 /// associated state. No-op when the workload is already gone.
5331 async fn teardown_workload(&self, name: &str) -> anyhow::Result<()>;
5332
5333 /// Returns `true` when the named workload is currently running (i.e.
5334 /// the container process is alive and has not exited).
5335 async fn is_running(&self, name: &str) -> anyhow::Result<bool>;
5336
5337 /// Probe the runtime backend. Returns `true` when the backend socket is
5338 /// reachable and healthy (e.g. docker daemon up, containerd gRPC up).
5339 /// Used by health endpoints and startup checks.
5340 async fn runtime_health(&self) -> anyhow::Result<bool>;
5341}
5342
5343// ── Tests ─────────────────────────────────────────────────────────────────────
5344
5345#[cfg(test)]
5346mod tests {
5347 use super::*;
5348
5349 // ── R658-B1 `routes` belongs at the top level, not inside [build] ─────────
5350
5351 /// The canonical `mesofact-static` manifest shape: `routes` above the
5352 /// `[build]` header, where TOML keeps it top-level.
5353 #[test]
5354 fn mesofact_static_routes_parse_at_the_top_level() {
5355 let src = r#"
5356schema_version = 1
5357kind = "mesofact-static"
5358routes = "./mesofact.routes.ts"
5359
5360[build]
5361command = "bun run build"
5362out_dir = "dist"
5363"#;
5364 let Workload::MesofactStatic(site) =
5365 toml::from_str::<Workload>(src).expect("canonical shape must parse")
5366 else {
5367 panic!("kind = \"mesofact-static\" must select MesofactStatic");
5368 };
5369 assert_eq!(site.routes, PathBuf::from("./mesofact.routes.ts"));
5370 assert_eq!(site.build.out_dir, PathBuf::from("dist"));
5371 }
5372
5373 /// The bug R658-B1 exists for: `routes` written *below* `[build]` is
5374 /// `build.routes` as far as TOML is concerned. `BuildConfig` used to
5375 /// discard the stray key, so this manifest parsed as far as the missing
5376 /// top-level field and blamed the wrong line — or, once `routes` had a
5377 /// default, would have deployed a site that enumerated no routes at all.
5378 ///
5379 /// `deny_unknown_fields` makes the misplacement itself the error, and the
5380 /// message names `routes`, which is the one thing the author needs to move.
5381 #[test]
5382 fn mesofact_static_routes_inside_build_is_rejected_by_name() {
5383 let src = r#"
5384schema_version = 1
5385kind = "mesofact-static"
5386
5387[build]
5388command = "bun run build"
5389out_dir = "dist"
5390routes = "./mesofact.routes.ts"
5391"#;
5392 let err = toml::from_str::<Workload>(src)
5393 .expect_err("`routes` under [build] must not parse silently")
5394 .to_string();
5395 assert!(
5396 err.contains("routes"),
5397 "the error must name the misplaced key so the fix is obvious; got: {err}"
5398 );
5399 }
5400
5401 /// Guard the general case, not just the one key that bit us: any unknown
5402 /// `[build]` key is refused rather than dropped on the floor.
5403 #[test]
5404 fn unknown_build_keys_are_refused_rather_than_ignored() {
5405 let src = r#"
5406command = "bun run build"
5407out_dir = "dist"
5408outdir = "dist"
5409"#;
5410 let err = toml::from_str::<BuildConfig>(src)
5411 .expect_err("a typo'd build key must not be silently ignored")
5412 .to_string();
5413 assert!(err.contains("outdir"), "got: {err}");
5414
5415 // …and the keys that ARE modelled still round-trip.
5416 let ok: BuildConfig = toml::from_str(
5417 r#"
5418command = "bun run build"
5419out_dir = "dist"
5420render_command = "mesofact-build render . --route {route}"
5421"#,
5422 )
5423 .expect("modelled keys must still parse");
5424 assert_eq!(ok.render_command.as_deref(), Some("mesofact-build render . --route {route}"));
5425 }
5426
5427 // ── R783-F1 / W324: container manifest vs wire spec ────────────────────────
5428
5429 /// The acceptance case. `crates/yah/cloud-admin/workload.toml` is the file
5430 /// that could not parse through the envelope at all (R658-B2 pinned it in
5431 /// `xtask/tests/workload_envelope.rs` as `missing field \`image\``): it is a
5432 /// Dockerfile recipe, and the envelope only knew digest-pinned specs.
5433 ///
5434 /// The `[process]` table is deliberately present — that file is read by
5435 /// `LocalProcessReconciler` on the dev mirror *and* `ContainerReconciler`
5436 /// on pond, so the container form must tolerate the other tier's table
5437 /// rather than reject the file (W324 §1).
5438 #[test]
5439 fn container_recipe_parses_including_the_other_tier_s_table() {
5440 let src = r#"
5441schema_version = 1
5442name = "yah-cloud-admin"
5443kind = "container"
5444
5445[build]
5446dockerfile = "Dockerfile"
5447context = "."
5448image = "yah-local/yah-cloud-admin:dev"
5449
5450[run]
5451port = 4325
5452host_port = 4326
5453
5454[run.env]
5455YAH_CLOUD_ADMIN_ADDR = "0.0.0.0:4325"
5456
5457[[run.mounts]]
5458host = ".yah/infra"
5459container = "/workspace/.yah/infra"
5460
5461[process]
5462cargo_package = "yah-cloud-admin"
5463port = 4325
5464"#;
5465 let workload = toml::from_str::<Workload>(src).expect("the recipe form must parse");
5466 assert_eq!(workload.kind_str(), "container");
5467
5468 let recipe = workload
5469 .container_manifest()
5470 .and_then(ContainerManifest::as_recipe)
5471 .expect("a [build] table selects the recipe form");
5472 assert_eq!(recipe.name, "yah-cloud-admin");
5473 assert_eq!(recipe.build.dockerfile, PathBuf::from("Dockerfile"));
5474 assert_eq!(recipe.build.context, Some(PathBuf::from(".")));
5475 assert_eq!(
5476 recipe.build.image.as_deref(),
5477 Some("yah-local/yah-cloud-admin:dev")
5478 );
5479 assert_eq!(recipe.run.port, Some(4325));
5480 assert_eq!(recipe.run.host_port, Some(4326));
5481 assert_eq!(
5482 recipe.run.env.get("YAH_CLOUD_ADMIN_ADDR").map(String::as_str),
5483 Some("0.0.0.0:4325")
5484 );
5485 assert_eq!(recipe.run.mounts.len(), 1);
5486 assert!(recipe.run.mounts[0].read_only, "mounts default to read-only");
5487
5488 // The recipe has no spec — that is the whole point of the split.
5489 assert!(workload.container_spec().is_none());
5490 }
5491
5492 /// The other branch: no `[build]` table means the flat fields are a
5493 /// digest-pinned `WorkloadSpec`, exactly as before the split.
5494 #[test]
5495 fn container_reference_still_parses_as_a_workload_spec() {
5496 let spec = archetype_test_spec("noisetable-api");
5497 let toml_src = toml::to_string(&Workload::container(spec.clone())).expect("serialize");
5498 assert!(
5499 toml_src.contains("kind = \"container\""),
5500 "the on-disk form stays flat + internally tagged: {toml_src}"
5501 );
5502
5503 let back = toml::from_str::<Workload>(&toml_src).expect("deserialize");
5504 assert_eq!(back.container_spec(), Some(&spec));
5505 }
5506
5507 /// Explicit-branch deserialize exists so this error survives. Under
5508 /// `#[serde(untagged)]` it would read "data did not match any variant of
5509 /// untagged enum ContainerManifest", which tells an author nothing.
5510 #[test]
5511 fn a_malformed_container_reference_still_names_the_missing_field() {
5512 let src = r#"
5513schema_version = 1
5514kind = "container"
5515name = "noisetable-api"
5516image = "ghcr.io/noisetable/api:v1@sha256:0000000000000000000000000000000000000000000000000000000000000000"
5517replicas = 1
5518"#;
5519 let err = toml::from_str::<Workload>(src)
5520 .expect_err("a reference missing a required field must not parse")
5521 .to_string();
5522 assert!(err.contains("missing field `tier`"), "got: {err}");
5523 }
5524
5525 /// The one file that names neither marker. `missing field \`image\`` would
5526 /// send a recipe author off to add a field their form does not have, so
5527 /// the error names both forms instead.
5528 #[test]
5529 fn a_container_with_neither_image_nor_build_names_both_forms() {
5530 let src = r#"
5531schema_version = 1
5532kind = "container"
5533name = "yah-cloud-admin"
5534
5535[run]
5536port = 4325
5537"#;
5538 let err = toml::from_str::<Workload>(src)
5539 .expect_err("neither form is declared")
5540 .to_string();
5541 assert!(err.contains("image"), "got: {err}");
5542 assert!(err.contains("[build]"), "got: {err}");
5543 }
5544
5545 /// W324 §5's invariant, as a signature: there is no path from a recipe to
5546 /// a `WorkloadSpec` that does not name a digest.
5547 #[test]
5548 fn a_recipe_lowers_only_once_a_build_has_produced_a_digest() {
5549 let recipe = ContainerBuild {
5550 schema_version: SchemaVersion::V1,
5551 name: "yah-cloud-admin".into(),
5552 build: ContainerBuildStep {
5553 dockerfile: "Dockerfile".into(),
5554 context: Some(".".into()),
5555 image: Some("yah-local/yah-cloud-admin:dev".into()),
5556 },
5557 run: ContainerRunConfig {
5558 port: Some(4325),
5559 host_port: Some(4326),
5560 env: BTreeMap::from([("A".to_string(), "b".to_string())]),
5561 mounts: vec![ContainerMount {
5562 host: ".yah/infra".into(),
5563 container: "/workspace/.yah/infra".into(),
5564 read_only: true,
5565 }],
5566 },
5567 };
5568
5569 let digest = testing::test_digest();
5570 let spec = recipe
5571 .clone()
5572 .into_spec(&digest, TierTag("private".into()))
5573 .expect("a well-formed digest lowers");
5574 assert_eq!(spec.name, "yah-cloud-admin");
5575 assert_eq!(spec.image.digest, digest);
5576 assert_eq!(spec.image.repository, "yah-local/yah-cloud-admin");
5577 assert_eq!(spec.image.tag, "dev");
5578 assert_eq!(spec.expose.mesh.numbers(), vec![4325]);
5579 assert_eq!(spec.env.len(), 1);
5580 assert_eq!(spec.volumes.len(), 1);
5581
5582 // A bare tag is not a digest. Lowering must fail rather than mint a
5583 // spec that lies about being content-addressed (R438-T3).
5584 let err = recipe
5585 .into_spec("dev", TierTag("private".into()))
5586 .expect_err("an unpinned digest must not lower");
5587 assert!(err.contains("sha256"), "got: {err}");
5588 }
5589
5590 /// A recipe is a first-class on-disk value: it survives a write/read of
5591 /// the manifest unchanged. The other half of the gate — that the same
5592 /// value is *refused* by postcard — is in `tests/round_trip.rs`, which
5593 /// also pins the reference form's byte layout.
5594 #[test]
5595 fn a_recipe_round_trips_on_disk_under_the_container_kind() {
5596 let recipe = Workload::Container(ContainerManifest::Recipe(ContainerBuild {
5597 schema_version: SchemaVersion::V1,
5598 name: "yah-cloud-admin".into(),
5599 build: ContainerBuildStep::default(),
5600 run: ContainerRunConfig::default(),
5601 }));
5602 assert_eq!(recipe.kind_str(), "container");
5603
5604 let src = toml::to_string(&recipe).expect("a recipe serializes to disk");
5605 assert!(src.contains("kind = \"container\""), "{src}");
5606 let back: Workload = toml::from_str(&src).expect("and parses back");
5607 assert_eq!(back, recipe);
5608 }
5609
5610 // ── R603-T5 durable forge produced convention ──────────────────────────────
5611
5612 #[test]
5613 fn forge_produced_ident_parse() {
5614 assert_eq!(forge_produced::forge_id_from_ident("forge.abc123"), Some("abc123"));
5615 assert_eq!(forge_produced::forge_id_from_ident("svc.web"), None);
5616 assert_eq!(forge_produced::forge_id_from_ident("abc123"), None);
5617 }
5618
5619 #[test]
5620 fn forge_produced_host_path_translates_under_convention_dir() {
5621 let hp = forge_produced::host_path(
5622 "fid",
5623 std::path::Path::new("/yah/produced/librusty_v8.tar.gz"),
5624 )
5625 .expect("path under the convention dir translates");
5626 assert_eq!(
5627 hp,
5628 PathBuf::from("/var/lib/yah/qed/produced/fid/librusty_v8.tar.gz")
5629 );
5630 }
5631
5632 #[test]
5633 fn forge_produced_host_path_rejects_paths_outside_convention_dir() {
5634 assert_eq!(
5635 forge_produced::host_path("fid", std::path::Path::new("/tmp/x.tar.gz")),
5636 None,
5637 "a path outside /yah/produced has no durable host mapping"
5638 );
5639 }
5640
5641 #[test]
5642 fn forge_produced_host_path_rejects_traversal() {
5643 // A `..` component must never let a read escape the per-forge dir.
5644 assert_eq!(
5645 forge_produced::host_path(
5646 "fid",
5647 std::path::Path::new("/yah/produced/../../etc/passwd")
5648 ),
5649 None,
5650 "traversal out of the per-forge dir must be refused"
5651 );
5652 }
5653
5654 #[test]
5655 fn forge_produced_durable_mount_shape() {
5656 let m = forge_produced::durable_mount("fid");
5657 assert_eq!(m.target, PathBuf::from("/yah/produced"));
5658 assert!(!m.read_only, "the build must be able to write to it");
5659 assert_eq!(
5660 m.source,
5661 VolumeSource::Bind {
5662 host_path: PathBuf::from("/var/lib/yah/qed/produced/fid"),
5663 }
5664 );
5665 }
5666
5667 const HASH_64: &str = "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890";
5668
5669 #[test]
5670 fn blake_hash_accepts_64_hex() {
5671 let h: BlakeHash = toml::from_str(&format!("x = \"{HASH_64}\""))
5672 .map(|t: toml::Table| t["x"].as_str().unwrap().to_owned())
5673 .map(|s| serde_json::from_value(serde_json::Value::String(s)).unwrap())
5674 .unwrap();
5675 assert_eq!(h.0, HASH_64);
5676 }
5677
5678 #[test]
5679 fn blake_hash_rejects_wrong_length() {
5680 let short = "abcdef";
5681 let res: Result<BlakeHash, _> =
5682 serde_json::from_value(serde_json::Value::String(short.into()));
5683 assert!(res.is_err());
5684 }
5685
5686 #[test]
5687 fn blake_hash_rejects_non_hex() {
5688 let bad = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
5689 let res: Result<BlakeHash, _> =
5690 serde_json::from_value(serde_json::Value::String(bad.into()));
5691 assert!(res.is_err());
5692 }
5693
5694 fn image_ref(digest: &str) -> ImageRef {
5695 ImageRef {
5696 registry: "ghcr.io".into(),
5697 repository: "yah-ai/rusty-v8-musl-builder".into(),
5698 tag: "latest".into(),
5699 digest: digest.into(),
5700 }
5701 }
5702
5703 #[test]
5704 fn is_pinned_distinguishes_real_digest_from_sentinel() {
5705 assert!(!image_ref(ImageRef::UNPINNED_DIGEST).is_pinned());
5706 assert!(!image_ref(&testing::test_digest()).is_pinned());
5707 assert!(image_ref("sha256:deadbeef").is_pinned());
5708 }
5709
5710 #[test]
5711 fn pull_ref_pinned_carries_tag_and_digest() {
5712 assert_eq!(
5713 image_ref("sha256:deadbeef").pull_ref(),
5714 "ghcr.io/yah-ai/rusty-v8-musl-builder:latest@sha256:deadbeef",
5715 );
5716 }
5717
5718 #[test]
5719 fn pull_ref_unpinned_falls_back_to_tag_only() {
5720 // An unpinned catalog image (all-zeros sentinel) resolves by tag —
5721 // no store holds `…@sha256:0000…`, so the tag is the only usable key.
5722 assert_eq!(
5723 image_ref(ImageRef::UNPINNED_DIGEST).pull_ref(),
5724 "ghcr.io/yah-ai/rusty-v8-musl-builder:latest",
5725 );
5726 }
5727
5728 #[test]
5729 fn test_digest_alias_is_the_unpinned_sentinel() {
5730 assert_eq!(testing::TEST_DIGEST, ImageRef::UNPINNED_DIGEST);
5731 }
5732
5733 #[test]
5734 fn static_asset_workload_round_trips() {
5735 let src = format!(
5736 r#"
5737schema_version = "V1"
5738
5739[[asset]]
5740filename = "whisper/distil-large-v3-q5_1.bin"
5741source = "sources/distil-large-v3-q5_1.bin"
5742blake3 = "{HASH_64}"
5743
5744[[asset]]
5745filename = "whisper/distil-large-v3-q4_0.bin"
5746source = "sources/distil-large-v3-q4_0.bin"
5747blake3 = "{HASH_64}"
5748
5749[aliases]
5750"whisper-default" = "whisper/distil-large-v3-q5_1.bin"
5751"#
5752 );
5753 let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
5754 assert_eq!(w.assets.len(), 2);
5755 assert_eq!(w.assets[0].filename, "whisper/distil-large-v3-q5_1.bin");
5756 assert_eq!(w.assets[0].blake3.0, HASH_64);
5757 assert_eq!(w.aliases["whisper-default"], "whisper/distil-large-v3-q5_1.bin");
5758
5759 let back = toml::to_string(&w).expect("serialize");
5760 let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
5761 assert_eq!(w, w2);
5762 }
5763
5764 #[test]
5765 fn license_round_trip_each_variant() {
5766 // Wire format is whatever serde's `rename_all = "kebab-case"` emits.
5767 // heck's kebab-case keeps letter→digit attached but splits digit→uppercase,
5768 // so `Apache2 → "apache2"` and `Bsd2Clause → "bsd2-clause"`.
5769 for (variant, on_wire) in [
5770 (License::Mit, "mit"),
5771 (License::Apache2, "apache2"),
5772 (License::Bsd2Clause, "bsd2-clause"),
5773 (License::Bsd3Clause, "bsd3-clause"),
5774 (License::Isc, "isc"),
5775 ] {
5776 let ser = serde_json::to_value(variant).expect("serialize");
5777 assert_eq!(ser, serde_json::Value::String(on_wire.into()));
5778 let back: License = serde_json::from_value(ser).expect("deserialize");
5779 assert_eq!(back, variant);
5780 }
5781 }
5782
5783 #[test]
5784 fn license_rejects_non_permissive_variants() {
5785 for unknown in ["GPL-3.0", "AGPL", "lgpl-2.1", "unknown", "MIT"] {
5786 let res: Result<License, _> =
5787 serde_json::from_value(serde_json::Value::String(unknown.into()));
5788 assert!(res.is_err(), "expected rejection for {unknown:?}");
5789 }
5790 }
5791
5792 #[test]
5793 fn fetch_source_round_trips() {
5794 let src = format!(
5795 r#"
5796url = "https://example.invalid/upstream.bin"
5797blake3 = "{HASH_64}"
5798license = "mit"
5799"#
5800 );
5801 let fs: FetchSource = toml::from_str(&src).expect("parse");
5802 assert_eq!(fs.url, "https://example.invalid/upstream.bin");
5803 assert_eq!(fs.blake3.0, HASH_64);
5804 assert_eq!(fs.license, License::Mit);
5805
5806 let back = toml::to_string(&fs).expect("serialize");
5807 let fs2: FetchSource = toml::from_str(&back).expect("re-parse");
5808 assert_eq!(fs, fs2);
5809 }
5810
5811 #[test]
5812 fn fetch_source_rejects_unknown_license() {
5813 let src = format!(
5814 r#"
5815url = "https://example.invalid/upstream.bin"
5816blake3 = "{HASH_64}"
5817license = "GPL-3.0"
5818"#
5819 );
5820 let res: Result<FetchSource, _> = toml::from_str(&src);
5821 assert!(res.is_err(), "expected non-permissive license to reject");
5822 }
5823
5824 #[test]
5825 fn asset_entry_derive_mode_round_trips() {
5826 let src = format!(
5827 r#"
5828schema_version = "V1"
5829
5830[[asset]]
5831filename = "whisper/distil-large-v3-q5_1.bin"
5832blake3 = "{HASH_64}"
5833
5834[asset.derive.fetch]
5835url = "https://example.invalid/ggml-distil-large-v3.bin"
5836blake3 = "{HASH_64}"
5837license = "mit"
5838
5839[asset.derive.transform]
5840recipe = "whisper-quantize"
5841params = {{ quant = "q5_1" }}
5842"#
5843 );
5844 let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
5845 assert_eq!(w.assets.len(), 1);
5846 let entry = &w.assets[0];
5847 assert!(entry.source.is_none());
5848 let derive = entry.derive.as_ref().expect("derive present");
5849 assert_eq!(derive.fetch.url, "https://example.invalid/ggml-distil-large-v3.bin");
5850 assert_eq!(derive.fetch.license, License::Mit);
5851 let transform = derive.transform.as_ref().expect("transform present");
5852 assert_eq!(transform.recipe, "whisper-quantize");
5853 assert_eq!(transform.params.get("quant").map(String::as_str), Some("q5_1"));
5854
5855 let back = toml::to_string(&w).expect("serialize");
5856 let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
5857 assert_eq!(w, w2);
5858 }
5859
5860 #[test]
5861 fn legacy_source_only_asset_serializes_without_derive_field() {
5862 // Verify the skip_serializing_if guards keep legacy TOMLs round-tripping
5863 // without ever emitting an empty `derive = ...` line.
5864 let src = format!(
5865 r#"
5866schema_version = "V1"
5867
5868[[asset]]
5869filename = "operator-curated.bin"
5870source = "sources/operator-curated.bin"
5871blake3 = "{HASH_64}"
5872"#
5873 );
5874 let w: StaticAssetWorkload = toml::from_str(&src).expect("parse");
5875 let back = toml::to_string(&w).expect("serialize");
5876 assert!(!back.contains("derive"), "serialized output leaked a derive field: {back}");
5877 let w2: StaticAssetWorkload = toml::from_str(&back).expect("re-parse");
5878 assert_eq!(w, w2);
5879 }
5880
5881 /// W212/R518: the `[asset.derive.lock]` block round-trips through TOML, and
5882 /// is omitted from output when absent (so non-derive / unlocked assets stay
5883 /// clean).
5884 #[test]
5885 fn derive_lock_round_trips_through_toml() {
5886 let toml = r#"
5887url = "https://example.invalid/config.json"
5888blake3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
5889license = "mit"
5890"#;
5891 let fetch: FetchSource = ::toml::from_str(toml).unwrap();
5892 let derive = AssetDerive {
5893 fetch,
5894 transform: Some(TransformSpec {
5895 recipe: "whisper-bundle-tar".into(),
5896 params: BTreeMap::new(),
5897 }),
5898 lock: Some(DeriveLock {
5899 input_hash: "1111111111111111111111111111111111111111111111111111111111111111".into(),
5900 output_blake3: "2222222222222222222222222222222222222222222222222222222222222222".into(),
5901 }),
5902 };
5903 let s = ::toml::to_string(&derive).unwrap();
5904 assert!(s.contains("[lock]"), "lock serialized: {s}");
5905 let back: AssetDerive = ::toml::from_str(&s).unwrap();
5906 assert_eq!(derive, back);
5907
5908 // Absent lock → no `[lock]` table in the output.
5909 let unlocked = AssetDerive { lock: None, ..derive };
5910 let s2 = ::toml::to_string(&unlocked).unwrap();
5911 assert!(!s2.contains("[lock]"), "unlocked must omit lock: {s2}");
5912 }
5913
5914 #[test]
5915 fn shape_static_asset_rejects_both_source_and_derive() {
5916 use crate::validate::{shape_static_asset, FieldPath, ShapeError};
5917
5918 let entry = AssetEntry {
5919 filename: "ambiguous.bin".into(),
5920 source: Some("sources/ambiguous.bin".into()),
5921 derive: Some(AssetDerive {
5922 fetch: FetchSource {
5923 url: "https://example.invalid/x".into(),
5924 blake3: BlakeHash(HASH_64.into()),
5925 license: License::Mit,
5926 },
5927 transform: None,
5928 lock: None,
5929 }),
5930 blake3: BlakeHash(HASH_64.into()),
5931 };
5932 let w = StaticAssetWorkload {
5933 schema_version: SchemaVersion::V1,
5934 assets: vec![entry],
5935 aliases: BTreeMap::new(),
5936 };
5937 let err = shape_static_asset(&w).expect_err("XOR violated");
5938 match err {
5939 ShapeError::Field { path: FieldPath::Asset(0, "source"), .. } => {}
5940 other => panic!("expected Asset(0, \"source\") shape error, got {other:?}"),
5941 }
5942 }
5943
5944 #[test]
5945 fn shape_static_asset_rejects_neither_source_nor_derive() {
5946 use crate::validate::{shape_static_asset, FieldPath, ShapeError};
5947
5948 let entry = AssetEntry {
5949 filename: "empty.bin".into(),
5950 source: None,
5951 derive: None,
5952 blake3: BlakeHash(HASH_64.into()),
5953 };
5954 let w = StaticAssetWorkload {
5955 schema_version: SchemaVersion::V1,
5956 assets: vec![entry],
5957 aliases: BTreeMap::new(),
5958 };
5959 let err = shape_static_asset(&w).expect_err("XOR violated");
5960 match err {
5961 ShapeError::Field { path: FieldPath::Asset(0, "source"), .. } => {}
5962 other => panic!("expected Asset(0, \"source\") shape error, got {other:?}"),
5963 }
5964 }
5965
5966 #[test]
5967 fn shape_static_asset_accepts_either_mode() {
5968 use crate::validate::shape_static_asset;
5969
5970 let legacy = AssetEntry {
5971 filename: "a.bin".into(),
5972 source: Some("sources/a.bin".into()),
5973 derive: None,
5974 blake3: BlakeHash(HASH_64.into()),
5975 };
5976 let derived = AssetEntry {
5977 filename: "b.bin".into(),
5978 source: None,
5979 derive: Some(AssetDerive {
5980 fetch: FetchSource {
5981 url: "https://example.invalid/b".into(),
5982 blake3: BlakeHash(HASH_64.into()),
5983 license: License::Apache2,
5984 },
5985 transform: None,
5986 lock: None,
5987 }),
5988 blake3: BlakeHash(HASH_64.into()),
5989 };
5990 let w = StaticAssetWorkload {
5991 schema_version: SchemaVersion::V1,
5992 assets: vec![legacy, derived],
5993 aliases: BTreeMap::new(),
5994 };
5995 shape_static_asset(&w).expect("both modes accepted");
5996 }
5997
5998 #[test]
5999 fn image_ref_string_form_rejects_bare_tag() {
6000 let res: Result<ImageRef, _> =
6001 serde_json::from_value(serde_json::Value::String("node:20".into()));
6002 let err = res.expect_err("bare-tag must reject");
6003 let msg = format!("{err}");
6004 assert!(msg.contains("digest"), "error should mention digest: {msg}");
6005 }
6006
6007 #[test]
6008 fn image_ref_string_form_accepts_digest_pinned() {
6009 let pinned = format!("node:20@sha256:{HASH_64}");
6010 let img: ImageRef =
6011 serde_json::from_value(serde_json::Value::String(pinned.clone())).expect("parse");
6012 assert_eq!(img.registry, "docker.io");
6013 assert_eq!(img.repository, "library/node");
6014 assert_eq!(img.tag, "20");
6015 assert_eq!(img.digest, format!("sha256:{HASH_64}"));
6016 }
6017
6018 #[test]
6019 fn image_ref_string_form_accepts_ghcr_with_pin() {
6020 let pinned = format!("ghcr.io/foo/bar:v1.7.4@sha256:{HASH_64}");
6021 let img: ImageRef =
6022 serde_json::from_value(serde_json::Value::String(pinned)).expect("parse");
6023 assert_eq!(img.registry, "ghcr.io");
6024 assert_eq!(img.repository, "foo/bar");
6025 assert_eq!(img.tag, "v1.7.4");
6026 assert!(img.digest.starts_with("sha256:"));
6027 }
6028
6029 #[test]
6030 fn image_ref_string_form_rejects_non_sha256_digest() {
6031 for bad in [
6032 "node:20@md5:abcdef",
6033 "node:20@sha1:abcdef",
6034 "node:20@sha256:",
6035 "node:20@sha256:zzznothex",
6036 ] {
6037 let res: Result<ImageRef, _> =
6038 serde_json::from_value(serde_json::Value::String(bad.into()));
6039 assert!(res.is_err(), "expected reject for {bad:?}");
6040 }
6041 }
6042
6043 #[test]
6044 fn image_ref_struct_form_rejects_missing_digest() {
6045 // Digest is now structurally required (R438-T3). Struct-form payloads
6046 // without `digest` must fail at serde-deserialize.
6047 let v = serde_json::json!({
6048 "registry": "ghcr.io",
6049 "repository": "noisetable/api",
6050 "tag": "v1.4.2",
6051 });
6052 let res: Result<ImageRef, _> = serde_json::from_value(v);
6053 assert!(res.is_err(), "missing digest must reject");
6054 }
6055
6056 #[test]
6057 fn image_ref_struct_form_round_trips_through_toml() {
6058 let img = ImageRef {
6059 registry: "ghcr.io".into(),
6060 repository: "ggerganov/whisper.cpp".into(),
6061 tag: "v1.7.4".into(),
6062 digest: format!("sha256:{HASH_64}"),
6063 };
6064 let toml_doc = toml::to_string(&img).expect("serialize");
6065 let back: ImageRef = toml::from_str(&toml_doc).expect("re-parse");
6066 assert_eq!(img, back);
6067 }
6068
6069 /// R546-B7: assert the shape real files use. This test previously fed the
6070 /// EXTERNALLY-tagged wrapping-table form (`[static-asset]` +
6071 /// `[[static-asset.asset]]`), which no on-disk `workload.toml` has ever
6072 /// used — so it stayed green while `yah cloud apply` was broken for every
6073 /// static-asset component. The flat `kind = "..."` form below is what every
6074 /// workload.toml in the workspace is written in.
6075 #[test]
6076 fn workload_envelope_dispatches_static_asset() {
6077 let src = format!(
6078 r#"
6079kind = "static-asset"
6080schema_version = "V1"
6081
6082[[asset]]
6083filename = "foo/bar.bin"
6084source = "sources/bar.bin"
6085blake3 = "{HASH_64}"
6086"#
6087 );
6088 let w: Workload = toml::from_str(&src).expect("parse");
6089 assert!(matches!(w, Workload::StaticAsset(_)));
6090 }
6091
6092 /// R546-B7: the format branch, both directions. Human-readable formats get
6093 /// the flat `kind`-tagged shape; postcard keeps the externally-tagged
6094 /// variant-index encoding the kamaji UDS depends on (R590-B3). Regressing
6095 /// either side breaks a different half of the system, so pin both.
6096 #[test]
6097 fn workload_envelope_is_tagged_in_toml_and_external_in_postcard() {
6098 let src = format!(
6099 r#"
6100kind = "static-asset"
6101schema_version = "V1"
6102
6103[[asset]]
6104filename = "foo/bar.bin"
6105source = "sources/bar.bin"
6106blake3 = "{HASH_64}"
6107"#
6108 );
6109 let w: Workload = toml::from_str(&src).expect("parse flat TOML");
6110
6111 // Human-readable round-trips stay flat — no wrapping table.
6112 let json = serde_json::to_string(&w).expect("serialize json");
6113 assert!(json.contains("\"kind\":\"static-asset\""), "got {json}");
6114 assert!(
6115 !json.contains("{\"static-asset\":"),
6116 "human-readable output must not be externally tagged: {json}"
6117 );
6118 assert_eq!(
6119 serde_json::from_str::<Workload>(&json).expect("re-parse json"),
6120 w
6121 );
6122
6123 // postcard is non-self-describing: it can only round-trip because the
6124 // binary branch never asks for deserialize_any.
6125 let bytes = postcard::to_allocvec(&w).expect("postcard encode");
6126 assert_eq!(
6127 postcard::from_bytes::<Workload>(&bytes).expect("postcard decode"),
6128 w
6129 );
6130 }
6131
6132 // ── R572-F1: lifecycle archetype discriminator ─────────────────────────
6133
6134 fn archetype_test_spec(name: &str) -> WorkloadSpec {
6135 WorkloadSpec::for_forge(
6136 name,
6137 ImageRef {
6138 registry: "ghcr.io".into(),
6139 repository: "yah/test".into(),
6140 tag: "latest".into(),
6141 digest: testing::test_digest(),
6142 },
6143 TierTag("infra".into()),
6144 vec![],
6145 )
6146 }
6147
6148 #[test]
6149 fn explicit_archetype_round_trips_through_json_and_wins_over_inference() {
6150 for archetype in [
6151 LifecycleArchetype::Server,
6152 LifecycleArchetype::Appliance,
6153 LifecycleArchetype::Job,
6154 ] {
6155 let mut spec = archetype_test_spec("explicit");
6156 // Volumes present + restart_policy Always would infer Appliance
6157 // (see effective_archetype_infers_* below) — deliberately
6158 // mismatched against every archetype under test so the
6159 // assertion actually proves the explicit field wins, not that
6160 // it happens to agree with inference.
6161 spec.volumes = vec![VolumeMount {
6162 source: VolumeSource::Named { name: "data".into() },
6163 target: PathBuf::from("/data"),
6164 read_only: false,
6165 }];
6166 spec.restart_policy = RestartPolicy::Always;
6167 spec.archetype = Some(archetype);
6168
6169 let json = serde_json::to_string(&spec).expect("serialize");
6170 assert!(
6171 json.contains("\"archetype\""),
6172 "explicit archetype must be present on the wire"
6173 );
6174 let back: WorkloadSpec = serde_json::from_str(&json).expect("deserialize");
6175 assert_eq!(spec, back, "spec did not survive JSON round-trip");
6176 assert_eq!(back.archetype, Some(archetype));
6177 assert_eq!(
6178 back.effective_archetype(),
6179 archetype,
6180 "explicit archetype must win over the volumes/restart_policy inference"
6181 );
6182 }
6183 }
6184
6185 #[test]
6186 fn archetype_serializes_as_null_when_none() {
6187 let mut spec = archetype_test_spec("omitted");
6188 spec.archetype = None;
6189 let json = serde_json::to_value(&spec).expect("to_value");
6190 // Postcard-native (R590-B3): no `skip_serializing_if` anywhere on the
6191 // graph, so every field is always on the wire — a None Option is an
6192 // explicit `null`, not an absent key. The binary UDS wire is positional
6193 // and requires the slot to be present.
6194 assert_eq!(json.get("archetype"), Some(&serde_json::Value::Null));
6195 }
6196
6197 #[test]
6198 fn spec_without_archetype_field_deserializes_to_none() {
6199 // Simulates an on-disk spec written before R572-F1: no `archetype`
6200 // key at all. Omitting the key must still parse to None (the additive-
6201 // default contract) even though we now always *emit* the field.
6202 let mut spec = archetype_test_spec("pre-existing");
6203 spec.archetype = None;
6204 let mut json = serde_json::to_value(&spec).expect("to_value");
6205 json.as_object_mut().unwrap().remove("archetype");
6206 let back: WorkloadSpec = serde_json::from_value(json).expect("deserialize");
6207 assert_eq!(back.archetype, None);
6208 }
6209
6210 #[test]
6211 fn effective_archetype_infers_appliance_from_volumes_when_field_absent() {
6212 // Pre-R572 behavior: a workload with a volume was understood (by
6213 // convention, never a type) to be stateful/pinned. Confirm that
6214 // meaning is preserved bit-for-bit through effective_archetype().
6215 let mut spec = archetype_test_spec("appliance-inferred");
6216 spec.volumes = vec![VolumeMount {
6217 source: VolumeSource::Named { name: "pgdata".into() },
6218 target: PathBuf::from("/var/lib/postgresql/data"),
6219 read_only: false,
6220 }];
6221 spec.restart_policy = RestartPolicy::Always;
6222 spec.archetype = None;
6223 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Appliance);
6224 }
6225
6226 #[test]
6227 fn effective_archetype_infers_job_from_restart_never_when_field_absent() {
6228 // Pre-R572 behavior: RestartPolicy::Never + no volumes is the forge
6229 // run-once convention (see RestartPolicy::Never's own doc comment) —
6230 // structurally a job. WorkloadSpec::for_forge already produces
6231 // exactly this shape; isolate the pure-inference path by clearing
6232 // the explicit archetype for_forge now sets.
6233 let mut spec = archetype_test_spec("job-inferred");
6234 assert!(spec.volumes.is_empty());
6235 assert!(matches!(spec.restart_policy, RestartPolicy::Never));
6236 spec.archetype = None;
6237 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Job);
6238 }
6239
6240 #[test]
6241 fn effective_archetype_defaults_to_server_as_the_common_case_when_field_absent() {
6242 // Pre-R572 behavior: no volumes + a restartable policy (the common
6243 // stateless-web-server shape) inferred as movable/fungible.
6244 let mut spec = archetype_test_spec("server-inferred");
6245 spec.restart_policy = RestartPolicy::Always;
6246 spec.archetype = None;
6247 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Server);
6248 }
6249
6250 // ── R860-T1 / W338: requirement vocabulary ──────────────────────────────
6251
6252 /// An ordinary `anywhere` + `wait` requirement — what a `depends_on` entry
6253 /// has always meant, written the long way.
6254 fn wait_requirement(ident: &str) -> Requirement {
6255 Requirement {
6256 ident: MeshIdent(ident.into()),
6257 locality: Locality::Anywhere,
6258 supply: Supply::Wait,
6259 provides: None,
6260 }
6261 }
6262
6263 /// A `local` + `self` requirement carrying its provider — the sidecar
6264 /// shape, W338's motivating case. `ident` must be the provider's own mesh
6265 /// identity, which `archetype_test_spec` spells `forge.<name>`.
6266 fn self_requirement(provider_name: &str) -> Requirement {
6267 let provider = archetype_test_spec(provider_name);
6268 Requirement {
6269 ident: provider.expose.mesh.identity.clone(),
6270 locality: Locality::Local,
6271 supply: Supply::SelfProvision,
6272 provides: Some(Box::new(provider)),
6273 }
6274 }
6275
6276 #[test]
6277 fn locality_and_supply_use_the_wire_spellings_the_design_names() {
6278 // The TOML in W338 is written against these strings; a rename here is a
6279 // silent break of every manifest on disk. `self` in particular cannot
6280 // be the variant name (Rust keyword), so it is a serde rename and needs
6281 // guarding rather than trusting rename_all.
6282 assert_eq!(
6283 serde_json::to_string(&Locality::Anywhere).unwrap(),
6284 "\"anywhere\""
6285 );
6286 assert_eq!(
6287 serde_json::to_string(&Locality::PreferLocal).unwrap(),
6288 "\"prefer-local\""
6289 );
6290 assert_eq!(serde_json::to_string(&Locality::Local).unwrap(), "\"local\"");
6291 assert_eq!(serde_json::to_string(&Supply::Wait).unwrap(), "\"wait\"");
6292 assert_eq!(
6293 serde_json::to_string(&Supply::SelfProvision).unwrap(),
6294 "\"self\""
6295 );
6296
6297 assert_eq!(
6298 serde_json::from_str::<Supply>("\"self\"").unwrap(),
6299 Supply::SelfProvision
6300 );
6301 assert_eq!(
6302 serde_json::from_str::<Locality>("\"prefer-local\"").unwrap(),
6303 Locality::PreferLocal
6304 );
6305 }
6306
6307 #[test]
6308 fn a_requirement_omitting_locality_and_supply_defaults_to_the_depends_on_meaning() {
6309 // Folding `depends_on` into `requires` must not change any existing
6310 // spec's meaning, which is only true if the defaults are exactly the
6311 // old behaviour.
6312 let req: Requirement =
6313 serde_json::from_str(r#"{"ident":"headscale-db"}"#).expect("bare ident must parse");
6314 assert_eq!(req.locality, Locality::Anywhere);
6315 assert_eq!(req.supply, Supply::Wait);
6316 assert_eq!(req.provides, None);
6317 }
6318
6319 #[test]
6320 fn a_self_provisioned_requirement_round_trips_its_nested_provider_spec() {
6321 // `provides` makes WorkloadSpec recursive. Confirm the box survives a
6322 // JSON round trip rather than trusting the derive.
6323 let mut spec = archetype_test_spec("headscale");
6324 spec.requires = vec![self_requirement("replicator")];
6325
6326 let json = serde_json::to_string(&spec).expect("serialize");
6327 let back: WorkloadSpec = serde_json::from_str(&json).expect("deserialize");
6328 assert_eq!(back, spec);
6329
6330 let provided = back.requires[0]
6331 .provides
6332 .as_ref()
6333 .expect("the nested provider spec must survive the round trip");
6334 assert_eq!(provided.expose.mesh.identity, back.requires[0].ident);
6335 }
6336
6337 #[test]
6338 fn effective_requirements_returns_requires_verbatim_when_depends_on_is_empty() {
6339 let mut spec = archetype_test_spec("requires-only");
6340 spec.depends_on = vec![];
6341 spec.requires = vec![
6342 Requirement {
6343 ident: MeshIdent("headscale-db".into()),
6344 locality: Locality::PreferLocal,
6345 supply: Supply::Wait,
6346 provides: None,
6347 },
6348 self_requirement("replicator"),
6349 ];
6350
6351 assert_eq!(spec.effective_requirements(), spec.requires);
6352 }
6353
6354 #[test]
6355 fn effective_requirements_folds_depends_on_into_anywhere_wait() {
6356 // The back-compat projection: a pre-R860 spec carries everything in
6357 // `depends_on`, and reading `requires` alone would call it
6358 // requirement-free.
6359 let mut spec = archetype_test_spec("depends-on-only");
6360 spec.depends_on = vec![MeshIdent("noisetable-db".into()), MeshIdent("redis".into())];
6361 spec.requires = vec![];
6362
6363 assert_eq!(
6364 spec.effective_requirements(),
6365 vec![
6366 wait_requirement("noisetable-db"),
6367 wait_requirement("redis"),
6368 ]
6369 );
6370 }
6371
6372 #[test]
6373 fn effective_requirements_dedups_by_ident_and_requires_wins() {
6374 // An ident in both fields is the author restating one dependency with a
6375 // locality, not two edges — so the richer entry survives and the folded
6376 // `depends_on` projection is dropped, order following `requires` first.
6377 let mut spec = archetype_test_spec("overlap");
6378 spec.depends_on = vec![
6379 MeshIdent("headscale-db".into()),
6380 MeshIdent("only-in-depends-on".into()),
6381 ];
6382 spec.requires = vec![Requirement {
6383 ident: MeshIdent("headscale-db".into()),
6384 locality: Locality::Local,
6385 supply: Supply::Wait,
6386 provides: None,
6387 }];
6388
6389 let effective = spec.effective_requirements();
6390 assert_eq!(
6391 effective,
6392 vec![
6393 Requirement {
6394 ident: MeshIdent("headscale-db".into()),
6395 locality: Locality::Local,
6396 supply: Supply::Wait,
6397 provides: None,
6398 },
6399 wait_requirement("only-in-depends-on"),
6400 ],
6401 "the `requires` entry must win and the ident must appear exactly once"
6402 );
6403 }
6404
6405 #[test]
6406 fn effective_requirements_is_empty_when_neither_field_is_set() {
6407 let mut spec = archetype_test_spec("neither");
6408 spec.depends_on = vec![];
6409 spec.requires = vec![];
6410 assert!(spec.effective_requirements().is_empty());
6411 }
6412
6413 // ── R860-T1: `requires` shape validation ────────────────────────────────
6414
6415 /// Assert `shape` rejects `spec` with an error naming `requires[0]` and
6416 /// mentioning `needle`, so a failure points at the rule that fired.
6417 fn assert_requires_rejected(spec: &WorkloadSpec, needle: &str) {
6418 let err = validate::shape(spec).expect_err("shape must reject this spec");
6419 let rendered = err.to_string();
6420 assert!(
6421 rendered.contains("requires[0]"),
6422 "error must name the offending requirement; got: {rendered}"
6423 );
6424 assert!(
6425 rendered.contains(needle),
6426 "error must explain the rule ({needle:?}); got: {rendered}"
6427 );
6428 }
6429
6430 #[test]
6431 fn a_valid_requires_list_passes_shape_validation() {
6432 let mut spec = archetype_test_spec("valid-requires");
6433 spec.requires = vec![
6434 wait_requirement("headscale-db"),
6435 self_requirement("replicator"),
6436 ];
6437 validate::shape(&spec).expect("a well-formed requires list must pass");
6438 }
6439
6440 #[test]
6441 fn self_supply_without_a_provides_spec_is_rejected() {
6442 let mut spec = archetype_test_spec("self-without-provides");
6443 spec.requires = vec![Requirement {
6444 ident: MeshIdent("replicator".into()),
6445 locality: Locality::Local,
6446 supply: Supply::SelfProvision,
6447 provides: None,
6448 }];
6449 assert_requires_rejected(&spec, "no `provides` spec");
6450 }
6451
6452 #[test]
6453 fn wait_supply_carrying_a_provides_spec_is_rejected() {
6454 // The other direction matters just as much: a spec attached to a
6455 // `wait` requirement has no owner, so nothing would ever deploy it and
6456 // the author's intent is silently lost.
6457 let mut spec = archetype_test_spec("wait-with-provides");
6458 let mut req = self_requirement("replicator");
6459 req.supply = Supply::Wait;
6460 spec.requires = vec![req];
6461 assert_requires_rejected(&spec, "would have no owner");
6462 }
6463
6464 #[test]
6465 fn a_provides_spec_naming_a_different_identity_is_rejected() {
6466 // Each member keeps its own mesh identity (W338), and it has to be the
6467 // identity the edge points at — otherwise the provider is not the thing
6468 // the requirer asked for and is not discoverable as it.
6469 let mut spec = archetype_test_spec("identity-mismatch");
6470 let mut req = self_requirement("replicator");
6471 req.ident = MeshIdent("something-else".into());
6472 spec.requires = vec![req];
6473 assert_requires_rejected(&spec, "the provider keeps its own mesh identity");
6474 }
6475
6476 #[test]
6477 fn a_provides_spec_that_itself_self_provisions_is_rejected() {
6478 // The depth bound. Without it, `provides` is an arbitrarily deep tree
6479 // that placement would have to flatten before scheduling anything.
6480 let mut spec = archetype_test_spec("too-deep");
6481 let mut req = self_requirement("replicator");
6482 req.provides
6483 .as_mut()
6484 .expect("self_requirement always carries a provider")
6485 .requires = vec![self_requirement("replicator-of-the-replicator")];
6486 spec.requires = vec![req];
6487 assert_requires_rejected(&spec, "bounded at one");
6488 }
6489
6490 #[test]
6491 fn a_provides_spec_that_only_waits_is_accepted_at_depth_one() {
6492 // The bound is on `self` supply, not on nesting a `requires` list at
6493 // all — a provider may still name things it does not deploy.
6494 let mut spec = archetype_test_spec("nested-wait-ok");
6495 let mut req = self_requirement("replicator");
6496 req.provides
6497 .as_mut()
6498 .expect("self_requirement always carries a provider")
6499 .requires = vec![wait_requirement("object-storage")];
6500 spec.requires = vec![req];
6501 validate::shape(&spec).expect("a nested `wait` requirement is within the depth bound");
6502 }
6503
6504 #[test]
6505 fn a_repeated_requirement_ident_is_rejected() {
6506 let mut spec = archetype_test_spec("repeated-ident");
6507 spec.requires = vec![
6508 wait_requirement("headscale-db"),
6509 Requirement {
6510 ident: MeshIdent("headscale-db".into()),
6511 locality: Locality::Local,
6512 supply: Supply::Wait,
6513 provides: None,
6514 },
6515 ];
6516 let err = validate::shape(&spec)
6517 .expect_err("a duplicate ident must be rejected")
6518 .to_string();
6519 assert!(err.contains("requires[1]"), "got: {err}");
6520 assert!(err.contains("declared twice"), "got: {err}");
6521 }
6522
6523 #[test]
6524 fn a_requirement_naming_the_spec_itself_is_rejected() {
6525 let mut spec = archetype_test_spec("self-naming");
6526 spec.requires = vec![wait_requirement(&spec.expose.mesh.identity.0.clone())];
6527 assert_requires_rejected(&spec, "cannot be its own provider");
6528 }
6529
6530 // ── R594-F2: public-ingress appliance (container-shaped, not a new
6531 // Workload variant — see Workload::Container's doc comment) ───────────
6532
6533 #[test]
6534 fn ingress_marked_spec_is_appliance_and_carries_public_ip_placement_requirement() {
6535 let mut spec = archetype_test_spec("public-ingress");
6536 spec.archetype = Some(LifecycleArchetype::Appliance);
6537 spec.annotations.insert(
6538 REQUIRES_TAINT_ANNOTATION.to_string(),
6539 PUBLIC_IP_TAINT.to_string(),
6540 );
6541
6542 assert_eq!(
6543 spec.effective_archetype(),
6544 LifecycleArchetype::Appliance,
6545 "ingress must be pinned-per-node/non-drainable, the R572 appliance sense"
6546 );
6547 assert_eq!(
6548 spec.requires_taint(),
6549 Some(PUBLIC_IP_TAINT),
6550 "ingress must declare it can only land on a public-ip-tainted node"
6551 );
6552
6553 // No taint exists to match against yet (R572-F3) and nothing
6554 // enforces placement yet (R572-F5) — confirm this ticket stays
6555 // declarative-only by checking a spec with no requirement stays
6556 // unaffected.
6557 let unrelated = archetype_test_spec("unrelated");
6558 assert_eq!(unrelated.requires_taint(), None);
6559 }
6560
6561 #[test]
6562 fn ingress_marked_spec_round_trips_through_json_as_a_container_workload() {
6563 // Mirrors the on-disk envelope: the externally-tagged `container`
6564 // variant wrapping the WorkloadSpec, exactly like every other
6565 // container-shaped workload. No new Workload variant, no new
6566 // discriminator.
6567 let mut inner = archetype_test_spec("public-ingress");
6568 inner.archetype = Some(LifecycleArchetype::Appliance);
6569 inner.annotations.insert(
6570 REQUIRES_TAINT_ANNOTATION.to_string(),
6571 PUBLIC_IP_TAINT.to_string(),
6572 );
6573 let workload = Workload::container(inner.clone());
6574
6575 let json = serde_json::to_string(&workload).expect("serialize");
6576 assert!(json.contains("\"container\""));
6577 assert!(json.contains(REQUIRES_TAINT_ANNOTATION));
6578 assert!(json.contains(PUBLIC_IP_TAINT));
6579
6580 let back: Workload = serde_json::from_str(&json).expect("deserialize");
6581 match back.container_spec() {
6582 Some(spec) => {
6583 assert_eq!(spec, &inner);
6584 assert_eq!(spec.effective_archetype(), LifecycleArchetype::Appliance);
6585 assert_eq!(spec.requires_taint(), Some(PUBLIC_IP_TAINT));
6586 }
6587 None => panic!("expected a container reference workload, got {back:?}"),
6588 }
6589 }
6590
6591 // ── Nested-sandbox grant (R636-B2) ──────────────────────────────────────
6592
6593 #[test]
6594 fn nested_sandbox_marker_is_opt_in_and_reads_back() {
6595 // The half that matters: no workload gets the grant by default, so
6596 // adding the marker cannot widen anything already deployed.
6597 let plain = archetype_test_spec("ordinary-build");
6598 assert!(!plain.wants_nested_sandbox());
6599
6600 let mut buildkit = archetype_test_spec("build-image");
6601 buildkit.annotations.insert(
6602 NESTED_SANDBOX_ANNOTATION.to_string(),
6603 NESTED_SANDBOX_VALUE.to_string(),
6604 );
6605 assert!(buildkit.wants_nested_sandbox());
6606
6607 // Fails closed on any other value, same strictness as
6608 // `wants_host_network` — a typo must not hand out CAP_SETUID.
6609 let mut typo = archetype_test_spec("typo");
6610 typo.annotations
6611 .insert(NESTED_SANDBOX_ANNOTATION.to_string(), "Nested".to_string());
6612 assert!(!typo.wants_nested_sandbox());
6613 }
6614
6615 /// The three markers are independent axes: asking for host networking or
6616 /// native exec must not imply the capability grant, and vice versa.
6617 #[test]
6618 fn nested_sandbox_marker_is_independent_of_the_other_markers() {
6619 let mut host_net = archetype_test_spec("host-net");
6620 host_net.annotations.insert(
6621 HOST_NETWORK_ANNOTATION.to_string(),
6622 HOST_NETWORK_VALUE.to_string(),
6623 );
6624 assert!(host_net.wants_host_network());
6625 assert!(!host_net.wants_nested_sandbox());
6626
6627 let mut nested = archetype_test_spec("nested");
6628 nested.annotations.insert(
6629 NESTED_SANDBOX_ANNOTATION.to_string(),
6630 NESTED_SANDBOX_VALUE.to_string(),
6631 );
6632 assert!(nested.wants_nested_sandbox());
6633 assert!(!nested.wants_host_network());
6634 assert!(!nested.wants_native_exec());
6635 }
6636
6637 // ── Native exec marker (R577-T1 / W254) ─────────────────────────────────
6638
6639 #[test]
6640 fn native_exec_marker_is_opt_in_and_reads_back() {
6641 // Default: every forge workload is a container workload. This is the
6642 // half that matters most — the marker must not silently reroute the
6643 // Linux offload leg proven live on us-west-002.
6644 let plain = archetype_test_spec("linux-build");
6645 assert!(!plain.wants_native_exec());
6646
6647 let mut native = archetype_test_spec("darwin-build");
6648 native.annotations.insert(
6649 NATIVE_EXEC_ANNOTATION.to_string(),
6650 NATIVE_EXEC_VALUE.to_string(),
6651 );
6652 assert!(native.wants_native_exec());
6653
6654 // Any other value is not the opt-in — same strictness as
6655 // `wants_host_network`, so a typo fails closed onto the container
6656 // backend rather than escaping the sandbox.
6657 let mut typo = archetype_test_spec("typo");
6658 typo.annotations
6659 .insert(NATIVE_EXEC_ANNOTATION.to_string(), "Native".to_string());
6660 assert!(!typo.wants_native_exec());
6661 }
6662
6663 #[test]
6664 fn native_marked_spec_round_trips_through_json_as_a_container_workload() {
6665 // The point of the annotation shape: a native workload is still a
6666 // `Workload::Container` on the wire, so kamaji-proto's codec, yubaba
6667 // admission and the mesh-assignment path need no new variant.
6668 let mut inner = archetype_test_spec("darwin-build");
6669 inner.annotations.insert(
6670 NATIVE_EXEC_ANNOTATION.to_string(),
6671 NATIVE_EXEC_VALUE.to_string(),
6672 );
6673 let workload = Workload::container(inner.clone());
6674
6675 let json = serde_json::to_string(&workload).expect("serialize");
6676 assert!(json.contains(NATIVE_EXEC_ANNOTATION));
6677
6678 let back: Workload = serde_json::from_str(&json).expect("deserialize");
6679 match back.container_spec() {
6680 Some(spec) => {
6681 assert_eq!(spec, &inner);
6682 assert!(spec.wants_native_exec());
6683 }
6684 None => panic!("expected a container reference workload, got {back:?}"),
6685 }
6686 }
6687
6688 // ── MicroVM marker (R605-F8 / W325 §5) ──────────────────────────────────
6689
6690 #[test]
6691 fn microvm_marker_is_opt_in_and_reads_back() {
6692 let plain = archetype_test_spec("linux-build");
6693 assert!(!plain.wants_microvm());
6694
6695 let mut vm = archetype_test_spec("isolated-build");
6696 vm.annotations.insert(
6697 NATIVE_EXEC_ANNOTATION.to_string(),
6698 MICROVM_EXEC_VALUE.to_string(),
6699 );
6700 assert!(vm.wants_microvm());
6701
6702 // Fails closed onto the container backend, like every other marker: a
6703 // typo must not be read as "boot a VM", because the deploy that would
6704 // then be refused for lack of a microVM backend is a *worse* failure
6705 // than the container run the author actually spelled.
6706 let mut typo = archetype_test_spec("typo");
6707 typo.annotations
6708 .insert(NATIVE_EXEC_ANNOTATION.to_string(), "MicroVM".to_string());
6709 assert!(!typo.wants_microvm());
6710 assert!(!typo.wants_native_exec());
6711 }
6712
6713 #[test]
6714 fn exec_substrate_markers_are_mutually_exclusive_by_construction() {
6715 // This is the property that buys R605-F8 out of a refusal branch: the
6716 // three substrates share one annotation key, so no spec can ask for two
6717 // of them. Pinned because a later "let's give microVM its own key"
6718 // refactor would silently re-open the incoherent-pair case that
6719 // `yah.sandbox` + `yah.exec = native` still has to be refused for.
6720 assert_eq!(
6721 NATIVE_EXEC_ANNOTATION, NATIVE_EXEC_ANNOTATION,
6722 "both substrate values must live on the same key"
6723 );
6724 assert_ne!(NATIVE_EXEC_VALUE, MICROVM_EXEC_VALUE);
6725
6726 for value in [NATIVE_EXEC_VALUE, MICROVM_EXEC_VALUE, "", "container"] {
6727 let mut spec = archetype_test_spec("substrate");
6728 spec.annotations
6729 .insert(NATIVE_EXEC_ANNOTATION.to_string(), value.to_string());
6730 assert!(
6731 !(spec.wants_native_exec() && spec.wants_microvm()),
6732 "yah.exec={value:?} selected two substrates at once"
6733 );
6734 }
6735 }
6736
6737 #[test]
6738 fn microvm_marked_spec_round_trips_through_json_as_a_container_workload() {
6739 // Same zero-blast-radius claim as the native case: a microVM workload
6740 // is still `Workload::Container` on the wire, so kamaji-proto's codec
6741 // gains no variant and its positional postcard encoding does not move.
6742 let mut inner = archetype_test_spec("isolated-build");
6743 inner.annotations.insert(
6744 NATIVE_EXEC_ANNOTATION.to_string(),
6745 MICROVM_EXEC_VALUE.to_string(),
6746 );
6747 let workload = Workload::container(inner.clone());
6748
6749 let json = serde_json::to_string(&workload).expect("serialize");
6750 assert!(json.contains(MICROVM_EXEC_VALUE));
6751
6752 let back: Workload = serde_json::from_str(&json).expect("deserialize");
6753 match back.container_spec() {
6754 Some(spec) => {
6755 assert_eq!(spec, &inner);
6756 assert!(spec.wants_microvm());
6757 assert!(!spec.wants_native_exec());
6758 }
6759 None => panic!("expected a container reference workload, got {back:?}"),
6760 }
6761 }
6762
6763 // ── R850-P4: durability declaration ──────────────────────────────────────
6764
6765 fn durability_spec(pairs: &[(&str, &str)]) -> WorkloadSpec {
6766 let mut spec = archetype_test_spec("durable");
6767 for (k, v) in pairs {
6768 spec.annotations.insert((*k).into(), (*v).into());
6769 }
6770 spec
6771 }
6772
6773 /// The distinction the whole surface rests on. Every spec in the tree
6774 /// predates the annotation, so `None` has to keep meaning "nobody said" —
6775 /// and a workload that says `tier = "none"` has to be distinguishable from
6776 /// one that never considered the question, because only one of those is a
6777 /// finding.
6778 #[test]
6779 fn an_absent_declaration_and_a_declared_none_are_different_answers() {
6780 assert_eq!(durability_spec(&[]).durability().unwrap(), None);
6781
6782 let declared = durability_spec(&[(DURABILITY_TIER_ANNOTATION, "none")])
6783 .durability()
6784 .unwrap()
6785 .expect("tier = none is a declaration");
6786 assert_eq!(declared.tier, DurabilityTier::None);
6787 assert_eq!(declared.store, None);
6788 }
6789
6790 #[test]
6791 fn a_stream_tier_carries_its_store_rpo_and_state_size() {
6792 let d = durability_spec(&[
6793 (DURABILITY_TIER_ANNOTATION, "stream"),
6794 (DURABILITY_ENGINE_ANNOTATION, "turso"),
6795 (DURABILITY_STORE_ANNOTATION, "s3://backups/db"),
6796 (DURABILITY_SUBJECTS_ANNOTATION, "accounts.db"),
6797 (DURABILITY_RPO_ANNOTATION, "30"),
6798 (DURABILITY_STATE_MB_ANNOTATION, "100"),
6799 ])
6800 .durability()
6801 .unwrap()
6802 .expect("declared");
6803 assert_eq!(d.tier, DurabilityTier::Stream);
6804 assert_eq!(d.engine, Some(DurabilityEngine::Turso));
6805 assert_eq!(d.store.as_deref(), Some("s3://backups/db"));
6806 assert_eq!(d.subjects, vec!["accounts.db".to_string()]);
6807 assert_eq!(d.rpo_seconds, Some(30));
6808 assert_eq!(d.state_mb, Some(100));
6809 }
6810
6811 /// A misspelled tier must not read as "no backups configured". This is the
6812 /// one place the crate's usual permissive-fallback habit
6813 /// (`memory_request_mb`, `wants_host_network`) is actively wrong: a
6814 /// mistyped memory request costs a placement, a mistyped durability tier
6815 /// costs the database.
6816 #[test]
6817 fn a_misspelled_tier_is_refused_rather_than_read_as_undeclared() {
6818 let err = durability_spec(&[(DURABILITY_TIER_ANNOTATION, "streem")])
6819 .durability()
6820 .unwrap_err();
6821 assert_eq!(
6822 err,
6823 DurabilityDeclError::UnknownTier {
6824 value: "streem".into()
6825 }
6826 );
6827 assert!(err.to_string().contains("none|snapshot|dedup|stream"));
6828 }
6829
6830 /// The same failure one key over: `yah.durability.teir = "stream"` leaves a
6831 /// store behind with no tier, which without this check is indistinguishable
6832 /// from a workload that declared nothing at all.
6833 #[test]
6834 fn a_store_with_no_tier_key_names_the_likely_typo() {
6835 let err = durability_spec(&[(DURABILITY_STORE_ANNOTATION, "s3://backups/db")])
6836 .durability()
6837 .unwrap_err();
6838 assert_eq!(
6839 err,
6840 DurabilityDeclError::OrphanKey {
6841 key: DURABILITY_STORE_ANNOTATION
6842 }
6843 );
6844 assert!(err.to_string().contains("spelling"));
6845 }
6846
6847 #[test]
6848 fn a_tier_that_ships_bytes_must_name_where() {
6849 let err = durability_spec(&[(DURABILITY_TIER_ANNOTATION, "snapshot")])
6850 .durability()
6851 .unwrap_err();
6852 assert_eq!(
6853 err,
6854 DurabilityDeclError::MissingStore {
6855 tier: DurabilityTier::Snapshot
6856 }
6857 );
6858 // The refusal has to say why there is no default, or the next reader
6859 // adds one.
6860 assert!(err.to_string().contains("nobody chose"));
6861 }
6862
6863 #[test]
6864 fn a_store_alongside_tier_none_is_contradictory_and_refused() {
6865 let err = durability_spec(&[
6866 (DURABILITY_TIER_ANNOTATION, "none"),
6867 (DURABILITY_STORE_ANNOTATION, "s3://backups/db"),
6868 ])
6869 .durability()
6870 .unwrap_err();
6871 assert_eq!(err, DurabilityDeclError::StoreWithoutTier);
6872 }
6873
6874 /// Only tier 2 has a recovery point the spec can state. Accepting an RPO on
6875 /// a snapshot tier would let a report print a bound nothing enforces.
6876 #[test]
6877 fn an_rpo_on_a_snapshot_tier_is_refused() {
6878 let err = durability_spec(&[
6879 (DURABILITY_TIER_ANNOTATION, "snapshot"),
6880 (DURABILITY_STORE_ANNOTATION, "s3://backups/db"),
6881 (DURABILITY_RPO_ANNOTATION, "30"),
6882 ])
6883 .durability()
6884 .unwrap_err();
6885 assert_eq!(
6886 err,
6887 DurabilityDeclError::RpoOnNonStreamTier {
6888 tier: DurabilityTier::Snapshot
6889 }
6890 );
6891 }
6892
6893 #[test]
6894 fn an_unparseable_rpo_or_state_size_is_refused() {
6895 assert!(matches!(
6896 durability_spec(&[
6897 (DURABILITY_TIER_ANNOTATION, "stream"),
6898 (DURABILITY_STORE_ANNOTATION, "s3://b"),
6899 (DURABILITY_RPO_ANNOTATION, "2m"),
6900 ])
6901 .durability()
6902 .unwrap_err(),
6903 DurabilityDeclError::UnparseableRpo { .. }
6904 ));
6905
6906 assert!(matches!(
6907 durability_spec(&[
6908 (DURABILITY_TIER_ANNOTATION, "none"),
6909 (DURABILITY_STATE_MB_ANNOTATION, "100MB"),
6910 ])
6911 .durability()
6912 .unwrap_err(),
6913 DurabilityDeclError::UnparseableStateMb { .. }
6914 ));
6915 }
6916
6917 /// The declaration rides `annotations`, which is an existing map on an
6918 /// existing wire — so an older kamaji decodes a spec carrying it. Pinned
6919 /// because the reason this is not a struct field (R590-B3's positional
6920 /// postcard wire) is invisible from the call site.
6921 #[test]
6922 fn a_durability_declaration_round_trips_as_plain_annotations() {
6923 let spec = durability_spec(&[
6924 (DURABILITY_TIER_ANNOTATION, "stream"),
6925 (DURABILITY_ENGINE_ANNOTATION, "turso"),
6926 (DURABILITY_STORE_ANNOTATION, "s3://backups/db"),
6927 (DURABILITY_SUBJECTS_ANNOTATION, "accounts.db"),
6928 ]);
6929 let json = serde_json::to_string(&spec).expect("serialize");
6930 assert!(json.contains("yah.durability.tier"), "{json}");
6931 assert!(
6932 !json.contains("\"durability\""),
6933 "durability must not be a top-level field: {json}"
6934 );
6935 let back: WorkloadSpec = serde_json::from_str(&json).expect("deserialize");
6936 assert_eq!(back.durability().unwrap(), spec.durability().unwrap());
6937 }
6938
6939 // ── R850-F1: the engine and subject axes ─────────────────────────────────
6940
6941 /// The driving shape from R850: one appliance, one named volume, three
6942 /// turso databases inside it. The declaration has to carry all three by
6943 /// name, because a restore's unit is a file and "the volume" is not one.
6944 #[test]
6945 fn three_databases_in_one_volume_are_three_named_subjects() {
6946 let d = durability_spec(&[
6947 (DURABILITY_TIER_ANNOTATION, "stream"),
6948 (DURABILITY_ENGINE_ANNOTATION, "turso"),
6949 (DURABILITY_STORE_ANNOTATION, "s3://yah-backups/noisetable-account"),
6950 (
6951 DURABILITY_SUBJECTS_ANNOTATION,
6952 "accounts.db, passkeys.db ,sessions.db",
6953 ),
6954 ])
6955 .durability()
6956 .unwrap()
6957 .expect("declared");
6958 assert_eq!(d.subjects, vec!["accounts.db", "passkeys.db", "sessions.db"]);
6959 }
6960
6961 /// Gotcha (c) on R850-F1, closed: the three tier names are turso-backup's,
6962 /// so a Postgres appliance saying `tier = "stream"` was declaring something
6963 /// no code in this tree can do. It now cannot say it without also naming an
6964 /// engine, and the only engine with a restore path is the one that has one.
6965 #[test]
6966 fn a_bytes_shipping_tier_must_name_an_engine_and_only_turso_has_one() {
6967 let err = durability_spec(&[
6968 (DURABILITY_TIER_ANNOTATION, "stream"),
6969 (DURABILITY_STORE_ANNOTATION, "s3://b"),
6970 (DURABILITY_SUBJECTS_ANNOTATION, "a.db"),
6971 ])
6972 .durability()
6973 .unwrap_err();
6974 assert_eq!(
6975 err,
6976 DurabilityDeclError::MissingEngine {
6977 tier: DurabilityTier::Stream
6978 }
6979 );
6980
6981 let err = durability_spec(&[
6982 (DURABILITY_TIER_ANNOTATION, "stream"),
6983 (DURABILITY_ENGINE_ANNOTATION, "postgres"),
6984 (DURABILITY_STORE_ANNOTATION, "s3://b"),
6985 (DURABILITY_SUBJECTS_ANNOTATION, "a.db"),
6986 ])
6987 .durability()
6988 .unwrap_err();
6989 assert_eq!(
6990 err,
6991 DurabilityDeclError::UnknownEngine {
6992 value: "postgres".into()
6993 }
6994 );
6995 assert!(err.to_string().contains("no restore path"), "{err}");
6996 }
6997
6998 #[test]
6999 fn a_bytes_shipping_tier_must_name_its_databases() {
7000 let err = durability_spec(&[
7001 (DURABILITY_TIER_ANNOTATION, "snapshot"),
7002 (DURABILITY_ENGINE_ANNOTATION, "turso"),
7003 (DURABILITY_STORE_ANNOTATION, "s3://b"),
7004 ])
7005 .durability()
7006 .unwrap_err();
7007 assert_eq!(
7008 err,
7009 DurabilityDeclError::MissingSubjects {
7010 tier: DurabilityTier::Snapshot
7011 }
7012 );
7013 assert!(err.to_string().contains("guessing"), "{err}");
7014 }
7015
7016 /// `tier = "none"` ships nothing, so an engine or a subject list beside it
7017 /// is a half-edited declaration — the same shape `StoreWithoutTier`
7018 /// already refuses, and refused for the same reason: the reader cannot tell
7019 /// which half is the mistake.
7020 #[test]
7021 fn an_engine_or_subject_list_alongside_tier_none_is_refused() {
7022 assert_eq!(
7023 durability_spec(&[
7024 (DURABILITY_TIER_ANNOTATION, "none"),
7025 (DURABILITY_ENGINE_ANNOTATION, "turso"),
7026 ])
7027 .durability()
7028 .unwrap_err(),
7029 DurabilityDeclError::EngineWithoutTier
7030 );
7031 assert_eq!(
7032 durability_spec(&[
7033 (DURABILITY_TIER_ANNOTATION, "none"),
7034 (DURABILITY_SUBJECTS_ANNOTATION, "a.db"),
7035 ])
7036 .durability()
7037 .unwrap_err(),
7038 DurabilityDeclError::SubjectsWithoutTier
7039 );
7040 }
7041
7042 /// A subject is joined onto a host directory by something that then writes
7043 /// to it, so traversal is refused by name rather than normalized away.
7044 /// Silently rewriting a path a human typed is how the right bytes land in
7045 /// the wrong place.
7046 #[test]
7047 fn a_subject_cannot_escape_the_volume_it_is_scoped_to() {
7048 let bad = |subjects: &str| {
7049 durability_spec(&[
7050 (DURABILITY_TIER_ANNOTATION, "snapshot"),
7051 (DURABILITY_ENGINE_ANNOTATION, "turso"),
7052 (DURABILITY_STORE_ANNOTATION, "s3://b"),
7053 (DURABILITY_SUBJECTS_ANNOTATION, subjects),
7054 ])
7055 .durability()
7056 .unwrap_err()
7057 };
7058 assert_eq!(
7059 bad("/etc/passwd"),
7060 DurabilityDeclError::AbsoluteSubject {
7061 subject: "/etc/passwd".into()
7062 }
7063 );
7064 assert_eq!(
7065 bad("../../../etc/passwd"),
7066 DurabilityDeclError::TraversingSubject {
7067 subject: "../../../etc/passwd".into()
7068 }
7069 );
7070 assert_eq!(
7071 bad("data/./a.db"),
7072 DurabilityDeclError::TraversingSubject {
7073 subject: "data/./a.db".into()
7074 }
7075 );
7076 // A trailing comma truncates a list without looking like it did.
7077 assert_eq!(bad("a.db,"), DurabilityDeclError::EmptySubject);
7078 assert_eq!(
7079 bad("a.db,a.db"),
7080 DurabilityDeclError::DuplicateSubject {
7081 subject: "a.db".into()
7082 }
7083 );
7084 }
7085
7086 /// Nested subjects are legal — a workload is free to keep its databases in
7087 /// a subdirectory of the volume — so the traversal guard must reject `..`
7088 /// without rejecting every path containing a slash.
7089 #[test]
7090 fn a_subject_may_sit_in_a_subdirectory_of_the_volume() {
7091 let d = durability_spec(&[
7092 (DURABILITY_TIER_ANNOTATION, "dedup"),
7093 (DURABILITY_ENGINE_ANNOTATION, "turso"),
7094 (DURABILITY_STORE_ANNOTATION, "s3://b"),
7095 (DURABILITY_SUBJECTS_ANNOTATION, "db/accounts.db"),
7096 ])
7097 .durability()
7098 .unwrap()
7099 .expect("declared");
7100 assert_eq!(d.subjects, vec!["db/accounts.db".to_string()]);
7101 }
7102
7103 /// `yah.durability.engien = "turso"` must not read as "no backups
7104 /// configured" — the same orphan-key guard the store and RPO keys get.
7105 #[test]
7106 fn an_engine_or_subject_key_with_no_tier_names_the_likely_typo() {
7107 assert_eq!(
7108 durability_spec(&[(DURABILITY_ENGINE_ANNOTATION, "turso")])
7109 .durability()
7110 .unwrap_err(),
7111 DurabilityDeclError::OrphanKey {
7112 key: DURABILITY_ENGINE_ANNOTATION
7113 }
7114 );
7115 assert_eq!(
7116 durability_spec(&[(DURABILITY_SUBJECTS_ANNOTATION, "a.db")])
7117 .durability()
7118 .unwrap_err(),
7119 DurabilityDeclError::OrphanKey {
7120 key: DURABILITY_SUBJECTS_ANNOTATION
7121 }
7122 );
7123 }
7124}