Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

feat(capability): merge_capability_payload deep-merge with array atomicity

xf gonge16eac90afec
diff --git a/cubepi/providers/capability.py b/cubepi/providers/capability.py
index c278b3d..bc3f5be 100644
--- a/cubepi/providers/capability.py
+++ b/cubepi/providers/capability.py
@@ -62,3 +62,28 @@ class CapabilityDescriptor(BaseModel):
supports_tools: bool = True
supports_images: bool = False
supports_streaming: bool = True
+
+
+def merge_capability_payload(kwargs: dict[str, Any], patch: dict[str, Any]) -> None:
+ """Deep-merge ``patch`` into ``kwargs`` in place.
+
+ Rules (spec §3.3):
+ 1. Recurse into nested dicts.
+ 2. Arrays are atomic — capability replaces caller's array on collision.
+ 3. On scalar / array key collision, capability (``patch``) wins.
+ 4. Patch is never mutated; nested dicts are copied on write.
+ """
+
+ for key, patch_value in patch.items():
+ if (
+ key in kwargs
+ and isinstance(kwargs[key], dict)
+ and isinstance(patch_value, dict)
+ ):
+ merge_capability_payload(kwargs[key], patch_value)
+ elif isinstance(patch_value, dict):
+ # Copy nested dict so subsequent kwargs mutation doesn't bleed into patch.
+ kwargs[key] = {}
+ merge_capability_payload(kwargs[key], patch_value)
+ else:
+ kwargs[key] = patch_value
diff --git a/tests/test_capability.py b/tests/test_capability.py
index aa33d0f..72305a6 100644
--- a/tests/test_capability.py
+++ b/tests/test_capability.py
@@ -61,3 +61,46 @@ def test_temperature_default_must_be_within_range():
with pytest.raises(ValidationError):
TemperatureSpec(min=0.0, max=1.0, default=2.0)
+
+
+from cubepi.providers.capability import merge_capability_payload
+
+
+def test_merge_empty_patch_is_noop():
+ kwargs = {"a": 1, "extra_body": {"b": 2}}
+ merge_capability_payload(kwargs, {})
+ assert kwargs == {"a": 1, "extra_body": {"b": 2}}
+
+
+def test_merge_adds_new_top_level_keys():
+ kwargs = {"a": 1}
+ merge_capability_payload(kwargs, {"reasoning_effort": "low"})
+ assert kwargs == {"a": 1, "reasoning_effort": "low"}
+
+
+def test_merge_recurses_into_nested_dicts():
+ kwargs: dict = {"extra_body": {"existing": True}}
+ merge_capability_payload(kwargs, {"extra_body": {"enable_thinking": False}})
+ assert kwargs == {"extra_body": {"existing": True, "enable_thinking": False}}
+
+
+def test_merge_capability_wins_on_leaf_collision():
+ kwargs = {"extra_body": {"enable_thinking": True}}
+ merge_capability_payload(kwargs, {"extra_body": {"enable_thinking": False}})
+ assert kwargs == {"extra_body": {"enable_thinking": False}}
+
+
+def test_merge_arrays_are_atomic_capability_wins():
+ """Arrays at colliding keys are replaced, not unioned."""
+ kwargs = {"stop": ["\n", "."]}
+ merge_capability_payload(kwargs, {"stop": ["END"]})
+ assert kwargs == {"stop": ["END"]}
+
+
+def test_merge_does_not_mutate_patch():
+ """The patch dict the caller passes in must be left untouched."""
+ patch = {"extra_body": {"enable_thinking": False}}
+ kwargs: dict = {}
+ merge_capability_payload(kwargs, patch)
+ kwargs["extra_body"]["enable_thinking"] = True
+ assert patch == {"extra_body": {"enable_thinking": False}}