PDX-514: fix(mcp): base estimatedTokens on content text only#225
Merged
Conversation
RCA: estimateTokens serialized the whole ToolResult, counting the payload twice (escaped JSON in content[0].text plus the structuredContent object) and inflating the per-call estimate roughly 2x versus what the model actually reads. Fix: add estimateResultTokens summing content[].text and use it in attachMeta and the depth-guard accumulator; update docs/mcp.md formula and note plus tokenMeta tests; bump version to 1.6.2 in package.json and server.json.
Quality Orchestrator🟢 LOW · 🧪 Tests to Run · Running 1 of 57 tests
▶ Run commandnpx vitest run \
unit/mcp/tokenMeta.test.ts⚡ quality-orchestrator · |
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
_meta.estimatedTokensdouble-counting. The estimator serialized the entireToolResult, so the payload was counted twice — once as escaped JSON incontent[0].textand again as thestructuredContentobject — inflating per-call estimates ~2x (e.g. a testrun reported 169,866 vs ~77,873 actual content-text tokens).estimateResultTokens(result)sumscontent[].textonly (the text an LLM client actually consumes) and is now used by bothattachMetaand the depth-guard session accumulator. The genericestimateTokensis retained/exported.1.6.1 → 1.6.2(package.json+server.jsonkept in sync).Jira
https://provartesting.atlassian.net/browse/PDX-514
Test plan
yarn compilepassesestimateResultTokensdescribe block + a regression test proving a tinycontentwith a hugestructuredContentyields a tiny estimate)yarn lintpasses (lint:script-names+lint)mcp-smoke.cjs: 30 PASS; only the two heaviest tools (provar_automation_testrun,provar_automation_config_load) time out, and they do so even at a 90s per-request timeout with no competing load. Smoke spawns the installed global package (sf provar mcp start, currently 1.6.1), not this worktree build, so this change cannot affect those results — the timeouts are pre-existing/environmental (JVM/automation boot), not a regression from this PR.Changes
src/mcp/utils/tokenMeta.ts: addestimateResultTokens; switchattachMetaand the depth-guard accumulator to it.docs/mcp.md: update theestimatedTokensformula toceil(len(content[].text) / 4)and expand the implementation note explaining whystructuredContentis excluded.test/unit/mcp/tokenMeta.test.ts: rebase the estimate test on content text; add astructuredContent-not-counted regression test; add anestimateResultTokensdescribe block.package.json,server.json: version1.6.1 → 1.6.2.Notes
_meta.estimatedTokens(now content-text-based, not whole-result). Ifdocs/provar-mcp-public-docs.md/ the University course reference the estimate basis, they should be updated separately by the Provar team.