Fix the run-breaking tool-result order; e2e test runs the slice (M2.1, PR D)
CI / checks (push) Skipped
CI / checks (pull_request) Successful in 2m17s

The first live run of the new loop's e2e test found that a fact recorded
while start_milestone was in flight landed between the tool call and its
result; both API formats reject that, so every later request in the run
failed. Views now keep each result right after its call. Scripted test
models check the APIs' ordering rules, so this can't pass unit tests
again.

The e2e tests no longer depend on the sandbox's history: each run asks for
a uniquely named function and works against a base branch of its own,
deleted afterwards. The review step asks for a change that can't already
be there. The scripted operator gives up after five fruitless answers, and
each run keeps its state store for inspection. The driver's prompt says
review fixes stay in the current milestone.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PPQ14L3LikyefCMFBqEbKE
This commit is contained in:
dongarandClaude Opus 5.5 committed 2026-10-03 15:11:06 +00:00
1 parent 30ab5c9b9c
commit 37965aefca
11 files changed
+203 -37

No files matched your search

+2 -2
View File
@@ -70,8 +70,8 @@ After the go-ahead, work in a milestone: call start_milestone before
changing anything (never create or switch branches yourself), make the
change, run the project's tests, then call finish_milestone with a summary.
The harness commits, pushes and opens a PR, and you wait for review. Review
comments arrive as messages: make the changes, run the tests, and call
finish_milestone again to push the fix. When the PR has merged, call
comments arrive as messages: make the changes within the same milestone (don't
start another), run the tests, and call finish_milestone again to push the fix. When the PR has merged, call
close_item with what was done, what was verified, and what is left open. If
the PR is closed without merging, ask the operator what next.
+30 -3
View File
@@ -90,8 +90,9 @@ def project(events: list[StoredEvent], viewer: str) -> list[StoredEvent]:
shown = _SHOWN.get(viewer)
if shown is None:
# A role with no view defined (the expert is stateless, the
# classifier sees only an action) reads the log as it is.
return list(events)
# classifier sees only an action) reads the log as it is, with
# results kept after their calls.
return _results_follow_calls(list(events))
results = {
e.event.call_id: e.event for e in events if isinstance(e.event, ToolResult)
}
@@ -128,4 +129,30 @@ def project(events: list[StoredEvent], viewer: str) -> list[StoredEvent]:
out.append(_said(stored, actor, line))
elif isinstance(event, AssistantText) and event.text.strip():
out.append(_said(stored, actor, event.text.strip()))
return out
return _results_follow_calls(out)
def _results_follow_calls(events: list[StoredEvent]) -> list[StoredEvent]:
"""Keep each tool result right after its call, as both API formats
require: anything recorded while a call was in flight (a fact the
harness wrote while handling it, say) moves to just after the results.
A call with no result yet keeps its place; the renderer answers it."""
out: list[StoredEvent] = []
pending: set[str] = set()
held: list[StoredEvent] = []
for stored in events:
event = stored.event
if isinstance(event, ToolCall):
pending.add(event.call_id)
out.append(stored)
elif isinstance(event, ToolResult) and event.call_id in pending:
pending.discard(event.call_id)
out.append(stored)
if not pending:
out.extend(held)
held = []
elif pending:
held.append(stored)
else:
out.append(stored)
return out + held
+58
View File
@@ -0,0 +1,58 @@
"""What a real model API insists on, for scripted test models.
Both formats the agent sends require a tool call's result to come right
after it: in Anthropic's, the next message is the user's and starts with a
tool_result for every tool_use, before any text; in OpenAI's, the assistant
message with tool_calls is followed directly by a tool message for each. A
scripted model that doesn't check this lets a broken transcript pass the
unit tests and be rejected live; this makes it fail here.
"""
from typing import Any
from nisse_dev.render import OpenAIChatRenderer
from nisse_dev.render_anthropic import AnthropicMessagesRenderer
def assert_renderable(events: list[Any]) -> None:
_anthropic(AnthropicMessagesRenderer().render(list(events), None, "m")["messages"])
_openai(OpenAIChatRenderer().render(list(events), None, "m")["messages"])
def _anthropic(messages: list[dict[str, Any]]) -> None:
for index, message in enumerate(messages):
if message["role"] != "assistant":
continue
used = [b["id"] for b in message["content"] if b.get("type") == "tool_use"]
if not used:
continue
following = messages[index + 1] if index + 1 < len(messages) else None
assert following is not None and following["role"] == "user", (
f"anthropic messages.{index}: tool_use with no user message after it"
)
blocks = following["content"]
leading = []
for block in blocks:
if block.get("type") != "tool_result":
break
leading.append(block["tool_use_id"])
assert set(used) <= set(leading), (
f"anthropic messages.{index}: tool_use ids {sorted(set(used) - set(leading))} "
"lack a tool_result at the start of the next message"
)
def _openai(messages: list[dict[str, Any]]) -> None:
for index, message in enumerate(messages):
calls = [c["id"] for c in message.get("tool_calls") or []]
if not calls:
continue
answered = []
for after in messages[index + 1 :]:
if after["role"] != "tool":
break
answered.append(after["tool_call_id"])
assert set(calls) <= set(answered), (
f"openai messages.{index}: tool_calls {sorted(set(calls) - set(answered))} "
"are not answered by the tool messages right after them"
)
+2
View File
@@ -6,6 +6,7 @@ from pathlib import Path
from typing import Any
import pytest
from model_contract import assert_renderable
from nisse_dev.cancel import Cancel
from nisse_dev.canonical import AssistantText, CanonicalEvent, Fact, ToolCall
@@ -34,6 +35,7 @@ class ScriptedModel:
self.seen: list[list[Any]] = []
def complete(self, events, tools, model, system=None) -> list[CanonicalEvent]:
assert_renderable(events) # as the real API would
self.offered.append({t["function"]["name"] for t in tools or []})
self.seen.append(events)
assert self.turns, "the model was called more often than scripted"
+2
View File
@@ -8,6 +8,7 @@ from pathlib import Path
from typing import Any
import pytest
from model_contract import assert_renderable
from nisse_dev.cancel import Cancel
from nisse_dev.canonical import ToolCall
@@ -42,6 +43,7 @@ class ScriptedModel:
self.turns = list(turns)
def complete(self, events, tools, model, system=None) -> list[Any]:
assert_renderable(events) # as the real API would
assert self.turns, "the model was called more often than scripted"
return self.turns.pop(0)
+2
View File
@@ -13,6 +13,7 @@ from pathlib import Path
from typing import Any
import pytest
from model_contract import assert_renderable
from test_loop_milestones import DON, Forge, call
from test_loop_milestones import world as world # noqa: PLC0414 -- the fixture
@@ -41,6 +42,7 @@ class PolicyModel:
self.n = 0
def complete(self, events, tools, model, system=None) -> list[Any]:
assert_renderable(events) # as the real API would
self.n += 1
offered = {t["function"]["name"] for t in tools}
facts = [getattr(e.event, "text", "") for e in events]
+15
View File
@@ -282,3 +282,18 @@ def test_operator_messages_carry_their_channel(tmp_path: Path) -> None:
"[operator, comment on plan issue #9] hi",
"[operator, Gitea comment] hi",
]
def test_something_written_mid_call_moves_after_the_result(tmp_path: Path) -> None:
# Live, M2.1: start_milestone's fact landed between the call and its
# result, and the model API rejected every later request in the run.
store = new_store(tmp_path)
driver = store.transcript("a", "driver")
driver.append(ToolCall(call_id="c1", tool="start_milestone", arguments={}))
store.transcript("a", "orchestrator").append(Fact(text="milestone 1 started"))
driver.append(ToolResult(call_id="c1", result="started", success=True))
kinds = [type(e.event).__name__ for e in driver.view()]
assert kinds == ["ToolCall", "ToolResult", "UserMessage"]
body = AnthropicMessagesRenderer().render(driver.view(), tools=None, model="m")
result_first = body["messages"][1]["content"][0]
assert result_first["type"] == "tool_result"