Python
Tool results: multi-byte output never overshoots the byte cap
Florian van Trielcd51535c6d82
diff --git a/coworker/toolresult.py b/coworker/toolresult.pyindex 28dde426..465c23e3 100644--- a/coworker/toolresult.py+++ b/coworker/toolresult.py@@ -69,6 +69,22 @@ def head_tail(text: str, keep_bytes: int, *, spill_path: Optional[Path], total_breturn head + marker + tail+def _fit(cut, keep: int, overshoot) -> str:+ """`cut(keep)`, shrinking `keep` until `overshoot(result)` is <= 0. One retry by the+ overshoot is not enough: a byte cut drops any character split at the boundary (so+ shrinking by N can save fewer than N bytes) and `head_tail` halves `keep` with floor+ division (so shrinking by 1 can save nothing). Bounded — `head_tail` never keeps less+ than `_MIN_KEEP`, so a budget below that can't be met by shrinking."""+ bounded = cut(keep)+ for _ in range(8):+ over = overshoot(bounded)+ if over <= 0:+ break+ keep -= max(over, 2)+ bounded = cut(keep)+ return bounded++def bound_tool_result(result: Any,*,@@ -116,21 +132,21 @@ def bound_tool_result(budget = max_bytes - others - _MARKER_RESERVEoriginal = out[key]path = spill(f"{step:04d}-{safe_tool}-{_SAFE_NAME.sub('_', key)[:30]}.txt", original)- bounded = head_tail(original, budget, spill_path=path, total_bytes=_nbytes(original))- # JSON escaping (newlines, quotes) grows the serialised size; tighten once.- over = _nbytes(serialize_result({**out, key: bounded})) - max_bytes- if over > 0:- bounded = head_tail(original, budget - over, spill_path=path, total_bytes=_nbytes(original))- out[key] = bounded+ # JSON escaping (newlines, quotes) grows the serialised size; tighten to fit.+ out[key] = _fit(+ lambda keep: head_tail(original, keep, spill_path=path, total_bytes=_nbytes(original)),+ budget,+ lambda b: _nbytes(serialize_result({**out, key: b})) - max_bytes,+ )if _nbytes(serialize_result(out)) <= max_bytes:breakreturn outpath = spill(f"{step:04d}-{safe_tool}.txt", text)- bounded = head_tail(text, max_bytes - _MARKER_RESERVE, spill_path=path, total_bytes=_nbytes(text))- # The marker names the spill file, and a long path can outgrow the reserve; tighten once+ # The marker names the spill file, and a long path can outgrow the reserve; tighten to fit# (OPE-199: the cap was overshot by the length of the path).- over = _nbytes(bounded) - max_bytes- if over > 0:- bounded = head_tail(text, max_bytes - _MARKER_RESERVE - over, spill_path=path, total_bytes=_nbytes(text))- return bounded+ return _fit(+ lambda keep: head_tail(text, keep, spill_path=path, total_bytes=_nbytes(text)),+ max_bytes - _MARKER_RESERVE,+ lambda b: _nbytes(b) - max_bytes,+ )diff --git a/tests/test_tool_result_cap.py b/tests/test_tool_result_cap.pyindex aa93e9f7..d1c3a853 100644--- a/tests/test_tool_result_cap.py+++ b/tests/test_tool_result_cap.py@@ -171,3 +171,15 @@ def test_engine_default_cap_is_ten_thousand_and_zero_disables(tmp_path):assert e1._tool_result_max_bytes == toolresult.DEFAULT_TOOL_RESULT_MAX_BYTES == 10_000e2 = TurnEngine(provider=_Scripted([]), registry=registry, permissions=permissions, model="m", tool_result_max_bytes=0)assert e2._tool_result_max_bytes == 0+++def test_multibyte_cut_fits_at_any_spill_path_length(tmp_path):+ """The marker names the spill file, so the path length decides how far the cut must+ shrink; with multi-byte text one shrink by the overshoot can land a byte over."""+ text = "é☃𝄞" * 20_000+ for n in range(1, 120):+ spill = tmp_path / ("d" * n)+ out = _bounded(text, spill=spill)+ assert len(out.encode()) <= 10_000, n+ result = {"command": "x", "exit_code": 0, "output": text, "truncated": False}+ assert _size(_bounded(result, spill=spill)) <= 10_000, n