fix(llm): warn once when file exceeds _FILE_CHAR_CAP in _read_files (#3773)

(cherry picked from commit 5f0ae1f784)
This commit is contained in:
kclamano authored and safishamsi committed 2026-09-30 20:10:34 +01:00
1 parent 03ac9071fd
commit b507207a1a
2 files changed
+62

No files matched your search

+11
View File
@@ -28,6 +28,9 @@ from graphify.file_slice import (
# `_read_files` truncates each file at this many characters before joining into
# the user message. Token estimates use the same cap so packing matches reality.
_FILE_CHAR_CAP = 20_000
# Warn at most once per run when a file exceeds _FILE_CHAR_CAP, so corpora with
# multiple oversized files do not spam stderr (#3773).
_file_truncation_warned = False
# `_read_files` wraps each file in an `<untrusted_source path=... sha256=...>`
# delimiter block (see issue #1210); this is roughly the per-file overhead in
# characters that wrapper adds (open tag + 64-char sha + close tag + newlines).
@@ -630,6 +633,14 @@ def _read_files(units: "list[Path | FileSlice]", root: Path) -> str:
continue
# Whole files are still capped (covers non-splittable large files like
# code); slices are already bounded to the cap, so the cap is a no-op.
if len(content) > _FILE_CHAR_CAP:
global _file_truncation_warned
if not _file_truncation_warned:
_file_truncation_warned = True
print(
f"[graphify] WARNING: file {rel} exceeds {_FILE_CHAR_CAP} characters and was truncated",
file=sys.stderr,
)
parts.append(_wrap_untrusted(rel, content[:_FILE_CHAR_CAP]))
return "\n\n".join(parts)
+51
View File
@@ -0,0 +1,51 @@
"""Tests for file truncation warning in _read_files when exceeding _FILE_CHAR_CAP (#3773)."""
from pathlib import Path
import pytest
from graphify import llm
@pytest.fixture(autouse=True)
def _reset_warn_flag(monkeypatch):
# The warn-once flag is process-global; reset it per test so each test
# starts from an un-warned state.
monkeypatch.setattr(llm, "_file_truncation_warned", False)
def test_read_files_exceeding_char_cap_warns(tmp_path: Path, capsys):
doc = tmp_path / "oversized.txt"
doc.write_text("a" * (llm._FILE_CHAR_CAP + 500), encoding="utf-8")
result = llm._read_files([doc], tmp_path)
assert len(result) > 0
captured = capsys.readouterr()
assert f"[graphify] WARNING: file {doc.name} exceeds {llm._FILE_CHAR_CAP} characters and was truncated" in captured.err
def test_read_files_under_char_cap_does_not_warn(tmp_path: Path, capsys):
doc = tmp_path / "small.txt"
doc.write_text("a" * 1000, encoding="utf-8")
result = llm._read_files([doc], tmp_path)
assert len(result) > 0
captured = capsys.readouterr()
assert captured.err == ""
def test_read_files_warns_once_across_multiple_oversized_files(tmp_path: Path, capsys):
"""A run with multiple files exceeding the cap must produce exactly one warning,
not spam stderr per file."""
docs = []
for i in range(3):
doc = tmp_path / f"large_{i}.txt"
doc.write_text("x" * (llm._FILE_CHAR_CAP + 1000), encoding="utf-8")
docs.append(doc)
result = llm._read_files(docs, tmp_path)
assert len(result) > 0
captured = capsys.readouterr()
assert captured.err.count("was truncated") == 1