Skip to content

fix(config): preserve unknown top-level config sections across writes - #1942

Merged
tkircsi merged 2 commits into
agntcy:mainfrom
conorbronsdon:fix/config-roundtrip-rebased
Jul 31, 2026
Merged

tkircsi merged 2 commits into
agntcy:mainfrom
conorbronsdon:fix/config-roundtrip-rebased

Conversation

@conorbronsdon

Copy link
Copy Markdown
Member

Closes #1777.

SaveFile and its callers (SaveContext, SaveExtractor, SetCurrentContext, ClearExtractor) decode the config into the typed File struct and re-marshal it, so any top-level YAML key not modeled on File is dropped on write. An older dirctl silently discards a section a newer one wrote.

The issue notes why the obvious fix is wrong: an ,inline catch-all on File would absorb unknown keys and break the strict-rejection contract LoadFile and TestLoadFile rely on. So this separates the two concerns instead, taking the yaml.Node approach the issue suggests.

How it works

The tolerant load path (loadFile with allowUnknownFields=true) parses the raw bytes into a yaml.Node tree alongside the typed decode, and records every top-level key not represented on File as its original nodes. SaveFile re-appends them after encoding the known fields. Strict LoadFile is untouched and still rejects unknown keys — capture is gated on the tolerant flag and is unreachable from the strict path, since Decode has already errored.

Scope is top-level only, as the issue specifies. All four writers go through the tolerant path, so the preservation applies wherever it matters.

Two hazards this had to handle

An adversarial review pass caught both, and each would have produced a config no version of dirctl could load — strictly worse than the dropped section this fixes. They're the reason for the second commit.

Aliases. Recognized sections are re-encoded from the struct and emit no anchors. An unknown section containing an alias to an anchor defined in a recognized section — contexts: dev: &devctx plus future: {copied_from: *devctx} — was written out dangling, and every later load failed with yaml: unknown anchor 'devctx' referenced. Captured nodes now have aliases inlined and anchors stripped. That's value-preserving: an alias means exactly the node it references, and only the indirection is lost. A visiting set guards the self-referential case.

Key-set derivation. knownTopLevelKeys read yaml struct tags but skipped untagged fields, while the encoder falls back to the lowercased field name. A future untagged field would therefore be captured as unknown and emitted by the struct marshal — a duplicate top-level key, which is a load error. Latent today since all three fields are tagged, but it's precisely the trap the next contributor would hit. The derivation now uses the encoder's own fallback.

Verification

go test ./config/... passes. The alias fix has a regression test that reproduces the exact original failure when the inlining is removed, and the four preservation tests were mutation-checked — removing the re-append in marshalFile, or no-oping the capture, turns them red. golangci-lint run ./config/... is at the same baseline as upstream/main (10 goconst, plus 2 gci that are a local CRLF artifact): no new issues.

Known limits, stated rather than implied

  • Single document. A second YAML document after --- was dropped before this change and still is; both the decoder and the capture pass see only the first.
  • A leading file comment does not survive, since it attaches to the first known key, which is re-encoded. Comments attached to unknown sections do survive, including inline ones.
  • SaveFile is still lossy for a hand-built File. Only a File from the tolerant load carries the sections. Every in-repo writer does; the doc comment now says so, since it's exported client API.

Happy to squash the two commits if you'd prefer a single one — I kept them separate because the second is a distinct set of fixes with its own reasoning.

conorbronsdon and others added 2 commits July 30, 2026 16:04
… (lossless round-trip)

The dirctl config write path (SaveContext, SaveExtractor, SetCurrentContext,
ClearExtractor) loads the file with a tolerant decoder, mutates the typed
File struct, and re-marshals it. Any top-level YAML key not modeled on File
was silently dropped on write, making the round-trip lossy: an older dirctl
would discard sections written by a newer one, breaking forward compatibility.

Capture unrecognized top-level sections as raw yaml.Node values during the
tolerant load and re-append them in SaveFile, so a load->save cycle preserves
them verbatim (value, nesting, and any comments). Scope is top-level only, as
the issue specifies. The strict LoadFile path is unchanged and still rejects
unknown fields; capture runs only when unknown fields are tolerated.

Closes agntcy#1777

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Conor Bronsdon <[email protected]>
A review of the preservation commit found two ways it could write a
config that no version of dirctl can load. Both are worse than the
dropped section the change exists to prevent, so they are worth stating.

An unknown section holding a YAML alias was re-emitted verbatim while its
anchor was not. Recognized sections are re-encoded from the typed struct,
which emits no anchors, so `contexts: dev: &devctx` plus an unknown
`future: {copied_from: *devctx}` wrote out a dangling reference and every
later load failed with "unknown anchor 'devctx' referenced". Captured
nodes now have their aliases inlined and their anchors stripped, which is
value-preserving because an alias means exactly the node it points at.
A visiting set guards the self-referential case. Covered by a test that
reproduces the original breakage when the inlining is removed.

knownTopLevelKeys derived its exclusion set from yaml struct tags but
skipped any field without one, while the encoder falls back to the
lowercased field name. The two disagreeing means a key gets both captured
as unknown and written by the struct marshal, and a duplicate top-level
key is a load error. No current field is untagged, so this was latent,
but it is exactly the trap a future field would spring. The derivation
now uses the same fallback the encoder does, and the comment that claimed
collisions were impossible says what actually makes them impossible.

SaveFile's doc comment now states the precondition it always had: only a
File from the tolerant load path carries the sections, so saving a
hand-built one still drops them. It is exported client API, so leaving
that unsaid was a footgun.

Also wraps the three unwrapped errors, moves the reflection to
reflect.TypeFor and Type.Fields, and pulls a repeated test literal into a
constant, keeping the package at its existing lint baseline.

The round-trip is single-document. A second YAML document after a `---`
was dropped before this change and still is; both the decoder and the
capture pass see only the first.

Signed-off-by: Conor Bronsdon <[email protected]>
@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Jul 30, 2026
@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.69697% with 20 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
client/config/config.go 69.7% 10 Missing and 10 partials ⚠️

📢 Thoughts on this report? Let us know!

@conorbronsdon
conorbronsdon marked this pull request as ready for review July 31, 2026 01:00
@conorbronsdon
conorbronsdon requested a review from a team as a code owner July 31, 2026 01:00

@tkircsi tkircsi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for contributing!

@tkircsi
tkircsi merged commit c53e4ee into agntcy:main Jul 31, 2026
31 checks passed
markpmarton pushed a commit to markpmarton/dir that referenced this pull request Aug 3, 2026
…agntcy#1942)

* fix(config): preserve unknown top-level config sections across writes (lossless round-trip)

The dirctl config write path (SaveContext, SaveExtractor, SetCurrentContext,
ClearExtractor) loads the file with a tolerant decoder, mutates the typed
File struct, and re-marshals it. Any top-level YAML key not modeled on File
was silently dropped on write, making the round-trip lossy: an older dirctl
would discard sections written by a newer one, breaking forward compatibility.

Capture unrecognized top-level sections as raw yaml.Node values during the
tolerant load and re-append them in SaveFile, so a load->save cycle preserves
them verbatim (value, nesting, and any comments). Scope is top-level only, as
the issue specifies. The strict LoadFile path is unchanged and still rejects
unknown fields; capture runs only when unknown fields are tolerated.

Closes agntcy#1777

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Conor Bronsdon <[email protected]>

* fix(config): inline aliases and align the known-key set with the encoder

A review of the preservation commit found two ways it could write a
config that no version of dirctl can load. Both are worse than the
dropped section the change exists to prevent, so they are worth stating.

An unknown section holding a YAML alias was re-emitted verbatim while its
anchor was not. Recognized sections are re-encoded from the typed struct,
which emits no anchors, so `contexts: dev: &devctx` plus an unknown
`future: {copied_from: *devctx}` wrote out a dangling reference and every
later load failed with "unknown anchor 'devctx' referenced". Captured
nodes now have their aliases inlined and their anchors stripped, which is
value-preserving because an alias means exactly the node it points at.
A visiting set guards the self-referential case. Covered by a test that
reproduces the original breakage when the inlining is removed.

knownTopLevelKeys derived its exclusion set from yaml struct tags but
skipped any field without one, while the encoder falls back to the
lowercased field name. The two disagreeing means a key gets both captured
as unknown and written by the struct marshal, and a duplicate top-level
key is a load error. No current field is untagged, so this was latent,
but it is exactly the trap a future field would spring. The derivation
now uses the same fallback the encoder does, and the comment that claimed
collisions were impossible says what actually makes them impossible.

SaveFile's doc comment now states the precondition it always had: only a
File from the tolerant load path carries the sections, so saving a
hand-built one still drops them. It is exported client API, so leaving
that unsaid was a footgun.

Also wraps the three unwrapped errors, moves the reflection to
reflect.TypeFor and Type.Fields, and pulls a repeated test literal into a
constant, keeping the package at its existing lint baseline.

The round-trip is single-document. A second YAML document after a `---`
was dropped before this change and still is; both the decoder and the
capture pass see only the first.

Signed-off-by: Conor Bronsdon <[email protected]>

---------

Signed-off-by: Conor Bronsdon <[email protected]>
Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Denotes a PR that changes 200-999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(config): preserve unknown top-level config sections across writes (lossless round-trip)

2 participants