The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: ranged @file read bails at the char budget mid-line (review follow-up)
teknium16d1341a13b0c
diff --git a/agent/context_references.py b/agent/context_references.pyindex 2e04134d559..5561b0876cc 100644--- a/agent/context_references.py+++ b/agent/context_references.py@@ -307,8 +307,11 @@ def _expand_path_reference(ref: ContextReference, cwd: Path, *, allowed_root: Pachar_budget = None if max_inline_tokens is None else max_inline_tokens * CHARS_PER_TOKENline_cap = None if char_budget is None else char_budget + 1- def _next_line(fh, collect: bool) -> str | None:- pieces, seen = [], False+ def _next_line(fh, collect: bool, remaining: int | None = None) -> str | None:+ """One line ("" when skipping), None at EOF. While collecting, stop as soon as the+ pieces exceed ``remaining``: the caller returns the oversized block and never needs+ the rest of a giant line, so it is never materialized."""+ pieces, seen, collected = [], False, 0while True:piece = fh.readline() if line_cap is None else fh.readline(line_cap)if not piece:@@ -316,6 +319,9 @@ def _expand_path_reference(ref: ContextReference, cwd: Path, *, allowed_root: Paseen = Trueif collect:pieces.append(piece)+ collected += len(piece)+ if remaining is not None and collected > remaining:+ breakif piece.endswith("\n"):breakreturn ("".join(pieces) if collect else "") if seen else None@@ -326,7 +332,7 @@ def _expand_path_reference(ref: ContextReference, cwd: Path, *, allowed_root: Paif _next_line(fh, collect=False) is None:breakfor _ in range((ref.line_end or ref.line_start) - ref.line_start + 1):- line = _next_line(fh, collect=True)+ line = _next_line(fh, collect=True, remaining=None if char_budget is None else char_budget - total_chars)if line is None:breaktotal_chars += len(line)diff --git a/tests/agent/test_context_references.py b/tests/agent/test_context_references.pyindex 1db00cd73a9..30408af8a3f 100644--- a/tests/agent/test_context_references.py+++ b/tests/agent/test_context_references.py@@ -246,7 +246,7 @@ def test_line_range_bounds_reads_on_single_line_file(tmp_path: Path, monkeypatchpayload = tmp_path / "oneline.txt"payload.write_text("x" * 100_000, encoding="utf-8") # one giant line, no newline- read_sizes = []+ read_sizes, returned = [], []orig_open = Path.opendef _counting_open(self, *args, **kwargs):@@ -254,7 +254,14 @@ def test_line_range_bounds_reads_on_single_line_file(tmp_path: Path, monkeypatchmode = args[0] if args else kwargs.get("mode", "r")if "b" not in mode:orig_readline = fh.readline- fh.readline = lambda *a, **k: (read_sizes.append(a[0] if a else -1), orig_readline(*a, **k))[1]++ def _readline(*a, **k):+ read_sizes.append(a[0] if a else -1)+ piece = orig_readline(*a, **k)+ returned.append(len(piece))+ return piece++ fh.readline = _readlinereturn fhmonkeypatch.setattr(Path, "open", _counting_open)@@ -264,6 +271,9 @@ def test_line_range_bounds_reads_on_single_line_file(tmp_path: Path, monkeypatchassert "too large to inline safely" in result.message# hard_limit = 500 tokens -> char budget 2000 -> each readline bounded at 2001assert read_sizes and max(read_sizes) <= 2001+ # ... and the giant line is never materialized: reading stops once the budget is exceeded+ # instead of collecting every 2001-char piece up to the newline (review follow-up).+ assert sum(returned) <= 2 * 2001def test_run_quiet_caps_child_output(tmp_path: Path):