{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.tarsana.io/image-spec/v2.schema.json",
  "title": "Tarsana image spec v2",
  "description": "Declarative, distro-agnostic description of a tenant image. The spec is the source of truth; the image artifact is derived and disposable. Nothing in this schema is distro-specific: switching `distro` selects a different compiler backend, never a different schema shape.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "distro",
    "snapshot_id",
    "base_ref",
    "policy_class"
  ],

  "_gate_X145_G1": "GATE G1 (X145) -- CLOSED by X241 (2026-09-18, under D83: a defect fix, no MAJOR). The defect was never in this document: every `pattern` here is ECMA-262, where `^...$` means the whole string. It was in the READER -- python's jsonschema applies `pattern` with re.search and python's `$` matches before a trailing newline, so `\"2.0\\n\"` validated against `^2\\\\.(0|[1-9][0-9]*)$`. The fix is ONE place, `tools/tarsana_spec/schema.py` (`Validator`), which every validator construction under tools/ goes through and which compiles each `$` as end-of-string; `tests/test_x241_whole_string.py` fails the tree on a validator built from jsonschema directly, and drives the trailing-newline input at `tarsana-spec validate` as a process in both directions. DO NOT spell `\\\\A...\\\\Z` or `\\\\Z` into this document: `\\\\Z` is not ECMA-262, the validator REFUSES it as non-portable, and `$` already means what it says. See docs/X241-whole-string-means-whole-string.md.",

  "properties": {
    "schema_version": {
      "type": "string",
      "pattern": "^2\\.(0|[1-9][0-9]*)$",
      "description": "MAJOR.MINOR of the spec language. MINOR is additive and hash-preserving; MAJOR is breaking and changes the spec-hash domain tag. See docs/image-spec-v2.md. MAJOR 2 was taken by X123 under operator decisions D27 and D83 (2026-09-18): the only breaking change from 1.x is that `distro` no longer admits `rhel` or `suse`, so a 1.x spec migrates by editing this one field to `2.0` -- its layer hashes are unchanged and its spec-hash moves once, with the domain tag (`tarsana-image-spec/v2`). 1.x specs are refused by the version gate, never validated against a retired schema: nothing serves v1 side by side, because serving it would keep advertising two distros the factory does not build."
    },

    "metadata": {
      "type": "object",
      "additionalProperties": false,
      "description": "Non-semantic annotations. EXCLUDED from the canonical form and therefore from the spec-hash: editing metadata never invalidates a cache entry.",
      "properties": {
        "name": {"type": "string", "minLength": 1, "description": "A name for this spec, for people to read."},
        "tenant": {"type": "string", "minLength": 1, "description": "The tenant ID the spec belongs to. A submission for another tenant ID is refused; leave it out and the tenant ID the spec is sent for is used."},
        "description": {"type": "string", "description": "Free text describing what the image is for."},
        "annotations": {
          "type": "object",
          "additionalProperties": {"type": "string"},
          "description": "Free-form key/value notes. Not read by any build step."
        }
      }
    },

    "distro": {
      "enum": ["ubuntu", "debian", "almalinux"],
      "description": "Selects the compiler backend. This is the ONLY field that varies between the same image built for different userlands, and the enum advertises exactly what the estate can build (D83): `ubuntu` and `debian` through the apt backend, `almalinux` -- the one free RHEL-compatible distro D1 chose and P4.2-a wired -- through the dnf backend. `rhel` and `suse` are NOT here: no paid subscription is being acquired (D1), so a spec naming either is refused at submit time, by this enum, with `tarsana_spec.validate` naming D27 in the message (X123, 2026-09-18). Until X123 the two names stayed in the enum on purpose, refused by name only when a backend was loaded, with D1's reason: removing an enum value is not additive and §6 of docs/image-spec-v2.md said what that cost and who owned it. That limit was real -- it is exactly the MAJOR bump the estate deferred for 45 days -- and D27 (2026-08-27) and D83 (2026-09-18) lifted it by ruling the bump taken and its cache re-baseline accepted, which is why this is v2. `rocky` is deliberately still absent: stages/build/lib/primitives.sh routes the name, but backend-dnf.sh does not declare it and content/vuln/platforms.tsv excludes it, and an enum value that nothing builds is the same defect in the other direction (D83 amendment). It joins when the backend declares it and the vulnerability data covers it."
    },

    "snapshot_id": {
      "type": "string",
      "minLength": 1,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:+-]*$",
      "description": "Opaque, backend-interpreted identifier of the pinned mirror snapshot (e.g. an ubuntu snapshot timestamp, or a Pulp publication id for the RHEL-compatible family). Deliberately unstructured so no distro's snapshot convention leaks into the schema."
    },

    "base_ref": {
      "type": "string",
      "minLength": 1,
      "description": "Opaque, backend-interpreted reference to the base rootfs the build starts from (e.g. a debootstrap suite, a kickstart base, or a digest-pinned OCI ref). Resolved by the backend against `distro` + `snapshot_id`."
    },

    "repos": {
      "$ref": "#/$defs/repoList",
      "description": "Additional pinned package repositories available to every layer. Always permitted, including alongside an explicit `layers` stack."
    },

    "packages": {
      "$ref": "#/$defs/packageList",
      "description": "Shorthand: OS packages for the single node layer. Mutually exclusive with `layers`."
    },

    "files": {
      "$ref": "#/$defs/fileList",
      "description": "Shorthand: config files for the single node layer. Mutually exclusive with `layers`."
    },

    "service_accounts": {
      "$ref": "#/$defs/serviceAccountList",
      "description": "Shorthand: declared service accounts for the single node layer. Mutually exclusive with `layers`."
    },

    "workload": {
      "$ref": "#/$defs/workload",
      "description": "Shorthand: the tenant layer. Mutually exclusive with `layers`."
    },

    "state": {
      "$ref": "#/$defs/stateDeclaration",
      "description": "Shorthand: the state declarations for the single node layer. Mutually exclusive with `layers`. THIS IS THE LAYER-LEVEL `state` KEY (X166, D64 \u00a78) and it is NOT `packageEntry.state`, which is the `present`/`absent` enum on one package inside `packages[]`. Different level, different type, no structural collision -- see $defs/stateDeclaration."
    },

    "hardening_profile": {
      "type": "string",
      "enum": ["none", "tarsana-baseline", "cis-level1", "cis-level2", "stig", "pci-dss"],
      "default": "none",
      "description": "Sole input of the hardening layer. Profiles are distro-agnostic names; each backend maps a profile onto its own remediation content. Adding a profile is a MINOR bump. `pci-dss` was added by P4.3-b and, like `stig`, is served only where content/scap/profiles.tsv has a row for the platform: the SCAP Security Guide ships a native PCI-DSS control baseline for the RHEL family and none for Ubuntu, so a spec asking for it on ubuntu/noble is refused rather than approximated from CIS content."
    },

    "hardening_packages": {
      "type": "array",
      "description": "X212, operator decision D72. The packages the selected hardening_profile's remediation CAN ask the package manager to install -- DERIVED by the factory (`tarsana-remediation derive --spec <this file>`) from the pinned CIS content and DECLARED here, never hand-typed: the build refuses a spec whose declaration is not exactly what its profile derives, naming the packages. They are STAGED so a build with no network (a closure, X191/X198) can serve the remediation's own `apt-get install` / `dnf install` lines; they are NOT installed by any layer -- the remediation decides at run time which of them it installs. Optional and absent by default, which canonicalizes to nothing and keeps every existing spec's hash (MINOR, hash-preserving). The list lives in the tenant's spec because D64 s4(c) permits per-package knowledge here and nowhere in the factory.",
      "items": {
        "type": "string",
        "pattern": "^[A-Za-z0-9][A-Za-z0-9._+-]*$"
      }
    },

    "retain_generated_key_material": {
      "$ref": "#/$defs/generatedKeyMaterialOptOut",
      "description": "X339, operator decision D102: the generated key material a package's install script MINTS -- the class content/layers/generated-key-material.tsv names, X156 \u00a7C2's -- is NULLIFIED in every image BY DEFAULT, and this is the one way a spec keeps it: an opt-out IN WRITING, with a reason. Present means the image ships those bytes; absent (the default) means it does not. Never an environment variable (D92). The declaration, its reason and the class's paths reach the build record (plan.json, the recipe's inputs, and the provenance statement's externalParameters.spec.generatedKeyMaterial), and it moves the spec hash and the hash of the hardening layer and every layer above it, because it changes those layers' bytes; the base layer never retains (it is reused across specs, D18). Optional and absent by default, which canonicalizes to nothing and keeps every existing spec's hash (MINOR, hash-preserving). X358 (operator ruling R20): the trusted side's key-material scan (X202) ADMITS exactly this class's files under the declaration -- at the class's own paths, as the class's own generated output (under no other name in the image, PEM alone; not a path the spec writes itself or a package ships) -- and only when the provenance statement records this declaration exactly; the admitted files are named in the build's status record. Any other key material, or any without the declaration, is still refused at the harvest."
    },

    "policy_class": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]*$",
      "description": "The assurance/policy class this spec requests. Consumed by the P2.3 policy gate, which decides whether the class is granted. Free-form kebab-case so operators and tenants can define classes without a schema change."
    },

    "layers": {
      "type": "array",
      "minItems": 1,
      "description": "Explicit composition above the implied base and hardening layers. Order is semantic: all `node` layers must precede all `tenant` layers. Mutually exclusive with `packages` / `files` / `workload`.",
      "items": {"$ref": "#/$defs/layer"}
    },

    "placement": {
      "$ref": "#/$defs/placement",
      "description": "X248, operator decision D80 and the operator's ruling of 2026-09-25 (state/operator-rulings-20260925.md item 1): WHERE a server built from this spec is to live -- the provider, its server type, and the estate's own standard for the optional things a provider accepts at create time. OPTIONAL: a spec without it can be BUILT but not CREATED FROM. EXCLUDED from the canonical form and therefore from the spec-hash, exactly like `metadata`: the same image content is the same image wherever it lands, so two specs that differ only here share one spec-hash and one cache entry. The schema fixes the SHAPE; which provider ids exist, which of them have a built adapter, and which `options` each adapter supports is `content/placement/providers.tsv`, checked at submit by tools/tarsana_spec/placement.py."
    }
  },

  "allOf": [
    {
      "if": {"required": ["layers"]},
      "then": {
        "allOf": [
          {"not": {"required": ["packages"]}},
          {"not": {"required": ["files"]}},
          {"not": {"required": ["service_accounts"]}},
          {"not": {"required": ["state"]}},
          {"not": {"required": ["workload"]}}
        ]
      }
    }
  ],

  "$defs": {
    "packageList": {
      "type": "array",
      "description": "Unordered set. Canonicalization sorts by (name, version) and rejects conflicting duplicates.",
      "items": {"$ref": "#/$defs/packageEntry"}
    },

    "packageEntry": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._+-]*$",
          "description": "Shorthand for {\"name\": <string>}."
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["name"],
          "properties": {
            "name": {
              "type": "string",
              "pattern": "^[A-Za-z0-9][A-Za-z0-9._+-]*$",
              "description": "Package or group name as the backend knows it. The character class is the union of Debian and RPM naming rules."
            },
            "version": {
              "type": "string",
              "minLength": 1,
              "description": "Exact version pin. Omit to take whatever the pinned snapshot resolves to (still reproducible: the snapshot is the pin)."
            },
            "kind": {
              "enum": ["package", "group"],
              "default": "package",
              "description": "`group` covers apt tasks/metapackages, dnf groups and zypper patterns - one neutral concept so P4.2 needs no schema change."
            },
            "state": {
              "enum": ["present", "absent"],
              "default": "present",
              "description": "`absent` removes a package that the base image ships (used by hardening profiles). \u26a0 THIS IS THE PACKAGE-ENTRY `state` AND IT IS A DIFFERENT KEY FROM THE LAYER-LEVEL `state` X166 ADDED under D64 \u00a78. This one is an ENUM on ONE PACKAGE inside `packages[]` and says whether that package is in the image; that one is an OBJECT on a LAYER and says which directories the tenant declares persistent, volatile or exec. D64 \u00a78 chose the layer-level name deliberately, knowing this key was here: different level, different type, nothing can be written that satisfies both readings. See $defs/stateDeclaration."
            }
          }
        }
      ]
    },

    "fileList": {
      "type": "array",
      "description": "Unordered set keyed by path. Canonicalization sorts by path and rejects duplicate paths.",
      "items": {"$ref": "#/$defs/fileEntry"}
    },

    "fileEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "content"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^/(?:[^/\\u0000]+/)*[^/\\u0000]+$",
          "description": "Absolute destination path. No empty segments, no trailing slash; `.` and `..` segments are rejected by the validator."
        },
        "content": {
          "type": "string",
          "description": "Inline UTF-8 content. v1 deliberately has no external file reference: a spec must be self-contained so its hash cannot depend on ambient state. A digest-pinned `content_ref` is the planned MINOR extension."
        },
        "mode": {
          "type": "string",
          "pattern": "^0[0-7]{3}$",
          "default": "0644",
          "description": "Octal permission bits of the file, as a four-digit string."
        },
        "owner": {"type": "string", "minLength": 1, "default": "root", "description": "The account that owns the file, by name."},
        "group": {"type": "string", "minLength": 1, "default": "root", "description": "The group that owns the file, by name."}
      }
    },

    "serviceAccountList": {
      "type": "array",
      "description": "Unordered set keyed by name. Canonicalization sorts by name and rejects duplicate names. D64 \u00a74: a tenant DECLARES the account a package would otherwise allocate, so the id cannot drift between two builds of the same image.",
      "items": {"$ref": "#/$defs/serviceAccount"}
    },

    "serviceAccount": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "uid", "package"],
      "description": "One service account, mapped to the package that would otherwise create it. `name`, `uid` and `package` are required: an account with no id is exactly the drifting account this vocabulary exists to remove, and an account mapped to no package has nothing to be created before. `gid` defaults to `uid`; `description`, `shell` and `homedir` default to the platform's own answer and carry no default in the schema, because a default spelled here would be a distro-specific path.",
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z_][a-z0-9_-]*[$]?$",
          "maxLength": 32,
          "description": "Account name, and the name of the group created alongside it. The character class is shadow-utils' own NAME_REGEX, which both families use."
        },
        "uid": {
          "type": "integer",
          "minimum": 0,
          "maximum": 4294967293,
          "description": "The numeric user id, EXACTLY as declared. D64 \u00a74(c): the declared range is deliberately NOT fenced away from the distro's own system range, because a tenant migrating existing data onto this factory has to be able to name the id that data is already owned by. A collision is answered by REFUSAL at compile time, never by moving the tenant's number."
        },
        "gid": {
          "type": "integer",
          "minimum": 0,
          "maximum": 4294967293,
          "description": "The numeric group id of the group created with the account. Defaults to `uid`, which is what a sysusers.d `u` line with a single id does."
        },
        "description": {
          "type": "string",
          "maxLength": 255,
          "pattern": "^[^:\\t\\n\\r]*$",
          "description": "The GECOS field. `:` is refused because it separates the fields of the account database this becomes a row of."
        },
        "shell": {
          "type": "string",
          "pattern": "^/(?:[^/\\u0000:]+/)*[^/\\u0000:]+$",
          "description": "Absolute path to the login shell. Omit to take the platform's own nologin, which is what keeps this field distro-agnostic."
        },
        "homedir": {
          "type": "string",
          "pattern": "^/$|^/(?:[^/\\u0000:]+/)*[^/\\u0000:]+$",
          "description": "Absolute path to the home directory recorded in the account database. Omit to take the platform default. Declaring it does NOT create the directory: a datadir is the package's to create, and this vocabulary exists so the package creates it under an id the tenant pinned."
        },
        "package": {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._+-]*$",
          "description": "The package this account belongs to, as the backend knows it -- the mapping D64 \u00a74 asks for. The package must be declared `present` in the SAME layer: the account is created in that layer's packages-install step, before the package is configured, so a mapping to a package this layer does not install would order nothing."
        }
      }
    },

    "stateDeclaration": {
      "type": "object",
      "additionalProperties": false,
      "description": "The tenant's OPTIONAL state declarations for one layer (X166; operator decision D64 \u00a78, ARCHITECTURE.md \u00a710). Each list names DIRECTORIES, and each declaration is checked exactly like a directory the persistence census found by itself -- the same never-exclude row, the same estate base-system table, the same masked-spec-file rule, the same budget, the same owner-name rule. IT IS NEVER REQUIRED: a spec that declares nothing behaves exactly as it did before this vocabulary existed, and the canonical form carries no new key for it, so no cached layer re-hashes. A classifier that ever REQUIRED a declaration to proceed would be the per-package supported-list the operator rejected round 1 of this design over. \u26a0 NAMING: `packageEntry.state` is a different key at a different level -- see its own description.",
      "properties": {
        "persist": {
          "$ref": "#/$defs/statePathList",
          "description": "Directories that must be PERSISTENT on the node: bind-mounted from the state partition at their default path. This is the declaration for state the census cannot see -- a datadir a package creates only at RUNTIME, at a location compiled into its binary, appears in no file list, no install delta, no unit directive and no tmpfiles fragment. Admissible locations are the persist classes of ARCHITECTURE.md \u00a74 (`/var/lib/<d>`, `/var/www`, `/srv/<d>`, `/var/spool/<d>`, `/var/mail`, `/var/opt/<d>`, `/var/local`, `/var/backups`, `/home/<u>`) and `/etc/<d>` for the runtime-written class. An `/etc/<d>` declaration is a DIRECTORY bind and therefore MASKS any spec-written file under it (round-1 G3.3), which is a refusal and not a warning."
        },
        "volatile": {
          "$ref": "#/$defs/statePathList",
          "description": "Directory units the census DID route that the tenant demotes to a volatile read-write mount: the content is scratch, it need not survive a reboot, and it should not consume the state partition. A path the census did not route is REFUSED rather than accepted silently -- a demotion of nothing looks exactly like a demotion that worked."
        },
        "exec": {
          "$ref": "#/$defs/statePathList",
          "description": "Directory units whose bind must NOT carry `noexec`. The default is a measurement, not an opinion: a unit is `exec` when a file inside it already carries an exec bit, and `noexec` otherwise. This overrides that for a directory whose executable content arrives at runtime. A path that is not a directory unit is REFUSED."
        }
      }
    },

    "statePathList": {
      "type": "array",
      "description": "Unordered set of absolute directory paths. Canonicalization sorts and de-duplicates; `.` and `..` segments are refused by the validator, exactly as they are in `files[].path`, because an unresolved path cannot be compared against the unit set it would have to match.",
      "items": {
        "type": "string",
        "pattern": "^/(?:[^/\\u0000]+/)*[^/\\u0000]+$"
      }
    },

    "generatedKeyMaterialOptOut": {
      "type": "object",
      "additionalProperties": false,
      "required": ["reason"],
      "properties": {
        "reason": {
          "type": "string",
          "minLength": 1,
          "description": "Why this image must carry the key material its packages generated at build time, instead of each node minting its own at first start. Required and non-blank: D102 calls the opt-out a tenant's claim about their threat model, in writing, and a claim without words is the silent waiver D12 forbids. A reason of only whitespace is refused by the validator, not by a `pattern` -- an unanchored one would not be a whole-string rule (X241)."
        }
      }
    },

    "reservedPathExceptionList": {
      "type": "array",
      "description": "X253, operator ruling 8 of 2026-09-25 under D64 \u00a79 (default deny, declared exception) and D12 (exceptions get a NARROWER KEY). The ONLY door through the assembler's layer boundary gate: THIS layer may write each named path although `content/layers/reserved-paths.tsv` reserves it for `hardening`. Keyed as narrowly as the gate can see: this spec (the list is in the spec's canonical form, so declaring one moves the spec hash and the recipe hash), this layer, this EXACT path -- never a prefix, never another layer, never a `delete`. A path reserved for `base` or `node` (the account database among them) has NO door and a declaration naming one is refused; so is one naming a path nothing reserves, and one the layer never writes (a stale exception is a door with nobody behind it). Admitted exceptions are recorded in the assembly record and the build provenance. Unordered set keyed by path; canonicalization sorts and rejects duplicates.",
      "items": {"$ref": "#/$defs/reservedPathException"}
    },

    "reservedPathException": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "reason"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^/(?:[^/\\u0000]+/)*[^/\\u0000]+$",
          "description": "The EXACT absolute path this layer writes. No trailing slash (a directory is named by its own path, without one, and admits nothing beneath it); `.` and `..` segments are rejected by the validator."
        },
        "reason": {
          "type": "string",
          "minLength": 1,
          "description": "Why this layer must write a path the hardening profile owns. Required and non-blank: an exception without a reason is the silent waiver D12 forbids. A reason of only whitespace is refused by the validator, not by a `pattern` -- an unanchored one would not be a whole-string rule (X241)."
        }
      }
    },

    "repoList": {
      "type": "array",
      "description": "Unordered set keyed by id. Canonicalization sorts by id and rejects duplicate ids.",
      "items": {"$ref": "#/$defs/repoEntry"}
    },

    "repoEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "url", "signing_key"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$", "description": "A name for the repository, unique within the spec."},
        "url": {
          "type": "string",
          "pattern": "^https?://[^\\s]+$",
          "description": "Which URLs are actually allowed is a policy decision (P2.3), not a schema one - the schema only fixes the shape."
        },
        "signing_key": {
          "type": "object",
          "additionalProperties": false,
          "required": ["fingerprint"],
          "description": "The OpenPGP key the repository's metadata is signed with. Packages from the repository are accepted only under this key.",
          "properties": {
            "fingerprint": {
              "type": "string",
              "pattern": "^[A-F0-9]{40}$",
              "description": "Uppercase 40-hex key fingerprint. Uppercase is required rather than normalized, so canonicalization stays a pure reordering."
            },
            "url": {"type": "string", "pattern": "^https?://[^\\s]+$", "description": "Where the public key can be fetched. Optional."}
          }
        },
        "selectors": {
          "type": "array",
          "items": {"type": "string", "minLength": 1},
          "description": "Backend-interpreted repository selectors. ORDER IS SEMANTIC and preserved. apt: suite followed by components (e.g. [\"noble\", \"main\", \"universe\"]). dnf/zypper: usually empty."
        }
      }
    },

    "layer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "role"],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9-]*$",
          "description": "Layer identity within the spec. Must be unique."
        },
        "role": {
          "enum": ["node", "tenant"],
          "description": "`base` and `hardening` are reserved: those layers are implied by base_ref/snapshot_id and hardening_profile and are materialized by canonicalization."
        },
        "packages": {"$ref": "#/$defs/packageList"},
        "files": {"$ref": "#/$defs/fileList"},
        "repos": {"$ref": "#/$defs/repoList"},
        "service_accounts": {"$ref": "#/$defs/serviceAccountList"},
        "state": {"$ref": "#/$defs/stateDeclaration"},
        "reserved_path_exceptions": {"$ref": "#/$defs/reservedPathExceptionList"},
        "workload": {"$ref": "#/$defs/workload"}
      },
      "if": {"properties": {"role": {"const": "node"}}, "required": ["role"]},
      "then": {"not": {"required": ["workload"]}}
    },

    "workload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["containers"],
      "description": "Bring-your-own workload. Opaque tenant containers RUN ON the node; they are never unpacked into the OS image. This is the node/workload boundary that keeps the image reproducible: the OS is built from pinned packages, the workload is referenced by digest.",
      "properties": {
        "runtime": {
          "enum": ["systemd-unit", "kubernetes"],
          "default": "systemd-unit",
          "description": "How the node is configured to run the containers. Independent of `distro`."
        },
        "containers": {
          "type": "array",
          "minItems": 1,
          "description": "Unordered set keyed by name. Canonicalization sorts by name and rejects duplicate names.",
          "items": {"$ref": "#/$defs/container"}
        }
      }
    },

    "container": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "image"],
      "properties": {
        "name": {"type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$", "description": "The container's name, unique within the workload. It names the service the container runs as."},
        "image": {
          "type": "string",
          "pattern": "^[a-z0-9]([a-z0-9.-]*[a-z0-9])?(:[0-9]{1,5})?/[a-z0-9][a-z0-9._/-]*@sha256:[a-f0-9]{64}$",
          "description": "Digest-pinned OCI reference including an explicit registry host. Floating tags are rejected: an unpinned workload would make the node's configuration irreproducible."
        },
        "env": {
          "type": "object",
          "propertyNames": {"pattern": "^[A-Za-z_][A-Za-z0-9_]*$"},
          "additionalProperties": {"type": "string"},
          "description": "Non-secret environment only. Secrets are delivered at runtime by the credential vault (P5.2), never baked into a spec."
        },
        "args": {
          "type": "array",
          "items": {"type": "string"},
          "description": "ORDER IS SEMANTIC and preserved."
        },
        "ports": {
          "type": "array",
          "items": {"$ref": "#/$defs/port"},
          "description": "Unordered set. Canonicalization sorts by (container, protocol)."
        },
        "restart": {
          "enum": ["always", "on-failure", "never"],
          "default": "always",
          "description": "When the container is started again after it stops."
        }
      }
    },

    "port": {
      "type": "object",
      "additionalProperties": false,
      "required": ["container"],
      "properties": {
        "container": {"type": "integer", "minimum": 1, "maximum": 65535, "description": "The port the container listens on."},
        "host": {"type": "integer", "minimum": 1, "maximum": 65535, "description": "The port on the server that forwards to it. Omit to publish nothing on the server."},
        "protocol": {"enum": ["tcp", "udp"], "default": "tcp", "description": "The transport protocol of the port."}
      }
    },

    "placement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["provider", "server_type"],
      "description": "The placement block (X248, D80). Both `provider` and `server_type` are required once the block is present: D80 says a spec that is to be created from carries both.",
      "properties": {
        "provider": {
          "type": "string",
          "maxLength": 63,
          "pattern": "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$",
          "description": "The adapter's declared id (`ProviderAdapter.name`, as listed in content/placement/providers.tsv), never a display name. An id the estate does not know, and a known provider with no built adapter, are both refused at submit, each named as what it is; a built provider that is unreachable when a server is created is NOT a spec error."
        },
        "server_type": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$",
          "description": "The provider's own machine-type identifier, VERBATIM -- no case folding, no estate alias. Whether the provider sells it is the provider's answer at create time, not the schema's."
        },
        "region": {
          "type": "string",
          "maxLength": 63,
          "pattern": "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$",
          "description": "Optional. The provider's own location/zone/region identifier, verbatim. The alphabet is the one X219 and X220 made the adapters refuse outside of, because an adapter places this value in a URL path or host."
        },
        "options": {"$ref": "#/$defs/placementOptions"}
      }
    },

    "placementOptions": {
      "type": "object",
      "additionalProperties": false,
      "description": "THE ESTATE'S PLACEMENT VOCABULARY, declared HERE and nowhere else (X248 fence 2): tools/tarsana_spec/placement.py reads its keys from this object, the submit check and every adapter's declaration are checked against them, and content/placement/providers.tsv is generated against them. D80: the standard is the estate's own and an adapter TRANSLATES into it -- a provider's own option name is never a key here. Each key is derived from what the five built adapters' products accept at create time (docs/X248-the-spec-says-where-it-lives.md §3). A key no built adapter supports may not be added: a key nothing implements is a key nobody can test.",
      "properties": {
        "ipv6": {
          "type": "boolean",
          "description": "Whether the server gets a public IPv6 address."
        },
        "labels": {
          "type": "object",
          "description": "Key/value labels put on the server at the provider. The grammar is the narrowest the label-carrying products share (flat-tag providers carry `key=value`). Keys starting with `tarsana` are RESERVED: the adapters write their own records under that prefix (e.g. the mechanism label `status` reads back), and a tenant label must never be able to forge one.",
          "propertyNames": {
            "maxLength": 63,
            "pattern": "^(?!tarsana)[a-z0-9]([a-z0-9._-]*[a-z0-9])?$"
          },
          "additionalProperties": {
            "type": "string",
            "maxLength": 63,
            "pattern": "^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)?$"
          }
        }
      }
    }
  }
}
