Validate ids and token counts, add required items, hygiene for 0.2.0 - #1
Merged
Conversation
- Reject duplicate item ids - Reject tokens fields and tokenizer results that are negative, NaN, Infinity or not numbers - Add optional required flag on Item; fit() throws naming the item and the token shortfall when a required item or pair group does not fit, and pair groups must agree on required - Add examples/demo.ts (runs with tsx) and stop ignoring examples/ - Correct the README on Anthropic token counting and document the new validation errors - Add CI workflow, CHANGELOG, homepage, bugs, sideEffects and engines - Bump to 0.2.0 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
JeremySNR
commented
Sep 2, 2026
JeremySNR
left a comment
Owner
Author
There was a problem hiding this comment.
Reviewed the full diff locally. Duplicate id and token count validation both throw with clear messages (verified with the original failing inputs). The required flag is placed by priority and only changes the failure mode, which is the right semantics, and pair groups are required to agree on it. Demo now runs. 42 tests pass locally. CI green. Merging.
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
tokensand tokenizer output must be finite and >= 0 (negative and NaN values previously corruptedtokensUsed).required?: booleanonItem. A required item or pair group that cannot fit throws with the shortfall instead of silently vanishing.requiredand every validation error; corrects the tokenizer section (tiktoken does not count Anthropic tokens).npm run demonow works:examples/demo.tsadded and un-ignored.Test plan
npm test: 42 passed (24 before)npm run buildnpm run demoPublish this before the three companion packages.
🤖 Generated with Claude Code
Note
Medium Risk
Core
fit()behavior changes: new throws for bad inputs and required items that do not fit may break callers that relied on silent exclusion or duplicate ids. Required-item semantics are intentional but callers must set priorities correctly.Overview
0.2.0 tightens
fit()input handling and adds required items, plus release hygiene (CI, changelog, demo).required?: booleanonItemmeans a required item or pair group throws with item id, shortfall, budget, and reserve when it cannot be included—instead of being silently excluded. Placement still follows priority; the flag only changes the failure mode. Pair groups must agree onrequired, likepriority.Validation now rejects duplicate
ids and invalid token counts ontokensor tokenizer output (negative,NaN,Infinity, non-number), which previously could corrupttokensUsed/tokensRemaining.Docs and tooling: README covers
required, validation errors, and corrected Anthropic vs tiktoken guidance;examples/demo.tsandnpm run demo(tsx);examples/un-gitignored; GitHub Actions runs test + build on Node 20; package metadata and version bump to 0.2.0.Reviewed by Cursor Bugbot for commit 33b2bfe. Bugbot is set up for automated code reviews on this repo. Configure here.