The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(evals): fast-jev-compaction arm + compaction cost metering in the compaction eval
teknium1139047ae2a84
diff --git a/evals/compaction/README.md b/evals/compaction/README.mdindex 476fd8296f5..89a4703c1d6 100644--- a/evals/compaction/README.md+++ b/evals/compaction/README.md@@ -69,6 +69,25 @@ kwargs plus optional attribute overrides applied post-construction (e.g.`tail_token_budget`). Add new policies there — the runner picks them up byname.+A policy with `"engine": "jev"` bypasses `ContextCompressor` and runs+`jev_arm.py`, a Python port of+[fast-jev-compaction](https://github.com/tamaratran/fast-jev-compaction): no+summary at all — TypeSafe's Jev decision model scores every tool call/result+(`noul` keep probabilities over the whole history) and stale ones are dropped+or truncated while user/assistant text stays verbatim. Transport is+OpenRouter's Decisions API (`~typesafe/jev-latest`, needs+`OPENROUTER_API_KEY`); `"jev": {...}` overrides `JevOptions` (threshold,+pinned tail, state/request ceilings). When the fitted state cannot get under+the 25K-token ceiling the arm records `jev_fallback` (the plugin throws and+Claude Code falls back to its built-in summary) instead of scoring.++Every arm's result carries its compaction spend: `compaction_calls`,+`compaction_input_tokens` / `compaction_output_tokens`, `compaction_model`+and `compaction_cost_usd` (Jev reports cost directly; summary calls are+priced at the OpenRouter list price of the model that answered). The run+also writes `eval_usage.json` — the harness's own question/answer/judge+token bill.+## Notes- Question generation and judging use `agent.auxiliary_client.call_llm`diff --git a/evals/compaction/jev_arm.py b/evals/compaction/jev_arm.pynew file mode 100644index 00000000000..569645ca578--- /dev/null+++ b/evals/compaction/jev_arm.py@@ -0,0 +1,521 @@+"""fast-jev-compaction as a compaction eval arm.++Python port of https://github.com/tamaratran/fast-jev-compaction (MIT), which+replaces the compaction summary with per-tool-call keep/drop decisions from+TypeSafe's Jev "System One" model: the whole history (tool results replaced by+a size note) is sent as `state`, two `noul` questions per candidate call ask+whether the call and whether its verbatim result must stay, and the transcript+is rebuilt with nothing rewritten. Text messages are never touched.++Differences from the TypeScript original are format-only: Hermes transcripts+carry `tool_calls` on assistant rows and one `role: tool` row per result, so a+"message" here is one chat row and `preserve_recent_messages` counts rows.++Transport is OpenRouter's Decisions API (`POST /api/alpha/decisions`, model+`~typesafe/jev-latest`), so the eval needs only OPENROUTER_API_KEY.+"""+from __future__ import annotations++import copy+import json+import math+import os+import re+import time+from concurrent.futures import ThreadPoolExecutor+from dataclasses import dataclass, field+from typing import Any, Callable, Dict, List, Optional++STATE_CONTEXT = (+ "A coding assistant conversation is being compacted to free context. `history` is the "+ "whole conversation so far, oldest first; tool outputs are replaced by a short `result` "+ "note and long texts may be abridged. Each question asks whether one tool call, or the "+ "full output of that call, still needs to stay in the history verbatim. Whatever is not "+ "kept is deleted permanently, but the assistant can always re-run a tool or re-read a file."+)+INPUT_CHARS = (1000, 200, 60)+TEXT_HEAD = 400+TEXT_TAIL = 150+REQUEST_OVERHEAD_TOKENS = 20+OPENROUTER_DECISIONS_URL = "https://openrouter.ai/api/alpha/decisions"+OPENROUTER_JEV_MODEL = "~typesafe/jev-latest"++_TOKEN_PIECES = re.compile(r"[A-Za-z]+|\d+|[^\sA-Za-z\d]")+_WS = re.compile(r"\s+")+++def estimate_tokens(text: str) -> int:+ """Port of the plugin's tokenizer-free estimate (letters/6, digits/2, symbols 0.9)."""+ tokens = 0.0+ for piece in _TOKEN_PIECES.findall(text):+ c = piece[0]+ if c.isdigit():+ tokens += len(piece) / 2+ elif c.isascii() and c.isalpha():+ tokens += 1 + (len(piece) - 1) // 6+ else:+ tokens += 0.9+ return math.ceil(tokens)+++def truncate(text: str, limit: int) -> str:+ return text if len(text) <= limit else text[: max(0, limit - 1)] + "…"+++def abridge(text: str, head: int, tail: int) -> str:+ if len(text) <= head + tail + 40:+ return text+ return f"{text[:head]}\n[… {len(text) - head - tail} chars omitted …]\n{text[-tail:]}"+++def _json(value: Any) -> str:+ return json.dumps(value, ensure_ascii=False, separators=(",", ":"), default=str)+++def message_text(m: Dict[str, Any]) -> str:+ c = m.get("content")+ if isinstance(c, str):+ return c+ if isinstance(c, list):+ return "\n".join(p.get("text", "") for p in c if isinstance(p, dict) and p.get("type") == "text")+ return ""+++@dataclass+class ToolCall:+ id: str+ tool_call_id: str+ tool: str+ input: Any+ call_index: int+ result_index: int+ result_chars: int+ is_error: bool+ pinned: bool+++@dataclass+class Decision:+ id: str+ tool: str+ keep_call: float+ keep_result: float+ action: str # keep | drop_result | drop_call+ reason: str # pinned | kept | result_dropped | call_dropped+++@dataclass+class JevOptions:+ goal: str = ""+ keep_threshold: float = 0.5+ preserve_recent_messages: int = 6+ max_state_tokens: int = 25_000+ max_request_tokens: int = 30_000+ truncate_head_chars: int = 300+++@dataclass+class JevUsage:+ requests: int = 0+ input_tokens: int = 0+ output_tokens: int = 0+ cost_usd: float = 0.0+ models: List[str] = field(default_factory=list)+++def is_pinned(index: int, total: int, preserve: int) -> bool:+ return index == 0 or index >= total - preserve+++def _parse_arguments(fn: Dict[str, Any]) -> Any:+ args = fn.get("arguments")+ if isinstance(args, str):+ try:+ return json.loads(args)+ except Exception:+ return {"arguments": args}+ return args if args is not None else {}+++def collect_tool_calls(messages: List[Dict[str, Any]], preserve: int) -> List[ToolCall]:+ results: Dict[str, int] = {}+ for idx, m in enumerate(messages):+ if m.get("role") == "tool" and m.get("tool_call_id"):+ results[m["tool_call_id"]] = idx+ calls: List[ToolCall] = []+ total = len(messages)+ for idx, m in enumerate(messages):+ for tc in m.get("tool_calls") or []:+ tcid = tc.get("id")+ if tcid not in results:+ continue+ ridx = results[tcid]+ fn = tc.get("function") or {}+ rtext = message_text(messages[ridx])+ calls.append(ToolCall(+ id=f"t{len(calls) + 1}",+ tool_call_id=tcid,+ tool=fn.get("name") or tc.get("name") or "tool",+ input=_parse_arguments(fn),+ call_index=idx,+ result_index=ridx,+ result_chars=len(rtext),+ is_error=bool(re.match(r"\s*(\{\"error\"|Error\b|error:)", rtext[:40], re.I)),+ pinned=is_pinned(idx, total, preserve) or is_pinned(ridx, total, preserve),+ ))+ return calls+++def _input_text(inp: Any, limit: int) -> str:+ return truncate(_json(inp), limit)+++def _result_note(call: ToolCall) -> str:+ return f"{'error' if call.is_error else 'ok'}, {call.result_chars} chars (omitted)"+++def _compact_call(call: ToolCall) -> str:+ if isinstance(call.input, dict):+ parts = []+ for k, v in call.input.items():+ text = v if isinstance(v, str) else _json({k: v})[:200]+ flat = _WS.sub(" ", text)+ parts.append(f"{k}={flat}")+ inp = " ".join(parts)+ else:+ inp = _json(call.input)+ return f"{call.id} {call.tool} {truncate(inp, INPUT_CHARS[2])} → {'error' if call.is_error else 'ok'} {call.result_chars}ch"+++def _history_entries(messages, calls: List[ToolCall], input_chars: int) -> List[Dict[str, Any]]:+ by_msg: Dict[int, List[ToolCall]] = {}+ for c in calls:+ by_msg.setdefault(c.call_index, []).append(c)+ entries = []+ for i, m in enumerate(messages):+ tcs = [{"id": c.id, "tool": c.tool, "input": _input_text(c.input, input_chars), "result": _result_note(c)}+ for c in by_msg.get(i, [])]+ text = message_text(m)+ if m.get("role") == "tool":+ text = "" # results are represented by the call's note, never verbatim+ if not text.strip() and not tcs:+ continue+ entry: Dict[str, Any] = {"i": i, "role": m.get("role"), "text": text}+ if tcs:+ entry["tool_calls"] = tcs+ entries.append(entry)+ return entries+++def goal_from_messages(messages) -> str:+ prompts = [message_text(m) for m in messages if m.get("role") == "user" and message_text(m).strip()]+ return "\n".join(truncate(p, 500) for p in prompts[-3:])+++def fit_state(messages, calls: List[ToolCall], opt: JevOptions) -> Dict[str, Any]:+ """Port of fitState: shrink the whole-history state in stages until it fits."""+ goal = opt.goal or goal_from_messages(messages)+ total = len(messages)++ def state_of(history):+ return {"context": STATE_CONTEXT, "goal": goal, "history": history}++ def entry_tokens(e):+ return estimate_tokens(_json(e)) + 1++ base = estimate_tokens(_json(state_of([])))+ history: List[Dict[str, Any]] = []+ per: List[int] = []+ tokens = 0++ def rebuild(limit):+ nonlocal history, per, tokens+ history = _history_entries(messages, calls, limit)+ per = [entry_tokens(e) for e in history]+ tokens = base + sum(per)++ def fits():+ return tokens <= opt.max_state_tokens++ def fitted(h, stage):+ return {"state": state_of(h), "tokens": tokens, "stage": stage}++ def shrink(index, change):+ nonlocal tokens+ change(history[index])+ now = entry_tokens(history[index])+ tokens += now - per[index]+ per[index] = now++ rebuild(INPUT_CHARS[0])+ if fits():+ return fitted(history, "full")+ for limit in INPUT_CHARS[1:]:+ rebuild(limit)+ if fits():+ return fitted(history, f"inputs<={limit}")++ def pinned(e):+ return is_pinned(e["i"], total, opt.preserve_recent_messages)++ idx = list(range(len(history)))+ order = [i for i in idx if not pinned(history[i])] + [i for i in idx if pinned(history[i])]++ for i in order:+ if len(history[i]["text"]) <= TEXT_HEAD + TEXT_TAIL + 40:+ continue+ shrink(i, lambda e: e.__setitem__("text", abridge(e["text"], TEXT_HEAD, TEXT_TAIL)))+ if fits():+ return fitted(history, "texts abridged")++ for i in order:+ e = history[i]+ if pinned(e) or not e["text"]:+ continue+ original = len(message_text(messages[e["i"]]))+ shrink(i, lambda e, n=original: e.__setitem__("text", f"[… {n} chars omitted …]"))+ if fits():+ return fitted(history, "old messages collapsed")++ by_msg: Dict[int, List[ToolCall]] = {}+ for c in calls:+ by_msg.setdefault(c.call_index, []).append(c)+ for i in order:+ e = history[i]+ own = by_msg.get(e["i"])+ if pinned(e) or not own:+ continue+ shrink(i, lambda e, own=own: e.__setitem__("tool_calls", [_compact_call(c) for c in own]))+ if fits():+ return fitted(history, "old calls compacted")++ left = set()+ for i in order:+ e = history[i]+ if pinned(e) or e.get("tool_calls"):+ continue+ left.add(i)+ tokens -= per[i]+ if fits():+ return fitted([h for j, h in enumerate(history) if j not in left], "old messages left out")++ remaining = [h for j, h in enumerate(history) if j not in left]+ merged: List[Dict[str, Any]] = []++ def foldable(e):+ return not pinned(e) and not e["text"] and isinstance((e.get("tool_calls") or [None])[0], str)++ for e in remaining:+ prev = merged[-1] if merged else None+ if prev and foldable(prev) and foldable(e) and prev["role"] == e["role"]:+ prev["tool_calls"] = list(prev["tool_calls"]) + list(e["tool_calls"])+ continue+ merged.append(dict(e))+ history = merged+ per = [entry_tokens(e) for e in history]+ tokens = base + sum(per)+ if fits():+ return fitted(history, "old calls merged")+ raise ValueError(f"history too large for Jev (~{tokens} tokens after truncation, limit {opt.max_state_tokens})")+++def questions_for(call: ToolCall) -> Dict[str, Any]:+ return {+ f"call_{call.id}": {+ "type": "noul",+ "instructions": (+ f"Tool call {call.id} ({call.tool}) should stay in the history: knowing this call "+ "was made, with its input, still matters for what the assistant does next"),+ },+ f"result_{call.id}": {+ "type": "noul",+ "instructions": (+ f"The full output of tool call {call.id} ({call.tool}, {call.result_chars} chars) should "+ "stay in the history verbatim: the assistant still needs its contents and re-running "+ "the tool would not do"),+ },+ }+++def batch_calls(calls: List[ToolCall], state_tokens: int, opt: JevOptions) -> List[List[ToolCall]]:+ budget = opt.max_request_tokens - state_tokens - REQUEST_OVERHEAD_TOKENS+ batches: List[List[ToolCall]] = []+ cur: List[ToolCall] = []+ cur_tokens = 0+ for c in calls:+ t = estimate_tokens(_json(questions_for(c)))+ if cur and cur_tokens + t > budget:+ batches.append(cur)+ cur, cur_tokens = [], 0+ if not cur and t > budget:+ raise ValueError(f"state leaves no room for questions (~{state_tokens} of {opt.max_request_tokens} tokens)")+ cur.append(c)+ cur_tokens += t+ if cur:+ batches.append(cur)+ return batches+++def decide_call(call: ToolCall, keep_call: float, keep_result: float, opt: JevOptions) -> Decision:+ base = dict(id=call.id, tool=call.tool, keep_call=keep_call, keep_result=keep_result)+ if call.pinned:+ return Decision(**base, action="keep", reason="pinned")+ if keep_result >= opt.keep_threshold:+ return Decision(**base, action="keep", reason="kept")+ if keep_call >= opt.keep_threshold:+ return Decision(**base, action="drop_result", reason="result_dropped")+ return Decision(**base, action="drop_call", reason="call_dropped")+++def _truncated_result(text: str, is_error: bool, head: int) -> str:+ if len(text) <= head + 120:+ return text+ lead = f"{text[:head]}\n" if head > 0 else ""+ return (f"{lead}[fast-jev-compaction truncated {len(text) - head} chars of this tool result"+ f"{' (error)' if is_error else ''}; re-run the tool if needed]")+++def apply_decisions(messages, decisions: List[Decision], calls: List[ToolCall], head: int):+ """Rebuild the Hermes transcript: dropped calls vanish with their result row,+ dropped results keep a bounded head + note, untouched rows are the same objects."""+ by_id = {c.id: c for c in calls}+ actions: Dict[str, str] = {}+ for d in decisions:+ c = by_id.get(d.id)+ if c and d.action != "keep":+ actions[c.tool_call_id] = d.action+ errors = {c.tool_call_id: c.is_error for c in calls}+ kept = []+ for m in messages:+ if m.get("role") == "tool":+ act = actions.get(m.get("tool_call_id"))+ if act == "drop_call":+ continue+ if act == "drop_result":+ nm = dict(m)+ nm["content"] = _truncated_result(message_text(m), errors.get(m.get("tool_call_id"), False), head)+ kept.append(nm)+ continue+ kept.append(m)+ continue+ tcs = m.get("tool_calls") or []+ if not any(actions.get(tc.get("id")) == "drop_call" for tc in tcs):+ kept.append(m)+ continue+ remaining = [tc for tc in tcs if actions.get(tc.get("id")) != "drop_call"]+ if not remaining and not message_text(m).strip():+ continue+ nm = dict(m)+ if remaining:+ nm["tool_calls"] = remaining+ else:+ nm.pop("tool_calls", None)+ kept.append(nm)+ return kept+++def openrouter_asker(api_key: Optional[str] = None, model: str = OPENROUTER_JEV_MODEL,+ url: str = OPENROUTER_DECISIONS_URL, timeout: float = 120.0) -> Callable:+ """`ask(state, questions) -> response dict` over OpenRouter's Decisions API."""+ import urllib.request++ key = api_key or os.environ.get("OPENROUTER_API_KEY") or ""+ if not key:+ raise RuntimeError("OPENROUTER_API_KEY is not configured")++ def ask(state, questions):+ body = json.dumps({"model": model, "state": state, "questions": questions}).encode()+ req = urllib.request.Request(url, data=body, headers={+ "Authorization": f"Bearer {key}", "Content-Type": "application/json",+ "HTTP-Referer": "https://github.com/NousResearch/hermes-agent", "X-Title": "hermes compaction eval",+ })+ try:+ with urllib.request.urlopen(req, timeout=timeout) as r:+ return json.loads(r.read().decode())+ except urllib.error.HTTPError as e: # pragma: no cover - network+ raise RuntimeError(f"Jev request failed ({e.code}): {e.read().decode()[:200]}") from e++ return ask+++class JevCompactor:+ """Eval-arm compressor with the ContextCompressor.compress() call shape."""++ def __init__(self, asker: Optional[Callable] = None, options: Optional[JevOptions] = None,+ concurrency: int = 4):+ self.asker = asker or openrouter_asker()+ self.opt = options or JevOptions()+ self.concurrency = concurrency+ self.usage = JevUsage()+ self.stats: Dict[str, Any] = {}+ self.decisions: List[Decision] = []+ self._last_summary_error: Optional[str] = None++ def _ask_batch(self, state, batch: List[ToolCall]) -> Dict[str, Dict[str, float]]:+ questions: Dict[str, Any] = {}+ for c in batch:+ questions.update(questions_for(c))+ resp = self.asker(state, questions)+ answers = resp.get("answers") if isinstance(resp, dict) else None+ if not isinstance(answers, dict):+ raise ValueError("Jev response is missing answers")+ usage = resp.get("usage") or {}+ self.usage.requests += 1+ self.usage.input_tokens += int(usage.get("input_tokens") or 0)+ self.usage.output_tokens += int(usage.get("output_tokens") or 0)+ self.usage.cost_usd += float(usage.get("cost") or 0.0)+ if resp.get("model") and resp["model"] not in self.usage.models:+ self.usage.models.append(resp["model"])++ def noul(name):+ a = answers.get(name)+ if not isinstance(a, dict) or not isinstance(a.get("noul"), (int, float)):+ raise ValueError(f"Invalid Jev answer for {name}")+ return float(a["noul"])++ return {c.id: {"keep_call": noul(f"call_{c.id}"), "keep_result": noul(f"result_{c.id}")} for c in batch}++ def compress(self, messages: List[Dict[str, Any]], current_tokens: int = 0, force: bool = True):+ t0 = time.time()+ opt = self.opt+ calls = collect_tool_calls(messages, opt.preserve_recent_messages)+ candidates = [c for c in calls if not c.pinned]+ answers: Dict[str, Dict[str, float]] = {}+ fitted = {"tokens": 0, "stage": ""}+ batches: List[List[ToolCall]] = []+ if candidates:+ fitted = fit_state(messages, calls, opt)+ batches = batch_calls(candidates, fitted["tokens"], opt)+ with ThreadPoolExecutor(max_workers=self.concurrency) as pool:+ for result in pool.map(lambda b: self._ask_batch(fitted["state"], b), batches):+ answers.update(result)+ self.decisions = [+ decide_call(c, *(answers.get(c.id, {}).get(k, 1.0) for k in ("keep_call", "keep_result")), opt)+ for c in calls+ ]+ kept = apply_decisions(messages, self.decisions, calls, opt.truncate_head_chars)+ reasons = [d.reason for d in self.decisions]+ self.stats = {+ "messages_before": len(messages), "messages_after": len(kept),+ "calls": len(calls), "kept": reasons.count("kept"),+ "results_dropped": reasons.count("result_dropped"),+ "calls_dropped": reasons.count("call_dropped"), "pinned": reasons.count("pinned"),+ "state_tokens": fitted["tokens"], "state_stage": fitted["stage"],+ "requests": len(batches), "seconds": round(time.time() - t0, 1),+ }+ return kept+++def fake_asker(keep_call: float = 0.2, keep_result: float = 0.1) -> Callable:+ """Deterministic asker for offline tests: every candidate gets the same answer."""+ def ask(state, questions):+ return {"model": "fake-jev", "answers": {n: {"type": "noul", "noul": keep_result if n.startswith("result_") else keep_call}+ for n in questions},+ "usage": {"input_tokens": estimate_tokens(_json(state)), "output_tokens": len(questions), "cost": 0.0}}+ return ask+++__all__ = [+ "JevCompactor", "JevOptions", "JevUsage", "apply_decisions", "batch_calls", "collect_tool_calls",+ "decide_call", "estimate_tokens", "fake_asker", "fit_state", "openrouter_asker", "questions_for",+]diff --git a/evals/compaction/policies.py b/evals/compaction/policies.pyindex f51bace9f34..e68a04c520e 100644--- a/evals/compaction/policies.py+++ b/evals/compaction/policies.py@@ -45,6 +45,26 @@ POLICIES: Dict[str, Dict[str, Any]] = {"ctor": {"tail_mode": "lean"},"attrs": {"_session_id": "eval-session"},},+ # fast-jev-compaction (evals/compaction/jev_arm.py): no summary at all —+ # Jev scores every tool call/result and stale ones are dropped or+ # truncated; user/assistant text stays verbatim. Plugin defaults.+ "jev": {+ "engine": "jev",+ "jev": {},+ },+ # Same, with the pinned tail widened from the plugin's 6 rows to roughly+ # lean's 25K-token tail so the two arms protect comparable recent context.+ "jev_tail40": {+ "engine": "jev",+ "jev": {"preserve_recent_messages": 40},+ },+ # Threshold lowered to ~the median keep_result Jev assigns on Hermes+ # transcripts (0.15): tests whether its ranking carries signal below the+ # plugin's 0.5 calibration point, where it drops every candidate.+ "jev_t15": {+ "engine": "jev",+ "jev": {"keep_threshold": 0.15},+ },}diff --git a/evals/compaction/runner.py b/evals/compaction/runner.pyindex 21cd27f3f72..fce9cc384e9 100644--- a/evals/compaction/runner.py+++ b/evals/compaction/runner.py@@ -153,6 +153,9 @@ def keyword_search(archive: list, query: str, top_k: int = 4, excerpt_chars: intreturn "\n\n".join(hits) if hits else "(no results)"+EVAL_USAGE = {"calls": 0, "prompt_tokens": 0, "completion_tokens": 0, "cached_tokens": 0}++def _call(prompt: str, max_tokens: int = 2000) -> str:from agent.auxiliary_client import call_llm@@ -161,6 +164,13 @@ def _call(prompt: str, max_tokens: int = 2000) -> str:task="compression",max_tokens=max_tokens,)+ usage = getattr(resp, "usage", None)+ if usage is not None:+ EVAL_USAGE["calls"] += 1+ EVAL_USAGE["prompt_tokens"] += int(getattr(usage, "prompt_tokens", 0) or 0)+ EVAL_USAGE["completion_tokens"] += int(getattr(usage, "completion_tokens", 0) or 0)+ details = getattr(usage, "prompt_tokens_details", None)+ EVAL_USAGE["cached_tokens"] += int(getattr(details, "cached_tokens", 0) or 0) if details else 0if hasattr(resp, "choices"):return resp.choices[0].message.content or ""return str(resp)@@ -216,17 +226,122 @@ def generate_questions(messages, n: int, cache_path: Path) -> list:return questions-def run_policy(name: str, spec: dict, messages, questions, out_dir: Path,- with_recovery: bool = False) -> dict:+_PRICES: dict = {}+++def openrouter_price_usd(model: str, input_tokens: int, output_tokens: int):+ """Price a call from OpenRouter's public catalog (per-token USD); None when unknown.++ Used as a common yardstick across arms — a summary routed through another+ provider is priced at the OpenRouter list price for that model id.+ """+ if not _PRICES:+ try:+ import urllib.request+ with urllib.request.urlopen("https://openrouter.ai/api/v1/models", timeout=30) as r:+ for m in json.load(r)["data"]:+ _PRICES[m["id"]] = m.get("pricing") or {}+ except Exception:+ _PRICES["__failed__"] = {}+ p = _PRICES.get(model) or _PRICES.get(model.split(":")[0])+ if not p:+ return None+ return input_tokens * float(p.get("prompt") or 0) + output_tokens * float(p.get("completion") or 0)+++class _AuxMeter:+ """Wraps the compressor's module-level ``call_llm`` binding to total summary usage."""++ def __init__(self):+ self.calls = 0+ self.input_tokens = 0+ self.output_tokens = 0+ self.models: list = []++ def __enter__(self):+ import agent.context_compressor as cc+ self._cc, self._orig = cc, cc.call_llm++ def metered(*args, **kwargs):+ resp = self._orig(*args, **kwargs)+ self.calls += 1+ usage = getattr(resp, "usage", None) or (resp.get("usage") if isinstance(resp, dict) else None)+ if usage is not None:+ get = (lambda k: getattr(usage, k, None)) if not isinstance(usage, dict) else usage.get+ self.input_tokens += int(get("prompt_tokens") or 0)+ self.output_tokens += int(get("completion_tokens") or 0)+ route = kwargs.get("route_info") or {}+ model = route.get("model") or kwargs.get("model") or getattr(resp, "model", None)+ if model and model not in self.models:+ self.models.append(model)+ return resp++ cc.call_llm = metered+ return self++ def __exit__(self, *exc):+ self._cc.call_llm = self._orig++ def summary(self) -> dict:+ model = self.models[0] if self.models else EVAL_MODEL+ return {+ "compaction_calls": self.calls,+ "compaction_input_tokens": self.input_tokens,+ "compaction_output_tokens": self.output_tokens,+ "compaction_model": model,+ "compaction_cost_usd": openrouter_price_usd(model, self.input_tokens, self.output_tokens),+ }+++def _compress_with_policy(spec: dict, messages) -> tuple:+ """Run one policy; returns (compressed, compressor, compaction-cost dict)."""+ if spec.get("engine") == "jev":+ from evals.compaction.jev_arm import JevCompactor, JevOptions++ comp = JevCompactor(options=JevOptions(**(spec.get("jev") or {})))+ try:+ compressed = comp.compress(copy.deepcopy(messages), current_tokens=total_tokens(messages), force=True)+ except ValueError as e:+ # The plugin throws here and Claude Code falls back to its built-in+ # summary; record the fallback rather than scoring an uncompressed arm.+ comp._last_summary_error = str(e)+ return None, comp, {"jev_fallback": str(e), "compaction_calls": comp.usage.requests,+ "compaction_cost_usd": comp.usage.cost_usd}+ cost = {+ "compaction_calls": comp.usage.requests,+ "compaction_input_tokens": comp.usage.input_tokens,+ "compaction_output_tokens": comp.usage.output_tokens,+ "compaction_model": comp.usage.models[0] if comp.usage.models else "jev",+ "compaction_cost_usd": comp.usage.cost_usd,+ "jev_stats": comp.stats,+ }+ return compressed, comp, cost+from agent.context_compressor import ContextCompressor- before = copy.deepcopy(messages)comp = apply_policy(ContextCompressor(model=EVAL_MODEL, quiet_mode=True), spec)for key, value in (spec.get("ctor") or {}).items():setattr(comp, key, value)+ with _AuxMeter() as meter:+ compressed = comp.compress(copy.deepcopy(messages), current_tokens=total_tokens(messages), force=True)+ return compressed, comp, meter.summary()+++def run_policy(name: str, spec: dict, messages, questions, out_dir: Path,+ with_recovery: bool = False) -> dict:+ before = copy.deepcopy(messages)t0 = time.time()- compressed = comp.compress(copy.deepcopy(messages), current_tokens=total_tokens(messages), force=True)+ compressed, comp, compaction_cost = _compress_with_policy(spec, messages)elapsed = time.time() - t0+ label = f"{name}+recovery" if with_recovery else name+ if compressed is None:+ summary = {"policy": label, "before_tokens": total_tokens(before), "after_tokens": None,+ "recall_pct": None, "compress_seconds": round(elapsed, 1),+ "summary_error": comp._last_summary_error, **compaction_cost}+ out_dir.mkdir(parents=True, exist_ok=True)+ (out_dir / f"{label.replace('+', '_')}.json").write_text(+ json.dumps({"summary": summary, "results": []}, indent=1), encoding="utf-8")+ return summary# The archived region = original messages that did not survive verbatim.surviving = set()@@ -277,7 +392,6 @@ def run_policy(name: str, spec: dict, messages, questions, out_dir: Path,results.append(entry)scored = [r["score"] for r in results]- label = f"{name}+recovery" if with_recovery else namesummary = {"policy": label,"before_tokens": total_tokens(before),@@ -287,6 +401,7 @@ def run_policy(name: str, spec: dict, messages, questions, out_dir: Path,"recall_pct": round(100 * sum(scored) / (2 * len(scored)), 1) if scored else 0.0,"scores": scored,"summary_error": getattr(comp, "_last_summary_error", None),+ **compaction_cost,}out_dir.mkdir(parents=True, exist_ok=True)(out_dir / f"{label.replace('+', '_')}.json").write_text(json.dumps({"summary": summary, "results": results}, indent=1), encoding="utf-8")@@ -305,7 +420,7 @@ def main():messages = load_transcript(args.transcript, cap_tokens=args.cap_tokens)out_dir = Path(args.out)- tid = hashlib.md5(args.transcript.encode()).hexdigest()[:10]+ tid = hashlib.md5(f"{args.transcript}@{args.cap_tokens}".encode()).hexdigest()[:10]qcache = out_dir / f"questions-{tid}.json"questions = generate_questions(messages, args.questions, qcache)print(f"{len(questions)} questions ready ({qcache})")@@ -349,7 +464,9 @@ def main():print(json.dumps(s, indent=1))(out_dir / "scorecard.json").write_text(json.dumps(summaries, indent=1), encoding="utf-8")+ (out_dir / "eval_usage.json").write_text(json.dumps(EVAL_USAGE, indent=1), encoding="utf-8")print(f"\nscorecard -> {out_dir}/scorecard.json")+ print(f"eval LLM usage (questions+answers+judge): {EVAL_USAGE}")if __name__ == "__main__":diff --git a/tests/evals/test_compaction_jev_arm.py b/tests/evals/test_compaction_jev_arm.pynew file mode 100644index 00000000000..162a4110556--- /dev/null+++ b/tests/evals/test_compaction_jev_arm.py@@ -0,0 +1,45 @@+"""Invariants of the fast-jev-compaction eval arm (evals/compaction/jev_arm.py).++Offline: the fake asker stands in for Jev. The contract under test is the+plugin's own: nothing is rewritten, only tool calls/results go, and no tool+result is ever left without its call (or vice versa).+"""+from __future__ import annotations++from evals.compaction.fixtures import synthetic_transcript, total_tokens+from evals.compaction.jev_arm import JevCompactor, JevOptions, fake_asker, message_text+++def _pairs(messages):+ call_ids = {tc["id"] for m in messages for tc in m.get("tool_calls") or []}+ result_ids = {m["tool_call_id"] for m in messages if m.get("role") == "tool"}+ return call_ids, result_ids+++def test_drop_decisions_never_orphan_and_never_rewrite_text():+ msgs = synthetic_transcript(40)+ texts_before = [message_text(m) for m in msgs if m.get("role") != "tool"]+ comp = JevCompactor(asker=fake_asker(keep_call=0.2, keep_result=0.1))+ out = comp.compress(msgs)++ call_ids, result_ids = _pairs(out)+ assert call_ids == result_ids, "a dropped call must take its result with it, and only its result"+ assert total_tokens(out) < total_tokens(msgs)+ # user/assistant text is preserved verbatim and in order (only tool_calls / tool rows change)+ texts_after = [message_text(m) for m in out if m.get("role") != "tool"]+ assert texts_after == [t for t in texts_before if t.strip()]+ # pinned calls (first row / newest rows) are never candidates+ assert comp.stats["pinned"] >= 1 and comp.stats["calls_dropped"] == comp.stats["calls"] - comp.stats["pinned"]+++def test_drop_result_keeps_call_and_bounded_head():+ msgs = synthetic_transcript(30)+ comp = JevCompactor(asker=fake_asker(keep_call=0.9, keep_result=0.1), options=JevOptions(truncate_head_chars=50))+ out = comp.compress(msgs)++ call_ids, result_ids = _pairs(out)+ assert call_ids == result_ids and len(out) == len(msgs)+ truncated = [m for m in out if m.get("role") == "tool" and "fast-jev-compaction truncated" in m["content"]]+ assert truncated and all(m["content"].startswith("step output") for m in truncated)+ assert all(len(m["content"]) < 300 for m in truncated)+ assert comp.stats["results_dropped"] == len(truncated)