fix(offload): invalidate token cache after stripping deleted tool_use blocks#234
Open
akhilesharora wants to merge 1 commit into
Open
fix(offload): invalidate token cache after stripping deleted tool_use blocks#234akhilesharora wants to merge 1 commit into
akhilesharora wants to merge 1 commit into
Conversation
… blocks The two hooks that strip deleted tool_use blocks in place never invalidated the per-message token cache, so the next snapshot returned the stale pre-strip count and could over-delete messages. Mirror the other in-place mutations and call invalidateTokenCache after each strip loop.
Collaborator
|
@akhilesharora Clean fix! Adding invalidateTokenCache() at both splice sites is the right approach — targeted and thorough with regression tests. Nice work 👍 |
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.
Description | 描述
context-token-tracker.tscaches per-message token counts in a WeakMap keyed on message identity plus_offloaded. Two offload hooks strip deletedtool_useblocks out of mixed assistant messages in place withcontent.splice(...)but never callinvalidateTokenCache(msg)and never set_offloaded:before-prompt-build.ts(splice at line 109)llm-input-l3.ts(splice at line 1377)So the next
buildTiktokenContextSnapshotreturns the stale pre-strip count, and since the strip runs just before the guard and emergency snapshots, the inflated total can cross the thresholds and over-delete real messages. Every other in-place mutation already invalidates the cache (l3-helpers.ts:219/257/302,llm-input-l3.ts:1019); these two were the only ones that skipped it. This callsinvalidateTokenCache(msg)after each strip loop, only when a block was actually removed, and adds the import tobefore-prompt-build.ts.Related Issue | 关联 Issue
Closes #233
Change Type | 修改类型
Self-test Checklist | 自测清单
Additional Notes | 其他说明
Reproduced against the real
context-token-tracker.ts: seed the cache, strip a deletedtool_useblock in place, snapshot again.Before:
After (invalidate following the splice): 18, drift 0.
npm test-> 47 passed. No new test added: the strip loops live in non-exported hook closures, so a unit test cannot drive them without a mocking harness the repo does not have. The runtime repro above is the evidence.