Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ All notable changes to SkillEvaluator are documented in this file.

### Changed

- Claude Code log recovery pairs batched and delayed tool results without repeatedly
Comment thread
Afloat16 marked this conversation as resolved.
Resolved
scanning earlier tool calls, reducing reconstruction time for long trajectories.
- The `skillevaluator` agent skill now asks the user before installing the
CLI, an extra or a missing scanner, using a provider key, spending on a Tier 3
run or using `--env-mode local` (trusted skills only; it does not reuse the
Expand Down
32 changes: 17 additions & 15 deletions src/skillevaluator/tier3/eval_core/log_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,24 @@ def _stringify_tool_result_content(content: Any) -> str:


def _attach_tool_result(
steps: list[dict[str, Any]], tool_use_id: str, content_text: str, *, is_error: bool = False
steps_by_call_id: dict[str, dict[str, Any]], tool_use_id: str, content_text: str, *, is_error: bool = False
) -> None:
for step in reversed(steps):
if step.get("source") != "agent":
continue
for tc in step.get("tool_calls") or []:
if tc.get("tool_call_id") == tool_use_id:
step.setdefault("observation", {"results": []})
step["observation"].setdefault("results", [])
result: dict[str, Any] = {"source_call_id": tool_use_id, "content": content_text}
if is_error:
# Match Harbor's native Claude Code ATIF result metadata.
result["extra"] = {"tool_result_is_error": True}
step["observation"]["results"].append(result)
return
step = steps_by_call_id.get(tool_use_id)
if step is None:
return
step.setdefault("observation", {"results": []})
step["observation"].setdefault("results", [])
result: dict[str, Any] = {"source_call_id": tool_use_id, "content": content_text}
if is_error:
# Match Harbor's native Claude Code ATIF result metadata.
result["extra"] = {"tool_result_is_error": True}
step["observation"]["results"].append(result)


def synthetic_trajectory_from_claude_stream_jsonl(text: str) -> dict[str, Any] | None:
"""Parse Claude Code ``--output-format stream-json`` lines (tee'd to claude-code.txt)."""
steps: list[dict[str, Any]] = []
steps_by_call_id: dict[str, dict[str, Any]] = {}
for raw_line in text.splitlines():
line = raw_line.strip()
if not line:
Expand Down Expand Up @@ -145,6 +143,10 @@ def synthetic_trajectory_from_claude_stream_jsonl(text: str) -> dict[str, Any] |
"observation": {"results": []},
}
)
for call in tool_calls:
if call["tool_call_id"]:
# Reused IDs belong to the latest preceding step, as in the reverse scan.
steps_by_call_id[call["tool_call_id"]] = steps[-1]

elif et == "user":
msg = evt.get("message")
Expand All @@ -159,7 +161,7 @@ def synthetic_trajectory_from_claude_stream_jsonl(text: str) -> dict[str, Any] |
tid = str(block.get("tool_use_id") or "")
res_text = _stringify_tool_result_content(block.get("content"))
if tid:
_attach_tool_result(steps, tid, res_text, is_error=block.get("is_error") is True)
_attach_tool_result(steps_by_call_id, tid, res_text, is_error=block.get("is_error") is True)

elif et == "result":
res = evt.get("result")
Expand Down
87 changes: 87 additions & 0 deletions tests/tier3/test_claude_tool_result_pairing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Claude result lookup must preserve chronological pairing and evidence ordering."""

import json

import pytest

from skillevaluator.tier3.eval_core.log_converters import synthetic_trajectory_from_claude_stream_jsonl


def _message(role, content):
return {"type": role, "message": {"content": content}}


def _call(call_id, path="file.md"):
return {"type": "tool_use", "id": call_id, "name": "Read", "input": {"file_path": path}}


def _result(call_id, content, **extra):
return {"type": "tool_result", "tool_use_id": call_id, "content": content, **extra}


def _trajectory(*events):
return synthetic_trajectory_from_claude_stream_jsonl("\n".join(json.dumps(event) for event in events))


def test_delayed_out_of_order_and_repeated_results_keep_their_steps():
trajectory = _trajectory(
_message("assistant", [_call("a"), _call("b")]),
_message("assistant", "progress update"),
_message("assistant", [_call("c")]),
_message(
"user",
[
_result("b", "failed", is_error=True),
_result("c", [{"type": "text", "text": "third"}]),
_result("a", "first"),
_result("b", "retry result"),
],
),
{"type": "result", "result": "finished"},
)

steps = trajectory["steps"]
assert len(steps) == 4
assert steps[0]["observation"]["results"] == [
{"source_call_id": "b", "content": "failed", "extra": {"tool_result_is_error": True}},
{"source_call_id": "a", "content": "first"},
{"source_call_id": "b", "content": "retry result"},
]
assert steps[1]["message"] == "progress update"
assert steps[1]["observation"]["results"] == []
assert steps[2]["observation"]["results"] == [{"source_call_id": "c", "content": "third"}]
assert steps[3]["message"] == "finished"
assert steps[3]["observation"]["results"] == []


def test_reused_ids_attach_once_to_the_latest_preceding_step():
trajectory = _trajectory(
_message("user", [_result("same", "too early")]),
_message("assistant", [_call("same", "old.md"), _call("same", "duplicate.md")]),
_message("user", [_result("same", "old result")]),
_message("assistant", [_call("same", "new.md"), _call("")]),
_message("user", [_result("same", "new result"), _result("unknown", "orphan"), _result("", "empty")]),
)

first, second = trajectory["steps"]
assert len(first["tool_calls"]) == 2
assert first["observation"]["results"] == [{"source_call_id": "same", "content": "old result"}]
assert second["tool_calls"][0]["arguments"]["path"] == "new.md"
assert second["observation"]["results"] == [{"source_call_id": "same", "content": "new result"}]


@pytest.mark.parametrize("reverse", [False, True])
def test_batched_calls_keep_result_order(reverse):
ids = [f"call-{index}" for index in range(256)]
result_ids = list(reversed(ids)) if reverse else ids
trajectory = _trajectory(
_message("assistant", [_call(call_id) for call_id in ids]),
_message("user", [_result(call_id, f"result {call_id}") for call_id in result_ids]),
)

assert trajectory["steps"][0]["observation"]["results"] == [
{"source_call_id": call_id, "content": f"result {call_id}"} for call_id in result_ids
]
Loading