8000 fix(anthropic): preserve terminal streaming usage by ChrAlpha · Pull Request #137 · felinics/twilight · GitHub
[go: up one dir, main page]

Skip to content

fix(anthropic): preserve terminal streaming usage - #137

Open
ChrAlpha wants to merge 1 commit into
felinics:mainfrom
ChrAlpha:fix/anthropic-terminal-usage
Open

ChrAlpha wants to merge 1 commit into
felinics:mainfrom
ChrAlpha:fix/anthropic-terminal-usage

Conversation

@ChrAlpha
@ChrAlpha ChrAlpha commented Oct 28, 2026 •
Copy link
Copy Markdown
Member

Author

Agent

Type

  • bug
  • feat
  • test

Description

Anthropic-compatible relay 可能在 message_start 返回零值或缺失的 input/cache usage,直到末尾 message_delta 才补全。当前流式实现只更新 output_tokens,导致 SDK 的输入与缓存计数一直为 0,下游 UI 的 Cache hit 显示为「—」。

保留原始 usage 状态并按字段合并累计更新,再通过现有 convertUsage 统一归一化。省略字段保留旧值,显式零值覆盖,重复累计快照不会相加;仅含 usage 的 delta 也不会清空已经收到的 stop reason。

FinishStepPart 改为在 message_stop 发出一次,包含完整计数。未完成流保留已观察到的 usage,同时不发出成功的步骤完成事件。

Verification

  • MISE_TRUSTED_CONFIG_PATHS=$PWD mise run ci:build、vet、lint 和完整 short test suite 通过。
  • go test -race ./provider/anthropic/messages ./sdk -count=1 通过。
  • 新增 SSE 回归覆盖末帧补全 usage、仅 output 更新、显式零值、重复/稀疏累计更新与 TTL 字段,以及未完成流。
  • Memoh 使用固定到本 PR 提交的远程 module replacement 完成真实 provider、数据库及 UI 验证。

集成验证版本:Memoh e3901c745,Twilight 20c743e256e2795fa5aa2950cbffd000ece4ca35。环境为本地开发栈 http://127.0.0.1:41682,模型 mimo-v2.6-pro,使用 /v1/messages。

通过真实聊天 UI 创建并逐个读取 8 个文件,刷新页面后续聊复核;独立工作区读取确认每个文件 101 行、首行整数之和为 28。两轮聊天的 7 次流式请求逐次核对,provider 与 PostgreSQL 完全一致:

Counter Provider Persisted
Input including cache 154,964 154,964
Cache read 115,840 115,840
Cache write reported 0 0
Output 752 752

UI 显示 Input 155.0K、Output 752、Cache hit 75%,与 74.7528% 的实际计数比值一致。另通过真实 Memoh Generate、Stream 和消息追加三组场景,42 次工具调用均恰好执行一次,provider usage 与 SDK 聚合值一致。

UI 截图已在本地核验。gh pr edit --attach 因当前账号没有上游仓库写权限而失败,PR 暂缺截图附件。

Human QA

  • Human QA passed

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.

1 participant

0