mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-24 19:52:53 +08:00
fix(compression): bound the JSON-block and truncation-banner scans (#4845)
## Description
Two scans over tool output ran in quadratic time:
1. **Mixed-content JSON detection** (`_has_valid_json_block_with_text`,
default-on through `ContentRouter` → `is_mixed_content`) and the section
splitter try every line that starts with `{` or `[` as a JSON block. A
candidate that never balances walks to the end of the content, so N such
lines cost N²/2 line steps (20,000 lines of `{` took about a minute).
The existing per-line memo removes the character work but not the line
walk.
2. **ast-grep truncation-banner detection** (`_detect_truncation`): the
banner regex has unbounded runs on both sides of the `showing lines A-B
of T total` phrase, so an unclosed banner was rescanned once per phrase.
Fixes:
1. `_JsonBlockScanner` answers every candidate of one pass with exactly
the result of walking it. Candidates are walked directly until they have
walked as many lines as the content has. After that, all candidates are
answered together:
- Every walk is a chain of (line, string-state) nodes with one successor
each, so all walks form one forest, and each node is scanned once.
- With `D(v)` the bracket and brace sums from node `v` to the end of its
chain, a walk from `u` balances at `w` exactly when `D(next(w)) >= D(u)`
in both sums. So each candidate ends just before its nearest ancestor
that satisfies this.
- One depth-first pass with an undoable tree of (brace sum, depth)
stacks, keyed by bracket sum, finds that ancestor in O(log² n). There is
no budget.
2. The banner is found in two linear steps: locate `[truncated: partial
view`, take the span to the next bracket (it must be `]`), then match
the phrase inside that span. Same matches as the old regex (checked
against it on 3,000 random layouts).
Fix adapted from a proposal by HetCreep.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [x] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/transforms/mixed_content.py`: `_JsonBlockScanner` (direct
walks, then a forest solve over all candidates), used by
`_has_valid_json_block_with_text` and `split_into_sections`.
`_extract_json_block` is unchanged from main.
- `headroom/proxy/interceptors/astgrep.py`: `_TRUNCATION_SIGNATURES` now
holds only the phrase pattern; `_iter_banners` finds the `[truncated:
partial view … ]` span and searches the phrase inside it.
- `tests/test_tool_output_scan_bounds.py`: timing tests for both shapes,
an oracle test against the previous banner regex, and checks that a real
banner and a 5,000-line JSON block inside text are still found. A JSON
object that follows 150 or 20,000 unclosed `{` lines, or a prefix whose
bracket and brace counts each reach zero but never on the same line
(once, and repeated 130 times), still gets its own JSON section, as does
one after 100 or 10,000 pairs of lines ending inside an escaped string.
The solve is checked against `_extract_json_block` on 3,000 random
inputs.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
New tests on `origin/main` code:
```text
FAILED tests/test_tool_output_scan_bounds.py::test_truncation_scan_is_linear_on_unclosed_banner
FAILED tests/test_tool_output_scan_bounds.py::test_json_block_scans_are_bounded_on_unclosed_braces
assert time.perf_counter() - start < 10.0
E assert (63611.161382791 - 63549.279589625) < 10.0
==================== 2 failed, 3 passed in 75.96s (0:01:15) ====================
```
With the fix:
```text
$ pytest -q tests/test_tool_output_scan_bounds.py --durations=3 # (first version; now 10 tests, 10 passed in 0.88s)
0.34s call tests/test_tool_output_scan_bounds.py::test_json_block_scans_are_bounded_on_unclosed_braces
0.02s call tests/test_tool_output_scan_bounds.py::test_truncation_banner_matches_previous_regex
============================== 5 passed in 1.09s ===============================
$ pytest -q tests/test_tool_output_scan_bounds.py tests/test_mixed_content_scan_cache.py tests/test_mixed_content_sections.py tests/test_content_router_mixed_fence_placeholder.py
============================= 300 passed in 5.90s ==============================
$ pytest -q tests/test_tool_result_interceptors.py tests/test_proxy_compression_executor.py tests/test_subprocess_encoding.py tests/test_runtime_env.py tests/test_content_router*.py
============================= 247 passed in 32.58s =============================
$ ruff check / ruff format --check (0.17.3) on changed files: All checks passed
$ mypy headroom/proxy/interceptors/astgrep.py headroom/transforms/mixed_content.py --ignore-missing-imports
Success: no issues found in 2 source files
```
## Real Behavior Proof
- Environment: macOS (Apple silicon), Python 3.12 venv, this branch vs
`origin/main`.
- Exact command / steps: called `_has_valid_json_block_with_text` +
`split_into_sections` on 20,000 lines of `{`, and `_detect_truncation`
on `"[truncated: partial view " + "showing lines 1-10 of 50 total " *
12000`.
- Observed result: JSON scans about 62 s on main, 0.34 s here; banner
scan about 13 s on main, milliseconds here. The existing scan-cache
oracle tests (which compare against the original extractor) still pass.
- Not tested: end-to-end through the proxy; both functions are pure and
covered directly.
## Runtime Rollout Safety
- Rollout-managed feature(s): none
- Minimum rollout channel: n/a
- Stable/default behavior changed: no change in results (exact); only
time and memory bounds change
- Kill switch / disable path: none needed
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this PR
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation — n/a
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
## Additional Notes
Timings with the fix: 20,000 unclosed `{` lines take 0.13 s, the
crossed-count prefix repeated 130 times takes 0.23 s, 20,000 alternating
`{` / `}[` lines take 0.29 s, and 10,000 escaped-string line pairs take
0.14 s. With direct walking, the first takes about 62 s. Separately, on
main a balanced block nested about 10,000 deep makes `json.loads` raise
`RecursionError` in the gate; that is unchanged here.
Note: `tests/test_transforms_content_router.py` has 3 failures
(`test_json_in_html_preserves_records_and_markup`,
`test_embedded_json_mixed_tool_output_preserves_every_record[minify-*]`)
that also fail on unmodified `origin/main` in my environment, so they
are not caused by this change.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Reported by @JoaoMarcos44. Thank you for your contribution! 🩵
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
JerrettDavis
parent
f5c4217648
commit
bae62bc87b
@@ -19,6 +19,7 @@ import shutil
|
||||
import stat
|
||||
import subprocess
|
||||
import tempfile
|
||||
from collections.abc import Iterator
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
@@ -98,15 +99,33 @@ OUTLINE_MARKER = " # ... (body elided by Headroom; Read a specific line range
|
||||
_TRUNCATION_SIGNATURES: tuple[re.Pattern[str], ...] = (
|
||||
# Claude Code: "[Truncated: PARTIAL view -- <path>: showing lines A-B of
|
||||
# T total (...). Call Read with offset=N to see more.]"
|
||||
# Matched in two linear steps (see _iter_banners): this pattern is only the
|
||||
# body of one bracket-free span, never run against the rest of the output.
|
||||
re.compile(
|
||||
r"\[\s*truncated\s*:\s*partial\s+view\b"
|
||||
r"[^\[\]]*?"
|
||||
r"showing\s+lines?\s+(?P<start_line>\d+)\s*[-–]\s*(?P<end_line>\d+)"
|
||||
r"\s+of\s+(?P<total_lines>\d+)\s+total"
|
||||
r"[^\[\]]*\]",
|
||||
r"\s+of\s+(?P<total_lines>\d+)\s+total",
|
||||
re.IGNORECASE,
|
||||
),
|
||||
)
|
||||
_BANNER_OPENER = re.compile(r"\[\s*truncated\s*:\s*partial\s+view\b", re.IGNORECASE)
|
||||
_BRACKET = re.compile(r"[\[\]]")
|
||||
|
||||
|
||||
def _iter_banners(source: str, body: re.Pattern[str]) -> Iterator[re.Match[str]]:
|
||||
"""Yield body matches inside each ``[truncated: partial view ... ]`` span.
|
||||
|
||||
A span runs from the opener to the next bracket and counts only when that
|
||||
bracket is ``]``. Spans cannot overlap (the next opener starts with ``[``),
|
||||
so total work is linear. A single regex with unbounded runs on both sides of
|
||||
the phrase rescanned the unclosed tail once per phrase: quadratic.
|
||||
"""
|
||||
for opener in _BANNER_OPENER.finditer(source):
|
||||
close = _BRACKET.search(source, opener.end())
|
||||
if close is None or close.group() != "]":
|
||||
continue
|
||||
m = body.search(source, opener.end(), close.start())
|
||||
if m:
|
||||
yield m
|
||||
|
||||
|
||||
def _is_plausible_truncation_range(
|
||||
@@ -123,7 +142,7 @@ def _detect_truncation(source: str) -> tuple[int, int] | None:
|
||||
internally-consistent upstream truncation banner, else None."""
|
||||
source_line_count = len(source.splitlines())
|
||||
for pattern in _TRUNCATION_SIGNATURES:
|
||||
for m in pattern.finditer(source):
|
||||
for m in _iter_banners(source, pattern):
|
||||
start_line = int(m.group("start_line"))
|
||||
end_line = int(m.group("end_line"))
|
||||
total_lines = int(m.group("total_lines"))
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from bisect import bisect_left, bisect_right
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .content_detector import ContentType, _is_search_result_line
|
||||
@@ -97,19 +98,14 @@ def _any_nonblank(lines: list[str], start: int, stop: int) -> bool:
|
||||
def _has_valid_json_block_with_text(content: str) -> bool:
|
||||
"""Return true when prose or log text wraps a valid JSON block."""
|
||||
lines = content.split("\n")
|
||||
# Built only after a scan has run to the end without balancing — see
|
||||
# _extract_json_block. Content that balances promptly never allocates it and
|
||||
# so pays nothing for it.
|
||||
scan_cache: dict[tuple[int, bool, bool], tuple[int, int, bool, bool]] | None = None
|
||||
blocks = _JsonBlockScanner(lines)
|
||||
|
||||
for index, line in enumerate(lines):
|
||||
if not line.strip().startswith(("[", "{")):
|
||||
if not _is_json_candidate(line):
|
||||
continue
|
||||
|
||||
json_content, end_index = _extract_json_block(lines, index, cache=scan_cache)
|
||||
json_content, end_index = blocks.extract(index)
|
||||
if json_content is None:
|
||||
if scan_cache is None:
|
||||
scan_cache = {}
|
||||
continue
|
||||
|
||||
try:
|
||||
@@ -141,7 +137,7 @@ def split_into_sections(content: str, *, isolate: tuple[str, ...] = ()) -> list[
|
||||
def _carries_isolate(text: str) -> bool:
|
||||
return any(marker in text for marker in isolate)
|
||||
|
||||
scan_cache: dict[tuple[int, bool, bool], tuple[int, int, bool, bool]] | None = None
|
||||
blocks = _JsonBlockScanner(lines)
|
||||
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
@@ -183,12 +179,8 @@ def split_into_sections(content: str, *, isolate: tuple[str, ...] = ()) -> list[
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if line.strip().startswith(("[", "{")):
|
||||
json_content, end_i = _extract_json_block(lines, i, cache=scan_cache)
|
||||
if json_content is None and scan_cache is None:
|
||||
# First scan that ran to the end without balancing: from here on
|
||||
# every later candidate would re-walk the same tail.
|
||||
scan_cache = {}
|
||||
if _is_json_candidate(line):
|
||||
json_content, end_i = blocks.extract(i)
|
||||
if json_content is not None:
|
||||
# Bracket balance alone is not JSON: prose like a harness
|
||||
# sanitizer banner ("[harness: ... you.]") balances on one
|
||||
@@ -336,6 +328,181 @@ def _scan_line(line: str, in_string: bool, escaped: bool) -> tuple[int, int, boo
|
||||
return bracket, brace, in_string, escaped
|
||||
|
||||
|
||||
def _is_json_candidate(line: str) -> bool:
|
||||
return line.strip().startswith(("[", "{"))
|
||||
|
||||
|
||||
class _JsonBlockScanner:
|
||||
"""Answers :func:`_extract_json_block` for every candidate of one pass.
|
||||
|
||||
Each candidate walks forward until its running bracket and brace counts
|
||||
are both <= 0. Walked one by one, N candidates that balance late or never
|
||||
cost N^2/2 line steps. The scanner walks them directly until they have
|
||||
walked as many lines as the content has, then answers all the rest at
|
||||
once, with exactly the same results:
|
||||
|
||||
* A walk is a chain of (line, string state) nodes, and each node has one
|
||||
successor, so the walks of all candidates form a forest whose roots are
|
||||
past the last line. Every node is scanned once, however many walks
|
||||
share it (walks that start in different string states and later fall
|
||||
into the same state merge).
|
||||
* With ``D(v)`` the counts summed from node ``v`` to the end of its
|
||||
chain, a walk from ``u`` balances at ``w`` exactly when
|
||||
``D(next(w)) >= D(u)`` in both counts. So a candidate ends just before
|
||||
its nearest ancestor ``z`` with ``D(z) >= D(u)``.
|
||||
* One depth-first pass from the root keeps, over the ancestors of the
|
||||
current node, a tree keyed by bracket sum whose nodes hold stacks of
|
||||
(brace sum, depth); stacks are updated by overwrite-and-undo so leaving
|
||||
a node restores them. Each query and each push costs O(log^2 n).
|
||||
"""
|
||||
|
||||
def __init__(self, lines: list[str]) -> None:
|
||||
self._lines = lines
|
||||
self._direct_steps_left = len(lines)
|
||||
# Candidate line -> last line of its block (None: never balances).
|
||||
self._ends: dict[int, int | None] | None = None
|
||||
|
||||
def extract(self, start: int) -> tuple[str | None, int]:
|
||||
if self._direct_steps_left > 0:
|
||||
json_content, end = _extract_json_block(self._lines, start)
|
||||
walked = (end if json_content is not None else len(self._lines) - 1) - start + 1
|
||||
self._direct_steps_left -= walked
|
||||
return json_content, end
|
||||
if self._ends is None:
|
||||
self._ends = self._solve()
|
||||
end_line = self._ends.get(start)
|
||||
if end_line is None:
|
||||
return None, start
|
||||
return "\n".join(self._lines[start : end_line + 1]), end_line
|
||||
|
||||
def _solve(self) -> dict[int, int | None]:
|
||||
lines = self._lines
|
||||
n = len(lines)
|
||||
candidates = [i for i, line in enumerate(lines) if _is_json_candidate(line)]
|
||||
|
||||
# Build the forest: node ids in creation order, one per (line, state).
|
||||
node_of: dict[tuple[int, bool, bool], int] = {}
|
||||
node_line: list[int] = []
|
||||
d_bracket: list[int] = []
|
||||
d_brace: list[int] = []
|
||||
succ: list[int] = [] # -1: past the last line (the root)
|
||||
start_node: dict[int, int] = {}
|
||||
for s in candidates:
|
||||
key = (s, False, False)
|
||||
prev = -1
|
||||
while key[0] < n:
|
||||
found = node_of.get(key)
|
||||
if found is not None:
|
||||
if prev >= 0:
|
||||
succ[prev] = found
|
||||
break
|
||||
b, c, in_string, escaped = _scan_line(lines[key[0]], key[1], key[2])
|
||||
v = len(node_line)
|
||||
node_of[key] = v
|
||||
node_line.append(key[0])
|
||||
d_bracket.append(b)
|
||||
d_brace.append(c)
|
||||
succ.append(-1)
|
||||
if prev >= 0:
|
||||
succ[prev] = v
|
||||
prev = v
|
||||
key = (key[0] + 1, in_string, escaped)
|
||||
start_node[s] = node_of[(s, False, False)]
|
||||
|
||||
# Suffix sums D, children lists (successors have larger lines).
|
||||
count = len(node_line)
|
||||
sum_b = [0] * count
|
||||
sum_c = [0] * count
|
||||
children: list[list[int]] = [[] for _ in range(count)]
|
||||
roots: list[int] = []
|
||||
for v in sorted(range(count), key=node_line.__getitem__, reverse=True):
|
||||
nxt = succ[v]
|
||||
if nxt < 0:
|
||||
sum_b[v], sum_c[v] = d_bracket[v], d_brace[v]
|
||||
roots.append(v)
|
||||
else:
|
||||
sum_b[v], sum_c[v] = d_bracket[v] + sum_b[nxt], d_brace[v] + sum_c[nxt]
|
||||
children[nxt].append(v)
|
||||
is_query = {start_node[s] for s in candidates}
|
||||
|
||||
# Tree over bracket sums; each tree node stacks ancestors as
|
||||
# (-brace sum, depth) with -brace increasing toward the top.
|
||||
coords = sorted({0, *sum_b})
|
||||
size = 1
|
||||
while size < len(coords):
|
||||
size *= 2
|
||||
neg_c: list[list[int]] = [[] for _ in range(2 * size)]
|
||||
depth_of: list[list[int]] = [[] for _ in range(2 * size)]
|
||||
used = [0] * (2 * size)
|
||||
|
||||
def push(bracket: int, brace: int, depth: int) -> list[tuple[int, int, int, int, int]]:
|
||||
undo = []
|
||||
node = bisect_left(coords, bracket) + size
|
||||
while node:
|
||||
stack_c, stack_d, top = neg_c[node], depth_of[node], used[node]
|
||||
# Entries with brace <= this one are shallower: drop them.
|
||||
pos = bisect_left(stack_c, -brace, 0, top)
|
||||
if pos < len(stack_c):
|
||||
undo.append((node, top, pos, stack_c[pos], stack_d[pos]))
|
||||
stack_c[pos], stack_d[pos] = -brace, depth
|
||||
else:
|
||||
undo.append((node, top, pos, 0, -1))
|
||||
stack_c.append(-brace)
|
||||
stack_d.append(depth)
|
||||
used[node] = pos + 1
|
||||
node //= 2
|
||||
return undo
|
||||
|
||||
def pop(undo: list[tuple[int, int, int, int, int]]) -> None:
|
||||
for node, top, pos, old_c, old_d in reversed(undo):
|
||||
if old_d < 0:
|
||||
neg_c[node].pop()
|
||||
depth_of[node].pop()
|
||||
else:
|
||||
neg_c[node][pos], depth_of[node][pos] = old_c, old_d
|
||||
used[node] = top
|
||||
|
||||
def deepest(bracket: int, brace: int) -> int:
|
||||
# Deepest ancestor with bracket sum >= bracket and brace sum >= brace.
|
||||
best = -1
|
||||
lo, hi = bisect_left(coords, bracket) + size, 2 * size
|
||||
while lo < hi:
|
||||
for node in ((lo,) if lo & 1 else ()) + ((hi - 1,) if hi & 1 else ()):
|
||||
idx = bisect_right(neg_c[node], -brace, 0, used[node]) - 1
|
||||
if idx >= 0 and depth_of[node][idx] > best:
|
||||
best = depth_of[node][idx]
|
||||
lo = (lo + 1) // 2
|
||||
hi //= 2
|
||||
return best
|
||||
|
||||
ends: dict[int, int | None] = {}
|
||||
path: list[int] = [-1] # path[d]: ancestor at depth d; depth 0 is the root
|
||||
root_undo = push(0, 0, 0)
|
||||
# Iterative DFS: (node, undo record or None when entering).
|
||||
work: list[tuple[int, list[tuple[int, int, int, int, int]] | None]] = [
|
||||
(v, None) for v in roots
|
||||
]
|
||||
while work:
|
||||
v, undo = work.pop()
|
||||
if undo is not None:
|
||||
pop(undo)
|
||||
path.pop()
|
||||
continue
|
||||
depth = len(path)
|
||||
if v in is_query:
|
||||
z = deepest(sum_b[v], sum_c[v])
|
||||
if z < 0:
|
||||
ends[node_line[v]] = None
|
||||
else:
|
||||
w = path[z + 1] if z + 1 < depth else v
|
||||
ends[node_line[v]] = node_line[w]
|
||||
path.append(v)
|
||||
work.append((v, push(sum_b[v], sum_c[v], depth)))
|
||||
work.extend((child, None) for child in children[v])
|
||||
pop(root_undo)
|
||||
return ends
|
||||
|
||||
|
||||
def _extract_json_block(
|
||||
lines: list[str],
|
||||
start: int,
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
"""Scans over tool output must stay linear on long unclosed shapes.
|
||||
|
||||
The timing bounds are loose (the old code took minutes at these sizes); the
|
||||
oracle comparison checks the banner scan still finds exactly what it did.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
import re
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from headroom.proxy.interceptors.astgrep import (
|
||||
_detect_truncation,
|
||||
_is_plausible_truncation_range,
|
||||
)
|
||||
from headroom.transforms.mixed_content import (
|
||||
ContentType,
|
||||
_extract_json_block,
|
||||
_has_valid_json_block_with_text,
|
||||
_JsonBlockScanner,
|
||||
split_into_sections,
|
||||
)
|
||||
|
||||
_PREVIOUS_BANNER = re.compile(
|
||||
r"\[\s*truncated\s*:\s*partial\s+view\b"
|
||||
r"[^\[\]]*?"
|
||||
r"showing\s+lines?\s+(?P<start_line>\d+)\s*[-–]\s*(?P<end_line>\d+)"
|
||||
r"\s+of\s+(?P<total_lines>\d+)\s+total"
|
||||
r"[^\[\]]*\]",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
def _previous_detect(source: str) -> tuple[int, int] | None:
|
||||
count = len(source.splitlines())
|
||||
for m in _PREVIOUS_BANNER.finditer(source):
|
||||
s, e, t = (int(m.group(g)) for g in ("start_line", "end_line", "total_lines"))
|
||||
if _is_plausible_truncation_range(s, e, t, count):
|
||||
return e, t
|
||||
return None
|
||||
|
||||
|
||||
def test_truncation_banner_still_recognised() -> None:
|
||||
src = (
|
||||
"x\n" * 50 + "[Truncated: PARTIAL view -- a.py: showing lines 1-40 of 90 total (x). Read.]"
|
||||
)
|
||||
assert _detect_truncation(src) == (40, 90)
|
||||
|
||||
|
||||
def test_truncation_banner_matches_previous_regex() -> None:
|
||||
rng = random.Random(1234)
|
||||
parts = [
|
||||
"[truncated: partial view", "[ Truncated : PARTIAL view", "]", "[", "\n",
|
||||
" showing lines 1-10 of 50 total ", "showing line 3–4 of 9 total", "showing lines 5-5 of 5 total",
|
||||
"x", " ", "path.py:", "showing lines", "total", "12", "(more)",
|
||||
] # fmt: skip
|
||||
for _ in range(3000):
|
||||
src = "".join(rng.choice(parts) for _ in range(rng.randint(1, 25)))
|
||||
assert _detect_truncation(src) == _previous_detect(src), repr(src)
|
||||
|
||||
|
||||
def test_truncation_scan_is_linear_on_unclosed_banner() -> None:
|
||||
src = "[truncated: partial view " + "showing lines 1-10 of 50 total " * 12000
|
||||
start = time.perf_counter()
|
||||
assert _detect_truncation(src) is None
|
||||
assert time.perf_counter() - start < 5.0
|
||||
|
||||
|
||||
def test_json_block_scans_are_bounded_on_unclosed_braces() -> None:
|
||||
content = "\n".join(["{"] * 20000)
|
||||
start = time.perf_counter()
|
||||
assert _has_valid_json_block_with_text(content) is False
|
||||
sections = split_into_sections(content)
|
||||
assert time.perf_counter() - start < 10.0
|
||||
assert "\n".join(s.content for s in sections).count("{") == 20000
|
||||
|
||||
|
||||
def test_long_json_block_inside_text_still_found() -> None:
|
||||
body = ",\n".join(f' {{"k": {i}}}' for i in range(5000))
|
||||
content = "log line\n[\n" + body + "\n]\ntrailing"
|
||||
assert _has_valid_json_block_with_text(content) is True
|
||||
types = [s.content_type for s in split_into_sections(content)]
|
||||
assert ContentType.JSON_ARRAY in types
|
||||
|
||||
|
||||
@pytest.mark.parametrize("unclosed", [150, 20000])
|
||||
def test_json_object_after_many_unclosed_braces_keeps_its_own_section(unclosed: int) -> None:
|
||||
obj = '{"critical": "disk full"}'
|
||||
content = "\n".join(["{"] * unclosed + ["some prose"] * 6 + [obj, "more prose"])
|
||||
assert _has_valid_json_block_with_text(content) is True
|
||||
json_sections = [
|
||||
s for s in split_into_sections(content) if s.content_type is ContentType.JSON_ARRAY
|
||||
]
|
||||
assert [s.content for s in json_sections] == [obj]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("repeats", [1, 130])
|
||||
def test_json_object_after_crossed_count_prefix_keeps_its_own_section(repeats: int) -> None:
|
||||
# Each count reaches zero on its own but never on the same line, so no
|
||||
# early rejection applies; the object after it must still be found.
|
||||
prefix = (["{"] * 150 + ["[" + "}" * 150]) * repeats
|
||||
obj = '{"critical": "disk full"}'
|
||||
content = "\n".join(prefix + ["some prose"] * 6 + [obj, "more prose"])
|
||||
start = time.perf_counter()
|
||||
assert _has_valid_json_block_with_text(content) is True
|
||||
json_sections = [
|
||||
s for s in split_into_sections(content) if s.content_type is ContentType.JSON_ARRAY
|
||||
]
|
||||
assert time.perf_counter() - start < 10.0
|
||||
assert [s.content for s in json_sections] == [obj]
|
||||
|
||||
|
||||
def test_block_scanner_matches_walking_each_candidate() -> None:
|
||||
rng = random.Random(7)
|
||||
parts = ["{", "}", "[", "]", '"', "\\\\", "a", " ", ",", "{}", "[]", '"x"', "}}", "]]", "{["]
|
||||
parts += ['{"' + chr(92), '"' + chr(92)] # lines ending inside a string, escaped
|
||||
for _ in range(3000):
|
||||
lines = [
|
||||
"".join(rng.choice(parts) for _ in range(rng.randint(0, 5)))
|
||||
for _ in range(rng.randint(1, 25))
|
||||
]
|
||||
scanner = _JsonBlockScanner(lines)
|
||||
scanner._direct_steps_left = 0 # answer every candidate from the sweep
|
||||
for i, line in enumerate(lines):
|
||||
if line.strip().startswith(("[", "{")):
|
||||
assert scanner.extract(i) == _extract_json_block(lines, i), (lines, i)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("pairs", [100, 10000])
|
||||
def test_json_object_after_escaped_string_lines_keeps_its_own_section(pairs: int) -> None:
|
||||
# Lines ending inside a string with a trailing backslash put each walk in
|
||||
# a different string state, so candidates start walks that only merge
|
||||
# later; every one must still be answered.
|
||||
obj = '{"critical": "disk full"}'
|
||||
prefix_line = '{"' + chr(92)
|
||||
content = "\n".join(
|
||||
["{"] + [prefix_line, '"'] * pairs + ["some prose"] * 6 + [obj, "more prose"]
|
||||
)
|
||||
start = time.perf_counter()
|
||||
assert _has_valid_json_block_with_text(content) is True
|
||||
json_sections = [
|
||||
s.content for s in split_into_sections(content) if s.content_type is ContentType.JSON_ARRAY
|
||||
]
|
||||
assert time.perf_counter() - start < 10.0
|
||||
assert json_sections == [obj]
|
||||
Reference in New Issue
Block a user