# Edit the Declaration, Preserve the Document: Lossless Pigsty Inventory

> Why pig inventory separates YAML semantics from source bytes so scoped edits can preserve comments, ordering, anchors, and formatting.

---

LLMS index: [llms.txt](/llms.txt)

---

> **Decision date:** 2026-07-18<br>
> **Status:** Implemented and released in [pig v1.6.0](/release/pig-1.6.0/); the 2026-10-16 to 2026-10-17 refinements shipped in [pig v1.10.0](/release/pig-1.10.0/).<br>
> **Latest refinement:** The 2026-11-12 CMDB scalar fixes from [`f569d0d`](https://github.com/pgsty/pig/commit/f569d0d4f75e39942baf393a224f21a617b29699) are Released in [pig v1.11.0](https://github.com/pgsty/pig/releases/tag/v1.11.0).<br>
> **Current reference:** [`pig inventory`](/inventory/)<br>
> **Scope:** Static Pigsty Inventory inspection, scoped editing, validation, comparison, and safe writes.

## Decision {#decision}

PIG treats `pigsty.yml` as both a semantic declaration and a human-maintained source document.
Semantic parsers determine what the Inventory means; the original bytes remain authoritative for
how it is written. Scoped edits replace bounded source ranges and then reparse the complete
candidate before an atomic write.

This avoids a common YAML-tool failure: a logically correct edit that silently rewrites comments,
key order, quoting, anchors, block scalars, or line endings across the entire file.

## Context {#context}

Pigsty Inventories are long-lived operational assets. They contain topology, tuning, credentials,
comments, examples, anchors, and locally meaningful ordering. A conventional parse-mutate-serialize
cycle can produce a valid but unreviewable diff and may change constructs the operator never
selected.

At the same time, raw text editing without semantic validation can put invalid or contradictory
configuration on disk. The design needed source fidelity and whole-document correctness together.

## Alternatives considered {#alternatives}

- **Round-trip through one YAML serializer.** Rejected because no selected serializer preserved the
  full source contract byte-for-byte across the real Pigsty corpus.
- **Use regular expressions for YAML.** Rejected because quoting, comments, aliases, block scalars,
  and nested collections make text-only semantic decisions unsafe.
- **Edit only a normalized generated copy.** Rejected because the active Inventory is operator-owned
  and would still diverge from the generated representation.
- **Allow every node type to be replaced.** Rejected because anchors, aliases, tags, and block
  scalars need stricter handling than ordinary mappings and scalars.

## Contract {#contract}

- duplicate keys and multi-document YAML are rejected;
- selectors address one unambiguous declaration fragment;
- semantic decoding and source-range discovery are separate concerns;
- an edit starts from the exact source revision and fails if the file changes concurrently;
- the edited fragment is normalized only as required for its insertion context;
- the complete candidate is reparsed and validated before commit;
- writes use a same-directory temporary file, sync, rename, and directory sync;
- symlink and unsafe path changes are rejected;
- a successful edit removes group and other permissions, preserving the owner's existing read and write permissions;
- diagnostics, diffs, plans, and structured results omit declaration values unless a command is
  explicitly a raw text surface.

The 2026-10-16 acceptance refinement preserves role-name strings and structured role grants in
`pg_users[].roles`. Editing, serialization, and CMDB export must preserve YAML 1.1 string semantics,
including `"1:30"`; `"010"` is a regression control, not a claim of an earlier defect.
The 2026-10-17 refinement also requires dates and timestamps supplied as strings to remain strings
when loaded by PyYAML. Existing timestamp values retain their source bytes as opaque scalars;
this does not add date arithmetic or normalize the document. Acceptance compares emitted types
with an independent YAML loader, rather than relying only on PIG's own round trip.

[`sty deploy`](/sty/#sty-deploy) and [`do`](/do/) consume the configuration layer's already resolved
Inventory path. Flag and environment relative paths use the caller's cwd, quoted `~/` uses the
user's home, and defaults remain explicit at execution. Missing sources do not fall back.
Static inspection still never executes dynamic sources; Ansible retains its execution boundary.

The 2026-11-12 refinement also quotes `"="`, `"<<"`, and numeric-looking strings such as `"1e1"`.
CMDB export formats canonical JSON numbers for YAML 1.1: ordinary integers remain integers,
floating-point exponents include the required decimal point and sign, and signed or unsigned
64-bit integer values retain full precision. Export still validates semantic equivalence and refuses
values that cannot round-trip. This formatting applies to generated CMDB YAML, not untouched source text.

## Consequences {#impact}

The editor is more complex than ordinary YAML marshaling, and some syntactically valid fragments
are deliberately refused when source fidelity cannot be proven. The benefit is a reviewable diff:
unselected parts of the Inventory remain byte-for-byte stable, invalid YAML cannot be committed,
and a concurrent edit cannot be overwritten silently.

Inventory can contain custom credential fields, so field names alone cannot reliably identify secrets.
For this reason, `show` prints the selected declaration as raw text. Metadata and validation commands omit declaration values.

## Verification and evolution {#verification}

Fixed source commits and regression tests for the committed v1.10.0 refinements cover:
[resolved sources and role grants](https://github.com/pgsty/pig/commit/7be9b2b6cd31603de0e8a564aa5e9859b936b8e3),
[date-like string preservation](https://github.com/pgsty/pig/commit/a3ea1e50befbf412ae1267d57f1dbf1b26dd530d),
[independent YAML consumer acceptance](https://github.com/pgsty/pig/commit/eb3fc1e4ff39318972277fd96626ffa2c449f01d). These refinements shipped in [v1.10.0](https://github.com/pgsty/pig/releases/tag/v1.10.0).

The root Inventory contract and existing-CMDB boundary were established in
[`ba6e678`](https://github.com/pgsty/pig/commit/ba6e678), with the implementation landing in
[`ea43858`](https://github.com/pgsty/pig/commit/ea43858). Tests exercise real Pigsty configuration
corpora, byte-identical round trips, scoped replacement, protected YAML forms, concurrent-change
rejection, atomic-write failures, selectors, validation, and secret-free diagnostics.

Later refactors removed legacy options and separated validation stages without changing the
source-fidelity model.

The [2026-11-12 scalar implementation](https://github.com/pgsty/pig/commit/f569d0d4f75e39942baf393a224f21a617b29699)
has exact-integer and CMDB-dump regression tests. With `PIG_TEST_PYYAML` pointing to PyYAML 6.0.3,
the tests independently check the emitted scalar types and values, including the largest unsigned
64-bit integer. Local acceptance passed with this independent consumer.

## Current status {#status}

The static Inventory remains the primary declaration surface. Use the current
[`pig inventory` reference](/inventory/) for selectors, validation profiles, structured output, and
the explicitly experimental CMDB bridge.
