Skip to content

Commit f9e9790

Browse files
moonbox3Copilot
andcommitted
Add regression tests proving shallow copy bug and deep copy isolation (microsoft#4500)
Add two additional tests to TestStateSnapshotDiff: - test_shallow_copy_would_miss_nested_mutations: reproduces the original bug by demonstrating that dict() (shallow copy) misses nested mutations - test_create_state_snapshot_isolates_nested_objects: verifies the production _create_state_snapshot helper creates a true deep copy These tests ensure a regression back to shallow copy would be caught. Co-authored-by: Copilot <[email protected]>
1 parent 284574a commit f9e9790

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

‎python/packages/azurefunctions/tests/test_app.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,62 @@ def test_unchanged_nested_state_produces_empty_diff(self) -> None:
15801580

15811581
assert updates == {}
15821582

1583+
def test_shallow_copy_would_miss_nested_mutations(self) -> None:
1584+
"""Regression test: a shallow copy (dict()) shares nested refs, hiding mutations.
1585+
1586+
This reproduces the original bug from #4500 where ``dict(deserialized_state)``
1587+
was used instead of ``copy.deepcopy()``. With a shallow copy the snapshot and
1588+
the live state share nested objects, so in-place mutations appear in both and
1589+
the diff produces an empty update set.
1590+
"""
1591+
from agent_framework._workflows._state import State
1592+
1593+
from agent_framework_azurefunctions._app import _compute_state_updates
1594+
1595+
deserialized_state: dict[str, Any] = {
1596+
"Local.config": {"code": "", "enabled": False},
1597+
}
1598+
1599+
# Shallow copy (the OLD, buggy behaviour)
1600+
shallow_snapshot = dict(deserialized_state)
1601+
1602+
shared_state = State()
1603+
shared_state.import_state(deserialized_state)
1604+
1605+
config = shared_state.get("Local.config")
1606+
config["code"] = "SOMECODEXXX"
1607+
config["enabled"] = True
1608+
1609+
shared_state.commit()
1610+
current_state = shared_state.export_state()
1611+
1612+
# With a shallow copy the mutation leaks into the snapshot → empty diff
1613+
updates_shallow = _compute_state_updates(shallow_snapshot, current_state)
1614+
assert updates_shallow == {}, "shallow copy should miss nested mutations (demonstrating the bug)"
1615+
1616+
def test_create_state_snapshot_isolates_nested_objects(self) -> None:
1617+
"""Verify _create_state_snapshot produces a deep copy that is mutation-proof.
1618+
1619+
This ensures the production snapshot helper is not equivalent to ``dict()``
1620+
and will correctly isolate nested objects so that later mutations are detected.
1621+
"""
1622+
from agent_framework_azurefunctions._app import _create_state_snapshot
1623+
1624+
original: dict[str, Any] = {
1625+
"nested_dict": {"a": 1},
1626+
"nested_list": [1, 2, 3],
1627+
}
1628+
1629+
snapshot = _create_state_snapshot(original)
1630+
1631+
# Mutate the originals in place
1632+
original["nested_dict"]["a"] = 999
1633+
original["nested_list"].append(4)
1634+
1635+
# Snapshot must be unaffected
1636+
assert snapshot["nested_dict"]["a"] == 1
1637+
assert snapshot["nested_list"] == [1, 2, 3]
1638+
15831639

15841640
if __name__ == "__main__":
15851641
pytest.main([__file__, "-v", "--tb=short"])

0 commit comments

Comments
 (0)