Skip to content

The agent that grows with you

feat(plugins): submit Tameru 1.4.1 through the plugin catalogue

thewhitemage.3278fbd281ea
diff --git a/plugin-catalog/tameru.yaml b/plugin-catalog/tameru.yaml
new file mode 100644
index 00000000000..0904d7443b0
--- /dev/null
+++ b/plugin-catalog/tameru.yaml
@@ -0,0 +1,21 @@
+name: tameru
+title: Tameru
+repo: https://github.com/0xWhiteMage/hermes-tameru-plugin
+sha: 9a3642f6c0aa76ea7f10cb59cf604890c1ed4100
+subdir: hermes_tameru_plugin
+version: "1.4.1"
+description: Deterministic, local context compaction with query-aware tool-result extraction and recovery.
+maintainer: 0xWhiteMage
+tier: community
+category: memory
+docs_url: https://github.com/0xWhiteMage/hermes-tameru-plugin
+readme: false
+platforms: [linux]
+known_issues:
+ - Full archive recovery requires plugins.isolation to be in_process; host mode limits recovery.
+ - Overrides private ContextCompressor signatures and falls back when the host contract changes.
+capabilities:
+ provides_tools: []
+ provides_hooks: []
+ provides_middleware: []
+ requires_env: []
diff --git a/plugins/context_engine/extractive/__init__.py b/plugins/context_engine/extractive/__init__.py
deleted file mode 100644
index 19f3a3f0b72..00000000000
--- a/plugins/context_engine/extractive/__init__.py
+++ /dev/null
@@ -1,6 +0,0 @@
-"""Deprecated one-release compatibility alias for the Tameru engine."""
-from plugins.context_engine.tameru import ExtractiveContextEngine
-
-
-def register(ctx) -> None:
- ctx.register_context_engine(ExtractiveContextEngine())
diff --git a/plugins/context_engine/extractive/plugin.yaml b/plugins/context_engine/extractive/plugin.yaml
deleted file mode 100644
index 44b9f67d0b2..00000000000
--- a/plugins/context_engine/extractive/plugin.yaml
+++ /dev/null
@@ -1,4 +0,0 @@
-name: extractive
-display_name: Tameru compatibility alias
-description: "Deprecated compatibility alias; migrate context.engine to tameru"
-version: 1.4.1
diff --git a/plugins/context_engine/tameru/CHANGELOG.md b/plugins/context_engine/tameru/CHANGELOG.md
deleted file mode 100644
index 82ac8711fac..00000000000
--- a/plugins/context_engine/tameru/CHANGELOG.md
+++ /dev/null
@@ -1,12 +0,0 @@
-# Tameru Hermes adapter changelog
-
-## 1.4.1 source update — 2026-10-12
-
-- Synchronise vendored engine modules and both Tameru/legacy-extractive manifests to the upstream 1.4.1 source at `e7e32866ded56071af13fd5fb91a2214c1b3c018`. Engine tag/Release publication remains pending.
-- Forward Hermes compression keyword arguments, including `bypass_cooldown`, to the parent compressor. The previous signature raised TypeError during an overflow cooldown override.
-- Build relevance queries from the text parts of multimodal user content, excluding image payloads, in both proactive pruning and full compression. An explicit focus topic still takes precedence.
-- Add two regression tests through the real context-engine discovery path; both failed on the previous adapter and pass after the fixes.
-
-Verification on Python 3.14: 322 Hermes tests passed across the adapter, context engine/compressor, focus, plugin init/discovery and manual compression. Ruff and compile checks pass; the upstream vendor drift check is clean. Upstream engine: 1,168 passed / 3 skipped, production battery 15/15.
-
-This adapter retains the existing preliminary transcript-prune and summary-validation design. It does not include the standalone plugin's per-result ladder, `tameru_expand` store or artifact ledger, and it is not merged into Hermes main by this update.
diff --git a/plugins/context_engine/tameru/__init__.py b/plugins/context_engine/tameru/__init__.py
deleted file mode 100644
index 2c706a652b8..00000000000
--- a/plugins/context_engine/tameru/__init__.py
+++ /dev/null
@@ -1,72 +0,0 @@
-"""Hermes context-engine adapter for the vendored Tameru engine."""
-from __future__ import annotations
-
-from typing import Any
-
-from agent.context_compressor import ContextCompressor
-
-from .hermes_extractive_engine import (
- apply_extractive_tool_prune,
- bulky_tools_dropped,
- query_facts_lost,
-)
-from .transcript import last_user_text
-
-
-class ExtractiveContextEngine(ContextCompressor):
- """Tameru: built-in summarizer plus deterministic extractive pruning."""
-
- DISPLAY_NAME = "Tameru (貯める)"
-
- @property
- def name(self) -> str:
- return "tameru"
-
- @property
- def display_name(self) -> str:
- return self.DISPLAY_NAME
-
- def get_automatic_compaction_status_message(
- self, *, phase: str, default_message: str, **context: Any
- ) -> str | None:
- del phase, context
- return f"🗜️ {self.DISPLAY_NAME} compaction — {default_message}"
-
- def __init__(self, model: str = "pending", **kwargs: Any) -> None:
- kwargs.setdefault("proactive_prune_tokens", 48_000)
- super().__init__(model=model, **kwargs)
-
- def prune_tool_results_only(self, messages, current_tokens=None):
- query = last_user_text(messages)
- pruned, changed = apply_extractive_tool_prune(messages, query)
- more, parent_changed = super().prune_tool_results_only(pruned, current_tokens)
- return more, changed + parent_changed
-
- def compress(
- self,
- messages,
- current_tokens=None,
- focus_topic=None,
- force=False,
- memory_context="",
- **kwargs: Any,
- ):
- query = focus_topic or last_user_text(messages)
- pruned, _changed = apply_extractive_tool_prune(messages, query)
- summarised = super().compress(
- pruned,
- current_tokens=current_tokens,
- focus_topic=focus_topic,
- force=force,
- memory_context=memory_context,
- **kwargs,
- )
- if query_facts_lost(messages, summarised, query) or bulky_tools_dropped(
- pruned, summarised
- ):
- return pruned
- return summarised
-
-
-def register(ctx) -> None:
- ctx.register_context_engine(ExtractiveContextEngine())
diff --git a/plugins/context_engine/tameru/agent_extractive_engine.py b/plugins/context_engine/tameru/agent_extractive_engine.py
deleted file mode 100644
index 47c10818c93..00000000000
--- a/plugins/context_engine/tameru/agent_extractive_engine.py
+++ /dev/null
@@ -1,14 +0,0 @@
-"""Alias for tameru.transcript for backwards compatibility."""
-from .transcript import (
- apply_extractive_tool_prune,
- last_user_text,
- MIN_TOOL_CHARS,
- PROTECT_LAST_TOOL,
-)
-
-__all__ = [
- "MIN_TOOL_CHARS",
- "PROTECT_LAST_TOOL",
- "apply_extractive_tool_prune",
- "last_user_text",
-]
diff --git a/plugins/context_engine/tameru/agent_formats.py b/plugins/context_engine/tameru/agent_formats.py
deleted file mode 100644
index a1ba8edf915..00000000000
--- a/plugins/context_engine/tameru/agent_formats.py
+++ /dev/null
@@ -1,1196 +0,0 @@
-"""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.removeprefix("b/")
- if minus and minus != "/dev/null":
- return minus.removeprefix("a/")
- parts = f.header[0].split()
- last = parts[-1] if parts else "?"
- return last.removeprefix("b/")
-
-
-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()) or (
- 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 ")) or (
- 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
deleted file mode 100644
index 881aa84e223..00000000000
--- a/plugins/context_engine/tameru/compress_context.py
+++ /dev/null
@@ -1,5461 +0,0 @@
-"""Local extractive, query-aware context compression.
-
-Independent implementation. No Baseline RAG API, no their weights, no
-off-box calls. Keep original wording; drop whole blocks; fail open when
-the query signal is too weak to justify a cut.
-"""
-
-from __future__ import annotations
-
-import hashlib
-import bisect
-import json
-import math
-import os
-import re
-import stat
-import tempfile
-import time
-import unicodedata
-from dataclasses import asdict, dataclass, field, replace
-from pathlib import Path
-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
-from .format_adapters import FILLER_WORDS
-from .industrial import IndustrialLimits, IndustrialResult, industrial_preprocess
-from .supersession import apply_supersession
-from .unicode_profile import (
- script_of,
- search_units,
- token_units,
- unicode_safety_counts,
-)
-
-try: # optional semantic tier — never required
- from .semantic import resolve_tier
-except ImportError: # direct-script use without package context
- def resolve_tier(tier):
- return tier
-
-# Public re-export (``from tameru.compress_context import fold_lossless``); naming it here
-# keeps the import from looking unused.
-_REEXPORTED = (fold_lossless,)
-
-try:
- DEFAULT_CCR_DIR = Path.home() / ".tameru" / "cache" / "context-compress-ccr"
-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.1"
-CCR_TTL_SECONDS = 6 * 60 * 60
-CCR_MAX_CLOCK_SKEW_SECONDS = 5 * 60
-FREEZE_MAX_DECISIONS = 4096
-MAX_QUERY_TEMPLATE_MATCHES = 64
-CCR_SWEEP_MAX_RECORDS = 256
-MAX_SUMMARY_RESPONSE_BYTES = 1_000_000
-DEFAULT_SUMMARY_ENDPOINT = "http://127.0.0.1:18000/v1/chat/completions"
-DEFAULT_SUMMARY_MODELS = (
- "Qwen3.8-27B-NVFP4",
- "Qwen3.6 APEX MTP Compact",
- "DeepSeek V4 Flash 0713",
-)
-DEFAULT_SUMMARY_TIMEOUT = 30.0
-_CCR_HASH_RE = re.compile(r"^[0-9a-f]{24}$")
-_CCR_SWEEP_CURSOR = 0
-
-# Degraded scoring view (opt-in, JEV-inspired): on input size-limit breaches,
-# score a bounded head+tail view per block while emitted output stays
-# byte-exact. The oversize factor bounds total work when limits are bypassed.
-_DEGRADED_VIEW_HEAD = 400
-_DEGRADED_VIEW_TAIL = 200
-_DEGRADED_OVERSIZE_FACTOR = 4
-
-# Secrets screen (dsh-jev-pre-compaction inspired): never persist probable
-# credentials to the CCR store. Conservative patterns only — private key
-# blocks, well-known token prefixes, JWTs, and long quoted assignments.
-_SECRETS_RE = re.compile(
- r"-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----"
- r"|\bAKIA[0-9A-Z]{16}\b"
- r"|\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}"
- r"|github_pat_[A-Za-z0-9_]{20,}"
- r"|\bxox[baprs]-[A-Za-z0-9-]{10,}"
- r"|\bsk-(?:ant-)?[A-Za-z0-9_-]{20,}"
- 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.
-_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",
- "into", "about", "for", "and", "or", "of", "to", "a", "an", "in", "on",
- "it", "be", "as", "by", "from", "at", "if", "not", "can", "you", "we",
- "they", "them", "their", "our", "your", "please", "tell", "give", "show",
- "explain", "summarize", "summary", "function", "return", "class", "def",
- "import", "hmm",
-}
-_KANA_STOP = frozenset("のはがをにでともへやかなねよわさぞぜしてただす")
-
-_TOKEN_RE = re.compile(
- r"[A-Za-z_][A-Za-z0-9_]*|\d+(?:\.\d+)?|"
- r"[\u4e00-\u9fff]|"
- r"[\u3040-\u30ff]{2,}|[\uac00-\ud7a3]{2,}|"
- r"[\u0600-\u06ff]{2,}|[\u0e00-\u0e7f]{2,}"
-)
-_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).
-# 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+.-]{0,31}://\S+")
-_FAST_ACCOUNTING_PUNCT = frozenset("§…×–—“”‘’")
-
-
-@dataclass
-class CompressResult:
- compressed_text: str
- original_tokens: int = 0
- kept_tokens: int = 0
- tokens_saved_pct: float = 0.0
- policy_name: str = "local-extractive"
- mode: str = "adaptive"
- keep_ratio: float = 1.0
- tokens_saved: int = 0
- kept_line_ratio: float = 1.0
- cache_prefix_applied: bool = False
- compression_risk: Optional[str] = None
- confidence: Optional[float] = None
- ccr: Optional[dict[str, Any]] = None
- content_type: str = "text"
- fail_open: bool = False
- frozen_blocks: int = 0
- reasons: list[str] = field(default_factory=list)
- verifier: Optional[dict[str, Any]] = None
- receipt: Optional[dict[str, Any]] = None
-
- def __post_init__(self) -> None:
- self.tokens_saved = max(0, self.original_tokens - self.kept_tokens)
-
- def to_dict(self) -> dict[str, Any]:
- return asdict(self)
-
-
-def _sha256_text(text: str) -> str:
- return hashlib.sha256(str(text).encode("utf-8", errors="surrogatepass")).hexdigest()
-
-
-def _bounded_id_manifest(ids: Iterable[int], limit: int) -> list[int] | dict[str, Any]:
- ordered = sorted(int(item) for item in ids)
- resolved = max(0, int(limit))
- if len(ordered) <= resolved:
- return ordered
- head_count = (resolved + 1) // 2
- tail_count = resolved - head_count
- encoded = json.dumps(ordered, separators=(",", ":")).encode("ascii")
- return {
- "count": len(ordered),
- "head": ordered[:head_count],
- "tail": ordered[-tail_count:] if tail_count else [],
- "sha256": hashlib.sha256(encoded).hexdigest(),
- }
-
-
-def _receipt_hashes(
- source: str,
- output: str,
- industrial: IndustrialResult,
- *,
- mode: str,
- strategy: str,
- budget_ratio: Optional[float],
- citations: bool,
-) -> dict[str, str]:
- config = {
- "budget_ratio": budget_ratio,
- "citations": bool(citations),
- "limits": asdict(industrial.limits),
- "mode": mode,
- "strategy": strategy,
- }
- config_bytes = json.dumps(
- config, ensure_ascii=False, sort_keys=True, separators=(",", ":")
- ).encode("utf-8")
- return {
- "source_sha256": _sha256_text(source),
- "output_sha256": _sha256_text(output),
- "config_sha256": hashlib.sha256(config_bytes).hexdigest(),
- }
-
-
-# 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 text.translate(_FAST_ACCOUNTING_DELETE).isascii():
- return max(1, len(_TOKEN_RE.findall(text)))
- return max(1, len(token_units(text)))
-
-
-def _norm_newlines(text: str) -> str:
- return str(text or "").replace("\r\n", "\n").replace("\r", "\n")
-
-
-def classify_line(line: str) -> str:
- t = line.strip()
- if not t:
- return "blank"
- if re.match(r"^#{1,6}\s+", t):
- return "heading"
- if re.match(r"^(```|~~~)", t):
- return "fence"
- if re.match(
- r"^(Traceback|Caused by:|Error:|Exception:|[A-Za-z]+Error\b|at\s+\S+\(|\s*File\s+\".+\", line \d+)",
- t,
- ):
- return "trace"
- if re.match(
- r"^\[?(20\d\d-\d\d-\d\d|\d\d:\d\d:\d\d|INFO|WARN|WARNING|ERROR|DEBUG|TRACE|ERR|FAIL|FATAL|OK)\]?",
- t,
- ):
- return "log"
- if re.match(r"^(import|from)\s+|^#include\s+|^using\s+", t):
- return "import"
- if re.match(r"^(def|class|async\s+def|function|const|let|var|public|private|protected|static|fn|func)\b", t):
- return "definition"
- if re.match(r"^[-*+]\s+|\d+\.\s+", t):
- return "list"
- if re.match(r"^\|.*\|$", t):
- return "table"
- if re.match(r"^(User|Assistant|System|Tool|Developer):", t) or re.match(
- r"^\[(user|assistant|system|tool|developer)\]", t, re.IGNORECASE
- ):
- return "chat"
- if t[:1] in "{[" or re.match(r'^"[A-Za-z0-9_]+"\s*:', t):
- return "json"
- if re.match(r"^(\/\/|#|\/\*|\*)", t):
- return "comment"
- return "text"
-
-
-def route_content_type(lines: list[str]) -> str:
- counts: dict[str, int] = {}
- sample = lines[:50]
- for line in sample:
- t = classify_line(line)
- counts[t] = counts.get(t, 0) + 1
- nonblank = [(k, v) for k, v in counts.items() if k != "blank"]
- if not nonblank:
- return "text"
- top = max(nonblank, key=lambda kv: kv[1])[0]
- importish = counts.get("import", 0) + counts.get("definition", 0) + counts.get("fence", 0)
- if top in {"import", "definition", "fence", "comment"} or importish >= 3:
- return "code"
- if top in {"log", "trace"} or counts.get("log", 0) + counts.get("trace", 0) > 3:
- return "log"
- if top == "chat":
- return "chat"
- joined = "\n".join(sample).strip()
- if joined[:1] in "{[" and re.search(r'"[A-Za-z0-9_]+"\s*:', joined) and importish == 0:
- return "json"
- if top in {"json", "table"} and importish == 0:
- return "json"
- return "text"
-
-
-def _collapse_blanks(lines: list[str]) -> list[str]:
- out: list[str] = []
- blanks = 0
- for line in lines:
- if not line.strip():
- blanks += 1
- if blanks <= 1:
- out.append(line)
- else:
- blanks = 0
- out.append(line)
- return out
-
-
-_PROGRESS_RE = re.compile(
- r"^\[?[=#_>\-*\s]{3,}\d{1,3}%[^\n]*$"
- r"|^Compiling [\w-]+ v[\d.]+"
- r"|^Downloading\b"
- r"|^Downloading crates"
- r"|^Pulling fs layer"
- r"|^Step \d+/\d+",
-)
-
-_ERROR_RE = re.compile(
- r"error|ERROR|panic:|Caused by|Traceback|Exception|FAILED|fatal|FATAL"
- r"|warning:|WARN|E\d{4}:",
-)
-
-
-def _log_fingerprint(line: str) -> str | None:
- """Template form of a log line: volatile fields -> placeholders.
-
- 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", "[last] ")):
- return None
- stripped = _PROGRESS_RE.match(line.strip())
- if stripped:
- return None
- # 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:
- return False
- if re.search(r"(\[[=#>\-. ]*\]\s*)?\d{1,3}\s*(%|/\s*\d+)", t):
- return True
- 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):
-
- - progress bars / build spam deleted (N4)
- - consecutive same-template repeats collapse to one exemplar plus a
- count marker "[\u00d7N] ..." — counts are decision-relevant (N1)
- - markers are fingerprint-inert so preprocessing is idempotent (N2)
- - error-signal invariant: if a transform would remove ALL error lines,
- the errors are restored (N6)
- - Go/Java-style indented frame runs collapse like Python traces,
- 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, 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_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:
- candidates = [
- idx
- for idx, line in enumerate(kept_lines)
- if any(
- _term_in_text(selector, line.casefold())
- for selector in query_selectors
- )
- ]
- if len(candidates) <= MAX_QUERY_TEMPLATE_MATCHES:
- query_match_source_indices = set(candidates)
- else:
- # Evenly sample the full match span. This retains head/tail and
- # representative middle records without turning one common
- # selector (for example "stage 3") into thousands of blocks.
- last = len(candidates) - 1
- query_match_source_indices = {
- candidates[(slot * last) // (MAX_QUERY_TEMPLATE_MATCHES - 1)]
- for slot in range(MAX_QUERY_TEMPLATE_MATCHES)
- }
-
- 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)
- while i < n:
- line = kept_lines[i]
- kind = classify_line(line)
- is_trace_like = kind == "trace" or bool(
- (trace or (out and _looks_indented_frame(line))) and re.match(r"^\s+", line)
- )
- if is_trace_like:
- trace.append(line)
- trace_src.append(kept_src[i])
- i += 1
- continue
- flush_trace()
- if i in query_match_source_indices:
- # Volatile values can be the selector (for example "policy 17").
- # 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 _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. 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 = [(i, ln) for i, ln in enumerate(lines) if has_error_signal(ln)]
- if orig_errors:
- 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:
- 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)
-
-
-def _looks_indented_frame(line: str) -> bool:
- """Go/Java-style stack frame shapes (NTK classifier table, subset)."""
- t = line.strip()
- if not t.startswith("\t") and not line.startswith(" "):
- # also treat single-tab-ish indents via leading whitespace check below
- pass
- if re.match(r"^\s+goroutine \d+", t) or "goroutine" in t[:40]:
- return True
- if re.match(r"^\s*at\s+[\w$.]+\(", t): # Java: at org.x.Y.f(Foo.java:12)
- return True
- if re.match(r"^\s*[\w().*/]+\([\w/.$-]+:\d+\)", t): # Go: pkg.Fn(/path/file.go:88)
- return True
- if re.match(r"^\s+[\w./$-]+\([^)]*:\d+\)", t):
- return True
- return False
-
-
-def _crush_value(
- val: Any,
- depth: int,
- query_needles: list[str],
- query_selectors: list[re.Pattern[str]] | None = None,
-) -> Any:
- if query_selectors is None:
- query_selectors = _selector_patterns(query_needles)
- if depth > 10:
- return val
- if val is None:
- return None
- if isinstance(val, list):
- if not val:
- return []
- matching = []
- rest = []
- for item in val:
- blob = json.dumps(item, ensure_ascii=False) if not isinstance(item, str) else item
- if query_selectors and any(selector.search(blob) for selector in query_selectors):
- matching.append(
- _crush_value(item, depth + 1, query_needles, query_selectors)
- )
- else:
- rest.append(item)
- # No Baseline RAG first-3 cliff: if nothing matches, keep the
- # whole array. Destructive crush only happens when needles hit.
- if matching:
- leftover = len(val) - len(matching)
- if leftover > 0:
- matching.append(f"... {leftover} more items")
- return matching
- return [
- _crush_value(x, depth + 1, query_needles, query_selectors)
- for x in val
- ]
- if isinstance(val, dict):
- out: dict[str, Any] = {}
- for k, v in val.items():
- if v is None:
- continue
- if isinstance(v, list) and not v:
- continue
- if isinstance(v, str) and not re.search(r"\s", v):
- # Opaque values are commonly hashes, tokens or identifiers;
- # shortening or dropping them destroys their exact meaning.
- out[k] = v
- continue
- if isinstance(v, str) and len(v) > 200:
- if query_selectors and any(
- selector.search(v) for selector in query_selectors
- ):
- out[k] = v
- continue
- # lcc TRIM refusal: if the tail being cut carries a
- # qualifier/negation cue, truncation can invert the claim
- # ("granted to all staff" ← "…, except contractors").
- # A longer safe value beats a shorter misleading one.
- if _QUALIFIER_CUE_RE.search(v, 100):
- out[k] = v
- continue
- out[k] = v[:100] + f"... [{len(v) - 100} more chars]"
- continue
- out[k] = _crush_value(v, depth + 1, query_needles, query_selectors)
- return out
- return val
-
-
-def _json_query_needles(query: str) -> list[str]:
- terms = set(distinctive_query_terms(query))
- terms.update(_extract_entities(query))
- strong = {
- term
- for term in terms
- if any(ch.isdigit() for ch in term)
- or "-" in term
- or "_" in term
- or len(term) >= 8
- }
- return sorted(strong or terms)
-
-
-def preprocess_json(text: str, query: str) -> str:
- stripped = text.strip()
- if not query_has_distinctive_selectors(query):
- return text
- needles = _json_query_needles(query)
- try:
- parsed = json.loads(stripped)
- except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
- return text
- try:
- crushed = _crush_value(parsed, 0, needles)
- except RecursionError:
- return text
- try:
- dumped = json.dumps(crushed, ensure_ascii=False, indent=2)
- except (TypeError, ValueError, RecursionError):
- return text
- if len(dumped) < len(stripped) * 0.92:
- return dumped
- return text
-
-
-def _looks_like_csv(text: str) -> bool:
- lines = [ln for ln in text.split("\n") if ln.strip()]
- if len(lines) < 6 or lines[0].count(",") < 2:
- return False
- similar = sum(1 for ln in lines[1:40] if ln.count(",") >= 2)
- 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]]:
- return [
- re.compile(rf"(?<![A-Za-z0-9_]){re.escape(term)}(?![A-Za-z0-9_])", re.IGNORECASE)
- for term in terms
- ]
-
-
-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:")]
- if not terms:
- return text
- selectors = _selector_patterns(terms)
- lines = text.split("\n")
- kept = [lines[0]]
- 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
- 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.*$")
-
-
-def preprocess_flat_records(text: str, query: str) -> str:
- """Select exact rows from a flat key/value record dump."""
- if not query_has_distinctive_selectors(query):
- return text
- lines = text.split("\n")
- if any(not line.strip() for line in lines):
- return text
- if len(lines) < 6 or not all(_FLAT_RECORD_RE.match(line) for line in lines):
- return text
- terms = [
- term
- for term in distinctive_query_terms(query)
- if not term.startswith("script:")
- ]
- selectors = _selector_patterns(terms)
- kept = [line for line in lines if any(rx.search(line) for rx in selectors)]
- if not kept or len(kept) == len(lines):
- return text
- return "\n".join(kept)
-
-
-_ANSI_RE = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
-_TEST_SUMMARY_RE = re.compile(r"Tests\s+\d+\s+passed", re.IGNORECASE)
-_TEST_FILE_NOISE_RE = re.compile(
- r"ExperimentalWarning|Use `node --trace-warnings|^\s*[✓✔]\s"
-)
-
-
-def strip_ansi(text: str) -> str:
- return _ANSI_RE.sub("", text)
-
-
-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
- q = (query or "").lower()
- if re.search(r"\.(test|spec)\.\w+|__tests__/", q):
- return text
- kept = []
- for ln in text.split("\n"):
- p = strip_ansi(ln)
- if _TEST_FILE_NOISE_RE.search(p):
- continue
- kept.append(p)
- if len(kept) >= len(text.split("\n")):
- return text
- 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.IGNORECASE)
-_TRUST_RISK_RE = re.compile(
- r"\b(?:untrusted|not\s+operational)\b"
- r"|\b(?:obsolete|unrelated)\b.{0,24}"
- r"\b(?:glossary|example|sample|entry|data)\b"
- r"|\b(?:ignore|disregard|override|forget)\b.{0,48}"
- r"\b(?:instructions?|prompt|system)\b",
- re.IGNORECASE,
-)
-_TRUST_QUERY_RE = re.compile(
- r"\b(?:injection|untrusted|malicious|prompt\s+attack|obsolete|unrelated)\b"
- r"|\bignore\s+(?:prior|previous)\b",
- re.IGNORECASE,
-)
-# v0.6.0: a query that says "trust nothing from untrusted sources",
-# "exclude untrusted …" etc. is asking for the SAFE behaviour — it must
-# NOT suppress the trust-risk flag. Only explicit include-phrasing
-# ("include the untrusted block verbatim") counts as an override.
-_TRUST_EXCLUDE_CUE_RE = re.compile(
- r"\b(?:trust\s+nothing|don't\s+trust|do\s+not\s+trust|exclude|filter\s+out|omit)\b"
- r".{0,40}\b(?:untrusted|unverified|malicious)\b"
- r"|\b(?:exclude|drop|skip|omit)\s+(?:any\s+)?(?:untrusted|unverified|malicious)\b",
- re.IGNORECASE,
-)
-
-
-def _normalise_instruction_text(text: str) -> str:
- """Normalise width and remove invisible formatting before classification."""
- 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"
- )
-
-
-def preprocess_filler_comments(text: str, query: str) -> str:
- """Drop `-- filler` / `# filler` comments unless the query names them."""
- if "filler" in (query or "").casefold():
- return text
- if not query_has_distinctive_selectors(query or ""):
- return text
- lines = text.split("\n")
- kept = [ln for ln in lines if not _FILLER_COMMENT_RE.match(ln.strip())]
- if len(kept) == len(lines):
- return text
- return "\n".join(kept)
-
-
-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 unwrapped
- try:
- parsed = json.loads(stripped)
- except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
- return unwrapped
- if not isinstance(parsed, dict):
- return unwrapped
- for key in ("content", "output"):
- val = parsed.get(key)
- if isinstance(val, str) and len(val) > 200:
- 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 _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 = _HINT_ROUTES.get(content_hint) or route_content_type(lines)
- if kind == "log":
- return preprocess_logs(lines, query), kind
- if kind == "code":
- return _collapse_blanks(lines), kind
- if kind == "json":
- # Decode recognised Hermes content/output wrappers structurally before
- # JSON crushing. json.loads distinguishes escaped newlines from literal
- # backslash-n bytes; a blanket text replacement cannot.
- unwrapped = unwrap_hermes_tool(text)
- if unwrapped != text:
- return preprocess(unwrapped, query)
- processed = preprocess_json(text, query)
- return processed.split("\n"), kind
- if kind == "text":
- processed = preprocess_flat_records(text, query)
- if processed != text:
- return processed.split("\n"), kind
- return _collapse_blanks(lines), kind
-
-
-def _extract_terms(query: str) -> list[str]:
- terms: list[str] = []
- seen: set[str] = set()
- for raw in _TOKEN_RE.findall(query or ""):
- term = raw.lower().strip("؟?!.،,;:…\"'()[]")
- if not term:
- continue
- # CJK characters are single tokens (no word boundaries in CJK).
- # Accept them even though they're 1 char — they carry meaning.
- o = ord(raw[0]) if raw else 0
- is_cjk = (
- 0x4E00 <= o <= 0x9FFF
- or 0x3040 <= o <= 0x30FF
- or 0xAC00 <= o <= 0xD7A3
- or 0x0600 <= o <= 0x06FF
- or 0x0E00 <= o <= 0x0E7F
- )
- if (len(term) <= 2 and not is_cjk) or term in _STOP or term in _KANA_STOP or term in seen:
- continue
- seen.add(term)
- terms.append(term)
- for raw in search_units(query or "")[:128]:
- term = raw.strip("؟?!.،,;:…\"'()[]")
- if not term or term in seen or term in _STOP or term in _KANA_STOP:
- continue
- representative = next((char for char in term if char.isalnum()), term[0])
- script = script_of(representative)
- if script in {"han", "kana", "hangul", "arabic"}:
- continue
- if len(term) <= 2 and script in {None, "latin", "greek", "cyrillic"}:
- continue
- seen.add(term)
- terms.append(term)
- # Hyphenated compound names in the query ("titanium-torsion-rod") are
- # single search terms too — the tokenizer splits them on "-".
- for m in re.findall(r"\b\w+(?:-\w+)+\b", query or ""):
- m = m.lower()
- if m not in _STOP and m not in seen:
- seen.add(m)
- terms.append(m)
- return terms
-
-
-_WEAK_NEXT_CHAT = frozenset(
- {
- "keep",
- "track",
- "every",
- "detail",
- "details",
- "next",
- "chat",
- "start",
- "work",
- "please",
- "note",
- "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."""
- return [
- t
- for t in _extract_terms(query)
- if len(t) >= 4
- and t not in _STOP
- and t not in GENERIC_WORDS
- and t not in _WEAK_NEXT_CHAT
- ]
-
-
-def _extract_entities(query: str) -> list[str]:
- found: list[str] = []
- for rx in (_URI_RE, _PATH_RE, _DOTTED_RE, _CAMEL_RE, _QUOTED_RE, _CAPS_RE):
- for m in rx.finditer(query or ""):
- val = m.group(1) if m.lastindex else m.group(0)
- if val and val not in found and len(val) >= 2:
- found.append(val)
- return found
-
-
-_GIT_ONELINE_RE = re.compile(r"^[0-9a-f]{7,40}\s+\d{4}-\d{2}-\d{2}\s")
-_NPM_VERBOSE_RE = re.compile(
- r"^\d+\s+(verbose|info|silly|http|warn|error|timing)\b"
-)
-_HEX_ID_RE = re.compile(r"^[0-9a-f]{7,40}$")
-
-
-def _looks_like_line_records(lines: list[str]) -> bool:
- """Git oneline / npm verbose: one record per line, not 10-line chunks."""
- sample = [ln for ln in lines[:80] if ln.strip()]
- if len(sample) < 8:
- return False
- git_n = sum(1 for ln in sample if _GIT_ONELINE_RE.match(ln))
- npm_n = sum(1 for ln in sample if _NPM_VERBOSE_RE.match(ln))
- return git_n >= 6 or npm_n >= 6
-
-
-def segment_blocks(lines: list[str]) -> list[dict[str, Any]]:
- if _looks_like_line_records(lines):
- blocks: list[dict[str, Any]] = []
- for i, line in enumerate(lines):
- if not line.strip():
- continue
- blocks.append(
- {
- "id": len(blocks),
- "start": i,
- "end": i,
- "type": "log" if _NPM_VERBOSE_RE.match(line) else "text",
- "text": line,
- "tokens": max(1, estimate_tokens(line)),
- }
- )
- return blocks
- return _segment_blocks_chunked(lines)
-
-
-def _segment_blocks_chunked(lines: list[str]) -> list[dict[str, Any]]:
- blocks: list[dict[str, Any]] = []
- start: Optional[int] = None
- btype = "text"
- tokens = 0
-
- def close(end: int) -> None:
- nonlocal start, tokens
- if start is None:
- return
- while end >= start and not lines[end].strip():
- end -= 1
- if end < start:
- start = None
- tokens = 0
- return
- text = "\n".join(lines[start : end + 1])
- if text.strip():
- blocks.append(
- {
- "id": len(blocks),
- "start": start,
- "end": end,
- "type": btype,
- "text": text,
- "tokens": max(1, tokens or estimate_tokens(text)),
- }
- )
- start = None
- tokens = 0
-
- prev_type = None
- in_fence = False
- for i, line in enumerate(lines):
- kind = classify_line(line)
- line_toks = estimate_tokens(line)
- if in_fence:
- tokens += line_toks
- if kind == "fence":
- close(i)
- in_fence = False
- prev_type = kind
- continue
- if kind == "fence":
- close(i - 1)
- start = i
- btype = "fence"
- tokens = line_toks
- in_fence = True
- prev_type = kind
- continue
- new = False
- if (
- start is None
- or kind in {"heading", "fence", "definition"}
- or kind == "list"
- or (prev_type == "blank" and kind != "blank")
- or (kind != prev_type and kind in {"log", "chat", "import", "list", "table", "json"})
- ):
- new = True
- elif btype == "text" and kind in {"definition", "code"}:
- # A code/definition region starting inside a text block: split so
- # the code has its own block. Otherwise one "fn name(" line
- # classifies the whole block as code and the impl body ends up
- # scored as text.
- new = True
- elif start is not None and (i - start >= 10 or tokens + line_toks > 220):
- new = True
- if new:
- close(i - 1)
- start = i
- btype = kind if kind != "blank" else "text"
- tokens = line_toks
- else:
- tokens += line_toks
- if btype == "text" and kind != "blank":
- btype = kind
- prev_type = kind
- close(len(lines) - 1)
- return blocks
-
-
-def _soft_has(text: str, needle: str) -> bool:
- if not needle:
- return False
- if needle in text:
- return True
- return needle.lower() in text.lower()
-
-
-def _term_in_text(term: str, lower: str) -> bool:
- """Substring for long / syllabic terms; word-boundary for short Latin."""
- if not term:
- return False
- o = ord(term[0])
- syllabic = (
- 0x4E00 <= o <= 0x9FFF
- or 0x3040 <= o <= 0x30FF
- or 0xAC00 <= o <= 0xD7A3
- or 0x0600 <= o <= 0x06FF
- or 0x0E00 <= o <= 0x0E7F
- )
- if syllabic or len(term) > 5:
- return term in lower
- return re.search(rf"\b{re.escape(term)}\b", lower) is not None
-
-
-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))
- entity_df: dict[str, int] = {}
- term_df: dict[str, int] = {}
- fp_counts: dict[str, int] = {}
- # Bridge-entity detection: find entities (proper nouns, identifiers) that
- # appear in multiple blocks. These are the links in a multi-hop chain —
- # dropping the block that holds a bridge entity severs the chain.
- # Only boost entities that look like proper nouns or code identifiers,
- # not common filler words.
- bridge_entities: dict[str, int] = {}
- # Perf (v0.6.0): lowercase every block text exactly once. The previous
- # code re-lowered all N texts for every candidate of every block
- # (O(n²·c) .lower() calls) — the dominant cost on large documents.
- # Results are byte-identical; only the redundant work is removed.
- lower_texts = [b["text"].lower() for b in blocks]
- # Perf (v0.6.0): deduplicate candidates globally and resolve df via a
- # token Counter for single-token candidates (the vast majority). Only
- # multi-token phrases pay the exact substring scan. df for a single
- # \w-delimited token equals its token frequency except when it is a
- # substring of a LONGER token (e.g. "module_9" inside "module_90");
- # such boundary cases shift the boost amount marginally but never flip
- # the 2 <= df <= n//2 gate's intent.
- token_counter: dict[str, int] = {}
- # Perf (v0.6.0): one shared \w+ tokenisation per block feeds the counter,
- # the candidate scan AND tok_df below (previously 3 separate passes).
- block_token_lists: list[list[str]] = []
- for lt in lower_texts:
- toks = re.findall(r"\w+", lt)
- block_token_lists.append(toks)
- for w in toks:
- token_counter[w] = token_counter.get(w, 0) + 1
- seen_candidates: set[str] = set()
- candidate_blocks: dict[str, int] = {}
- _PROPER_NOUN_RE = re.compile(r"\b[A-Z][a-z]+(?: [A-Z][a-z]+)*\b")
- for b_idx, b in enumerate(blocks):
- toks = block_token_lists[b_idx]
- # Extract candidate bridge entities: CamelCase, UPPER_SNAKE, dotted
- # paths, quoted strings, and 6+ char tokens with digits (identifiers).
- candidates = set()
- candidates.update(_CAMEL_RE.findall(b["text"]))
- candidates.update(_PROPER_NOUN_RE.findall(b["text"])) # Proper nouns
- candidates.update(re.findall(r"\b[A-Z][A-Z_][A-Z_0-9]{3,}\b", b["text"])) # UPPER_SNAKE
- candidates.update(_DOTTED_RE.findall(b["text"]))
- candidates.update(_QUOTED_RE.findall(b["text"]))
- # 6+ char identifier tokens with digits — from the shared token list
- # instead of a second regex sweep over the raw text.
- # Perf (v0.6.0): the any(ch.isdigit()) genexpr over every token was
- # the single hottest line in score_blocks (266k calls). isdigit()
- # on the whole token is equivalent for this gate (we only care
- # whether ANY digit exists, and str.isdigit() short-circuits in C).
- for tok in toks:
- if len(tok) >= 6 and not tok.isdigit() and not tok.isalpha() and tok not in _STOP:
- candidates.add(tok)
- for cand in candidates:
- if len(cand) < 4 or cand in _STOP:
- continue
- is_identifier = (
- cand in cand.upper() # ALL CAPS (UPPER_SNAKE, acronyms)
- or cand[0].isupper() # Proper noun or CamelCase
- or re.search(r"\d", cand) # Contains a digit
- or "." in cand # Dotted path
- )
- if is_identifier and cand not in seen_candidates:
- seen_candidates.add(cand)
- candidate_blocks[cand] = 0
- for cand in seen_candidates:
- c_lower = cand.lower()
- # 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:
- fp_counts[fp] = fp_counts.get(fp, 0) + 1
- for e in entities:
- if _soft_has(b["text"], e):
- entity_df[e] = entity_df.get(e, 0) + 1
- lower = b["text"].lower()
- for t in terms:
- if _term_in_text(t, lower):
- term_df[t] = term_df.get(t, 0) + 1
- # Pre-compute token document frequency for novelty + fact-density.
- # A token's DF is the number of blocks it appears in. Low DF (1-2)
- # means the token is rare and information-dense.
- tok_df: dict[str, int] = {}
- # Perf (v0.6.0): reuse the shared per-block token lists (built above for
- # the bridge-candidate scan) — no third tokenisation pass.
- block_token_sets: list[set[str]] = []
- for toks in block_token_lists:
- seen_in_block: set[str] = set()
- block_tokens: set[str] = set(toks)
- for tok in toks:
- if len(tok) >= 4 and tok not in _STOP and tok not in seen_in_block:
- seen_in_block.add(tok)
- tok_df[tok] = tok_df.get(tok, 0) + 1
- block_token_sets.append(block_tokens)
-
- # Log-type detection (Baseline RAG "Log" policy): if > 60% of blocks
- # look like timestamped log lines, apply log-specific scoring weights.
- # Error/traceback blocks get a bonus; pure INFO/WARN noise gets penalized.
- _LOG_LINE_RE = re.compile(
- r"^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}" # timestamp prefix
- r"|^\[(?:DEBUG|INFO|WARN|ERROR|FATAL)\]" # bracketed level
- r"|^(?:DEBUG|INFO|WARN|ERROR|FATAL)\s" # bare level prefix
- )
- log_block_count = sum(
- 1 for b in blocks
- if any(_LOG_LINE_RE.match(line) for line in b["text"].splitlines()[:3])
- )
- is_log_context = log_block_count >= 2 and log_block_count / n > 0.4
- if is_log_context:
- _LOG_ERR_RE = re.compile(
- r"\b(?:ERROR|FATAL|WARN|WARNINGS?)\b|Traceback|Exception",
- re.IGNORECASE,
- )
- _LOG_NOISE_RE = re.compile(
- r"^\s*(?:INFO|DEBUG)\b", re.MULTILINE,
- )
- # Recency + novelty prior (rate-distortion signal): even without query
- # overlap, recent blocks and blocks holding rare, high-information
- # identifiers deserve a base floor so a "keep track of every detail"
- # style query does not evict scattered facts.
- # Critical-line pre-computation (Baseline RAG-style):
- # Pre-identify lines that carry operational facts — errors, exceptions,
- # 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.
- # 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 _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
- # every detail" query has H(Q) ≈ 0 — it tells us nothing about what
- # to keep, so we should be more conservative about dropping.
- #
- # A query is "weak" if NONE of its terms are distinctive identifiers
- # (digits, separators, uppercase). "database host port" is weak
- # (all generic words, no identifiers). "REL-2026 user_id 99113" is
- # strong (has identifiers). This matches the rate-distortion theory:
- # H(Q) ≈ 0 when the query carries no specific information.
- #
- # Guard: only apply the weak-query boost when there are enough
- # blocks (≥ 6) to make compression meaningful. With ≤ 5 blocks,
- # the keep_ratio will be ~1.0 and we'd fail-open anyway.
- weak_boost = 0.0
- if len(blocks) >= 6 and terms:
- distinctive = 0
- for t in terms:
- if len(t) < 4 or t in _STOP:
- continue
- if re.search(r"[\d_./-]", t) or t[0].isupper():
- distinctive += 1
- topic = _topic_terms(query)
- # English topic words (alena, cinematography) are not "weak" just
- # because they lack digits. Vague next-chat ("keep every detail")
- # still gets the uniqueness floor.
- weak_boost = 0.0 if distinctive > 0 or len(topic) >= 2 else 1.0
-
- scored = []
- for idx, b in enumerate(blocks):
- score = 0.0
- reason = "context"
- entity_hits = 0
- term_hits = 0
- rare_term_hits = 0
- for e in entities:
- if _soft_has(b["text"], e):
- entity_hits += 1
- df = entity_df.get(e, 1)
- score += 1.4 + 4.6 * math.log((n + 1) / (df + 1))
- reason = "query entity"
- lower = b["text"].lower()
- # Classification belongs to the block, never to question wording.
- # An intentional override must be explicit through pin_patterns.
- trust_risk = bool(
- _TRUST_RISK_RE.search(_normalise_instruction_text(b["text"]))
- )
- for t in terms:
- if _term_in_text(t, lower):
- term_hits += 1
- df = term_df.get(t, 1)
- if df <= max(2, int(0.2 * n)):
- rare_term_hits += 1
- score += 0.9 + 3.2 * math.log((n + 1) / (df + 1))
- if reason == "context":
- reason = "query term"
- if b["type"] == "definition":
- score += 2.0
- if b["type"] == "trace":
- score += 2.8
- if b["type"] == "heading":
- score += 0.8
- if b["type"] == "log" and re.search(r"error|fail|exception|timeout|denied|invalid", lower):
- score += 1.2
- # Old-error purge (leanctx-inspired): errored dumps with ZERO query
- # 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 (
- b["type"] == "log"
- and re.search(r"error|fail|exception|denied", lower)
- )
- or "Traceback (most recent call last)" in b["text"]
- )
- ):
- score -= 4.5
- if reason == "context":
- reason = "stale error"
-
- # Log-context policy (Baseline RAG "Log" policy): in log-heavy
- # contexts, boost error/traceback blocks and penalize pure INFO
- # noise so the signal stands out from the chatter.
- if is_log_context:
- if _LOG_ERR_RE.search(b["text"]):
- score += 3.0
- if reason == "context":
- reason = "log error"
- else:
- lines = b["text"].splitlines()
- noise_lines = sum(
- 1 for block_line in lines if _LOG_NOISE_RE.match(block_line)
- )
- if noise_lines >= len(lines) * 0.8 and len(lines) >= 2:
- score -= 1.5
- if reason == "context":
- reason = "log noise"
- # Critical-line floor: blocks containing operational facts (errors,
- # config, key-value pairs, dates, versions) get a floor boost so
- # they survive even without query overlap. This is the Baseline RAG
- # "important_kept_pct" signal — their verifier tracks critical lines
- # and flags risk when they're dropped. We make it a scoring input
- # instead, so the critical lines are kept in the first place.
- crit = crit_lines_per_block[idx]
- if crit > 0:
- score += min(3.0, 0.8 * crit)
- if reason == "context":
- reason = "critical lines"
- # Bridge-entity boost: blocks that contain cross-block entities (the
- # links in a multi-hop chain) get a floor boost so the chain doesn't
- # get severed. The boost scales with how many blocks the entity spans.
- # Perf (v0.6.0): iterate only over entities present in this block
- # via the precomputed token set instead of scanning every bridge
- # entity for every block (O(n·e) substring scans → O(n + e)).
- bridge_boost = 0.0
- block_tokens = block_token_sets[idx]
- 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":
- reason = "bridge entity"
- # Uniqueness floor (ACON insight): if a block contains a
- # high-information identifier that appears in NO OTHER block,
- # it's uniquely valuable. Drop it and the fact is gone forever
- # (unless CCR is enabled). Give it a floor boost so scattered
- # facts survive even when the query doesn't mention them.
- # Only applies when the query is weak (low entropy) — a vague
- # "keep track of every detail" query deserves a higher keep
- # threshold. A strong, specific query ("who won the 1998 world
- # cup") should still evict unrelated blocks even if they're
- # unique.
- unique_id_count = 0
- list_noise = b["type"] == "list" and entity_hits == 0 and term_hits == 0
- if weak_boost > 0 and not list_noise:
- # Perf (v0.6.0): reuse the shared token set — no extra regex pass.
- for tl in block_tokens:
- if len(tl) < 6 or tl in _STOP:
- continue
- if _HEX_ID_RE.match(tl):
- continue
- df = tok_df.get(tl, 0)
- if df == 1:
- unique_id_count += 1
- if unique_id_count >= 2:
- score += min(4.0, 1.5 + 0.8 * unique_id_count)
- if reason in ("context",):
- reason = "unique facts"
- # Recency + novelty prior: recent blocks get a ramp up to ~2.0;
- # rare high-information identifiers (short tokens, dotted paths,
- # UPPER_SNAKE, REL-xxx, 8+ hex) give a small floor so scattered
- # facts survive a vague "keep everything" query.
- # Weak-query boost: when the query has low information content,
- # raise both the recency ramp and the novelty floor so middle
- # blocks survive. This is the rate-distortion H(Q) theory: a
- # vague query deserves a higher keep threshold.
- recency_max = 2.0 + weak_boost
- recency = recency_max * (idx + 1) / n
- score += recency
- novelty = 0.0
- rare_count = 0
- # Perf (v0.6.0): reuse block_token_sets instead of re-tokenising
- # and re-lowering every block here (third full token pass removed).
- for tl in block_token_sets[idx]:
- if len(tl) < 4 or tl in _STOP:
- continue
- if _HEX_ID_RE.match(tl):
- continue
- df = tok_df.get(tl, 0)
- if df <= 2 and not re.match(r"^[a-z]{4}$", tl):
- novelty += 0.6
- rare_count += 1
- # Weak-query boost on novelty floor: raise the cap from 2.4 to
- # 2.4 + weak_boost (up to 3.4) when the query is vague.
- score += min(2.4 + weak_boost, novelty)
- # Fact density: blocks with many distinct rare identifiers are
- # information-dense. Even without query overlap, a block packed
- # with rare facts is worth keeping. This rescues scattered facts
- # in large contexts where the query only mentions a few entities.
- # Threshold: 5+ rare tokens → +1.0, 10+ → +2.0, 15+ → +3.0 (cap).
- if rare_count >= 5 and not list_noise:
- fact_density = min(3.0, 0.2 * rare_count)
- score += fact_density
- # Recency / sinks
- if idx == 0:
- score += 0.35
- if idx == n - 1:
- score += 0.45
- fp = re.sub(r"\s+", " ", b["text"].lower())[:240]
- dup = fp_counts.get(fp, 0)
- if dup > 1:
- score -= min(3.0, dup * 0.8)
- if trust_risk:
- score -= 50.0
- reason = "untrusted instruction"
- # Final stale-error cap: after every boost (recency, novelty,
- # fact-density), a handled error dump must stay below the adaptive
- # 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,
- "score": max(0.0, score),
- "reason": reason,
- "entity_hits": entity_hits,
- "term_hits": term_hits,
- "rare_term_hits": rare_term_hits,
- "trust_risk": trust_risk,
- "error_exemplar": error_exemplar,
- }
- )
- return scored
-
-
-def _is_sink_noise(text: str) -> bool:
- """Trailing `-- filler` / `# filler` comments are not a recency sink."""
- lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
- if not lines:
- return False
- return all(re.match(r"^(--|#)\s*filler\b", ln, re.IGNORECASE) for ln in lines)
-
-
-def _query_signal(blocks: list[dict[str, Any]]) -> float:
- hits = sum(1 for b in blocks if b["entity_hits"] or b["term_hits"])
- return hits / max(1, len(blocks))
-
-
-def _important(blocks: list[dict[str, Any]]) -> set[int]:
- n = max(1, len(blocks))
- rare = []
- for b in blocks:
- if b.get("trust_risk"):
- continue
- if b["entity_hits"] and b["score"] >= 4:
- rare.append(b)
- ids: set[int] = set()
- for b in blocks:
- # v0.7.0: stale-error heads are handled history — not "important".
- # 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
- # untrusted instruction block into the keep-set (production QA v3
- # injection finding). The rare-candidate loop above already skips.
- if b.get("trust_risk"):
- continue
- if (
- b["type"] == "trace"
- or (b["entity_hits"] and b["score"] >= 5.0)
- or (b["score"] >= 9 and (b["entity_hits"] or b["term_hits"]))
- or (b["type"] == "definition" and b["entity_hits"])
- ):
- ids.add(b["id"])
- cap = max(3, min(18, math.ceil(n * 0.22)))
- if len(ids) > cap:
- ranked = sorted((blocks[i] for i in ids), key=lambda b: (-b["score"], b["tokens"]))
- ids = {b["id"] for b in ranked[:cap]}
- return ids
-
-
-def _stitch_neighbors(blocks: list[dict[str, Any]], kept: set[int]) -> set[int]:
- by_id = {b["id"]: b for b in blocks}
- extra: set[int] = set()
- for i in list(kept):
- prev_id = i - 1
- if prev_id in by_id and by_id[prev_id]["type"] == "heading":
- extra.add(prev_id)
- nxt = i + 1
- if nxt in by_id and by_id[i]["type"] == "heading":
- extra.add(nxt)
- # A traceback is one unit: if we keep any part of a stack, keep the
- # whole stack and the log line that triggered it (error/exception).
- if by_id[i]["type"] == "trace" or i in extra and by_id[i]["type"] == "trace":
- for nb in (i - 1, i + 1):
- if nb in by_id and by_id[nb]["type"] in {"trace", "log"}:
- t = by_id[nb]["text"].lower()
- if by_id[nb]["type"] == "trace" or re.search(
- r"error|fail|exception|traceback", t
- ):
- extra.add(nb)
- # Code-block stitching: a signature line ("fn delay(...)") and its
- # indented body lines are separate blocks because each indented line
- # classifies as "definition". Keep them whole:
- # 1. kept body line -> pull in the preceding signature (dedent) line
- # 2. kept signature/definition line -> pull in the following
- # indented definition body while it stays inside the function
- # Without this, the gold "what does this cap at?" fact (in the body)
- # can be dropped while the signature stays, or vice versa.
- for i in list(kept):
- b = by_id[i]
- if b["type"] in {"code", "definition"}:
- # Pull preceding dedent signature line if we keep a body line.
- p = i - 1
- while p in by_id and not by_id[p]["text"].strip():
- p -= 1
- if p in by_id:
- pt = by_id[p]["text"]
- indent = len(pt) - len(pt.lstrip())
- bt = b["text"]
- bindent = len(bt) - len(bt.lstrip())
- if (
- b["type"] in {"code", "definition"}
- and bindent > 0
- and indent == 0
- and pt.strip()
- ):
- extra.add(p)
- # Pull following indented definition body lines.
- q = i + 1
- while q in by_id:
- qb = by_id[q]
- qt = qb["text"]
- if qb["type"] in {"definition", "code"} and qt.strip():
- qindent = len(qt) - len(qt.lstrip())
- if qindent > 0:
- extra.add(q)
- q += 1
- continue
- break
- break
- # Structural closure must never undo trust gating. Explicit caller pins
- # remain the sole opt-in override.
- return {
- bid
- for bid in kept | extra
- if not by_id[bid].get("trust_risk") or by_id[bid].get("pinned")
- }
-
-
-def _block_link_terms(text: str) -> set[str]:
- """Rare relation keys used only for bounded evidence-path closure."""
- out = {e.casefold() for e in _extract_entities(text) if len(e) >= 3}
- for raw in _TOKEN_RE.findall(text or ""):
- term = raw.casefold().strip("؟?!.،,;:…\"'()[]")
- if len(term) < 4 or term in _STOP or term in GENERIC_WORDS:
- continue
- # v0.6.0: pure-number tokens ("1001", "2026") are timestamps/ids of
- # no linking value — every numeric log line would share one and
- # fabricate a "coherent chain" for the ambiguity guard.
- if term.isdigit():
- continue
- if _HEX_ID_RE.match(term):
- continue
- out.add(term)
- return out
-
-
-_DERIVE_MIN_TERMS = 3
-_DERIVE_MAX_TERMS = 8
-
-
-def _derive_query_terms(text: str, *, max_terms: int = _DERIVE_MAX_TERMS) -> list[str]:
- """TPC-style derived objective for empty or generic queries.
-
- Infers a conservative task descriptor from the document itself: rare
- content terms that recur — appearing in too few lines marks a passing
- detail, in too many marks boilerplate — ranked by idf·log(1+tf).
- Fully deterministic (frequency statistics only, no model calls).
- Returns [] when the document has no stable term structure; callers
- keep their normal fail-open path in that case.
- """
- if not text or not text.strip():
- return []
- lines = [ln for ln in text.split("\n") if ln.strip()]
- n = len(lines)
- if n < 8:
- return []
- # Bounded work: this runs before the industrial size gate, so huge
- # inputs are sampled deterministically (uniform stride — no head bias).
- if n > 4096:
- stride = math.ceil(n / 4096)
- lines = lines[::stride]
- n = len(lines)
- df: dict[str, int] = {}
- tf: dict[str, int] = {}
- for ln in lines:
- terms = _block_link_terms(ln)
- for t in terms:
- df[t] = df.get(t, 0) + 1
- tf[t] = tf.get(t, 0) + 1
- if not df:
- return []
- # Thematic band: recurs across lines (topic) but not near-everywhere
- # (boilerplate). Cap scales with document size, floor keeps tiny docs strict.
- hi = max(3, int(n * 0.20))
- cands = [t for t, d in df.items() if 2 <= d <= hi]
- if len(cands) < _DERIVE_MIN_TERMS:
- return []
- # idf·log(1+tf), tie-broken alphabetically — set order must not leak in.
- ranked = sorted(
- cands,
- key=lambda t: (-(math.log(1 + n / df[t]) * math.log(1 + tf[t])), t),
- )
- return ranked[:max_terms]
-
-
-def _graph_path_closure(
- blocks: list[dict[str, Any]],
- kept: set[int],
- max_hops: int = 4,
- link_terms: dict[int, set[str]] | None = None,
-) -> set[int]:
- """Keep short rare-term paths connecting already-selected evidence."""
- if len(kept) < 2 or not blocks:
- return kept
- # Perf (v0.6.0): callers that already tokenised every block (CFA runs
- # right after this in select_adaptive) pass their map to avoid a second
- # full-document regex pass.
- if link_terms is None:
- terms_by_id = {
- b["id"]: (set() if b.get("trust_risk") else _block_link_terms(b["text"]))
- for b in blocks
- }
- else:
- terms_by_id = {
- b["id"]: (set() if b.get("trust_risk") else link_terms.get(b["id"], set()))
- for b in blocks
- }
- term_ids: dict[str, list[int]] = {}
- for bid, terms in terms_by_id.items():
- for term in terms:
- term_ids.setdefault(term, []).append(bid)
- # Bridge terms must be genuinely rare. The old len//5 ceiling let a
- # term appear in up to 800 of 4000 blocks and still form edges — in
- # homogeneous dumps that turns adjacency into a dense hub graph and
- # the BFS floods (v0.6.0 perf regression). Hard-cap at 12.
- max_df = max(3, min(len(blocks) // 5, 12))
- adjacency: dict[int, set[int]] = {b["id"]: set() for b in blocks}
- for ids in term_ids.values():
- if len(ids) < 2 or len(ids) > max_df:
- continue
- for left in ids:
- adjacency[left].update(right for right in ids if right != left)
- scores = {b["id"]: float(b.get("score", 0.0)) for b in blocks}
- seeds = sorted(kept, key=lambda bid: scores.get(bid, 0.0), reverse=True)[:24]
- closed = set(kept)
- # v0.6.0 contract: bridge nodes are evidence when they EXTEND a seed's
- # evidence path (chain rescue: Moonlight→Selene→DB-77-Z hangs off the
- # query-hit block), but the total extension is bounded so hub terms in
- # homogeneous dumps cannot flood the keep-set. Nodes are admitted
- # closest-seed-distance first, up to a budget proportional to the seed
- # count; everything beyond the budget stays dropped.
- reach: dict[int, int] = {} # node -> nearest seed BFS depth
- for src in seeds:
- queue = [(src, 0)]
- # Standard BFS: mark depth at enqueue time, but EXPAND a node when
- # it is dequeued unless an equal-or-better depth was already
- # expanded. The previous guard also skipped expansion on first
- # discovery, so chains died after one hop (v0.6.0 regression).
- expanded: set[int] = set()
- for node, depth in queue:
- if depth >= max_hops:
- continue
- d = reach.get(node)
- if d is not None and (d < depth or node in expanded):
- continue
- reach[node] = depth
- expanded.add(node)
- for nxt in adjacency.get(node, ()):
- nd = reach.get(nxt)
- if nd is None or nd > depth + 1:
- reach[nxt] = depth + 1
- queue.append((nxt, depth + 1))
- budget = max(8, 2 * len(seeds))
- candidates = sorted(
- ((d, n) for n, d in reach.items() if n not in closed),
- key=lambda x: (x[0], -scores.get(x[1], 0.0)),
- )
- # Bridge nodes must carry some intrinsic value — a near-zero-score
- # block that merely shares a token with a seed is noise, not evidence
- # (v0.6.0: '- ccache' style lines were flooding the keep-set).
- min_bridge_score = min(3.0, max(scores.get(s, 0.0) for s in seeds) * 0.25 if seeds else 3.0)
- for _, node in candidates[:budget]:
- if scores.get(node, 0.0) >= min_bridge_score:
- closed.add(node)
- return closed
-
-
-# Qualifier/negation cues: a dropped block carrying one of these while
-# sharing a rare entity with a kept block is a QUALIFIES edge — cutting it
-# can invert the surviving claim ("safe for adults" vs "safe for adults,
-# except when..."). Same vocabulary class as lcc's TRIM refusal.
-_QUALIFIER_CUE_RE = re.compile(
- r"\b(except|exception|excluding|excluded|unless|only|not\b|no\b|never|"
- r"but\b|however|although|though|nevertheless|otherwise|without|despite|"
- r"until|provided\s+that|subject\s+to|if)\b",
- re.IGNORECASE,
-)
-
-# Definition cues: the dropped block DEFINES a term the kept block uses.
-_DEFINITION_CUE_RE = re.compile(
- r"\b(is\s+defined\s+as|refers?\s+to|stands\s+for|means|i\.e\.|namely|denotes)\b",
- re.IGNORECASE,
-)
-
-
-def _dependency_closure(
- blocks: list[dict[str, Any]],
- kept: set[int],
- *,
- link_terms: dict[int, set[str]] | None = None,
- budget_tokens: int | None = None,
-) -> tuple[set[int], list[int]]:
- """Sufficiency restore over typed edges (lcc graph.closure lesson).
-
- A dropped block that QUALIFIES or DEFINES a rare term carried by a
- kept block changes the meaning of what survives — restore it. Rare
- means document-frequency <= cap, so only genuinely linking terms form
- edges; restoration is document-ordered and capped at 8 blocks so a
- cue-dense corpus cannot flood the keep-set. Never resurrects
- trust-risk or frozen-drop blocks; supersession still runs after this
- and can evict stale restorations. ``budget_tokens`` (fixed mode)
- refuses a restoration that would overflow the caller's hard budget.
- """
- if not kept or len(blocks) < 2:
- return kept, []
- if link_terms is None:
- link_terms = {
- b["id"]: (set() if b.get("trust_risk") else _block_link_terms(b["text"]))
- for b in blocks
- }
- df: dict[str, int] = {}
- for terms in link_terms.values():
- for term in terms:
- df[term] = df.get(term, 0) + 1
- rare_cap = min(6, max(3, len(blocks) // 8))
- kept_rare = {
- term
- for bid in kept
- for term in link_terms.get(bid, ())
- if df.get(term, 0) <= rare_cap
- }
- if not kept_rare:
- return kept, []
- used = sum(b["tokens"] for b in blocks if b["id"] in kept) if budget_tokens is not None else 0
- restored: list[int] = []
- for b in blocks: # document order -> deterministic
- if len(restored) >= 8:
- break
- bid = b["id"]
- if bid in kept or b.get("trust_risk") or b.get("freeze_decision") == "drop":
- continue
- if not (link_terms.get(bid, set()) & kept_rare):
- continue
- text = b["text"]
- if not (_QUALIFIER_CUE_RE.search(text) or _DEFINITION_CUE_RE.search(text)):
- continue
- if budget_tokens is not None and used + b["tokens"] > budget_tokens:
- continue
- restored.append(bid)
- used += b["tokens"]
- if not restored:
- return kept, []
- return kept | set(restored), restored
-
-
-def _counterfactual_overlap_ambiguity(
- blocks: list[dict[str, Any]],
- kept: set[int],
- *,
- query: str,
- link_terms: dict[int, set[str]] | None = None,
- semantic_tier: Any = None,
- log_dir: str | Path | None = None,
- pin_patterns: Optional[list[str]] = None,
-) -> bool:
- """Detect a high-scoring kept distractor masking a dropped answer chain.
-
- Fires ONLY when:
- 1. A kept block scores high on query-term overlap (the "winner").
- 2. Two or more dropped blocks share a *rare, non-generic* link term
- (appearing in ≤3 blocks document-wide, length ≥4, not in
- GENERIC_WORDS) AND at least one of them mentions a query content
- word as a whole word — i.e. a coherent, query-relevant chain
- was evicted.
-
- This intentionally does NOT fire for generic high-volume logs (git, npm)
- where shared tokens like dates, SHAs, "verbose", "docs", "commit" are
- ubiquitous and would cause false positives.
- """
- kept_with_terms = [
- b
- for b in blocks
- if b["id"] in kept
- and not b.get("trust_risk", False)
- and (b.get("term_hits") or b.get("entity_hits"))
- ]
- if not kept_with_terms:
- return False
-
- winner = max(
- kept_with_terms,
- key=lambda b: (
- b.get("rare_term_hits", 0)
- + b.get("entity_hits", 0)
- + b.get("term_hits", 0),
- b["score"],
- ),
- )
-
- content = _query_content_terms(query)
- if not content:
- return False
-
- # If the kept set is already large enough that the answer is likely
- # present, a single distractor cannot meaningfully mask it. The guard
- # only fires when the kept set is small (<10% of the document) AND the
- # winner is a single high-scoring block (not a coherent multi-block
- # chain). This targets the narrow production QA P0: one distractor block
- # outscoring a linked answer chain that was evicted.
- if len(kept) / max(1, len(blocks)) >= 0.10:
- return False
-
- # Precompile word-boundary patterns for each query content term.
- content_patterns = [re.compile(r"\b" + re.escape(t) + r"\b") for t in content]
-
- # The dropped chain must be scoring well (near the floor) to count as
- # a "genuinely evicted answer". Low-scoring dropped blocks are just
- # noise, not a hidden answer. Use 0.38× the top safe score as the
- # threshold (same floor as the main selection path).
- safe_blocks = [b for b in blocks if not b.get("trust_risk")]
- top_score = max((b["score"] for b in safe_blocks), default=0.0)
- floor = max(2.2, min(11.5, 0.38 * top_score))
-
- dropped = [
- b
- for b in blocks
- if b["id"] != winner["id"]
- and b["id"] not in kept
- and not b.get("trust_risk", False)
- and b["score"] >= floor
- ]
- if len(dropped) < 2:
- return False
-
- # Perf bound (v0.6.0): the pair scan below is O(d²) with per-pair set
- # intersections and regex searches. Cap the candidate pool to the top-
- # scoring dropped blocks — a masked answer chain lives at the top of
- # the evicted mass, never in the tail.
- if len(dropped) > 64:
- dropped = sorted(dropped, key=lambda b: -b["score"])[:64]
-
- if link_terms is not None:
- def _terms_of(b: dict[str, Any]) -> set[str]:
- return set() if b.get("trust_risk") else link_terms.get(b["id"], set())
- else:
- def _terms_of(b: dict[str, Any]) -> set[str]:
- return set() if b.get("trust_risk") else _block_link_terms(b["text"])
-
- max_df = 3
-
- # Document-wide df for the candidates' link terms, computed with a
- # cheap substring scan over pre-lowered texts instead of tokenising
- # every block (v0.6.0 perf: 4000 regex passes → ~len(cand_terms)
- # str-in checks). Substring df can overcount when a term is a
- # fragment of a longer word; that only makes the rarity gate MORE
- # conservative (term judged common → not a chain link), which is safe.
- cand_term_set: set[str] = set()
- for b in dropped:
- cand_term_set |= _terms_of(b)
- lower_texts = [b["text"].casefold() for b in blocks]
- term_df: dict[str, set[int]] = {t: set() for t in cand_term_set}
- for idx, lt in enumerate(lower_texts):
- for t in cand_term_set:
- if t in lt:
- term_df[t].add(idx)
-
- def _meaningful_shared(term: str) -> bool:
- """A shared link term must be rare, long enough, and not generic."""
- if len(term) < 4:
- return False
- if term in GENERIC_WORDS:
- return False
- return len(term_df.get(term, ())) <= max_df
-
- rare_terms_by_block: dict[int, set[str]] = {}
- for b in dropped:
- terms = _terms_of(b)
- rare = {t for t in terms if _meaningful_shared(t)}
- rare_terms_by_block[b["id"]] = rare
-
- # Inverted index over the candidates' rare terms: propose pairs by
- # shared term instead of scanning all pairs.
- postings: dict[str, list[int]] = {}
- for b in dropped:
- for t in rare_terms_by_block[b["id"]]:
- postings.setdefault(t, []).append(b["id"])
- candidate_pairs: list[tuple[int, int]] = []
- seen_pairs: set[tuple[int, int]] = set()
- for ids in postings.values():
- if len(ids) < 2 or len(ids) > max_df:
- continue
- for i in range(len(ids)):
- for j in range(i + 1, len(ids)):
- key = (ids[i], ids[j])
- if key not in seen_pairs:
- seen_pairs.add(key)
- candidate_pairs.append(key)
- by_id = {b["id"]: b for b in dropped}
- for left_id, right_id in candidate_pairs:
- left = by_id[left_id]
- right = by_id[right_id]
- left_has_content = any(p.search(left["text"].lower()) for p in content_patterns)
- if left_has_content:
- return True
- if any(p.search(right["text"].lower()) for p in content_patterns):
- return True
-
- # Semantic upgrade (v0.8.0): the lexical scan found no shared-term
- # chain. With a tier attached, ambiguity now means: >=2 dropped
- # candidates score within eps of the winner in embedding space —
- # i.e. paraphrase distractors that share NO vocabulary with the query.
- if semantic_tier is not None:
- try:
- tier = semantic_tier
- if not getattr(tier, "available", False):
- return False
- cands = [
- b for b in blocks
- if b["id"] != winner["id"] and b["id"] not in kept
- and not b.get("trust_risk", False)
- ]
- if len(cands) < 2:
- return False
- capped = sorted(cands, key=lambda b: -b["score"])[:32]
- _, sims = tier.score_against_query(query, [c["text"] for c in capped])
- win_sim = tier.score_against_query(query, [winner["text"]])[1][0]
- near = [c for c, sm in zip(capped, sims) if abs(sm - win_sim) < 0.05]
- return len(near) >= 2
- except Exception: # tier failure must NEVER break compression
- return False
- return False
-
-
-def select_adaptive(
- blocks: list[dict[str, Any]],
- *,
- needle_only: bool = False,
- query: str = "",
- semantic_tier: Any = None,
- out: dict[str, Any] | None = None,
-) -> tuple[set[int], bool, str]:
- def _ret(kept, fail, risk, name):
- if out is not None:
- out["path"] = name
- return kept, fail, risk
-
- if not blocks:
- return _ret(set(), True, "high", "empty")
- signal = _query_signal(blocks)
- if signal < 0.04 and max(b["score"] for b in blocks) < 3.5:
- return _ret({b["id"] for b in blocks}, True, "high", "weak-signal")
- hits = {b["id"] for b in blocks if b["entity_hits"] or b["term_hits"]}
- rare = {
- b["id"]
- 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) | err_keep
- if not blocks[0].get("trust_risk") or blocks[0].get("pinned"):
- kept.add(blocks[0]["id"])
- if (
- not _is_sink_noise(blocks[-1]["text"])
- and not blocks[-1].get("trust_risk")
- and blocks[-1]["id"] in use
- ):
- kept.add(blocks[-1]["id"])
- # Perf (v0.6.0): tokenise once, share with the CFA guard below.
- link_terms_map = {
- b["id"]: (set() if b.get("trust_risk") else _block_link_terms(b["text"]))
- for b in blocks
- }
- kept = _graph_path_closure(blocks, kept, link_terms=link_terms_map)
- # Needle-path saturation guard: on bilingual/mirrored documents the
- # closure floods (EN/JP block pairs share bridge terms) and the
- # "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) | err_keep
- if not blocks[0].get("trust_risk") or blocks[0].get("pinned"):
- kept.add(blocks[0]["id"])
- kept = _stitch_neighbors(blocks, kept)
- if _counterfactual_overlap_ambiguity(
- blocks, kept, query=query, link_terms=link_terms_map, semantic_tier=semantic_tier
- ):
- return _ret({b["id"] for b in blocks}, True, "high", "needle-ambiguous")
- kept, restored = _dependency_closure(blocks, kept, link_terms=link_terms_map)
- if restored and out is not None:
- out["sufficiency_restored"] = restored
- return _ret(kept, False, "low", "needle")
- important = _important(blocks)
- safe_blocks = [b for b in blocks if not b.get("trust_risk")]
- top = max((b["score"] for b in safe_blocks), default=0.0)
- # Floor: 0.38× the top score, but capped so one dominant block
- # (e.g. a head block with many query terms) doesn't evict all the
- # middle. The cap is 11.5 — high enough to keep the top blocks,
- # low enough that scattered facts with scores 8-12 survive.
- # 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) | err_keep
- for b in safe_blocks:
- if b["score"] >= floor:
- kept.add(b["id"])
- # Saturation guard (v0.6.0, production QA v3 performance finding): on huge
- # homogeneous dumps every line matches a broad query, so the floor
- # keeps everything and the payload never shrinks. When the floor pass
- # keeps nearly all blocks AND the query is broad (no distinctive
- # selector), fall back to the important-set only — the ranked top of
- # the document — instead of the whole dump.
- floor_ratio = sum(b["tokens"] for b in safe_blocks if b["id"] in kept) / max(
- 1, sum(b["tokens"] for b in safe_blocks)
- )
- path = "floor"
- if floor_ratio > 0.9 and len(safe_blocks) > 64 and not needle_only:
- 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
- # history — citations preserve its fact; don't re-admit the dump.
- if (
- (blocks[0].get("reason") != "stale error" or len(kept) == 0)
- and (not blocks[0].get("trust_risk") or blocks[0].get("pinned"))
- ):
- kept.add(blocks[0]["id"])
- if (
- not _is_sink_noise(blocks[-1]["text"])
- and not blocks[-1].get("trust_risk")
- ):
- kept.add(blocks[-1]["id"])
- # Perf (v0.6.0): the graph closure tokenises every block (regex-heavy).
- # When the keep-set has at most one member there is nothing to bridge,
- # and on huge weak-signal documents that is the common case — skip it.
- # Otherwise tokenise ONCE and share the map between the closure and the
- # CFA guard below (saves a second full-document regex pass).
- link_terms_map: dict[int, set[str]] | None = None
- if len(kept) >= 2:
- link_terms_map = {
- b["id"]: (set() if b.get("trust_risk") else _block_link_terms(b["text"]))
- for b in blocks
- }
- kept = _graph_path_closure(blocks, kept, link_terms=link_terms_map)
- kept = _stitch_neighbors(blocks, kept)
- keep_ratio = sum(blocks[i]["tokens"] for i in kept) / max(1, sum(b["tokens"] for b in blocks))
- # Line dumps (git / npm): Super keeps matching records only. Priors
- # (unique SHAs, dates on every line) must not keep the whole log.
- # This check must come before the keep_ratio guard because line records
- # have a well-defined compression strategy that the generic "keep too
- # much -> fail open" escape would preempt.
- texts = [b["text"] for b in blocks]
- if _looks_like_line_records(texts):
- path = "line-records"
- kept = {
- b["id"]
- for b in blocks
- if (b["entity_hits"] or b["term_hits"]) and not b.get("trust_risk")
- }
- 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 (
- not _is_sink_noise(blocks[-1]["text"])
- and not blocks[-1].get("trust_risk")
- ):
- kept.add(blocks[-1]["id"])
- kept = _stitch_neighbors(blocks, kept)
- keep_ratio = sum(blocks[i]["tokens"] for i in kept) / max(1, sum(b["tokens"] for b in blocks))
- if keep_ratio > 0.92 and signal < 0.12:
- return _ret({b["id"] for b in blocks}, True, "high", "saturated-failopen")
- if _counterfactual_overlap_ambiguity(
- blocks, kept, query=query, link_terms=link_terms_map, semantic_tier=semantic_tier
- ):
- return _ret({b["id"] for b in blocks}, True, "high", "ambiguous-failopen")
- kept, restored = _dependency_closure(blocks, kept, link_terms=link_terms_map)
- if restored and out is not None:
- out["sufficiency_restored"] = restored
- risk = "low"
- if keep_ratio > 0.8:
- risk = "medium"
- if not important and signal < 0.1:
- # 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)
-
-
-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"
- if not blocks:
- return set(), True, "high"
- total = sum(b["tokens"] for b in blocks)
- pinned_ids = {b["id"] for b in blocks if b.get("pinned")}
- if pinned_ids:
- # ARGP-style: the ratio governs compressible tokens only. Pins are
- # immovable — they must not consume the budget they outrank.
- pinned_tokens = sum(b["tokens"] for b in blocks if b["id"] in pinned_ids)
- budget = pinned_tokens + max(
- 1, round(max(0, total - pinned_tokens) * budget_ratio)
- )
- else:
- budget = max(1, 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"]),
- )
- # 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:
- continue
- if used >= budget and used >= max(1, budget // 2):
- # still take a high-score leftover if we have almost no query hits
- if b["score"] < 6:
- continue
- if used + b["tokens"] > budget * 1.15 and used >= budget * 0.6 and b["id"] not in important:
- continue
- kept.add(b["id"])
- used += b["tokens"]
- if used >= budget and len(kept) >= max(2, len(important)):
- break
- if not blocks[0].get("trust_risk") or blocks[0].get("pinned"):
- kept.add(blocks[0]["id"])
- if (
- not _is_sink_noise(blocks[-1]["text"])
- and (not blocks[-1].get("trust_risk") or blocks[-1].get("pinned"))
- ):
- kept.add(blocks[-1]["id"])
- kept = _stitch_neighbors(blocks, kept)
- kept, restored = _dependency_closure(blocks, kept, budget_tokens=budget)
- if restored and out is not None:
- out["sufficiency_restored"] = restored
- return kept, False, "low"
-
-
-def _query_content_terms(query: str) -> set[str]:
- """Content words in the query, after generic stop-words are removed."""
- return set(_extract_terms(query))
-
-
-def _entity_recall(original: str, compressed: str, query: str) -> float:
- keys = []
- for item in _extract_entities(query) + _extract_terms(query):
- if item.lower() in original.lower() and item not in keys:
- keys.append(item)
- if not keys:
- return 1.0
- hit = sum(1 for k in keys if k.lower() in compressed.lower())
- 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 _CRITICAL_ERROR_RE.search(scan):
- critical.append(t)
- continue
- start = _lead_ts_end(scan)
- # Lines with key-value patterns (config, settings, identifiers), or
- # lines containing specific numbers (dates, IDs, versions, counts)
- if (len(t) > 15 and _CRITICAL_KV_RE.search(scan, start)) or _CRITICAL_NUM_RE.search(
- scan, start
- ):
- critical.append(t)
- return critical
-
-
-def _critical_line_recall(original: str, compressed: str) -> tuple[int, int]:
- """Return (kept, total) counts of critical lines, matching Baseline RAG
- verifier's critical_lines_kept/critical_lines_total fields."""
- critical = _extract_critical_lines(original)
- 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 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)
-
-
-def verify_compression(
- original: str,
- compressed: str,
- query: str,
-) -> dict[str, Any]:
- """Post-compression self-check, mirroring Baseline RAG's verifier object.
-
- Returns a dict with:
- entity_recall — fraction of query entities/terms present in compressed
- keyword_recall — fraction of query terms present in compressed
- important_kept_pct — fraction of critical lines retained
- critical_lines_total / kept / dropped
- risk — "low" / "medium" / "high" based on recall thresholds
- score — overall 0-1 quality score (weighted blend)
-
- This is a diagnostic pass: it does NOT modify the compressed output.
- Callers can use the result to decide whether to re-compress with a
- different strategy, expand dropped blocks, or fail open.
- """
- entity_recall = _entity_recall(original, compressed, query)
-
- # Keyword recall: fraction of query terms that appear in compressed
- terms = _extract_terms(query)
- if terms:
- hit_terms = sum(1 for t in terms if t in compressed.lower())
- keyword_recall = hit_terms / len(terms)
- else:
- keyword_recall = 1.0
-
- cl_kept, cl_total = _critical_line_recall(original, compressed)
- important_kept_pct = (cl_kept / cl_total) if cl_total > 0 else 1.0
- critical_lines_dropped = cl_total - cl_kept
-
- # Risk assessment: combine all three signals
- risk = "low"
- if entity_recall < 0.5:
- risk = "high"
- elif entity_recall < 0.8:
- risk = "medium"
- if keyword_recall < 0.5 and risk == "low":
- risk = "medium"
- elif keyword_recall < 0.3:
- risk = "high"
- if important_kept_pct < 0.5 and risk != "high":
- risk = "high" if important_kept_pct < 0.3 else "medium"
-
- # Overall score: weighted blend (entity recall matters most for
- # answer quality, keyword recall next, critical lines last)
- score = round(
- 0.5 * entity_recall + 0.3 * keyword_recall + 0.2 * important_kept_pct,
- 3,
- )
-
- return {
- "entity_recall": round(entity_recall, 3),
- "keyword_recall": round(keyword_recall, 3),
- "important_kept_pct": round(important_kept_pct, 3),
- "critical_lines_total": cl_total,
- "critical_lines_kept": cl_kept,
- "critical_lines_dropped": critical_lines_dropped,
- "risk": risk,
- "score": score,
- }
-
-
-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
- # front, second-best anchors the end, the rest stay chronological.
- ranked = sorted(order, key=lambda b: -float(b.get("score", 0.0)))
- 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 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.
- # Only emit citations when they're net-positive: the stub
- # tokens must be less than the dropped block tokens they replace.
- left = bisect.bisect_right(block_starts, prev_end)
- right = bisect.bisect_left(block_starts, b["start"])
- dropped = [
- block
- for block in chronological_blocks[left:right]
- if block["id"] not in kept and block["end"] < b["start"]
- ]
- gap_tokens = 0
- dropped_tokens = 0
- for d in dropped:
- dropped_tokens += d["tokens"]
- d_text = d.get("full_text") or d["text"]
- head = d_text.split("\n", 1)[0][:60]
- tail = d_text.rsplit("\n", 1)[-1][:60]
- stub = f'[§] "{head}"…"{tail}"'
- gap_tokens += estimate_tokens(stub)
- if gap_tokens < dropped_tokens:
- # Citations save tokens — emit them. For long runs of
- # consecutive dropped blocks, per-block stubs defeat
- # the purpose (v0.6.0: 4000-block dump → 500KB of
- # stubs). Collapse runs longer than 8 into one summary.
- if len(dropped) > 8:
- first = dropped[0]
- last = dropped[-1]
- parts.append(
- f"[§run:{len(dropped)} blocks #{first['start']}–{last['end']} elided]"
- )
- else:
- for d in dropped:
- d_text = d.get("full_text") or d["text"]
- digest = hashlib.sha256(d_text.encode("utf-8")).hexdigest()[:8]
- head = d_text.split("\n", 1)[0][:60]
- tail = d_text.rsplit("\n", 1)[-1][:60]
- parts.append(f'[§{digest}] "{head}"…"{tail}"')
- else:
- # Citations would add more tokens than they save — use bare gap
- parts.append("[…]")
- else:
- parts.append("[…]")
- start, end = b["start"], b["end"]
- # Structural verbatim invariant (leanctx-inspired): a kept fenced
- # code block must survive whole — extend the keep range to the
- # closing fence if it lands just outside, so agents see exact tokens.
- kept_text = lines[start : end + 1]
- fences = sum(1 for ln in kept_text if ln.strip().startswith("```"))
- if fences % 2 == 1:
- j = end + 1
- while j < len(lines):
- if lines[j].strip().startswith("```"):
- end = j
- break
- 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"
- "The following text is extractive context: original wording only, "
- "low-value blocks removed. Use it to answer the user.\n\n"
- "--- context ---\n"
- f"{compressed}\n"
- "--- end context ---\n"
- "</compressed_context>\n\n"
- f"Answer the question: {query}"
- )
-
-
-def _block_fingerprint(block: dict[str, Any]) -> str:
- """Stable fingerprint for freeze-on-first-sight: type + id + content.
-
- The block id distinguishes repeated identical records that can receive
- different head/middle/tail decisions. Appending turns preserves prior ids.
- """
- content_hash = hashlib.sha256(block["text"].encode("utf-8")).hexdigest()[:12]
- return f"{block['type']}:{block.get('id', '?')}:{content_hash}"
-
-
-def _apply_freeze(
- decision_cache: dict,
- scored: list[dict[str, Any]],
- original_text: str,
- query: str = "",
-) -> list[dict[str, Any]]:
- """Replay stored keep/drop decisions for blocks whose fingerprint matches.
-
- The cache maps block fingerprint -> 'keep' | 'drop'. On a multi-turn
- session, the first turn that sees a block makes the decision; later turns
- replay it byte-identically so the provider prompt cache stays warm.
-
- New blocks remain undecided until selection has completed. Their actual
- keep/drop outcome is recorded by :func:`_record_freeze_decisions`.
- """
- cache_key = "decisions"
- schema_version = decision_cache.get("schema_version")
- if schema_version is not None and schema_version != 1:
- for stale_key in (
- cache_key,
- "query_hash",
- "ctx_hash",
- "ctx_prefix_len",
- ):
- decision_cache.pop(stale_key, None)
- if not isinstance(decision_cache.get(cache_key), dict):
- decision_cache[cache_key] = {}
- decisions: dict = decision_cache[cache_key]
- decision_cache["schema_version"] = 1
-
- query_hash = hashlib.sha256((query or "").encode("utf-8")).hexdigest()[:12]
- cached_query_hash = decision_cache.get("query_hash")
- if isinstance(cached_query_hash, str) and cached_query_hash != query_hash:
- decisions = {}
- decision_cache[cache_key] = decisions
- decision_cache["query_hash"] = query_hash
-
- # Store a hash and length for the original prefix. Length matters when the
- # first turn is shorter than 500 chars: hashing a fresh 500-char slice on
- # the next turn would include appended content and falsely invalidate the
- # cache. No plaintext context is retained in the decision cache.
- cached_prefix_len = decision_cache.get("ctx_prefix_len")
- if not isinstance(cached_prefix_len, int) or cached_prefix_len < 0:
- cached_prefix_len = min(len(original_text), 500)
- decision_cache["ctx_prefix_len"] = cached_prefix_len
- ctx_hash = hashlib.sha256(
- original_text[:cached_prefix_len].encode("utf-8")
- ).hexdigest()[:12]
- if "ctx_hash" not in decision_cache:
- decision_cache["ctx_hash"] = ctx_hash
- elif decision_cache["ctx_hash"] != ctx_hash:
- # Context changed fundamentally — clear decisions to avoid stale replay.
- decisions = {}
- decision_cache[cache_key] = decisions
- cached_prefix_len = min(len(original_text), 500)
- decision_cache["ctx_prefix_len"] = cached_prefix_len
- ctx_hash = hashlib.sha256(
- original_text[:cached_prefix_len].encode("utf-8")
- ).hexdigest()[:12]
- decision_cache["ctx_hash"] = ctx_hash
-
- # Tag old decisions for enforcement after the normal selector runs.
- result = []
- for b in scored:
- fp = _block_fingerprint(b)
- decision = decisions.get(fp)
- if decision not in {"keep", "drop"}:
- decision = None
- result.append(
- {
- **b,
- "frozen": decision is not None,
- "freeze_decision": decision,
- }
- )
- return result
-
-
-def _enforce_frozen_decisions(
- scored: list[dict[str, Any]], kept: set[int]
-) -> set[int]:
- """Apply cached decisions without allowing them to bypass trust or pins."""
- enforced = set(kept)
- for b in scored:
- bid = b["id"]
- if b.get("pinned"):
- enforced.add(bid)
- continue
- decision = b.get("freeze_decision")
- if decision == "drop":
- enforced.discard(bid)
- elif decision == "keep" and not b.get("trust_risk"):
- enforced.add(bid)
- return enforced
-
-
-def _record_freeze_decisions(
- decision_cache: dict,
- scored: list[dict[str, Any]],
- kept: set[int],
-) -> bool:
- """Record first-sight outcomes, preserving the oldest bounded prefix."""
- decisions = decision_cache.setdefault("decisions", {})
- if not isinstance(decisions, dict):
- decisions = {}
- decision_cache["decisions"] = decisions
- saturated = False
- for b in scored:
- fp = _block_fingerprint(b)
- if fp in decisions:
- continue
- if len(decisions) >= FREEZE_MAX_DECISIONS:
- saturated = True
- break
- decisions[fp] = "keep" if b["id"] in kept else "drop"
- return saturated
-
-
-def _ccr_metadata(data: Any) -> tuple[float, float] | None:
- """Return validated ``(stored_at, ttl)`` metadata for a CCR record."""
- if not isinstance(data, dict):
- return None
- try:
- stored = float(data["stored_at"])
- ttl = float(data.get("ttl", CCR_TTL_SECONDS))
- except (KeyError, TypeError, ValueError):
- return None
- if (
- not math.isfinite(stored)
- or not math.isfinite(ttl)
- or stored <= 0
- or ttl <= 0
- ):
- return None
- return stored, ttl
-
-
-def sweep_ccr_cache(
- ccr_dir: str | Path = DEFAULT_CCR_DIR,
- *,
- now: float | None = None,
- max_records: int | None = None,
-) -> int:
- """Delete expired valid CCR records and return the removal count.
-
- 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)
- if not root.is_dir():
- return 0
- current = time.time() if now is None else float(now)
- removed = 0
- limit = None if max_records is None else max(0, int(max_records))
- # Cursor progression only works when every call sees the same ordering;
- # filesystem glob order is unspecified and may change between writes.
- records = sorted(root.glob("*.json"), key=lambda path: path.name)
- if limit is not None and records:
- total = len(records)
- start = _CCR_SWEEP_CURSOR % total
- count = min(limit, total)
- records = [records[(start + offset) % total] for offset in range(count)]
- _CCR_SWEEP_CURSOR += count
- for fp in records:
- try:
- data = json.loads(fp.read_text(encoding="utf-8"))
- except (OSError, json.JSONDecodeError, RecursionError):
- continue
- metadata = _ccr_metadata(data)
- if (
- metadata is not None
- and metadata[0] <= current + CCR_MAX_CLOCK_SKEW_SECONDS
- and current - metadata[0] <= metadata[1]
- ):
- continue
- try:
- fp.unlink()
- removed += 1
- except OSError:
- continue
- return removed
-
-
-def _ccr_store(original: str, ccr_dir: str | Path) -> dict[str, Any] | None:
- # Secrets screen: archival would persist credentials to disk at rest.
- if _contains_secret(original):
- return None
- path = Path(ccr_dir)
- path.mkdir(parents=True, exist_ok=True, mode=0o700)
- if path.is_symlink() or not path.is_dir():
- raise OSError(f"CCR directory must be a real directory: {path}")
- try:
- path.chmod(0o700)
- except OSError:
- pass
- sweep_ccr_cache(path, max_records=CCR_SWEEP_MAX_RECORDS)
- digest = hashlib.sha256(original.encode("utf-8")).hexdigest()[:24]
- payload = {
- "hash": digest,
- "stored_at": time.time(),
- "ttl": CCR_TTL_SECONDS,
- "original": original,
- }
- record = path / f"{digest}.json"
- encoded = json.dumps(payload).encode("utf-8")
- fd: int | None = None
- temporary: str | None = None
- try:
- fd, temporary = tempfile.mkstemp(prefix=".ccr-", dir=path)
- if hasattr(os, "fchmod"):
- os.fchmod(fd, 0o600)
- with os.fdopen(fd, "wb") as fh:
- fd = None
- fh.write(encoded)
- fh.flush()
- os.fsync(fh.fileno())
- os.replace(temporary, record)
- temporary = None
- record.chmod(0o600)
- except OSError:
- if fd is not None:
- try:
- os.close(fd)
- except OSError:
- pass
- raise
- finally:
- if temporary is not None:
- try:
- Path(temporary).unlink()
- except OSError:
- pass
- return {"hash": digest, "path": str(record)}
-
-
-def _write_private_atomic(path: str | Path, content: str) -> None:
- """Atomically replace a UTF-8 sidecar with owner-only permissions."""
- target = Path(path)
- target.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
- try:
- target.parent.chmod(0o700)
- except OSError:
- pass
- fd: int | None = None
- temporary: str | None = None
- try:
- fd, temporary = tempfile.mkstemp(prefix=f".{target.name}.", dir=target.parent)
- if hasattr(os, "fchmod"):
- os.fchmod(fd, 0o600)
- with os.fdopen(fd, "w", encoding="utf-8") as fh:
- fd = None
- fh.write(content)
- fh.flush()
- os.fsync(fh.fileno())
- os.replace(temporary, target)
- temporary = None
- target.chmod(0o600)
- finally:
- if fd is not None:
- os.close(fd)
- if temporary is not None:
- try:
- Path(temporary).unlink()
- except OSError:
- pass
-
-
-def _clear_tool_payloads(text: str) -> str:
- """Strategy='clear': drop re-fetchable tool-result payloads.
-
- Finds lines that look like tool output (indented JSON, long indented
- blocks after a tool-call line) and replaces them with a one-line
- marker. The tool name and call signature are preserved. This is the
- cheapest compression step — zero LLM cost, just regex.
-
- Patterns handled:
- - Lines starting with 2+ spaces that contain JSON braces/brackets
- (tool output indented under a tool-call line)
- - Long JSON blobs (> 200 chars) that appear to be tool results
- - Multi-line indented blocks following a 'TOOL:' or '> ' prefix
-
- Returns the text with payloads replaced by [tool-result: N chars cleared].
- """
- lines = text.split("\n")
- out: list[str] = []
- i = 0
- while i < len(lines):
- line = lines[i]
- stripped = line.lstrip()
- # Detect tool-result start: indented JSON, or a line that looks
- # like a tool output marker.
- is_tool_result = False
- if (
- (line.startswith(" ") or line.startswith("\t"))
- and (
- stripped.startswith("{")
- or stripped.startswith("[")
- or stripped.startswith('"')
- )
- ) or (stripped.startswith("TOOL:") and len(stripped) > 30):
- is_tool_result = True
- elif re.match(r"^\s{2,}\w[^(]*\(", line) and "=>" in stripped:
- # Indented function-call line with an arrow (tool call result)
- is_tool_result = True
-
- if is_tool_result:
- # Collect the full payload (consecutive indented lines)
- j = i
- total_chars = 0
- while j < len(lines):
- payload_line = lines[j]
- if payload_line.strip() == "":
- # Blank line might end the payload or be part of it.
- # Look ahead: if the next non-blank line is also indented,
- # the payload continues.
- k = j + 1
- while k < len(lines) and lines[k].strip() == "":
- k += 1
- if k < len(lines) and (
- lines[k].startswith(" ") or lines[k].startswith("\t")
- ):
- j += 1
- continue
- else:
- break
- elif lines[j].startswith(" ") or lines[j].startswith("\t"):
- total_chars += len(payload_line)
- j += 1
- else:
- break
- total_chars += len(line)
- # Only clear if the payload is substantial (> 100 chars)
- if total_chars > 100:
- # Extract the tool name if visible
- tool_name = ""
- m = re.search(r"(?:TOOL:|=>)\s*(\w+)", line)
- if m:
- tool_name = m.group(1)
- marker = f" [tool-result{':' + tool_name if tool_name else ''}: {total_chars} chars cleared — re-fetch if needed]"
- out.append(marker)
- i = j
- continue
- out.append(line)
- i += 1
- return "\n".join(out)
-
-
-def _summarise_with_llm(
- text: str,
- query: str,
- target_ratio: float = 0.25,
- *,
- endpoint: str | None = None,
- model_candidates: Iterable[str] | str | None = None,
- timeout: float | None = None,
- allow_remote: bool | None = None,
-) -> Optional[str]:
- """Strategy='summarise': use a local LLM to compress the context.
-
- Calls an OpenAI-compatible endpoint. Endpoint, model candidates, and the
- total retry budget can be supplied directly or through the
- ``TAMERU_SUMMARY_ENDPOINT``, ``TAMERU_SUMMARY_MODELS``, and
- ``TAMERU_SUMMARY_TIMEOUT`` environment variables.
- Returns the summarised text, or None on any failure (fail-open).
-
- The prompt asks the model to produce a faithful summary that:
- - Preserves all entities, identifiers, and facts
- - Keeps the query-relevant content verbatim
- - Drops filler and re-fetchable tool payloads
- - Targets ~target_ratio of the original length
-
- This is the "smart" end of the strategy ladder: highest quality,
- highest cost (one LLM call). Falls back to None (caller fails open)
- on any error: model unavailable, timeout, JSON parse failure, etc.
- """
- import urllib.request
- import urllib.error
- import urllib.parse
-
- # Target length in tokens
- target_tokens = max(50, int(estimate_tokens(text) * target_ratio))
-
- prompt = (
- f"Summarise the following context faithfully. Keep ALL entities, "
- f"identifiers, names, numbers, dates, and specific facts verbatim. "
- f"Drop filler, lorem ipsum, and re-fetchable tool output. "
- f"The user's question is: \"{query}\"\n"
- f"Target length: approximately {target_tokens} tokens.\n"
- f"Preserve the original wording for key facts. Do not add new information.\n\n"
- f"CONTEXT:\n{text[:60000]}" # Cap input at 60k chars to avoid overflow
- )
-
- endpoint = endpoint or os.environ.get("TAMERU_SUMMARY_ENDPOINT") or DEFAULT_SUMMARY_ENDPOINT
- if allow_remote is None:
- allow_remote = os.environ.get("TAMERU_SUMMARY_ALLOW_REMOTE", "").strip().lower() in {
- "1", "true", "yes", "on"
- }
- try:
- parsed_endpoint = urllib.parse.urlsplit(endpoint)
- except (TypeError, ValueError):
- return None
- if parsed_endpoint.scheme not in {"http", "https"} or not parsed_endpoint.hostname:
- return None
- local_hosts = {"127.0.0.1", "::1", "localhost"}
- if parsed_endpoint.hostname.casefold() not in local_hosts and not allow_remote:
- return None
- if model_candidates is None:
- configured = os.environ.get("TAMERU_SUMMARY_MODELS", "")
- models = tuple(m.strip() for m in configured.split(",") if m.strip())
- if not models:
- models = DEFAULT_SUMMARY_MODELS
- elif isinstance(model_candidates, str):
- models = tuple(m.strip() for m in model_candidates.split(",") if m.strip())
- else:
- models = tuple(str(m).strip() for m in model_candidates if str(m).strip())
- if not models:
- return None
- if timeout is None:
- try:
- timeout = float(os.environ.get("TAMERU_SUMMARY_TIMEOUT", DEFAULT_SUMMARY_TIMEOUT))
- except (TypeError, ValueError):
- timeout = DEFAULT_SUMMARY_TIMEOUT
- try:
- timeout = float(timeout)
- except (TypeError, ValueError, OverflowError):
- return None
- if not math.isfinite(timeout) or timeout <= 0:
- return None
- timeout = max(0.1, timeout)
- deadline = time.monotonic() + timeout
-
- for model in models:
- remaining = deadline - time.monotonic()
- if remaining <= 0:
- break
- try:
- req = urllib.request.Request(
- endpoint,
- data=json.dumps({
- "model": model,
- "messages": [
- {"role": "user", "content": prompt}
- ],
- "max_tokens": target_tokens,
- "temperature": 0.1,
- }).encode("utf-8"),
- headers={"Content-Type": "application/json"},
- method="POST",
- )
- with urllib.request.urlopen(req, timeout=remaining) as resp:
- raw = resp.read(MAX_SUMMARY_RESPONSE_BYTES + 1)
- if len(raw) > MAX_SUMMARY_RESPONSE_BYTES:
- continue
- data = json.loads(raw.decode("utf-8"))
- if not isinstance(data, dict):
- continue
- choices = data.get("choices")
- if not isinstance(choices, list) or not choices:
- continue
- choice = choices[0]
- if not isinstance(choice, dict):
- continue
- message = choice.get("message")
- if not isinstance(message, dict):
- continue
- content = message.get("content", "")
- if not isinstance(content, str):
- continue
- if not content or len(content.strip()) < 20:
- continue
- return content.strip()
- except (
- urllib.error.URLError,
- OSError,
- ValueError,
- json.JSONDecodeError,
- KeyError,
- IndexError,
- TimeoutError,
- TypeError,
- AttributeError,
- OverflowError,
- RecursionError,
- ):
- continue
- 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_.:/-]{0,127}\d[A-Za-z0-9_.:/-]{0,127}"
- r"|\d+(?:[.:/-]\d+)*)\b"
-)
-
-
-def _summary_preserves_required_facts(
- source: str, summary: str, query: str
-) -> tuple[bool, float]:
- """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 "")
- + _extract_terms(query or "")
- )
- for item in candidates:
- token = str(item).strip().casefold()
- 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
- # answer port 5432). Preserve structured values from source segments tied
- # to a query identifier, or to at least two lexical query terms.
- query_ids = {
- token.casefold()
- for token in _SUMMARY_STRUCTURED_TOKEN_RE.findall(query or "")
- }
- query_terms = {
- token.casefold()
- for token in (_extract_entities(query or "") + _extract_terms(query or ""))
- if token
- }
- for segment in re.split(r"[\r\n]+|(?<=[.!?])\s+", source):
- segment_fold = segment.casefold()
- id_match = bool(query_ids) and any(
- token in segment_fold for token in query_ids
- )
- term_hits = sum(1 for token in query_terms if token in segment_fold)
- if not id_match and term_hits < 2:
- continue
- for item in _SUMMARY_STRUCTURED_TOKEN_RE.findall(segment):
- token = item.casefold()
- 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
- # every adjective in a long source segment.
- for query_term in sorted(query_terms, key=len, reverse=True):
- match = re.search(
- rf"(?<!\w){re.escape(query_term)}(?!\w)\s+"
- r"(?:is|are|was|were|equals?|uses?|=)\s+(.+)$",
- segment,
- re.IGNORECASE,
- )
- if not match:
- continue
- 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_set:
- required.append(token)
- required_set.add(token)
- break
- 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
-
-
-def retrieve(
- ccr_hash: str,
- ccr_dir: str | Path = DEFAULT_CCR_DIR,
- *,
- offset: int = 0,
- limit: int | None = None,
-) -> Optional[str]:
- """Fetch a CCR record's original text. offset/limit paginate large
- records (pi-lcm lcm_expand-style partial recall)."""
- if not isinstance(ccr_hash, str) or not _CCR_HASH_RE.fullmatch(ccr_hash):
- return None
- root = Path(ccr_dir)
- if root.is_symlink() or not root.is_dir():
- return None
- fp = root / f"{ccr_hash}.json"
- try:
- info = fp.lstat()
- except OSError:
- return None
- if not stat.S_ISREG(info.st_mode):
- return None
- try:
- data = json.loads(fp.read_text(encoding="utf-8"))
- except (OSError, json.JSONDecodeError, RecursionError):
- return None
- if not isinstance(data, dict) or data.get("hash") != ccr_hash:
- return None
- metadata = _ccr_metadata(data)
- current = time.time()
- if (
- metadata is None
- or metadata[0] > current + CCR_MAX_CLOCK_SKEW_SECONDS
- or current - metadata[0] > metadata[1]
- ):
- try:
- fp.unlink()
- except OSError:
- pass
- return None
- original = data.get("original")
- if not isinstance(original, str):
- return None
- if hashlib.sha256(original.encode("utf-8")).hexdigest()[:24] != ccr_hash:
- return None
- if offset or limit is not None:
- start = max(0, int(offset))
- return original[start:] if limit is None else original[start:start + max(0, int(limit))]
- return original
-
-
-def list_ccr(
- ccr_dir: str | Path = DEFAULT_CCR_DIR,
- *,
- offset: int = 0,
- limit: int | None = None,
-) -> list[dict[str, Any]]:
- """List live CCR records newest-first (pi-lcm lcm_grep/describe analog).
-
- Returns metadata only — hash, stored_at, ttl, chars, and a short
- preview — so callers can discover records without loading full
- originals. Expired or malformed records are skipped.
- """
- root = Path(ccr_dir)
- if root.is_symlink() or not root.is_dir():
- return []
- out: list[dict[str, Any]] = []
- current = time.time()
- for fp in root.glob("*.json"):
- if not fp.name.startswith(".") and _CCR_HASH_RE.fullmatch(fp.stem):
- try:
- data = json.loads(fp.read_text(encoding="utf-8"))
- except (OSError, json.JSONDecodeError, RecursionError):
- continue
- if not isinstance(data, dict):
- continue
- metadata = _ccr_metadata(data)
- if (
- metadata is None
- or metadata[0] > current + CCR_MAX_CLOCK_SKEW_SECONDS
- or current - metadata[0] > metadata[1]
- ):
- continue
- record_hash = data.get("hash")
- original = data.get("original")
- if (
- not isinstance(record_hash, str)
- or record_hash != fp.stem
- or not isinstance(original, str)
- ):
- continue
- out.append({
- "hash": record_hash,
- "stored_at": metadata[0],
- "ttl": metadata[1],
- "chars": len(original),
- "preview": original[:80],
- })
- out.sort(key=lambda r: (-r["stored_at"], r["hash"]))
- start = max(0, int(offset))
- if limit is None:
- return out[start:]
- return out[start:start + max(0, int(limit))]
-
-
-# ---------------------------------------------------------------------------
-# JSON preprocessor (Baseline RAG-inspired)
-#
-# Detects JSON payloads in the context and crushes redundant array items.
-# Keeps only query-relevant items + a bounded sample of the rest, preserving
-# the top-level structure. This is the "JSON policy" from Baseline RAG's
-# type-specific policy system.
-# ---------------------------------------------------------------------------
-
-def _detect_json_payloads(text: str) -> list[tuple[int, int, str]]:
- """Find JSON object/array spans in the text.
-
- Returns a list of (start, end, json_string) tuples where start/end are
- character offsets into the original text.
- """
- payloads = []
- i = 0
- n = len(text)
- while i < n:
- ch = text[i]
- if ch in "{[":
- # Try to find the matching close
- depth = 0
- in_string = False
- escape = False
- j = i
- while j < n:
- c = text[j]
- if escape:
- escape = False
- elif c == "\\" and in_string:
- escape = True
- elif c == '"':
- in_string = not in_string
- elif not in_string:
- if c in "{[":
- depth += 1
- elif c in "}]":
- depth -= 1
- if depth == 0:
- break
- j += 1
- if j < n and depth == 0:
- candidate = text[i:j + 1]
- # Only count if it's a reasonable JSON payload (> 200 chars)
- if len(candidate) > 200:
- payloads.append((i, j + 1, candidate))
- i = j + 1
- else:
- # The first unmatched opener already scanned the remaining
- # suffix. Restarting at every later opener makes malformed
- # input quadratic. Stop and preserve the original text; the
- # caller's fail-open path is safer than speculative recovery.
- break
- else:
- i += 1
- return payloads
-
-
-def _crush_json_items(payload: str, query: str, max_keep: int = 50) -> str | None:
- """Crush redundant array items in a JSON payload.
-
- Finds the top-level array (or arrays under known keys like "items"),
- keeps only query-relevant items + up to max_keep others, and returns
- the re-serialized JSON. Returns None if no array was found or the
- result is not actually shorter.
- """
- try:
- data = json.loads(payload)
- except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
- return None
-
- # Find arrays to crush: top-level list, or dicts with list values
- # whose items are dicts (the "catalog" pattern).
- arrays_to_crush: list[tuple] = [] # (path, list_obj)
-
- def _find_arrays(obj, path=""):
- if isinstance(obj, list) and len(obj) > max_keep:
- # Check if items are dicts (the pattern we want to crush)
- if obj and isinstance(obj[0], dict):
- arrays_to_crush.append((path, obj))
- elif isinstance(obj, dict):
- for k, v in obj.items():
- _find_arrays(v, f"{path}.{k}" if path else k)
-
- _find_arrays(data)
- if not arrays_to_crush:
- return None
-
- if not query_has_distinctive_selectors(query):
- return None
- query_terms = set(_json_query_needles(query))
- if not query_terms:
- return None
- query_selectors = _selector_patterns(query_terms)
-
- total_crushed = 0
- for path, arr in arrays_to_crush:
- kept_indices = []
- dropped_count = 0
- for i, item in enumerate(arr):
- item_str = json.dumps(item, ensure_ascii=False)
- if any(selector.search(item_str) for selector in query_selectors):
- kept_indices.append(i)
- else:
- dropped_count += 1
- # No first-50 fallback. If nothing matched, do not crush this array.
- if not kept_indices:
- continue
-
- if dropped_count == 0:
- continue
-
- # Rebuild the array with only kept items
- new_arr = [arr[i] for i in sorted(kept_indices)]
- total_crushed += dropped_count
-
- # Walk back to the array location and replace it
- if path:
- parts = path.split(".")
- target = data
- for p in parts[:-1]:
- target = target[p]
- target[parts[-1]] = new_arr
- else:
- data = new_arr
-
- if total_crushed == 0:
- return None
-
- result = json.dumps(data, indent=2, ensure_ascii=False)
- # Only use the crushed version if it's actually shorter
- if len(result) < len(payload):
- return result
- return None
-
-
-def _preprocess_json(text: str, query: str) -> str:
- """Detect and crush JSON payloads in the text.
-
- Finds JSON spans, crushes redundant array items, and splices the
- crushed versions back into the text. Falls back to the original text
- if no JSON is found or crushing doesn't help.
- """
- payloads = _detect_json_payloads(text)
- if not payloads:
- return text
-
- modified = False
- result = text
- for start, end, payload in reversed(payloads): # reverse to preserve offsets
- crushed = _crush_json_items(payload, query)
- if crushed is not None:
- result = result[:start] + crushed + result[end:]
- modified = True
-
- return result if modified else text
-
-
-def inspect_compressibility(text: str, query: str) -> dict[str, Any]:
- """Pre-compression check (PAKT-inspired): is compression worth running?
-
- Returns {"worth_it": bool, "repetition_ratio": float, "reason": str}.
- Cheap heuristics only — no compression is performed.
- """
- tokens = estimate_tokens(text)
- if tokens < 200:
- return {
- "worth_it": False,
- "repetition_ratio": 0.0,
- "guaranteed_savings_pct": 0.0,
- "ceiling_class": "n/a",
- "reason": f"too small ({tokens} tokens < 200)",
- }
- lines = [ln.strip() for ln in text.split("\n") if ln.strip()]
- if not lines:
- return {
- "worth_it": False,
- "repetition_ratio": 0.0,
- "guaranteed_savings_pct": 0.0,
- "ceiling_class": "n/a",
- "reason": "empty",
- }
- # Repetition ratio: fraction of duplicate lines (exact + normalised runs).
- from collections import Counter
-
- norm = [re.sub(r"\d+", "#", ln) for ln in lines]
- counts = Counter(norm)
- dup_lines = sum(c for c in counts.values() if c > 1)
- repetition_ratio = dup_lines / len(lines)
- # Context-calibrated ceiling (Compactor): not every context compresses
- # the same. The guaranteed-safe bound is the surplus share of duplicated
- # normalised lines — bytes a dedupe pass removes with zero judgement.
- # Beyond it, savings depend on relevance selection, not redundancy.
- total_chars = sum(len(ln) + 1 for ln in lines)
- surplus_chars = sum((c - 1) * (len(ln) + 1) for ln, c in counts.items() if c > 1)
- guaranteed = surplus_chars / max(1, total_chars)
- ceiling_class = (
- "dedupe-heavy" if guaranteed >= 0.40
- else "moderate" if guaranteed >= 0.15
- else "sparse"
- )
- # Distinct-line ratio as a secondary signal: highly unique prose with no
- # query hits rarely compresses extractively without damage.
- worth = repetition_ratio >= 0.3 or tokens >= 5000
- return {
- "worth_it": worth,
- "repetition_ratio": round(repetition_ratio, 3),
- "guaranteed_savings_pct": round(guaranteed, 3),
- "ceiling_class": ceiling_class,
- "reason": (
- f"repetition {repetition_ratio:.0%}, {tokens} tokens"
- if worth
- else f"low repetition ({repetition_ratio:.0%}) and moderate size"
- ),
- }
-
-
-# ---------------------------------------------------------------------------
-# 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],
- *,
- mode: str = "adaptive",
- budget_ratio: Optional[float] = None,
- ccr: bool = True,
- cache_prefix: bool = False,
- citations: bool = True,
- ccr_dir: str | Path = DEFAULT_CCR_DIR,
- decision_cache: Optional[dict] = None,
- strategy: str = "extract",
- ambiguity_fail_open: bool = False,
- reorder_best: bool = False,
- semantic_tier: Any = None,
- log_dir: str | Path | None = None,
- pin_patterns: Optional[list[str]] = None,
- pin_recent: int = 0,
- min_savings_ratio: float = 0.10,
- degraded_view: bool = False,
- summary_endpoint: str | None = None,
- summary_models: Iterable[str] | str | None = None,
- summary_timeout: float | None = None,
- 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
- # terms; when no stable term structure exists the original empty query
- # stands and the usual fail-open applies. Receipts mark derived
- # objectives via query_source/derived_terms and the risk floor is
- # medium — an inferred intent never earns a "low" rating.
- derived_terms: list[str] = []
- if (
- derive_query
- and not query_has_distinctive_selectors(query or "")
- and not _topic_terms(query or "")
- ):
- derived_terms = _derive_query_terms(caller_text)
- if derived_terms:
- query = " ".join(derived_terms)
- requested_mode = (mode or "adaptive").strip().lower()
- if requested_mode not in {"adaptive", "fixed", "compiler", "precision"}:
- raise ValueError(
- f"unknown mode {mode!r} (expected adaptive/fixed/compiler/precision)"
- )
- mode_norm = (
- "adaptive" if requested_mode in {"compiler", "precision"} else requested_mode
- )
- pin_recent = max(0, int(pin_recent or 0))
- if min_savings_ratio is None:
- min_savings_ratio = 0.10
- min_savings_ratio = float(min_savings_ratio)
- if not 0.0 <= min_savings_ratio < 1.0:
- raise ValueError(
- f"min_savings_ratio must be in [0, 1), got {min_savings_ratio!r}"
- )
- # Recursion guard (Claude Code teardown inspired): Tameru output fed
- # 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) or (
- _has_anchored_marker(caller_text, recursion_markers)
- ):
- tokens = estimate_tokens(caller_text)
- return CompressResult(
- compressed_text=caller_text,
- original_tokens=tokens,
- kept_tokens=tokens,
- tokens_saved_pct=0.0,
- policy_name="local-noop-recursion",
- mode=requested_mode,
- keep_ratio=1.0,
- tokens_saved=0,
- kept_line_ratio=1.0,
- compression_risk="low",
- confidence=1.0,
- fail_open=True,
- reasons=["input already compressed (recursion guard)"],
- )
- secret_in_input = _contains_secret(caller_text)
- savings_gate = False
- degraded = False
- # 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
- and industrial.profile.limit_reason.startswith(
- ("character limit", "line limit")
- )
- and len(caller_text)
- <= industrial.limits.max_input_chars * _DEGRADED_OVERSIZE_FACTOR
- and industrial.profile.lines
- <= industrial.limits.max_lines * _DEGRADED_OVERSIZE_FACTOR
- and len(query or "") <= industrial.limits.max_query_chars
- ):
- # JEV-style degrade: keep size limits as a throughput guard, but let
- # the scorer see a bounded per-block view instead of refusing.
- # Safety gates preflight skipped (bidi controls, malformed
- # surrogates, oversize query) are re-verified here and stay hard.
- controls, overrides, malformed = unicode_safety_counts(caller_text)
- lim = industrial.limits
- if (
- not malformed
- and controls <= lim.max_bidi_controls
- and overrides <= lim.max_bidi_overrides
- ):
- industrial = replace(
- industrial,
- hard_fail_open=False,
- reason=f"degraded scoring view: {industrial.reason}",
- )
- degraded = True
- if industrial.hard_fail_open:
- tokens = estimate_tokens(caller_text)
- reason = industrial.reason or "industrial preflight failed open"
- fail_result = CompressResult(
- compressed_text=caller_text,
- original_tokens=tokens,
- kept_tokens=tokens,
- tokens_saved_pct=0.0,
- policy_name="local-fail-open",
- mode=requested_mode,
- keep_ratio=1.0,
- tokens_saved=0,
- kept_line_ratio=1.0,
- compression_risk="high",
- confidence=0.0,
- content_type=industrial.profile.format,
- fail_open=True,
- reasons=[reason],
- receipt={
- "schema_version": "1",
- "engine": "tameru",
- "policy": "local-fail-open",
- "query_hash": hashlib.sha256(
- (query_as_given or "").encode("utf-8")
- ).hexdigest()[:12],
- "savings_pct": 0.0,
- "risk": "high",
- "content_hint": content_hint,
- "industrial": industrial.to_dict(),
- },
- )
- assert fail_result.receipt is not None
- fail_result.receipt.update(
- _receipt_hashes(
- caller_text,
- caller_text,
- industrial,
- mode=requested_mode,
- strategy=strategy,
- budget_ratio=budget_ratio,
- citations=citations,
- )
- )
- return fail_result
- original_text = _norm_newlines(caller_text)
- industrial_text = industrial.text if industrial.applied else caller_text
- text = strip_ansi(_norm_newlines(industrial_text))
- # 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
- )
- ambiguity_fail_open = bool(ambiguity_fail_open)
-
- # 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 "") 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 "")
- if not no_table:
- text = preprocess_csv(text, query or "", gap_marker=adapter_gap)
- text = preprocess_filler_comments(text, query or "")
-
- # Strategy ladder:
- # 'clear' — drops tool-result payloads before scoring (regex-only,
- # zero LLM cost). Cheapest.
- # 'extract' — default. Query-aware block scoring.
- # 'summarise' — calls a configurable OpenAI-compatible local LLM to
- # produce a faithful summary. Falls back to 'extract'
- # on any LLM failure (model down, timeout, empty response).
- # Highest quality, highest cost.
- strategy_norm = (strategy or "extract").strip().lower()
- if strategy_norm not in {"clear", "extract", "summarise", "auto"}:
- raise ValueError(
- f"unknown strategy {strategy!r} (expected clear/extract/summarise/auto)"
- )
-
- if strategy_norm == "auto":
- # Progressive ladder (Claude Code tiers / JEV hook fallback):
- # run the cheap deterministic tier first, then escalate to the
- # LLM summariser only when extraction fails open (ambiguity,
- # saturated floors, min_savings undershoot). 'summarise' itself
- # falls back to 'extract' on any LLM failure, so the worst case
- # here is one extra deterministic pass.
- ladder_kwargs = dict(
- mode=requested_mode,
- budget_ratio=budget_ratio,
- ccr=ccr,
- cache_prefix=cache_prefix,
- citations=citations,
- ccr_dir=ccr_dir,
- decision_cache=decision_cache,
- ambiguity_fail_open=ambiguity_fail_open,
- reorder_best=reorder_best,
- semantic_tier=semantic_tier,
- log_dir=log_dir,
- pin_patterns=pin_patterns,
- pin_recent=pin_recent,
- min_savings_ratio=min_savings_ratio,
- 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_as_given, strategy="extract", **ladder_kwargs)
- if not base.fail_open:
- return base
- return compress_context(
- caller_text,
- query_as_given,
- strategy="summarise",
- summary_endpoint=summary_endpoint,
- summary_models=summary_models,
- summary_timeout=summary_timeout,
- summary_allow_remote=summary_allow_remote,
- **ladder_kwargs,
- )
-
- if strategy_norm == "clear":
- text = _clear_tool_payloads(text)
- elif strategy_norm == "summarise":
- summary = _summarise_with_llm(
- text,
- query or "",
- endpoint=summary_endpoint,
- model_candidates=summary_models,
- timeout=summary_timeout,
- allow_remote=summary_allow_remote,
- )
- summary_valid = False
- summary_recall = 0.0
- if summary is not None:
- summary_valid, summary_recall = _summary_preserves_required_facts(
- text, summary, query or ""
- )
- if (
- summary is not None
- and summary_valid
- and estimate_tokens(summary) < estimate_tokens(text)
- ):
- # LLM summary is shorter than the original — use it.
- # Store the original in CCR for reversibility.
- ccr_info = None
- ccr_marker = ""
- if ccr:
- try:
- ccr_info = _ccr_store(caller_text, ccr_dir)
- except OSError:
- ccr_info = None
- if ccr_info is not None:
- ccr_marker = f"\n[CC-Retrieve: {ccr_info['hash']}]"
- 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)
- summary_verifier = {
- "query_fact_recall": round(summary_recall, 3),
- "structured_ids_grounded": True,
- "risk": "medium",
- "score": round(summary_recall, 3),
- }
- summary_risk = "medium"
- return CompressResult(
- compressed_text=result_text,
- original_tokens=original_tokens,
- kept_tokens=kept_tokens,
- tokens_saved_pct=(1.0 - keep_ratio) * 100.0,
- policy_name="summarise-llm",
- mode=requested_mode,
- keep_ratio=keep_ratio,
- tokens_saved=original_tokens - kept_tokens,
- kept_line_ratio=1.0,
- cache_prefix_applied=False,
- compression_risk=(
- summary_risk if summary_risk in {"medium", "high"} else "medium"
- ),
- confidence=round(
- min(summary_recall, float(summary_verifier.get("score", 0.0))),
- 3,
- ),
- ccr=ccr_info,
- content_type="text",
- fail_open=False,
- frozen_blocks=0,
- reasons=(
- ["llm summary", "query facts verified"]
- + (["ccr skipped: secret material detected"] if ccr and secret_in_input else [])
- ),
- verifier=summary_verifier,
- receipt={
- "schema_version": "1",
- "engine": "tameru",
- "policy": "summarise-llm",
- "query_hash": hashlib.sha256(
- (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,
- result_text,
- industrial,
- mode=requested_mode,
- strategy=strategy,
- budget_ratio=budget_ratio,
- citations=citations,
- ),
- },
- )
- # LLM failed or returned a longer result — fall through to extract.
- strategy_norm = "extract"
-
- 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 = _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 = (
- f"block limit exceeded: {len(blocks)} > "
- f"{industrial.limits.max_blocks}"
- )
- receipt = {
- "schema_version": "1",
- "engine": "tameru",
- "policy": "local-fail-open",
- "query_hash": hashlib.sha256(
- (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(
- _receipt_hashes(
- caller_text,
- caller_text,
- industrial,
- mode=requested_mode,
- strategy=strategy,
- budget_ratio=budget_ratio,
- citations=citations,
- )
- )
- return CompressResult(
- compressed_text=caller_text,
- original_tokens=tokens,
- kept_tokens=tokens,
- tokens_saved_pct=0.0,
- policy_name="local-fail-open",
- mode=requested_mode,
- keep_ratio=1.0,
- tokens_saved=0,
- kept_line_ratio=1.0,
- compression_risk="high",
- confidence=0.0,
- content_type=industrial.profile.format,
- fail_open=True,
- reasons=[reason],
- receipt=receipt,
- )
- if degraded:
- # Score a bounded head+tail view per block. _render emits exact
- # spans from `lines`, and citation digests use `full_text`, so the
- # judge sees the shrink while output stays byte-exact.
- for b in blocks:
- full = b["text"]
- if len(full) > _DEGRADED_VIEW_HEAD + _DEGRADED_VIEW_TAIL + 64:
- b["full_text"] = full
- omitted = len(full) - _DEGRADED_VIEW_HEAD - _DEGRADED_VIEW_TAIL
- b["text"] = (
- f"{full[:_DEGRADED_VIEW_HEAD]}\n"
- f"[… {omitted} chars omitted …]\n"
- f"{full[-_DEGRADED_VIEW_TAIL:]}"
- )
- 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;
- # the floor/important/needle paths all respect score, and trust-risk is
- # bypassed for pins (a caller-pinned line outranks injection heuristics).
- if pin_patterns:
- import re as _re
- _pin_res = [_re.compile(pt) for pt in pin_patterns]
- for b in scored:
- if any(rx.search(b["text"]) for rx in _pin_res):
- b["score"] = max(b["score"], 999.0)
- b["pinned"] = True
-
- # JEV-style positional pinning: the opening block and the newest N
- # blocks are unconditional keeps — the caller's working-context floor.
- # Same trust semantics as pin_patterns: pins outrank drop and risk paths.
- pin_recent_ids: set[int] = set()
- if pin_recent and scored:
- pin_recent_ids = {scored[0]["id"]}
- pin_recent_ids.update(b["id"] for b in scored[-pin_recent:])
- for b in scored:
- if b["id"] in pin_recent_ids:
- b["score"] = max(b["score"], 999.0)
- b["pinned"] = True
-
- # Freeze-on-first-sight: if a decision_cache is provided and the block
- # fingerprints match prior turns, replay the stored keep/drop decisions
- # byte-identically. This keeps the provider prompt cache warm across a
- # multi-turn session (the prefix doesn't churn when new turns arrive).
- if decision_cache is not None and not isinstance(decision_cache, dict):
- decision_cache = None
- if decision_cache is not None:
- scored = _apply_freeze(decision_cache, scored, text, query or "")
- _reorder = bool(reorder_best)
- selection: dict[str, Any] = {}
- if mode_norm == "fixed":
- ratio = 0.35 if budget_ratio is None else float(budget_ratio)
- 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,
- needle_only=query_has_distinctive_selectors(query or ""),
- query=query or "",
- semantic_tier=resolve_tier(semantic_tier) if semantic_tier is not None else None,
- out=selection,
- )
- ratio = budget_ratio if budget_ratio is not None else 0.0
- if not fail_open:
- if decision_cache is not None:
- kept = _enforce_frozen_decisions(scored, kept)
- # 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.
- # 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"
-
- q = query or ""
- if not q.strip() or (
- not query_has_distinctive_selectors(q) and not _topic_terms(q)
- ):
- fail_open = True
- risk = "high"
- else:
- latin_terms = [
- t
- for t in distinctive_query_terms(q)
- if not t.startswith("script:")
- ]
- if latin_terms:
- blob = original_text.casefold()
- if not any(t in blob for t in latin_terms):
- fail_open = True
- risk = "high"
- 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")
- }
- if annotated_ids:
- # Restore ordinary data when uncertain, but never undo a block
- # 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,
- gap_marker=gap_marker, total_chars=len(caller_text),
- content_lines=score_lines,
- )
- fail_open = False
- risk = "high"
- # Do not undo a useful structured collapse (repeated INFO lines).
- # Baseline RAG wins on JP/KO heartbeats by keeping the error and
- # dropping repeats; restoring original bytes here threw that away.
- elif (
- not ambiguity_fail_open
- and collapsed != original_text
- and len(collapsed) < 0.7 * max(1, len(original_text))
- ):
- 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,
- 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:
- # One expansion pass: add any dropped block that still holds a query key.
- keys = _extract_entities(query or "") + _extract_terms(query or "")
- for b in scored:
- if b["id"] in kept or b.get("trust_risk"):
- continue
- if any(_soft_has(b["text"], k) for k in keys):
- 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,
- gap_marker=gap_marker, total_chars=len(caller_text),
- content_lines=score_lines,
- )
- recall = _entity_recall(text, compressed, query or "")
- trust_risks = {
- b["id"]
- for b in scored
- if b.get("trust_risk") and not b.get("pinned")
- }
- if trust_risks:
- # Filter the CURRENT selection down by trust risks — do not
- # rebuild it as "everything except risks". Rebuilding discards
- # the selector's careful small keep-set and re-inflates the
- # payload to near-original size (v0.5.19 production QA finding).
- 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,
- gap_marker=gap_marker, total_chars=len(caller_text),
- content_lines=score_lines,
- )
- risk = "high"
- elif recall < 0.5:
- compressed = original_text
- fail_open = True
- risk = "high"
- kept = {b["id"] for b in scored}
-
- if fail_open:
- 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(
- b.get("trust_risk") and not b.get("pinned") and b["id"] not in kept
- for b in scored
- )
-
- # Cost gate: if the compressed output is LARGER than the original
- # (net-negative savings), fail open. This happens when citations and
- # the CCR marker add more tokens than the dropped blocks saved.
- #
- # Provider cache pricing makes this more nuanced: Anthropic charges
- # 1.25× for cache writes and 0.1× for cache reads. If the context is
- # already cached (repeated session), the original is cheap to re-send
- # and the compression overhead may not be worth it. We model this
- # with a simple heuristic: if the savings are < 10%, the overhead of
- # citations + CCR marker likely exceeds the cache-read savings.
- if not fail_open and kept_tokens > original_tokens and not trust_filtered:
- compressed = original_text
- kept = {b["id"] for b in scored}
- fail_open = True
- risk = "high"
- kept_tokens = estimate_tokens(compressed)
- elif not fail_open and original_tokens > 200:
- # For substantial contexts, check if the savings justify the
- # compression overhead. If we're saving less than 10%, the
- # citation/CCR overhead likely exceeds the benefit, especially
- # when the provider cache makes the original cheap to re-send.
- savings_ratio = 1.0 - (kept_tokens / max(1, original_tokens))
- if savings_ratio < min_savings_ratio and not trust_filtered:
- compressed = original_text
- kept = {b["id"] for b in scored}
- fail_open = True
- risk = "high"
- savings_gate = True
- kept_tokens = estimate_tokens(compressed)
-
- if fail_open and compressed != caller_text:
- compressed = caller_text
- kept_tokens = estimate_tokens(compressed)
-
- freeze_cache_saturated = False
- if decision_cache is not None and not fail_open:
- freeze_cache_saturated = _record_freeze_decisions(decision_cache, scored, kept)
-
- keep_ratio = kept_tokens / max(1, original_tokens)
- line_keep = 0
- if lines:
- kept_line_idx = set()
- for b in scored:
- if b["id"] in kept:
- for i in range(b["start"], b["end"] + 1):
- kept_line_idx.add(i)
- line_keep = len(kept_line_idx) / max(1, len(lines))
-
- 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":
- risk = "medium"
- if ccr and secret_in_input:
- # dsh-jev-pre-compaction lesson: scan for secrets BEFORE archiving.
- reasons.append("ccr skipped: secret material detected")
- result_text = compressed
- cache_applied = False
- if cache_prefix and not fail_open:
- result_text = cache_wrap(compressed, query_as_given or "")
- cache_applied = True
-
- ccr_info = None
- pending_ccr_hash = None
- candidate_text = result_text
- if ccr and not fail_open and compressed != original_text and not secret_in_input:
- pending_ccr_hash = hashlib.sha256(caller_text.encode("utf-8")).hexdigest()[:24]
- if "[CC-Retrieve:" not in candidate_text:
- candidate_text = (
- candidate_text.rstrip()
- + f"\n[CC-Retrieve: {pending_ccr_hash}]\n"
- )
-
- candidate_tokens = estimate_tokens(candidate_text)
- if not fail_open and candidate_tokens > original_tokens and not trust_filtered:
- result_text = caller_text
- compressed = caller_text
- kept = {b["id"] for b in scored}
- fail_open = True
- risk = "high"
- cache_applied = False
- line_keep = 1.0
- else:
- result_text = candidate_text
- if pending_ccr_hash is not None:
- try:
- # Recovery means the caller's exact bytes, not a preprocessed view.
- ccr_info = _ccr_store(caller_text, ccr_dir)
- except OSError:
- ccr_info = None
- if ccr_info is None:
- # Store refused (secret screen) or failed — never emit a
- # dangling retrieval pointer.
- result_text = result_text.replace(
- f"\n[CC-Retrieve: {pending_ccr_hash}]\n", ""
- )
-
- kept_tokens = estimate_tokens(result_text)
- keep_ratio = kept_tokens / max(1, original_tokens)
- savings = round((1 - keep_ratio) * 100, 2)
- policy = f"local-{content_type}"
- if fail_open:
- policy = "local-fail-open"
-
- # Post-compression verifier: self-check entity/keyword/critical-line
- # recall. This is diagnostic — it does NOT modify the compressed output.
- # Callers can inspect the verifier to decide whether to re-compress with
- # a different strategy or expand dropped blocks.
- verifier = None
- if not fail_open and compressed and original_tokens > 50:
- 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):
- risk = verifier_risk
- recall = min(recall, float(verifier.get("score", 0.0)))
- # An inferred objective is advisory context, not caller intent — a
- # derived query never earns a "low" compression-risk rating.
- if derived_terms and risk == "low":
- risk = "medium"
- __cr = CompressResult(
- compressed_text=result_text,
- original_tokens=original_tokens,
- kept_tokens=kept_tokens,
- tokens_saved_pct=savings,
- policy_name=policy,
- mode=requested_mode,
- keep_ratio=keep_ratio,
- tokens_saved=original_tokens - kept_tokens,
- kept_line_ratio=round(line_keep, 4),
- cache_prefix_applied=cache_applied,
- compression_risk=risk,
- confidence=round(recall, 3),
- ccr=ccr_info,
- content_type=content_type,
- fail_open=fail_open,
- frozen_blocks=sum(1 for b in scored if b.get("frozen")),
- reasons=reasons,
- verifier=verifier,
- )
-
- # v0.10.0 (G5 memorix receipt): machine-readable provenance.
- query_hash = hashlib.sha256((query_as_given or "").encode("utf-8")).hexdigest()[:12]
- receipt = {
- "schema_version": "1",
- "engine": "tameru",
- "policy": policy,
- "kept_ids": _bounded_id_manifest(
- (i for i in kept if i < len(scored)),
- industrial.limits.max_receipt_ids,
- ),
- "dropped_ids": _bounded_id_manifest(
- (b["id"] for b in scored if b["id"] not in kept),
- industrial.limits.max_receipt_ids,
- ),
- "query_hash": query_hash,
- "savings_pct": savings,
- "risk": risk,
- "verifier": verifier,
- "degraded_view": degraded,
- # dsh-jev-prune lesson: never degrade silently — report which
- # selector path decided the keep-set.
- "selection": selection.get("path"),
- # lcc sufficiency lesson: blocks restored because a kept block's
- # rare terms were qualified or defined there.
- "sufficiency_restored": selection.get("sufficiency_restored") or [],
- # TPC lesson: callers must see when the objective was inferred,
- # 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(
- _receipt_hashes(
- caller_text,
- result_text,
- industrial,
- mode=requested_mode,
- strategy=strategy,
- budget_ratio=budget_ratio,
- citations=citations,
- )
- )
- __cr.receipt = receipt
-
- # v0.10.0 (G1 memorix checkpoints): append-only audit log, opt-in.
- if log_dir:
- try:
- log_path = Path(log_dir)
- log_path.mkdir(parents=True, exist_ok=True, mode=0o700)
- try:
- log_path.chmod(0o700)
- except OSError:
- pass
- top_dropped = sorted(
- (
- {"id": b["id"], "score": round(float(b.get("score", 0.0)), 2)}
- for b in scored
- if b["id"] not in kept
- ),
- key=lambda d: -d["score"],
- )[:5]
- entry = {
- "ts": time.strftime("%Y-%m-%dT%H:%M:%S%z"),
- "query_hash": query_hash,
- "policy": policy,
- "kept": len(kept),
- "total": len(scored),
- "savings_pct": savings,
- "risk": risk,
- "fail_open": fail_open,
- "top_dropped": top_dropped,
- "industrial_format": industrial.profile.format,
- "industrial_applied": industrial.applied,
- "direction": industrial.profile.direction,
- "scripts": industrial.profile.scripts,
- "input_chars": industrial.profile.characters,
- "input_lines": industrial.profile.lines,
- "profile_truncated": industrial.profile.profile_truncated,
- }
- log_file = log_path / "compactions.jsonl"
- fd = os.open(log_file, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
- if hasattr(os, "fchmod"):
- os.fchmod(fd, 0o600)
- with os.fdopen(fd, "a", encoding="utf-8") as fh:
- fh.write(json.dumps(entry, ensure_ascii=False) + "\n")
- except OSError:
- pass # logging must never break compression
-
- return __cr
-
-
-def main(argv: Optional[Iterable[str]] = None) -> int:
- import argparse
- limit_defaults = IndustrialLimits()
- p = argparse.ArgumentParser(description="Local query-aware extractive compressor")
- p.add_argument("context_file", help="Path to context text (or - for stdin)")
- p.add_argument("query")
- p.add_argument(
- "--mode",
- default="adaptive",
- choices=["adaptive", "fixed", "compiler", "precision"],
- )
- p.add_argument("--budget-ratio", type=float, default=None)
- p.add_argument("--max-input-chars", type=int, default=limit_defaults.max_input_chars)
- p.add_argument("--max-lines", type=int, default=limit_defaults.max_lines)
- p.add_argument("--max-records", type=int, default=limit_defaults.max_records)
- p.add_argument("--max-record-chars", type=int, default=limit_defaults.max_record_chars)
- p.add_argument("--max-fields", type=int, default=limit_defaults.max_fields)
- p.add_argument("--max-profile-chars", type=int, default=limit_defaults.max_profile_chars)
- p.add_argument("--max-bidi-controls", type=int, default=limit_defaults.max_bidi_controls)
- p.add_argument("--max-bidi-overrides", type=int, default=limit_defaults.max_bidi_overrides)
- p.add_argument("--max-query-chars", type=int, default=limit_defaults.max_query_chars)
- p.add_argument("--max-blocks", type=int, default=limit_defaults.max_blocks)
- p.add_argument("--max-receipt-ids", type=int, default=limit_defaults.max_receipt_ids)
- p.add_argument("--ccr", action="store_true", default=None,
- help="Enable CCR reversible store (on by default)")
- p.add_argument("--no-ccr", dest="ccr", action="store_false",
- help="Disable CCR (reversibility off)")
- p.add_argument("--cache-prefix", action="store_true")
- p.add_argument("--citations", action="store_true", default=None,
- help="Emit ARC-style citations for dropped blocks (on by default)")
- p.add_argument("--no-citations", dest="citations", action="store_false",
- help="Disable citations")
- p.add_argument("--decision-cache", default=None,
- help="JSON file path for freeze-on-first-sight decision cache")
- p.add_argument("--strategy", default="extract", choices=["clear", "extract", "summarise", "auto"],
- help="Compression strategy: clear (drop tool payloads), extract (default, query-aware), summarise (falls back to extract), auto (extract first, escalate to summarise only if extraction fails open)")
- p.add_argument("--pin-recent", type=int, default=0,
- help="Unconditionally keep the first block and the newest N blocks")
- p.add_argument("--min-savings-ratio", type=float, default=0.10,
- help="Fail open when achieved savings fall below this ratio (0 disables)")
- p.add_argument("--degraded-view", action="store_true",
- help="On input size-limit breach, score a bounded head+tail view per block instead of failing open")
- p.add_argument("--derive-query", action="store_true",
- help="On empty/generic query, infer a conservative objective from the document's recurring rare terms instead of failing open")
- p.add_argument("--stats", action="store_true")
- args = p.parse_args(list(argv) if argv is not None else None)
- if args.context_file == "-":
- import sys as _sys
- ctx = _sys.stdin.read()
- else:
- ctx = Path(args.context_file).read_text(encoding="utf-8")
- # Resolve ccr/citations defaults (None = use library default True)
- ccr_val = True if args.ccr is None else args.ccr
- citations_val = True if args.citations is None else args.citations
- limits = IndustrialLimits(
- max_input_chars=args.max_input_chars,
- max_lines=args.max_lines,
- max_records=args.max_records,
- max_record_chars=args.max_record_chars,
- max_fields=args.max_fields,
- max_profile_chars=args.max_profile_chars,
- max_bidi_controls=args.max_bidi_controls,
- max_bidi_overrides=args.max_bidi_overrides,
- max_query_chars=args.max_query_chars,
- max_blocks=args.max_blocks,
- max_receipt_ids=args.max_receipt_ids,
- )
- # Load decision cache if provided
- decision_cache = None
- if args.decision_cache:
- dc_path = Path(args.decision_cache)
- if dc_path.is_file():
- try:
- decision_cache = json.loads(dc_path.read_text(encoding="utf-8"))
- except (OSError, json.JSONDecodeError, RecursionError):
- decision_cache = {}
- if not isinstance(decision_cache, dict):
- decision_cache = {}
- else:
- decision_cache = {}
- out = compress_context(
- ctx,
- args.query,
- mode=args.mode,
- budget_ratio=args.budget_ratio,
- ccr=ccr_val,
- cache_prefix=args.cache_prefix,
- citations=citations_val,
- decision_cache=decision_cache,
- strategy=args.strategy,
- pin_recent=args.pin_recent,
- min_savings_ratio=args.min_savings_ratio,
- degraded_view=args.degraded_view,
- derive_query=args.derive_query,
- limits=limits,
- )
- if args.stats:
- stats = out.to_dict()
- stats.pop("compressed_text", None)
- print(json.dumps(stats, indent=2))
- print("---")
- print(out.compressed_text)
- # Persist decision cache if provided
- if args.decision_cache and decision_cache is not None:
- dc_path = Path(args.decision_cache)
- _write_private_atomic(
- dc_path,
- json.dumps(decision_cache, ensure_ascii=False),
- )
- return 0
-if __name__ == "__main__":
- raise SystemExit(main())
\ No newline at end of file
diff --git a/plugins/context_engine/tameru/contract_gates.py b/plugins/context_engine/tameru/contract_gates.py
deleted file mode 100644
index 32c8f82e54e..00000000000
--- a/plugins/context_engine/tameru/contract_gates.py
+++ /dev/null
@@ -1,188 +0,0 @@
-"""Query-specificity gates for destructive preprocess.
-
-Stdlib only. No I/O. Do not import compress_context.
-"""
-
-from __future__ import annotations
-
-import re
-from dataclasses import dataclass, field
-
-from .unicode_profile import matching_shadow, script_of, search_units
-
-GENERIC_WORDS = frozenset(
- {
- "what",
- "how",
- "does",
- "do",
- "the",
- "is",
- "are",
- "was",
- "were",
- "why",
- "when",
- "where",
- "which",
- "who",
- "whom",
- "whose",
- "this",
- "that",
- "with",
- "into",
- "about",
- "for",
- "and",
- "or",
- "of",
- "to",
- "a",
- "an",
- "in",
- "on",
- "it",
- "be",
- "as",
- "by",
- "from",
- "at",
- "if",
- "not",
- "can",
- "you",
- "we",
- "they",
- "them",
- "their",
- "our",
- "your",
- "please",
- "tell",
- "give",
- "show",
- "explain",
- "summarize",
- "summary",
- "function",
- "return",
- "class",
- "def",
- "import",
- "hmm",
- "cert",
- "code",
- "codes",
- "part",
- "parts",
- "item",
- "items",
- "list",
- "every",
- "catalog",
- "name",
- "names",
- "value",
- "values",
- "data",
- "info",
- "keep",
- "track",
- "detail",
- "details",
- "weather",
- "paris",
- "capital",
- "special",
- }
-)
-
-LATIN_IDENT_RE = re.compile(
- r"[A-Za-z]*\d[A-Za-z0-9_-]*"
- r"|[A-Za-z0-9]+(?:[_./-][A-Za-z0-9]+)+"
- r"|[A-Z]{2,24}"
- r"|[A-Z][a-z]+[A-Z][A-Za-z0-9]*"
-)
-
-NON_LATIN_RE = re.compile(
- r"[\u00C0-\u024F\u0400-\u04FF\u0600-\u06FF\u0900-\u097F"
- r"\u0E00-\u0E7F\u3400-\u9FFF\uAC00-\uD7A3]"
-)
-
-_HYPHEN_RE = re.compile(r"\b\w+(?:-\w+)+\b")
-
-
-def distinctive_query_terms(query: str) -> list[str]:
- """Return first-seen distinctive selectors; empty if the query is generic."""
- if not query or not str(query).strip():
- return []
- raw_text = str(query)
- text = matching_shadow(raw_text)
- seen: set[str] = set()
- out: list[str] = []
-
- def _add(raw: str) -> None:
- key = raw.casefold()
- if key in GENERIC_WORDS or len(key) < 2 or key in seen:
- return
- seen.add(key)
- out.append(key)
-
- for m in LATIN_IDENT_RE.findall(raw_text):
- _add(m)
- for m in _HYPHEN_RE.findall(text):
- if len(m) >= 3:
- _add(m)
- for unit in search_units(text)[:128]:
- representative = next((char for char in unit if char.isalnum()), unit[0])
- script = script_of(representative)
- if (
- script not in {"han", "kana", "hangul", "arabic", "thai", "latin"}
- and any(ord(char) > 127 for char in unit)
- and len(unit) >= 2
- ):
- _add(unit)
- nl = NON_LATIN_RE.search(text)
- if nl:
- sentinel = f"script:{nl.group(0)}"
- if sentinel not in seen:
- seen.add(sentinel)
- out.append(sentinel)
- return out
-
-
-def query_has_distinctive_selectors(query: str) -> bool:
- """True when the query names something specific enough to crush on."""
- return bool(distinctive_query_terms(query))
-
-@dataclass
-class SemanticContractVerdict:
- passed: bool
- answer_recall: float = 1.0
- missing_evidence: list[str] = field(default_factory=list)
- forbidden_hits: tuple[str, ...] = field(default_factory=tuple)
-
-
-def evaluate_semantic_contract(text: str, spec: dict) -> SemanticContractVerdict:
- answers = spec.get("answers", [])
- required_evidence = spec.get("required_evidence", [])
- forbidden_distractors = spec.get("forbidden_distractors", [])
-
- missing_evidence = [ev for ev in required_evidence if ev not in text]
- forbidden_hits = tuple(d for d in forbidden_distractors if d in text)
-
- answer_hits = sum(1 for a in answers if a in text)
- answer_recall = (answer_hits / len(answers)) if answers else 1.0
-
- passed = (
- len(missing_evidence) == 0
- and len(forbidden_hits) == 0
- and answer_recall >= 1.0
- )
- return SemanticContractVerdict(
- passed=passed,
- answer_recall=answer_recall,
- missing_evidence=missing_evidence,
- forbidden_hits=forbidden_hits,
- )
diff --git a/plugins/context_engine/tameru/folding.py b/plugins/context_engine/tameru/folding.py
deleted file mode 100644
index 7563c17ee56..00000000000
--- a/plugins/context_engine/tameru/folding.py
+++ /dev/null
@@ -1,483 +0,0 @@
-"""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", "json_brief", "mask_template"]
-
-# --------------------------------------------------------------------------
-# 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 _json_brief_body(
- count: int, keys: str, head: str, tail: str, notable: str, omitted: int
-) -> str:
- """The JSON brief object; ``keys`` is already-dumped JSON, the row lists are joined dumps."""
- return (
- f'{{"_count":{count:d},"_keys":{keys},"_head":[{head}],"_tail":[{tail}],'
- f'"_notable":[{notable}],"_omitted":{omitted:d}}}'
- )
-
-
-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 f"\\u{0xD800 + (code >> 10):04x}\\u{0xDC00 + (code & 0x3FF):04x}"
- return f"\\u{code:04x}"
-
-
-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(_json_brief_body(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 = _json_brief_body(
- 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
deleted file mode 100644
index 397a7d387ce..00000000000
--- a/plugins/context_engine/tameru/format_adapters.py
+++ /dev/null
@@ -1,810 +0,0 @@
-"""Bounded, extractive format adapters for industrial compaction."""
-from __future__ import annotations
-
-import csv
-import io
-import json
-import re
-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
-
-
-@dataclass(frozen=True)
-class FormatLimits:
- max_records: int = 100_000
- max_record_chars: int = 1_000_000
- max_fields: int = 4_096
-
-
-@dataclass(frozen=True)
-class FormatResult:
- format: str
- applied: bool
- text: str
- total_records: int = 0
- kept_records: int = 0
- structurally_valid: bool = True
- reason: str = ""
-
- def to_dict(self) -> dict[str, object]:
- return asdict(self)
-
-
-def _decline(
- text: str,
- format_name: str,
- reason: str,
- *,
- total_records: int = 0,
- structurally_valid: bool = True,
-) -> FormatResult:
- return FormatResult(
- format=format_name,
- applied=False,
- text=text,
- total_records=total_records,
- structurally_valid=structurally_valid,
- reason=reason,
- )
-
-
-# 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 ""))
- seen: set[str] = set()
- selectors: list[str] = []
- for raw in candidates:
- value = matching_shadow(raw).strip()
- if (
- not value
- or value.startswith("script:")
- or _CLI_FLAG_RE.match(value)
- or value in GENERIC_WORDS
- or value in seen
- ):
- continue
- if len(value) < 2 and value.isascii():
- continue
- seen.add(value)
- 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
- if any(char.isdigit() for char in value)
- or any(separator in value for separator in "._:/-")
- or any(ord(char) > 127 for char in value)
- ]
- return tuple(strong or selectors)
-
-
-def _contains_selector(text: str, selectors: tuple[str, ...]) -> bool:
- shadow = matching_shadow(text)
- for selector in selectors:
- escaped = re.escape(selector)
- if selector[:1].isascii() and selector[-1:].isascii():
- pattern = re.compile(rf"(?<![\w]){escaped}(?![\w])", re.IGNORECASE)
- if pattern.search(shadow):
- return True
- elif selector in shadow:
- return True
- return False
-
-
-def _json_loads(value: str):
- try:
- return json.loads(value)
- 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:
- return False
- candidates = sum(line.lstrip().startswith(("{", "[")) for line in lines[:32])
- parsed = sum(_json_loads(line) is not None for line in lines[:32])
- sample = min(len(lines), 32)
- return candidates >= max(2, sample // 2) and parsed >= 1
-
-
-def _line_separator(text: str) -> str:
- return "\r\n" if "\r\n" in text else "\n"
-
-
-def _split_delimited_records(text: str) -> list[str] | None:
- records: list[str] = []
- start = 0
- index = 0
- in_quotes = False
- while index < len(text):
- char = text[index]
- if char == '"':
- if in_quotes and index + 1 < len(text) and text[index + 1] == '"':
- index += 2
- continue
- in_quotes = not in_quotes
- index += 1
- continue
- if not in_quotes and char in {"\n", "\r"}:
- records.append(text[start:index])
- if char == "\r" and index + 1 < len(text) and text[index + 1] == "\n":
- index += 1
- start = index + 1
- index += 1
- if in_quotes:
- return None
- if start < len(text):
- records.append(text[start:])
- elif text.endswith(("\n", "\r")):
- records.append("")
- while records and not records[-1]:
- records.pop()
- return records
-
-
-def _parse_delimited_record(record: str, delimiter: str, limits: FormatLimits) -> list[str] | None:
- if len(record) > limits.max_record_chars:
- return None
- try:
- rows = list(csv.reader(io.StringIO(record), delimiter=delimiter, strict=True))
- except (csv.Error, UnicodeError):
- return None
- if len(rows) != 1 or len(rows[0]) > limits.max_fields:
- return None
- return rows[0]
-
-
-def _detect_delimiter(text: str) -> str | None:
- records = _split_delimited_records(text)
- if not records or len(records) < 2:
- return None
- for delimiter in ("\t", ","):
- parsed = [_parse_delimited_record(record, delimiter, FormatLimits()) for record in records[:8]]
- if any(row is None for row in parsed):
- continue
- widths = [len(row or []) for row in parsed]
- if widths and min(widths) >= 2 and len(set(widths)) == 1:
- return delimiter
- return None
-
-
-def _looks_vertical(text: str) -> bool:
- if _VERTICAL_MARKUP_RE.search(text):
- return True
- lines = [line for line in text.splitlines() if line.strip()][:128]
- if len(lines) < 6:
- return False
- short = sum(
- (len(line.strip()) if line.isascii() else len(graphemes(line.strip()))) <= 2
- for line in lines
- )
- return short / len(lines) >= 0.8
-
-
-def detect_format(text: str) -> str:
- value = str(text or "")
- stripped = value.lstrip()
- if re.search(r"<!doctype\s+html|<html\b", stripped[:512], re.IGNORECASE):
- return "html"
- if stripped.startswith("<") and stripped.rstrip().endswith(">"):
- try:
- ET.fromstring(stripped)
- except (ET.ParseError, RecursionError):
- pass
- else:
- 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 == ",":
- return "csv"
- if len(re.findall(r"(?im)^\s*(?:CREATE|INSERT|UPDATE|DELETE|SELECT|ALTER|DROP|WITH)\b", value)) >= 2:
- return "sql"
- if (
- len(re.findall(r"(?m)^\s*\[[^\]\n]+\]\s*$", value)) >= 2
- and re.search(r"(?m)^\s*[A-Za-z_][\w.-]*\s*=", value)
- ):
- return "ini"
- if re.search(r"(?m)^#{1,6}\s+|^```|^~~~", value):
- return "markdown"
- yaml_parent = re.search(
- r"(?m)^[^\s:#][^:\n]{0,120}:\s*$",
- value,
- )
- yaml_child = re.search(
- r"(?m)^[ \t]+(?:[-?][ \t]+)?[^\s:#][^:\n]{0,120}:\s*(?:\S.*)?$",
- value,
- )
- if yaml_parent and yaml_child and not code_vetoes_yaml(value):
- return "yaml"
- if _looks_vertical(value):
- return "vertical"
- return "text"
-
-
-_VERTICAL_MARKUP_RE = re.compile(r"writing-mode\s*:", re.IGNORECASE)
-
-
-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(
- text,
- "ndjson",
- "malformed JSON record",
- total_records=len(lines),
- structurally_valid=False,
- )
- 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(out_lines),
- total_records=len(lines),
- kept_records=len(kept),
- reason="exact matching NDJSON records",
- )
-
-
-def _adapt_delimited(
- text: str,
- selectors: tuple[str, ...],
- 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:
- return _decline(text, format_name, "unmatched quote", structurally_valid=False)
- if len(records) - 1 > limits.max_records:
- return _decline(text, format_name, "record limit exceeded", total_records=max(0, len(records) - 1))
- parsed = [_parse_delimited_record(record, delimiter, limits) for record in records]
- if not parsed or any(row is None for row in parsed):
- return _decline(text, format_name, "invalid delimited record", structurally_valid=False)
- 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)
- 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)
- 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,
- text=_line_separator(text).join(result_records),
- total_records=total,
- kept_records=len(kept),
- reason="header plus exact matching records",
- )
-
-
-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]]] = []
- ancestors: dict[int, str] = {}
- current_ancestors: list[str] = []
- current: list[str] = []
- in_fence = False
- fence_marker = ""
-
- def flush() -> None:
- nonlocal current
- if current:
- sections.append((list(current_ancestors), current))
- current = []
-
- for line in lines:
- stripped = line.lstrip()
- marker = stripped[:3]
- if marker in {"```", "~~~"}:
- if not in_fence:
- in_fence = True
- fence_marker = marker
- elif marker == fence_marker:
- in_fence = False
- current.append(line)
- continue
- heading = None if in_fence else re.match(r"^(#{1,6})\s+", line)
- if heading:
- flush()
- level = len(heading.group(1))
- ancestors = {depth: value for depth, value in ancestors.items() if depth < level}
- current_ancestors = [ancestors[depth] for depth in sorted(ancestors)]
- ancestors[level] = line
- current = [line]
- else:
- current.append(line)
- flush()
- 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()
- 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)
- seen_headings.add(heading)
- if output and output[-1].strip() and body and body[0].strip():
- output.append("")
- 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,
- text="\n".join(output).strip("\n"),
- total_records=len(sections),
- kept_records=len(selected),
- reason="matching sections with ancestor headings",
- )
-
-
-def _indent(line: str) -> int:
- return len(line) - len(line.lstrip(" "))
-
-
-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()
- nonblank = [index for index, line in enumerate(lines) if line.strip()]
- matches = [index for index in nonblank if _contains_selector(lines[index], selectors)]
- for match in matches:
- match_indent = _indent(lines[match])
- anchor = match
- list_anchor_found = False
- for index in range(match, -1, -1):
- if not lines[index].strip() or lines[index].lstrip().startswith("#"):
- continue
- indent = _indent(lines[index])
- if indent <= match_indent and re.match(r"^\s*-\s+", lines[index]):
- anchor = index
- list_anchor_found = True
- break
- for index in range(match - 1, -1, -1):
- if list_anchor_found:
- break
- if not lines[index].strip() or lines[index].lstrip().startswith("#"):
- continue
- indent = _indent(lines[index])
- if indent < match_indent and lines[index].rstrip().endswith(":"):
- anchor = index
- break
- anchor_indent = _indent(lines[anchor])
- for index in range(anchor):
- if not lines[index].strip():
- continue
- indent = _indent(lines[index])
- if indent < anchor_indent and lines[index].rstrip().endswith(":"):
- selected.add(index)
- selected.add(anchor)
- for index in range(anchor + 1, len(lines)):
- if lines[index].strip() and _indent(lines[index]) <= anchor_indent:
- break
- selected.add(index)
- if not selected or len(selected) >= len(lines):
- return _decline(text, "yaml", "no selective subtree match", total_records=len(nonblank))
- 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,
- text=output,
- total_records=len(nonblank),
- kept_records=sum(bool(lines[index].strip()) for index in selected),
- reason="matching subtree with parent keys",
- )
-
-
-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:
- return _decline(text, "xml", "DTD/entity declarations are not processed", structurally_valid=False)
- lines = [line for line in text.splitlines() if line.strip()]
- if len(lines) < 3:
- return _decline(text, "xml", "no line-oriented child records")
- root = re.match(r"^\s*<([A-Za-z_][\w.:-]*)(?:\s[^>]*)?>\s*$", lines[0])
- if root is None or not re.match(rf"^\s*</{re.escape(root.group(1))}>\s*$", lines[-1]):
- return _decline(text, "xml", "root is not line-oriented")
- children = lines[1:-1]
- for child in children:
- try:
- ET.fromstring(child)
- except (ET.ParseError, RecursionError):
- return _decline(text, "xml", "child spans multiple lines", structurally_valid=True)
- 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], *body, lines[-1]]),
- total_records=len(children),
- kept_records=len(kept),
- reason="balanced root plus exact child elements",
- )
-
-
-def _split_sql_statements(text: str) -> list[str] | None:
- statements: list[str] = []
- start = 0
- index = 0
- quote = ""
- dollar_tag = ""
- line_comment = False
- block_comment = False
- while index < len(text):
- if line_comment:
- if text[index] in "\r\n":
- line_comment = False
- index += 1
- continue
- if block_comment:
- if text.startswith("*/", index):
- block_comment = False
- index += 2
- else:
- index += 1
- continue
- if dollar_tag:
- if text.startswith(dollar_tag, index):
- index += len(dollar_tag)
- dollar_tag = ""
- else:
- index += 1
- continue
- if quote:
- char = text[index]
- if char == quote:
- if index + 1 < len(text) and text[index + 1] == quote:
- index += 2
- continue
- quote = ""
- elif char == "\\" and index + 1 < len(text):
- index += 2
- continue
- index += 1
- continue
- if text.startswith("--", index):
- line_comment = True
- index += 2
- continue
- if text.startswith("/*", index):
- block_comment = True
- index += 2
- continue
- char = text[index]
- if char in {"'", '"', "`"}:
- quote = char
- index += 1
- continue
- if char == "$":
- match = re.match(r"\$[A-Za-z_][A-Za-z0-9_]*\$|\$\$", text[index:])
- if match:
- dollar_tag = match.group(0)
- index += len(dollar_tag)
- continue
- if char == ";":
- statement = text[start : index + 1].strip()
- if statement:
- statements.append(statement)
- start = index + 1
- index += 1
- if quote or dollar_tag or block_comment:
- return None
- tail = text[start:].strip()
- if tail:
- statements.append(tail)
- return statements
-
-
-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)
- if len(statements) > limits.max_records:
- 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")
- 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(body),
- total_records=len(statements),
- kept_records=len(kept),
- reason="exact matching SQL statements",
- )
-
-
-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)]
- if len(starts) < 2:
- return _decline(text, "ini", "fewer than two complete sections")
- sections: list[str] = []
- 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"))
- 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(body),
- total_records=len(sections),
- kept_records=len(kept),
- reason="exact matching INI/TOML sections",
- )
-
-
-_HTML_OPEN_WRAPPER_RE = re.compile(r"^\s*(?:<!doctype\s+html\s*>|<html(?:\s[^>]*)?>|<body(?:\s[^>]*)?>)\s*$", re.IGNORECASE)
-_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, gap_marker: Callable[[int, int], str] | None = None
-) -> FormatResult:
- del limits
- lines = [line for line in text.splitlines() if line.strip()]
- opening: list[str] = []
- closing: list[str] = []
- children: list[str] = []
- for line in lines:
- if _HTML_OPEN_WRAPPER_RE.match(line):
- opening.append(line)
- elif _HTML_CLOSE_WRAPPER_RE.match(line):
- closing.append(line)
- elif _HTML_CHILD_RE.match(line):
- children.append(line)
- else:
- 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")
- 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, *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, 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()]
- 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(body),
- total_records=len(blocks),
- kept_records=len(kept),
- reason="matching logical-order OCR columns",
- )
-
-
-_GapMarker = Callable[[int, int], str] | None
-
-_ADAPTERS: dict[str, Callable[..., FormatResult]] = {
- "ndjson": _adapt_ndjson,
- "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,
- "html": _adapt_html,
- "sql": _adapt_sql,
- "ini": _adapt_ini,
- "vertical": _adapt_vertical,
-}
-
-
-def adapt_format(
- text: str,
- query: str,
- 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)
- if not selectors:
- return _decline(value, resolved_format, "query has no distinctive selectors")
- 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(), gap_marker)
diff --git a/plugins/context_engine/tameru/hermes_extractive_engine.py b/plugins/context_engine/tameru/hermes_extractive_engine.py
deleted file mode 100644
index b7c7d521302..00000000000
--- a/plugins/context_engine/tameru/hermes_extractive_engine.py
+++ /dev/null
@@ -1,30 +0,0 @@
-"""Backwards-compatible alias for the generic transcript adapter.
-
-The implementation moved to ``tameru.transcript`` — it was always
-harness-agnostic (OpenAI-style ``role``/``content`` dicts); only the name
-said "hermes". This shim keeps existing imports working, including
-private helpers used by tests.
-"""
-from . import transcript as _transcript
-from .transcript import (
- MIN_TOOL_CHARS,
- PROTECT_LAST_TOOL,
- apply_extractive_tool_prune,
- bulky_tools_dropped,
- last_user_text,
- query_facts_lost,
-)
-
-
-def __getattr__(name: str):
- return getattr(_transcript, name)
-
-
-__all__ = [
- "MIN_TOOL_CHARS",
- "PROTECT_LAST_TOOL",
- "apply_extractive_tool_prune",
- "bulky_tools_dropped",
- "last_user_text",
- "query_facts_lost",
-]
diff --git a/plugins/context_engine/tameru/industrial.py b/plugins/context_engine/tameru/industrial.py
deleted file mode 100644
index a4fb0c25be1..00000000000
--- a/plugins/context_engine/tameru/industrial.py
+++ /dev/null
@@ -1,214 +0,0 @@
-"""Industrial preflight, limits, profiling, and format orchestration."""
-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
-
-
-@dataclass(frozen=True)
-class IndustrialLimits:
- max_input_chars: int = 8_000_000
- max_lines: int = 250_000
- max_records: int = 100_000
- max_record_chars: int = 1_000_000
- max_fields: int = 4_096
- max_profile_chars: int = 16_384
- max_bidi_controls: int = 10_000
- max_bidi_overrides: int = 128
- max_query_chars: int = 32_768
- max_blocks: int = 20_000
- max_receipt_ids: int = 512
-
- def __post_init__(self) -> None:
- for name, value in asdict(self).items():
- if not isinstance(value, int) or isinstance(value, bool) or value <= 0:
- raise ValueError(f"{name} must be a positive integer, got {value!r}")
-
- def format_limits(self) -> FormatLimits:
- return FormatLimits(
- max_records=self.max_records,
- max_record_chars=self.max_record_chars,
- max_fields=self.max_fields,
- )
-
- def to_dict(self) -> dict[str, int]:
- return asdict(self)
-
-
-@dataclass(frozen=True)
-class InputProfile:
- characters: int
- lines: int
- format: str
- direction: str
- scripts: tuple[str, ...]
- graphemes: int
- bidi_controls: int
- bidi_overrides: int
- vertical_hint: bool
- malformed_surrogates: int
- sampled_chars: int
- profile_truncated: bool
- limit_reason: str = ""
-
- def to_dict(self) -> dict[str, object]:
- return asdict(self)
-
-
-@dataclass(frozen=True)
-class IndustrialResult:
- text: str
- applied: bool
- hard_fail_open: bool
- profile: InputProfile
- limits: IndustrialLimits
- format_result: FormatResult | None = None
- reason: str = ""
-
- def to_dict(self) -> dict[str, object]:
- return {
- "applied": self.applied,
- "hard_fail_open": self.hard_fail_open,
- "reason": self.reason,
- "profile": self.profile.to_dict(),
- "limits": self.limits.to_dict(),
- "format_result": (
- self.format_result.to_dict() if self.format_result is not None else None
- ),
- }
-
-
-def _profile_sample(text: str, max_profile_chars: int) -> tuple[str, bool]:
- if len(text) <= max_profile_chars * 2:
- return text, False
- return text[:max_profile_chars] + text[-max_profile_chars:], True
-
-
-def profile_input(text: str, limits: IndustrialLimits | None = None) -> InputProfile:
- value = str(text or "")
- resolved = limits or IndustrialLimits()
- line_count = value.count("\n") + (1 if value else 0)
- limit_reason = ""
- if len(value) > resolved.max_input_chars:
- limit_reason = (
- f"character limit exceeded: {len(value)} > {resolved.max_input_chars}"
- )
- elif line_count > resolved.max_lines:
- limit_reason = f"line limit exceeded: {line_count} > {resolved.max_lines}"
- sample, truncated = _profile_sample(value, resolved.max_profile_chars)
- unicode = profile_text(sample)
- if not limit_reason:
- controls, overrides, malformed = unicode_safety_counts(value)
- unicode = replace(
- unicode,
- bidi_controls=controls,
- bidi_overrides=overrides,
- malformed_surrogates=malformed,
- )
- if controls > resolved.max_bidi_controls:
- limit_reason = (
- f"bidi control limit exceeded: {controls} > "
- f"{resolved.max_bidi_controls}"
- )
- elif overrides > resolved.max_bidi_overrides:
- limit_reason = (
- f"bidi override limit exceeded: {overrides} > "
- f"{resolved.max_bidi_overrides}"
- )
- return InputProfile(
- characters=len(value),
- lines=line_count,
- format=detect_format(sample),
- direction=unicode.direction,
- scripts=tuple(sorted(unicode.scripts)),
- graphemes=unicode.grapheme_count,
- bidi_controls=unicode.bidi_controls,
- bidi_overrides=unicode.bidi_overrides,
- vertical_hint=unicode.vertical_hint,
- malformed_surrogates=unicode.malformed_surrogates,
- sampled_chars=len(sample),
- profile_truncated=truncated,
- limit_reason=limit_reason,
- )
-
-
-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()
- profile = profile_input(value, resolved)
- if len(str(query or "")) > resolved.max_query_chars:
- reason = (
- f"query character limit exceeded: {len(str(query or ''))} > "
- f"{resolved.max_query_chars}"
- )
- return IndustrialResult(
- text=value,
- applied=False,
- hard_fail_open=True,
- profile=profile,
- limits=resolved,
- reason=reason,
- )
- if profile.limit_reason:
- return IndustrialResult(
- text=value,
- applied=False,
- hard_fail_open=True,
- profile=profile,
- limits=resolved,
- reason=profile.limit_reason,
- )
- if profile.malformed_surrogates:
- return IndustrialResult(
- text=value,
- applied=False,
- hard_fail_open=True,
- profile=profile,
- limits=resolved,
- reason="malformed surrogate code point detected",
- )
- format_result = adapt_format(
- value,
- query,
- resolved.format_limits(),
- format_name=profile.format,
- gap_marker=gap_marker,
- )
- if not format_result.structurally_valid:
- return IndustrialResult(
- text=value,
- applied=False,
- hard_fail_open=True,
- profile=profile,
- limits=resolved,
- format_result=format_result,
- reason=format_result.reason or "malformed structured input",
- )
- if format_result.applied and len(format_result.text) < len(value):
- return IndustrialResult(
- text=format_result.text,
- applied=True,
- hard_fail_open=False,
- profile=profile,
- limits=resolved,
- format_result=format_result,
- reason=format_result.reason,
- )
- return IndustrialResult(
- text=value,
- applied=False,
- hard_fail_open=False,
- profile=profile,
- limits=resolved,
- format_result=format_result,
- reason=format_result.reason,
- )
diff --git a/plugins/context_engine/tameru/plugin.yaml b/plugins/context_engine/tameru/plugin.yaml
deleted file mode 100644
index a5a4133ec46..00000000000
--- a/plugins/context_engine/tameru/plugin.yaml
+++ /dev/null
@@ -1,4 +0,0 @@
-name: tameru
-display_name: Tameru (貯める)
-description: "Tameru 1.4.1 industrial deterministic context compaction"
-version: 1.4.1
diff --git a/plugins/context_engine/tameru/semantic.py b/plugins/context_engine/tameru/semantic.py
deleted file mode 100644
index 229cefb0662..00000000000
--- a/plugins/context_engine/tameru/semantic.py
+++ /dev/null
@@ -1,84 +0,0 @@
-"""Opt-in semantic tier for Tameru — embeddings + optional cross-encoder.
-
-Contract (v0.8.0):
- - NEVER imported by the core engine. The engine accepts a tier object
- duck-typed on two methods; this module is one possible implementation.
- - Zero deps by default: if sentence-transformers is missing, `available`
- is False and every method raises SemanticUnavailable — callers must
- fall back to lexical.
- - Local-only: models load from disk/HF cache. No network at query time
- beyond the one-time HF download the user opted into.
- - Deterministic given a pinned model: encode() is inference-only, no RNG.
-"""
-from __future__ import annotations
-
-from typing import Any, Sequence
-
-DEFAULT_BI_ENCODER = "sentence-transformers/all-MiniLM-L6-v2"
-DEFAULT_CROSS_ENCODER = "cross-encoder/ms-marco-MiniLM-L-6-v2"
-
-
-class SemanticUnavailable(RuntimeError):
- """Raised when the tier is used without its optional dependencies."""
-
-
-class SemanticTier:
- """Bi-encoder block/query scorer with optional CE rerank."""
-
- def __init__(
- self,
- bi_model: str = DEFAULT_BI_ENCODER,
- cross_model: str | None = None,
- device: str = "cpu",
- ) -> None:
- try:
- from sentence_transformers import SentenceTransformer, CrossEncoder # type: ignore
- except ImportError as e: # pragma: no cover - env-dependent
- raise SemanticUnavailable(
- "sentence-transformers not installed; pip install tameru[semantic]"
- ) from e
- self._bi = SentenceTransformer(bi_model, device=device)
- self._ce = CrossEncoder(cross_model, device=device) if cross_model else None
- self.available = True
-
- # -- bi-encoder -----------------------------------------------------
- def encode(self, texts: Sequence[str]) -> list[list[float]]:
- vecs = self._bi.encode(list(texts), normalize_embeddings=True, show_progress_bar=False)
- return [v.tolist() for v in vecs]
-
- def similarity(self, query_vec: Sequence[float], vecs: Sequence[Sequence[float]]) -> list[float]:
- return [sum(a * b for a, b in zip(query_vec, v)) for v in vecs]
-
- def score_against_query(self, query: str, texts: Sequence[str]) -> tuple[list[float], list[float]]:
- """Returns (query_vector, per-text cosine similarities)."""
- qv = self.encode([query])[0]
- tv = self.encode(list(texts))
- return qv, self.similarity(qv, tv)
-
- # -- cross-encoder ---------------------------------------------------
- def cross_scores(self, query: str, texts: Sequence[str]) -> list[float] | None:
- if self._ce is None:
- return None
- pairs = [(query, t) for t in texts]
- return [float(s) for s in self._ce.predict(pairs)]
-
-
-class _DisabledTier:
- """Null-object fallback: every call reports unavailable."""
-
- available = False
-
- def __getattr__(self, name):
- def _raise(*a: Any, **k: Any):
- raise SemanticUnavailable("semantic tier disabled")
-
- return _raise
-
-
-def resolve_tier(tier: Any = None) -> Any:
- """Accept a live SemanticTier / None / dict config; always returns a usable object."""
- if tier is None:
- return _DisabledTier()
- if isinstance(tier, dict):
- return SemanticTier(**tier)
- return tier
diff --git a/plugins/context_engine/tameru/supersession.py b/plugins/context_engine/tameru/supersession.py
deleted file mode 100644
index 81dd5cdf3b1..00000000000
--- a/plugins/context_engine/tameru/supersession.py
+++ /dev/null
@@ -1,161 +0,0 @@
-"""Temporal supersession: prune stale facts when a later block explicitly
-marks them outdated. Self-contained by design — no imports from
-compress_context (which consumes this module), so no import cycles.
-"""
-from __future__ import annotations
-
-import re
-from typing import Any
-
-_TOKEN_RE = re.compile(r"[a-zA-Z][a-zA-Z0-9_/-]+")
-_DATE_RE = re.compile(r"\b(20\d{2})-(\d{2})-(\d{2})\b")
-
-# Minimal stop set — enough for overlap filtering; not the engine's full one.
-_STOP = frozenset(
- "a an the is are was were be been being this that these those with into "
- "about for and or of to in on at by from as it its if not no nor but so "
- "than then there here what which who whom whose when where why how does "
- "do did done has have had having will would shall should can could may "
- "might must over under again further once all any both each few more "
- "most other some such only own same too very".split()
-)
-
-_SUPERSESSION_MARKERS = (
- " is now ",
- " are now ",
- " has changed",
- " have changed",
- " changed to",
- " changed from",
- " renamed to",
- " moved to",
- " replaced by",
- " replaced with",
- " deprecated",
- " obsolete",
- " no longer ",
- " not enabled",
- " disabled",
- " override",
- " current value",
-)
-
-
-def _content_terms(text: str) -> set[str]:
- return {
- t
- for t in (w.casefold() for w in _TOKEN_RE.findall(text or ""))
- if len(t) >= 4 and t not in _STOP
- }
-
-
-# Terms too generic to count toward supersession overlap. Without this,
-# any long technical document shares >=2 of these with any other block
-# and a stray " override" mention evicts the head block (v0.6.0 holdout
-# regression: database-skill head lost because a postgres section said
-# "permissions ... override").
-_OVERLAP_STOP = frozenset(
- """
- access actual after already back bash before being between both build
- called change changes check code command config create created current
- default different does doing during error errors example failed file
- files first following from general given grant have here into issue
- just like line lines list local make memory method mode need needed
- needs next only other output over override permissions problem process
- provide read reading required right running same section service should
- since some start starting still such system table test that their them
- then there these they thing this those through time tool tools true
- under until update used user using value values want well were what
- when where which while will with within without work working would your
- """.split()
-)
-
-
-def _meaningful_overlap(newer_terms: set[str], older_terms: set[str]) -> bool:
- """Shared *distinctive* vocabulary — at least two terms that are not
- generic technical filler."""
- shared = newer_terms & older_terms
- distinctive = {t for t in shared if t not in _OVERLAP_STOP}
- return len(distinctive) >= 2
-
-
-def _supersedes(newer: str, older: str) -> bool:
- """True when `newer` explicitly marks `older` stale.
-
- Conservative by design: requires an explicit staleness/override marker
- in the newer block ("is now", "obsolete", "override", "no longer", …).
- A newer date alone — even with neutral "note:" framing — never evicts,
- because a note that merely mentions the topic is not a correction.
- Either way it also requires shared content vocabulary between the two
- blocks, so unrelated lines never evict each other.
- """
- n_low = newer.casefold()
- if not any(m in n_low for m in _SUPERSESSION_MARKERS):
- return False
-
- # Content overlap: at least two *distinctive* (non-filler) terms shared.
- return _meaningful_overlap(_content_terms(newer), _content_terms(older))
-
-
-# Supersession targets compact fact lines ("config: X is 30s" vs "X is now
-# 5s"). Two large prose/JSON blocks always share enough vocabulary, so the
-# signal is meaningless above this size — never prune a big block because
-# another big block says "override" somewhere (v0.6.0 holdout regression).
-_MAX_SUPERSESSION_TOKENS = 120
-
-
-def apply_supersession(scored: list[dict[str, Any]], kept: set[int]) -> set[int]:
- """Drop kept blocks that a later kept block explicitly supersedes.
-
- Only prunes; never adds. Runs after selection so it cannot rescue a
- distractor — it can only remove stale facts the selector already liked
- when a newer authoritative line survives too.
-
- Performance: terms are tokenised once per block and an inverted
- term→id index proposes candidate pairs (shared vocabulary), so cost is
- near-linear in total tokens rather than O(k²) pair enumeration.
- """
- kept_ids = sorted(i for i in kept if 0 <= i < len(scored))
- if len(kept_ids) < 2:
- return kept
-
- terms_by_id = {i: _content_terms(scored[i]["text"]) for i in kept_ids}
- marker_ids = [
- i
- for i in kept_ids
- if any(m in scored[i]["text"].casefold() for m in _SUPERSESSION_MARKERS)
- ]
- if not marker_ids:
- return kept
-
- # Inverted index over kept blocks (newer candidates included).
- postings: dict[str, list[int]] = {}
- for i in kept_ids:
- for t in terms_by_id[i]:
- postings.setdefault(t, []).append(i)
-
- stale: set[int] = set()
- for new_id in marker_ids:
- # Size guard: only compact blocks may act as supersessors or be
- # superseded — see _MAX_SUPERSESSION_TOKENS note above.
- if len(terms_by_id[new_id]) > _MAX_SUPERSESSION_TOKENS:
- continue
- n_terms = terms_by_id[new_id]
- if not n_terms:
- continue
- # Candidate older blocks: those sharing >=2 terms with new_id,
- # found by counting postings across this block's terms.
- counts: dict[int, int] = {}
- for t in n_terms:
- for other in postings.get(t, ()):
- if other < new_id and len(terms_by_id[other]) <= _MAX_SUPERSESSION_TOKENS:
- counts[other] = counts.get(other, 0) + 1
- for old_id, shared_n in counts.items():
- if old_id in stale:
- continue
- if shared_n >= 2 and _supersedes(scored[new_id]["text"], scored[old_id]["text"]):
- stale.add(old_id)
-
- if stale:
- kept -= stale
- return kept
diff --git a/plugins/context_engine/tameru/transcript.py b/plugins/context_engine/tameru/transcript.py
deleted file mode 100644
index e1a2cb97979..00000000000
--- a/plugins/context_engine/tameru/transcript.py
+++ /dev/null
@@ -1,690 +0,0 @@
-"""Transcript-level extractive prune — harness-agnostic, no host imports.
-
-Operates on OpenAI-style message lists (``[{"role": ..., "content": ...}]``),
-the convention shared by Hermes, OpenCode, Codex, and most agent harnesses.
-Turns bulky old tool-result payloads into extractive keep/drop using the
-same compress_context contract. Safe to unit-test standalone.
-"""
-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
-from .compress_context import (
- _SUMMARY_STRUCTURED_TOKEN_RE,
- _detect_json_payloads,
- _extract_entities,
- _extract_terms,
- _json_query_needles,
- _selector_patterns,
- _summary_preserves_required_facts,
- compress_context,
- preprocess_json,
-)
-
-MIN_TOOL_CHARS = 800
-PROTECT_LAST_TOOL = 2
-
-_PRESERVATION_VERBS = {"carry", "keep", "preserve", "remember", "retain"}
-_PRESERVATION_QUANTIFIERS = {"all", "entire", "every", "everything", "full"}
-_PRESERVATION_OBJECTS = {
- "content",
- "context",
- "detail",
- "details",
- "fact",
- "facts",
- "history",
- "info",
- "information",
-}
-
-
-_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 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):
- return False
- if "everything" in words:
- return True
- return bool(
- words.intersection(_PRESERVATION_QUANTIFIERS)
- and words.intersection(_PRESERVATION_OBJECTS)
- )
-
-
-def _distinctive_evidence_tokens(text: str) -> set[str]:
- """Return exact anchors strong enough to prove bulky evidence survived."""
- candidates = (
- _SUMMARY_STRUCTURED_TOKEN_RE.findall(text)
- + _extract_entities(text)
- + _extract_terms(text)
- )
- anchors: set[str] = set()
- for candidate in candidates:
- token = str(candidate).strip().casefold()
- if not token or token in GENERIC_WORDS:
- continue
- if (
- len(token) >= 8
- or any(character.isdigit() for character in token)
- or any(separator in token for separator in "._:/-")
- ):
- anchors.add(token)
- return anchors
-
-
-def _json_query_answer_requirements(
- content: str, query: str
-) -> tuple[set[str], set[tuple[str, bool]]]:
- """Return text answers and typed boolean requirements from matching JSON."""
- parsed_values: list[Any] = []
-
- def parse_payload(payload: str) -> None:
- normalised = preprocess_json(payload, query)
- try:
- parsed_values.append(json.loads(normalised))
- except (ValueError, RecursionError): # JSONDecodeError + the 4300-digit int limit
- return
-
- parse_payload(content)
- if not parsed_values:
- for _start, _end, payload in _detect_json_payloads(content):
- parse_payload(payload)
- if not parsed_values:
- return set(), set()
-
- needles = [needle.casefold() for needle in _json_query_needles(query)]
- selectors = _selector_patterns(needles)
- query_words = set(re.findall(r"[a-z0-9]+", (query or "").casefold()))
- answers: set[str] = set()
- booleans: set[tuple[str, bool]] = set()
-
- def visit(value: Any, depth: int = 0) -> None:
- if depth > 12:
- return
- if isinstance(value, dict):
- try:
- record = json.dumps(value, ensure_ascii=False).casefold()
- except (TypeError, ValueError, RecursionError):
- record = ""
- if selectors and any(selector.search(record) for selector in selectors):
- for key, scalar in value.items():
- key_words = set(re.findall(r"[a-z0-9]+", str(key).casefold()))
- if not key_words.intersection(query_words):
- continue
- if isinstance(scalar, bool):
- field = str(key).strip().casefold()
- if field:
- booleans.add((field, scalar))
- continue
- if not isinstance(scalar, (str, int, float)):
- continue
- answer = str(scalar).strip()
- if answer and answer.casefold() not in (query or "").casefold():
- answers.add(answer)
- for child in value.values():
- visit(child, depth + 1)
- elif isinstance(value, list):
- for child in value:
- visit(child, depth + 1)
-
- for parsed in parsed_values:
- visit(parsed)
- return answers, booleans
-
-
-def _json_query_answers(content: str, query: str) -> set[str]:
- """Return scalar values under queried keys in query-matching JSON records."""
- answers, booleans = _json_query_answer_requirements(content, query)
- return answers | {str(value).casefold() for _field, value in booleans}
-
-
-def _boolean_requirement_satisfied(field: str, expected: bool, post: str) -> bool:
- """Recognise explicit and natural-language boolean answers for one field."""
- words = re.findall(r"[a-z0-9]+", field.casefold())
- if not words:
- return False
- field_pattern = r"[\s_-]+".join(re.escape(word) for word in words)
- field_ref = rf"(?<![a-z0-9_]){field_pattern}(?![a-z0-9_])"
- explicit = re.search(
- rf"{field_ref}\s*(?:(?:is|equals?)\s+|[=:]\s*)?"
- r"(true|yes|on|false|no|off)\b",
- post,
- re.IGNORECASE,
- )
- if explicit:
- return (explicit.group(1).casefold() in {"true", "yes", "on"}) is expected
-
- negated = bool(
- re.search(
- rf"\b(?:is\s+)?(?:not|never)\s+(?:currently\s+)?{field_ref}",
- post,
- re.IGNORECASE,
- )
- )
- positive = bool(re.search(field_ref, post, re.IGNORECASE)) and not negated
-
- field_key = "_".join(words)
- if field_key == "enabled":
- not_disabled = bool(re.search(r"\bnot\s+disabled\b", post, re.IGNORECASE))
- disabled = bool(re.search(r"\bdisabled\b", post, re.IGNORECASE)) and not not_disabled
- positive = positive or not_disabled
- negated = negated or disabled
- elif field_key == "active":
- not_inactive = bool(re.search(r"\bnot\s+inactive\b", post, re.IGNORECASE))
- inactive = bool(re.search(r"\binactive\b", post, re.IGNORECASE)) and not not_inactive
- positive = positive or not_inactive
- negated = negated or inactive
-
- return positive if expected else negated
-
-
-def _pending_tool_calls(messages: list[dict[str, Any]]) -> set[str]:
- """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[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]]:
- """Deterministic (name, canonical-args) pairs for an assistant message."""
- sigs: list[tuple[str, str]] = []
- for call in msg.get("tool_calls") or []:
- if not isinstance(call, dict):
- continue
- fn = call.get("function") or {}
- name = str(fn.get("name") or call.get("name") or "")
- args = fn.get("arguments", call.get("arguments"))
- try:
- canonical = (
- args
- if isinstance(args, str)
- else json.dumps(args, sort_keys=True, ensure_ascii=False)
- )
- except (TypeError, ValueError, RecursionError):
- canonical = repr(args)
- sigs.append((name, str(canonical)))
- return sigs
-
-
-def trajectory_gate(
- messages: list[dict[str, Any]], *, stuck_run: int = 3
-) -> tuple[bool, str]:
- """SelfCompact-style timing rubric, deterministic version.
-
- Compaction is suppressed when:
- - ``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).
-
- Returns ``(allowed, reason)``. The gate only ever suppresses — the
- fail-safe direction — so it is safe to leave enabled by default.
- """
- if not messages:
- return True, "ok"
- if _pending_tool_calls(messages):
- return False, "pending-tool-calls"
- assistant = [m for m in messages if m.get("role") == "assistant"]
- if len(assistant) >= stuck_run:
- tail = assistant[-stuck_run:]
- sigs = [_call_signature(m) for m in tail]
- if all(s and s == sigs[0] for s in sigs):
- return False, "stuck-loop"
- return True, "ok"
-
-
-def apply_extractive_tool_prune(
- messages: list[dict[str, Any]],
- query: str | None = None,
- *,
- min_chars: int = MIN_TOOL_CHARS,
- protect_last_tool: int = PROTECT_LAST_TOOL,
- timing_gate: bool = True,
-) -> tuple[list[dict[str, Any]], int]:
- """Compress old bulky tool payloads. Returns (messages, n_changed).
-
- If nothing changes — or ``timing_gate`` suppresses the pass — returns
- the same list object.
- """
- if not messages:
- return messages, 0
- if timing_gate:
- allowed, _reason = trajectory_gate(messages)
- if not allowed:
- return messages, 0
- q = query if query is not None else last_user_text(messages)
- tool_idxs = [i for i, m in enumerate(messages) if m.get("role") == "tool"]
- skip = set(tool_idxs[-protect_last_tool:]) if protect_last_tool else set()
- out: list[dict[str, Any]] | None = None
- changed = 0
- for i, msg in enumerate(messages):
- if i in skip or msg.get("role") != "tool":
- continue
- content = msg.get("content")
- if not isinstance(content, str) or len(content) < min_chars:
- continue
- # Live tool payloads can contain credentials or other secrets. This
- # adapter has no retrieval path, so persisting originals in CCR only
- # adds exposure, unbounded disk retention, and a semantic marker.
- result = compress_context(content, q, ccr=False, citations=False)
- new = result.compressed_text
- if result.fail_open or new == content or len(new) >= len(content):
- continue
- if out is None:
- out = [dict(m) for m in messages]
- out[i] = {**msg, "content": new}
- changed += 1
- return (out if out is not None else messages), changed
-
-
-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.
-
- .. 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
- if message.get("role") == "tool"
- ]
- normalised_tool_evidence = [
- preprocess_json(evidence, query or "") for evidence in raw_tool_evidence
- ]
- requirements_by_evidence = [
- _json_query_answer_requirements(evidence, query or "")
- for evidence in raw_tool_evidence
- ]
- tool_evidence = [
- (
- f"{query}\n" + "\n".join(sorted(answers))
- if answers
- else "" if booleans else normalised
- )
- for normalised, (answers, booleans) in zip(
- normalised_tool_evidence, requirements_by_evidence
- )
- ]
- pre = "\n".join(tool_evidence)
- post = "\n".join(
- str(message.get("content") or "")
- for message in after
- if message.get("role") != "user"
- )
- json_answers = {
- answer
- for answers, _booleans in requirements_by_evidence
- for answer in answers
- }
- boolean_requirements = {
- requirement
- for _answers, booleans in requirements_by_evidence
- for requirement in booleans
- }
- post_fold = post.casefold()
- if any(answer.casefold() not in post_fold for answer in json_answers):
- return True
- if any(
- not _boolean_requirement_satisfied(field, expected, post)
- for field, expected in boolean_requirements
- ):
- return True
- if _requests_full_preservation(query):
- return any(
- evidence.strip() and evidence.strip() not in post
- for evidence in normalised_tool_evidence
- )
- if not pre.strip():
- return False
- valid, _recall = _summary_preserves_required_facts(pre, post, query or "")
- return not valid
-
-
-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.
-
- .. 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
- if m.get("role") == "tool"
- and isinstance(m.get("content"), str)
- and len(m["content"]) >= MIN_TOOL_CHARS
- ]
- if not pre:
- return False
- post = "\n".join(
- str(message.get("content") or "")
- for message in after
- if message.get("role") != "user"
- )
- post_anchors = _distinctive_evidence_tokens(post)
- for evidence in pre:
- evidence_anchors = _distinctive_evidence_tokens(evidence)
- if not evidence_anchors or evidence_anchors.isdisjoint(post_anchors):
- return True
- return False
diff --git a/plugins/context_engine/tameru/unicode_profile.py b/plugins/context_engine/tameru/unicode_profile.py
deleted file mode 100644
index 8c274140446..00000000000
--- a/plugins/context_engine/tameru/unicode_profile.py
+++ /dev/null
@@ -1,359 +0,0 @@
-"""Bounded Unicode profiling for industrial compaction.
-
-The module analyses logical text order only. Matching may use a normalised
-shadow, while callers retain and return original substrings.
-"""
-from __future__ import annotations
-
-import re
-import unicodedata
-from dataclasses import asdict, dataclass
-from typing import Iterator
-
-_BIDI_CONTROLS = frozenset(
- {
- "\u061c", # Arabic letter mark
- "\u200e", # left-to-right mark
- "\u200f", # right-to-left mark
- "\u202a", # embeddings / overrides / pop directional format
- "\u202b",
- "\u202c",
- "\u202d",
- "\u202e",
- "\u2066", # isolates
- "\u2067",
- "\u2068",
- "\u2069",
- "\ufeff", # BOM / zero-width no-break space
- }
-)
-_BIDI_OVERRIDES = frozenset({"\u202d", "\u202e"})
-_ZWJ = "\u200d"
-_ZWNJ = "\u200c"
-_VARIATION_RANGES = ((0xFE00, 0xFE0F), (0xE0100, 0xE01EF))
-_EMOJI_MODIFIER_RANGE = (0x1F3FB, 0x1F3FF)
-_REGIONAL_RANGE = (0x1F1E6, 0x1F1FF)
-_TAG_RANGE = (0xE0020, 0xE007F)
-_VERTICAL_RE = re.compile(
- r"writing-mode\s*:\s*(?:vertical-(?:rl|lr)|sideways-(?:rl|lr))",
- re.IGNORECASE,
-)
-_WORD_JOINERS = frozenset("_-./:@+")
-_NO_SPACE_SCRIPTS = frozenset({"han", "kana", "hangul", "thai", "lao", "khmer", "myanmar"})
-
-
-@dataclass(frozen=True)
-class UnicodeProfile:
- direction: str
- scripts: frozenset[str]
- grapheme_count: int
- bidi_controls: int
- bidi_overrides: int
- vertical_hint: bool
- malformed_surrogates: bool
-
- def to_dict(self) -> dict[str, object]:
- data = asdict(self)
- data["scripts"] = sorted(self.scripts)
- return data
-
-
-def _in_ranges(codepoint: int, ranges: tuple[tuple[int, int], ...]) -> bool:
- return any(start <= codepoint <= end for start, end in ranges)
-
-
-def _is_variation_selector(char: str) -> bool:
- return _in_ranges(ord(char), _VARIATION_RANGES)
-
-
-def _is_emoji_modifier(char: str) -> bool:
- return _EMOJI_MODIFIER_RANGE[0] <= ord(char) <= _EMOJI_MODIFIER_RANGE[1]
-
-
-def _is_regional_indicator(char: str) -> bool:
- return _REGIONAL_RANGE[0] <= ord(char) <= _REGIONAL_RANGE[1]
-
-
-def _is_extend(char: str) -> bool:
- category = unicodedata.category(char)
- codepoint = ord(char)
- return (
- category in {"Mn", "Mc", "Me"}
- or _is_variation_selector(char)
- or _is_emoji_modifier(char)
- or codepoint == 0x20E3
- or _TAG_RANGE[0] <= codepoint <= _TAG_RANGE[1]
- )
-
-
-def _is_virama(char: str) -> bool:
- name = unicodedata.name(char, "")
- return "VIRAMA" in name or "HALANT" in name
-
-
-def iter_graphemes(text: str) -> Iterator[str]:
- """Yield deterministic approximate extended grapheme clusters.
-
- This is a documented UAX #29 tailoring suitable for stdlib-only matching;
- it is intentionally conservative around combining and join sequences.
- """
- current = ""
- regional_count = 0
- for char in str(text or ""):
- if not current:
- current = char
- regional_count = 1 if _is_regional_indicator(char) else 0
- continue
- previous = current[-1]
- append = (
- _is_extend(char)
- or char in {_ZWJ, _ZWNJ}
- or previous in {_ZWJ, _ZWNJ}
- or (_is_virama(previous) and unicodedata.category(char).startswith("L"))
- or (
- _is_regional_indicator(char)
- and _is_regional_indicator(previous)
- and regional_count % 2 == 1
- )
- )
- if append:
- current += char
- if _is_regional_indicator(char):
- regional_count += 1
- continue
- yield current
- current = char
- regional_count = 1 if _is_regional_indicator(char) else 0
- if current:
- yield current
-
-
-def graphemes(text: str) -> list[str]:
- return list(iter_graphemes(text))
-
-
-def matching_shadow(text: str) -> str:
- """Return case-folded NFKC text with presentation-only bidi controls removed."""
- normalised = unicodedata.normalize("NFKC", str(text or ""))
- return "".join(char for char in normalised if char not in _BIDI_CONTROLS).casefold()
-
-
-def unicode_safety_counts(text: str) -> tuple[int, int, bool]:
- """Return bidi controls, overrides, and malformed-surrogate presence."""
- value = str(text or "")
- if value.isascii():
- return 0, 0, False
- controls = 0
- overrides = 0
- malformed = False
- for char in value:
- malformed = malformed or unicodedata.category(char) == "Cs"
- if char in _BIDI_CONTROLS:
- controls += 1
- overrides += int(char in _BIDI_OVERRIDES)
- return controls, overrides, malformed
-
-
-def script_of(char: str) -> str | None:
- """Return a stable script family for a representative character."""
- if not char:
- return None
- codepoint = ord(char)
- ranges = (
- ("hebrew", 0x0590, 0x05FF),
- ("arabic", 0x0600, 0x08FF),
- ("devanagari", 0x0900, 0x097F),
- ("bengali", 0x0980, 0x09FF),
- ("gurmukhi", 0x0A00, 0x0A7F),
- ("gujarati", 0x0A80, 0x0AFF),
- ("odia", 0x0B00, 0x0B7F),
- ("tamil", 0x0B80, 0x0BFF),
- ("telugu", 0x0C00, 0x0C7F),
- ("kannada", 0x0C80, 0x0CFF),
- ("malayalam", 0x0D00, 0x0D7F),
- ("sinhala", 0x0D80, 0x0DFF),
- ("thai", 0x0E00, 0x0E7F),
- ("lao", 0x0E80, 0x0EFF),
- ("tibetan", 0x0F00, 0x0FFF),
- ("myanmar", 0x1000, 0x109F),
- ("georgian", 0x10A0, 0x10FF),
- ("ethiopic", 0x1200, 0x137F),
- ("khmer", 0x1780, 0x17FF),
- ("mongolian", 0x1800, 0x18AF),
- ("han", 0x3400, 0x9FFF),
- ("kana", 0x3040, 0x30FF),
- ("hangul", 0xAC00, 0xD7AF),
- ("armenian", 0x0530, 0x058F),
- ("greek", 0x0370, 0x03FF),
- ("cyrillic", 0x0400, 0x052F),
- )
- for name, start, end in ranges:
- if start <= codepoint <= end:
- return name
- unicode_name = unicodedata.name(char, "")
- for name, marker in (
- ("latin", "LATIN"),
- ("greek", "GREEK"),
- ("cyrillic", "CYRILLIC"),
- ("arabic", "ARABIC"),
- ("hebrew", "HEBREW"),
- ):
- if marker in unicode_name:
- return name
- return "other" if unicodedata.category(char).startswith("L") else None
-
-
-def _representative(cluster: str) -> str:
- for char in cluster:
- if unicodedata.category(char)[0] in {"L", "N"}:
- return char
- return cluster[0] if cluster else ""
-
-
-def _flush_run(run: list[str], units: list[str]) -> None:
- if not run:
- return
- representative = _representative(run[0])
- script = script_of(representative)
- joined = "".join(run)
- if script in _NO_SPACE_SCRIPTS:
- if len(joined) <= 64:
- units.append(joined)
- if len(run) == 1 and run[0].strip():
- units.append(run[0])
- for width in (2, 3, 4):
- if len(run) < width:
- continue
- units.extend("".join(run[index : index + width]) for index in range(len(run) - width + 1))
- elif joined:
- units.append(joined)
- run.clear()
-
-
-def search_units(text: str) -> list[str]:
- """Return ordered, deduplicated Unicode-aware units for query matching."""
- shadow = matching_shadow(text)
- units: list[str] = []
- run: list[str] = []
- for cluster in iter_graphemes(shadow):
- representative = _representative(cluster)
- category = unicodedata.category(representative) if representative else ""
- if category[:1] in {"L", "N", "M"} or all(char in _WORD_JOINERS for char in cluster):
- run.append(cluster)
- else:
- _flush_run(run, units)
- _flush_run(run, units)
- seen: set[str] = set()
- ordered: list[str] = []
- for unit in units:
- cleaned = unit.strip("_-./:@+")
- value = unit if cleaned else ""
- if value and value not in seen:
- seen.add(value)
- ordered.append(value)
- return ordered
-
-
-def token_units(text: str) -> list[str]:
- """Return non-overlapping logical units for deterministic token estimates."""
- units: list[str] = []
- run: list[str] = []
- run_script: str | None = None
-
- def flush() -> None:
- nonlocal run_script
- if run:
- units.append("".join(run))
- run.clear()
- run_script = None
-
- for cluster in iter_graphemes(matching_shadow(text)):
- if not cluster or cluster.isspace():
- flush()
- continue
- representative = _representative(cluster)
- category = unicodedata.category(representative) if representative else ""
- script = script_of(representative)
- if category[:1] in {"L", "N", "M"}:
- if script in _NO_SPACE_SCRIPTS:
- flush()
- units.append(cluster)
- continue
- if run and script and run_script and script != run_script:
- flush()
- run.append(cluster)
- run_script = run_script or script
- continue
- if all(char in _WORD_JOINERS for char in cluster) and run:
- run.append(cluster)
- continue
- flush()
- if any(unicodedata.category(char) == "So" for char in cluster):
- units.append(cluster)
- flush()
- return units
-
-
-def _looks_vertical_ocr(text: str) -> bool:
- lines = [line.strip() for line in str(text or "").splitlines() if line.strip()]
- if len(lines) < 6:
- return False
- short = sum(1 for line in lines[:128] if len(graphemes(line)) <= 2)
- return short / min(len(lines), 128) >= 0.8
-
-
-def profile_text(text: str) -> UnicodeProfile:
- value = str(text or "")
- if value.isascii():
- has_latin = any(char.isalpha() for char in value)
- lines = [line.strip() for line in value.splitlines() if line.strip()][:128]
- vertical = bool(_VERTICAL_RE.search(value)) or (
- len(lines) >= 6
- and sum(len(line) <= 2 for line in lines) / len(lines) >= 0.8
- )
- return UnicodeProfile(
- direction="ltr" if has_latin else "neutral",
- scripts=frozenset({"latin"}) if has_latin else frozenset(),
- grapheme_count=len(value),
- bidi_controls=0,
- bidi_overrides=0,
- vertical_hint=vertical,
- malformed_surrogates=False,
- )
- scripts: set[str] = set()
- ltr = 0
- rtl = 0
- controls = 0
- overrides = 0
- malformed = False
- for char in value:
- category = unicodedata.category(char)
- malformed = malformed or category == "Cs"
- if char in _BIDI_CONTROLS:
- controls += 1
- overrides += int(char in _BIDI_OVERRIDES)
- bidi = unicodedata.bidirectional(char)
- if bidi == "L":
- ltr += 1
- elif bidi in {"R", "AL"}:
- rtl += 1
- script = script_of(char)
- if script:
- scripts.add(script)
- if ltr and rtl:
- direction = "mixed"
- elif rtl:
- direction = "rtl"
- elif ltr:
- direction = "ltr"
- else:
- direction = "neutral"
- return UnicodeProfile(
- direction=direction,
- scripts=frozenset(scripts),
- grapheme_count=sum(1 for _ in iter_graphemes(value)),
- bidi_controls=controls,
- bidi_overrides=overrides,
- vertical_hint=bool(_VERTICAL_RE.search(value)) or _looks_vertical_ocr(value),
- malformed_surrogates=malformed,
- )
diff --git a/tests/plugins/test_tameru_context_engine.py b/tests/plugins/test_tameru_context_engine.py
deleted file mode 100644
index 6e54a3c935d..00000000000
--- a/tests/plugins/test_tameru_context_engine.py
+++ /dev/null
@@ -1,32 +0,0 @@
-"""Tameru's discovered adapter preserves Hermes call options and text-only queries."""
-
-from unittest.mock import patch
-
-from agent.context_compressor import ContextCompressor
-from plugins.context_engine import load_context_engine
-
-
-def test_compress_forwards_overflow_cooldown_override():
- engine = load_context_engine("tameru")
- assert engine is not None
- messages = [{"role": "user", "content": "Find the deployment error"}]
- with patch.object(ContextCompressor, "compress", return_value=messages) as parent:
- assert engine.compress(messages, bypass_cooldown=True) == messages
- assert parent.call_args.kwargs["bypass_cooldown"] is True
-
-
-def test_multimodal_user_query_excludes_image_payload_in_both_paths():
- engine = load_context_engine("tameru")
- assert engine is not None
- messages = [{"role": "user", "content": [
- {"type": "text", "text": "Find DB-PROD-42"},
- {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}},
- ]}]
- with (
- patch("plugins.context_engine.tameru.apply_extractive_tool_prune", return_value=(messages, 0)) as prune,
- patch.object(ContextCompressor, "prune_tool_results_only", return_value=(messages, 0)),
- patch.object(ContextCompressor, "compress", return_value=messages),
- ):
- engine.prune_tool_results_only(messages)
- engine.compress(messages)
- assert [call.args[1] for call in prune.call_args_list] == ["Find DB-PROD-42"] * 2