fix: disclose mid-line clamp in truncation hint

When a single line exceeds the entire char budget, its tail is
unreachable via offset pagination (offsets are line-granular). Tell
the model so it doesn't assume it saw the full line.
This commit is contained in:
teknium1 2026-07-05 14:03:53 -07:00 committed by Teknium
parent 25f0cecf5e
commit f514132ff3
2 changed files with 27 additions and 0 deletions

View file

@ -275,6 +275,21 @@ class TestCharacterCountGuard(unittest.TestCase):
self.assertNotIn("error", result)
self.assertTrue(result["content"]) # not empty
self.assertEqual(result["next_offset"], 2) # advanced past line 1
# The hint must disclose that the line was clamped mid-line and its
# remainder is unreachable via offset pagination.
self.assertIn("clamped mid-line", result["hint"])
@patch("tools.file_tools._get_file_ops")
@patch("tools.file_tools._get_max_read_chars", return_value=1000)
def test_multiline_truncation_hint_has_no_clamp_note(self, _mock_limit, mock_ops):
"""Ordinary multi-line truncation must NOT carry the clamp note."""
big_content = "\n".join(f"{i}|" + "z" * 98 for i in range(1, 51))
mock_ops.return_value = _make_fake_ops(
content=big_content, total_lines=50, file_size=len(big_content),
)
result = json.loads(read_file_tool("/tmp/manylines.txt", task_id="manylines"))
self.assertTrue(result["truncated"])
self.assertNotIn("clamped mid-line", result["hint"])
@patch("tools.file_tools._get_file_ops")
def test_small_read_not_truncated(self, mock_ops):

View file

@ -1240,6 +1240,12 @@ def read_file_tool(path: str, offset: int = 1, limit: int = 500, task_id: str =
f"{shown_end} of {total_lines}). Use offset={next_offset} "
"to continue."
)
if len(trimmed.split("\n", 1)[0]) >= max_chars:
result_dict["hint"] += (
" Note: the first line alone exceeded the budget and "
"was clamped mid-line; its remainder is not "
"retrievable via offset."
)
if result_dict["content"]:
result_dict["content"] = redact_sensitive_text(result_dict["content"], file_read=True)
return json.dumps(result_dict, ensure_ascii=False)
@ -1365,6 +1371,12 @@ def read_file_tool(path: str, offset: int = 1, limit: int = 500, task_id: str =
f"{lines_kept} line(s) (showing lines {offset}-{shown_end} of "
f"{total_lines}). Use offset={next_offset} to continue."
)
if len(trimmed.split("\n", 1)[0]) >= max_chars:
result_dict["hint"] += (
" Note: the first line alone exceeded the budget and was "
"clamped mid-line; its remainder is not retrievable via "
"offset."
)
content_len = len(trimmed)
# ── Redact secrets (after guard check to skip oversized content) ──