Files
ai-agent/backend/test_pi_errors.py
gabrielvidalandClaude Opus 5.5 1017684db4 fix(pi): a follow-up keeps the model it ran on, and a failed call shows its error
An untouched follow-up of a pi conversation started on the EVOX2's
ggml-org/qwen3.8-27b went out with no model; the sidecar filled in
PI_DEFAULT_MODEL — OpenRouter's twin qwen/qwen3.8-27b — whose key expired on
2026-10-31, so the turn died in 0.3 s. The runner dropped pi's errorMessage
and wrote an empty reply, so the conversation just looked finished.

- sidecar/pi_runs.py: session_model() reads the model a pi session last
  answered on from pi's own session file; resume and fork use it when nothing
  was picked. PI_DEFAULT_MODEL (new runs) is now the local Qwen 3.8.
- runner: a failed call is Claude Code's API-error turn (isApiErrorMessage,
  <synthetic> model, typed error field); a failure pi retries is dropped, an
  EVOX2 that never wakes or a pi that dies before answering get one too, the
  prompt is recorded before the wake, and the exit waits for stdout to drain.
  A --resume with no --model keeps the session's model there too.
- parser: invalid_api_key → api_key, context_exceeded → context,
  network_error → network; an old empty stop_reason "error" turn reads as a
  "didn't record why" card (23 of 61 pi sessions ended that way).
- error card: "API key refused" / "Context full", provider-neutral hints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 17:49:19 +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 (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