Fix shallow is_changed handling

This commit is contained in:
xmarre 2026-03-18 12:26:30 +01:00
parent fdcc38b9ea
commit e13da8104c
2 changed files with 38 additions and 8 deletions

View File

@ -68,20 +68,22 @@ _FAILED_SIGNATURE = object()
def _shallow_is_changed_signature(value):
"""Sanitize execution-time `is_changed` values with a small fail-closed budget."""
"""Reduce execution-time `is_changed` values without deep traversal."""
value_type = type(value)
if value_type in _PRIMITIVE_SIGNATURE_TYPES:
return value
canonical = to_hashable(value, max_nodes=64)
if type(canonical) is Unhashable:
return canonical
if value_type is list or value_type is tuple:
container_tag = "is_changed_list" if value_type is list else "is_changed_tuple"
return (container_tag, canonical[1])
try:
items = tuple(value)
except RuntimeError:
return Unhashable()
if all(type(item) in _PRIMITIVE_SIGNATURE_TYPES for item in items):
container_tag = "is_changed_list" if value_type is list else "is_changed_tuple"
return (container_tag, items)
return Unhashable()
return canonical
return Unhashable()
def _primitive_signature_sort_key(obj):

View File

@ -25,6 +25,34 @@ class _StubNode:
return {"required": {}}
def test_shallow_is_changed_signature_keeps_primitive_only_list_shallow():
assert caching._shallow_is_changed_signature([1, "two", None, True]) == (
"is_changed_list",
(1, "two", None, True),
)
def test_shallow_is_changed_signature_keeps_primitive_only_tuple_shallow():
assert caching._shallow_is_changed_signature((1, "two", None, True)) == (
"is_changed_tuple",
(1, "two", None, True),
)
def test_shallow_is_changed_signature_fails_closed_for_nested_container(monkeypatch):
monkeypatch.setattr(
caching,
"to_hashable",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
AssertionError("is_changed signature must not deep-canonicalize")
),
)
signature = caching._shallow_is_changed_signature([1, [2, 3]])
assert isinstance(signature, caching.Unhashable)
def test_get_immediate_node_signature_canonicalizes_non_link_inputs(monkeypatch):
live_value = [1, {"nested": [2, 3]}]
dynprompt = _StubDynPrompt(