From cd8341fafe933d210dcbb439d8360cb266c751f3 Mon Sep 17 00:00:00 2001 From: Kiran Date: Tue, 27 Oct 2026 12:49:38 +0100 Subject: [PATCH 1/3] feat: record Claude Code's autocompact thrashing stop as an error on the trace (RIUS-1666) --- scripts/rius_cc/spans.py | 26 ++++++- scripts/rius_cc/transcript.py | 5 ++ tests/fixtures/autocompact_thrashing.jsonl | 3 + tests/test_autocompact_thrashing.py | 88 ++++++++++++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/autocompact_thrashing.jsonl create mode 100644 tests/test_autocompact_thrashing.py diff --git a/scripts/rius_cc/spans.py b/scripts/rius_cc/spans.py index 0b42449..e092eba 100644 --- a/scripts/rius_cc/spans.py +++ b/scripts/rius_cc/spans.py @@ -50,6 +50,10 @@ # Status.message for a failed tool when content capture is off. Spelled out # rather than a bare "tool error" so a viewer can tell "we deliberately did # not send you the detail" from "the detail went missing". +# Claude Code stops the turn with a row of this apiError when compaction keeps +# refilling the context. The row is a synthetic assistant message, so the span +# built from it is marked as the failure it is. +AUTOCOMPACT_THRASHING = "autocompact_thrashing" TOOL_ERROR_WITHHELD = "tool error (detail withheld: content capture off)" # A failed tool's error message is one line that says why, capped here. The @@ -625,14 +629,30 @@ def _generation_span(ctx: Ctx, trace_id: str, gen: dict, lines: List[Any], if gen.get("context_sizes"): # Sizes, not content: sent whatever the capture setting. attrs[semconv.RIUS_CONTEXT_SIZES] = gen["context_sizes"] - _content_attr(ctx, attrs, semconv.OUTPUT_VALUE, - earlier_text + "".join(line.text() for line in lines)) + text = earlier_text + "".join(line.text() for line in lines) + _content_attr(ctx, attrs, semconv.OUTPUT_VALUE, text) + + status_code, status_message, events = "OK", "", [] + if any(line.api_error() == AUTOCOMPACT_THRASHING for line in lines): + # The stop has no hook and no span of its own: this row is all there + # is. Its text is Claude Code's message, but a later version may + # quote a path in it, so it follows the capture gate like a tool's. + status_code = "ERROR" + status_message = AUTOCOMPACT_THRASHING + if ctx.capture_content and text: + status_message = scrub.scrub(text) + attrs[semconv.ERROR_TYPE] = AUTOCOMPACT_THRASHING + events.append((lines[-1].timestamp_ns, semconv.EXCEPTION_EVENT, { + semconv.EXCEPTION_TYPE: AUTOCOMPACT_THRASHING, + semconv.EXCEPTION_MESSAGE: status_message, + })) return Span( trace_id=trace_id, span_id=gen["span_id"], parent_span_id=gen["parent_span_id"], name=model or "assistant", kind_oi="LLM", start_ns=gen["start_ns"], end_ns=lines[-1].timestamp_ns, - attributes=attrs, status_code="OK", status_message="", pending=False, + attributes=attrs, status_code=status_code, status_message=status_message, + pending=False, events=events, ) diff --git a/scripts/rius_cc/transcript.py b/scripts/rius_cc/transcript.py index 9a60ae3..ed6e4f7 100644 --- a/scripts/rius_cc/transcript.py +++ b/scripts/rius_cc/transcript.py @@ -113,6 +113,11 @@ def is_compact_summary(self) -> bool: """The user entry that holds a compaction's summary text.""" return self.kind == "user" and bool(self.raw.get("isCompactSummary")) + def api_error(self) -> str: + """Claude Code's name for the stop it wrote this row for, else "".""" + value = self.raw.get("apiError") + return value if isinstance(value, str) else "" + def compact_metadata(self) -> Dict[str, Any]: meta = self.raw.get("compactMetadata") return meta if isinstance(meta, dict) else {} diff --git a/tests/fixtures/autocompact_thrashing.jsonl b/tests/fixtures/autocompact_thrashing.jsonl new file mode 100644 index 0000000..7ba291a --- /dev/null +++ b/tests/fixtures/autocompact_thrashing.jsonl @@ -0,0 +1,3 @@ +{"isSidechain": false, "sessionId": "99999999-9999-9999-9999-999999999999", "cwd": "/tmp/proj", "gitBranch": "main", "version": "2.1.304", "parentUuid": null, "type": "user", "uuid": "u1", "timestamp": "2026-10-08T08:54:00.000Z", "promptId": "p1", "message": {"role": "user", "content": "read the big log"}} +{"isSidechain": false, "sessionId": "99999999-9999-9999-9999-999999999999", "cwd": "/tmp/proj", "gitBranch": "main", "version": "2.1.304", "parentUuid": "u1", "type": "assistant", "uuid": "a1", "timestamp": "2026-10-08T08:54:05.000Z", "message": {"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-opus-5", "stop_reason": "end_turn", "content": [{"type": "text", "text": "Reading it."}], "usage": {"input_tokens": 150000, "output_tokens": 20}}} +{"isSidechain": false, "sessionId": "99999999-9999-9999-9999-999999999999", "cwd": "/tmp/proj", "gitBranch": "main", "version": "2.1.304", "parentUuid": "a1", "type": "assistant", "uuid": "a2", "timestamp": "2026-10-08T08:54:16.233Z", "message": {"id": "msg_2", "type": "message", "role": "assistant", "model": "", "stop_reason": "stop_sequence", "stop_sequence": "", "content": [{"type": "text", "text": "Autocompact is thrashing: the context refilled to the limit within 3 turns of the previous compact, 3 times in a row. A file being read or a tool output is likely too large for the context window. Try reading in smaller chunks, or use /clear to start fresh."}], "usage": {"input_tokens": 0, "output_tokens": 0, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0}}, "apiError": "autocompact_thrashing", "error": "invalid_request", "isApiErrorMessage": true} diff --git a/tests/test_autocompact_thrashing.py b/tests/test_autocompact_thrashing.py new file mode 100644 index 0000000..f79bb0a --- /dev/null +++ b/tests/test_autocompact_thrashing.py @@ -0,0 +1,88 @@ +"""Claude Code's autocompact thrashing stop is an error on the session's trace. + +When compaction keeps refilling the context, Claude Code stops the turn and +writes a synthetic assistant row with `apiError: "autocompact_thrashing"`. +No hook carries it, so the transcript row is the only record. It becomes the +LLM span it always was, now ERROR, with error.type and an exception event at +the row's time. tests/fixtures/autocompact_thrashing.jsonl is that row, as +Claude Code 2.1.292 wrote it, after an ordinary call. +""" +import json + +from rius_cc import spans, state, transcript +from rius_cc import _semconv_generated as semconv + +SESSION = "99999999-9999-9999-9999-999999999999" +THRASHING_NS = 1791449656233000000 # 2026-10-08T08:54:16.233Z +STOP_TEXT = "Autocompact is thrashing: the context refilled to the limit" + + +def _build(entries, capture=True): + ctx = spans.Ctx(session_id=SESSION, cwd="/tmp/proj", git_branch="main", + cc_version="2.1.304", service_name="claude-code", + capture_content=capture, max_attr_bytes=32768) + return spans.build(entries, state.new_state(), ctx) + + +def _entries(fixtures_dir, name="autocompact_thrashing.jsonl"): + return transcript.read_from(str(fixtures_dir / name), 0)[0] + + +def _llm(out): + return [s for s in out if s.kind_oi == "LLM"] + + +def _stops(out): + return [s for s in out if s.attributes.get(semconv.ERROR_TYPE)] + + +def test_the_stop_is_an_error_span_with_its_time_and_text(fixtures_dir): + stops = _stops(_build(_entries(fixtures_dir))) + assert len(stops) == 1 + stop = stops[0] + assert stop.kind_oi == "LLM" + assert stop.status_code == "ERROR" + assert stop.attributes[semconv.ERROR_TYPE] == "autocompact_thrashing" + assert stop.end_ns == THRASHING_NS + [(at, name, attrs)] = stop.events + assert at == THRASHING_NS + assert name == semconv.EXCEPTION_EVENT + assert attrs[semconv.EXCEPTION_TYPE] == "autocompact_thrashing" + assert attrs[semconv.EXCEPTION_MESSAGE].startswith(STOP_TEXT) + assert stop.status_message.startswith(STOP_TEXT) + + +def test_the_stop_text_follows_the_capture_gate(fixtures_dir): + [stop] = _stops(_build(_entries(fixtures_dir), capture=False)) + assert stop.attributes[semconv.ERROR_TYPE] == "autocompact_thrashing" + assert stop.status_message == "autocompact_thrashing" + [(_, _, attrs)] = stop.events + assert attrs[semconv.EXCEPTION_MESSAGE] == "autocompact_thrashing" + assert STOP_TEXT not in json.dumps([stop.events, stop.status_message]) + + +def test_the_call_before_the_stop_is_not_marked(fixtures_dir): + out = _build(_entries(fixtures_dir)) + before = [s for s in _llm(out) if s.name == "claude-opus-5"] + assert len(before) == 1 + assert before[0].status_code == "OK" + assert before[0].events == [] + assert semconv.ERROR_TYPE not in before[0].attributes + + +def test_a_transcript_without_the_row_gets_no_stop(fixtures_dir): + for name in ("simple.jsonl", "compaction.jsonl", "tool_call.jsonl"): + out = _build(_entries(fixtures_dir, name)) + assert _stops(out) == [] + assert [s for s in _llm(out) if s.events] == [] + + +def test_other_api_errors_are_left_as_they_were(fixtures_dir): + lines = (fixtures_dir / "autocompact_thrashing.jsonl").read_text().splitlines() + row = json.loads(lines[-1]) + row["apiError"] = "rate_limit" + entries = [transcript.parse_line(json.dumps(r)) + for r in [json.loads(lines[0]), json.loads(lines[1]), row]] + out = _build(entries) + assert _stops(out) == [] + assert all(s.status_code == "OK" for s in _llm(out)) From cd48ce78f2151c13b890354c51bef121903a0801 Mon Sep 17 00:00:00 2001 From: Kiran Date: Tue, 27 Oct 2026 12:57:29 +0100 Subject: [PATCH 2/3] fix: cap the thrashing stop message, keep the withheld text with its comment, document the stop (RIUS-1666) --- README.md | 4 ++-- scripts/rius_cc/spans.py | 8 ++++++-- tests/test_autocompact_thrashing.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cc7e8a8..bee4d01 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ capped at 32 KB. | `rius.context.sizes` | byte sizes of the prompt by role and tool, no text | always | | `gen_ai.tool.name`, `gen_ai.tool.call.id` | `Bash`, `mcp__github__create_issue`; the model's id for the call | always | | `gen_ai.skill.name` | `superpowers:systematic-debugging`: the Agent Skill a `Skill` call loaded | always | -| `error.type`, `exception.type` | `Bash.exit_1`, `Read.tool_error` | always | +| `error.type`, `exception.type` | `Bash.exit_1`, `Read.tool_error`, `autocompact_thrashing`: Claude Code stopped the turn because compaction kept refilling the context | always | | `rius.command.class`, `process.exit.code` | `test`, `1`: the kind of command a shell call ran (`test`, `build`, `lint`, `package`, `git` or `other`), worked out on your machine, and its exit code when known. The command is not sent | always | | `gen_ai.agent.name`, `cc.subagent.id`, `cc.subagent.depth`, `cc.turn.source` | `Explore`, `user` | always | | `rius.span.pending` | `true` while a span is still running | always | @@ -94,7 +94,7 @@ capped at 32 KB. | `gen_ai.agent.description` | a subagent's description | content mode | | `gen_ai.skill.source.uri` | `file:///Users/ana/.claude/skills/review`: the folder a skill loaded from, when Claude Code names it (not for its built-in skills, which it unpacks to a temporary folder) | content mode | | session span name | the session title instead of `claude-code session` | content mode | -| `exception.message`, failed tool status | one scrubbed line of the failing output; otherwise `tool error (detail withheld: content capture off)` | content mode | +| `exception.message`, failed tool status | one scrubbed line of the failing output; otherwise `tool error (detail withheld: content capture off)`. For `autocompact_thrashing`, Claude Code's message; otherwise just `autocompact_thrashing` | content mode | The plugin sends token counts. Rius prices them at API rates, so the cost in the console isn't what you pay on a Pro or Max subscription. diff --git a/scripts/rius_cc/spans.py b/scripts/rius_cc/spans.py index e092eba..18f105d 100644 --- a/scripts/rius_cc/spans.py +++ b/scripts/rius_cc/spans.py @@ -50,11 +50,15 @@ # Status.message for a failed tool when content capture is off. Spelled out # rather than a bare "tool error" so a viewer can tell "we deliberately did # not send you the detail" from "the detail went missing". +TOOL_ERROR_WITHHELD = "tool error (detail withheld: content capture off)" + # Claude Code stops the turn with a row of this apiError when compaction keeps # refilling the context. The row is a synthetic assistant message, so the span # built from it is marked as the failure it is. AUTOCOMPACT_THRASHING = "autocompact_thrashing" -TOOL_ERROR_WITHHELD = "tool error (detail withheld: content capture off)" +# Its message is one fixed sentence of about 260 bytes, which the tool-error +# cap would cut in the middle of its advice. +AUTOCOMPACT_MESSAGE_MAX_BYTES = 512 # A failed tool's error message is one line that says why, capped here. The # backend groups errors by it, so the whole output (an 80-line file a grep @@ -640,7 +644,7 @@ def _generation_span(ctx: Ctx, trace_id: str, gen: dict, lines: List[Any], status_code = "ERROR" status_message = AUTOCOMPACT_THRASHING if ctx.capture_content and text: - status_message = scrub.scrub(text) + status_message = exportable(text, AUTOCOMPACT_MESSAGE_MAX_BYTES) attrs[semconv.ERROR_TYPE] = AUTOCOMPACT_THRASHING events.append((lines[-1].timestamp_ns, semconv.EXCEPTION_EVENT, { semconv.EXCEPTION_TYPE: AUTOCOMPACT_THRASHING, diff --git a/tests/test_autocompact_thrashing.py b/tests/test_autocompact_thrashing.py index f79bb0a..33a8a46 100644 --- a/tests/test_autocompact_thrashing.py +++ b/tests/test_autocompact_thrashing.py @@ -86,3 +86,16 @@ def test_other_api_errors_are_left_as_they_were(fixtures_dir): out = _build(entries) assert _stops(out) == [] assert all(s.status_code == "OK" for s in _llm(out)) + + +def test_a_long_stop_message_is_capped(fixtures_dir): + lines = (fixtures_dir / "autocompact_thrashing.jsonl").read_text().splitlines() + row = json.loads(lines[-1]) + row["message"]["content"][0]["text"] += " Please retry." * 200 + entries = [transcript.parse_line(json.dumps(r)) + for r in [json.loads(lines[0]), json.loads(lines[1]), row]] + [stop] = _stops(_build(entries)) + assert len(stop.status_message.encode("utf-8")) < 600 + assert stop.status_message.startswith(STOP_TEXT) + [(_, _, attrs)] = stop.events + assert attrs[semconv.EXCEPTION_MESSAGE] == stop.status_message From 0f8103724690fdcfdba22b4b1c02d688d7b04a0c Mon Sep 17 00:00:00 2001 From: Kiran Date: Tue, 27 Oct 2026 13:06:57 +0100 Subject: [PATCH 3/3] test: pin that the real thrashing stop message is kept whole (RIUS-1666) --- tests/test_autocompact_thrashing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_autocompact_thrashing.py b/tests/test_autocompact_thrashing.py index 33a8a46..5376717 100644 --- a/tests/test_autocompact_thrashing.py +++ b/tests/test_autocompact_thrashing.py @@ -50,6 +50,9 @@ def test_the_stop_is_an_error_span_with_its_time_and_text(fixtures_dir): assert attrs[semconv.EXCEPTION_TYPE] == "autocompact_thrashing" assert attrs[semconv.EXCEPTION_MESSAGE].startswith(STOP_TEXT) assert stop.status_message.startswith(STOP_TEXT) + # The real message is about 257 bytes: kept whole, not cut by a cap. + assert "…[truncated" not in attrs[semconv.EXCEPTION_MESSAGE] + assert "…[truncated" not in stop.status_message def test_the_stop_text_follows_the_capture_gate(fixtures_dir):