Skip to content

feat(zcode): add ZCode usage adapter - #1614

Closed
aseelye wants to merge 4 commits into
ccusage:mainfrom
aseelye:feat/zcode-adapter
Closed

feat(zcode): add ZCode usage adapter#1614
aseelye wants to merge 4 commits into
ccusage:mainfrom
aseelye:feat/zcode-adapter

Conversation

@aseelye

@aseelye aseelye commented Aug 18, 2026

Copy link
Copy Markdown

Summary

Add ZCode as a first-class ccusage data source for daily, monthly, session, and unified reports.

ZCode stores completed model usage in a local SQLite database. This adapter reads that database in read-only mode, normalizes ZCode's inclusive cache-token accounting into ccusage's additive token buckets, and estimates costs from the shared pricing tables.

What changed

  • add the Rust ZCode adapter and wire it into focused and unified reports
  • discover $ZCODE_HOME/cli/db/db.sqlite (including multiple roots), with ~/.zcode as the default
  • load completed requests, deduplicate rows across roots, and preserve raw model labels
  • support the confirmed legacy session(id, directory) schema without requiring a version column
  • carve cache-read and cache-creation tokens out of ZCode's inclusive input_tokens
  • prefer provider-qualified Z.ai pricing while honoring exact raw-model overrides
  • add CLI/config schema integration, fixtures, snapshots, and user documentation
  • document that --mode display is zero because ZCode records no per-request cost

Validation

  • cargo test -p ccusage-adapter-zcode -p ccusage-cli-parser -p ccusage-config -p ccusage-adapter-all -p ccusage
  • cargo clippy -p ccusage-adapter-zcode --all-targets -- -D warnings
  • pnpm --dir docs build
  • cargo fmt --all -- --check
  • git diff --check

Context

Closes #1595.

The schema/token-accounting investigation and earlier implementation in axisrow/ccusage#1 were useful prior art. This implementation targets ccusage's current per-adapter Rust architecture and adds compatibility and pricing behavior for the schemas and models reported in that thread.

Summary by CodeRabbit

  • New Features

    • Added ZCode as a supported data source.
    • Added daily, monthly, and session usage reports with JSON and formatted output.
    • Added token, cache, pricing, date filtering, and parallel-processing options.
    • Added support for custom ZCode data locations through ZCODE_HOME.
    • Added ZCode configuration options and unified reporting support.
  • Documentation

    • Added comprehensive ZCode setup, reporting, configuration, and troubleshooting guidance.
    • Updated supported-source lists and examples throughout the documentation.

Aaron added 4 commits August 18, 2026 11:41
Reads completed model requests from the ZCode desktop app's SQLite
ledger (~/.zcode/cli/db/db.sqlite, model_usage joined with session)
and exposes the standard daily/monthly/session reports, focused via
`ccusage zcode <report>` and aggregated into the unified reports as
the seventeenth built-in agent.

Token semantics follow the schema's own arithmetic: input_tokens
includes the cache-read slice (computed_total = input + output holds
for every observed row), so the parser carves cache reads out of input
to keep the reported buckets additive and priced at their own rate.
Reasoning is always zero in observed rows and assumed to sit inside
output; the shared total-token fallback catches a future version
moving it outside. ZCode records no per-request cost, so every cost
mode derives from the pricing tables, with model ids lowercased to
match the case-sensitive pricing keys (GLM-5.3 -> glm-5.3).

Discovery honors a non-empty ZCODE_HOME (single root or comma-
separated list, kilo-style) before ~/.zcode, opens the database
read-only so reports work while ZCode runs, and degrades to no
entries with a --debug line when the schema does not match. Fixture
tests pin the current shape: cache carve-out, status filter, missing
db, unexpected schema, cross-root dedupe, and session aggregation
with project paths.
Adds the guide page (data location, ZCODE_HOME, token and cost
semantics, troubleshooting) with VitePress navigation, and registers
ZCode in the supported-agent lists, data-location tables, env-var
table, config namespaces, and README examples.
@github-actions

Copy link
Copy Markdown
Contributor

This PR was auto-closed. Only contributors approved with lgtm can open PRs. Open an issue first.

Maintainers review auto-closed issues and reopen worthwhile ones. Issues that do not meet the quality bar in CONTRIBUTING.md may not be reopened or receive a reply.

If a maintainer replies lgtmi, your future issues will stay open. If a maintainer replies lgtm, your future issues and PRs will stay open.

See CONTRIBUTING.md.

@github-actions github-actions Bot closed this Aug 18, 2026
@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 88979ea2-43b1-40c6-a053-3eabfdc1186f

📥 Commits

Reviewing files that changed from the base of the PR and between 9236678 and 2801bc7.

⛔ Files ignored due to path filters (4)
  • rust/Cargo.lock is excluded by !**/*.lock
  • rust/crates/ccusage-cli-parser/src/snapshots/ccusage_cli_parser__tests__root_help.snap is excluded by !**/*.snap
  • rust/crates/ccusage-cli-parser/src/snapshots/ccusage_cli_parser__tests__snapshots_representative_cli_parse_shapes.snap is excluded by !**/*.snap
  • rust/crates/ccusage-config/src/snapshots/ccusage_config__config_schema__tests__snapshots_schema_agent_specific_option_edges.snap is excluded by !**/*.snap
📒 Files selected for processing (38)
  • apps/ccusage/README.md
  • apps/ccusage/config-schema.json
  • docs/.vitepress/config.ts
  • docs/guide/all-reports.md
  • docs/guide/claude/index.md
  • docs/guide/config-files.md
  • docs/guide/configuration.md
  • docs/guide/environment-variables.md
  • docs/guide/getting-started.md
  • docs/guide/index.md
  • docs/guide/zcode/index.md
  • docs/index.md
  • nix/cargo-artifacts.nix
  • rust/Cargo.toml
  • rust/adapters/zcode/Cargo.toml
  • rust/adapters/zcode/README.md
  • rust/adapters/zcode/src/lib.rs
  • rust/adapters/zcode/src/loader.rs
  • rust/adapters/zcode/src/parser.rs
  • rust/adapters/zcode/src/paths.rs
  • rust/adapters/zcode/src/report.rs
  • rust/crates/ccusage-adapter-all/Cargo.toml
  • rust/crates/ccusage-adapter-all/README.md
  • rust/crates/ccusage-adapter-all/src/lib.rs
  • rust/crates/ccusage-adapter-all/src/loader.rs
  • rust/crates/ccusage-adapter-all/src/report.rs
  • rust/crates/ccusage-adapter-all/src/tests.rs
  • rust/crates/ccusage-cli-parser/src/cli-commands.json
  • rust/crates/ccusage-cli-parser/src/parser.rs
  • rust/crates/ccusage-cli-parser/src/tests.rs
  • rust/crates/ccusage-cli/README.md
  • rust/crates/ccusage-cli/src/types.rs
  • rust/crates/ccusage-config/src/config_schema.rs
  • rust/crates/ccusage-core/src/lib.rs
  • rust/crates/ccusage/Cargo.toml
  • rust/crates/ccusage/src/adapter/mod.rs
  • rust/crates/ccusage/src/cli/last_window.rs
  • rust/crates/ccusage/src/main.rs

📝 Walkthrough

Walkthrough

This change adds ZCode as a supported ccusage source. It includes a SQLite adapter, daily/monthly/session CLI commands, configuration schema support, unified reporting integration, pricing and token reporting, environment variables, and documentation.

Changes

ZCode source support

Layer / File(s) Summary
ZCode adapter implementation
rust/adapters/zcode/*
Adds path discovery, SQLite loading, completed-row filtering, token normalization, pricing resolution, deduplication, and daily, weekly, monthly, and session reports with tests.
CLI and configuration contracts
rust/crates/ccusage-cli-parser/*, rust/crates/ccusage-cli/src/types.rs, rust/crates/ccusage-config/*, apps/ccusage/config-schema.json
Adds zcode, daily/monthly/session commands, Command::ZCode, shared configuration options, command-specific configuration, and schema validation.
Adapter registration and dispatch
rust/crates/ccusage-adapter-all/*, rust/crates/ccusage/src/*, rust/crates/ccusage-core/src/lib.rs, rust/Cargo.toml, nix/cargo-artifacts.nix
Registers ZCode in workspace dependencies, built-in source names, unified loading, labels, command dispatch, --last handling, and runner tests.
Documentation and examples
docs/guide/zcode/*, docs/guide/*, docs/index.md, apps/ccusage/README.md
Documents ZCode commands, data discovery, reporting, configuration, environment variables, troubleshooting, and unified-report integration.

Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: ryoppippi

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant CLIParser
  participant CcusageMain
  participant ZcodeLoader
  participant SQLite
  participant ZcodeReport
  User->>CLIParser: run ccusage zcode daily
  CLIParser->>CcusageMain: create Command::ZCode
  CcusageMain->>ZcodeLoader: run with AgentCommandArgs
  ZcodeLoader->>SQLite: read completed model_usage rows
  SQLite-->>ZcodeLoader: return usage rows
  ZcodeLoader->>ZcodeReport: summarize LoadedEntry values
  ZcodeReport-->>User: render JSON, JQ, or usage table
Loading
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@aseelye

aseelye commented Aug 18, 2026

Copy link
Copy Markdown
Author

@coderabbitai review
@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

@coderabbitai review
@cubic-dev-ai review

@aseelye I have started the AI code review. It will take a few minutes to complete.

@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

@aseelye I will review the changes in #1614.

⚠️ Action not completed

Pull request is closed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes This review covers the complete four-commit PR: the new ZCode SQLite adapter, token and pricing normalization, focused and unified report integration, CLI/config/schema wiring, tests, packaging, and documentation.

  • ZCode data loading Reads completed model_usage rows from the configured or default SQLite home, joins session directories, handles legacy session metadata, deduplicates multiple roots, and preserves source model labels.
  • Usage and cost semantics Normalizes cache-inclusive input tokens into additive buckets, preserves total-token fallbacks, applies Z.ai-qualified pricing and exact raw-model overrides, and keeps display mode at zero when the source has no recorded cost.
  • Report and CLI integration Wires daily, monthly, and session focused reports, unified aggregation, command parsing, --last, configuration namespaces, agent detection, and native packaging.
  • Coverage and documentation Adds adapter fixtures and unit coverage for schema handling, token normalization, filtering, deduplication, pricing, and report shapes, along with the generated schema and user-facing source documentation.

Pullfrog  | ⚠️ this action is pinned to a commit SHA, which freezes the cleanup step — switch to @v0 or keep the SHA fresh with Dependabot | View workflow run | Using GPT Luna (free via Pullfrog for OSS) | 𝕏

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 42 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="rust/adapters/zcode/README.md">

<violation number="1" location="rust/adapters/zcode/README.md:56">
P3: report::report_from_rows is listed under "Public surface", but it is only `pub(crate)` in lib.rs, so it is not part of the crate's public API. Correct the docs to match the actual export (only loader::load_entries, report::summarize_entries, and run are public).</violation>
</file>

<file name="rust/crates/ccusage-adapter-all/README.md">

<violation number="1" location="rust/crates/ccusage-adapter-all/README.md:32">
P3: The Depends on list is alphabetized, but `ccusage-adapter-zcode` was inserted between `ccusage-adapter-grok` and `ccusage-adapter-hermes` rather than after `ccusage-adapter-qwen`. Move it to the end to restore the ordering, matching `ccusage-adapter-all/Cargo.toml` where zcode is listed last among adapters.</violation>
</file>

<file name="rust/adapters/zcode/src/paths.rs">

<violation number="1" location="rust/adapters/zcode/src/paths.rs:24">
P2: When an explicitly configured `ZCODE_HOME` root is missing or inaccessible, this filter silently drops it and `paths()` returns no paths, so `--debug` cannot explain missing ZCode data. Pass debug context into discovery and log rejected configured roots, or propagate a discovery failure that the loader can log.</violation>
</file>

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

.filter(|path| !path.is_empty())
{
let path = PathBuf::from(raw);
if path.is_dir() && seen.insert(path.clone()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When an explicitly configured ZCODE_HOME root is missing or inaccessible, this filter silently drops it and paths() returns no paths, so --debug cannot explain missing ZCode data. Pass debug context into discovery and log rejected configured roots, or propagate a discovery failure that the loader can log.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At rust/adapters/zcode/src/paths.rs, line 24:

<comment>When an explicitly configured `ZCODE_HOME` root is missing or inaccessible, this filter silently drops it and `paths()` returns no paths, so `--debug` cannot explain missing ZCode data. Pass debug context into discovery and log rejected configured roots, or propagate a discovery failure that the loader can log.</comment>

<file context>
@@ -0,0 +1,65 @@
+            .filter(|path| !path.is_empty())
+        {
+            let path = PathBuf::from(raw);
+            if path.is_dir() && seen.insert(path.clone()) {
+                paths.push(path);
+            }
</file context>

## Public surface

- `loader::load_entries`
- `report::report_from_rows`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: report::report_from_rows is listed under "Public surface", but it is only pub(crate) in lib.rs, so it is not part of the crate's public API. Correct the docs to match the actual export (only loader::load_entries, report::summarize_entries, and run are public).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At rust/adapters/zcode/README.md, line 56:

<comment>report::report_from_rows is listed under "Public surface", but it is only `pub(crate)` in lib.rs, so it is not part of the crate's public API. Correct the docs to match the actual export (only loader::load_entries, report::summarize_entries, and run are public).</comment>

<file context>
@@ -0,0 +1,70 @@
+## Public surface
+
+- `loader::load_entries`
+- `report::report_from_rows`
+- `report::summarize_entries`
+- `run`
</file context>
Suggested change
- `report::report_from_rows`
- `report::report_from_rows`

- `ccusage-adapter-gemini`
- `ccusage-adapter-goose`
- `ccusage-adapter-grok`
- `ccusage-adapter-zcode`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The Depends on list is alphabetized, but ccusage-adapter-zcode was inserted between ccusage-adapter-grok and ccusage-adapter-hermes rather than after ccusage-adapter-qwen. Move it to the end to restore the ordering, matching ccusage-adapter-all/Cargo.toml where zcode is listed last among adapters.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At rust/crates/ccusage-adapter-all/README.md, line 32:

<comment>The Depends on list is alphabetized, but `ccusage-adapter-zcode` was inserted between `ccusage-adapter-grok` and `ccusage-adapter-hermes` rather than after `ccusage-adapter-qwen`. Move it to the end to restore the ordering, matching `ccusage-adapter-all/Cargo.toml` where zcode is listed last among adapters.</comment>

<file context>
@@ -29,6 +29,7 @@ themselves independent of each other.
 - `ccusage-adapter-gemini`
 - `ccusage-adapter-goose`
 - `ccusage-adapter-grok`
+- `ccusage-adapter-zcode`
 - `ccusage-adapter-hermes`
 - `ccusage-adapter-kilo`
</file context>

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.

Feature request: according to #1382 add zcode support

1 participant