Files
ai-agent/backend/test_pi_errors.py
gabrielvidalandClaude Opus 5.5 c3945fe965 refactor: retire core runner/ — pi's runner lives in the harness-pi plugin
runner/ (cli.mjs, extensions/folder-permissions.ts, test, README) was a
byte-for-byte duplicate of the harness-pi plugin's runner/, kept only as the
driver's fallback through the sidecar's config.RUNNER_BIN.

- delete runner/ and sidecar config.RUNNER_BIN
- scripts/test.py: drop the `runner` suite (it runs in ai-agent-plugins now)
- scripts/ab: the pi mirror replay uses a tree's runner/ when it has one
  (refs before this), else the plugin's (AB_PI_RUNNER)
- CLAUDE.md / docs / comments point at harness-pi's runner
- sidecar/local-model.md stays (harness-opencode's LOCAL_MODEL_GUIDE)

Merge only after harness-pi 1.1.0 is installed on the lab: harness-pi 1.0.x
reads config.RUNNER_BIN and would fail every pi run on this core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 11:44:54 +01:00

102 lines
4.2 KiB
Python

"""A pi turn that failed reads as the error card, not a silent empty reply.
The runner (harness-pi's runner/cli.mjs) writes Claude Code's API-error turn with its own
``error`` fields; transcripts from before it did hold an empty
``stop_reason: "error"`` record, which the parser reads as one too. Neither
makes the failed model the one the conversation runs on."""
import json
import conversations as c
SID = "f88d0a9d-ddf3-4a3f-97f7-e9e073f96a90"
LOCAL = "ggml-org/qwen3.8-27b"
def _user(text, ts):
return {"type": "user", "sessionId": SID, "timestamp": ts,
"message": {"role": "user", "content": text}}
def _answer(ts):
return {"type": "assistant", "sessionId": SID, "timestamp": ts, "costUSD": 0,
"message": {"role": "assistant", "model": LOCAL, "stop_reason": "stop",
"usage": {"input_tokens": 10, "output_tokens": 2},
"content": [{"type": "text", "text": "Done ✅"}]}}
def _runner_error(ts, error, text):
return {"type": "assistant", "sessionId": SID, "timestamp": ts,
"isApiErrorMessage": True, "error": error,
"message": {"role": "assistant", "model": "<synthetic>",
"stop_reason": "stop_sequence",
"usage": {"input_tokens": 0, "output_tokens": 0},
"content": [{"type": "text", "text": text}]}}
def _legacy_error(ts):
return {"type": "assistant", "sessionId": SID, "timestamp": ts, "costUSD": 0,
"message": {"role": "assistant", "model": "qwen/qwen3.8-27b",
"stop_reason": "error", "usage": {"input_tokens": 0,
"output_tokens": 0},
"content": []}}
def _turn_end(ts):
return {"type": "system", "subtype": "turn_end", "entrypoint": "pi",
"sessionId": SID, "timestamp": ts}
def _parse(tmp_path, recs):
p = tmp_path / f"{SID}.jsonl"
p.write_text("".join(json.dumps(r) + "\n" for r in recs))
st = c.ParserState(p, full=True)
for r in recs:
st.feed(r)
return st, c.parse_conversation(p)
def test_the_runners_expired_key_turn_is_an_api_key_card(tmp_path):
text = "API Error (openrouter · qwen/qwen3.8-27b): 401 API key expired."
st, summary = _parse(tmp_path, [
_user("fix it", "2026-11-06T16:09:30Z"), _answer("2026-11-06T16:14:49Z"),
_turn_end("2026-11-06T16:14:50Z"),
_user("test it", "2026-11-06T16:16:27Z"),
_runner_error("2026-11-06T16:16:29Z", "invalid_api_key", text),
_turn_end("2026-11-06T16:16:30Z")])
card = st.thread[-1]
assert card["apiError"] == "api_key"
assert card["text"] == text
assert summary["lastApiError"]["kind"] == "api_key"
# The conversation still runs on the model that answered.
assert summary["contextModel"] == LOCAL
def test_context_and_dropped_stream_fields():
assert c.api_error_kind("context_exceeded", "Context size has been exceeded.") == "context"
assert c.api_error_kind("network_error", "terminated") == "network"
# Text alone, for a CLI that wrote none of these fields.
assert c.api_error_kind(None, "API Error: Prompt is too long") == "context"
assert c.api_error_kind(None, '401 {"message":"User not found."}') == "api_key"
# A Claude login stays `auth` (its card's Log in button).
assert c.api_error_kind(None, "Invalid API key · Please run /login") == "auth"
def test_an_old_silent_error_turn_reads_as_an_error_card(tmp_path):
st, summary = _parse(tmp_path, [
_user("fix it", "2026-11-06T16:09:30Z"), _answer("2026-11-06T16:14:49Z"),
_user("test it", "2026-11-06T16:16:27Z"), _legacy_error("2026-11-06T16:16:29Z"),
_turn_end("2026-11-06T16:16:30Z")])
card = st.thread[-1]
assert card["apiError"] == "unknown"
assert "qwen/qwen3.8-27b" in card["text"] and "didn't record why" in card["text"]
assert summary["lastApiError"]["kind"] == "unknown"
assert summary["contextModel"] == LOCAL
assert "qwen/qwen3.8-27b" not in (summary.get("models") or {})
def test_a_failed_turn_that_streamed_something_is_left_alone():
o = _legacy_error("2026-11-06T16:16:29Z")
o["message"]["content"] = [{"type": "text", "text": "partial"}]
assert c.legacy_error_turn(o) is o