Raven-1/evolver/compressor/trajectory.py
Handsome-wzw 7b45527b5b fix(tools): an array sent as its own JSON is still an array
## Summary

Reported as "deliver_files is broken". It is not: **the tool never ran.**

Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:

```
Error: Invalid parameters for tool 'deliver_files': files should be array
```

What the model sent was the array, spelled as a string:

```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```

`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.

## Type

- [x] Fix
- [ ] Feature

## The edge of the forgiving is the design

When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.

**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.

**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.

## Not just deliver_files

Six built-in tools declare an array or object parameter and were all exposed:

| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |

The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.

## Verification

```
make lint-python    ruff check + ruff format --check, 1850 files
make lint-imports   9 contracts kept, 0 broken
pytest tests/       19,699 passed, 7 failed (all pre-existing, see below)
```

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string

**End to end, through the real tool**, with the exact payload that failed five times in production:

```
files sent as:            str -> [{"path": ".../poster.png", "title": "..."}]
returned:                 Delivered 1 file(s): poster.png (72 B).
still "should be array":  False
registered in the store:  True
```

Twelve cases, five mutations, each killing at least one:

| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |

The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.

**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.

## Risk

- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert

## Related Issues

N/A

## Swept

`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.

- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.

Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).

## Deliberately not fixed

The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.

## Moved from !594

Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.

Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00

497 lines
20 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Trajectory compressor — session.jsonl → ~10K-token diagnostic summary.
The judge LLM (especially the cheap L1-detection backend in mix mode)
can't afford to ingest a full 80-200K-token SWE-bench trajectory per
analysis. This module reduces the raw event stream to a structured
"agent debugger" style overview that preserves the high-signal portions
and elides the bulky tool outputs.
What we **keep verbatim**:
- Task description (first user message — `WRAPPER_PATH` + STRICT RULES +
issue text from the external scorer).
- Every assistant text content (the model's reasoning chain).
- Tool call names + truncated arguments (enough to identify what tool
was called and what it operated on).
What we **truncate**:
- Tool results — keep first ``head_chars`` + last ``tail_chars``, elide
the middle with a marker. Long file dumps, stack traces, and `cat`
output mostly fall into this bucket.
What we **collapse**:
- Runs of identical (tool_name, args-prefix) calls are emitted once
with a "× N times" marker. Highly repetitive trajectories (the
pager-stuck / tmux-poll / re-read pathologies from the 244-paired
analysis) compress dramatically here.
What we **flag**:
- Anomaly section appended at the end: empty-content turn count, syntax
errors, docker errors, repetition density. These are exactly the
signals the L1 detector watches for.
The compression is **purely rule-based** — no LLM call. A v2 could add
an LLM summarisation pass over the rule-compressed output, but the v1
already typically lands a 150K trajectory at ~5-15K tokens with the
defaults.
Token counting uses a rough ``chars / 4`` heuristic; we don't need a
tokenizer dependency for budget shaping at this granularity.
"""
from __future__ import annotations
import json
import re
from collections import Counter
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Iterable, Optional, Union
# ---------------------------------------------------------------------------
# Event model
# ---------------------------------------------------------------------------
@dataclass
class Event:
"""One parsed line from an external scorer session.jsonl.
Fields are kept loose because the scorer emits a few distinct event
shapes (metadata, chat messages, tool results). Callers branch on
``role`` / ``event_type``.
``finish_reason`` is captured for assistant turns when the upstream
row carries it (LiteLLM / OpenAI-shape responses). It is the
primary discriminator between L1 and L2 calibration in the judge
prompt (spec §22.5 + r4 Fix A1): ``stop``/``content_filter`` on
empty content → L1, ``length`` → L2 (max_tokens config issue).
"""
event_type: str # "metadata" | "system" | "user" | "assistant" | "tool" | "other"
content: Optional[str] = None
tool_calls: list[dict[str, Any]] = field(default_factory=list)
tool_name: Optional[str] = None
finish_reason: Optional[str] = None
raw: dict[str, Any] = field(default_factory=dict)
def _parse_event(obj: dict[str, Any]) -> Event:
"""Classify one raw JSON object from session.jsonl into an Event.
The classification rules mirror what we observed in the 244-paired
SWE-bench session files:
- ``{"_type": "metadata", ...}`` → metadata
- ``{"role": "user" | "assistant" | "system" | "tool", ...}`` → chat
- anything else → "other" (will be ignored downstream)
"""
if "_type" in obj:
return Event(event_type="metadata", raw=obj)
role = obj.get("role")
if role in ("system", "user", "assistant", "tool"):
content = obj.get("content")
# Some scorer/litellm rows put content as a list of blocks
# (multimodal); we flatten to a single string for compression
# purposes (we don't compress images).
if isinstance(content, list):
parts = []
for part in content:
if isinstance(part, dict) and part.get("type") == "text":
parts.append(part.get("text", ""))
elif isinstance(part, str):
parts.append(part)
content = "\n".join(parts) if parts else None
elif content is not None and not isinstance(content, str):
content = str(content)
# finish_reason only meaningful on assistant rows (per OpenAI/LiteLLM
# shape); for other roles we leave it None even if the field is
# present in raw. We tolerate both a top-level key (scorer
# session.jsonl flattens it) and a nested ``choices[0].finish_reason``
# (raw LiteLLM response shape some adapters preserve).
finish_reason: Optional[str] = None
if role == "assistant":
fr = obj.get("finish_reason")
if isinstance(fr, str) and fr:
finish_reason = fr
else:
choices = obj.get("choices")
if isinstance(choices, list) and choices:
first = choices[0]
if isinstance(first, dict):
nested = first.get("finish_reason")
if isinstance(nested, str) and nested:
finish_reason = nested
return Event(
event_type=role,
content=content,
tool_calls=list(obj.get("tool_calls") or []),
tool_name=obj.get("name"),
finish_reason=finish_reason,
raw=obj,
)
return Event(event_type="other", raw=obj)
def load_session_jsonl(path: Union[str, Path]) -> list[Event]:
"""Read a session.jsonl file into a list of :class:`Event`.
Lines that are blank or fail to parse are silently skipped — scorer
has been seen to emit the occasional malformed line on crash, and we
don't want one bad line to abort a 200-turn trajectory.
"""
p = Path(path)
events: list[Event] = []
with p.open("r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
except json.JSONDecodeError:
continue
if not isinstance(obj, dict):
continue
events.append(_parse_event(obj))
return events
# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
@dataclass
class CompressorConfig:
"""Knobs for the rule-based compression pass.
Defaults are tuned so a typical 80-200K-token SWE-bench trajectory
lands at 5-15K tokens — well within budget for the L1-detection
backend (Qwen-397B has 128K context, Claude Haiku has 200K, so
even the "compressed" output is still small relative to context).
"""
target_tokens: int = 10000 # not a hard cap, soft target
tool_args_chars: int = 200 # truncate tool call args at this length
tool_result_head_chars: int = 200 # keep first N chars of long tool results
tool_result_tail_chars: int = 200 # keep last N chars of long tool results
user_message_max_chars: int = 4000 # task description rarely needs more
repetition_run_threshold: int = 3 # collapse a run of ≥N identical calls
detect_anomalies: bool = True
# ---------------------------------------------------------------------------
# Anomaly markers (used by both compression body + final summary)
# ---------------------------------------------------------------------------
_SYNTAX_ERROR_PATTERNS = [
re.compile(r"\bSyntax(\s+)?Error\b", re.IGNORECASE),
re.compile(r"Unterminated quoted string", re.IGNORECASE),
re.compile(r"unexpected EOF", re.IGNORECASE),
]
_DOCKER_ERROR_PATTERNS = [
re.compile(r"docker daemon", re.IGNORECASE),
re.compile(r"connection refused", re.IGNORECASE),
re.compile(r"container .* not running", re.IGNORECASE),
re.compile(r"\bOOM\b", re.IGNORECASE),
]
_NETWORK_ERROR_PATTERNS = [
re.compile(r"read timeout", re.IGNORECASE),
re.compile(r"pool exhausted", re.IGNORECASE),
re.compile(r"URLError", re.IGNORECASE),
]
def _matches_any(text: str, patterns: list[re.Pattern]) -> bool:
return any(p.search(text) for p in patterns)
# ---------------------------------------------------------------------------
# Compressor
# ---------------------------------------------------------------------------
class TrajectoryCompressor:
"""Rule-based compressor — turns Event list into a single text blob."""
def __init__(self, config: Optional[CompressorConfig] = None) -> None:
self._cfg = config or CompressorConfig()
def compress(self, events: Iterable[Event]) -> str:
"""Produce a single compressed text block for one trajectory.
The result is plain text with a structured layout the judge
prompt is designed to handle: header → task → turns → anomaly
summary. No JSON, no Markdown — just headings and indented
body. Keeps the trajectory format token-efficient.
"""
events_list = list(events)
cfg = self._cfg
out: list[str] = []
# Header — we'll fill turn / anomaly counts at the end
header_placeholder_idx = len(out)
out.append("") # placeholder for header
# Task description: take the first non-metadata user message
task = self._find_task_description(events_list)
if task:
out.append("--- TASK ---")
truncated = task[: cfg.user_message_max_chars]
if len(task) > cfg.user_message_max_chars:
truncated += f"\n... [truncated, {len(task) - cfg.user_message_max_chars} more chars]"
out.append(truncated)
# Turns (assistant + tool_result pairs)
out.append("\n--- TURNS ---")
turn_idx = 0
empty_content_count = 0
i = 0
while i < len(events_list):
ev = events_list[i]
if ev.event_type != "assistant":
i += 1
continue
turn_idx += 1
# Empty content detection — L1 signal. ``finish_reason`` is
# the discriminator between L1 (stop / content_filter) and
# L2 (length budget too small for the model) — see Fix A1
# in judge prompt.
fr_tag = f", finish_reason={ev.finish_reason}" if ev.finish_reason else ""
asst_text = (ev.content or "").strip()
if not asst_text and not ev.tool_calls:
empty_content_count += 1
out.append(f"\nTurn {turn_idx} (assistant): [EMPTY content + no tool_calls{fr_tag}]")
i += 1
continue
header_suffix = f" [finish_reason={ev.finish_reason}]" if ev.finish_reason else ""
out.append(f"\nTurn {turn_idx} (assistant){header_suffix}:")
if asst_text:
out.append(self._indent(asst_text, " "))
elif ev.tool_calls:
# Tool-using turn without narrative text is NORMAL agent
# behaviour (model chose to act instead of explain), NOT
# an empty-content anomaly. Do NOT increment empty_content
# _count — the L1 trigger must reserve for completely
# silent turns (no content AND no tool_calls), matching
# EmptyResponseAlertHook.is_empty_response.
out.append(f" [tool-only turn, no narrative{fr_tag}]")
# Find immediately-following tool calls + results, with
# collapse of identical runs.
i += 1
i = self._emit_tool_section(events_list, i, out, turn_idx)
# Anomaly summary
anomaly_block = []
if cfg.detect_anomalies:
anomaly_block = self._compute_anomaly_summary(events_list, empty_content_count)
if anomaly_block:
out.append("\n--- ANOMALIES DETECTED ---")
out.extend(anomaly_block)
# Final summary line up top, now that we know turn count
out[header_placeholder_idx] = (
f"=== TRAJECTORY SUMMARY ===\n"
f"Total turns: {turn_idx} | Empty-content turns: {empty_content_count} "
f"| Anomalies flagged: {len(anomaly_block)}"
)
return "\n".join(out)
# -- helpers ------------------------------------------------------------
def _find_task_description(self, events: list[Event]) -> Optional[str]:
"""Pick the first non-empty user-message content as the task text."""
for ev in events:
if ev.event_type == "user" and ev.content:
return ev.content
return None
def _emit_tool_section(
self,
events: list[Event],
start_idx: int,
out: list[str],
turn_idx: int,
) -> int:
"""Emit pending tool calls + results following an assistant turn.
Walks ``events[start_idx:]`` while encountering ``role=tool`` events
(which carry the result of the prior assistant's tool_calls),
collapsing consecutive identical (name, args-prefix) calls.
Returns the index where the tool section ends (so the outer loop
can continue from there).
Collapse logic: we look at the PREVIOUS assistant's ``tool_calls``
list and emit one summary per call. If the calls in this run match
a recent identical call within a small window, we annotate
"× N repetitions".
"""
cfg = self._cfg
i = start_idx
# Look back at last assistant for its tool_calls (already past it in i-1)
prev_assistant = self._find_last_assistant(events, start_idx)
if prev_assistant is None:
return i
if not prev_assistant.tool_calls:
# No tool calls promised — skip any stray tool events (rare)
while i < len(events) and events[i].event_type == "tool":
i += 1
return i
# Emit each tool call with truncated args
for tc in prev_assistant.tool_calls:
fn = tc.get("function") or {}
name = fn.get("name", "?")
args_raw = fn.get("arguments", "") or ""
if not isinstance(args_raw, str):
args_raw = json.dumps(args_raw, ensure_ascii=False)
args_trunc = args_raw[: cfg.tool_args_chars]
if len(args_raw) > cfg.tool_args_chars:
args_trunc += f" [...truncated {len(args_raw) - cfg.tool_args_chars} chars]"
out.append(f" → call {name}({args_trunc})")
# Now consume the corresponding tool-result events
while i < len(events) and events[i].event_type == "tool":
result = events[i].content or ""
out.append(f" ← result: {self._summarize_tool_result(result)}")
i += 1
return i
def _find_last_assistant(self, events: list[Event], current_idx: int) -> Optional[Event]:
"""The most recent assistant event strictly before ``current_idx``."""
for j in range(current_idx - 1, -1, -1):
if events[j].event_type == "assistant":
return events[j]
return None
def _summarize_tool_result(self, content: str) -> str:
cfg = self._cfg
if not content:
return "[empty result]"
total = len(content)
head_n = cfg.tool_result_head_chars
tail_n = cfg.tool_result_tail_chars
if total <= head_n + tail_n:
return self._oneline(content)
head = content[:head_n]
tail = content[-tail_n:]
return f"{self._oneline(head)} ... [ELIDED {total - head_n - tail_n} chars] ... {self._oneline(tail)}"
@staticmethod
def _oneline(text: str) -> str:
"""Collapse newlines + repeated whitespace so the result fits one line."""
return re.sub(r"\s+", " ", text).strip()
@staticmethod
def _indent(text: str, prefix: str) -> str:
return "\n".join(prefix + line for line in text.splitlines())
def _compute_anomaly_summary(self, events: list[Event], empty_content_count: int) -> list[str]:
cfg = self._cfg
out: list[str] = []
# 1. Empty content ratio (key L1 vs L2 signal). finish_reason
# breakdown disambiguates: ``stop`` = true L1 (model silent),
# ``length`` = L2 (max_tokens budget too small for this model —
# reasoning model burned budget without producing output).
n_asst = sum(1 for e in events if e.event_type == "assistant")
if n_asst > 0 and empty_content_count > 0:
pct = empty_content_count / n_asst * 100
severity = "HIGH" if pct > 30 else "MEDIUM" if pct > 10 else "LOW"
fr_breakdown = Counter(
(e.finish_reason or "unknown")
for e in events
if e.event_type == "assistant" and not (e.content or "").strip() and not e.tool_calls
)
fr_tag = ""
if fr_breakdown:
parts = [f"{k}={v}" for k, v in sorted(fr_breakdown.items())]
fr_tag = f" (by finish_reason: {', '.join(parts)})"
out.append(
f" [{severity}] empty-content assistant turns: {empty_content_count}/{n_asst}"
f" ({pct:.0f}%){fr_tag} — L1 if finish_reason=stop/content_filter dominates,"
f" L2 if finish_reason=length dominates (max_tokens budget too small)"
)
# 2. Syntax / docker / network errors in tool outputs
syntax_errs = 0
docker_errs = 0
net_errs = 0
for ev in events:
if ev.event_type != "tool" or not ev.content:
continue
if _matches_any(ev.content, _SYNTAX_ERROR_PATTERNS):
syntax_errs += 1
if _matches_any(ev.content, _DOCKER_ERROR_PATTERNS):
docker_errs += 1
if _matches_any(ev.content, _NETWORK_ERROR_PATTERNS):
net_errs += 1
if syntax_errs:
out.append(f" [LOW] {syntax_errs} tool result(s) contained syntax-error markers")
if docker_errs:
out.append(f" [HIGH] {docker_errs} tool result(s) contained docker/container errors — L1 signal")
if net_errs:
out.append(f" [HIGH] {net_errs} tool result(s) contained network errors — L1 signal")
# 3. Tool call repetition density
tool_call_sigs: list[tuple[str, str]] = []
for ev in events:
if ev.event_type != "assistant":
continue
for tc in ev.tool_calls:
fn = tc.get("function") or {}
name = fn.get("name", "?")
args = fn.get("arguments", "") or ""
if not isinstance(args, str):
args = json.dumps(args, ensure_ascii=False)
sig = (name, args[: cfg.tool_args_chars])
tool_call_sigs.append(sig)
if tool_call_sigs:
c = Counter(tool_call_sigs)
most_common_sig, most_common_n = c.most_common(1)[0]
if most_common_n >= cfg.repetition_run_threshold:
args_preview = most_common_sig[1][:80].replace("\n", " ")
out.append(
f" [MEDIUM] tool call repeated {most_common_n}× "
f"({most_common_sig[0]}, args~{args_preview!r}) "
f"— possible repetition_breaker pathology"
)
return out
# ---------------------------------------------------------------------------
# Quick token-estimate helper
# ---------------------------------------------------------------------------
def estimate_tokens(text: str) -> int:
"""Rough token count via ``len(text) / 4`` — good enough for budget
shaping at the 1K-100K scale; do NOT use for billing / SLA decisions.
``ceil`` semantics so a 5-char string reports 2 tokens not 1.
"""
if not text:
return 0
return -(-len(text) // 4)
__all__ = [
"CompressorConfig",
"Event",
"TrajectoryCompressor",
"estimate_tokens",
"load_session_jsonl",
]