Skip to content

Commit 0459667

Browse files
authored
fix(contrib): isolate tracked raw tool payloads (#176)
* fix(contrib): isolate tracked raw tool payloads * refactor(contrib): keep only raw_input/raw_output deepcopy in tracked tool call init Per review, only the two deepcopy calls in _TrackedToolCall.__init__ are needed; drop the other deepcopy changes, the class docstring note, and tests that depended on them.
1 parent 8a9ee27 commit 0459667

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎src/acp/contrib/tool_calls.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import uuid
44
from collections.abc import Callable, Sequence
5+
from copy import deepcopy
56
from typing import Any, cast
67

78
from pydantic import BaseModel, ConfigDict
@@ -86,8 +87,8 @@ def __init__(
8687
self.status = status
8788
self.content = _copy_model_list(content)
8889
self.locations = _copy_model_list(locations)
89-
self.raw_input = raw_input
90-
self.raw_output = raw_output
90+
self.raw_input = deepcopy(raw_input)
91+
self.raw_output = deepcopy(raw_output)
9192
self._stream_buffer: str | None = None
9293

9394
def to_view(self) -> TrackedToolCallView:

‎tests/contrib/test_contrib_tool_calls.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import pytest
4+
35
from acp.contrib.tool_calls import ToolCallTracker
46
from acp.schema import ContentToolCallContent, TextContentBlock, ToolCallProgress
57

@@ -33,3 +35,41 @@ def test_tool_call_tracker_streaming_text_updates_content():
3335
assert isinstance(second_content, ContentToolCallContent)
3436
assert isinstance(second_content.content, TextContentBlock)
3537
assert second_content.content.text == "hello, world"
38+
39+
40+
@pytest.mark.parametrize("field", ["raw_input", "raw_output"])
41+
def test_tracker_start_snapshots_incoming_raw_payloads(field: str) -> None:
42+
tracker = ToolCallTracker()
43+
payload = {"nested": ["original"]}
44+
notification = tracker.start(
45+
"external",
46+
title="Run",
47+
raw_input=payload if field == "raw_input" else None,
48+
raw_output=payload if field == "raw_output" else None,
49+
)
50+
51+
payload["nested"].append("caller mutation")
52+
53+
assert getattr(tracker.view("external"), field) == {"nested": ["original"]}
54+
assert getattr(notification, field) == {"nested": ["original"]}
55+
56+
57+
@pytest.mark.parametrize("payload", [None, False, 0, "text", ["item"]])
58+
def test_tracker_preserves_json_raw_payload_values(payload: object) -> None:
59+
tracker = ToolCallTracker()
60+
tracker.start("external", title="Run", raw_input=payload, raw_output=payload)
61+
62+
assert tracker.view("external").raw_input == payload
63+
assert tracker.tool_call_model("external").raw_output == payload
64+
65+
66+
def test_tracker_progress_can_clear_raw_payloads() -> None:
67+
tracker = ToolCallTracker()
68+
tracker.start("external", title="Run", raw_input={"old": True}, raw_output={"old": True})
69+
70+
progress = tracker.progress("external", raw_input=None, raw_output=None)
71+
72+
assert progress.raw_input is None
73+
assert progress.raw_output is None
74+
assert tracker.view("external").raw_input is None
75+
assert tracker.view("external").raw_output is None

0 commit comments

Comments
 (0)