feat(zcode): port ZCode support and sync version - #79
Conversation
Ported from upstream 640e97b4731e740d9f79580eb9c0e74349f07062. Includes the ZCode usage parsing subset from upstream f7a124df69d877cdfb1620a2c82805ac2ad2aeb1 without the Codex login workflow.
Ported from upstream 38202b3399698d080ea4c56eac3560c494ebf8bb.
Collapses upstream release bump commits 4efba615177e7d22c750265d344113d5d2ede9a8, 4236758661417a00e550e30f36b4385a979421a3, 7c1c7ba36a136c0311e7d39c6828c2b1c9a7f5c1, and 4062d4d721d6ce13dcb7540f7c145903f66c3d73.
WalkthroughPR 新增 ZCode 的会话解析、本地客户端/适配器注册、客户端目录/前端/CLI 暴露、README 与上游文档同步,以及各包版本升级到 4.0.3;同时调整 TUI 使用页的自动刷新和状态保留逻辑。 ChangesZCode 支持与版本同步
Usage 自动刷新
Sequence Diagram(s)sequenceDiagram
participant App
participant fetch_subscription_usage_with_status
participant begin_subscription_usage_fetch
participant usage_rx
App->>fetch_subscription_usage_with_status: fetch_subscription_usage_with_status(preserve_status)
fetch_subscription_usage_with_status->>begin_subscription_usage_fetch: start usage fetch
begin_subscription_usage_fetch-->>App: usage_rx
App->>usage_rx: poll result on tick
usage_rx-->>App: success / failure / disconnect
App->>App: update cache and status_message
Estimated code review effort🎯 4 (Complex) | ⏱️ ~75 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
/juya review |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/tokscale-cli/src/tui/app.rs`:
- Around line 888-894: The in-flight usage fetch handling in
fetch_subscription_usage_with_status currently only sets
usage_fetch_preserve_status when preserve_status is true, so a manual fetch
cannot override an earlier preserved-status fetch. Update this early-return path
to let a non-preserve request clear usage_fetch_preserve_status when
usage_rx.is_some(), so manual u/r fetches can replace auto-refresh behavior and
allow the completion status to display normally.
In `@crates/tokscale-core/src/sessions/zcode.rs`:
- Around line 190-198: The content_chars helper is currently measuring JSON
serialization length instead of the actual message text length, which inflates
token estimates for strings, arrays, and objects. Update content_chars in
zcode.rs to recursively walk serde_json::Value and sum only real text content
(ignoring quotes, keys, and structural characters), and then tighten the related
tests around the fallback estimation to assert the exact expected value rather
than only checking for a positive result.
- Around line 106-107: The parsing logic in `zcode.rs` is normalizing
`entry.model` too early by passing it through `canonicalize_model`, which causes
`UnifiedMessage.model_id` to lose the original transcript value. Update the
`model_id` assignment in the transcript parsing path to preserve the raw model
string, and restrict normalization to display/grouping code only; then adjust
the related assertions in the affected parsing/test sections so they expect the
original `model_id` rather than a lowercased one.
In `@docs/upstream/2026-06-25.md`:
- Line 9: Three fenced code blocks in the markdown document are missing language
identifiers, which will keep triggering markdownlint MD040. Update each affected
fenced block in the document to include a consistent language tag such as text,
and apply the same fix to the other referenced fenced blocks so the markdown
style check passes without noise.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8a09d2c4-e6a3-4085-990b-2a0f29c41237
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (27)
Cargo.tomlREADME.ja.mdREADME.ko.mdREADME.mdREADME.zh-cn.mdcrates/tokscale-cli/src/main.rscrates/tokscale-cli/src/tui/app.rscrates/tokscale-cli/src/tui/data/mod.rscrates/tokscale-core/client-catalog.jsoncrates/tokscale-core/src/adapters/file.rscrates/tokscale-core/src/adapters/mod.rscrates/tokscale-core/src/local_clients.rscrates/tokscale-core/src/message_cache.rscrates/tokscale-core/src/sessions/mod.rscrates/tokscale-core/src/sessions/zcode.rsdocs/upstream/2026-06-25.mdpackages/cli-darwin-arm64/package.jsonpackages/cli-darwin-x64/package.jsonpackages/cli-linux-arm64-gnu/package.jsonpackages/cli-linux-arm64-musl/package.jsonpackages/cli-linux-x64-gnu/package.jsonpackages/cli-linux-x64-musl/package.jsonpackages/cli-win32-arm64-msvc/package.jsonpackages/cli-win32-x64-msvc/package.jsonpackages/cli/package.jsonpackages/frontend/src/lib/clientRegistry.generated.tspackages/tokscale/package.json
There was a problem hiding this comment.
4 issues found across 28 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Keep the preserve-status mode attached to the in-flight usage fetch so auto-refresh cannot hide a manual fetch result. Use an explicit unknown ZCode model fallback, and cover the requested-model carry-forward behavior with a parser test.
Let manual usage refreshes request visible completion status for an in-flight auto-refresh fetch. Preserve raw ZCode model IDs and estimate fallback tokens from text content instead of JSON encoding length. Add language tags to the upstream port manifest code fences.
There was a problem hiding this comment.
1 issue found across 3 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
Re-trigger cubic
Treat boolean and numeric ZCode content as fallback text instead of empty content, so non-string assistant messages still emit estimated usage.
Parse Z.AI prompt_tokens_details.cached_tokens as cache_read and subtract it from prompt_tokens for ordinary input accounting. Track subscription usage and local report auto-refresh clocks independently, including overdue checks when switching tabs.
Compute an effective cache_read value before splitting Z.AI prompt_tokens, then subtract and report that same value so flat cache fields do not double-count prompt tokens.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
crates/tokscale-cli/src/tui/app.rs (1)
713-725: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win断连时也要清空旧的 Usage 数据和缓存。
Line 713 的 worker 断连分支只记录错误,但保留了旧的
subscription_usage和磁盘缓存;下一帧/下次启动可能继续展示过期用量。这里应与空结果/错误结果分支一致清空数据与缓存。建议修复
Err(std::sync::mpsc::TryRecvError::Disconnected) => { let preserve_status = self.usage_fetch_preserve_status; self.usage_fetch_preserve_status = false; self.usage_rx = None; + self.subscription_usage.clear(); + crate::commands::usage::clear_cache(); self.subscription_usage_errors = vec![crate::commands::usage::UsageProviderError { provider: "unknown".to_string(),🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/tokscale-cli/src/tui/app.rs` around lines 713 - 725, The disconnected-worker branch in app.rs only sets an error and status but leaves stale usage state behind. Update the TryRecvError::Disconnected handling in the app state update path to match the empty/error-result branches by clearing the current subscription_usage data and any persisted usage cache, in addition to resetting usage_rx and recording the UsageProviderError. Use the existing usage_fetch_preserve_status flow in app::App to keep the status behavior unchanged while ensuring no outdated usage is shown on the next frame or after restart.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@crates/tokscale-cli/src/tui/app.rs`:
- Around line 713-725: The disconnected-worker branch in app.rs only sets an
error and status but leaves stale usage state behind. Update the
TryRecvError::Disconnected handling in the app state update path to match the
empty/error-result branches by clearing the current subscription_usage data and
any persisted usage cache, in addition to resetting usage_rx and recording the
UsageProviderError. Use the existing usage_fetch_preserve_status flow in
app::App to keep the status behavior unchanged while ensuring no outdated usage
is shown on the next frame or after restart.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 591c72e8-a062-49b8-9cae-918065909989
📒 Files selected for processing (3)
crates/tokscale-cli/src/tui/app.rscrates/tokscale-core/src/sessions/zcode.rsdocs/upstream/2026-06-25.md
✅ Files skipped from review due to trivial changes (1)
- docs/upstream/2026-06-25.md
Summary
Upstream References
Verification
Local Build and RSS Smoke
36ea09f2withcargo build --release -p tokscale-cli; release binary reportstokscale 4.0.3.~/.local/bin/tokscalewith the current-head release binary and verifiedtokscale --versionplustokscale --no-spinner --json --light.tokscale 3.1.3; each value below is the median of 3 runs.--no-spinner --json --light)clientsCLItokscale tuiin a pseudo-tty)No RSS regression was observed in the local smoke measurements. TUI exits with timeout status
124in this measurement because it is a persistent interactive view.Summary by cubic
Adds ZCode local session support and improves Usage tab refresh with independent clocks and status preservation. Also fixes ZCode token accounting and syncs the workspace and
@tokscale/clito 4.0.3.New Features
~/.zcode/projects/**/*.jsonlvia a native parser; added to the catalog, frontend registry, and CLI/TUI (zcode, hotkeyq). Preserves raw model IDs, uses an unknown-model fallback, carries forward a requested model until the assistant reports one, and estimates fallback tokens from text content.Bug Fixes
prompt_tokens_details.cached_tokensis treated ascache_readand subtracted fromprompt_tokens, and flat cache-read fields are merged and clamped toprompt_tokensto avoid double-counting.Written for commit 36ea09f. Summary will update on new commits.
Summary by CodeRabbit
新功能
优化
版本更新