Skip to content

fix(antigravity): keep Conversation totals out of the context reading - #587

Merged
BytePioneer-AI merged 1 commit into
BytePioneer-AI:mainfrom
BeiLuoqu:fix/antigravity-context-reading
Oct 24, 2026
Merged

BytePioneer-AI merged 1 commit into
BytePioneer-AI:mainfrom
BeiLuoqu:fix/antigravity-context-reading

Conversation

@BeiLuoqu

@BeiLuoqu BeiLuoqu commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Antigravity 的上下文读数存在两个偏差:每个 Turn 结束时会变成整个会话的累计输入,并且随 Turn 增加越来越大;Turn 进行中如果命中提示词缓存,读数又会明显偏低。

原因有两个:

  1. agy result 事件里的 usage 是这个原生会话所有请求的合计,而 hostUsage 在没有显式上下文字段时会回退使用 input_tokens。实测一个恢复的会话,4 次请求的 input_tokens 依次为 7,259 / 7,725 / 7,939 / 8,499,第 4 个 Turn 的 result.usage.input_tokens 是 31,422,正好是四者之和。于是上下文显示为 31,422,而最近一次请求的提示词只有 8,499。
  2. 步骤 usage 的 input_tokens 不包含缓存读取,缓存部分单独记在 cache_read_tokens。用本机 Antigravity CLI 1.4.6 的原生转录核对:下一次请求的 cache_read_tokens 约等于上一次请求的完整提示词(例如 7,284 + 0 之后紧接着是 2,717 + 7,285)。所以只看 input_tokens 时,命中缓存会让上下文被严重低估(例如显示 2,751,实际约 83,183)。

改动只涉及 Adapter:

  • 步骤 usage 对应一次请求,上下文按 input_tokens + cache_read_tokens 估算;显式的 context_used_tokens / estimated_tokens_used 仍然优先。
  • result usage 是会话合计:只有 agy 明确给出上下文字段时才带上下文;输入、输出等合计照常发布。
  • 能取到 Language Server 返回的精确上下文(GetCascadeTrajectoryGeneratorMetadata)时,仍用它覆盖估算值。本机 agy 1.4.6 上这个请求返回 401 unauthenticated: missing CSRF token,所以实际显示的是步骤估算值。

未包含:在状态栏标注“上下文(估算)”。现有的 HostUsage / ThreadUsageSnapshot 合约没有表示上下文来源的字段,而按边界规则,Renderer 不应按 Harness ID 做分支。如果维护者认为需要这个标注,可以另开 PR 在合约里加一个可选字段。

Related issues

N/A(#177 讨论过各 Adapter 的用量口径,已关闭)。

这是同一批 4 个 Antigravity 修复之一(#585、#586、#587、#588),各 PR 相互独立,可以单独合并;本地已验证 4 个分支一起合并没有冲突,测试通过。

Test plan

  • npx vitest run --config tests/vitest.config.js packages/adapters/antigravity:13 个文件,169 项测试通过。新增 1 项:
    • 场景:步骤 usage 为 2,751 输入 + 80,432 缓存读取,result 为会话合计(输入 120,418)。
    • 结果:所有 session.usage.changed 里的上下文都不等于 120,418,最后一次为 83,183 / 1,048,576;同时合计输入 120,418、输出 1,204 照常发布。
    • 去掉源码改动后这项测试失败,上下文依次为 [2751, 120418]。
  • 现有的模型切换测试(1M / 200k 窗口)不受影响。
  • tsc -b、tsc -p tests/tsconfig.json --noEmit 通过,改动文件的 ESLint 与 Prettier 检查通过。
  • 实机:上面引用的数字来自 Windows 11 + CodexHost 0.13.3 + Antigravity CLI 1.4.6 的真实会话和原生转录。本分支的构建产物没有在 Desktop 中实机运行过。

🤖 Generated with Claude Code

A Turn's result usage sums every request of the native Conversation: on a
resumed Conversation its input_tokens was 31,422 while the latest request's
prompt was 8,499. hostUsage fell back to input_tokens for context, so each
Turn ended by reporting the Conversation's cumulative input as its context,
growing with every Turn until the next step replaced it.

Estimate context only from a step's usage, which describes one request, and
count that request's whole prompt. agy reports the cached part separately as
cache_read_tokens (a follow-up request's cache_read_tokens is about the
previous request's prompt), so input_tokens alone undercounted context
whenever the prompt cache was hit. Result usage still publishes its totals,
but carries context only when agy names it explicitly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 2eedaef and b51e466.

📒 Files selected for processing: 2
  • packages/adapters/antigravity/src/antigravity-adapter.ts
  • packages/adapters/antigravity/test/antigravity-adapter.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout: 3
  • GitHub Check: Check ubuntu-22.04
  • GitHub Check: Check macos-14
  • GitHub Check: Check windows-latest
🧰 Additional context used
📚 Code guidelines: 1
AGENTS.md — auto-discovered
📓 Path-based instructions: 1
Source excerpt: `codexhost` runs external Agent Harnesses as independent Threads inside Codex Desktop, preserving the official shell and native Codex path.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/adapters/antigravity/test/antigravity-adapter.test.ts
  • packages/adapters/antigravity/src/antigravity-adapter.ts
📝 Summary

Summary by CodeRabbit

  • 修复
    • 修正用量统计的范围:请求级上下文用量包含输入和缓存读取量;会话级上下文用量仅在有明确数据时显示,不再以输入量代替。
    • 流式结果分别展示最新请求的用量与整个会话的累计用量。

适配器现在按请求范围或会话范围计算上下文用量。新增测试验证请求级上下文估算与会话累计输入、输出用量的区别。

Changes: 用量范围

Layer / File(s) Summary
按范围计算上下文用量
packages/adapters/antigravity/src/antigravity-adapter.ts, packages/adapters/antigravity/test/antigravity-adapter.test.ts
hostUsage 优先使用 context_used_tokens 或 estimated_tokens_used。请求范围缺少这些字段时,以 input_tokens 加有效的 cache_read_tokens 估算上下文用量;会话范围不使用此回退。step_update 使用请求范围,result 使用会话范围。新增测试验证最终上下文用量为 83_183,累计输入和输出用量分别为 120_418 和 1_204。

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: gy212, bytepioneer-ai

Merge Risk: ⚪ Minimal · up to b51e4

The scoped context estimate and conversation totals have no identified issue that needs resolution before merge.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed 标题准确概括了主要改动:避免将 Conversation 的累计用量作为上下文读数。标题简洁、具体,并与变更内容一致。
Description check Passed 描述明确说明了 Antigravity 用量计算的问题、修复方案、测试结果和限制,与变更内容直接相关。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@BytePioneer-AI
BytePioneer-AI merged commit 967a93e into BytePioneer-AI:main Oct 24, 2026
5 checks passed
zxd-666 added a commit to zxd-666/codex-host that referenced this pull request Oct 29, 2026
codexhost v0.15.0

## What's New

Import native Sessions from a selected Host, answer multi-select questions, and use Grok's native questions. This release also adds Antigravity usage statistics and improves conversation reliability across Claude Code, Pi, Windows, and Codex Desktop.
支持从所选 Host 导入原生 Session、回答多选问题,并接入 Grok 原生提问。本版还新增 Antigravity 用量统计,改进 Claude Code、Pi、Windows 和 Codex Desktop 的会话可靠性。

### Features

- Select a local or remote Host for Session import, keeping import and opening on the selected machine; support macOS Broker-backed imports for Claude Code, Cursor CLI, CodeBuddy, and WorkBuddy (BytePioneer-AI#207)
- Answer external Harness multi-select questions in a shared card, with accompanying single-choice and text fields and drafts retained while switching chats (BytePioneer-AI#625)
- Answer Grok's native single-choice, multi-select, and text questions, with cancellation and native withdrawal support (BytePioneer-AI#625)
- Read Antigravity's native historical request and Token usage, including Sessions created outside codexhost; model and cost information remain unavailable when absent from native records (BytePioneer-AI#588)

### Fixes

- Steer Claude Code continuations within the same Turn without stopping background Subagents, and keep accepted instructions attached to that Turn (BytePioneer-AI#617)
- Release Claude Code Turns after refused Subagent messages, and allow Fork and message-edit recovery after entering a native worktree (BytePioneer-AI#626, BytePioneer-AI#629)
- Improve Pi's large-history RPC performance and ignore late replies to expired read-only queries without faulting the Session (BytePioneer-AI#607)
- Recover stale Windows Mapping Store locks when a PID has been reused by another process (BytePioneer-AI#627)
- Keep Antigravity questions working across Turns, preserve the project working directory, and bound process cleanup waits (BytePioneer-AI#586)
- Keep Antigravity Conversation totals separate from current context usage (BytePioneer-AI#587)
- Disable background self-update by default for Host-started Antigravity processes on Windows and macOS, while retaining explicit updates (BytePioneer-AI#585)
- Explain unavailable ZCode plans or quotas without asking an already signed-in account to log in again (BytePioneer-AI#621)
- Compact Composer controls in narrow layouts and keep native question and approval inputs separate from chat controls (BytePioneer-AI#624, BytePioneer-AI#625)
- Support Codex Desktop 26.1023 native language settings (BytePioneer-AI#636)
- Fix availability checks routed through the Harness Broker

### 新功能

- 会话导入支持选择本地或远程 Host,导入和打开始终使用所选机器;macOS 上通过 Broker 托管的 Claude Code、Cursor CLI、CodeBuddy 和 WorkBuddy 支持导入 (BytePioneer-AI#207)
- 外部 Harness 多选问题使用公共卡片,可与单选、文本字段一起回答,切换聊天时保留未提交草稿 (BytePioneer-AI#625)
- 接入 Grok 原生单选、多选和文本提问,支持取消及原生问题撤回 (BytePioneer-AI#625)
- 从 Antigravity 原生历史记录统计请求和 Token,包括未经 codexhost 创建的 Session;原生记录缺少模型信息时显示为未知模型且不计价 (BytePioneer-AI#588)

### 修复

- Claude Code 后台 Subagent 运行期间支持同一 Turn 内继续插入指令,不必停止后台任务;已接受的指令保持归属当前 Turn (BytePioneer-AI#617)
- Claude Code 子 Agent 消息被拒绝后正确释放 Turn;进入原生 worktree 后仍可 Fork 和进行消息编辑恢复 (BytePioneer-AI#626, BytePioneer-AI#629)
- 改善 Pi 大历史 RPC 处理性能;只读查询超时后的迟到回复不再导致 Session 故障 (BytePioneer-AI#607)
- Windows 上 PID 被其他进程复用时,可以回收残留的 Mapping Store 锁 (BytePioneer-AI#627)
- 修复 Antigravity 多轮提问失效,明确项目工作目录,并限制进程清理等待时间 (BytePioneer-AI#586)
- 避免把 Antigravity 整段 Conversation 的累计用量误当作当前上下文占用 (BytePioneer-AI#587)
- Windows 和 macOS 上由 Host 启动的 Antigravity 进程默认关闭后台自更新,保留显式更新操作 (BytePioneer-AI#585)
- ZCode 已登录但套餐或额度不可用时显示正确原因,不再要求重复登录 (BytePioneer-AI#621)
- 窄布局下压缩 Composer 控件,并避免聊天控件干扰原生提问与审批输入 (BytePioneer-AI#624, BytePioneer-AI#625)
- 适配 Codex Desktop 26.1023 的原生语言设置 (BytePioneer-AI#636)
- 修复通过 Harness Broker 路由的可用性检查

### Breaking Changes

- Language overrides require Codex Desktop's native settings service. Desktops exposing only the legacy locale RPC are no longer supported for language overrides; reading can fall back to the browser language, but changing the override requires the native service (BytePioneer-AI#636)

### 破坏性变更

- 语言覆盖设置需要 Codex Desktop 原生 settings 服务。仅提供旧 locale RPC 的 Desktop 不再支持语言覆盖;读取时可以降级使用浏览器语言,但修改覆盖值需要原生服务 (BytePioneer-AI#636)

## Installation

### npm

```bash
npm install -g @codexhost/cli
codexhost
```

For remote Session import and multi-select questions, update the Desktop installation and the relevant remote Hosts and Brokers together.

### Installer

Download the installer matching your OS and CPU architecture from this release's assets.

### macOS Gatekeeper

After installing on macOS, if codexhost shows "Apple cannot verify this app" when you open it for the first time, run the following command in Terminal and reopen codexhost:

```bash
xattr -dr com.apple.quarantine /Applications/codexhost.app
```

## Contributors

Thanks to everyone who contributed to this release:

- @BeiLuoqu (BytePioneer-AI#585, BytePioneer-AI#586, BytePioneer-AI#587, BytePioneer-AI#588)
- @bwndlct (BytePioneer-AI#617)
- @BytePioneer-AI (BytePioneer-AI#207, BytePioneer-AI#625, BytePioneer-AI#626, BytePioneer-AI#627, BytePioneer-AI#629, BytePioneer-AI#636)
- @cy2s (BytePioneer-AI#624)
- @karlorz (BytePioneer-AI#625)
- @kunkun9527 (BytePioneer-AI#607)
- @SuperGoodGame (BytePioneer-AI#207)
- @ump45nose (BytePioneer-AI#621)

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	package-lock.json
#	package.json
#	packages/renderer-extension/src/renderer-binding-probe.ts
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.

2 participants