Skip to content

fix: account for current context growth in token usage - #582

Open
bittylicious wants to merge 1 commit into
Tarquinen:masterfrom
bittylicious:fix/detect-current-context-growth
Open

bittylicious wants to merge 1 commit into
Tarquinen:masterfrom
bittylicious:fix/detect-current-context-growth

Conversation

@bittylicious

Copy link
Copy Markdown

Summary

  • Use the greater of the latest reported assistant token usage and an estimate of the current transformed message payload when deciding context pressure.
  • Keep stale post-compaction reported-token handling intact.
  • Add a regression test for large context growth after the latest assistant token report.

Why

DCP currently decides whether to inject max-context compression nudges from the latest successful assistant response token report. If large tool output or user content is added after that report, the next assembled request can exceed the provider context window before the model ever sees the max-context nudge. Estimating the current transformed messages lets DCP detect that growth before the next request is sent.

Verification

  • node --import tsx --test tests/token-usage.test.ts
  • npm run typecheck
  • npm run format:check -- lib/token-utils.ts tests/token-usage.test.ts
  • npm test
  • npm run build

@bittylicious bittylicious changed the title Account for current context growth in token usage fix: account for current context growth in token usage Jul 6, 2026
@bittylicious
bittylicious marked this pull request as ready for review July 6, 2026 00:35
specializedken pushed a commit to specializedken/opencode-dynamic-context-pruning that referenced this pull request Oct 23, 2026
…arquinen#627 61adf37)

Cherry-picked from upstream PR Tarquinen#627, adapted to keep the tool named
"compress" (the dcp_compress rename is not taken):
- self-heal the compress tool into event.tools when the host drops it
- refuse to overwrite a state file that exists but failed to load;
  unique temp paths for atomic writes
- content-based token usage fallback when no assistant reports tokens,
  merged with Tarquinen#582's max(reported, estimate) and memoized per window, since
  Tarquinen#660 made counting async and it runs on every request

Bun-only tests (V2 setup seam, TUI render) move to tests/bun and run via
scripts/test-bun.mjs with bun or the OpenCode binary (BUN_BE_BUN=1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
specializedken added a commit to specializedken/opencode-dynamic-context-pruning that referenced this pull request Oct 24, 2026
… text

Seen live on OpenCode V2 with Mimo (1.05M context): three `read` results
carried PNG screenshots as data URIs (808k chars). extractCompletedToolOutput
JSON-stringified the V2 content array, so the base64 was tokenized as text:
566,152 phantom tokens (155 after this fix). With Tarquinen#582's max(reported,
estimate) that pushed usage past the 262k min threshold at ~200k real tokens;
DCP kept sending reminders and the model retried compress until the upstream
timed out. Strip uri/url/data from non-text items before counting; the
provider-reported usage still covers the real image cost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant