On #166 the reviewer called post_review with two points unanswered, was refused, answered them, then only skipped already-answered points until the 40-turn cap: the review went out as partial, its summary lost. A refused ending call is now held, and the driver ends the run with it as soon as the last point is settled (a new due hook in nisse-core's driver; nisse-dev doesn't use it). A held call that is wrong for another reason by then (sign_off over drafted comments) is left to the model. With nothing open, reply and skip say which tool ends the run. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J5Utyfzw7gF7UhjRDEtjTk
153 lines
5.1 KiB
Python
153 lines
5.1 KiB
Python
"""A driver run on its own (L2), as any agent uses it: the agent says which
|
|
tools wait, and execution goes to whatever executor it gives (L13)."""
|
|
|
|
import io
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from nisse_core.log.canonical import CanonicalEvent, ToolCall, ToolResult
|
|
from nisse_core.loop.driver import Driver, ToolOutcome
|
|
from nisse_core.loop.tools import tool
|
|
from nisse_core.oplog import OpLog
|
|
from nisse_core.state.store import StateStore
|
|
|
|
|
|
class Scripted:
|
|
def __init__(self, *turns: list[CanonicalEvent]) -> None:
|
|
self._turns = list(turns)
|
|
|
|
def complete(self, events, tools, model, system=None) -> list[CanonicalEvent]:
|
|
return self._turns.pop(0)
|
|
|
|
|
|
def call(name: str, **arguments: str) -> ToolCall:
|
|
return ToolCall(call_id=f"c-{name}", tool=name, arguments=arguments)
|
|
|
|
|
|
class Reader:
|
|
"""Not a workspace: reads from a dict, as a review agent's might."""
|
|
|
|
def __init__(self) -> None:
|
|
self.calls: list[tuple[str, dict[str, Any]]] = []
|
|
|
|
def dispatch(self, tool: str, arguments: dict[str, Any]) -> ToolOutcome:
|
|
self.calls.append((tool, arguments))
|
|
return ToolOutcome({"a.py": "x = 1"}.get(arguments["path"], "none"), True)
|
|
|
|
|
|
READ = tool("read_file", "Read a file.", path="Its path.")
|
|
POST = tool("post_review", "Post the review. Ends your turn.", body="The review.")
|
|
ASK = tool("ask_human", "Ask the operator.", question="The question.")
|
|
|
|
|
|
def transcript(tmp_path: Path) -> Any:
|
|
store = StateStore(tmp_path / "s.db", oplog=OpLog("t", io.StringIO()))
|
|
return store.transcript("x", "driver")
|
|
|
|
|
|
def results(log: Any) -> list[str]:
|
|
return [e.event.result for e in log.read() if isinstance(e.event, ToolResult)]
|
|
|
|
|
|
def test_the_agent_says_which_tools_wait(tmp_path: Path) -> None:
|
|
model = Scripted(
|
|
[call("read_file", path="a.py")], [call("post_review", body="looks fine")]
|
|
)
|
|
reader = Reader()
|
|
log = transcript(tmp_path)
|
|
ran = Driver(model, "m").run(
|
|
log,
|
|
system="s",
|
|
tools=[READ, POST],
|
|
waiting={"post_review"},
|
|
workspace=reader,
|
|
)
|
|
assert (ran.kind, ran.tool, ran.arguments) == (
|
|
"wait",
|
|
"post_review",
|
|
{"body": "looks fine"},
|
|
)
|
|
assert reader.calls == [("read_file", {"path": "a.py"})]
|
|
assert results(log) == ["x = 1", "posted; your turn has ended"]
|
|
|
|
|
|
def test_a_tool_not_named_waiting_doesnt_end_the_run(tmp_path: Path) -> None:
|
|
# ask_human waits in nisse-dev; here no handler answers it and it isn't
|
|
# waiting, so it is no tool at all, and the run goes on.
|
|
model = Scripted([call("ask_human", question="?")], [call("post_review", body="")])
|
|
log = transcript(tmp_path)
|
|
ran = Driver(model, "m").run(
|
|
log, system="s", tools=[ASK, POST], waiting={"post_review"}, workspace=None
|
|
)
|
|
assert ran.tool == "post_review"
|
|
assert results(log)[0].startswith("no such tool: ask_human")
|
|
|
|
|
|
NOTE = tool("note", "Note something.", text="The note.")
|
|
|
|
|
|
def test_a_held_call_goes_ahead_once_due_says_so(tmp_path: Path) -> None:
|
|
# post_review is refused while a point is open and held by the agent;
|
|
# the handled call that settles the point ends the run with it.
|
|
open_points = ["P1"]
|
|
held: dict[str, Any] = {}
|
|
|
|
def refuse(args: dict[str, Any]) -> str | None:
|
|
if open_points:
|
|
held["call"] = ("post_review", args)
|
|
return "answer P1 first"
|
|
return None
|
|
|
|
def settle(args: dict[str, Any]) -> tuple[str, bool]:
|
|
open_points.clear()
|
|
return "answered P1", True
|
|
|
|
def due() -> tuple[str, dict[str, Any], str] | None:
|
|
if "call" in held and not open_points:
|
|
tool_, args = held.pop("call")
|
|
return tool_, args, "your held post_review went ahead"
|
|
return None
|
|
|
|
model = Scripted(
|
|
[call("post_review", body="the summary")],
|
|
[call("note", text="P1"), call("read_file", path="a.py")],
|
|
# never reached: the run ended on the note
|
|
[call("note", text="again")],
|
|
)
|
|
log = transcript(tmp_path)
|
|
reader = Reader()
|
|
ran = Driver(model, "m").run(
|
|
log,
|
|
system="s",
|
|
tools=[READ, POST, NOTE],
|
|
waiting={"post_review"},
|
|
workspace=reader,
|
|
handlers={"note": settle},
|
|
checks={"post_review": refuse},
|
|
due=due,
|
|
)
|
|
assert (ran.kind, ran.tool, ran.arguments) == (
|
|
"wait",
|
|
"post_review",
|
|
{"body": "the summary"},
|
|
)
|
|
assert results(log) == [
|
|
"answer P1 first",
|
|
"answered P1\n\n[harness] your held post_review went ahead; your turn has ended",
|
|
"not run: the turn had ended",
|
|
]
|
|
assert reader.calls == [] # the read after it never ran
|
|
|
|
|
|
def test_without_due_a_settled_point_leaves_the_run_going(tmp_path: Path) -> None:
|
|
model = Scripted([call("note", text="x")], [call("post_review", body="b")])
|
|
ran = Driver(model, "m").run(
|
|
transcript(tmp_path),
|
|
system="s",
|
|
tools=[POST, NOTE],
|
|
waiting={"post_review"},
|
|
workspace=None,
|
|
handlers={"note": lambda args: ("noted", True)},
|
|
)
|
|
assert (ran.tool, ran.arguments) == ("post_review", {"body": "b"})
|