- planner.py: LLMPlanningAgent offers exactly two tools, submit_plan and ask_human, so gating stays structural at the tool layer; no proceed tool exists. System prompt requires explicit, low-inference plans because a weaker model executes them (A13) - Model output is appended to the canonical log as it arrives, before being interpreted, so a crash mid-planning leaves a truthful transcript - A bad turn (prose instead of a tool call, malformed arguments, or a failed call) retries once, then raises PlanningError; the orchestrator gates to AWAIT_INPUT rather than inventing a plan - Orchestrator: appends every operator message, plan_submitted, and gate_opened to the transcript, so re-planning sees rejection feedback as conversation rather than out-of-band parameters; pins the A11 role mapping at task start - llm.py: non-JSON or non-object tool arguments now raise LLMError instead of escaping as JSONDecodeError — observed from llama3.2, which emits truncated argument strings - Live planning test is opt-in on NISSE_LIVE_PLANNER_MODEL: planning escalates unconditionally (A12) and the local model does not hold the tool-call contract, so it awaits a frontier route Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
211 lines
7.3 KiB
Python
211 lines
7.3 KiB
Python
import io
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from nisse_dev.canonical import (
|
|
AssistantText,
|
|
CanonicalEvent,
|
|
PlanSubmitted,
|
|
StoredEvent,
|
|
ToolCall,
|
|
UserMessage,
|
|
)
|
|
from nisse_dev.control import AskHuman, Orchestrator, PlanningError, SubmitPlan
|
|
from nisse_dev.llm import RoleMap
|
|
from nisse_dev.oplog import OpLog
|
|
from nisse_dev.planner import PLANNER_TOOLS, LLMPlanningAgent
|
|
from nisse_dev.state import StateStore
|
|
from nisse_dev.transport import InMemoryTransport
|
|
|
|
ROLES = RoleMap(
|
|
executor="local/qwen",
|
|
planner="anthropic/claude-opus-5",
|
|
delegate="anthropic/claude-opus-5",
|
|
consultant="anthropic/claude-opus-5",
|
|
)
|
|
|
|
|
|
class ScriptedClient:
|
|
"""Returns canned canonical events per call, recording what it was asked."""
|
|
|
|
def __init__(self, turns: list[list[CanonicalEvent]]) -> None:
|
|
self._turns = turns
|
|
self.calls: list[dict[str, Any]] = []
|
|
|
|
def complete(
|
|
self,
|
|
events: list[StoredEvent],
|
|
tools: list[dict[str, Any]] | None,
|
|
model: str,
|
|
system: str | None = None,
|
|
) -> list[CanonicalEvent]:
|
|
self.calls.append(
|
|
{"events": events, "tools": tools, "model": model, "system": system}
|
|
)
|
|
if not self._turns:
|
|
return []
|
|
return self._turns.pop(0)
|
|
|
|
|
|
def plan_call(content: str, gate: str = "arch") -> ToolCall:
|
|
return ToolCall(
|
|
call_id="call-1",
|
|
tool="submit_plan",
|
|
arguments={"type": gate, "content": content},
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def store(tmp_path: Path) -> StateStore:
|
|
return StateStore(tmp_path / "state.db", oplog=OpLog("nisse-dev", io.StringIO()))
|
|
|
|
|
|
def test_submit_plan_tool_call_becomes_submit_plan(store: StateStore) -> None:
|
|
client = ScriptedClient([[plan_call("step 1: create src/app.py")]])
|
|
agent = LLMPlanningAgent(client, store, ROLES)
|
|
action = agent.plan("arch", "demo")
|
|
assert action == SubmitPlan("step 1: create src/app.py")
|
|
|
|
|
|
def test_ask_human_tool_call_becomes_ask_human(store: StateStore) -> None:
|
|
client = ScriptedClient(
|
|
[
|
|
[
|
|
ToolCall(
|
|
call_id="call-1",
|
|
tool="ask_human",
|
|
arguments={"question": "postgres or sqlite?"},
|
|
)
|
|
]
|
|
]
|
|
)
|
|
action = LLMPlanningAgent(client, store, ROLES).plan("arch", "demo")
|
|
assert action == AskHuman("postgres or sqlite?")
|
|
|
|
|
|
def test_planner_role_and_tools_are_used(store: StateStore) -> None:
|
|
client = ScriptedClient([[plan_call("plan")]])
|
|
LLMPlanningAgent(client, store, ROLES).plan("arch", "demo")
|
|
call = client.calls[0]
|
|
assert call["model"] == "anthropic/claude-opus-5" # never the executor
|
|
assert [t["function"]["name"] for t in call["tools"]] == [
|
|
"submit_plan",
|
|
"ask_human",
|
|
]
|
|
assert "demo" in call["system"] and "arch" in call["system"]
|
|
|
|
|
|
def test_no_proceed_tool_is_offered() -> None:
|
|
names = {tool["function"]["name"] for tool in PLANNER_TOOLS}
|
|
assert names == {"submit_plan", "ask_human"} # no self-advancement path
|
|
|
|
|
|
def test_model_output_is_appended_to_the_log(store: StateStore) -> None:
|
|
client = ScriptedClient(
|
|
[[AssistantText(text="thinking out loud", model="m"), plan_call("the plan")]]
|
|
)
|
|
LLMPlanningAgent(client, store, ROLES).plan("arch", "demo")
|
|
kinds = [type(e.event).__name__ for e in store.read_events()]
|
|
assert kinds == ["AssistantText", "ToolCall"]
|
|
|
|
|
|
def test_prose_only_turn_retries_once_then_raises(store: StateStore) -> None:
|
|
client = ScriptedClient(
|
|
[
|
|
[AssistantText(text="here's what I think", model="m")],
|
|
[AssistantText(text="still prose", model="m")],
|
|
]
|
|
)
|
|
with pytest.raises(PlanningError, match="no submit_plan or ask_human"):
|
|
LLMPlanningAgent(client, store, ROLES).plan("arch", "demo")
|
|
assert len(client.calls) == 2
|
|
assert "did not call a tool" in client.calls[1]["system"]
|
|
|
|
|
|
def test_retry_succeeds_after_prose(store: StateStore) -> None:
|
|
client = ScriptedClient(
|
|
[[AssistantText(text="prose", model="m")], [plan_call("the real plan")]]
|
|
)
|
|
action = LLMPlanningAgent(client, store, ROLES).plan("arch", "demo")
|
|
assert action == SubmitPlan("the real plan")
|
|
|
|
|
|
def test_malformed_tool_arguments_are_rejected(store: StateStore) -> None:
|
|
blank = ToolCall(call_id="c", tool="submit_plan", arguments={"content": " "})
|
|
client = ScriptedClient([[blank], [blank]])
|
|
with pytest.raises(PlanningError):
|
|
LLMPlanningAgent(client, store, ROLES).plan("arch", "demo")
|
|
|
|
|
|
def test_planner_sees_prior_transcript(store: StateStore) -> None:
|
|
store.append_event(UserMessage(text="use demo", source="transport:@don"))
|
|
client = ScriptedClient([[plan_call("plan")]])
|
|
LLMPlanningAgent(client, store, ROLES).plan("arch", "demo")
|
|
seen = [e.event for e in client.calls[0]["events"]]
|
|
assert UserMessage(text="use demo", source="transport:@don") in seen
|
|
|
|
|
|
# --- orchestrator integration (still no network) -------------------------
|
|
|
|
|
|
def build(tmp_path: Path, client: ScriptedClient) -> tuple[Orchestrator, StateStore]:
|
|
store = StateStore(tmp_path / "state.db", oplog=OpLog("nisse-dev", io.StringIO()))
|
|
transport = InMemoryTransport()
|
|
agent = LLMPlanningAgent(client, store, ROLES)
|
|
orch = Orchestrator(store, transport, agent, operators={"don"}, roles=ROLES)
|
|
orch.start()
|
|
return orch, store, transport
|
|
|
|
|
|
def test_use_project_produces_llm_plan_and_transcript(tmp_path: Path) -> None:
|
|
client = ScriptedClient([[plan_call("1. create app.py\n2. add a test")]])
|
|
_, store, transport = build(tmp_path, client)
|
|
transport.receive("don", "use demo")
|
|
|
|
request = transport.outbound[-1]
|
|
assert request.kind == "approval_request"
|
|
assert "create app.py" in request.text
|
|
|
|
events = [e.event for e in store.read_events()]
|
|
kinds = [type(e).__name__ for e in events]
|
|
assert kinds == ["UserMessage", "ToolCall", "PlanSubmitted", "GateOpened"]
|
|
submitted = next(e for e in events if isinstance(e, PlanSubmitted))
|
|
assert submitted.model == "anthropic/claude-opus-5" # pinned planner
|
|
|
|
|
|
def test_roles_pinned_at_task_start(tmp_path: Path) -> None:
|
|
client = ScriptedClient([[plan_call("plan")]])
|
|
_, store, transport = build(tmp_path, client)
|
|
transport.receive("don", "use demo")
|
|
state = store.get_state()
|
|
assert state is not None
|
|
assert state.data["roles"]["planner"] == "anthropic/claude-opus-5"
|
|
|
|
|
|
def test_planning_failure_gates_to_await_input(tmp_path: Path) -> None:
|
|
prose = [AssistantText(text="prose", model="m")]
|
|
client = ScriptedClient([prose, prose])
|
|
_, store, transport = build(tmp_path, client)
|
|
transport.receive("don", "use demo")
|
|
state = store.get_state()
|
|
assert state is not None
|
|
assert state.state == "AWAIT_INPUT"
|
|
assert transport.outbound[-1].kind == "question"
|
|
|
|
|
|
def test_rejection_feedback_reaches_the_planner_via_the_log(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
client = ScriptedClient([[plan_call("v1")], [plan_call("v2")]])
|
|
_, _store, transport = build(tmp_path, client)
|
|
transport.receive("don", "use demo")
|
|
transport.receive("don", "too vague, name the files")
|
|
|
|
replan_context = [e.event for e in client.calls[1]["events"]]
|
|
assert any(
|
|
isinstance(e, UserMessage) and "too vague" in e.text for e in replan_context
|
|
)
|
|
assert "v2" in transport.outbound[-1].text
|