Skip to content

perf(diagnosis): O(n) context truncation instead of O(n^2) (#70)#78

Merged
JumpTechCode merged 1 commit into
mainfrom
fix/diagnosis-truncation-quadratic
Jun 16, 2026
Merged

perf(diagnosis): O(n) context truncation instead of O(n^2) (#70)#78
JumpTechCode merged 1 commit into
mainfrom
fix/diagnosis-truncation-quadratic

Conversation

@JumpTechCode

Copy link
Copy Markdown
Collaborator

Summary

Closes #70.

truncate_for_budget dropped low-priority context items one at a time, and after every single pop re-serialized the entire (shrinking) blob via json.dumps(blob) to re-estimate tokens — quadratic on exactly the large-context case truncation exists to handle.

Fix

Track a running character total: serialize the blob once for a baseline, then subtract len(", ") + len(json.dumps(popped)) per pop. This is byte-exact because popping the last element of a default-json.dumps list removes precisely its leading ", " item separator plus its own serialization, so the running total stays identical to len(json.dumps(blob)).

Why it's safe

  • The loop only pops when len(items) > 1 and always pops the last element — which always has a leading separator (never list index 0).
  • Default json.dumps serializes any element identically standalone vs nested (no indent, default separators, ensure_ascii=True).
  • Verified against a brute-force re-dump over 200 randomized trials (zero mismatches in both dropped-stats and final serialized context), including float / unicode / 2→1-boundary cases.

Behavior is bit-identical to the old per-pop path; only the cost changes.

Test

test_whole_blob_serialized_at_most_once monkeypatches json.dumps and asserts the whole blob is serialized ≤1 time regardless of how many items are dropped (fails on old code at ~120×).

492 unit tests pass; ruff + mypy --strict clean.

truncate_for_budget dropped low-priority items one at a time, and after
every single pop re-serialized the entire (shrinking) blob via
json.dumps(blob) to re-estimate tokens — quadratic on exactly the
large-context case truncation exists to handle.

Track a running character total instead: serialize the blob once for a
baseline, then subtract len(", ") + len(json.dumps(popped)) per pop. This
is byte-exact because popping the last element of a default-json.dumps list
removes precisely its leading ", " item separator plus its own
serialization, so the total stays identical to json.dumps(blob). Verified
against a brute-force re-dump over 200 randomized trials (zero mismatches),
including float/unicode/boundary cases.

Behavior is bit-identical to the old per-pop path; only the cost changes.
Adds a regression test asserting the whole blob is serialized at most once
regardless of how many items are dropped.

Closes #70

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@JumpTechCode
JumpTechCode merged commit a63c743 into main Jun 16, 2026
6 checks passed
@JumpTechCode
JumpTechCode deleted the fix/diagnosis-truncation-quadratic branch June 16, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diagnosis: context truncation is O(n^2) — re-serializes the whole blob on every pop

1 participant