Repository navigation
Conversation
🦋 Changeset detectedLatest commit: f2575da The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f2575da6a7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| private requestTokensWithMeasuredPrefix(messages: readonly Message[]): number { | ||
| const context = this.tokenCounting.get(agentContextOfScope(this.agent), 0, messages.length); | ||
| return context.measured > 0 ? context.size : this.requestTokens(messages); |
There was a problem hiding this comment.
Require a provider-measured anchor before skipping request overhead
When a provider omits usage under the default measured+estimated strategy, TurnEnded pins an estimated (measured: false) anchor, but TokenCountingAgentModel.get() still puts that anchor's token count in ContextSize.measured. This condition therefore treats a message-only estimate as a provider-measured prefix and skips requestTokens(), which is the only path adding the current system prompt and tools; compaction-start and tokensBefore counts are underreported for these sessions. Check actual anchor provenance before taking this fast path.
AGENTS.md reference: AGENTS.md:L111-L113
Useful? React with 👍 / 👎.
Requirement or Bug
Resolve #4614
长会话自动压缩时,用户看到的压缩前 token 数可能明显低于实际触发值,从而误以为压缩过早发生。本 PR 让压缩任务和完成记录复用供应商实测前缀,只估算尚未实测的新增消息,使两处计数与自动压缩的触发口径一致。
Bug Reproduction Steps
See linked issue.
Root Cause
自动压缩的触发判断使用 token-counting 服务的“实测前缀 + 新增消息估算”,但压缩开始校验和最终记录都调用
requestTokens(),重新估算系统提示、工具定义和全部历史消息。两条路径对同一份上下文给出了不同结果。这是根本修复:统一压缩前计数的口径,不调整触发阈值,也不隐藏或修正展示数字。
Code Changes
validateCompactionStart:压缩任务和 PreCompact hook 使用上述计数。compactionRound:完成事件、会话记录和遥测中的tokensBefore使用同一计数。tokensAfter的估算与后续实测校正,以及 wire/API 字段结构均不变。核心实现位于
fullCompactionService.ts;回归测试只增加在现有fullCompaction.test.ts,分别覆盖有实测前缀的真实工具结果尾部和无实测锚点的回退路径。Behavior Changes and Affected Users
tokensBefore压缩后的
tokensAfter仍在下一次供应商响应前保持估算,随后按现有机制实测校正。没有修改默认配置、公开字段、旧会话格式或客户端请求。受影响模块与覆盖:
agent-core-v2压缩开始与完成路径:验证实测前缀、工具结果尾部估算,以及两个计数出口的一致性。验证记录
fullCompaction.test.ts:122/122 通过。@moonshot-ai/agent-core-v2typecheck 和 build 通过。pnpm lint:0 error;仓库已有 warning 未改动。git diff --check通过。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.