Repository navigation
fix(config): preserve unknown top-level config sections across writes - #1942
Merged
Merged
Conversation
… (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]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
conorbronsdon
marked this pull request as ready for review
July 31, 2026 01:00
tkircsi
approved these changes
Jul 31, 2026
tkircsi
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Thank you for contributing!
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1777.
SaveFileand its callers (SaveContext,SaveExtractor,SetCurrentContext,ClearExtractor) decode the config into the typedFilestruct and re-marshal it, so any top-level YAML key not modeled onFileis dropped on write. An olderdirctlsilently discards a section a newer one wrote.The issue notes why the obvious fix is wrong: an
,inlinecatch-all onFilewould absorb unknown keys and break the strict-rejection contractLoadFileandTestLoadFilerely on. So this separates the two concerns instead, taking theyaml.Nodeapproach the issue suggests.How it works
The tolerant load path (
loadFilewithallowUnknownFields=true) parses the raw bytes into ayaml.Nodetree alongside the typed decode, and records every top-level key not represented onFileas its original nodes.SaveFilere-appends them after encoding the known fields. StrictLoadFileis untouched and still rejects unknown keys — capture is gated on the tolerant flag and is unreachable from the strict path, sinceDecodehas 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: &devctxplusfuture: {copied_from: *devctx}— was written out dangling, and every later load failed withyaml: 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. Avisitingset guards the self-referential case.Key-set derivation.
knownTopLevelKeysread 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 inmarshalFile, or no-oping the capture, turns them red.golangci-lint run ./config/...is at the same baseline asupstream/main(10 goconst, plus 2 gci that are a local CRLF artifact): no new issues.Known limits, stated rather than implied
---was dropped before this change and still is; both the decoder and the capture pass see only the first.SaveFileis still lossy for a hand-builtFile. Only aFilefrom 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.