load_yaml: Deepcopy cached link expansions

The memoisation cache in _expand_links_recursive() stores the expanded
value of a link node.  A link node that is first reached via another
link, and later visited by the normal tree walk, is expanded only
once; the cached result object is then returned as-is for the second
location.  Both places in the output tree end up holding the same
dict, so mutating one of them silently mutates the other.

Deepcopy the value on every cache read so that each place in the
output tree holds an independent object.  Add a regression test that
mutates one cached location and checks the others stay untouched.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-08-16 12:21:47 +02:00
commit 568c92b009
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -621,6 +621,22 @@ class TestEdgeCases:
assert data['a'] == 'val'
assert data['b'] == 'val'
def test_cached_link_expansion_not_aliased(self, yaml_file: Callable) -> None:
"""Link results shared via the memoisation cache are independent."""
data = load_yaml(
yaml_file(
'target:\n'
' x: 1\n'
'link_b:\n'
' jw.link: target\n'
'link_a:\n'
' jw.link: link_b\n'
)
)
data['link_a']['x'] = 99
assert data['link_b']['x'] == 1
assert data['target']['x'] == 1
def test_named_link_to_list(self, yaml_file: Callable) -> None:
data = load_yaml(
yaml_file('target:\n - x\n - y\nlink:\n jw.link: items|target\n')