perf(diagnosis): O(n) context truncation instead of O(n^2) (#70)#78
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #70.
truncate_for_budgetdropped low-priority context items one at a time, and after every single pop re-serialized the entire (shrinking) blob viajson.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.dumpslist removes precisely its leading", "item separator plus its own serialization, so the running total stays identical tolen(json.dumps(blob)).Why it's safe
len(items) > 1and always pops the last element — which always has a leading separator (never list index 0).json.dumpsserializes any element identically standalone vs nested (noindent, default separators,ensure_ascii=True).Behavior is bit-identical to the old per-pop path; only the cost changes.
Test
test_whole_blob_serialized_at_most_oncemonkeypatchesjson.dumpsand 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.