Skip to content

fix(yaml): give earlier map precedence in a << merge sequence - #4995

Closed
PratikDhanave wants to merge 1 commit into
Graphify-Labs:v8from
PratikDhanave:fix/yaml-merge-sequence-precedence
Closed

PratikDhanave wants to merge 1 commit into
Graphify-Labs:v8from
PratikDhanave:fix/yaml-merge-sequence-precedence

Conversation

@PratikDhanave

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a YAML merge-key precedence bug in the structural YAML extractor
(graphify/extractors/yaml_config.py, _mapping_pairs).

For a sequence merge <<: [*a, *b], the YAML 1.1 merge spec gives the earlier
map precedence over the later one, and PyYAML's safe_load / flatten_mapping
honor this (flatten reverses the submerge so the first map's keys win). The
extractor instead fed the merge sources forward into a last-write-wins dict, so a
key defined in both anchors ended up resolving to the later map's value and
line — the opposite of the spec and of safe_load.

Concretely, given:

x-a: &a
  common:
    from_a: 1
x-b: &b
  common:
    from_b: 2
services:
  web:
    <<: [*a, *b]

yaml.safe_load(...)["services"]["web"]["common"] is *a's mapping (from_a,
whose key is at L2), but the extractor was emitting the web.common node at *b's
line (L5).

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant protected: the extractor's "keys as written" view
(_mapping_pairs over the composed node tree) must agree with the value PyYAML
actually constructs via safe_load. The fix feeds the merge sources in reverse,
mirroring flatten_mapping, so the earliest map keeps the final value/line.

Fail-pre / pass-post evidence (new test
tests/test_yaml_extraction.py::test_merge_key_sequence_earlier_map_wins):

Before the fix (test fails):

>       assert web_common["source_location"] == "L2"
E       AssertionError: assert 'L5' == 'L2'
E         - L2
E         + L5
tests/test_yaml_extraction.py:220: AssertionError
1 failed in 0.12s

After the fix (full file passes, incl. the existing merge test):

..................                                                       [100%]
18 passed in 0.23s
  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

# fail-pre (pre-fix code)
python -m pytest tests/test_yaml_extraction.py::test_merge_key_sequence_earlier_map_wins -q
#   -> AssertionError: assert 'L5' == 'L2'  (1 failed)

# pass-post (after fix)
python -m pytest tests/test_yaml_extraction.py -q
#   -> 18 passed

# lint
uv run ruff check --config pyproject.toml graphify/extractors/yaml_config.py tests/test_yaml_extraction.py
#   -> All checks passed!

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

A `<<: [*a, *b]` sequence merge was resolving keys to the LAST map in the
sequence, but the YAML merge spec (and PyYAML's safe_load / flatten_mapping)
give the EARLIER map precedence. _mapping_pairs fed the sources forward into a
last-write-wins dict, so for a key defined in both anchors the later anchor's
value and line won instead of the earlier one's.

Feed the merge sources in reverse, mirroring PyYAML's flatten_mapping (which
reverses the submerge), so the earliest map keeps the final value/line.

Regression test asserts services.web.common resolves to the first anchor's
mapping (L2), matching yaml.safe_load, not the second anchor's (L5).
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Thanks for the pull request, @PratikDhanave. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).

Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 vacuous).

Not verified on this run: \_mapping\_pairs (vacuous: never exercised).


Graphify review — findings

Fixes YAML merge-key handling so that in a <<: [*a, *b] sequence the earlier map wins over the later one, as the YAML merge spec and PyYAML's safe_load do. _mapping_pairs now feeds the merge sources in reverse, so the last-write-wins dict ends up with the first map's value and source line for any duplicate key. Adds a regression test where both anchors define common and checks that services.web.common resolves to *a's key at L2, not *b's at L5.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 46 functions depend on the 46 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract_yaml() — 18 callers, 9 callees

Verification — 46 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 46 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

1 of 518 test file(s) selected (0%) via static blast radius.

  • tests/test_yaml_extraction.py — impact, changed-test

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify \_mapping\_pairs.

The verifier did not have enough to check \_mapping\_pairs, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 16 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

· 1 more finding(s) on lines outside this diff (see the check run).

safishamsi added a commit that referenced this pull request Nov 5, 2026
…ractor fixes

#5003 (C# preprocessor blanking), #5005/#5001 (Solidity library + emit/revert/
require linking) and #4995 (YAML << merge precedence) each change per-file
extractor output; fold them into one schema-38 entry so warm caches re-run.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.103 (published to PyPI), cherry-picked with your authorship preserved. Folded into the combined 38 schema bump (per-file YAML source-line output changes). Thanks @PratikDhanave.

@safishamsi safishamsi closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants