From a18e2693f4e079dbba24bc6842c5f1d7b9c3bc1a Mon Sep 17 00:00:00 2001 From: lch <150694823+rootrisk@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:14:06 +0800 Subject: [PATCH] fix(contrib): preserve tool names in session snapshots --- docs/contrib.md | 1 + src/acp/contrib/session_state.py | 6 ++++++ tests/contrib/test_contrib_session_state.py | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/contrib.md b/docs/contrib.md index db81aaf9..b0687888 100644 --- a/docs/contrib.md +++ b/docs/contrib.md @@ -14,6 +14,7 @@ Feed raw `SessionNotification` events into one accumulator per UI controller and - `apply(notification)` merges message chunks, plan updates, mode/config changes, and `tool_call` / `tool_call_update` payloads (including out-of-order updates). - `snapshot()` returns an immutable `SessionSnapshot` Pydantic model with copied tool input/output payloads. +- Each tool call snapshot includes the optional programmatic `name`. An update that omits the name or sets it to `null` leaves the previous name unchanged. - `subscribe(callback)` pushes updated snapshots to subscribers whenever state changes. - `auto_reset_on_session_change` resets accumulated state automatically when a new `session_id` arrives. diff --git a/src/acp/contrib/session_state.py b/src/acp/contrib/session_state.py index d3ffad30..0a679979 100644 --- a/src/acp/contrib/session_state.py +++ b/src/acp/contrib/session_state.py @@ -50,6 +50,7 @@ class _MutableToolCallState: def __init__(self, tool_call_id: str) -> None: self.tool_call_id = tool_call_id self.title: str | None = None + self.name: str | None = None self.kind: ToolKind | None = None self.status: ToolCallStatus | None = None self.content: list[Any] | None = None @@ -59,6 +60,7 @@ def __init__(self, tool_call_id: str) -> None: def apply_start(self, update: ToolCallStart) -> None: self.title = update.title + self.name = update.name self.kind = update.kind self.status = update.status self.content = _copy_model_list(update.content) @@ -69,6 +71,8 @@ def apply_start(self, update: ToolCallStart) -> None: def apply_progress(self, update: ToolCallProgress) -> None: if update.title is not None: self.title = update.title + if update.name is not None: + self.name = update.name if update.kind is not None: self.kind = update.kind if update.status is not None: @@ -86,6 +90,7 @@ def snapshot(self) -> ToolCallView: return ToolCallView( tool_call_id=self.tool_call_id, title=self.title, + name=self.name, kind=self.kind, status=self.status, content=tuple(item.model_copy(deep=True) for item in self.content) if self.content else None, @@ -102,6 +107,7 @@ class ToolCallView(BaseModel): tool_call_id: str title: str | None + name: str | None = None kind: ToolKind | None status: ToolCallStatus | None content: tuple[Any, ...] | None diff --git a/tests/contrib/test_contrib_session_state.py b/tests/contrib/test_contrib_session_state.py index 1f30e616..dc406c00 100644 --- a/tests/contrib/test_contrib_session_state.py +++ b/tests/contrib/test_contrib_session_state.py @@ -88,9 +88,11 @@ def test_session_accumulator_merges_tool_calls(): session_update="tool_call", tool_call_id="call-1", title="Read file", + name="read_file", status="in_progress", ) - acc.apply(notification("s", start)) + first_snapshot = acc.apply(notification("s", start)) + assert first_snapshot.tool_calls["call-1"].name == "read_file" progress = ToolCallProgress( session_update="tool_call_update", tool_call_id="call-1", @@ -106,9 +108,22 @@ def test_session_accumulator_merges_tool_calls(): tool = snapshot.tool_calls["call-1"] assert tool.status == "completed" assert tool.title == "Read file" + assert tool.name == "read_file" assert tool.content and tool.content[0].content.text == "Done" +def test_session_accumulator_updates_tool_name_from_progress(): + acc = SessionAccumulator() + first = acc.apply(notification("s", ToolCallProgress(tool_call_id="call-1", name="read_file"))) + assert first.tool_calls["call-1"].name == "read_file" + + updated = acc.apply(notification("s", ToolCallProgress(tool_call_id="call-1", name="read_resource"))) + assert updated.tool_calls["call-1"].name == "read_resource" + + unchanged = acc.apply(notification("s", ToolCallProgress(tool_call_id="call-1", name=None))) + assert unchanged.tool_calls["call-1"].name == "read_resource" + + def test_session_accumulator_records_plan_and_mode(): acc = SessionAccumulator() acc.apply(