Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ All notable changes to `xgen-agent-runtime` are recorded here. The format
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
this project adheres to [Semantic Versioning](https://semver.org/).

## [4.107.1] - 2026-11-09

### Fixed: 같은 `tool_use` id 가 두 번 실려 요청이 거절되던 것

Agent-Geny 에서 `claude-sonnet-5-5` 를 쓰면 턴이 400 `messages.3.content.2: tool_use ids must be unique` 로 끝났다.
응답에 같은 id 의 `tool_use` 블록이 둘 실리면 그 응답을 담은 다음 요청이 거절되고, 기록에 남은 한 쌍 때문에 그
세션의 이후 턴도 모두 같은 400 을 맞는다. 두 곳에서 막는다.

- **응답을 읽을 때** (`AnthropicClient`·`BedrockClient`·`AnthropicProvider` 의 `_parse_response`): 같은 id 는 한 번만
받는다. 처음 것의 입력이 비었고 나중 것에 입력이 있으면 나중 것의 입력을 쓴다. 도구도 한 번만 돈다. 이런 응답이
오면 모델 · stop_reason · 블록 종류를 경고 로그로 남긴다. SDK 의 스트림 조립(`accumulate_event`)은
`content_block_start` 마다 블록을 덧붙이므로(인덱스를 보지 않는다) 같은 블록의 시작이 두 번 오면 이 모양이 된다.
- **요청을 보낼 때** (`normalize_messages_for_request`): 기록 전체에서 같은 id 의 `tool_use` 는 처음 것만, 같은 id 의
`tool_result` 도 처음 것만 싣는다. 비게 된 메시지는 뺀다. 저장된 기록은 건드리지 않는다. 이미 중복이 저장된
세션도 다음 턴부터 이어진다.

## [4.107.0] - 2026-11-03

### Added: OpenRouter 업체 지정 (`OpenRouterClient(provider_routing=...)`)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "xgen-agent-runtime"
version = "4.107.0"
version = "4.107.1"
description = "Harness-engineered agent pipeline library with 21-stage dual-abstraction architecture, built on the Anthropic API"
readme = "README.md"
license = "Apache-2.0"
Expand Down
46 changes: 36 additions & 10 deletions src/xgen_agent_runtime/core/message_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
from __future__ import annotations

import json
import logging
import re
from typing import Any, Callable, Dict, List, Sequence

logger = logging.getLogger(__name__)


#: The result put where a tool call has none. It says only what is known: no result was recorded.
#: It used to say the tool "did not finish", which is false when a stop landed after the tool ran but
Expand Down Expand Up @@ -93,6 +96,10 @@ def normalize_messages_for_request(

* every assistant ``tool_use`` has a later ``tool_result``;
* every ``tool_result`` refers to an earlier assistant ``tool_use``;
* a ``tool_use`` id appears once in the whole request, and so does its
``tool_result`` — the Anthropic API rejects the request otherwise
(400 "tool_use ids must be unique"), and one repeated block in a
persisted history used to fail every later turn of that session;
* repairs are request-only and never mutate the persisted history.

Missing outputs are inserted immediately after their assistant call. An
Expand All @@ -102,6 +109,8 @@ def normalize_messages_for_request(
"""
normalized: List[Dict[str, Any]] = []
seen_call_ids: set[str] = set()
answered_once: set[str] = set()
repeated_calls = 0

# Remove outputs that cannot be paired with an earlier call. Copy only a
# message whose content changes; clean histories retain their nested
Expand All @@ -113,37 +122,54 @@ def normalize_messages_for_request(
normalized.append(message)
continue

current_call_ids = _tool_use_ids(message) if message.get("role") == "assistant" else []

content = message.get("content")
if not isinstance(content, list):
normalized.append(message)
seen_call_ids.update(current_call_ids)
continue

is_assistant = message.get("role") == "assistant"
current_call_ids: set[str] = set()
filtered: List[Any] = []
removed_orphan = False
removed = False
for block in content:
is_result = isinstance(block, dict) and block.get("type") == "tool_result"
if is_result:
kind = block.get("type") if isinstance(block, dict) else None
if kind == "tool_use" and is_assistant:
call_id = block.get("id")
if isinstance(call_id, str) and call_id:
# A repeated call is the same call: keep the first, drop the copy.
if call_id in seen_call_ids or call_id in current_call_ids:
removed = True
repeated_calls += 1
continue
current_call_ids.add(call_id)
elif kind == "tool_result":
result_id = block.get("tool_use_id")
if (
not isinstance(result_id, str)
or not result_id
or result_id not in seen_call_ids
or result_id in answered_once
):
removed_orphan = True
removed = True
continue
answered_once.add(result_id)
filtered.append(block)

if not removed_orphan:
if not removed:
normalized.append(message)
elif filtered:
normalized.append({**message, "content": filtered})
# A message made entirely of orphan results has no model-visible
# content left and is intentionally omitted.
# A message made entirely of orphan or repeated blocks has no
# model-visible content left and is intentionally omitted.
seen_call_ids.update(current_call_ids)

if repeated_calls:
logger.warning(
"message_repair: dropped %d repeated tool_use block(s) from the request "
"(the same id appeared more than once in the history)",
repeated_calls,
)

answered_ids: set[str] = set()
for message in normalized:
if isinstance(message, dict):
Expand Down
21 changes: 21 additions & 0 deletions src/xgen_agent_runtime/llm_client/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,29 @@ def _build_kwargs(self, request: APIRequest) -> Dict[str, Any]:

def _parse_response(self, raw: Any) -> APIResponse:
content_blocks: List[ContentBlock] = []
# 같은 tool_use id 는 한 번만 받는다. 두 번 실리면 도구가 두 번 돌고, 그 응답을 담은 다음
# 요청이 400 "tool_use ids must be unique" 로 거절된다(2026-11-09 claude-sonnet-5-5).
tool_use_at: Dict[str, int] = {}

for block in raw.content:
if block.type == "tool_use" and block.id in tool_use_at:
kept = content_blocks[tool_use_at[block.id]]
# 비어 있는 쪽 대신 입력이 있는 쪽을 남긴다(스트림 조립이 블록을 한 번 더 붙이면
# 덧붙은 쪽은 입력이 비어 있다).
if not kept.tool_input and block.input:
kept.tool_input = block.input
kept.raw = {**(kept.raw or {}), "input": block.input}
logger.warning(
"anthropic: response repeated tool_use id %s (model=%s, stop_reason=%s, blocks=%s)"
" — kept one",
block.id,
getattr(raw, "model", ""),
getattr(raw, "stop_reason", ""),
[getattr(b, "type", "") for b in raw.content],
)
continue
if block.type == "tool_use":
tool_use_at[block.id] = len(content_blocks)
if block.type == "text":
content_blocks.append(
ContentBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import json
import logging
import time
from typing import Any, AsyncIterator, Dict, List, Optional

Expand All @@ -12,6 +13,8 @@
from xgen_agent_runtime.stages.s06_api.types import APIRequest, APIResponse, ContentBlock
from xgen_agent_runtime.stages.s06_api._translate import canonical_messages_to_anthropic

logger = logging.getLogger(__name__)


class AnthropicProvider(APIProvider):
"""Real Anthropic API provider using the official SDK."""
Expand Down Expand Up @@ -135,8 +138,29 @@ def _build_kwargs(self, request: APIRequest) -> Dict[str, Any]:

def _parse_response(self, raw: Any) -> APIResponse:
content_blocks: List[ContentBlock] = []
# 같은 tool_use id 는 한 번만 받는다. 두 번 실리면 도구가 두 번 돌고, 그 응답을 담은 다음
# 요청이 400 "tool_use ids must be unique" 로 거절된다(2026-11-09 claude-sonnet-5-5).
tool_use_at: Dict[str, int] = {}

for block in raw.content:
if block.type == "tool_use" and block.id in tool_use_at:
kept = content_blocks[tool_use_at[block.id]]
# 비어 있는 쪽 대신 입력이 있는 쪽을 남긴다(스트림 조립이 블록을 한 번 더 붙이면
# 덧붙은 쪽은 입력이 비어 있다).
if not kept.tool_input and block.input:
kept.tool_input = block.input
kept.raw = {**(kept.raw or {}), "input": block.input}
logger.warning(
"anthropic: response repeated tool_use id %s (model=%s, stop_reason=%s, blocks=%s)"
" — kept one",
block.id,
getattr(raw, "model", ""),
getattr(raw, "stop_reason", ""),
[getattr(b, "type", "") for b in raw.content],
)
continue
if block.type == "tool_use":
tool_use_at[block.id] = len(content_blocks)
if block.type == "text":
content_blocks.append(
ContentBlock(
Expand Down
109 changes: 109 additions & 0 deletions tests/llm_client/unit/test_anthropic_unique_tool_use.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""같은 tool_use id 는 응답에서 한 번만 받는다.

두 번 실리면 도구가 두 번 돌고, 그 응답을 담은 다음 요청이
400 ``messages.N.content.M: tool_use ids must be unique`` 로 거절된다
(2026-11-09 Agent-Geny 에서 claude-sonnet-5-5 를 쓰면 났다).
"""

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "src"))

from anthropic.lib.streaming._messages import accumulate_event
from anthropic.types import Message, RawMessageStreamEvent, Usage
from anthropic._models import construct_type

from xgen_agent_runtime.llm_client.anthropic import AnthropicClient
from xgen_agent_runtime.stages.s06_api.artifact.default.providers import AnthropicProvider
from xgen_agent_runtime.stages.s06_api.artifact.default.tool_loop import assistant_content_blocks


def _message(content, model="claude-sonnet-5-5"):
return Message(
id="msg_1",
type="message",
role="assistant",
content=content,
model=model,
stop_reason="tool_use",
usage=Usage(input_tokens=10, output_tokens=4),
)


def _tool_ids(blocks):
return [b["id"] for b in blocks if b.get("type") == "tool_use"]


def test_a_repeated_tool_use_is_received_once():
raw = _message(
[
{"type": "thinking", "thinking": "plan", "signature": "sig"},
{"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {"command": "ls"}},
{"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {"command": "ls"}},
{"type": "tool_use", "id": "toolu_2", "name": "Read", "input": {"path": "a"}},
]
)
for parser in (AnthropicClient(api_key="k"), AnthropicProvider(api_key="k")):
response = parser._parse_response(raw)
blocks = assistant_content_blocks(response)
assert _tool_ids(blocks) == ["toolu_1", "toolu_2"]
assert [c.tool_use_id for c in response.content if c.type == "tool_use"] == ["toolu_1", "toolu_2"]
assert blocks[0]["signature"] == "sig", "thinking 은 그대로"


def test_the_copy_with_input_wins_over_an_empty_one():
raw = _message(
[
{"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {}},
{"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {"command": "ls"}},
]
)
response = AnthropicClient(api_key="k")._parse_response(raw)
(call,) = [c for c in response.content if c.type == "tool_use"]
assert call.tool_input == {"command": "ls"}
assert assistant_content_blocks(response)[0]["input"] == {"command": "ls"}


def test_distinct_tool_uses_are_untouched():
raw = _message(
[
{"type": "text", "text": "two at once"},
{"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {"command": "ls"}},
{"type": "tool_use", "id": "toolu_2", "name": "Bash", "input": {"command": "pwd"}},
]
)
blocks = assistant_content_blocks(AnthropicClient(api_key="k")._parse_response(raw))
assert [b["type"] for b in blocks] == ["text", "tool_use", "tool_use"]
assert _tool_ids(blocks) == ["toolu_1", "toolu_2"]


def _event(value):
return construct_type(type_=RawMessageStreamEvent, value=value)


def test_a_block_start_sent_twice_in_a_stream_still_gives_one_call():
"""SDK 의 스트림 조립은 content_block_start 마다 블록을 덧붙인다(인덱스를 보지 않는다).

같은 블록의 시작이 두 번 오면 같은 id 의 tool_use 가 둘이 되고, 입력은 처음 것에만 채워진다.
"""
start = {"type": "content_block_start", "index": 0,
"content_block": {"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {}}}
events = [
{"type": "message_start", "message": {
"id": "msg_1", "type": "message", "role": "assistant", "content": [], "model": "claude-sonnet-5-5",
"stop_reason": None, "stop_sequence": None, "usage": {"input_tokens": 1, "output_tokens": 0}}},
start,
{"type": "content_block_delta", "index": 0, "delta": {"type": "input_json_delta", "partial_json": '{"command": "ls"}'}},
start,
{"type": "content_block_stop", "index": 0},
{"type": "message_delta", "delta": {"stop_reason": "tool_use", "stop_sequence": None},
"usage": {"output_tokens": 5}},
]
snapshot = None
for value in events:
snapshot = accumulate_event(event=_event(value), current_snapshot=snapshot)
assert [b.id for b in snapshot.content] == ["toolu_1", "toolu_1"], "SDK 는 둘로 만든다"

blocks = assistant_content_blocks(AnthropicClient(api_key="k")._parse_response(snapshot))
assert blocks == [{"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {"command": "ls"}}]
Loading
Loading