Skip to content

The agent that grows with you

feat(plugins): sync vendored tameru to 1.4.0 + 1.4.1 candidate fixes

Benjamin Ang5752d16a7dd8
diff --git a/plugins/context_engine/extractive/plugin.yaml b/plugins/context_engine/extractive/plugin.yaml
index 4625e1bdd22..2be32376e20 100644
--- a/plugins/context_engine/extractive/plugin.yaml
+++ b/plugins/context_engine/extractive/plugin.yaml
@@ -1,4 +1,4 @@
name: extractive
display_name: Tameru compatibility alias
description: "Deprecated compatibility alias; migrate context.engine to tameru"
-version: 1.3.0
+version: 1.4.0
diff --git a/plugins/context_engine/tameru/__init__.py b/plugins/context_engine/tameru/__init__.py
index 843d25119b1..e9f73ac1cc3 100644
--- a/plugins/context_engine/tameru/__init__.py
+++ b/plugins/context_engine/tameru/__init__.py
@@ -1,4 +1,4 @@
-"""Hermes context-engine adapter for vendored Tameru 1.2.0."""
+"""Hermes context-engine adapter for the vendored Tameru engine."""
from __future__ import annotations
from typing import Any
diff --git a/plugins/context_engine/tameru/agent_formats.py b/plugins/context_engine/tameru/agent_formats.py
new file mode 100644
index 00000000000..976986f3af7
--- /dev/null
+++ b/plugins/context_engine/tameru/agent_formats.py
@@ -0,0 +1,1196 @@
+"""Agent-output format detection and adapters (v1.4.0, E4/E5).
+
+What an agent's tool results actually look like: ``cat -n`` / Hermes
+``read_file`` reads (a line-number gutter), source code, unified diffs,
+``grep``/``rg`` output and test-runner output. None of these is a table, and
+the generic CSV/TSV/YAML detectors mistake them for one (a numbered read is a
+two-column TSV; Python is "YAML"; a JS file with a comma in its first line is
+a CSV). This module owns
+
+* the detectors that run BEFORE the table detectors (numbered gutter, code,
+ diff, grep) and the guards that stop code being read as a table;
+* two extractive adapters, :func:`adapt_unified_diff` (whole hunks, never a
+ split -/+ pair) and :func:`adapt_grep_lines` (grouped by file);
+* :func:`preprocess_test_output` (pytest, jest, go test, cargo test);
+* :func:`apply_agent_adapters`, the one entry point ``compress_context`` calls.
+
+Stdlib only, deterministic, linear in the input (no per-line regex over the
+whole text more than once). Gap markers are ``gap_marker(omitted_chars,
+total_chars)`` callables, as in ``compress_context``; with ``None`` each cut
+gets a plain ``[… N hunks omitted …]`` style marker.
+"""
+from __future__ import annotations
+
+import csv
+import re
+from dataclasses import asdict, dataclass
+from typing import Callable
+
+from .unicode_profile import matching_shadow
+
+GapMarker = Callable[[int, int], str]
+
+
+@dataclass(frozen=True)
+class AdapterResult:
+ """Outcome of an adapter: the (possibly shortened) text plus accounting."""
+
+ format: str
+ text: str
+ total_records: int = 0
+ kept_records: int = 0
+ reason: str = ""
+ hit: bool = True # False: a distinctive query selected nothing (orientation cut only)
+
+ def to_dict(self) -> dict[str, object]:
+ return asdict(self)
+
+
+def _plural(count: int, noun: str) -> str:
+ if count == 1:
+ return f"{count} {noun}"
+ return f"{count} {noun}{'es' if noun.endswith(('ch', 's')) else 's'}"
+
+
+def _marker(gap_marker: GapMarker | None, omitted_chars: int, total: int, default: str) -> str:
+ if gap_marker is None:
+ return default
+ return str(gap_marker(omitted_chars, total))
+
+
+def _inlines(gap_marker: GapMarker | None, marker: str, dropped: str) -> bool:
+ """v1.4.1: an opt-in caller marker (``InliningGapMarker``) that is not shorter
+ than the text it would replace keeps that text instead."""
+ return bool(getattr(gap_marker, "inline_short", False)) and len(marker) >= len(dropped)
+
+
+# ---------------------------------------------------------------------------
+# Query selectors (lazy import: format_adapters imports this module).
+# ---------------------------------------------------------------------------
+
+
+class _Matcher:
+ """Case-insensitive, boundary-aware multi-selector matcher."""
+
+ def __init__(self, selectors: tuple[str, ...]):
+ self.empty = not selectors
+ self._rx: list[re.Pattern[str]] = []
+ self._plain: list[str] = []
+ for selector in selectors:
+ if selector[:1].isascii() and selector[-1:].isascii():
+ self._rx.append(
+ re.compile(rf"(?<![\w]){re.escape(selector)}(?![\w])", re.IGNORECASE)
+ )
+ else:
+ self._plain.append(selector)
+
+ def hit(self, text: str) -> bool:
+ if self.empty:
+ return False
+ shadow = text if text.isascii() else matching_shadow(text)
+ if any(rx.search(shadow) for rx in self._rx):
+ return True
+ return any(sel in shadow for sel in self._plain)
+
+
+def _matcher_for(query: str) -> _Matcher:
+ from .format_adapters import _query_selectors
+
+ return _Matcher(_query_selectors(query or ""))
+
+
+# ---------------------------------------------------------------------------
+# E4: numbered gutter and code detection.
+# ---------------------------------------------------------------------------
+
+# `cat -n` (" 12\t..."), Hermes read_file ("12|..."), some readers ("12: ...").
+GUTTER_RE = re.compile(r"^\s*\d+(?:\t|\||:[ \t])")
+_GUTTER_TAB_RE = re.compile(r"^\s*\d+\t")
+_GUTTER_PIPE_RE = re.compile(r"^\s*\d+\|")
+NUMBERED_RATIO = 0.60
+
+
+def strip_gutter(line: str) -> str:
+ """``line`` without its line-number gutter (unchanged when it has none)."""
+ return GUTTER_RE.sub("", line, count=1)
+
+
+def numbered_gutter_ratio(lines: list[str]) -> float:
+ nonblank = [ln for ln in lines if ln.strip()]
+ if not nonblank:
+ return 0.0
+ return sum(1 for ln in nonblank if GUTTER_RE.match(ln)) / len(nonblank)
+
+
+def _consistent_delimiter_table(lines: list[str], delimiter: str) -> bool:
+ """True when ≥80% of ``lines`` carry the same count (≥2) of ``delimiter``:
+ a real table whose first column happens to be numeric, not a gutter.
+ A line whose first field after the gutter is blank or indented (tab- or
+ space-indented code: ``"12\t\treturn x"``) is code, not a row."""
+ counts: dict[int, int] = {}
+ for ln in lines:
+ c = ln.count(delimiter)
+ if delimiter == "\t" and GUTTER_RE.sub("", ln, count=1)[:1] in (" ", "\t"):
+ c = 0
+ counts[c] = counts.get(c, 0) + 1
+ if not counts:
+ return False
+ mode, share = max(counts.items(), key=lambda kv: (kv[1], -kv[0]))
+ return mode >= 2 and share / len(lines) >= 0.8
+
+
+def is_numbered_code(text: str) -> bool:
+ """≥60% of the non-blank lines start with a line-number gutter."""
+ lines = [ln for ln in text.split("\n") if ln.strip()]
+ if len(lines) < 2:
+ return False
+ if numbered_gutter_ratio(lines) < NUMBERED_RATIO:
+ return False
+ tab = sum(1 for ln in lines if _GUTTER_TAB_RE.match(ln))
+ pipe = sum(1 for ln in lines if _GUTTER_PIPE_RE.match(ln))
+ if tab >= pipe and tab and _consistent_delimiter_table(lines, "\t"):
+ return False # "1\tAlice\t30": a TSV with an id column
+ if pipe > tab and pipe and _consistent_delimiter_table(lines, "|"):
+ return False # "1|x|y": a pipe table
+ return True
+
+
+# Lines that open with a code keyword. A keyword immediately followed by a
+# colon ("if: ${{ ... }}", "from: 2020") is a YAML key, not code; "else:",
+# "try:" and "finally:" are the one-word Python statements.
+_CODE_KW_RE = re.compile(
+ r"^\s*(?:"
+ r"(?:import|from|def|class|function|const|let|var|return|if|for|while|elif|"
+ r"except|with|package|fn|pub|public|private|protected|async|export|switch|"
+ r"struct|impl|func|interface|enum|yield|raise|assert|lambda|using|namespace)\b(?!\s*[:,])"
+ r"|(?:else|try|finally)\s*:\s*$|else\s*\{|#include\b|#define\b"
+ r")"
+)
+_SQL_LIKE_RE = re.compile(
+ r"(?im)^\s*(?:CREATE|INSERT|UPDATE|DELETE|SELECT|ALTER|DROP)\b"
+)
+_STANDALONE_CLOSERS = frozenset({"}", "};", "})", "});", ");", "],", "},"})
+
+
+def code_scores(text: str) -> tuple[float, float, int]:
+ """``(strong, semicolon, n)`` for the non-blank, non-fenced lines of text.
+
+ ``strong`` is the share of lines that open with a code keyword or end in
+ ``{``, ``):`` or a standalone closing brace; ``semicolon`` the share ending
+ in ``;``. ``n`` is the number of lines scored.
+ """
+ strong = semi = n = 0
+ in_fence = False
+ for raw in text.split("\n"):
+ line = raw.strip()
+ if not line:
+ continue
+ if line.startswith(("```", "~~~")):
+ in_fence = not in_fence
+ continue
+ if in_fence:
+ continue
+ n += 1
+ if (
+ _CODE_KW_RE.match(raw)
+ or line.endswith(("{", "):"))
+ or line in _STANDALONE_CLOSERS
+ ):
+ strong += 1
+ elif line.endswith(";"):
+ semi += 1
+ if not n:
+ return 0.0, 0.0, 0
+ return strong / n, semi / n, n
+
+
+def looks_like_code(text: str) -> bool:
+ """Source code (Python, JS/TS, C-family, Go, Rust ...), judged by lines.
+
+ Not triggered by SQL scripts (``;``-terminated statements) or prose."""
+ strong, semi, n = code_scores(text)
+ if n < 4:
+ return False
+ if strong >= 0.25:
+ return True
+ return semi >= 0.5 and not _SQL_LIKE_RE.search(text)
+
+
+def code_vetoes_yaml(text: str) -> bool:
+ """Code lines count against YAML: a text with a real share of them is not."""
+ strong, semi, n = code_scores(text)
+ return n >= 3 and (strong >= 0.15 or semi >= 0.5)
+
+
+def delimited_columns_stable(lines: list[str], *, delimiter: str = ",") -> bool:
+ """A table keeps one column count on ≥80% of the sampled lines (and ≥3)."""
+ counts: dict[int, int] = {}
+ sample = [ln for ln in lines if ln.strip()][:40]
+ if not sample:
+ return False
+ for ln in sample:
+ try:
+ width = len(next(csv.reader([ln], delimiter=delimiter)))
+ except (csv.Error, StopIteration):
+ width = -1
+ counts[width] = counts.get(width, 0) + 1
+ mode, share = max(counts.items(), key=lambda kv: (kv[1], kv[0]))
+ return mode >= 3 and share / len(sample) >= 0.8
+
+
+def rejects_table(text_or_lines: str | list[str], *, stable_columns: bool = True) -> bool:
+ """``True`` when the text must NOT be read as CSV/TSV: it is numbered
+ code, it has code lines, or (``stable_columns``) its column count is not
+ stable."""
+ lines = text_or_lines.split("\n") if isinstance(text_or_lines, str) else text_or_lines
+ sample = [ln for ln in lines if ln.strip()][:200]
+ if is_numbered_code("\n".join(sample)):
+ return True
+ code_like = sum(
+ 1
+ for ln in sample[:40]
+ if _CODE_KW_RE.match(ln) or ln.rstrip().endswith(("{", "}", ";", "):"))
+ )
+ if code_like >= 2 and code_like / max(1, len(sample[:40])) >= 0.15:
+ return True
+ return stable_columns and not delimited_columns_stable(sample)
+
+
+# ---------------------------------------------------------------------------
+# Detectors: unified diff and grep/rg output.
+# ---------------------------------------------------------------------------
+
+_HUNK_HEADER_RE = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@")
+_HUNK_ANY_RE = re.compile(r"(?m)^@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@")
+_DIFF_FILE_RE = re.compile(r"(?m)^diff --git |^--- \S[^\n]*\n\+\+\+ ")
+_DIFF_STATS_RE = re.compile(r"^# \S.*: \+\d+ -\d+ in \d+ hunks?$")
+
+
+def detect_unified_diff(text: str) -> bool:
+ """A unified diff: at least one ``@@ -a,b +c,d @@`` hunk header and file
+ headers (``diff --git`` or a ``---``/``+++`` pair)."""
+ if not text or "@@ -" not in text:
+ return False
+ if not _HUNK_ANY_RE.search(text):
+ return False
+ return _DIFF_FILE_RE.search(text) is not None
+
+
+_GREP_MATCH_RE = re.compile(r"^([^\s:][^:]*):(\d+):(.*)$")
+_GREP_CTX_RE = re.compile(r"^([^\s:][^:]*?)-(\d+)-(.*)$")
+
+
+def _pathish(path: str) -> bool:
+ """A file path, not a clock ("12:00:00") or a date ("2026-03-14 12")."""
+ if not ("." in path or "/" in path):
+ return False
+ if re.fullmatch(r"[\d.\-/]+", path):
+ return False
+ if " " in path and "/" not in path:
+ return False
+ return any(ch.isalpha() for ch in path)
+
+
+def _grep_parse_line(line: str) -> tuple[str, str, int, str] | None:
+ m = _GREP_MATCH_RE.match(line)
+ if m and _pathish(m.group(1)):
+ return "m", m.group(1), int(m.group(2)), m.group(3)
+ m = _GREP_CTX_RE.match(line)
+ if m and _pathish(m.group(1)):
+ return "c", m.group(1), int(m.group(2)), m.group(3)
+ return None
+
+
+def detect_grep_lines(text: str) -> bool:
+ """≥70% of the non-blank lines are ``path:N:text`` matches or
+ ``path-N-text`` context lines (``--`` separators are neutral)."""
+ if not text:
+ return False
+ total = ok = real = 0
+ for raw in text.split("\n"):
+ if not raw.strip():
+ continue
+ total += 1
+ if raw == "--":
+ ok += 1
+ continue
+ parsed = _grep_parse_line(raw)
+ if parsed is not None:
+ ok += 1
+ if parsed[0] == "m":
+ real += 1
+ return total >= 2 and real >= 1 and ok / total >= 0.70
+
+
+# ---------------------------------------------------------------------------
+# Unified diff adapter.
+# ---------------------------------------------------------------------------
+
+
+@dataclass
+class _DiffFile:
+ header: list[str]
+ hunks: list[list[str]]
+ trailers: list[list[str]] # lines after hunk k that are not diff syntax
+ has_minus: bool = False
+
+
+def _read_hunk(lines: list[str], i: int) -> int:
+ """Index just past the hunk starting at ``lines[i]`` (a ``@@`` header).
+
+ Consumes by the header's line counts, so a removed line that reads
+ ``--- foo`` or an added ``+++ bar`` can never be taken for a file header.
+ """
+ m = _HUNK_HEADER_RE.match(lines[i])
+ assert m is not None
+ old = int(m.group(2)) if m.group(2) is not None else 1
+ new = int(m.group(4)) if m.group(4) is not None else 1
+ j = i + 1
+ n = len(lines)
+ while j < n and (old > 0 or new > 0):
+ first = lines[j][:1]
+ if first == " " or lines[j] == "":
+ old -= 1
+ new -= 1
+ elif first == "-":
+ old -= 1
+ elif first == "+":
+ new -= 1
+ elif first != "\\":
+ break
+ j += 1
+ while j < n and lines[j].startswith("\\"): # "\ No newline at end of file"
+ j += 1
+ return j
+
+
+def _parse_diff(lines: list[str]) -> tuple[list[str], list[_DiffFile]]:
+ preamble: list[str] = []
+ files: list[_DiffFile] = []
+ cur: _DiffFile | None = None
+ i = 0
+ n = len(lines)
+ while i < n:
+ ln = lines[i]
+ if _DIFF_STATS_RE.match(ln): # our own per-file stats comment: regenerated
+ i += 1
+ continue
+ if ln.startswith("diff --git "):
+ cur = _DiffFile([ln], [], [])
+ files.append(cur)
+ i += 1
+ continue
+ if ln.startswith("--- ") and i + 1 < n and lines[i + 1].startswith("+++ "):
+ if cur is not None and not cur.hunks and not cur.has_minus:
+ cur.header.extend((ln, lines[i + 1]))
+ cur.has_minus = True
+ else:
+ cur = _DiffFile([ln, lines[i + 1]], [], [], has_minus=True)
+ files.append(cur)
+ i += 2
+ continue
+ if cur is not None and _HUNK_HEADER_RE.match(ln):
+ j = _read_hunk(lines, i)
+ cur.hunks.append(lines[i:j])
+ cur.trailers.append([])
+ i = j
+ continue
+ if cur is None:
+ preamble.append(ln)
+ elif not cur.hunks:
+ cur.header.append(ln)
+ else:
+ cur.trailers[-1].append(ln)
+ i += 1
+ return preamble, files
+
+
+def _diff_path(f: _DiffFile) -> str:
+ plus = minus = ""
+ for ln in f.header:
+ if ln.startswith("+++ "):
+ plus = ln[4:].split("\t")[0].strip()
+ elif ln.startswith("--- "):
+ minus = ln[4:].split("\t")[0].strip()
+ if plus and plus != "/dev/null":
+ return plus[2:] if plus.startswith("b/") else plus
+ if minus and minus != "/dev/null":
+ return minus[2:] if minus.startswith("a/") else minus
+ parts = f.header[0].split()
+ last = parts[-1] if parts else "?"
+ return last[2:] if last.startswith("b/") else last
+
+
+def adapt_unified_diff(
+ text: str, query: str, *, gap_marker: GapMarker | None = None
+) -> AdapterResult | None:
+ """Keep every file header, per-file stats and every hunk header line; keep
+ whole hunks (context and -/+ lines together) that mention the query; the
+ bodies of a contiguous run of dropped hunks are replaced by ONE marker
+ (placed after that run's hunk header lines).
+
+ ``None`` when ``text`` is not a unified diff. With no distinctive query
+ terms (or none that hit) the first and last hunk of each file are kept.
+ When nothing is dropped the text is returned unchanged.
+ """
+ if not detect_unified_diff(text):
+ return None
+ lines = text.split("\n")
+ preamble, files = _parse_diff(lines)
+ if not files or not any(f.hunks for f in files):
+ return None
+ matcher = _matcher_for(query)
+ total_hunks = sum(len(f.hunks) for f in files)
+ keep: list[list[bool]] = []
+ any_hit = False
+ for f in files:
+ flags = [False] * len(f.hunks)
+ if not matcher.empty:
+ if matcher.hit(_diff_path(f)):
+ flags = [True] * len(f.hunks)
+ else:
+ for k, hunk in enumerate(f.hunks):
+ if matcher.hit("\n".join(hunk)):
+ flags[k] = True
+ any_hit = any_hit or any(flags)
+ keep.append(flags)
+ if not any_hit: # weak/empty query, or no hunk mentions it: orient the reader
+ for f, flags in zip(files, keep):
+ if flags:
+ flags[0] = True
+ flags[-1] = True
+ kept_hunks = sum(sum(flags) for flags in keep)
+ if kept_hunks == total_hunks:
+ return AdapterResult("diff", text, total_hunks, kept_hunks, "no hunks omitted", hit=any_hit)
+
+ out: list[str] = list(preamble)
+ for f, flags in zip(files, keep):
+ added = removed = 0
+ for hunk in f.hunks:
+ for ln in hunk[1:]:
+ if ln.startswith("+"):
+ added += 1
+ elif ln.startswith("-"):
+ removed += 1
+ out.append(f"# {_diff_path(f)}: +{added} -{removed} in {len(f.hunks)} hunks")
+ out.extend(f.header)
+ run: list[int] = [] # hunks whose body is dropped (header line is kept)
+
+ def flush() -> None:
+ if not run:
+ return
+ omitted = len("\n".join("\n".join(f.hunks[k][1:]) for k in run))
+ out.append(
+ _marker(
+ gap_marker,
+ omitted,
+ len(text),
+ f"[… {_plural(len(run), 'hunk')} omitted …]",
+ )
+ )
+ run.clear()
+
+ for k, hunk in enumerate(f.hunks):
+ if flags[k] or len(hunk) < 2:
+ flush()
+ out.extend(hunk)
+ else:
+ out.append(hunk[0]) # every hunk header line is kept
+ run.append(k)
+ if f.trailers[k]:
+ flush()
+ out.extend(f.trailers[k])
+ flush()
+ return AdapterResult(
+ "diff",
+ "\n".join(out),
+ total_hunks,
+ kept_hunks,
+ "file headers, hunk headers and matching whole hunks",
+ hit=any_hit or matcher.empty,
+ )
+
+
+# ---------------------------------------------------------------------------
+# grep / rg adapter.
+# ---------------------------------------------------------------------------
+
+
+@dataclass
+class _GrepRec:
+ kind: str # "m" match | "c" context
+ lineno: int
+ text: str
+ raw: str
+ group: int # `--` separator group
+
+
+# v1.4.1: a saturated grep query (it hits nearly every match) keeps the lines
+# that define or assign the searched name.
+_GREP_DEFINITION_RE = re.compile(
+ r"^\s*(?:export\s+)?(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?(?:class|def|function|func|fn|interface|struct|enum|trait|type|impl|module|const|let|var)\b"
+)
+_GREP_ASSIGNMENT_RE = re.compile(r"^\s*[A-Za-z_][\w.]*\s*(?::[^=]+)?=\s*")
+
+
+def adapt_grep_lines(
+ text: str,
+ query: str,
+ *,
+ gap_marker: GapMarker | None = None,
+ per_file_cap: int = 8,
+) -> AdapterResult | None:
+ """Regroup grep/rg output by file: ``path (N matches)`` then
+ `` L<line>: <text>`` for the kept matches. Matches that mention the query
+ are kept; a file with none keeps its first ``per_file_cap``. One marker per
+ run of omitted matches. With no query the regroup is lossless whenever it
+ is shorter than the input (otherwise the per-file cap applies).
+
+ When the query is saturated (it hits >=80% of all matches and there are
+ more than ``2 * per_file_cap`` matches), keep per file definition lines,
+ lines hitting the remaining query words, and the first occurrence of each
+ distinct match text (capped at ``per_file_cap`` new distinct lines).
+ """
+ if not detect_grep_lines(text):
+ return None
+ per_file: dict[str, list[_GrepRec]] = {}
+ other: list[str] = []
+ group = 0
+ for raw in text.split("\n"):
+ if not raw.strip():
+ continue
+ if raw == "--":
+ group += 1
+ continue
+ parsed = _grep_parse_line(raw)
+ if parsed is None:
+ other.append(raw)
+ continue
+ kind, path, lineno, body = parsed
+ per_file.setdefault(path, []).append(_GrepRec(kind, lineno, body, raw, group))
+ if not per_file:
+ return None
+ matcher = _matcher_for(query)
+ total_matches = sum(1 for recs in per_file.values() for r in recs if r.kind == "m")
+
+ saturated = False
+ saturated_keep: dict[str, set[int]] = {}
+ if not matcher.empty and total_matches > 2 * per_file_cap:
+ match_texts = [r.text for recs in per_file.values() for r in recs if r.kind == "m"]
+ hits_count = sum(1 for t in match_texts if matcher.hit(t))
+ if hits_count > 0 and hits_count * 5 >= total_matches * 4:
+ saturated = True
+ from .format_adapters import _query_selectors
+
+ selectors = tuple(_query_selectors(query or ""))
+ remaining: list[str] = []
+ for selector in selectors:
+ selector_matcher = _Matcher((selector,))
+ selector_hits = sum(1 for t in match_texts if selector_matcher.hit(t))
+ if selector_hits * 5 < total_matches * 4:
+ remaining.append(selector)
+ other_matcher = _Matcher(tuple(remaining)) if remaining else _Matcher(())
+ seen_texts: set[str] = set()
+ for path, recs in per_file.items():
+ selected: set[int] = set()
+ for i, r in enumerate(recs):
+ if (
+ r.kind == "m"
+ and matcher.hit(r.text)
+ and (_GREP_DEFINITION_RE.match(r.text) or _GREP_ASSIGNMENT_RE.match(r.text))
+ ):
+ selected.add(i)
+ if not other_matcher.empty:
+ for i, r in enumerate(recs):
+ if r.kind == "m" and other_matcher.hit(r.text):
+ selected.add(i)
+ distinct_kept = 0
+ for i, r in enumerate(recs):
+ if r.kind != "m":
+ continue
+ key = r.text.strip()
+ if not key or key in seen_texts:
+ continue
+ if i in selected:
+ seen_texts.add(key)
+ distinct_kept += 1
+ elif distinct_kept < per_file_cap:
+ selected.add(i)
+ seen_texts.add(key)
+ distinct_kept += 1
+ else:
+ seen_texts.add(key)
+ saturated_keep[path] = selected
+
+ any_hit = [False]
+
+ def render(keep_all: bool) -> tuple[str, int]:
+ out: list[str] = []
+ kept_total = 0
+ # v1.4.1: with a caller marker (a host's long elision marker, ~100
+ # chars), files whose matches are all omitted keep their header line
+ # but share ONE marker per consecutive run of such files, instead of
+ # one marker each. The default ``[… N matches omitted …]`` is unchanged.
+ cross: list[_GrepRec] = []
+
+ def line_of(r: _GrepRec) -> str:
+ return f" L{r.lineno}{':' if r.kind == 'm' else '-'} {r.text}"
+
+ def flush_cross() -> None:
+ nonlocal cross
+ n_match = sum(1 for r in cross if r.kind == "m")
+ if n_match:
+ omitted = len("\n".join(r.raw for r in cross))
+ out.append(" " + _marker(gap_marker, omitted, len(text), ""))
+ cross = []
+
+ for path, recs in per_file.items():
+ matches = [i for i, r in enumerate(recs) if r.kind == "m"]
+ keep = [True] * len(recs)
+ if not keep_all:
+ keep = [False] * len(recs)
+ if saturated:
+ any_hit[0] = True
+ for i in saturated_keep.get(path, ()):
+ keep[i] = True
+ else:
+ hits = [i for i in matches if matcher.hit(recs[i].text)] if not matcher.empty else []
+ any_hit[0] = any_hit[0] or bool(hits)
+ for i in hits or matches[:per_file_cap]:
+ keep[i] = True
+ # Context lines survive next to a kept match (same `--` group).
+ near = False
+ last_group = -1
+ for i, r in enumerate(recs): # forward: context after a kept match
+ if r.group != last_group:
+ near = False
+ last_group = r.group
+ if r.kind == "m":
+ near = keep[i]
+ elif near:
+ keep[i] = True
+ near = False
+ last_group = -1
+ for i in range(len(recs) - 1, -1, -1): # backward: context before one
+ r = recs[i]
+ if r.group != last_group:
+ near = False
+ last_group = r.group
+ if r.kind == "m":
+ near = keep[i]
+ elif near:
+ keep[i] = True
+ if gap_marker is not None and not any(keep):
+ out.append(f"{path} ({_plural(len(matches), 'match')})")
+ cross.extend(recs)
+ continue
+ flush_cross()
+ out.append(f"{path} ({_plural(len(matches), 'match')})")
+ run: list[_GrepRec] = []
+
+ def flush() -> None:
+ nonlocal run, kept_total
+ n_match = sum(1 for r in run if r.kind == "m")
+ if n_match:
+ omitted = len("\n".join(r.raw for r in run))
+ marker = " " + _marker(
+ gap_marker,
+ omitted,
+ len(text),
+ f"[… {_plural(n_match, 'match')} omitted …]",
+ )
+ lines_ = [line_of(r) for r in run]
+ if _inlines(gap_marker, marker, "\n".join(lines_)):
+ out.extend(lines_)
+ kept_total += n_match
+ else:
+ out.append(marker)
+ run = []
+
+ for i, r in enumerate(recs):
+ if keep[i]:
+ flush()
+ out.append(line_of(r))
+ kept_total += r.kind == "m"
+ else:
+ run.append(r)
+ flush()
+ flush_cross()
+ out.extend(other)
+ return "\n".join(out), kept_total
+
+ if matcher.empty:
+ lossless, kept = render(True)
+ if len(lossless) < len(text):
+ return AdapterResult(
+ "grep", lossless, total_matches, kept, "lossless regroup by file"
+ )
+ if saturated:
+ body, kept = render(False)
+ return AdapterResult(
+ "grep",
+ body,
+ total_matches,
+ kept,
+ "matches grouped by file; saturated query: definitions, other query words, distinct lines",
+ hit=True,
+ )
+ body, kept = render(False)
+ return AdapterResult(
+ "grep",
+ body,
+ total_matches,
+ kept,
+ "matches grouped by file, capped per file",
+ hit=any_hit[0] or matcher.empty,
+ )
+
+
+# ---------------------------------------------------------------------------
+# Test-runner output.
+# ---------------------------------------------------------------------------
+
+_PYTEST_HDR_RE = re.compile(r"^={3,} (.+?) ={3,}$")
+_PYTEST_SUMMARY_RE = re.compile(
+ r"\b\d+ (?:passed|failed|errors?|skipped|xfailed|xpassed|deselected|warnings?)\b.*\bin [\d.]+s"
+)
+_PYTEST_Q_SUMMARY_RE = re.compile(
+ r"^\d+ (?:passed|failed|errors?|skipped|xfailed|xpassed|deselected|warnings?)\b.*\bin [\d.]+s"
+)
+_PYTEST_PROGRESS_RE = re.compile(r"^\S+\s+([.FEsxX]+)(?:\s+\[\s*\d+%\])?$")
+_PYTEST_VERBOSE_RE = re.compile(r"^\S+::\S.*\s(PASSED|FAILED|ERROR|SKIPPED|XFAIL|XPASS)\b")
+_JEST_BLOCK_RE = re.compile(r"^\s*● (?!Console\b)")
+_JEST_SUMMARY_RE = re.compile(
+ r"^(?:Test Suites|Tests|Snapshots|Time|Ran all test suites|Test Files)\b"
+)
+_JEST_TICK_RE = re.compile(r"^\s*[✓✔√]\s")
+_GO_PKG_RE = re.compile(r"^(?:ok|FAIL|\?)\s+\S+(?:\s|$)")
+_CARGO_OK_RE = re.compile(r"^test \S.* \.\.\. ok$")
+_CARGO_RUNNING_RE = re.compile(r"^running \d+ tests?$")
+_GO_RESULT_RE = re.compile(r"^(?:ok|FAIL)\s+\S+\s+(?:[\d.]+s|\(cached\))")
+_PYTEST_START_RE = re.compile(r"^={3,} test session starts ={3,}$")
+_PYTEST_FINAL_RE = re.compile(
+ r"^={3,} .*\b\d+ (?:passed|failed|errors?|skipped)\b.* in [\d.]+s.* ={3,}$"
+)
+
+
+_LINE_CAP = 400 # test-runner structure lines are short; a longer line is data
+
+
+def detect_test_runner(text: str) -> str | None:
+ """``"cargo"``, ``"go"``, ``"jest"`` or ``"pytest"`` for recognised output.
+
+ Vitest-style ``Tests N passed`` output is left to the legacy path.
+ One pass over the lines, each capped at ``_LINE_CAP`` characters (a long
+ line with many ``N passed`` fragments made the old whole-text regex cubic).
+ """
+ running = cargo_result = jest_suites = jest_tests = go = pytest_open = pytest_sum = False
+ for ln in text.split("\n"):
+ if len(ln) > _LINE_CAP:
+ continue
+ if ln.startswith("running ") and _CARGO_RUNNING_RE.match(ln):
+ running = True
+ elif ln.startswith("test result: "):
+ cargo_result = True
+ elif ln.startswith("Test Suites:") and ln[12:13] in (" ", "\t"):
+ jest_suites = True
+ elif ln.startswith("Tests:") and ln[6:7] in (" ", "\t"):
+ jest_tests = True
+ elif ln.startswith(("--- PASS:", "--- FAIL:", "=== RUN ")) or _GO_RESULT_RE.match(ln):
+ go = True
+ elif ln.startswith("===="):
+ if _PYTEST_START_RE.match(ln):
+ pytest_open = True
+ elif _PYTEST_FINAL_RE.match(ln):
+ pytest_sum = True
+ if running and cargo_result:
+ return "cargo"
+ if jest_suites and jest_tests:
+ return "jest"
+ if go:
+ return "go"
+ if pytest_open or pytest_sum:
+ return "pytest"
+ return None
+
+
+def _render_kept(
+ lines: list[str],
+ keep: list[bool],
+ tests: list[int],
+ suites: list[int],
+ gap_marker: GapMarker | None,
+ total: int,
+ captured: list[int] | None = None,
+) -> str:
+ out: list[str] = []
+ run: list[int] = []
+
+ def flush() -> None:
+ if not run:
+ return
+ n_tests = sum(tests[i] for i in run)
+ n_suites = sum(suites[i] for i in run)
+ omitted = len("\n".join(lines[i] for i in run))
+ if gap_marker is not None:
+ dropped = "\n".join(lines[i] for i in run)
+ if omitted and dropped.strip():
+ marker = str(gap_marker(omitted, total))
+ if _inlines(gap_marker, marker, dropped):
+ out.extend(lines[i] for i in run)
+ else:
+ out.append(marker)
+ elif n_tests:
+ out.append(f"[… {n_tests} passing {'test' if n_tests == 1 else 'tests'} omitted …]")
+ elif n_suites:
+ out.append(f"[… {n_suites} passing {'suite' if n_suites == 1 else 'suites'} omitted …]")
+ elif captured is not None and any(captured[i] for i in run):
+ n_captured = sum(1 for i in run if lines[i].strip())
+ if n_captured:
+ out.append(f"[… {n_captured} captured lines omitted …]")
+ run.clear()
+
+ for i, ln in enumerate(lines):
+ if keep[i]:
+ flush()
+ out.append(ln)
+ else:
+ run.append(i)
+ flush()
+ return "\n".join(out)
+
+
+# Section headers pytest itself prints between "=" rules. A "=== x ===" line
+# inside a failure block that is NOT one of these is the test's own captured
+# output ("===== Report ====="), not a section boundary.
+_PYTEST_KNOWN_HDR_RE = re.compile(
+ r"test session starts|warnings? summary|passes|xfailures|xpasses"
+ r"|slowest(?: \d+)? durations|rerun test summary info|short test summary info"
+ r"|failures|errors|.*\bin [\d.]+s\b.*|no tests ran.*|.*\bwarnings?\b.*",
+ re.IGNORECASE,
+)
+# Lines of a dropped pytest section that still name a failure ("collected 3
+# items / 1 error", "ERROR: ...", an exception from a ``-s`` run).
+_PYTEST_FAILURE_WORD_RE = re.compile(
+ r"\b(?:errors?|failed|failures?|traceback|exceptions?|fatal|panic)\b|Error\b",
+ re.IGNORECASE,
+)
+
+# v1.4.1: captured-output sub-sections inside a pytest failure block.
+_PYTEST_CAPTURED_HDR_RE = re.compile(
+ r"^-{3,} Captured (?:log|stdout|stderr) (?:setup|call|teardown) -{3,}$"
+)
+_PYTEST_RULE_HDR_RE = re.compile(r"^[=-]{3,}\s.*\s[=-]{3,}$|^[=-]{3,}$")
+_PYTEST_TEST_BLOCK_HDR_RE = re.compile(r"^_{3,} .+ _{3,}$")
+_PYTEST_LOG_LEVEL_RE = re.compile(r"^(WARNING|ERROR|CRITICAL|FATAL)\b")
+
+
+def _classify_pytest(lines: list[str]) -> tuple[list[bool], list[int], list[int], list[int]]:
+ n = len(lines)
+ keep = [False] * n
+ tests = [0] * n
+ suites = [0] * n
+ captured = [0] * n
+ # "" (before the first header, and after the final summary) keeps what it
+ # does not recognise: `make`/`tox`/compiler output around a pytest run
+ # carries the errors that explain the run ("make: *** [test] Error 1").
+ section = ""
+ i = 0
+ while i < n:
+ ln = lines[i]
+ m = _PYTEST_HDR_RE.match(ln)
+ if m:
+ name = m.group(1).lower()
+ if _PYTEST_SUMMARY_RE.search(name):
+ keep[i] = True
+ section = ""
+ elif name in {"failures", "errors"} or name.startswith("short test summary"):
+ keep[i] = True
+ section = "keep"
+ elif section == "keep" and not _PYTEST_KNOWN_HDR_RE.fullmatch(name):
+ keep[i] = True # captured output inside a failure block
+ else:
+ section = "drop"
+ i += 1
+ continue
+ if section == "keep":
+ if _PYTEST_CAPTURED_HDR_RE.match(ln):
+ keep[i] = True
+ j = i + 1
+ # A captured section runs to the next rule, test-block header
+ # or pytest section header; captured stdout may hold blank
+ # lines, so a blank line does not end it.
+ while j < n:
+ nxt = lines[j]
+ if (
+ _PYTEST_RULE_HDR_RE.match(nxt)
+ or _PYTEST_TEST_BLOCK_HDR_RE.match(nxt)
+ or _PYTEST_HDR_RE.match(nxt)
+ ):
+ break
+ j += 1
+ body_start = i + 1
+ body_end = j
+ while body_end > body_start and not lines[body_end - 1].strip():
+ body_end -= 1 # trailing blank lines separate the next block
+ if body_end - body_start > 6:
+ from .compress_context import error_fingerprints
+
+ keep_idx = set()
+ for k in (body_start, body_end - 1): # first and last line
+ if body_start <= k < body_end:
+ keep_idx.add(k)
+ for k in range(body_start, body_end):
+ if k in keep_idx:
+ keep[k] = True
+ else:
+ line = lines[k]
+ if error_fingerprints(line) or _PYTEST_LOG_LEVEL_RE.match(line):
+ keep[k] = True
+ else:
+ keep[k] = False
+ captured[k] = 1
+ else:
+ for k in range(body_start, body_end):
+ keep[k] = True
+ for k in range(body_end, j):
+ keep[k] = True # the blank separator lines, as before
+ i = j
+ continue
+ keep[i] = True
+ i += 1
+ continue
+ if _PYTEST_Q_SUMMARY_RE.match(ln.strip()):
+ keep[i] = True
+ i += 1
+ continue
+ v = _PYTEST_VERBOSE_RE.match(ln)
+ if v:
+ if v.group(1) in {"FAILED", "ERROR"}:
+ keep[i] = True
+ else:
+ tests[i] = 1
+ i += 1
+ continue
+ p = _PYTEST_PROGRESS_RE.match(ln)
+ if p:
+ if "F" in p.group(1) or "E" in p.group(1):
+ keep[i] = True # names the file a failure is in (--tb=no has no block)
+ else:
+ tests[i] = p.group(1).count(".")
+ i += 1
+ continue
+ if section == "" and ln.strip():
+ keep[i] = True
+ elif section == "drop" and _PYTEST_FAILURE_WORD_RE.search(ln):
+ keep[i] = True
+ i += 1
+ return keep, tests, suites, captured
+
+
+def _classify_jest(lines: list[str]) -> tuple[list[bool], list[int], list[int]]:
+ n = len(lines)
+ keep = [False] * n
+ tests = [0] * n
+ suites = [0] * n
+ in_block = False
+ for i, ln in enumerate(lines):
+ if _JEST_BLOCK_RE.match(ln):
+ in_block = True
+ keep[i] = True
+ elif ln.startswith(("PASS ", "FAIL ")):
+ in_block = False
+ if ln.startswith("FAIL "):
+ keep[i] = True
+ else:
+ suites[i] = 1
+ elif _JEST_SUMMARY_RE.match(ln) or ln.startswith(("Summary of all failing", "Snapshot Summary")):
+ in_block = False
+ keep[i] = True
+ elif re.match(r"^\s*● Console\b", ln):
+ in_block = False
+ elif in_block:
+ keep[i] = True
+ elif _JEST_TICK_RE.match(ln):
+ tests[i] = 1
+ elif ln.strip() and ln[:1] not in (" ", "\t"):
+ # An unindented line outside every block ("npm ERR! Test failed",
+ # a build error printed around the run) is not jest's own report.
+ keep[i] = True
+ return keep, tests, suites
+
+
+def _classify_go(lines: list[str]) -> tuple[list[bool], list[int], list[int]]:
+ n = len(lines)
+ keep = [True] * n
+ tests = [0] * n
+ suites = [0] * n
+ pending: list[int] = [] # indented log lines since the last top-level result
+ dropping = False # inside the indented children of a passing/skipped test
+ for i, ln in enumerate(lines):
+ indented = ln[:1] in {" ", "\t"}
+ if ln.startswith(("--- PASS:", "--- SKIP:")):
+ for j in pending: # `t.Log` output of a test that passed
+ keep[j] = False
+ pending = []
+ keep[i] = False
+ tests[i] = 1 if ln.startswith("--- PASS:") else 0
+ dropping = True
+ elif ln.startswith("--- FAIL:"):
+ pending = []
+ dropping = False
+ elif ln.startswith("=== "):
+ keep[i] = False # RUN / PAUSE / CONT / NAME
+ dropping = False
+ elif ln == "PASS":
+ keep[i] = False
+ pending = []
+ dropping = False
+ elif indented and dropping:
+ keep[i] = False
+ if ln.lstrip().startswith("--- PASS:"):
+ tests[i] = 1
+ elif indented:
+ pending.append(i)
+ else:
+ pending = []
+ dropping = False
+ return keep, tests, suites
+
+
+def _classify_cargo(lines: list[str]) -> tuple[list[bool], list[int], list[int]]:
+ n = len(lines)
+ keep = [False] * n
+ tests = [0] * n
+ suites = [0] * n
+ in_failures = False
+ in_error = False # a compiler/cargo `error...` diagnostic, up to its blank line
+ for i, ln in enumerate(lines):
+ if in_failures:
+ keep[i] = True
+ if ln.startswith("test result:"):
+ in_failures = False
+ continue
+ if in_error:
+ if ln.strip():
+ keep[i] = True
+ continue
+ in_error = False
+ if ln == "failures:":
+ in_failures = True
+ keep[i] = True
+ elif ln.startswith(("test result:", "running ", " Running ", " Doc-tests ")):
+ keep[i] = True
+ elif ln.startswith("---- ") and ln.endswith(" ----"):
+ keep[i] = True
+ elif _CARGO_OK_RE.match(ln):
+ tests[i] = 1
+ elif ln.startswith("test ") and ln.endswith("FAILED"):
+ keep[i] = True
+ elif ln.startswith(("error:", "error[")):
+ # `error[E0432]: unresolved import`, `error: could not compile`,
+ # `error: test failed, to rerun pass ...`: never reduced away.
+ keep[i] = True
+ in_error = True
+ return keep, tests, suites
+
+
+_RUNNERS = {
+ "pytest": _classify_pytest,
+ "jest": _classify_jest,
+ "go": _classify_go,
+ "cargo": _classify_cargo,
+}
+
+
+def preprocess_test_output(
+ text: str,
+ query: str = "",
+ *,
+ gap_marker: GapMarker | None = None,
+ force: bool = False,
+ total_chars: int | None = None,
+) -> str | None:
+ """Reduce pytest / jest / go test / cargo test output to the failures and
+ summary lines; passing per-test lines become a count marker.
+
+ ``None`` when the output is not a recognised runner's, or nothing would be
+ dropped. A dropped line that mentions the query is kept. ``force`` is the
+ ``content_hint="test"`` case; it only matters to callers (a recognised
+ runner is processed either way), so detection is never skipped.
+ """
+ del force
+ runner = detect_test_runner(text)
+ if runner is None:
+ return None
+ lines = text.split("\n")
+ result = _RUNNERS[runner](lines)
+ captured = None
+ if len(result) == 4:
+ keep, tests, suites, captured = result
+ else:
+ keep, tests, suites = result
+ matcher = _matcher_for(query)
+ if not matcher.empty:
+ dropped = [i for i, ln in enumerate(lines) if not keep[i] and ln.strip()]
+ hits = [i for i in dropped if matcher.hit(lines[i])]
+ # A query word that appears on a large share of the dropped lines
+ # ("test" in "test tests::ok_17 ... ok") selects nothing: ignore it.
+ if hits and len(hits) <= 0.2 * len(dropped):
+ for i in hits:
+ keep[i] = True
+ if all(keep):
+ return None
+ out = _render_kept(
+ lines,
+ keep,
+ tests,
+ suites,
+ gap_marker,
+ total_chars if total_chars else len(text),
+ captured=captured,
+ )
+ return out if len(out) < len(text) else None
+
+
+# ---------------------------------------------------------------------------
+# Entry point for compress_context.
+# ---------------------------------------------------------------------------
+
+
+def apply_agent_adapters(
+ text: str,
+ query: str,
+ *,
+ hint: str | None = None,
+ gap_marker: GapMarker | None = None,
+ weak_query: bool = False,
+) -> tuple[str, dict[str, object] | None]:
+ """Run the diff / grep adapter that ``hint`` names or detection finds.
+
+ Returns ``(text, info)``; ``info`` is ``None`` (and ``text`` untouched)
+ unless an adapter produced a strictly shorter text. With a weak query
+ (``weak_query=True``: no distinctive terms, e.g. "continue") only a
+ lossless result is accepted: filler must not license a cut.
+ """
+ if hint not in (None, "diff", "grep"):
+ return text, None
+ result: AdapterResult | None = None
+ if hint in (None, "diff") and detect_unified_diff(text):
+ result = adapt_unified_diff(text, query, gap_marker=gap_marker)
+ elif hint in (None, "grep"):
+ result = adapt_grep_lines(text, query, gap_marker=gap_marker)
+ if result is None or len(result.text) >= len(text):
+ return text, None
+ if (weak_query or not result.hit) and result.kept_records < result.total_records:
+ # A filler or empty query -- or a distinctive one that selects nothing
+ # in this output -- licenses no cut (E6): only a lossless regroup
+ # (every match kept) may apply. The adapters themselves still
+ # offer first/last-hunk orientation to direct callers.
+ return text, None
+ return result.text, {
+ "name": result.format,
+ "total_records": result.total_records,
+ "kept_records": result.kept_records,
+ "reason": result.reason,
+ }
diff --git a/plugins/context_engine/tameru/compress_context.py b/plugins/context_engine/tameru/compress_context.py
index 8bc60de509e..11a7518969e 100644
--- a/plugins/context_engine/tameru/compress_context.py
+++ b/plugins/context_engine/tameru/compress_context.py
@@ -19,13 +19,23 @@ import time
import unicodedata
from dataclasses import asdict, dataclass, field, replace
from pathlib import Path
-from typing import Any, Iterable, Optional
-
+from typing import Any, Callable, Iterable, Optional
+
+from .agent_formats import (
+ GUTTER_RE as _AGENT_GUTTER_RE,
+ apply_agent_adapters,
+ detect_test_runner,
+ is_numbered_code,
+ preprocess_test_output,
+ rejects_table,
+)
from .contract_gates import (
GENERIC_WORDS,
distinctive_query_terms,
query_has_distinctive_selectors,
)
+from .folding import fold_lossless, json_brief, mask_template # noqa: F401 (fold_lossless re-exported)
+from .format_adapters import FILLER_WORDS
from .industrial import IndustrialLimits, IndustrialResult, industrial_preprocess
from .supersession import apply_supersession
from .unicode_profile import (
@@ -46,6 +56,9 @@ try:
except Exception:
import tempfile
DEFAULT_CCR_DIR = Path(tempfile.gettempdir()) / ".tameru" / "cache" / "context-compress-ccr"
+# Engine release this module ships in; the release bump keeps it in step with
+# pyproject.toml and src/tameru/__init__.py.
+ENGINE_VERSION = "1.4.0"
CCR_TTL_SECONDS = 6 * 60 * 60
CCR_MAX_CLOCK_SKEW_SECONDS = 5 * 60
FREEZE_MAX_DECISIONS = 4096
@@ -82,6 +95,31 @@ _SECRETS_RE = re.compile(
r"|\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}"
r"|(?i:\b(?:password|passwd|secret|api[_-]?key|access[_-]?token|auth[_-]?token)\b"
r"\s*[:=]\s*[\"'][^\"'\s]{16,}[\"'])"
+ r"|\bAIza[A-Za-z0-9_-]{35}(?![A-Za-z0-9_-])"
+ r"|\b(?:sk|rk)_live_[A-Za-z0-9]{16,}"
+ r"|(?i:\b(?:authorization\s*:\s*)?bearer\s+[A-Za-z0-9._~+/=-]{20,})"
+ r"|(?i:\b(?:[A-Za-z0-9_]+_)?(?:password|passwd|secret|api[_-]?key|access[_-]?token|auth[_-]?token)\b"
+ r"\s*[:=]\s*"
+ # 1.4.1 red-team: an unquoted value that is code (a call, an index, a
+ # ``${...}`` / ``<...>`` placeholder, an attribute chain, a snake_case
+ # variable such as ``api_key=fallback_api_key,``) names no credential.
+ r"(?![^\s\"']*[(\[{<$])(?!(?:[A-Za-z_]\w*\.)+[A-Za-z_]\w*(?![^\s,;)\]}]))"
+ r"(?![a-z_](?=[a-z0-9_]*_)[a-z0-9_]*(?![^\s,;)\]}]))"
+ r"[^\s\"']{16,})"
+ # 1.4.1 fix-all: the code exclusions above also let through unquoted
+ # snake_case passphrases and dotted passwords. A whole-line ``.env`` /
+ # shell assignment (``DB_PASSWORD=correct_horse_battery_staple``, no
+ # spaces around ``=``) or a YAML ``key: value`` line still names one: the
+ # value runs to the end of the line and does not end in ``,`` ``;`` ``)``
+ # (a keyword argument on its own line). YAML keeps the attribute-chain
+ # exclusion (``api_key: pydantic.types.SecretStr`` is an annotation).
+ r"|(?im:^[ \t]*(?:export[ \t]+)?(?:[A-Za-z0-9_]+_)?"
+ r"(?:password|passwd|secret|api[_-]?key|access[_-]?token|auth[_-]?token)"
+ r"=(?![^\s\"']*[(\[{<$])[^\s\"']{15,}[^\s\"',;)][ \t]*$)"
+ r"|(?im:^[ \t]*(?:[A-Za-z0-9_]+_)?"
+ r"(?:password|passwd|secret|api[_-]?key|access[_-]?token|auth[_-]?token)"
+ r":[ \t]+(?![^\s\"']*[(\[{<$])(?!(?:[A-Za-z_]\w*\.)+[A-Za-z_]\w*[ \t]*$)"
+ r"[^\s\"']{15,}[^\s\"',;)][ \t]*$)"
)
# Recursion guard markers: output Tameru itself produced.
@@ -91,6 +129,18 @@ _RECURSION_MARKERS = ("<compressed_context", "[CC-Retrieve:")
def _contains_secret(text: str) -> bool:
return bool(text) and _SECRETS_RE.search(text) is not None
+
+def contains_secret(text: str) -> bool:
+ """Public alias of :func:`_contains_secret` (v1.4.0).
+
+ True when ``text`` carries probable credential material (private-key
+ headers, well-known token prefixes, JWTs, long quoted assignments).
+ Callers that persist or forward text — the CCR store, a host plugin's
+ recovery store — use it to refuse to keep secrets. Conservative and
+ deterministic; never raises on non-string input.
+ """
+ return _contains_secret(text if isinstance(text, str) else str(text or ""))
+
_STOP = {
"what", "how", "does", "do", "the", "is", "are", "was", "were", "why",
"when", "where", "which", "who", "whom", "whose", "this", "that", "with",
@@ -111,11 +161,15 @@ _TOKEN_RE = re.compile(
_CAMEL_RE = re.compile(r"\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b")
# Slash is required between segments. `(?:/?[\w.-]+){2,}` is catastrophic
# on long hyphenated ids (acmecorp-genesis-v14-focused-node22-raw).
-_PATH_RE = re.compile(r"(?:[\w.-]+/){2,}[\w.-]+")
+# v1.4.0 (E2): segments are bounded and a match may only start at the start of
+# a run, so a 20k-char run without separators is linear instead of quadratic.
+# Segments over 255 chars are not paths (hash blobs); normal inputs match as
+# before.
+_PATH_RE = re.compile(r"(?<![\w.-])(?:[\w.-]{1,255}/){2,}[\w.-]{1,255}")
_DOTTED_RE = re.compile(r"\b[A-Za-z_][\w]*(?:\.[A-Za-z_][\w]*)+\b")
_QUOTED_RE = re.compile(r"[\"']([^\"']{2,64})[\"']")
_CAPS_RE = re.compile(r"\b[A-Z]{2,24}\b")
-_URI_RE = re.compile(r"\b[a-z][a-z0-9+.-]*://\S+")
+_URI_RE = re.compile(r"\b[a-z][a-z0-9+.-]{0,31}://\S+")
_FAST_ACCOUNTING_PUNCT = frozenset("§…×–—“”‘’")
@@ -195,12 +249,16 @@ def _receipt_hashes(
}
+# v1.4.0 (E2): deleting the accounting punctuation and testing isascii() is the
+# same predicate as the old per-character generator, but runs at C speed — the
+# generator cost ~0.6 s per call on a 650 KB output that contains one "[…]".
+_FAST_ACCOUNTING_DELETE = {ord(ch): None for ch in _FAST_ACCOUNTING_PUNCT}
+
+
def estimate_tokens(text: str) -> int:
if not text:
return 0
- if text.isascii() or all(
- ord(char) < 128 or char in _FAST_ACCOUNTING_PUNCT for char in text
- ):
+ if text.isascii() or text.translate(_FAST_ACCOUNTING_DELETE).isascii():
return max(1, len(_TOKEN_RE.findall(text)))
return max(1, len(token_units(text)))
@@ -306,16 +364,27 @@ def _log_fingerprint(line: str) -> str | None:
Returns None for marker lines ([\u00d7N] ...) so re-running preprocessing
never re-collapses its own output markers (NTK idempotency invariant).
"""
- if line.lstrip().startswith("[\u00d7"):
+ if line.lstrip().startswith(("[\u00d7", "[last] ")):
return None
stripped = _PROGRESS_RE.match(line.strip())
if stripped:
return None
- s = re.sub(r"\d+", "#", line.lower())
- s = re.sub(r"\s+", " ", s).strip()
+ # v1.4.0 (E10): volatile fields (ids, ips, timestamps, durations, quoted
+ # strings, paths) are masked before the digits, by the same function the
+ # error fingerprint uses.
+ s = mask_template(line[:_RX_LINE_CAP].lower())
return s[:240] or None
+def _template_long_enough(line: str) -> bool:
+ """A line is worth collapsing only if its digit-masked form is >12 chars.
+
+ Measured on the digit mask, not on the E10 template (``<ts> info ok`` is
+ short only because its timestamp became one token).
+ """
+ return len(_DIGITS_RE.sub("#", " ".join(line[:_RX_LINE_CAP].lower().split()))) > 12
+
+
def _is_progress_bar(line: str) -> bool:
t = line.strip()
if not t:
@@ -325,6 +394,168 @@ def _is_progress_bar(line: str) -> bool:
return bool(re.match(r"^Compiling [\w-]+ ", t))
+# ---------------------------------------------------------------------------
+# v1.4.0 shared line helpers (E1/E3): leading timestamps and error lines.
+# ---------------------------------------------------------------------------
+
+# Regex scans of a single line look at its first 2000 chars only: a 20k-char
+# run (base64 blob, minified bundle) must not make backtracking regexes
+# quadratic. The full line is still kept/emitted by callers.
+_RX_LINE_CAP = 2000
+
+# A leading log timestamp is a prefix, not a fact: ISO / RFC3339, syslog
+# ("Mar 14 12:00:03") and bare "HH:MM:SS[.mmm]", optionally bracketed. It
+# must not by itself make a line "critical" (E1) or a "critical line" for the
+# verifier (E3).
+_LEAD_TS_RE = re.compile(
+ r"^\s*[\[(]?(?:"
+ r"\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?(?:Z|[+-]\d{2}:?\d{2})?)?"
+ r"|[A-Z][a-z]{2}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}"
+ r"|\d{2}:\d{2}:\d{2}(?:[.,]\d+)?"
+ r")[\])]?[ \t]*"
+)
+
+
+# Critical-line pre-computation (Baseline RAG-style): lines that carry
+# operational facts — errors, config assignments, key-value pairs, dates,
+# versions. Blocks holding them get a floor boost in score_blocks.
+#
+# The date alternative does not match a date that opens the line (optionally
+# after up to two blanks and a "[" or "("): a line-leading timestamp is a log
+# prefix, not a fact. _is_critical_line() widens that to every leading
+# timestamp form and chat role labels.
+_CRIT_LINE_RE = re.compile(
+ r"\b(?:error|exception|traceback|fatal|critical)\b" # error keywords
+ r"|\bconfig(?:uration)?\s*[=:]" # config assignments
+ r"|\b(?:port|host|user|password|url|token|key)\s*[=:]" # key-value pairs
+ r"|(?<!^)(?<!^[ \t])(?<!^[ \t][ \t])(?<!^[\[(])(?<!^[ \t][\[(])"
+ r"\b20[2-3]\d-\d{2}-\d{2}\b" # dates (not line-leading)
+ r"|\b\d+\.\d+\.\d+(?:\.\d+)?\b", # version numbers
+ re.IGNORECASE,
+)
+
+
+# A leading chat role label ("User: ...", "Tool: ...") is a speaker tag, not a
+# `user:` key-value fact. The old buggy call (pos=2, case-sensitive) never
+# matched it either; IGNORECASE would, and boost every user turn.
+_LEAD_ROLE_RE = re.compile(r"[ \t]*(?:User|Assistant|System|Tool|Developer)[ \t]*:[ \t]*")
+
+
+def _lead_ts_end(line: str) -> int:
+ """Index just past a leading timestamp prefix (0 when there is none)."""
+ m = _LEAD_TS_RE.match(line)
+ return m.end() if m else 0
+
+
+def _crit_scan_start(line: str) -> int:
+ """Where a critical-line scan starts: after a leading timestamp and/or
+ chat role label, which are line prefixes rather than facts."""
+ pos = _lead_ts_end(line)
+ m = _LEAD_ROLE_RE.match(line, pos)
+ return m.end() if m else pos
+
+
+def _is_critical_line(line: str) -> bool:
+ """Does ``line`` carry an operational fact (error keyword, config or
+ key-value assignment, date, version)? A bare leading timestamp or chat
+ role label does not count (E1)."""
+ return _CRIT_LINE_RE.search(line, _crit_scan_start(line)) is not None
+
+
+# Level-tagged error lines. Case-sensitive on purpose: "error" in prose is not
+# a log level, ERROR is.
+_ERR_LEVEL_RE = re.compile(r"\b(?:ERROR|FATAL|CRITICAL|PANIC|FAIL(?:ED|URE)?)\b")
+_ERR_EXC_RE = re.compile(r"^\s*(?:\w+\.)*\w*(?:Error|Exception):")
+_ERR_PANIC_RE = re.compile(r"\bpanic:")
+_ERR_TRACEBACK = "Traceback (most recent call last)"
+# "0 errors", "errors: 0", "failed=0" report success; they are not errors. Only
+# a literal zero count negates: "no error body" / "no error handler" are the
+# TEXT of a real ERROR line, and a decimal ("ERROR: 0.5 exceeds ...") is not a
+# count.
+_ERR_NEGATED_RE = re.compile(
+ r"\b0\s+(?:errors?|failures?|failed|fatals?)\b"
+ r"|\b(?:errors?|failures?|failed|fatals?|critical)\s*[=:]\s*0(?!\w|\.\d)",
+ re.IGNORECASE,
+)
+_COUNT_PREFIX_RE = re.compile(r"^\s*\[(?:\u00d7\d+|last)\]\s*")
+_UUID_RE = re.compile(
+ r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
+)
+_HEXRUN_RE = re.compile(r"[0-9a-f]{6,}")
+_DIGITS_RE = re.compile(r"\d+")
+_FINGERPRINT_CAP = 160
+
+
+def _without_zero_counts(s: str) -> str:
+ """``s`` with its literal-zero count phrases ("0 errors", "errors: 0",
+ "failed=0") removed. A line is negated only when nothing else in it signals
+ a failure: Maven's ``[ERROR] Tests run: 5, Failures: 2, Errors: 0`` reports
+ a failure *and* a zero count, and must stay an error line."""
+ return _ERR_NEGATED_RE.sub(" ", s)
+
+
+def _is_level_error_line(line: str) -> bool:
+ """A level-tagged ERROR/FATAL/CRITICAL/PANIC/FAIL* log line."""
+ return _ERR_LEVEL_RE.search(_without_zero_counts(line[:_RX_LINE_CAP])) is not None
+
+
+def _is_error_line(line: str) -> bool:
+ """Level-tagged error, ``XxxError:``/``Exception:`` head, traceback head or
+ Go panic — the lines :func:`error_fingerprints` keys on."""
+ s = _without_zero_counts(line[:_RX_LINE_CAP])
+ # The exception head is anchored at the line start: look past a "[×N] "
+ # collapse prefix so the same failure keeps its key after a fold.
+ head = s
+ prefix = _COUNT_PREFIX_RE.match(s)
+ if prefix:
+ head = s[prefix.end():]
+ return bool(
+ _ERR_LEVEL_RE.search(s)
+ or _ERR_EXC_RE.match(head)
+ or _ERR_TRACEBACK in s
+ or _ERR_PANIC_RE.search(s)
+ )
+
+
+def _error_fp(line: str) -> str:
+ """Stable fingerprint of an error line: volatile fields are masked.
+
+ A leading ``[×N] `` collapse marker is stripped, the line lowercased,
+ UUIDs -> ``<uuid>``, hex runs of 6+ -> ``<hex>`` (before digits, so ids
+ that differ only in digit placement still collide), digits -> ``#``,
+ whitespace collapsed, capped at 160 chars.
+ """
+ s = _COUNT_PREFIX_RE.sub("", line[:_RX_LINE_CAP], count=1).lower()
+ return mask_template(s)[:_FINGERPRINT_CAP]
+
+
+def error_fingerprints(text: str) -> dict[str, str]:
+ """Map error fingerprint -> first exemplar line, in original order (v1.4.0).
+
+ An error line is a level-tagged ``ERROR``/``FATAL``/``CRITICAL``/``PANIC``/
+ ``FAIL``/``FAILED``/``FAILURE`` line (uppercase tokens, case-sensitive), an
+ ``XxxError:`` / ``Exception:`` head line, a ``Traceback (most recent call
+ last)`` line or a Go ``panic:`` line. Lines that report zero errors
+ ("0 errors", "errors: 0", "failed=0") are not errors.
+
+ The fingerprint masks everything volatile (see :func:`_error_fp`), so 500
+ repeats of one failure share a key and the first one is the exemplar.
+ Deterministic and stable under compaction: for compressed text ``c`` that
+ kept an exemplar of every failure, ``error_fingerprints(x).keys() <=
+ error_fingerprints(c).keys()`` — a host can use that as a retention check.
+ """
+ found: dict[str, str] = {}
+ if not text:
+ return found
+ for line in _norm_newlines(text).split("\n"):
+ if not _is_error_line(line):
+ continue
+ fp = _error_fp(line)
+ if fp and fp not in found:
+ found[fp] = line
+ return found
+
+
def preprocess_logs(lines: list[str], query: str = "") -> list[str]:
"""v0.9.0 log preprocessing (NTK layer1 filter adoption):
@@ -338,24 +569,34 @@ def preprocess_logs(lines: list[str], query: str = "") -> list[str]:
preserving first user frame (N3)
"""
out: list[str] = []
+ # v1.4.0 (N6): out_src[k] is the index in `lines` that out[k] came from, so
+ # restored error lines can go back to their original relative position.
+ out_src: list[int] = []
trace: list[str] = []
+ trace_src: list[int] = []
query_match_indices: set[int] = set()
query_selectors = distinctive_query_terms(query or "")
def flush_trace() -> None:
- nonlocal trace
+ nonlocal trace, trace_src
if not trace:
return
if len(trace) <= 4:
out.extend(trace)
+ out_src.extend(trace_src)
else:
out.append(trace[0])
+ out_src.append(trace_src[0])
out.append(f" ... {len(trace) - 2} frames omitted")
+ out_src.append(trace_src[1])
out.append(trace[-1])
+ out_src.append(trace_src[-1])
trace = []
+ trace_src = []
# First pass: drop progress bars but remember whether we saw any.
- kept_lines = [ln for ln in lines if not _is_progress_bar(ln)]
+ kept_src = [i for i, ln in enumerate(lines) if not _is_progress_bar(ln)]
+ kept_lines = [lines[i] for i in kept_src]
query_match_source_indices: set[int] = set()
if query_selectors:
@@ -381,6 +622,7 @@ def preprocess_logs(lines: list[str], query: str = "") -> list[str]:
seen: dict[str, int] = {}
exemplar_idx: dict[str, int] = {}
+ last_seen: dict[str, str] = {} # v1.4.0 (E10): last instance of each template
i = 0
n = len(kept_lines)
@@ -392,6 +634,7 @@ def preprocess_logs(lines: list[str], query: str = "") -> list[str]:
)
if is_trace_like:
trace.append(line)
+ trace_src.append(kept_src[i])
i += 1
continue
flush_trace()
@@ -400,47 +643,89 @@ def preprocess_logs(lines: list[str], query: str = "") -> list[str]:
# Keep the exact matching record outside numeric-template collapse.
query_match_indices.add(len(out))
out.append(line)
+ out_src.append(kept_src[i])
i += 1
continue
fp = _log_fingerprint(line)
- if fp and len(fp) > 12:
+ if fp and _template_long_enough(line):
if fp in exemplar_idx:
seen[fp] += 1
+ last_seen[fp] = line
i += 1
continue
exemplar_idx[fp] = len(out)
seen[fp] = 1
out.append(line)
+ out_src.append(kept_src[i])
i += 1
flush_trace()
# Attach counts: replace exemplar line with counted variant.
+ # v1.4.0 (E10): a template seen >=3 times keeps its LAST instance verbatim
+ # on the next line ("[last] ..."), so the end state of a repeating failure
+ # survives next to the first one.
counted_out: list[str] = []
+ counted_src: list[int] = []
for idx, ln in enumerate(out):
if idx in query_match_indices:
counted_out.append(ln)
+ counted_src.append(out_src[idx])
continue
fp = _log_fingerprint(ln)
cnt = seen.get(fp, 1) if fp else 1
if fp and cnt > 1 and not ln.lstrip().startswith("[\u00d7"):
counted_out.append(f"[\u00d7{cnt}] {ln}")
+ counted_src.append(out_src[idx])
+ last = last_seen.get(fp)
+ if cnt >= 3 and last is not None and last != ln:
+ counted_out.append(f"[last] {last}")
+ counted_src.append(out_src[idx])
else:
counted_out.append(ln)
+ counted_src.append(out_src[idx])
result = counted_out
+ out_src = counted_src
# N6 error-signal invariant: original error lines that are missing from
- # the output must be restored.
+ # the output must be restored. v1.4.0: a line counts as present when an
+ # exemplar of its template is present (possibly prefixed "[×N] "), so the
+ # 499 copies a "[×500] ..." exemplar already stands for are NOT re-added;
+ # genuinely missing lines go back at their ORIGINAL relative position, not
+ # at the top.
def has_error_signal(text: str) -> bool:
return bool(_ERROR_RE.search(text))
- orig_errors = [ln for ln in lines if has_error_signal(ln)]
+ orig_errors = [(i, ln) for i, ln in enumerate(lines) if has_error_signal(ln)]
if orig_errors:
- out_text = "\n".join(result)
- missing = [ln for ln in orig_errors if ln not in result and ln not in out_text]
+ present_lines = set(result)
+ present_fps: set[str] = set()
+ for ln in result:
+ if has_error_signal(ln):
+ fp = _log_fingerprint(_COUNT_PREFIX_RE.sub("", ln, count=1))
+ if fp:
+ present_fps.add(fp)
+ missing: list[tuple[int, str]] = []
+ for src_idx, ln in orig_errors:
+ if ln in present_lines:
+ continue
+ fp = _log_fingerprint(ln)
+ if fp and fp in present_fps:
+ continue
+ if fp:
+ present_fps.add(fp) # one restored exemplar stands for the template
+ missing.append((src_idx, ln))
if missing:
- # Restore missing error lines right after the head (or at top).
- insert_at = min(1, len(result))
- result = result[:insert_at] + missing + result[insert_at:]
+ merged: list[str] = []
+ mi = 0
+ for pos, ln in enumerate(result):
+ # Restored lines go after every kept line that precedes them
+ # in the original (ties: the kept line first), before the next.
+ while mi < len(missing) and missing[mi][0] < out_src[pos]:
+ merged.append(missing[mi][1])
+ mi += 1
+ merged.append(ln)
+ merged.extend(ln for _, ln in missing[mi:])
+ result = merged
return _collapse_blanks(result)
@@ -551,7 +836,7 @@ def preprocess_json(text: str, query: str) -> str:
needles = _json_query_needles(query)
try:
parsed = json.loads(stripped)
- except (json.JSONDecodeError, RecursionError):
+ except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
return text
try:
crushed = _crush_value(parsed, 0, needles)
@@ -571,7 +856,12 @@ def _looks_like_csv(text: str) -> bool:
if len(lines) < 6 or lines[0].count(",") < 2:
return False
similar = sum(1 for ln in lines[1:40] if ln.count(",") >= 2)
- return similar >= 4
+ if similar < 4:
+ return False
+ # v1.4.0 (E4): code is not CSV. A numbered read, code lines, or an unstable
+ # column count (a JS file whose first line happens to hold commas) is not a
+ # table, however many commas it has.
+ return not rejects_table(lines)
def _selector_patterns(terms: Iterable[str]) -> list[re.Pattern[str]]:
@@ -581,8 +871,15 @@ def _selector_patterns(terms: Iterable[str]) -> list[re.Pattern[str]]:
]
-def preprocess_csv(text: str, query: str) -> str:
- """Keep header + distinctive-matching rows. No first-N cliff."""
+def preprocess_csv(
+ text: str,
+ query: str,
+ gap_marker: Callable[[int, int], str] | None = None,
+) -> str:
+ """Keep header + distinctive-matching rows. No first-N cliff.
+
+ ``gap_marker(omitted_chars, total_chars)`` marks each run of dropped rows
+ (v1.4.0); ``None`` keeps the output byte-identical to earlier releases."""
if not query_has_distinctive_selectors(query) or not _looks_like_csv(text):
return text
terms = [t for t in distinctive_query_terms(query) if not t.startswith("script:")]
@@ -591,12 +888,33 @@ def preprocess_csv(text: str, query: str) -> str:
selectors = _selector_patterns(terms)
lines = text.split("\n")
kept = [lines[0]]
- for line in lines[1:]:
+ flags = [False] * len(lines)
+ flags[0] = True
+ for idx, line in enumerate(lines[1:], 1):
if any(selector.search(line) for selector in selectors):
kept.append(line)
+ flags[idx] = True
if len(kept) == 1 or len(kept) == len(lines):
return text
- return "\n".join(kept)
+ if gap_marker is None:
+ return "\n".join(kept)
+ out: list[str] = []
+ run: list[str] = []
+ for line, keep in zip(lines, flags):
+ if keep:
+ if run:
+ dropped = "\n".join(run)
+ if dropped.strip():
+ out.append(str(gap_marker(len(dropped), len(text))))
+ run = []
+ out.append(line)
+ else:
+ run.append(line)
+ if run:
+ dropped = "\n".join(run)
+ if dropped.strip():
+ out.append(str(gap_marker(len(dropped), len(text))))
+ return "\n".join(out)
_FLAT_RECORD_RE = re.compile(r"^[^\s:#][^:\n]{0,200}:\s+\S.*$")
@@ -634,11 +952,27 @@ def strip_ansi(text: str) -> str:
return _ANSI_RE.sub("", text)
-def preprocess_test_runner(text: str, query: str) -> str:
+def preprocess_test_runner(
+ text: str,
+ query: str,
+ content_hint: str | None = None,
+ gap_marker: Callable[[int, int], str] | None = None,
+) -> str:
"""Keep suite summary; drop per-file ticks and node warnings.
Super does this on vitest dumps. Skip if the query names a test file.
+
+ v1.4.0 (E5): pytest, jest, go test and cargo test output is reduced to its
+ failure blocks and summary lines (``agent_formats.preprocess_test_output``);
+ passing per-test lines become a count marker (or ``gap_marker``). Applied
+ when the runner is recognised, or when ``content_hint == "test"``.
"""
+ if content_hint in (None, "test"):
+ reduced = preprocess_test_output(
+ text, query, gap_marker=gap_marker, force=content_hint == "test"
+ )
+ if reduced is not None:
+ return reduced
plain = strip_ansi(text)
if not _TEST_SUMMARY_RE.search(plain):
return text
@@ -656,6 +990,8 @@ def preprocess_test_runner(text: str, query: str) -> str:
return "\n".join(kept)
+# A reduced test run at most this long is final (never re-scored).
+_TEST_REDUCTION_FINAL_CHARS = 16000
_FILLER_COMMENT_RE = re.compile(r"^(--|#)\s*filler\b", re.I)
_TRUST_RISK_RE = re.compile(
r"\b(?:untrusted|not\s+operational)\b"
@@ -684,7 +1020,13 @@ _TRUST_EXCLUDE_CUE_RE = re.compile(
def _normalise_instruction_text(text: str) -> str:
"""Normalise width and remove invisible formatting before classification."""
- normalised = unicodedata.normalize("NFKC", str(text or ""))
+ text = str(text or "")
+ if text.isascii():
+ # Perf (v1.4.0): NFKC is the identity on ASCII and ASCII has no Cf
+ # (format) characters, so the per-character scan below cannot change
+ # anything — it cost ~0.4 s per 850 KB of ASCII log text.
+ return text
+ normalised = unicodedata.normalize("NFKC", text)
return "".join(
char for char in normalised if unicodedata.category(char) != "Cf"
)
@@ -705,25 +1047,160 @@ def preprocess_filler_comments(text: str, query: str) -> str:
def unwrap_hermes_tool(text: str) -> str:
"""Score the inner payload of a Hermes tool JSON wrapper."""
+ return unwrap_tool_payload(text).inner
+
+
+def _compact_json(value: Any) -> str:
+ return json.dumps(value, separators=(",", ":"), ensure_ascii=False)
+
+
+@dataclass(frozen=True)
+class ToolPayload:
+ """A tool result split into its text payload and its envelope (v1.4.0).
+
+ ``inner`` the payload text (the whole input when it was not wrapped)
+ ``field`` ``"content"`` or ``"output"`` when unwrapped, else ``None``
+ ``meta`` the OTHER top-level keys whose values are scalars
+ (str/int/float/bool/None), in original key order — for Hermes
+ tools that is ``exit_code``, ``error``, ``total_lines``, ...
+ ``wrapped`` True when the input was a JSON object wrapper
+ ``order`` every top-level key of the original object, in order (lets
+ :func:`rewrap_tool_payload` put the payload back where it was);
+ not part of equality or repr, so ``ToolPayload(inner, field,
+ meta, wrapped)`` built by hand compares equal to an unwrapped one
+ """
+
+ inner: str
+ field: str | None
+ meta: dict
+ wrapped: bool
+ order: tuple[str, ...] = field(default=(), compare=False, repr=False)
+
+
+def unwrap_tool_payload(text: str) -> ToolPayload:
+ """Split a Hermes tool result into payload and envelope.
+
+ Detection mirrors :func:`unwrap_hermes_tool`: a JSON object with a string
+ ``content`` or ``output`` longer than 200 chars (``content`` first).
+ Anything else comes back as ``ToolPayload(text, None, {}, False)``.
+ """
+ unwrapped = ToolPayload(inner=text, field=None, meta={}, wrapped=False)
+ if not isinstance(text, str):
+ return unwrapped
stripped = text.strip()
if not stripped.startswith("{"):
- return text
+ return unwrapped
try:
parsed = json.loads(stripped)
- except (json.JSONDecodeError, RecursionError):
- return text
+ except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
+ return unwrapped
if not isinstance(parsed, dict):
- return text
+ return unwrapped
for key in ("content", "output"):
val = parsed.get(key)
if isinstance(val, str) and len(val) > 200:
- return val
- return text
+ meta = {
+ k: v
+ for k, v in parsed.items()
+ if k != key and (v is None or isinstance(v, (str, int, float, bool)))
+ }
+ return ToolPayload(
+ inner=val, field=key, meta=meta, wrapped=True, order=tuple(parsed)
+ )
+ return unwrapped
+
+
+def rewrap_tool_payload(payload: ToolPayload, new_inner: str) -> str:
+ """Rebuild a tool-result JSON object around a replacement payload.
+
+ ``new_inner`` goes back under ``payload.field`` and the scalar ``meta``
+ keys keep their original order; non-scalar keys the unwrap dropped are not
+ restored. An unwrapped payload returns ``new_inner`` unchanged. Compact
+ separators, so the result is one line of JSON that Hermes' own
+ ``"exit_code"\\s*:\\s*N`` matchers still read.
+ """
+ if not payload.wrapped or payload.field is None:
+ return new_inner
+ out: dict[str, Any] = {}
+ for key in payload.order:
+ if key == payload.field:
+ out[key] = new_inner
+ elif key in payload.meta:
+ out[key] = payload.meta[key]
+ if payload.field not in out:
+ out[payload.field] = new_inner
+ for key, value in payload.meta.items():
+ out.setdefault(key, value)
+ return _compact_json(out)
+
+
+# v1.4.0 (E8): caller-supplied routing hints. diff/grep/test are recorded in
+# the receipt only (their adapters arrive later); they route like None.
+_CONTENT_HINTS = frozenset(
+ {"code", "numbered_code", "log", "json", "text", "diff", "grep", "test"}
+)
+_CODE_HINTS = frozenset({"code", "numbered_code"})
+# Hints whose content must never be read as a table: source code (commas and
+# colons are syntax) and logs (a Java "12:00:00,123" timestamp makes every line
+# look like a two-column CSV row).
+_NO_TABLE_HINTS = frozenset({"code", "numbered_code", "log"})
+_HINT_ROUTES = {
+ "code": "code",
+ "numbered_code": "code",
+ "log": "log",
+ "json": "json",
+ "text": "text",
+}
+# Line-number gutter of numbered code: `cat -n` (" 12\t...") or `N|`.
+_GUTTER_RE = _AGENT_GUTTER_RE # `N\t` (cat -n), `N|` (Hermes read_file), `N: `
+
+
+def _marker_tuple(markers: Iterable[str] | str | None) -> tuple[str, ...]:
+ """Materialise ``recursion_markers`` once: a bare string is ONE marker (not
+ a set of characters), non-strings and empty strings are dropped (an empty
+ marker would match every line)."""
+ if not markers:
+ return ()
+ if isinstance(markers, str):
+ markers = (markers,)
+ return tuple(m for m in markers if isinstance(m, str) and m)
+
+
+def _has_anchored_marker(text: str, markers: tuple[str, ...]) -> bool:
+ """True when some line of ``text`` starts with one of ``markers`` (after
+ ``lstrip``). Explicit and anchored: a marker merely mentioned mid-line,
+ or text that resembles a marker without being one, does not count."""
+ if not markers:
+ return False
+ if any(m in text for m in markers) and any(
+ line.lstrip().startswith(markers) for line in text.splitlines()
+ ): # (the substring test is a cheap reject before the line scan)
+ return True
+ return _marker_in_tool_payload(text, markers)
-def preprocess(text: str, query: str) -> tuple[list[str], str]:
+def _marker_in_tool_payload(text: str, markers: tuple[str, ...]) -> bool:
+ """A Hermes tool result is a JSON object around ONE payload string, so a
+ marker that opens a line of the payload is escaped inside the JSON text and
+ never opens a line of it. Judge the unwrapped payload as well, otherwise a
+ host that re-wraps its compacted output (``rewrap_tool_payload``) would see
+ that output compressed a second time."""
+ if text.lstrip()[:1] != "{":
+ return False
+ escaped = [json.dumps(m, ensure_ascii=a)[1:-1] for m in markers for a in (False, True)]
+ if not any(e in text for e in escaped):
+ return False
+ inner = unwrap_tool_payload(text).inner
+ if inner is text:
+ return False
+ return any(line.lstrip().startswith(markers) for line in inner.splitlines())
+
+
+def preprocess(
+ text: str, query: str, content_hint: str | None = None
+) -> tuple[list[str], str]:
lines = _norm_newlines(text).split("\n")
- kind = route_content_type(lines)
+ kind = _HINT_ROUTES.get(content_hint) or route_content_type(lines)
if kind == "log":
return preprocess_logs(lines, query), kind
if kind == "code":
@@ -803,8 +1280,32 @@ _WEAK_NEXT_CHAT = frozenset(
"final",
"begin",
}
+) | FILLER_WORDS | frozenset(
+ # v1.4.0 (E6): conversational filler — a reply made only of these names
+ # nothing in the document ("continue", "thanks", "go ahead", "keep going",
+ # "carry on", "sounds good"). FILLER_WORDS (format_adapters.py) supplies
+ # continue/thanks/thank/ahead/proceed/okay/sure/lgtm/sounds/good/great/
+ # yes/yeah/yep/cool/done/again/retry/going/carry/nice/perfect/…
+ {"sounds", "looks", "works"}
)
+# Words that, alone or together, make a query "filler only": the reply to the
+# agent rather than a question about the text ("fix it", "ok thanks", "LGTM").
+# Such a query is treated as an EMPTY query on every path (see
+# _is_filler_query) — adapters, destructive preprocess, selection, fail-open.
+_FILLER_QUERY_WORDS = frozenset(_WEAK_NEXT_CHAT | _STOP | {"fix", "fixes", "do", "so", "now", "then"})
+
+
+def _is_filler_query(query: str) -> bool:
+ """True when every word of the query is conversational filler.
+
+ Uppercase variants count ("OK", "LGTM" would otherwise pass the
+ distinctive-identifier gate as CAPS tokens). A query with any other word —
+ a digit-bearing token, an identifier, a topical noun — is not filler.
+ """
+ words = re.findall(r"[^\W_]+", (query or "").casefold())
+ return bool(words) and all(w in _FILLER_QUERY_WORDS for w in words)
+
def _topic_terms(query: str) -> list[str]:
"""English topic words that are not stop/generic/next-chat filler."""
@@ -974,7 +1475,12 @@ def _term_in_text(term: str, lower: str) -> bool:
return re.search(rf"\b{re.escape(term)}\b", lower) is not None
-def score_blocks(blocks: list[dict[str, Any]], query: str) -> list[dict[str, Any]]:
+def score_blocks(
+ blocks: list[dict[str, Any]],
+ query: str,
+ *,
+ content_type: str | None = None,
+) -> list[dict[str, Any]]:
terms = _extract_terms(query)
entities = _extract_entities(query)
n = max(1, len(blocks))
@@ -1044,14 +1550,24 @@ def score_blocks(blocks: list[dict[str, Any]], query: str) -> list[dict[str, Any
candidate_blocks[cand] = 0
for cand in seen_candidates:
c_lower = cand.lower()
- if re.fullmatch(r"\w{4,}", c_lower):
- df = token_counter.get(c_lower, 0)
- else:
- df = sum(1 for lt in lower_texts if c_lower in lt)
+ # Perf (v1.4.0): only single-token candidates can ever match the
+ # per-block token sets in the boost loop below; dotted / multi-word /
+ # quoted candidates never could, and their df was an O(candidates x
+ # blocks) substring scan (thousands of unique `abc123.json` names in a
+ # log). Skipping them changes no score.
+ if not re.fullmatch(r"\w{4,}", c_lower):
+ continue
+ df = token_counter.get(c_lower, 0)
# Require the entity to be in at least 2 but at most n/2 blocks
# (if it's in every block, it's a filler word, not a bridge)
if 2 <= df <= n // 2:
bridge_entities[cand] = df
+ # Index by lowercase token so the per-block boost is O(tokens in block),
+ # not O(bridge entities). Boost values are multiples of 0.5, so summation
+ # order cannot change a score.
+ bridge_by_lower: dict[str, list[int]] = {}
+ for ent, span in bridge_entities.items():
+ bridge_by_lower.setdefault(ent.lower(), []).append(span)
for b in blocks:
fp = re.sub(r"\s+", " ", b["text"].lower())[:240]
if len(fp) > 20:
@@ -1109,21 +1625,35 @@ def score_blocks(blocks: list[dict[str, Any]], query: str) -> list[dict[str, Any
# config assignments, key-value pairs, dates, versions, large numbers.
# Blocks containing these lines get a floor boost so they survive
# compression even without query overlap.
- _CRIT_LINE_RE = re.compile(
- r"\b(?:error|exception|traceback|fatal|critical)\b" # error keywords
- r"|\bconfig(?:uration)?\s*[=:]" # config assignments
- r"|\b(?:port|host|user|password|url|token|key)\s*[=:]" # key-value pairs
- r"|\b20[2-3]\d-\d{2}-\d{2}\b" # dates
- r"|\b\d+\.\d+\.\d+(?:\.\d+)?\b" # version numbers
- )
+ # v1.4.0 (E1): the regex is module-level and compiled with IGNORECASE; the
+ # old call `.search(line, re.IGNORECASE)` passed the flag as `pos=2`, so it
+ # matched case-sensitively and skipped the first two characters of a line.
+ # A bare LEADING timestamp is a log prefix, not a fact: the search starts
+ # after it, so timestamped INFO lines are not all "critical".
crit_lines_per_block: list[int] = []
for b in blocks:
cnt = 0
for line in b["text"].splitlines():
- if _CRIT_LINE_RE.search(line, re.IGNORECASE):
+ if _is_critical_line(line):
cnt += 1
crit_lines_per_block.append(cnt)
+ # v1.4.0 (E3): for content routed as a log, the first block carrying each
+ # level-tagged error fingerprint (ERROR/FATAL/CRITICAL/PANIC/FAIL*) is an
+ # "error exemplar". It is exempt from the stale-error penalty/cap, and the
+ # selectors keep it even when the floor-saturated path reduces to
+ # `_important`. Tracebacks and non-log content are unaffected.
+ error_exemplar_ids: set[int] = set()
+ if content_type == "log":
+ seen_error_fps: set[str] = set()
+ for b in blocks:
+ for line in b["text"].splitlines():
+ if _is_level_error_line(line):
+ fp = _error_fp(line)
+ if fp not in seen_error_fps:
+ seen_error_fps.add(fp)
+ error_exemplar_ids.add(b["id"])
+
# Weak-query adaptive floor (rate-distortion H(Q) theory):
# When the query has low information content, raise the novelty floor
# and recency ramp so middle blocks survive. A vague "keep track of
@@ -1193,11 +1723,13 @@ def score_blocks(blocks: list[dict[str, Any]], query: str) -> list[dict[str, Any
# hits deep inside a large document are handled history — the fact of
# the error survives via citations; the bulk shouldn't. Only applies
# when there are enough blocks that recency is meaningful.
+ error_exemplar = b["id"] in error_exemplar_ids
if (
n >= 12
and idx < n - 6
and entity_hits == 0
and term_hits == 0
+ and not error_exemplar
and (
b["type"] == "trace"
or (
@@ -1247,9 +1779,10 @@ def score_blocks(blocks: list[dict[str, Any]], query: str) -> list[dict[str, Any
# entity for every block (O(n·e) substring scans → O(n + e)).
bridge_boost = 0.0
block_tokens = block_token_sets[idx]
- for ent, span in bridge_entities.items():
- if ent.lower() in block_tokens:
- bridge_boost += 1.5 + 0.5 * min(span, 5)
+ if bridge_by_lower:
+ for tok in block_tokens:
+ for span in bridge_by_lower.get(tok, ()):
+ bridge_boost += 1.5 + 0.5 * min(span, 5)
if bridge_boost > 0:
score += bridge_boost
if reason == "context":
@@ -1332,6 +1865,8 @@ def score_blocks(blocks: list[dict[str, Any]], query: str) -> list[dict[str, Any
# floor minimum so it drops to citations instead of re-inflating.
if reason == "stale error":
score = min(score, 1.8)
+ if error_exemplar and reason in ("context", "log error", "critical lines"):
+ reason = "error exemplar"
scored.append(
{
**b,
@@ -1341,6 +1876,7 @@ def score_blocks(blocks: list[dict[str, Any]], query: str) -> list[dict[str, Any
"term_hits": term_hits,
"rare_term_hits": rare_term_hits,
"trust_risk": trust_risk,
+ "error_exemplar": error_exemplar,
}
)
return scored
@@ -1370,7 +1906,8 @@ def _important(blocks: list[dict[str, Any]]) -> set[int]:
ids: set[int] = set()
for b in blocks:
# v0.7.0: stale-error heads are handled history — not "important".
- if b.get("reason") == "stale error":
+ # v1.4.0 (E3): a level-tagged log error exemplar is never "stale".
+ if b.get("reason") == "stale error" and not b.get("error_exemplar"):
continue
# v0.6.0: trust-risk blocks are never "important" — previously the
# trace/high-score/definition passes ignored the flag, letting an
@@ -1891,13 +2428,20 @@ def select_adaptive(
for b in blocks
if b.get("rare_term_hits") or b["entity_hits"]
}
+ # v1.4.0 (E3): one exemplar block per level-tagged log error fingerprint
+ # survives every selector path (stale-error cap, floor saturation, needle).
+ err_keep = {
+ b["id"]
+ for b in blocks
+ if b.get("error_exemplar") and not b.get("trust_risk")
+ }
if needle_only and hits:
use = rare if rare else hits
safe_use = {bid for bid in use if not blocks[bid].get("trust_risk")}
if not safe_use:
return _ret({b["id"] for b in blocks}, True, "high", "needle-all-trust")
use = safe_use
- kept = set(use)
+ kept = set(use) | err_keep
if not blocks[0].get("trust_risk") or blocks[0].get("pinned"):
kept.add(blocks[0]["id"])
if (
@@ -1917,7 +2461,7 @@ def select_adaptive(
# "needle" keep-set approaches the whole document. Cap it back to
# the query-hit core — the needle path's contract is a tight set.
if len(kept) > max(len(use) * 2, 16) and len(kept) / len(blocks) > 0.6:
- kept = set(use)
+ kept = set(use) | err_keep
if not blocks[0].get("trust_risk") or blocks[0].get("pinned"):
kept.add(blocks[0]["id"])
kept = _stitch_neighbors(blocks, kept)
@@ -1939,7 +2483,7 @@ def select_adaptive(
# Rate-distortion insight: lossy schemes compound under repeated
# compaction; a single high scorer shouldn't create a cascade.
floor = max(2.2, min(11.5, 0.38 * top))
- kept = set(important)
+ kept = set(important) | err_keep
for b in safe_blocks:
if b["score"] >= floor:
kept.add(b["id"])
@@ -1954,7 +2498,7 @@ def select_adaptive(
)
path = "floor"
if floor_ratio > 0.9 and len(safe_blocks) > 64 and not needle_only:
- kept = set(important)
+ kept = set(important) | err_keep
path = "floor-saturated"
# Keep the head. A trust-risk tail is not a safe recency sink.
# Exception (leanctx old-error purge): a stale-error head is handled
@@ -1998,6 +2542,7 @@ def select_adaptive(
}
if not kept:
return _ret({b["id"] for b in blocks}, True, "high", "line-records-empty")
+ kept |= err_keep
if not blocks[0].get("trust_risk") or blocks[0].get("pinned"):
kept.add(blocks[0]["id"])
if (
@@ -2020,7 +2565,9 @@ def select_adaptive(
if keep_ratio > 0.8:
risk = "medium"
if not important and signal < 0.1:
- risk = "high"
+ # No query signal and nothing important: a guess. Retained log error
+ # exemplars (E3) make it a defensible one, not a certain one.
+ risk = "medium" if err_keep else "high"
return _ret(kept, False, risk, path)
@@ -2028,6 +2575,8 @@ def select_fixed(
blocks: list[dict[str, Any]],
budget_ratio: float,
out: dict[str, Any] | None = None,
+ *,
+ reserve_tokens: int = 0,
) -> tuple[set[int], bool, str]:
if out is not None:
out["path"] = "fixed"
@@ -2044,12 +2593,22 @@ def select_fixed(
)
else:
budget = max(1, int(round(total * budget_ratio)))
+ if reserve_tokens > 0:
+ # v1.4.0 (E8): tokens the caller's gap markers will occupy are not
+ # available to content.
+ budget = max(1, budget - int(reserve_tokens))
important = _important(blocks)
ranked = sorted(
(b for b in blocks if not b.get("trust_risk")),
key=lambda b: (-b["score"], b["start"]),
)
- kept: set[int] = set(important) | pinned_ids
+ # v1.4.0 (E3): log error exemplars are forced into the keep-set.
+ err_keep = {
+ b["id"]
+ for b in blocks
+ if b.get("error_exemplar") and not b.get("trust_risk")
+ }
+ kept: set[int] = set(important) | pinned_ids | err_keep
used = sum(b["tokens"] for b in blocks if b["id"] in kept)
for b in ranked:
if b["id"] in kept:
@@ -2094,24 +2653,49 @@ def _entity_recall(original: str, compressed: str, query: str) -> float:
return hit / len(keys)
+# Substring-scan work allowed per verification (characters examined).
+_RECALL_SCAN_BUDGET_CHARS = 40_000_000
+
+_CRITICAL_ERROR_RE = re.compile(
+ r"\b(?:error|exception|traceback|fail|fatal|timeout|denied|invalid)\b", re.IGNORECASE
+)
+# v1.4.0 (E2): the key is bounded ({1,64}) — `[\w]+[=:]` rescanned a 20k-char
+# word run from every start position (quadratic). A longer key still matches
+# through its last 64 characters, so the boolean result is unchanged.
+_CRITICAL_KV_RE = re.compile(r"\w{1,64}[=:]\s*\w[\w.-]*")
+_CRITICAL_NUM_RE = re.compile(
+ r"\b\d{4}-\d{2}-\d{2}\b|\b\d+\.\d+\.\d+\b|\b\d{6,}\b"
+ r"|\b\d+\s*(?:ms|s|sec|bytes|kb|mb|gb)\b"
+)
+
+
def _extract_critical_lines(text: str) -> list[str]:
"""Extract lines that look like critical facts: error lines, key-value
pairs with identifiers, tracebacks, and lines containing numbers + words.
Mirrors Baseline RAG's critical_lines_total/kept/dropped reporting.
+
+ v1.4.0: a bare leading timestamp is a prefix, not a fact — previously
+ `12:03:45` satisfied the key-value rule and the date satisfied the number
+ rule, so EVERY timestamped log line was "critical" and the verifier could
+ not see error loss in the crowd. Error lines still are. (A chat role label
+ keeps counting here, as it always did; only scoring skips it.)
"""
critical: list[str] = []
for line in text.split("\n"):
t = line.strip()
if not t:
continue
+ scan = t[:_RX_LINE_CAP]
# Error/exception/traceback lines
- if re.search(r"\b(error|exception|traceback|fail|fatal|timeout|denied|invalid)\b", t, re.I):
+ if _CRITICAL_ERROR_RE.search(scan):
critical.append(t)
+ continue
+ start = _lead_ts_end(scan)
# Lines with key-value patterns (config, settings, identifiers)
- elif re.search(r"[\w]+[=:]\s*[\w][\w.-]*", t) and len(t) > 15:
+ if len(t) > 15 and _CRITICAL_KV_RE.search(scan, start):
critical.append(t)
# Lines containing specific numbers (dates, IDs, versions, counts)
- elif re.search(r"\b\d{4}-\d{2}-\d{2}\b|\b\d+\.\d+\.\d+\b|\b\d{6,}\b|\b\d+\s*(ms|s|sec|bytes|kb|mb|gb)\b", t):
+ elif _CRITICAL_NUM_RE.search(scan, start):
critical.append(t)
return critical
@@ -2123,12 +2707,37 @@ def _critical_line_recall(original: str, compressed: str) -> tuple[int, int]:
if not critical:
return 0, 0
kept = 0
+ # Perf (v1.4.0, E2): lowercase the compressed text ONCE. Lowering it inside
+ # the per-line loop was O(lines x output) — 30 s of a 35 s run on a 650 KB
+ # log (and ~10x worse again when the output holds a non-ASCII "[…]").
+ compressed_lower = compressed.lower()
+ # Whole-line fast path: the engine emits original lines (optionally with a
+ # "[×N] " collapse prefix), so nearly every kept critical line is a line of
+ # the output and a set lookup answers it. Only lines the sets cannot place
+ # fall back to the substring scan, under a work budget so the scan stays
+ # linear on big outputs; with small outputs the budget never binds and the
+ # result is exactly "core is a substring of the compressed text".
+ out_lines: set[str] = set()
+ out_prefixes: set[str] = set()
+ for out_line in compressed_lower.split("\n"):
+ out_line = out_line.strip()
+ out_lines.add(out_line)
+ out_prefixes.add(out_line[:80])
+ bare = _COUNT_PREFIX_RE.sub("", out_line, count=1)
+ if bare is not out_line:
+ out_lines.add(bare)
+ out_prefixes.add(bare[:80])
+ scan_budget = _RECALL_SCAN_BUDGET_CHARS
for line in critical:
# A critical line is "kept" if its core content (first 80 chars,
# lowercased) appears in the compressed text.
core = line.lower()[:80]
- if core in compressed.lower():
+ if core in out_lines or core in out_prefixes:
kept += 1
+ elif scan_budget > 0:
+ scan_budget -= len(compressed_lower)
+ if core in compressed_lower:
+ kept += 1
return kept, len(critical)
@@ -2197,16 +2806,48 @@ def verify_compression(
}
+def _span_chars(lines: list[str], lo: int, hi: int) -> int:
+ """Length of ``"\\n".join(lines[lo:hi])`` without building the string."""
+ if hi <= lo:
+ return 0
+ return sum(len(ln) for ln in lines[lo:hi]) + (hi - lo - 1)
+
+
+class InliningGapMarker:
+ """Wrap a gap marker so renderers may inline short dropped spans.
+
+ Renderers which see it emit a dropped span verbatim when the rendered
+ marker is not shorter than the span.
+ """
+
+ __slots__ = ("fn",)
+ inline_short = True
+
+ def __init__(self, fn: Callable[[int, int], str]) -> None:
+ self.fn = fn
+
+ def __call__(self, omitted: int, total: int) -> str:
+ return self.fn(omitted, total)
+
+
+def _inlines_short(marker) -> bool:
+ return bool(getattr(marker, "inline_short", False))
+
+
def _render(
lines: list[str],
blocks: list[dict[str, Any]],
kept: set[int],
citations: bool = False,
reorder_best: bool = False,
+ gap_marker: Callable[[int, int], str] | None = None,
+ total_chars: int | None = None,
+ content_lines: list[str] | None = None,
) -> str:
chronological_blocks = sorted(blocks, key=lambda block: block["start"])
block_starts = [block["start"] for block in chronological_blocks]
order = sorted((b for b in blocks if b["id"] in kept), key=lambda b: b["start"])
+ chronological = True
if reorder_best and len(order) >= 3:
# Lost-in-the-middle mitigation (LongLLMLingua / twotrim): models
# attend hardest to window edges. Best-scoring block anchors the
@@ -2215,10 +2856,52 @@ def _render(
best, second = ranked[0], ranked[1]
rest = [b for b in order if b["id"] not in {best["id"], second["id"]}]
order = [best, *rest, second]
+ chronological = False
+ # v1.4.0 (E8): a caller-supplied gap marker replaces EVERY gap marker
+ # (citations=False only): leading, internal and trailing. It is called as
+ # gap_marker(omitted_chars, total_chars) with the length of the dropped
+ # span (its lines joined with "\n") and the length of the caller's input.
+ # Spans of blank lines only are not content and get no marker, and one
+ # dropped span yields exactly one marker however many blocks it holds.
+ # gap_marker=None keeps the historical "[…]" behaviour byte for byte.
+ marker_fn = gap_marker if (gap_marker is not None and not citations) else None
+ total = total_chars if total_chars is not None else _span_chars(lines, 0, len(lines))
+ # "Is this span blank?" is judged on the lines the scorer read: a
+ # numbered-code gutter ("75|", " 75\t") makes an empty source line
+ # non-empty in `lines`, and would earn a bogus marker between two
+ # adjacent kept blocks.
+ probe_lines = content_lines if content_lines is not None else lines
+ inline_short = _inlines_short(marker_fn) if marker_fn is not None else False
+
+ def _gap(lo: int, hi: int) -> list[str] | None:
+ if marker_fn is None:
+ return None
+ if not any(ln.strip() for ln in probe_lines[lo:hi]):
+ return None
+ span_text = "\n".join(lines[lo:hi]) if inline_short else None
+ text = marker_fn(_span_chars(lines, lo, hi), total)
+ marker = str(text) if text is not None else ""
+ if not marker:
+ return None
+ # 1.4.1 opt-in: only InliningGapMarker emits the dropped lines
+ # verbatim when the rendered marker is not shorter than the span.
+ if inline_short and span_text is not None and len(marker) >= len(span_text):
+ return lines[lo:hi]
+ return [marker]
+
parts: list[str] = []
prev_end = -1
for b in order:
- if prev_end >= 0 and b["start"] > prev_end + 1:
+ if marker_fn is not None:
+ if prev_end < 0 and chronological and b["start"] > 0:
+ lead = _gap(0, b["start"])
+ if lead is not None:
+ parts.extend(lead)
+ elif prev_end >= 0 and b["start"] > prev_end + 1:
+ between = _gap(prev_end + 1, b["start"])
+ if between is not None:
+ parts.extend(between)
+ elif prev_end >= 0 and b["start"] > prev_end + 1:
if citations:
# ARC-style citation: name every dropped block so the model
# knows it can recall the exact head/tail by hash later.
@@ -2278,9 +2961,50 @@ def _render(
j += 1
parts.extend(lines[start : end + 1])
prev_end = end
+ if marker_fn is not None and chronological and parts and prev_end < len(lines) - 1:
+ trail = _gap(prev_end + 1, len(lines))
+ if trail is not None:
+ parts.extend(trail)
return "\n".join(parts)
+def _gap_marker_reserve(
+ lines: list[str],
+ blocks: list[dict[str, Any]],
+ kept: set[int],
+ gap_marker: Callable[[int, int], str],
+ total_chars: int,
+ content_lines: list[str] | None = None,
+) -> int:
+ """Tokens the caller's gap markers will take for this keep-set: the number
+ of dropped spans that hold content (leading, internal, trailing) times the
+ size of a marker. Mirrors _render's gap rules."""
+ spans = sorted((b["start"], b["end"]) for b in blocks if b["id"] in kept)
+ if not spans:
+ return 0
+ n = len(lines)
+ probe_lines = content_lines if content_lines is not None else lines
+
+ def content(lo: int, hi: int) -> bool:
+ return any(ln.strip() for ln in probe_lines[lo:hi])
+
+ gaps = 1 if spans[0][0] > 0 and content(0, spans[0][0]) else 0
+ prev_end = spans[0][1]
+ for start, end in spans[1:]:
+ if start > prev_end + 1 and content(prev_end + 1, start):
+ gaps += 1
+ prev_end = max(prev_end, end)
+ if prev_end < n - 1 and content(prev_end + 1, n):
+ gaps += 1
+ if not gaps:
+ return 0
+ # One probe call sizes a marker at its widest (a span as long as the
+ # whole input); callers that count gap_marker calls see exactly one extra
+ # call in fixed mode, none in adaptive mode.
+ sample = str(gap_marker(total_chars, total_chars) or "")
+ return gaps * estimate_tokens(sample)
+
+
def cache_wrap(compressed: str, query: str) -> str:
return (
"<compressed_context version=\"1\">\n"
@@ -2449,8 +3173,9 @@ def sweep_ccr_cache(
) -> int:
"""Delete expired valid CCR records and return the removal count.
- Malformed or unreadable files are left untouched: retention maintenance is
- best-effort and must not destroy data it cannot validate.
+ Files that cannot be read or parsed are left untouched: retention
+ maintenance is best-effort. A file that parses but is not a valid record
+ (bad metadata, a non-object payload) is removed like an expired one.
"""
global _CCR_SWEEP_CURSOR
root = Path(ccr_dir)
@@ -2794,8 +3519,12 @@ def _summarise_with_llm(
return None
+# v1.4.0 (E2): the two class runs are bounded ({0,127}); the unbounded form
+# re-scanned the rest of a long separator-rich run (`a.a.a.…`, 20k chars) from
+# every word boundary — quadratic. Tokens up to 256 chars match as before.
_SUMMARY_STRUCTURED_TOKEN_RE = re.compile(
- r"\b(?:[A-Za-z][A-Za-z0-9_.:/-]*\d[A-Za-z0-9_.:/-]*|\d+(?:[.:/-]\d+)*)\b"
+ r"\b(?:[A-Za-z][A-Za-z0-9_.:/-]{0,127}\d[A-Za-z0-9_.:/-]{0,127}"
+ r"|\d+(?:[.:/-]\d+)*)\b"
)
@@ -2805,7 +3534,11 @@ def _summary_preserves_required_facts(
"""Conservative factual gate for the optional model-summary strategy."""
source_fold = source.casefold()
summary_fold = summary.casefold()
+ # Perf (v1.4.0, E2): `required` stays a list (order is part of the
+ # contract) but membership is answered by a set — `token not in list` made
+ # this loop quadratic (92 s on a 3.7 MB history).
required: list[str] = []
+ required_set: set[str] = set()
candidates = (
_SUMMARY_STRUCTURED_TOKEN_RE.findall(query or "")
+ _extract_entities(query or "")
@@ -2813,8 +3546,9 @@ def _summary_preserves_required_facts(
)
for item in candidates:
token = str(item).strip().casefold()
- if token and token in source_fold and token not in required:
+ if token and token in source_fold and token not in required_set:
required.append(token)
+ required_set.add(token)
# Values answering the question often appear only in the matching source
# line, not in the question itself (for example query host db-prod-01 and
@@ -2839,8 +3573,9 @@ def _summary_preserves_required_facts(
continue
for item in _SUMMARY_STRUCTURED_TOKEN_RE.findall(segment):
token = item.casefold()
- if token not in required:
+ if token not in required_set:
required.append(token)
+ required_set.add(token)
# Plain-text scalar answers are not necessarily identifiers (for
# example "paint color is ultraviolet"). Require the first content
# term after a queried field and a simple relation verb; do not require
@@ -2857,18 +3592,24 @@ def _summary_preserves_required_facts(
answer_terms = _extract_terms(match.group(1))
if answer_terms:
token = answer_terms[0].casefold()
- if token not in query_terms and token not in required:
+ if token not in query_terms and token not in required_set:
required.append(token)
+ required_set.add(token)
break
- kept = sum(1 for token in required if token in summary_fold)
- recall = kept / len(required) if required else 1.0
-
source_ids = {
token.casefold() for token in _SUMMARY_STRUCTURED_TOKEN_RE.findall(source)
}
summary_ids = {
token.casefold() for token in _SUMMARY_STRUCTURED_TOKEN_RE.findall(summary)
}
+ # Perf (v1.4.0): a required token that is itself a token of the summary is
+ # found by a set lookup; only the others pay the substring scan (which is
+ # what decides the same question, since a token of the summary is a
+ # substring of it). Same answer, linear when the summary kept its facts.
+ kept = sum(
+ 1 for token in required if token in summary_ids or token in summary_fold
+ )
+ recall = kept / len(required) if required else 1.0
no_novel_ids = summary_ids.issubset(source_ids)
return recall == 1.0 and no_novel_ids, recall
@@ -3046,7 +3787,7 @@ def _crush_json_items(payload: str, query: str, max_keep: int = 50) -> str | Non
"""
try:
data = json.loads(payload)
- except (json.JSONDecodeError, RecursionError):
+ except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
return None
# Find arrays to crush: top-level list, or dicts with list values
@@ -3195,6 +3936,363 @@ def inspect_compressibility(text: str, query: str) -> dict[str, Any]:
}
+# ---------------------------------------------------------------------------
+# v1.4.0 public helpers: brief_context / extract_evidence_lines
+#
+# Deterministic, stdlib-only, query-optional "what is in here" views for hosts
+# that need a small, honest stand-in for a big tool result (a plugin's last
+# rung after the query-aware extract was rejected or too big). Both return
+# ORIGINAL lines only — nothing is paraphrased — and both treat a level-tagged
+# error as the most important thing in a log.
+# ---------------------------------------------------------------------------
+
+_WARN_LEVEL_RE = re.compile(r"\b(?:WARN|WARNING)\b")
+_EVID_VERSION_RE = re.compile(r"\bv?\d+\.\d+(?:\.\d+)+\b")
+_EVID_SHA_RE = re.compile(r"\b(?=[0-9a-f]*\d)(?=[0-9a-f]*[a-f])[0-9a-f]{7,40}\b")
+_EVID_KV_RE = re.compile(r"\b[A-Za-z_][\w.-]{0,63}=\S")
+_EVID_FILE_RE = re.compile(
+ r"\b[\w-]{1,64}\.(?:py|js|ts|tsx|go|rs|java|c|h|cpp|json|ya?ml|toml|md|txt|log|sh|sql|cfg|ini)\b"
+)
+_EVIDENCE_LINE_CAP = 2000
+
+
+def _query_hit_terms(query: str) -> list[str]:
+ """Lowercase, deduplicated query terms worth searching lines for."""
+ if not query or _is_filler_query(query):
+ return []
+ terms: list[str] = []
+ seen: set[str] = set()
+ for term in [
+ t for t in distinctive_query_terms(query) if not t.startswith("script:")
+ ] + _topic_terms(query):
+ key = term.casefold()
+ if key and key not in seen:
+ seen.add(key)
+ terms.append(key)
+ return terms
+
+
+def brief_context(
+ text: str,
+ *,
+ budget_chars: int = 1200,
+ query: str = "",
+ gap_marker: Callable[[int, int], str] | None = None,
+) -> str:
+ """Deterministic, query-independent structural brief of ``text`` (v1.4.0).
+
+ Keeps ORIGINAL lines, in original order, within ``budget_chars`` (newlines
+ and gap markers included):
+
+ 1. the first ``h`` lines;
+ 2. every error-fingerprint exemplar line (one per fingerprint, see
+ :func:`error_fingerprints`);
+ 3. warning (WARN/WARNING) exemplars, one per template, if they fit;
+ 4. lines holding distinctive terms of ``query``, if one is given;
+ 5. the last ``t`` lines.
+
+ Head and tail are given about 30% of the budget each and the rest goes to
+ the exemplars; if the errors do not fit that way the head/tail shrink so
+ they do, and whatever budget the exemplars leave over goes back to the
+ head and tail. When the budget cannot hold everything, errors outrank
+ query-term lines, which outrank warnings.
+
+ Each omitted span is marked with ``gap_marker(omitted_chars, total_chars)``
+ — or ``[… N chars omitted …]`` when it is ``None`` — and a span of blank
+ lines only is not marked. ``gap_marker`` is called once per marker that is
+ emitted, plus one probe with ``(total_chars, total_chars)`` that sizes the
+ widest marker for the budget. A line is never cut, except one that is
+ itself as long as the budget: it is truncated, with a marker. ``text`` is
+ returned unchanged when it already fits. A ``gap_marker`` wrapped in
+ :class:`InliningGapMarker` keeps a dropped span verbatim instead when its
+ marker would not be shorter than the span (1.4.1).
+ """
+ text = text if isinstance(text, str) else str(text or "")
+ budget = max(1, int(budget_chars))
+ if len(text) <= budget:
+ return text
+ # v1.4.0 (E11): JSON arrays of objects get a valid-JSON brief, not a
+ # line-cut that would leave unparseable fragments.
+ if text.lstrip()[:1] in ("[", "{"):
+ briefed = json_brief(text, budget_chars=budget, query=query)
+ if briefed is not None:
+ return briefed
+ total = len(text)
+ lines = text.split("\n")
+ n = len(lines)
+
+ def mark(omitted: int) -> str:
+ if gap_marker is not None:
+ made = gap_marker(omitted, total)
+ return str(made) if made else ""
+ return f"[… {omitted} chars omitted …]"
+
+ # Markers are sized at the widest count a span can have: ONE probe call,
+ # gap_marker(total, total), before any real span is known. (Every later call
+ # is for a span that is actually emitted.)
+ marker_len = len(mark(total))
+
+ eff_cache: dict[int, str] = {}
+
+ def eff(i: int) -> str:
+ cached = eff_cache.get(i)
+ if cached is None:
+ line = lines[i]
+ if len(line) + 1 >= budget:
+ cut = max(1, budget // 3)
+ line = line[:cut] + mark(len(lines[i]) - cut)
+ eff_cache[i] = cached = line
+ return cached
+
+ nonblank = [0] * (n + 1)
+ for i, line in enumerate(lines):
+ nonblank[i + 1] = nonblank[i] + (1 if line.strip() else 0)
+
+ def has_content(lo: int, hi: int) -> int:
+ return 1 if nonblank[hi] - nonblank[lo] > 0 else 0
+
+ # Candidate lines by priority class (indices, original order).
+ err_idx: list[int] = []
+ warn_idx: list[int] = []
+ seen_err: set[str] = set()
+ seen_warn: set[str] = set()
+ for i, line in enumerate(lines):
+ if not line.strip():
+ continue
+ if _is_error_line(line):
+ fp = _error_fp(line)
+ if fp not in seen_err:
+ seen_err.add(fp)
+ err_idx.append(i)
+ elif _WARN_LEVEL_RE.search(line[:_RX_LINE_CAP]):
+ fp = _error_fp(line)
+ if fp not in seen_warn:
+ seen_warn.add(fp)
+ warn_idx.append(i)
+ hit_idx: list[int] = []
+ terms = _query_hit_terms(query)
+ if terms:
+ ranked: list[tuple[int, int]] = []
+ seen_hit: set[str] = set()
+ for i, line in enumerate(lines):
+ if not line.strip():
+ continue
+ low = line[:_RX_LINE_CAP].casefold()
+ hits = sum(1 for t in terms if _term_in_text(t, low))
+ if hits:
+ ranked.append((-hits, i))
+ for _, i in sorted(ranked):
+ fp = _error_fp(lines[i])
+ if fp not in seen_hit:
+ seen_hit.add(fp)
+ hit_idx.append(i)
+
+ def build(head_cap: int, tail_cap: int, errors_first: bool) -> tuple[list[int], bool]:
+ sel: list[int] = []
+ chosen_set: set[int] = set()
+ state = {"raw": 0, "gaps": 0}
+
+ def gap_delta(p: int | None, q: int | None, i: int) -> int:
+ if p is None and q is None:
+ return has_content(0, i) + has_content(i + 1, n)
+ if p is None:
+ return has_content(0, i) + has_content(i + 1, q) - has_content(0, q)
+ if q is None:
+ return has_content(p + 1, i) + has_content(i + 1, n) - has_content(p + 1, n)
+ return has_content(p + 1, i) + has_content(i + 1, q) - has_content(p + 1, q)
+
+ def try_add(i: int) -> bool:
+ if i in chosen_set:
+ return True
+ pos = bisect.bisect_left(sel, i)
+ p = sel[pos - 1] if pos > 0 else None
+ q = sel[pos] if pos < len(sel) else None
+ raw = state["raw"] + len(eff(i)) + 1
+ gaps = state["gaps"] + gap_delta(p, q, i)
+ if raw - 1 + gaps * (marker_len + 1) > budget:
+ return False
+ sel.insert(pos, i)
+ chosen_set.add(i)
+ state["raw"], state["gaps"] = raw, gaps
+ return True
+
+ def add_head() -> None:
+ used = 0
+ for i in range(n):
+ if not lines[i].strip():
+ continue
+ c = len(eff(i)) + 1
+ if i > 0 and used + c > head_cap:
+ break
+ if not try_add(i):
+ break
+ used += c
+
+ def add_tail() -> None:
+ used = 0
+ for i in range(n - 1, -1, -1):
+ if i in chosen_set and used: # met the head
+ break
+ if not lines[i].strip():
+ continue
+ c = len(eff(i)) + 1
+ if used + c > tail_cap:
+ break
+ if not try_add(i):
+ break
+ used += c
+
+ def add_exemplars() -> bool:
+ all_errors = True
+ for i in err_idx:
+ if not try_add(i):
+ all_errors = False
+ # When the budget cannot hold everything, lines that answer the
+ # caller's query outrank warning exemplars.
+ for group in (hit_idx, warn_idx):
+ for i in group:
+ try_add(i)
+ return all_errors
+
+ def fill_edges() -> None:
+ """Spend what is left on more head and tail, alternately."""
+ hi, ti = 0, n - 1
+ head_open = tail_open = True
+ while (head_open or tail_open) and hi <= ti:
+ if head_open:
+ while hi < n and (hi in chosen_set or not lines[hi].strip()):
+ hi += 1
+ if hi >= n or not try_add(hi):
+ head_open = False
+ if tail_open:
+ while ti >= 0 and (ti in chosen_set or not lines[ti].strip()):
+ ti -= 1
+ if ti < 0 or not try_add(ti):
+ tail_open = False
+
+ if errors_first:
+ all_errors = add_exemplars()
+ add_head()
+ add_tail()
+ else:
+ add_head()
+ add_tail()
+ all_errors = add_exemplars()
+ fill_edges()
+ return sel, all_errors
+
+ chosen: list[int] = []
+ for share, errors_first in ((0.30, False), (0.15, False), (0.0, True)):
+ cap = int(budget * share)
+ chosen, all_errors = build(cap, cap, errors_first)
+ if all_errors:
+ break
+
+ # 1.4.1 (fix-all): an InliningGapMarker gets the dropped lines verbatim
+ # when its marker would not be shorter than them, as in compress_context.
+ inline_short = _inlines_short(gap_marker)
+
+ def gap_parts(lo: int, hi: int) -> list[str]:
+ if not has_content(lo, hi):
+ return []
+ span_chars = _span_chars(lines, lo, hi)
+ made = mark(span_chars)
+ if not made:
+ return []
+ if inline_short and len(made) >= span_chars:
+ return lines[lo:hi]
+ return [made]
+
+ parts: list[str] = []
+ prev = -1
+ for i in chosen:
+ lo = prev + 1
+ if prev < 0 and i > 0:
+ parts.extend(gap_parts(0, i))
+ elif prev >= 0 and i > prev + 1:
+ parts.extend(gap_parts(lo, i))
+ parts.append(eff(i))
+ prev = i
+ if chosen and prev < n - 1:
+ parts.extend(gap_parts(prev + 1, n))
+ out = "\n".join(parts)
+ if not out:
+ # Not even one line (with its marker) fits a budget this small: the
+ # plain prefix is the only honest brief left.
+ return text[:budget]
+ if len(out) > budget: # only a gap_marker that outgrew its probe can get here
+ out = out[:budget]
+ return out
+
+
+def extract_evidence_lines(text: str, query: str = "", limit: int = 20) -> list[str]:
+ """Exact original lines most likely to matter, deduplicated, capped (v1.4.0).
+
+ Ranking, most important first:
+
+ 1. error exemplars — one line per :func:`error_fingerprints` fingerprint;
+ 2. lines holding terms of ``query`` (more distinct terms first);
+ 3. identifier-dense lines — at least two of: file paths, versions, SHAs,
+ ``key=value`` pairs.
+
+ Lines are returned unmodified, in original order, at most ``limit`` of
+ them. Lines that differ only in volatile fields (ids, numbers, timestamps)
+ count as duplicates, so a thousand repeats cannot fill the list. Blank
+ lines are never returned. Deterministic.
+ """
+ if not text or limit <= 0:
+ return []
+ lines = _norm_newlines(text).split("\n")
+ picked: set[int] = set()
+ seen_tpl: set[str] = set()
+
+ def take(i: int) -> bool:
+ if len(picked) >= limit:
+ return False
+ fp = _error_fp(lines[i])
+ if fp in seen_tpl or i in picked:
+ return True
+ seen_tpl.add(fp)
+ picked.add(i)
+ return True
+
+ for i, line in enumerate(lines):
+ if line.strip() and _is_error_line(line):
+ if not take(i):
+ break
+ terms = _query_hit_terms(query)
+ if terms and len(picked) < limit:
+ ranked = []
+ for i, line in enumerate(lines):
+ if not line.strip() or i in picked:
+ continue
+ low = line[:_RX_LINE_CAP].casefold()
+ hits = sum(1 for t in terms if _term_in_text(t, low))
+ if hits:
+ ranked.append((-hits, i))
+ for _, i in sorted(ranked):
+ if not take(i):
+ break
+ if len(picked) < limit:
+ dense = []
+ for i, line in enumerate(lines):
+ if not line.strip() or i in picked or len(line) > _EVIDENCE_LINE_CAP:
+ continue
+ score = (
+ len(_PATH_RE.findall(line))
+ + len(_EVID_FILE_RE.findall(line))
+ + len(_EVID_VERSION_RE.findall(line))
+ + len(_EVID_SHA_RE.findall(line))
+ + len(_EVID_KV_RE.findall(line))
+ )
+ if score >= 2:
+ dense.append((-score, i))
+ for _, i in sorted(dense):
+ if not take(i):
+ break
+ return [lines[i] for i in sorted(picked)]
+
+
def compress_context(
context: str,
query: str | Iterable[str],
@@ -3221,12 +4319,70 @@ def compress_context(
summary_allow_remote: bool | None = None,
limits: IndustrialLimits | None = None,
derive_query: bool = False,
+ gap_marker: Callable[[int, int], str] | None = None,
+ recursion_markers: Iterable[str] = (),
+ content_hint: str | None = None,
+ inline_short_gaps: bool = False,
) -> CompressResult:
+ """Query-aware extractive compression of ``context``.
+
+ New in 1.4.0 (all optional, defaults keep 1.3.x behaviour):
+
+ ``gap_marker(omitted_chars, total_chars) -> str``
+ Replaces every gap marker the renderer emits when ``citations=False``
+ — including a leading gap (kept text starts after line 0) and a
+ trailing gap — with the caller's own marker text. ``omitted_chars`` is
+ the length of the dropped span (its lines joined with ``"\\n"``),
+ ``total_chars`` the length of ``context``. Adjacent dropped blocks
+ yield exactly one marker. ``None`` keeps the historical ``[…]``.
+ ``recursion_markers``
+ Extra strings that mark input as already compressed: if any line of
+ the input starts with one (after ``lstrip``) the call is a no-op with
+ ``policy_name="local-noop-recursion"``. Matching is explicit and
+ anchored; nothing is guessed (a host's own marker text only counts if
+ the caller passes it).
+ ``content_hint``
+ One of ``None, "code", "numbered_code", "log", "json", "text",
+ "diff", "grep", "test"`` (anything else raises ``ValueError``).
+ ``"code"``/``"numbered_code"`` skip the CSV/TSV/YAML paths and route
+ as code so function bodies survive (``"log"`` skips them too: a log is
+ never a table); ``"numbered_code"`` additionally
+ means each line carries a ``N\\t`` or ``N|`` gutter, which stays in the
+ output and is ignored for scoring. ``"log"``/``"json"``/``"text"``
+ force that routing. ``"diff"``/``"grep"``/``"test"`` are recorded in
+ the receipt and otherwise behave like ``None``. The hint is always
+ recorded in ``receipt["content_hint"]``.
+
+ ``inline_short_gaps`` (1.4.1)
+ With ``gap_marker`` set, a dropped span whose marker would not be
+ shorter than the span itself is kept verbatim instead. Off by
+ default: 1.4.0 output is unchanged byte for byte.
+ """
caller_text = str(context or "")
if not isinstance(query, str):
# PAACE-style plan awareness: a sequence of upcoming-task strings is
# scored as a union — a term from any planned step counts.
query = " ".join(str(part) for part in (query or []) if part)
+ if content_hint is not None and (
+ not isinstance(content_hint, str) or content_hint not in _CONTENT_HINTS
+ ):
+ raise ValueError(
+ f"unknown content_hint {content_hint!r} "
+ f"(expected None or one of {', '.join(sorted(_CONTENT_HINTS))})"
+ )
+ if gap_marker is not None and not callable(gap_marker):
+ raise TypeError("gap_marker must be callable as gap_marker(omitted_chars, total_chars)")
+ if inline_short_gaps and gap_marker is not None and not _inlines_short(gap_marker):
+ gap_marker = InliningGapMarker(gap_marker)
+ code_hint = content_hint in _CODE_HINTS
+ no_table = content_hint in _NO_TABLE_HINTS
+ recursion_markers = _marker_tuple(recursion_markers) # a generator is read once
+ # v1.4.0 (E6): a reply made only of conversational filler ("continue",
+ # "thanks", "go ahead", "fix it", "OK") is an EMPTY query on every path.
+ # The receipt/cache wrapper still show the caller's words.
+ query_as_given = query
+ if _is_filler_query(query):
+ query = ""
# TPC-style derived objective (opt-in): an empty/generic query normally
# fails open. With derive_query=True the engine first tries to infer a
# conservative task descriptor from the document's own recurring rare
@@ -3263,7 +4419,9 @@ def compress_context(
# back as input would nest wrappers and could drop the CCR pointer
# that makes earlier drops recoverable. Refuse to compact our own
# output — the caller already has the compacted form.
- if any(marker in caller_text for marker in _RECURSION_MARKERS):
+ if any(marker in caller_text for marker in _RECURSION_MARKERS) or (
+ _has_anchored_marker(caller_text, recursion_markers)
+ ):
tokens = estimate_tokens(caller_text)
return CompressResult(
compressed_text=caller_text,
@@ -3283,7 +4441,29 @@ def compress_context(
secret_in_input = _contains_secret(caller_text)
savings_gate = False
degraded = False
- industrial = industrial_preprocess(caller_text, query or "", limits)
+ # content_hint code/numbered_code (and log): the caller says what this is,
+ # so the format adapters (CSV/TSV/YAML row and subtree selection, which
+ # treat code with commas or colons — or a log with "12:00:00,123"
+ # timestamps — as a table) must not run. An empty selector query makes the
+ # adapter decline; the query-size limit is still enforced by passing the
+ # real query when it is over the limit.
+ industrial_query = query or ""
+ if no_table and len(industrial_query) <= (limits or IndustrialLimits()).max_query_chars:
+ industrial_query = ""
+ # v1.4.0 (E8 completion): adapter cuts carry the caller's marker; the
+ # total they report is always the caller's original length.
+ adapter_gap = (
+ None
+ if gap_marker is None
+ else (
+ InliningGapMarker(lambda omitted, _total: gap_marker(omitted, len(caller_text)))
+ if _inlines_short(gap_marker)
+ else (lambda omitted, _total: gap_marker(omitted, len(caller_text)))
+ )
+ )
+ industrial = industrial_preprocess(
+ caller_text, industrial_query, limits, gap_marker=adapter_gap
+ )
if (
industrial.hard_fail_open
and degraded_view
@@ -3336,10 +4516,11 @@ def compress_context(
"engine": "tameru",
"policy": "local-fail-open",
"query_hash": hashlib.sha256(
- (query or "").encode("utf-8")
+ (query_as_given or "").encode("utf-8")
).hexdigest()[:12],
"savings_pct": 0.0,
"risk": "high",
+ "content_hint": content_hint,
"industrial": industrial.to_dict(),
},
)
@@ -3359,8 +4540,61 @@ def compress_context(
original_text = _norm_newlines(caller_text)
industrial_text = industrial.text if industrial.applied else caller_text
text = strip_ansi(_norm_newlines(industrial_text))
- text = unwrap_hermes_tool(text)
- text = preprocess_test_runner(text, query or "")
+ # v1.4.0 (G9): keep the wrapper's scalar metadata (exit_code, error,
+ # total_lines, ...) so it can be re-emitted ahead of the compressed body.
+ tool_payload = unwrap_tool_payload(text)
+ text = tool_payload.inner
+ tool_meta_line = ""
+ if tool_payload.wrapped and tool_payload.meta:
+ tool_meta_line = _compact_json(tool_payload.meta) + "\n"
+ # v1.4.0 (E4): an un-hinted numbered read (`cat -n`, Hermes `N|`) is code,
+ # judged on the unwrapped payload: no table detector, no supersession, the
+ # gutter kept in the output but out of the scorer's view. `route_hint` is
+ # what the engine routes by; the receipt still records the caller's hint.
+ # A "code" hint on a numbered read means the same thing: the gutter must
+ # not count as code (it made blank source lines non-blank and split
+ # function bodies at the cut).
+ route_hint = content_hint
+ if route_hint in (None, "code") and is_numbered_code(text):
+ route_hint = "numbered_code"
+ code_hint = True
+ no_table = True
+ _pre_test_text = text
+ text = preprocess_test_runner(
+ text, query or "", content_hint=content_hint, gap_marker=adapter_gap
+ )
+ # v1.4.0 (E5): diff and grep/rg output get their own adapters (whole hunks,
+ # grouped matches) ahead of generic segmentation. The adapter's output is
+ # the selection: scoring below must not undo it (see `adapter_info`).
+ adapter_info: dict[str, Any] | None = None
+ if (
+ text is not _pre_test_text
+ and content_hint in (None, "test")
+ and len(text) <= _TEST_REDUCTION_FINAL_CHARS
+ and detect_test_runner(_pre_test_text) is not None
+ ):
+ # Same for a reduced test run (v1.4.0): block scoring must not drop
+ # the failing test's name/file line that the reduction kept (jest
+ # "FAIL f.test.js" / "● suite › case" vs "which tests failed?").
+ # Larger reductions still go through scoring and its budget.
+ adapter_info = {
+ "name": "test_output",
+ "total_records": None,
+ "kept_records": None,
+ "reason": "failure blocks and summaries kept",
+ }
+ if (
+ adapter_info is None
+ and route_hint not in _CODE_HINTS
+ and route_hint not in ("log", "json", "text")
+ ):
+ text, adapter_info = apply_agent_adapters(
+ text,
+ query or "",
+ hint=content_hint,
+ gap_marker=adapter_gap,
+ weak_query=not query_has_distinctive_selectors(query or ""),
+ )
if not text.strip():
return CompressResult(
compressed_text="", policy_name="noop", mode=requested_mode
@@ -3370,10 +4604,13 @@ def compress_context(
# Destructive preprocess (JSON crush / later log collapse) only when
# the query names something specific. Generic/empty queries must not
# delete array tails or fingerprint-collapse logs before scoring.
- if query_has_distinctive_selectors(query or ""):
+ if query_has_distinctive_selectors(query or "") and not code_hint and not adapter_info:
+ # (content_hint code/numbered_code: source code with commas or JSON
+ # literals is not a table to crush; function bodies must survive.)
if route_content_type(_norm_newlines(text).split("\n")) != "json":
text = _preprocess_json(text, query or "")
- text = preprocess_csv(text, query or "")
+ if not no_table:
+ text = preprocess_csv(text, query or "", gap_marker=adapter_gap)
text = preprocess_filler_comments(text, query or "")
# Strategy ladder:
@@ -3415,13 +4652,16 @@ def compress_context(
degraded_view=degraded_view,
limits=limits,
derive_query=derive_query,
+ gap_marker=gap_marker,
+ recursion_markers=recursion_markers,
+ content_hint=content_hint,
)
- base = compress_context(caller_text, query, strategy="extract", **ladder_kwargs)
+ base = compress_context(caller_text, query_as_given, strategy="extract", **ladder_kwargs)
if not base.fail_open:
return base
return compress_context(
caller_text,
- query,
+ query_as_given,
strategy="summarise",
summary_endpoint=summary_endpoint,
summary_models=summary_models,
@@ -3463,7 +4703,7 @@ def compress_context(
ccr_info = None
if ccr_info is not None:
ccr_marker = f"\n[CC-Retrieve: {ccr_info['hash']}]"
- result_text = summary + ccr_marker
+ result_text = tool_meta_line + summary + ccr_marker
original_tokens = estimate_tokens(original_text)
kept_tokens = estimate_tokens(result_text)
keep_ratio = kept_tokens / max(1, original_tokens)
@@ -3506,10 +4746,11 @@ def compress_context(
"engine": "tameru",
"policy": "summarise-llm",
"query_hash": hashlib.sha256(
- (query or "").encode("utf-8")
+ (query_as_given or "").encode("utf-8")
).hexdigest()[:12],
"savings_pct": round((1.0 - keep_ratio) * 100.0, 2),
"risk": summary_risk,
+ "content_hint": content_hint,
"industrial": industrial.to_dict(),
**_receipt_hashes(
caller_text,
@@ -3525,14 +4766,23 @@ def compress_context(
# LLM failed or returned a longer result — fall through to extract.
strategy_norm = "extract"
- lines, content_type = preprocess(text, query or "")
- if industrial.applied:
- content_type = industrial.profile.format
- if not query_has_distinctive_selectors(query or ""):
+ if query_has_distinctive_selectors(query or "") and not adapter_info:
+ lines, content_type = preprocess(text, query or "", content_hint=route_hint)
+ if industrial.applied:
+ content_type = industrial.profile.format
+ else:
# Do not fingerprint-collapse logs or crush JSON on a generic query.
+ # (Perf, v1.4.0: preprocess() output was computed and then discarded
+ # on this path — ~0.4 s of log collapsing per 650 KB — so it is not
+ # run at all; the result is identical.)
lines = text.split("\n")
- content_type = route_content_type(lines)
- blocks = segment_blocks(lines)
+ content_type = _HINT_ROUTES.get(route_hint) or route_content_type(lines)
+ # numbered_code: the "N\t" / "N|" gutter stays in the output (_render emits
+ # `lines`) but is not part of the code the scorer should read.
+ score_lines = lines
+ if route_hint == "numbered_code":
+ score_lines = [_GUTTER_RE.sub("", ln, count=1) for ln in lines]
+ blocks = segment_blocks(score_lines)
if len(blocks) > industrial.limits.max_blocks:
tokens = estimate_tokens(caller_text)
reason = (
@@ -3544,10 +4794,11 @@ def compress_context(
"engine": "tameru",
"policy": "local-fail-open",
"query_hash": hashlib.sha256(
- (query or "").encode("utf-8")
+ (query_as_given or "").encode("utf-8")
).hexdigest()[:12],
"savings_pct": 0.0,
"risk": "high",
+ "content_hint": content_hint,
"industrial": industrial.to_dict(),
}
receipt.update(
@@ -3592,7 +4843,7 @@ def compress_context(
f"[… {omitted} chars omitted …]\n"
f"{full[-_DEGRADED_VIEW_TAIL:]}"
)
- scored = score_blocks(blocks, query or "")
+ scored = score_blocks(blocks, query or "", content_type=content_type)
# v0.10.0 (G2, KVzip sink semantics): pinned blocks are exempt from
# dropping in every selector path. Applied by boosting score and marking;
@@ -3633,6 +4884,17 @@ def compress_context(
if not (0.0 < ratio <= 1.0):
raise ValueError(f"budget_ratio must be in (0, 1], got {ratio!r}")
kept, fail_open, risk = select_fixed(scored, ratio, out=selection)
+ if gap_marker is not None and not citations and not fail_open:
+ # Marker text costs tokens too: reserve them out of the budget and
+ # select once more (gap count may shift slightly; one pass is
+ # enough to keep the rendered output near the requested ratio).
+ reserve = _gap_marker_reserve(
+ lines, scored, kept, gap_marker, len(caller_text), content_lines=score_lines
+ )
+ if reserve:
+ kept, fail_open, risk = select_fixed(
+ scored, ratio, out=selection, reserve_tokens=reserve
+ )
else:
kept, fail_open, risk = select_adaptive(
scored,
@@ -3648,12 +4910,32 @@ def compress_context(
# Temporal supersession: a later kept block that explicitly marks an
# earlier kept block stale (now/obsolete/override/newer date) prunes
# it after cache replay, so a frozen keep cannot revive stale data.
- kept = apply_supersession(scored, kept)
+ # v1.4.0: not for content the caller says is source code. The markers
+ # are prose ("override", "deprecated", " disabled" are ordinary code
+ # words) and two functions share identifiers by construction, so a
+ # comment in a LATER function would evict the very function asked
+ # about — and the hint exists so function bodies survive.
+ if not code_hint:
+ kept = apply_supersession(scored, kept)
+ # v1.4.0 (E3) safety net: neither a frozen drop nor supersession may
+ # evict the one exemplar block of a level-tagged log error.
+ kept |= {
+ b["id"]
+ for b in scored
+ if b.get("error_exemplar") and not b.get("trust_risk")
+ }
if pin_recent_ids:
# Pins are never touched: a score-999 boost alone cannot reach
# kept when a selector path ignores raw scores (needle-only),
# and neither freeze nor supersession may evict a pin.
kept |= pin_recent_ids
+ if adapter_info is not None and scored and not ambiguity_fail_open:
+ # v1.4.0 (E5): the format adapter already chose what to keep (every
+ # header, whole hunks, grouped matches); block scoring must not drop a
+ # file header or half a hunk. Risk reflects how the adapter chose.
+ kept = {b["id"] for b in scored}
+ fail_open = False
+ risk = "low" if query_has_distinctive_selectors(query or "") else "medium"
if ambiguity_fail_open:
fail_open = True
risk = "high"
@@ -3675,11 +4957,16 @@ def compress_context(
if not any(t in blob for t in latin_terms):
fail_open = True
risk = "high"
- if not query_has_distinctive_selectors(q) and _looks_like_csv(original_text):
+ if (
+ not query_has_distinctive_selectors(q)
+ and not no_table
+ and _looks_like_csv(original_text)
+ ):
fail_open = True
risk = "high"
collapsed = "\n".join(lines)
+ structural_collapse_only = False
if fail_open:
annotated_ids = {
b["id"] for b in scored if b.get("trust_risk") and not b.get("pinned")
@@ -3689,7 +4976,9 @@ def compress_context(
# annotation. Explicitly pinned blocks remain eligible.
kept = {b["id"] for b in scored if b["id"] not in annotated_ids}
compressed = _render(
- lines, scored, kept, citations=citations, reorder_best=_reorder
+ lines, scored, kept, citations=citations, reorder_best=_reorder,
+ gap_marker=gap_marker, total_chars=len(caller_text),
+ content_lines=score_lines,
)
fail_open = False
risk = "high"
@@ -3704,11 +4993,19 @@ def compress_context(
fail_open = False
compressed = collapsed
kept = {b["id"] for b in scored}
+ # v1.4.0 (E7): the query never selected anything here; what is
+ # returned is the structural collapse alone (repeat folding,
+ # frame runs), not an answer-driven extract. Say so.
+ structural_collapse_only = True
else:
compressed = original_text
kept = {b["id"] for b in scored}
else:
- compressed = _render(lines, scored, kept, citations=citations, reorder_best=_reorder)
+ compressed = _render(
+ lines, scored, kept, citations=citations, reorder_best=_reorder,
+ gap_marker=gap_marker, total_chars=len(caller_text),
+ content_lines=score_lines,
+ )
recall = _entity_recall(text, compressed, query or "")
if recall < 0.66 and not fail_open:
@@ -3721,7 +5018,11 @@ def compress_context(
kept.add(b["id"])
if decision_cache is not None:
kept = _enforce_frozen_decisions(scored, kept)
- compressed = _render(lines, scored, kept, citations=citations, reorder_best=_reorder)
+ compressed = _render(
+ lines, scored, kept, citations=citations, reorder_best=_reorder,
+ gap_marker=gap_marker, total_chars=len(caller_text),
+ content_lines=score_lines,
+ )
recall = _entity_recall(text, compressed, query or "")
trust_risks = {
b["id"]
@@ -3736,7 +5037,11 @@ def compress_context(
kept = {bid for bid in kept if bid not in trust_risks}
if decision_cache is not None:
kept = _enforce_frozen_decisions(scored, kept)
- compressed = _render(lines, scored, kept, citations=citations, reorder_best=_reorder)
+ compressed = _render(
+ lines, scored, kept, citations=citations, reorder_best=_reorder,
+ gap_marker=gap_marker, total_chars=len(caller_text),
+ content_lines=score_lines,
+ )
risk = "high"
elif recall < 0.5:
compressed = original_text
@@ -3748,6 +5053,30 @@ def compress_context(
risk = "high"
compressed = caller_text
+ # v1.4.0 (E3): for content routed as a log, report how many level-tagged
+ # error fingerprints survive. The engine keeps one exemplar per
+ # fingerprint, so a loss means something upstream broke the invariant:
+ # say so in the risk instead of leaving the verifier blind to it.
+ log_error_retention: dict[str, int] | None = None
+ if content_type == "log" and not fail_open and compressed:
+ want = {
+ fp for fp, ex in error_fingerprints(text).items() if _is_level_error_line(ex)
+ }
+ if want:
+ have = set(error_fingerprints(compressed))
+ log_error_retention = {"total": len(want), "kept": len(want & have)}
+ if log_error_retention["kept"] < log_error_retention["total"]:
+ lost_all = log_error_retention["kept"] == 0
+ risk = "high" if lost_all else (risk if risk == "high" else "medium")
+
+ # v1.4.0 (G9): a Hermes tool wrapper ({"output": ..., "exit_code": 1, ...})
+ # was reduced to its inner text above; re-emit its scalar metadata as one
+ # compact JSON line so exit codes, errors and line counts survive. Only for
+ # a compressed (not fail-open) result — fail-open returns the caller's
+ # wrapper byte for byte.
+ if tool_meta_line and not fail_open:
+ compressed = tool_meta_line + compressed
+
original_tokens = estimate_tokens(original_text)
kept_tokens = estimate_tokens(compressed)
trust_filtered = any(
@@ -3806,10 +5135,19 @@ def compress_context(
reasons = sorted({scored[i]["reason"] for i in kept if i < len(scored)})
if industrial.applied:
reasons.append(f"industrial adapter: {industrial.profile.format}")
+ if adapter_info is not None and not fail_open:
+ reasons.append(f"format adapter: {adapter_info['name']}")
if freeze_cache_saturated:
reasons.append("freeze cache capacity reached")
if savings_gate:
reasons.append("savings below min_savings_ratio")
+ if structural_collapse_only and not fail_open:
+ reasons.append("structural collapse only (query terms absent)")
+ if log_error_retention and log_error_retention["kept"] < log_error_retention["total"]:
+ reasons.append(
+ f"log error fingerprints lost: {log_error_retention['kept']}"
+ f"/{log_error_retention['total']} kept"
+ )
if degraded:
reasons.append("degraded scoring view")
if risk == "low":
@@ -3820,7 +5158,7 @@ def compress_context(
result_text = compressed
cache_applied = False
if cache_prefix and not fail_open:
- result_text = cache_wrap(compressed, query or "")
+ result_text = cache_wrap(compressed, query_as_given or "")
cache_applied = True
ccr_info = None
@@ -3871,7 +5209,15 @@ def compress_context(
# a different strategy or expand dropped blocks.
verifier = None
if not fail_open and compressed and original_tokens > 50:
- verifier = verify_compression(text, compressed, query or "")
+ if route_hint == "numbered_code":
+ # the gutter ("12: ") is not content: it must not read as key: value
+ verifier = verify_compression(
+ "\n".join(_GUTTER_RE.sub("", ln, count=1) for ln in text.split("\n")),
+ "\n".join(_GUTTER_RE.sub("", ln, count=1) for ln in compressed.split("\n")),
+ query or "",
+ )
+ else:
+ verifier = verify_compression(text, compressed, query or "")
risk_order = {"low": 0, "medium": 1, "high": 2}
verifier_risk = str(verifier.get("risk", "high"))
if risk_order.get(verifier_risk, 2) > risk_order.get(risk, 2):
@@ -3903,7 +5249,7 @@ def compress_context(
)
# v0.10.0 (G5 memorix receipt): machine-readable provenance.
- query_hash = hashlib.sha256((query or "").encode("utf-8")).hexdigest()[:12]
+ query_hash = hashlib.sha256((query_as_given or "").encode("utf-8")).hexdigest()[:12]
receipt = {
"schema_version": "1",
"engine": "tameru",
@@ -3931,6 +5277,14 @@ def compress_context(
# not supplied — a derived query is advisory context, not intent.
"query_source": "derived" if derived_terms else "caller",
"derived_terms": derived_terms,
+ # v1.4.0: E7 — the keep-set came from structural collapse alone.
+ "structural_collapse_only": bool(structural_collapse_only and not fail_open),
+ # v1.4.0: E8 — the caller's routing hint, always recorded.
+ "content_hint": content_hint,
+ # v1.4.0: E5 — which diff/grep adapter shaped the text (None: none did).
+ "adapter": adapter_info,
+ # v1.4.0: E3 — level-tagged error fingerprints kept/total (logs only).
+ "log_error_fingerprints": log_error_retention,
"industrial": industrial.to_dict(),
}
receipt.update(
diff --git a/plugins/context_engine/tameru/folding.py b/plugins/context_engine/tameru/folding.py
new file mode 100644
index 00000000000..95ccbd30e0b
--- /dev/null
+++ b/plugins/context_engine/tameru/folding.py
@@ -0,0 +1,476 @@
+"""Lossless folding, log-template masking and JSON briefs (v1.4.0).
+
+Three small, stdlib-only, deterministic helpers that ``compress_context`` wires
+in:
+
+* :func:`fold_lossless` (E9) - a transformation that loses no information a
+ reader could see: terminal redraws, ANSI colour, trailing blanks, runs of
+ identical lines and pretty-printed JSON.
+* :func:`mask_template` (E10) - the ONE masking function behind both the log
+ template fingerprint and the error fingerprint, so the two never disagree.
+* :func:`json_brief` (E11) - a valid-JSON structural brief of a big array of
+ objects.
+
+Every regex here is linear: bounded character classes, no nested quantifiers.
+"""
+from __future__ import annotations
+
+import json
+import re
+from collections import Counter
+from typing import Any
+
+__all__ = ["fold_lossless", "mask_template", "json_brief"]
+
+# --------------------------------------------------------------------------
+# E9: fold_lossless
+# --------------------------------------------------------------------------
+
+_ANSI_RE = re.compile(
+ r"\x1b\[[0-?]*[ -/]*[@-~]" # CSI (colours, cursor moves, erase)
+ r"|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)" # OSC (titles, hyperlinks)
+ r"|\x1b[@-Z\\-_]" # two-byte escapes
+)
+_RUN_MIN = 3
+_ANSI_PASSES = 4
+# The marker this module writes. A literal marker-looking line is never run-
+# collapsed: otherwise ``a a a a`` (-> ``a`` + marker) followed by two literal
+# marker lines would fold again on the next pass (not idempotent).
+_RUN_MARKER_RE = re.compile(r"\[×\d+ identical lines\]")
+
+
+def _strip_ansi_line(ln: str) -> tuple[str, int]:
+ """Strip ANSI sequences to a fixed point (idempotence).
+
+ Removing one escape can splice the halves of another into a new one
+ (``ESC[3 ESC[0m 1m`` -> ``ESC[31m``), so a single pass is not idempotent.
+ Passes are capped (adversarial nesting would be quadratic); if the cap is
+ hit, the remaining stray ESC bytes are dropped, which cannot create a new
+ sequence since every sequence starts with ESC.
+ """
+ total = 0
+ for _ in range(_ANSI_PASSES):
+ ln, k = _ANSI_RE.subn("", ln)
+ total += k
+ if not k:
+ return ln, total
+ if _ANSI_RE.search(ln):
+ ln = ln.replace("\x1b", "")
+ return ln, total
+
+
+def _fold_text_steps(text: str, stats: dict) -> list[str]:
+ """CR redraws, ANSI, trailing whitespace -> list of lines."""
+ lines = text.split("\n")
+ cr = 0
+ for i, ln in enumerate(lines):
+ if "\r" in ln:
+ # Trailing \r's belong to the line ending (\r\n, or a bare \r
+ # that redraws nothing): they must not erase the line's text
+ # ("abc\r\r\n" shows "abc").
+ body = ln.rstrip("\r")
+ if "\r" in body:
+ body = body.rsplit("\r", 1)[1]
+ cr += 1
+ lines[i] = body
+ stats["cr_redraws"] = cr
+ if "\x1b" in text:
+ ansi = 0
+ for i, ln in enumerate(lines):
+ if "\x1b" in ln:
+ lines[i], k = _strip_ansi_line(ln)
+ ansi += k
+ stats["ansi"] = ansi
+ ws = 0
+ for i, ln in enumerate(lines):
+ stripped = ln.rstrip()
+ if stripped != ln:
+ lines[i] = stripped
+ ws += 1
+ stats["trailing_ws"] = ws
+ return lines
+
+
+def _fold_runs(lines: list[str], stats: dict) -> list[str]:
+ """Collapse >=3 identical consecutive lines when that is strictly shorter."""
+ out: list[str] = []
+ n = len(lines)
+ i = 0
+ collapsed = 0
+ while i < n:
+ j = i + 1
+ while j < n and lines[j] == lines[i]:
+ j += 1
+ run = j - i
+ if run >= _RUN_MIN and not _RUN_MARKER_RE.fullmatch(lines[i]):
+ marker = f"[×{run - 1} identical lines]"
+ if len(lines[i]) + 1 + len(marker) < run * len(lines[i]) + run - 1:
+ out.append(lines[i])
+ out.append(marker)
+ collapsed += 1
+ i = j
+ continue
+ out.extend(lines[i:j])
+ i = j
+ stats["runs_collapsed"] = collapsed
+ return out
+
+
+_JSON_STRING_OR_WS_RE = re.compile(r'("[^"\\]*(?:\\.[^"\\]*)*")|[ \t\r\n]+')
+
+
+def _json_minified(text: str) -> str | None:
+ """``text`` with the insignificant whitespace removed, when it is JSON and
+ that is strictly shorter.
+
+ Only whitespace outside string literals is removed, so every number
+ literal, string escape, key order and duplicate key stays byte for byte
+ (re-serialising would turn ``"\\ud800"`` into a lone surrogate, ``"\\u202e"``
+ into a raw bidi override, ``1.10`` into ``1.1`` and ``-0`` into ``0``).
+ """
+ head = text.lstrip()[:1]
+ if head not in ("{", "["):
+ return None
+ try:
+ # a number that overflows to inf (1e400) is not minified, as before
+ json.loads(text, parse_float=_finite_float)
+ except (ValueError, RecursionError):
+ return None
+ mini = _JSON_STRING_OR_WS_RE.sub(lambda m: m.group(1) or "", text)
+ return mini if len(mini) < len(text) else None
+
+
+def _finite_float(literal: str) -> float:
+ value = float(literal)
+ if value in (float("inf"), float("-inf")):
+ raise ValueError("non-finite number")
+ return value
+
+
+def fold_lossless(text: str) -> tuple[str, dict]:
+ """Fold ``text`` without losing information; return ``(folded, stats)``.
+
+ 1. per line, keep only what follows the last ``\\r`` that is not part of a
+ ``\\r\\n`` (a terminal shows nothing else);
+ 2. strip ANSI escape sequences;
+ 3. strip trailing whitespace of each line;
+ 4. a run of >=3 identical consecutive lines becomes the first line plus
+ ``[×{n-1} identical lines]`` (only when that is shorter);
+ 5. JSON that parses and re-serialises shorter is minified, accepted only
+ if the parsed values are equal to the original's.
+
+ Idempotent, deterministic and never longer than the input. ``stats`` has
+ ``cr_redraws``, ``ansi``, ``trailing_ws``, ``runs_collapsed``,
+ ``json_minified`` and ``saved_chars``.
+ """
+ text = text if isinstance(text, str) else str(text or "")
+ stats = {
+ "cr_redraws": 0,
+ "ansi": 0,
+ "trailing_ws": 0,
+ "runs_collapsed": 0,
+ "json_minified": False,
+ "saved_chars": 0,
+ }
+ if not text:
+ return text, stats
+ lines = _fold_text_steps(text, stats)
+ folded = "\n".join(lines)
+ mini = _json_minified(folded)
+ if mini is not None:
+ stats["json_minified"] = True
+ folded = mini
+ else:
+ folded = "\n".join(_fold_runs(lines, stats))
+ if len(folded) > len(text): # cannot happen; keep the invariant explicit
+ return text, {**stats, "saved_chars": 0}
+ stats["saved_chars"] = len(text) - len(folded)
+ return folded, stats
+
+
+# --------------------------------------------------------------------------
+# E10: template masking (shared by _log_fingerprint and _error_fp)
+# --------------------------------------------------------------------------
+
+_M_DQ = re.compile(r'"[^"\n]{0,200}"')
+_M_SQ = re.compile(r"(?<![\w'])'[^'\n]{0,200}'(?![\w'])")
+_M_TS = re.compile(
+ r"\b\d{4}-\d{2}-\d{2}(?:[t ]\d{2}:\d{2}:\d{2}(?:[.,]\d{1,9})?(?:z|[+-]\d{2}:?\d{2})?)?"
+ r"|\b\d{1,2}:\d{2}:\d{2}(?:[.,]\d{1,9})?"
+)
+_M_UUID = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
+_M_IP4 = re.compile(r"\b\d{1,3}(?:\.\d{1,3}){3}(?::\d{1,5})?\b")
+_M_IP6 = re.compile(r"(?<![\w:])(?:[0-9a-f]{1,4}:){2,7}[0-9a-f]{1,4}(?![\w:])")
+_M_PATH = re.compile(r"(?<![\w<>/.:=-])(?:/[\w.@+~%,=-]{1,128}){3,}/?")
+_M_DUR = re.compile(r"\b\d+(?:\.\d+)?(?:ms|us|µs|s|m|h)\b")
+_M_HEX = re.compile(r"[0-9a-f]{6,}")
+_M_DIGITS = re.compile(r"\d+")
+
+
+def _volatile(placeholder: str):
+ """A substitution that masks a quoted value or path only when it holds a digit (an id, a temp dir,
+ a port): ``table "orders"`` and ``table "payments"``, or ``/srv/data/a.db`` and ``/srv/data/b.db``,
+ are different messages, not one template."""
+ def sub(m: re.Match) -> str:
+ return placeholder if any(ch.isdigit() for ch in m.group(0)) else m.group(0)
+ return sub
+
+
+def mask_template(s: str) -> str:
+ """Mask the volatile fields of one (already lowercased) log line.
+
+ Quoted strings holding a digit -> ``"<str>"``, ISO-8601 timestamps and ``HH:MM:SS(.fff)``
+ -> ``<ts>``, UUIDs -> ``<uuid>``, IPv4/IPv6 -> ``<ip>``, paths of three or
+ more segments holding a digit -> ``<path>``, durations -> ``<dur>``, hex runs of 6+ ->
+ ``<hex>``, then digits -> ``#`` and whitespace collapsed. Callers apply
+ their own length cap.
+ """
+ s = _M_DQ.sub(_volatile('"<str>"'), s)
+ s = _M_SQ.sub(_volatile('"<str>"'), s)
+ s = _M_TS.sub("<ts>", s)
+ s = _M_UUID.sub("<uuid>", s)
+ s = _M_IP4.sub("<ip>", s)
+ s = _M_IP6.sub("<ip>", s)
+ s = _M_PATH.sub(_volatile("<path>"), s)
+ s = _M_DUR.sub("<dur>", s)
+ s = _M_HEX.sub("<hex>", s)
+ s = _M_DIGITS.sub("#", s)
+ return " ".join(s.split())
+
+
+# --------------------------------------------------------------------------
+# E11: json_brief
+# --------------------------------------------------------------------------
+
+_MAX_KEYS = 20
+_MAX_NOTABLE = 5
+_LOWCARD_MAX = 10
+
+
+# ensure_ascii=False writes these raw: lone surrogates (a str that cannot be
+# encoded as UTF-8), bidi/format controls (a brief must not turn the escaped
+# "\\u202e" of its input into a live override) and line separators. They are
+# escaped back, which is always valid inside a JSON string.
+_UNSAFE_RAW_RE = re.compile(
+ "[\x7f-\x9f\u00ad\u061c\u180e\u200b-\u200f\u2028-\u202e\u2060-\u206f"
+ "\ufeff\ufff9-\ufffb\ud800-\udfff\U000e0000-\U000e007f]"
+)
+
+
+def _dumps(obj: Any) -> str:
+ # allow_nan=False: NaN/Infinity are not valid JSON, and the brief must be.
+ out = json.dumps(obj, separators=(",", ":"), ensure_ascii=False, allow_nan=False)
+ if out.isascii():
+ return out
+ return _UNSAFE_RAW_RE.sub(_escape_char, out)
+
+
+def _escape_char(match: "re.Match[str]") -> str:
+ code = ord(match.group())
+ if code > 0xFFFF: # JSON escapes astral characters as a surrogate pair
+ code -= 0x10000
+ return "\\u%04x\\u%04x" % (0xD800 + (code >> 10), 0xDC00 + (code & 0x3FF))
+ return "\\u%04x" % code
+
+
+def _find_rows(obj: Any) -> tuple[list[dict], str | None, dict] | None:
+ """(rows, wrapper_key, scalar_siblings) for an applicable JSON value."""
+ if isinstance(obj, list):
+ if obj and all(isinstance(x, dict) for x in obj):
+ return obj, None, {}
+ return None
+ if isinstance(obj, dict):
+ best_key = None
+ best_len = -1
+ for k, v in obj.items():
+ if isinstance(v, (list, dict)):
+ ln = len(_dumps(v))
+ if ln > best_len:
+ best_key, best_len = k, ln
+ if best_key is None:
+ return None
+ v = obj[best_key]
+ if (
+ isinstance(v, list)
+ and len(v) >= 10
+ and all(isinstance(x, dict) for x in v)
+ ):
+ scalars = {
+ k: x
+ for k, x in obj.items()
+ if k != best_key and (x is None or isinstance(x, (str, int, float, bool)))
+ }
+ return v, best_key, scalars
+ return None
+
+
+def _query_terms(query: str) -> list[str]:
+ if not query:
+ return []
+ from .compress_context import _query_hit_terms # lazy: avoids an import cycle
+
+ return _query_hit_terms(query)
+
+
+def _notable_order(rows: list[dict], dumped: list[str], terms: list[str]) -> list[int]:
+ """Indices of notable rows, best first: query hits, then rare field values."""
+ n = len(rows)
+ ranked: list[tuple[int, int, int]] = [] # (class, rarity, index)
+ seen: set[int] = set()
+ if terms:
+ from .compress_context import _term_in_text # lazy, see above
+
+ term_rows: list[tuple[int, list[str]]] = []
+ for i, d in enumerate(dumped):
+ low = d[:4000].casefold()
+ hit_terms = [t for t in terms if _term_in_text(t, low)]
+ term_rows.append((i, hit_terms))
+ freq = Counter(t for _, ht in term_rows for t in ht)
+ for i, ht in term_rows:
+ # a term present in over half the rows (a key name such as
+ # "status") says nothing about which row matters
+ hits = sum(1 for t in ht if freq[t] * 2 <= n)
+ if hits:
+ ranked.append((0, -hits, i))
+ seen.add(i)
+ # Low-cardinality scalar fields: rows whose value is not the field's mode.
+ counts: dict[str, Counter] = {}
+ for row in rows:
+ for k, v in row.items():
+ if v is None or isinstance(v, (str, int, float, bool)):
+ c = counts.get(k)
+ if c is None:
+ c = counts[k] = Counter()
+ if len(c) <= _LOWCARD_MAX:
+ c[(type(v).__name__, v)] += 1
+ lowcard = {
+ k: c
+ for k, c in counts.items()
+ if 2 <= len(c) <= _LOWCARD_MAX and sum(c.values()) >= n * 0.9
+ }
+ modes = {k: c.most_common(1)[0][0] for k, c in lowcard.items()}
+ for i, row in enumerate(rows):
+ if i in seen:
+ continue
+ rarest = None
+ for k, c in lowcard.items():
+ if k in row:
+ v = row[k]
+ key = (type(v).__name__, v)
+ if key != modes[k] and key in c:
+ rarest = c[key] if rarest is None else min(rarest, c[key])
+ if rarest is not None:
+ ranked.append((1, rarest, i))
+ ranked.sort()
+ return [i for _, _, i in ranked]
+
+
+def json_brief(text: str, *, budget_chars: int, query: str = "") -> str | None:
+ """See :func:`_json_brief`; returns None for non-finite numbers too."""
+ try:
+ return _json_brief(text, budget_chars=budget_chars, query=query)
+ except ValueError: # NaN/Infinity cannot be written as valid JSON
+ return None
+
+
+def _json_brief(text: str, *, budget_chars: int, query: str = "") -> str | None:
+ """Valid-JSON structural brief of a JSON array of objects (v1.4.0).
+
+ Applies to a JSON array of objects, and to an object whose largest value
+ is an array of >=10 objects. Output::
+
+ {"_count": N, "_keys": [first 20 keys], "_head": [first K items],
+ "_tail": [last K items], "_notable": [<=5 items], "_omitted": M}
+
+ ``_notable`` holds items (not already in head/tail) that contain a query
+ term or whose low-cardinality field (e.g. ``status``) differs from that
+ field's mode, rarest first. ``_omitted`` counts the items shown nowhere.
+ For the object case the brief sits under the array's key beside the
+ object's other scalar keys. K is the largest value (>=1) that fits
+ ``budget_chars``. Returns ``None`` when the text is not applicable or even
+ K=1 does not fit.
+ """
+ if not isinstance(text, str) or text.lstrip()[:1] not in ("{", "["):
+ return None
+ try:
+ obj = json.loads(text)
+ except (ValueError, RecursionError):
+ return None
+ found = _find_rows(obj)
+ if found is None:
+ return None
+ rows, wrap_key, scalars = found
+ n = len(rows)
+ budget = int(budget_chars)
+
+ prefix = suffix = ""
+ if wrap_key is not None:
+ prefix = "{" + _dumps(wrap_key) + ":"
+ suffix = "".join("," + _dumps(k) + ":" + _dumps(v) for k, v in scalars.items()) + "}"
+ inner_budget = budget - len(prefix) - len(suffix)
+ if inner_budget <= 0:
+ return None
+
+ keys: list[str] = []
+ seen_keys: set[str] = set()
+ for row in rows:
+ for k in row:
+ if k not in seen_keys:
+ seen_keys.add(k)
+ if len(keys) < _MAX_KEYS:
+ keys.append(k)
+ dumped = [_dumps(r) for r in rows]
+ # prefix sums so a candidate K costs O(1) to size
+ pre = [0] * (n + 1)
+ for i, d in enumerate(dumped):
+ pre[i + 1] = pre[i] + len(d) + 1
+ head_overhead = len('{"_count":%d,"_keys":%s,"_head":[],"_tail":[],"_notable":[],"_omitted":%d}' % (n, _dumps(keys), n))
+
+ def parts(k: int) -> tuple[list[int], list[int]]:
+ head = list(range(min(k, n)))
+ tail = list(range(max(len(head), n - k), n))
+ return head, tail
+
+ def size(head: list[int], tail: list[int], notable: list[int]) -> int:
+ tot = head_overhead
+ for grp in (head, tail, notable):
+ tot += sum(len(dumped[i]) + 1 for i in grp)
+ return tot
+
+ notable_all = [i for i in _notable_order(rows, dumped, _query_terms(query))]
+
+ def build(k: int, notable_cap: int) -> tuple[str, int] | None:
+ head, tail = parts(k)
+ shown = set(head) | set(tail)
+ notable = [i for i in notable_all if i not in shown][:notable_cap]
+ if size(head, tail, notable) > inner_budget + 3 * len(notable) + 4:
+ return None
+ omitted = n - len(shown) - len(notable)
+ body = (
+ '{"_count":%d,"_keys":%s,"_head":[%s],"_tail":[%s],"_notable":[%s],"_omitted":%d}'
+ % (
+ n,
+ _dumps(keys),
+ ",".join(dumped[i] for i in head),
+ ",".join(dumped[i] for i in tail),
+ ",".join(dumped[i] for i in sorted(notable)),
+ omitted,
+ )
+ )
+ return (body, len(body)) if len(body) <= inner_budget else None
+
+ for cap in (_MAX_NOTABLE, 2, 0):
+ # Largest K that fits (size grows with K): binary search, K in [1, n].
+ lo, hi, best = 1, max(1, n // 2 + 1), None
+ if build(1, cap) is None:
+ continue
+ while lo <= hi:
+ mid = (lo + hi) // 2
+ r = build(mid, cap)
+ if r is not None:
+ best, lo = r, mid + 1
+ else:
+ hi = mid - 1
+ if best is not None:
+ return prefix + best[0] + suffix
+ return None
diff --git a/plugins/context_engine/tameru/format_adapters.py b/plugins/context_engine/tameru/format_adapters.py
index cbc1b96d49e..397a7d387ce 100644
--- a/plugins/context_engine/tameru/format_adapters.py
+++ b/plugins/context_engine/tameru/format_adapters.py
@@ -9,6 +9,15 @@ import xml.etree.ElementTree as ET
from dataclasses import asdict, dataclass
from typing import Callable
+from .agent_formats import (
+ code_vetoes_yaml,
+ detect_grep_lines,
+ detect_test_runner,
+ detect_unified_diff,
+ is_numbered_code,
+ looks_like_code,
+ rejects_table,
+)
from .contract_gates import GENERIC_WORDS, distinctive_query_terms
from .unicode_profile import graphemes, matching_shadow, search_units
@@ -52,6 +61,28 @@ def _decline(
)
+# v1.4.0 (E6): conversational filler. A reply made only of these words names
+# nothing in the document, so it must behave like an empty query everywhere —
+# including here, where the plain-word fallback below would otherwise turn
+# "continue" into a row selector ("98% saved, 1 CSV row kept").
+# compress_context.py extends its next-chat stoplist from this set.
+FILLER_WORDS = frozenset(
+ {
+ "continue", "thanks", "thank", "ahead", "proceed", "okay", "ok",
+ "sure", "lgtm", "sounds", "good", "great", "yes", "yeah", "yep",
+ "cool", "done", "again", "retry", "going", "carry", "nice",
+ "perfect", "please", "alright", "fine", "thx", "yup", "go", "fix",
+ }
+)
+
+
+# v1.4.1: a command-line option copied into the query with the command
+# (``rg -n NAME src``, ``git log --stat``) names no content. As a "strong"
+# selector it used to crowd out the searched name itself, so a grep result
+# whose every line matched NAME was matched by ``-n`` alone: no hit, no cut.
+_CLI_FLAG_RE = re.compile(r"^-{1,2}[A-Za-z][A-Za-z0-9-]*$")
+
+
def _query_selectors(query: str) -> tuple[str, ...]:
candidates = list(distinctive_query_terms(query or ""))
candidates.extend(search_units(query or ""))
@@ -62,6 +93,7 @@ def _query_selectors(query: str) -> tuple[str, ...]:
if (
not value
or value.startswith("script:")
+ or _CLI_FLAG_RE.match(value)
or value in GENERIC_WORDS
or value in seen
):
@@ -72,6 +104,10 @@ def _query_selectors(query: str) -> tuple[str, ...]:
selectors.append(value)
if len(selectors) >= 128:
break
+ if selectors and all(value in FILLER_WORDS for value in selectors):
+ # Filler-only query: no plain-word fallback (E6). Mixed queries keep
+ # their filler words ("which tasks are done" still selects "done").
+ return ()
strong = [
value
for value in selectors
@@ -98,10 +134,56 @@ def _contains_selector(text: str, selectors: tuple[str, ...]) -> bool:
def _json_loads(value: str):
try:
return json.loads(value)
- except (json.JSONDecodeError, RecursionError):
+ except (ValueError, RecursionError):
+ # ValueError covers JSONDecodeError AND CPython's 4300-digit int limit:
+ # a line that is a 20k-digit number (`python -c "print(2**100000)"`)
+ # is not JSON for our purposes, and must not crash detection.
return None
+def _with_gaps(
+ units: list[str],
+ keep: list[bool],
+ sep: str,
+ gap_marker: Callable[[int, int], str] | None,
+ total_chars: int,
+) -> list[str]:
+ """``units`` that are kept, with ONE marker per contiguous omitted run
+ (leading and trailing runs included). All-blank runs carry nothing and get
+ no marker. ``gap_marker(omitted_chars, total_chars)``. A caller marker that
+ is not shorter than the omitted run is replaced by the run verbatim only
+ when the marker opts in via ``inline_short``; otherwise the marker is
+ always used."""
+ out: list[str] = []
+ run: list[str] = []
+
+ def flush() -> None:
+ nonlocal run
+ if not run:
+ return
+ dropped = sep.join(run)
+ if dropped.strip():
+ if gap_marker is not None:
+ marker = str(gap_marker(len(dropped), total_chars))
+ if (
+ getattr(gap_marker, "inline_short", False)
+ and len(marker) >= len(dropped)
+ ):
+ out.extend(run)
+ else:
+ out.append(marker)
+ run = []
+
+ for unit, kept in zip(units, keep):
+ if kept:
+ flush()
+ out.append(unit)
+ else:
+ run.append(unit)
+ flush()
+ return out
+
+
def _looks_ndjson(text: str) -> bool:
lines = [line for line in text.splitlines() if line.strip()]
if len(lines) < 2:
@@ -200,7 +282,24 @@ def detect_format(text: str) -> str:
return "xml"
if _looks_ndjson(value):
return "ndjson"
+ if stripped[:1] in {"{", "["} and _json_loads(stripped) is not None:
+ return "json"
+ # v1.4.0 (E4): agent outputs that are NOT tables run before the table
+ # detectors — a numbered read is a two-column TSV, Python is "YAML".
+ if detect_unified_diff(value):
+ return "diff"
+ if detect_grep_lines(value):
+ return "grep"
+ if is_numbered_code(value):
+ return "numbered_code"
+ if detect_test_runner(value):
+ return "test_output" # pytest/jest/go/cargo: not YAML ("failures:")
+ is_code = looks_like_code(value)
+ if is_code:
+ return "code"
delimiter = _detect_delimiter(value)
+ if delimiter and rejects_table(value, stable_columns=False):
+ delimiter = None # code with commas/tabs, not a table
if delimiter == "\t":
return "tsv"
if delimiter == ",":
@@ -222,10 +321,8 @@ def detect_format(text: str) -> str:
r"(?m)^[ \t]+(?:[-?][ \t]+)?[^\s:#][^:\n]{0,120}:\s*(?:\S.*)?$",
value,
)
- if yaml_parent and yaml_child:
+ if yaml_parent and yaml_child and not code_vetoes_yaml(value):
return "yaml"
- if stripped[:1] in {"{", "["} and _json_loads(stripped) is not None:
- return "json"
if _looks_vertical(value):
return "vertical"
return "text"
@@ -234,11 +331,14 @@ def detect_format(text: str) -> str:
_VERTICAL_MARKUP_RE = re.compile(r"writing-mode\s*:", re.IGNORECASE)
-def _adapt_ndjson(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_ndjson(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
lines = [line for line in text.splitlines() if line.strip()]
if len(lines) > limits.max_records:
return _decline(text, "ndjson", "record limit exceeded", total_records=len(lines))
kept: list[str] = []
+ flags: list[bool] = []
for line in lines:
if len(line) > limits.max_record_chars or _json_loads(line) is None:
return _decline(
@@ -248,14 +348,17 @@ def _adapt_ndjson(text: str, selectors: tuple[str, ...], limits: FormatLimits) -
total_records=len(lines),
structurally_valid=False,
)
- if _contains_selector(line, selectors):
+ hit = _contains_selector(line, selectors)
+ flags.append(hit)
+ if hit:
kept.append(line)
if not kept or len(kept) == len(lines):
return _decline(text, "ndjson", "no selective record match", total_records=len(lines))
+ out_lines = kept if gap_marker is None else _with_gaps(lines, flags, "\n", gap_marker, len(text))
return FormatResult(
format="ndjson",
applied=True,
- text=_line_separator(text).join(kept),
+ text=_line_separator(text).join(out_lines),
total_records=len(lines),
kept_records=len(kept),
reason="exact matching NDJSON records",
@@ -268,6 +371,7 @@ def _adapt_delimited(
limits: FormatLimits,
delimiter: str,
format_name: str,
+ gap_marker: Callable[[int, int], str] | None = None,
) -> FormatResult:
records = _split_delimited_records(text)
if records is None:
@@ -280,11 +384,20 @@ def _adapt_delimited(
width = len(parsed[0] or [])
if width < 2 or any(len(row or []) != width for row in parsed[1:]):
return _decline(text, format_name, "inconsistent field count", structurally_valid=False)
- kept = [record for record, row in zip(records[1:], parsed[1:]) if _contains_selector("\t".join(row or []), selectors)]
+ flags = [
+ _contains_selector("\t".join(row or []), selectors) for row in parsed[1:]
+ ]
+ kept = [record for record, hit in zip(records[1:], flags) if hit]
total = max(0, len(records) - 1)
if not kept or len(kept) == total:
return _decline(text, format_name, "no selective record match", total_records=total)
- result_records = [records[0], *kept]
+ if gap_marker is None:
+ result_records = [records[0], *kept]
+ else:
+ result_records = [
+ records[0],
+ *_with_gaps(records[1:], flags, "\n", gap_marker, len(text)),
+ ]
return FormatResult(
format=format_name,
applied=True,
@@ -295,7 +408,9 @@ def _adapt_delimited(
)
-def _adapt_markdown(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_markdown(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
del limits
lines = text.splitlines()
sections: list[tuple[list[str], list[str]]] = []
@@ -333,12 +448,20 @@ def _adapt_markdown(text: str, selectors: tuple[str, ...], limits: FormatLimits)
else:
current.append(line)
flush()
- selected = [(parents, body) for parents, body in sections if _contains_selector("\n".join(body), selectors)]
+ flags = [_contains_selector("\n".join(body), selectors) for _, body in sections]
+ selected = [section for section, hit in zip(sections, flags) if hit]
if not selected or len(selected) == len(sections):
return _decline(text, "markdown", "no selective section match", total_records=len(sections))
output: list[str] = []
seen_headings: set[str] = set()
- for parents, body in selected:
+ run_start = 0 # first section not yet emitted or accounted for as omitted
+ selected_ids = [i for i, hit in enumerate(flags) if hit]
+ for position, (parents, body) in zip(selected_ids, selected):
+ if gap_marker is not None and position > run_start:
+ dropped = "\n".join("\n".join(sections[i][1]) for i in range(run_start, position))
+ if dropped.strip():
+ output.append(str(gap_marker(len(dropped), len(text))))
+ run_start = position + 1
for heading in parents:
if heading not in seen_headings:
output.append(heading)
@@ -348,6 +471,10 @@ def _adapt_markdown(text: str, selectors: tuple[str, ...], limits: FormatLimits)
output.extend(body)
if body and body[0].startswith("#"):
seen_headings.add(body[0])
+ if gap_marker is not None and run_start < len(sections):
+ dropped = "\n".join("\n".join(sections[i][1]) for i in range(run_start, len(sections)))
+ if dropped.strip():
+ output.append(str(gap_marker(len(dropped), len(text))))
return FormatResult(
format="markdown",
applied=True,
@@ -362,7 +489,9 @@ def _indent(line: str) -> int:
return len(line) - len(line.lstrip(" "))
-def _adapt_yaml(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_yaml(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
del limits
lines = text.splitlines()
selected: set[int] = set()
@@ -403,7 +532,11 @@ def _adapt_yaml(text: str, selectors: tuple[str, ...], limits: FormatLimits) ->
selected.add(index)
if not selected or len(selected) >= len(lines):
return _decline(text, "yaml", "no selective subtree match", total_records=len(nonblank))
- output = "\n".join(lines[index] for index in sorted(selected)).strip("\n")
+ if gap_marker is None:
+ output = "\n".join(lines[index] for index in sorted(selected)).strip("\n")
+ else:
+ flags = [index in selected for index in range(len(lines))]
+ output = "\n".join(_with_gaps(lines, flags, "\n", gap_marker, len(text))).strip("\n")
return FormatResult(
format="yaml",
applied=True,
@@ -414,7 +547,9 @@ def _adapt_yaml(text: str, selectors: tuple[str, ...], limits: FormatLimits) ->
)
-def _adapt_xml(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_xml(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
del limits
upper = text.upper()
if "<!DOCTYPE" in upper or "<!ENTITY" in upper:
@@ -431,13 +566,15 @@ def _adapt_xml(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> F
ET.fromstring(child)
except (ET.ParseError, RecursionError):
return _decline(text, "xml", "child spans multiple lines", structurally_valid=True)
- kept = [child for child in children if _contains_selector(child, selectors)]
+ flags = [_contains_selector(child, selectors) for child in children]
+ kept = [child for child, hit in zip(children, flags) if hit]
if not kept or len(kept) == len(children):
return _decline(text, "xml", "no selective child match", total_records=len(children))
+ body = kept if gap_marker is None else _with_gaps(children, flags, "\n", gap_marker, len(text))
return FormatResult(
format="xml",
applied=True,
- text="\n".join([lines[0], *kept, lines[-1]]),
+ text="\n".join([lines[0], *body, lines[-1]]),
total_records=len(children),
kept_records=len(kept),
reason="balanced root plus exact child elements",
@@ -517,7 +654,9 @@ def _split_sql_statements(text: str) -> list[str] | None:
return statements
-def _adapt_sql(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_sql(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
statements = _split_sql_statements(text)
if statements is None:
return _decline(text, "sql", "unterminated SQL quote or comment", structurally_valid=False)
@@ -525,20 +664,24 @@ def _adapt_sql(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> F
return _decline(text, "sql", "record limit exceeded", total_records=len(statements))
if any(len(statement) > limits.max_record_chars for statement in statements):
return _decline(text, "sql", "statement size limit exceeded")
- kept = [statement for statement in statements if _contains_selector(statement, selectors)]
+ flags = [_contains_selector(statement, selectors) for statement in statements]
+ kept = [statement for statement, hit in zip(statements, flags) if hit]
if not kept or len(kept) == len(statements):
return _decline(text, "sql", "no selective statement match", total_records=len(statements))
+ body = kept if gap_marker is None else _with_gaps(statements, flags, "\n", gap_marker, len(text))
return FormatResult(
format="sql",
applied=True,
- text="\n".join(kept),
+ text="\n".join(body),
total_records=len(statements),
kept_records=len(kept),
reason="exact matching SQL statements",
)
-def _adapt_ini(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_ini(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
del limits
lines = text.splitlines()
starts = [index for index, line in enumerate(lines) if re.match(r"^\s*\[[^\]]+\]\s*$", line)]
@@ -548,13 +691,15 @@ def _adapt_ini(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> F
for position, start in enumerate(starts):
end = starts[position + 1] if position + 1 < len(starts) else len(lines)
sections.append("\n".join(lines[start:end]).strip("\n"))
- kept = [section for section in sections if _contains_selector(section, selectors)]
+ flags = [_contains_selector(section, selectors) for section in sections]
+ kept = [section for section, hit in zip(sections, flags) if hit]
if not kept or len(kept) == len(sections):
return _decline(text, "ini", "no selective section match", total_records=len(sections))
+ body = kept if gap_marker is None else _with_gaps(sections, flags, "\n\n", gap_marker, len(text))
return FormatResult(
format="ini",
applied=True,
- text="\n\n".join(kept),
+ text="\n\n".join(body),
total_records=len(sections),
kept_records=len(kept),
reason="exact matching INI/TOML sections",
@@ -566,7 +711,9 @@ _HTML_CLOSE_WRAPPER_RE = re.compile(r"^\s*</(?:body|html)>\s*$", re.IGNORECASE)
_HTML_CHILD_RE = re.compile(r"^\s*<([A-Za-z][\w:-]*)(?:\s[^>]*)?>.*</\1>\s*$", re.IGNORECASE)
-def _adapt_html(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_html(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
del limits
lines = [line for line in text.splitlines() if line.strip()]
opening: list[str] = []
@@ -583,39 +730,51 @@ def _adapt_html(text: str, selectors: tuple[str, ...], limits: FormatLimits) ->
return _decline(text, "html", "child spans multiple lines")
if not opening or not closing or not children:
return _decline(text, "html", "missing line-oriented wrappers or children")
- kept = [child for child in children if _contains_selector(child, selectors)]
+ flags = [_contains_selector(child, selectors) for child in children]
+ kept = [child for child, hit in zip(children, flags) if hit]
if not kept or len(kept) == len(children):
return _decline(text, "html", "no selective child match", total_records=len(children))
+ body = kept if gap_marker is None else _with_gaps(children, flags, "\n", gap_marker, len(text))
return FormatResult(
format="html",
applied=True,
- text="\n".join([*opening, *kept, *closing]),
+ text="\n".join([*opening, *body, *closing]),
total_records=len(children),
kept_records=len(kept),
reason="HTML wrappers plus exact child elements",
)
-def _adapt_vertical(text: str, selectors: tuple[str, ...], limits: FormatLimits) -> FormatResult:
+def _adapt_vertical(
+ text: str, selectors: tuple[str, ...], limits: FormatLimits, gap_marker: Callable[[int, int], str] | None = None
+) -> FormatResult:
del limits
blocks = [block for block in re.split(r"(?:\r?\n){2,}", text) if block.strip()]
- kept = [block for block in blocks if _contains_selector("".join(block.splitlines()), selectors)]
+ flags = [_contains_selector("".join(block.splitlines()), selectors) for block in blocks]
+ kept = [block for block, hit in zip(blocks, flags) if hit]
if not kept or len(kept) == len(blocks):
return _decline(text, "vertical", "no selective vertical column match", total_records=len(blocks))
+ body = kept if gap_marker is None else _with_gaps(blocks, flags, "\n\n", gap_marker, len(text))
return FormatResult(
format="vertical",
applied=True,
- text="\n\n".join(kept),
+ text="\n\n".join(body),
total_records=len(blocks),
kept_records=len(kept),
reason="matching logical-order OCR columns",
)
-_ADAPTERS: dict[str, Callable[[str, tuple[str, ...], FormatLimits], FormatResult]] = {
+_GapMarker = Callable[[int, int], str] | None
+
+_ADAPTERS: dict[str, Callable[..., FormatResult]] = {
"ndjson": _adapt_ndjson,
- "csv": lambda text, selectors, limits: _adapt_delimited(text, selectors, limits, ",", "csv"),
- "tsv": lambda text, selectors, limits: _adapt_delimited(text, selectors, limits, "\t", "tsv"),
+ "csv": lambda text, selectors, limits, gap_marker=None: _adapt_delimited(
+ text, selectors, limits, ",", "csv", gap_marker
+ ),
+ "tsv": lambda text, selectors, limits, gap_marker=None: _adapt_delimited(
+ text, selectors, limits, "\t", "tsv", gap_marker
+ ),
"markdown": _adapt_markdown,
"yaml": _adapt_yaml,
"xml": _adapt_xml,
@@ -632,7 +791,14 @@ def adapt_format(
limits: FormatLimits | None = None,
*,
format_name: str | None = None,
+ gap_marker: _GapMarker = None,
) -> FormatResult:
+ """Run the extractive adapter for ``format_name`` (detected when omitted).
+
+ ``gap_marker(omitted_chars, total_chars)``: when given, every contiguous
+ run of dropped records/sections/lines is replaced by one marker line. With
+ ``None`` the output is byte-identical to earlier releases.
+ """
value = str(text or "")
resolved_format = format_name or detect_format(value)
selectors = _query_selectors(query)
@@ -641,4 +807,4 @@ def adapt_format(
adapter = _ADAPTERS.get(resolved_format)
if adapter is None:
return _decline(value, resolved_format, "no safe extractive adapter")
- return adapter(value, selectors, limits or FormatLimits())
+ return adapter(value, selectors, limits or FormatLimits(), gap_marker)
diff --git a/plugins/context_engine/tameru/industrial.py b/plugins/context_engine/tameru/industrial.py
index 67b370c0c52..a4fb0c25be1 100644
--- a/plugins/context_engine/tameru/industrial.py
+++ b/plugins/context_engine/tameru/industrial.py
@@ -2,6 +2,7 @@
from __future__ import annotations
from dataclasses import asdict, dataclass, replace
+from typing import Callable
from .format_adapters import FormatLimits, FormatResult, adapt_format, detect_format
from .unicode_profile import profile_text, unicode_safety_counts
@@ -138,6 +139,8 @@ def industrial_preprocess(
text: str,
query: str,
limits: IndustrialLimits | None = None,
+ *,
+ gap_marker: Callable[[int, int], str] | None = None,
) -> IndustrialResult:
value = str(text or "")
resolved = limits or IndustrialLimits()
@@ -178,6 +181,7 @@ def industrial_preprocess(
query,
resolved.format_limits(),
format_name=profile.format,
+ gap_marker=gap_marker,
)
if not format_result.structurally_valid:
return IndustrialResult(
diff --git a/plugins/context_engine/tameru/plugin.yaml b/plugins/context_engine/tameru/plugin.yaml
index f0c87cba557..8b4640cab0e 100644
--- a/plugins/context_engine/tameru/plugin.yaml
+++ b/plugins/context_engine/tameru/plugin.yaml
@@ -1,4 +1,4 @@
name: tameru
display_name: Tameru (貯める)
-description: "Tameru 1.3.0 industrial deterministic context compaction"
-version: 1.3.0
+description: "Tameru 1.4.0 industrial deterministic context compaction"
+version: 1.4.0
diff --git a/plugins/context_engine/tameru/transcript.py b/plugins/context_engine/tameru/transcript.py
index 8a0316afd46..528e1f7580b 100644
--- a/plugins/context_engine/tameru/transcript.py
+++ b/plugins/context_engine/tameru/transcript.py
@@ -9,6 +9,8 @@ from __future__ import annotations
import json
import re
+from collections.abc import Callable
+from itertools import chain
from typing import Any
from .contract_gates import GENERIC_WORDS
@@ -42,13 +44,280 @@ _PRESERVATION_OBJECTS = {
}
+_TEXT_PART_TYPES = frozenset({"text", "input_text", "output_text"})
+
+# task_query limits. Tool-call argument leaves are usually paths, commands or
+# patterns, so they get a tighter per-part cap than free text; that keeps one
+# huge ``old_string``/``content`` argument from using up the whole budget.
+_QUERY_PART_CHARS = 600
+_QUERY_ARG_CHARS = 240
+_QUERY_MAX_DEPTH = 8
+
+# ``data:<mime>[;param]*,<payload>`` -- the payload charset is base64 plus
+# percent-encoding, so text that follows the URI is not swallowed. Every part is
+# printable ASCII and length-bounded: unbounded, one ``data:a/b;`` with no comma
+# scans to the end of a whitespace-free run and a text with many of them costs
+# O(n^2); and ``_clean_query_text`` relies on a URI being a single run of
+# printable ASCII.
+_DATA_URI_RE = re.compile(
+ r"(?<![\w])data:[A-Za-z0-9_.+-]{1,127}/[A-Za-z0-9_.+-]{1,127}"
+ r"(?:;[\x21-\x2b\x2d-\x3a\x3c-\x7e]{0,255}){0,16},"
+ r"[A-Za-z0-9+/=%_.~-]*"
+)
+# A run of >=200 printable ASCII chars without whitespace is a blob (base64,
+# hex, minified data), never a task description. ASCII-only on purpose:
+# CJK prose has no spaces, so a long Japanese paragraph must survive.
+_LONG_TOKEN_RE = re.compile(r"[\x21-\x7e]{200,}")
+
+
+def text_of(content: Any) -> str:
+ """Plain text of a message ``content`` field, whatever its shape.
+
+ - ``str`` -> returned as is.
+ - ``list`` (multimodal parts) -> the text of parts whose ``type`` is
+ ``text``/``input_text``/``output_text``, plus bare ``str`` parts, joined
+ with ``"\\n"``. Image, base64 and every other part type is skipped.
+ - ``dict`` with a ``str`` ``"text"`` -> that text.
+ - anything else (``None``, numbers, ...) -> ``""``.
+ """
+ if isinstance(content, str):
+ return content
+ if isinstance(content, dict):
+ text = content.get("text")
+ return text if isinstance(text, str) else ""
+ if isinstance(content, list):
+ parts: list[str] = []
+ for part in content:
+ if isinstance(part, str):
+ parts.append(part)
+ elif isinstance(part, dict) and part.get("type") in _TEXT_PART_TYPES:
+ text = part.get("text")
+ if isinstance(text, str):
+ parts.append(text)
+ return "\n".join(parts)
+ return ""
+
+
def last_user_text(messages: list[dict[str, Any]]) -> str:
for msg in reversed(messages or []):
if msg.get("role") == "user":
- return str(msg.get("content") or "")
+ return text_of(msg.get("content"))
return ""
+# Smallest prefix ``_clean_query_text`` looks at when it is given a ``limit``.
+_CLEAN_WINDOW_MIN = 4096
+
+
+def _clean_query_text(text: str, limit: int | None = None) -> str:
+ """Drop ``data:`` URIs and base64-like blobs (>=200 printable non-space chars).
+
+ ``limit`` says how many leading characters of the result the caller will
+ use. ``task_query`` runs once per tool result against the same (possibly
+ huge) user messages, so cleaning them in full each time would cost O(size)
+ per call. With a ``limit`` only a prefix is cleaned, and it grows (x4) until
+ it yields ``limit`` characters or covers the whole text. The cut can split a
+ blob or a word, so the trailing run of printable characters is dropped from
+ a truncated result; what is returned is always a prefix of the full clean.
+ """
+ window = len(text) if limit is None else max(4 * limit, _CLEAN_WINDOW_MIN)
+ while True:
+ cleaned = _LONG_TOKEN_RE.sub(" ", _DATA_URI_RE.sub(" ", text[:window]))
+ if window >= len(text):
+ return cleaned.strip()
+ end = len(cleaned)
+ while end and "!" <= cleaned[end - 1] <= "~":
+ end -= 1
+ cleaned = cleaned[:end].strip()
+ if len(cleaned) >= (limit or 0):
+ return cleaned
+ window *= 4
+
+
+def _string_leaves(value: Any, depth: int = 0) -> list[str]:
+ """String leaf values of a JSON-like structure, in document order."""
+ if isinstance(value, str):
+ return [value]
+ if depth >= _QUERY_MAX_DEPTH:
+ return []
+ out: list[str] = []
+ if isinstance(value, dict):
+ for child in value.values():
+ out.extend(_string_leaves(child, depth + 1))
+ elif isinstance(value, (list, tuple)):
+ for child in value:
+ out.extend(_string_leaves(child, depth + 1))
+ return out
+
+
+def _call_name_and_args(call: Any) -> tuple[str, Any]:
+ """(function name, arguments) of an OpenAI-style or flat tool call."""
+ if not isinstance(call, dict):
+ return "", None
+ fn = call.get("function")
+ if not isinstance(fn, dict):
+ fn = {}
+ name = fn.get("name") or call.get("name") or ""
+ args = fn.get("arguments", call.get("arguments"))
+ return (name if isinstance(name, str) else ""), args
+
+
+def _arg_leaves(args: Any) -> list[str]:
+ """String leaves of tool-call arguments given as a JSON string or a dict."""
+ if isinstance(args, str):
+ try:
+ parsed = json.loads(args)
+ except (ValueError, RecursionError):
+ return [args]
+ return _string_leaves(parsed)
+ return _string_leaves(args)
+
+
+def _id_variants(*values: Any) -> frozenset[str]:
+ """Every spelling of one tool-call id.
+
+ Responses-style transports (Hermes' Codex path) give a call an ``id``, a
+ ``call_id`` and a ``response_item_id`` that differ, and the ``role: tool``
+ message answers with ``call_id`` or a composite ``call_id|response_item_id``.
+ All of those name ONE call, so ids are compared as sets of variants.
+ """
+ variants: set[str] = set()
+ for raw in values:
+ if raw is None or isinstance(raw, (dict, list, bool)):
+ continue
+ text = str(raw).strip()
+ if text:
+ variants.add(text)
+ variants.update(part for part in (p.strip() for p in text.split("|")) if part)
+ return frozenset(variants)
+
+
+def _call_id_variants(call: Any) -> frozenset[str]:
+ if not isinstance(call, dict):
+ return frozenset()
+ return _id_variants(call.get("id"), call.get("call_id"), call.get("response_item_id"))
+
+
+def _find_tool_call(messages: list[dict[str, Any]], idx: int, call_id: str) -> Any:
+ """The assistant tool_call that ``call_id`` answers, nearest before ``idx`` first."""
+ wanted = _id_variants(call_id)
+ order = chain(
+ range(min(idx, len(messages)) - 1, -1, -1), range(max(idx, 0), len(messages))
+ )
+ for i in order:
+ msg = messages[i]
+ if not isinstance(msg, dict) or msg.get("role") != "assistant":
+ continue
+ for call in msg.get("tool_calls") or []:
+ if wanted & _call_id_variants(call):
+ return call
+ return None
+
+
+def task_query(
+ messages: list[dict[str, Any]],
+ idx: int,
+ *,
+ focus: str | None = None,
+ max_chars: int = 2000,
+ skip_user: Callable[[dict[str, Any]], bool] | None = None,
+) -> list[str]:
+ """Query parts describing what the agent is doing around ``messages[idx]``.
+
+ ``messages[idx]`` is normally a tool result about to be compressed. A last
+ user message such as "continue" says nothing about it, so the query is built
+ from the surrounding task. Parts, in priority order:
+
+ 1. ``focus`` (an explicit topic, e.g. a ``/compress <topic>`` argument);
+ 2. the function name and string argument values of the assistant tool call
+ that produced this result (``messages[idx]["tool_call_id"]``, matched
+ against the call's ``id``/``call_id``/``response_item_id`` and either half
+ of a ``call_id|response_item_id`` composite);
+ 3. the first assistant message after ``idx``: its text and the string
+ arguments of its tool calls (what the agent did with the result);
+ 4. the last user message with non-empty text, scanning backwards
+ (``skip_user(msg)`` true skips a message, e.g. synthetic injections);
+ 5. the first such user message (the original task).
+
+ Returns an ordered list: parts are de-duplicated, empty ones dropped, each
+ capped, and ``data:`` URIs and base64-like tokens (runs of >=200 printable
+ ASCII characters without whitespace; CJK prose is kept) removed. The list never exceeds ``max_chars`` characters in
+ total (counting one ``"\\n"`` between parts); when the budget runs out,
+ lower-priority parts are cut or dropped first. Join with ``"\\n"`` or
+ ``" "`` to get a query string.
+ """
+ if max_chars <= 0 or not messages:
+ return []
+
+ parts: list[str] = []
+ seen: set[str] = set()
+ used = 0
+ full = False
+
+ def add(raw: Any, cap: int = _QUERY_PART_CHARS) -> None:
+ nonlocal used, full
+ if full or not isinstance(raw, str):
+ return
+ text = _clean_query_text(raw, cap)[:cap]
+ if not text:
+ return
+ room = max_chars - used - (1 if parts else 0)
+ if room <= 0:
+ full = True
+ return
+ if len(text) > room:
+ text = text[:room]
+ full = True
+ text = text.rstrip()
+ # De-duplicate what is actually emitted (after the cap and the budget
+ # cut), so two long parts that differ only past the cap are not both kept.
+ key = " ".join(text.split())
+ if not key or key in seen:
+ return
+ seen.add(key)
+ parts.append(text)
+ used += len(text) + (1 if len(parts) > 1 else 0)
+
+ if isinstance(focus, str):
+ add(focus)
+ elif focus:
+ add(text_of(focus))
+
+ msg = messages[idx] if isinstance(idx, int) and 0 <= idx < len(messages) else None
+ call_id = str((msg or {}).get("tool_call_id") or "") if isinstance(msg, dict) else ""
+ if call_id:
+ name, args = _call_name_and_args(_find_tool_call(messages, idx, call_id))
+ add(name, _QUERY_ARG_CHARS)
+ for leaf in _arg_leaves(args):
+ add(leaf, _QUERY_ARG_CHARS)
+
+ if isinstance(idx, int):
+ for i in range(max(idx + 1, 0), len(messages)):
+ follow = messages[i]
+ if isinstance(follow, dict) and follow.get("role") == "assistant":
+ add(text_of(follow.get("content")))
+ for call in follow.get("tool_calls") or []:
+ for leaf in _arg_leaves(_call_name_and_args(call)[1]):
+ add(leaf, _QUERY_ARG_CHARS)
+ break
+
+ def real_user_text(m: Any) -> str:
+ """Text of a user message that counts (non-blank, not skipped), else ``""``."""
+ if not isinstance(m, dict) or m.get("role") != "user":
+ return ""
+ text = text_of(m.get("content"))
+ if not text or text.isspace() or (skip_user is not None and skip_user(m)):
+ return ""
+ return text
+
+ # Scan lazily from each end: ``skip_user`` runs only until a message is
+ # accepted, not once per user message on every call.
+ last_user = next((t for t in map(real_user_text, reversed(messages)) if t), "")
+ add(last_user)
+ add(next((t for t in map(real_user_text, messages) if t), ""))
+ return parts
+
+
def _requests_full_preservation(query: str) -> bool:
words = set(re.findall(r"[a-z0-9]+", (query or "").casefold()))
if not words.intersection(_PRESERVATION_VERBS):
@@ -92,7 +361,7 @@ def _json_query_answer_requirements(
normalised = preprocess_json(payload, query)
try:
parsed_values.append(json.loads(normalised))
- except (json.JSONDecodeError, RecursionError):
+ except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
return
parse_payload(content)
@@ -189,16 +458,44 @@ def _boolean_requirement_satisfied(field: str, expected: bool, post: str) -> boo
def _pending_tool_calls(messages: list[dict[str, Any]]) -> set[str]:
- """Tool-call ids requested by an assistant turn that no tool message answers."""
- pending: set[str] = set()
+ """Tool-call ids of the trailing assistant turn that no tool message answers.
+
+ Only the trailing turn counts: the LAST assistant message, if it carries
+ ``tool_calls``, plus the ``tool`` messages after it. An orphan call earlier
+ in the history (a result lost to a crash, an interrupted turn, a history
+ rewrite) is not "in flight" once the agent has produced a later assistant
+ message, so it must not block ``trajectory_gate`` for the rest of the
+ session. A trailing assistant message without ``tool_calls`` leaves nothing
+ pending.
+
+ A call is answered when any spelling of its id (``id``, ``call_id``,
+ ``response_item_id``, or either half of a ``call_id|response_item_id``
+ composite) matches the ``tool_call_id`` of a later tool message. The
+ returned set holds each pending call's ``id`` (``call_id`` when it has none).
+ """
+ last = -1
+ for i in range(len(messages) - 1, -1, -1):
+ if messages[i].get("role") == "assistant":
+ last = i
+ break
+ if last < 0:
+ return set()
+ calls = [
+ call
+ for call in messages[last].get("tool_calls") or []
+ if _call_id_variants(call)
+ ]
+ if not calls:
+ return set()
answered: set[str] = set()
- for m in messages:
- for call in m.get("tool_calls") or []:
- if isinstance(call, dict) and call.get("id"):
- pending.add(str(call["id"]))
- if m.get("role") == "tool" and m.get("tool_call_id"):
- answered.add(str(m["tool_call_id"]))
- return pending - answered
+ for m in messages[last + 1 :]:
+ if m.get("role") == "tool":
+ answered |= _id_variants(m.get("tool_call_id"))
+ return {
+ str(call.get("id") or call.get("call_id") or call.get("response_item_id"))
+ for call in calls
+ if not _call_id_variants(call) & answered
+ }
def _call_signature(msg: dict[str, Any]) -> list[tuple[str, str]]:
@@ -228,9 +525,10 @@ def trajectory_gate(
"""SelfCompact-style timing rubric, deterministic version.
Compaction is suppressed when:
- - ``pending-tool-calls``: an assistant turn requested tools that have no
- result yet — the trajectory is mid-derivation and pruning could evict
- context the in-flight call needs (SelfCompact C1 closed-unit).
+ - ``pending-tool-calls``: the trailing assistant turn requested tools that
+ have no result yet — the trajectory is mid-derivation and pruning could
+ evict context the in-flight call needs (SelfCompact C1 closed-unit).
+ Orphan calls earlier in the history do not count.
- ``stuck-loop``: the last ``stuck_run`` assistant turns issued identical
tool calls — the agent is looping; the correct move is to diagnose,
not to erase evidence of the loop (SelfCompact N1 not-stuck).
@@ -296,7 +594,15 @@ def apply_extractive_tool_prune(
def query_facts_lost(before: list[dict[str, Any]], after: list[dict[str, Any]], query: str) -> bool:
- """True when distinctive query facts survived prune but not the summarizer."""
+ """True when distinctive query facts survived prune but not the summarizer.
+
+ .. deprecated:: 1.4.0
+ Kept for compatibility, with no runtime warning and no behaviour
+ change. It re-scans every tool payload and the whole transcript (O(n^2)
+ on long sessions) and the Hermes plugin no longer calls it: loss is
+ bounded per result at demotion time instead of being re-checked after
+ the summary. Do not use it on long transcripts.
+ """
raw_tool_evidence = [
str(message.get("content") or "")
for message in before
@@ -355,7 +661,13 @@ def query_facts_lost(before: list[dict[str, Any]], after: list[dict[str, Any]],
def bulky_tools_dropped(before: list[dict[str, Any]], after: list[dict[str, Any]]) -> bool:
- """True when no distinctive anchor from any bulky tool survives."""
+ """True when no distinctive anchor from any bulky tool survives.
+
+ .. deprecated:: 1.4.0
+ Kept for compatibility, with no runtime warning and no behaviour
+ change. It recomputes anchor sets for every bulky payload (O(n^2) on
+ long sessions) and the Hermes plugin no longer calls it.
+ """
pre = [
str(m.get("content") or "")
for m in before