Skip to content

feat(cli): add stats command - #40

Merged
Blankeos merged 7 commits into
Blankeos:mainfrom
yan-ad:feat/stats-command
Sep 2, 2026
Merged

feat(cli): add stats command#40
Blankeos merged 7 commits into
Blankeos:mainfrom
yan-ad:feat/stats-command

Conversation

@yan-ad

@yan-ad yan-ad commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add crabcode stats with an OpenCode-compatible terminal layout
  • support --days, --tools, --models [N], and --project [PROJECT]
  • aggregate persisted sessions, messages, output tokens, cost, model usage, and tool calls
  • add CLI parsing, aggregation, formatting, and rendering tests

Validation

  • cargo fmt
  • focused unit and CLI parser tests
  • cargo check
  • manual crabcode stats output comparison against opencode stats

Notes

Crabcode currently does not persist input/cache token breakdowns or per-message model cost, so those fields remain zero until that data is recorded. Existing output token, session cost, model, and tool-call data are reported from SQLite.

image

@yan-ad
yan-ad marked this pull request as draft August 31, 2026 08:57
@yan-ad
yan-ad marked this pull request as ready for review August 31, 2026 09:34
@Blankeos

Copy link
Copy Markdown
Owner

super nice! thanks!

@Blankeos Blankeos left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Merge confidence: 3/5 (feat/stats-command -> main)

Stats CLI looks good. Blocking issues are on the live Anthropic stream path, plus this branch now needs a rebase.

Rebase first

GitHub reports CONFLICTING vs current main (at least src/aisdk/README.md, compatible.rs, openai.rs, response.rs after #39/#41/#42). Please rebase onto main before anything else.

Blockers

  1. Anthropic message_delta drops stop_reason. Real Anthropic events always include usage. The new branch returns ChunkType::Usage and never calls anthropic_message_delta, so max_tokens / refusal / finish-reason End are lost. Existing tests omit usage, so they still pass.

    Probe with usage + stop_reason=max_tokens got Usage(...) instead of Incomplete. In aisdk/response.rs, Incomplete becomes Failed and aborts the step — so truncated/refusal turns now look like a clean message_stop success.

  2. Anthropic usage double-count. message_start emits partial usage; message_delta emits final. Chat::record_usage saturating-adds, so input/cache/cost can be ~2x the bill.

Non-blocking

  • No DB migration (usage lives in existing messages.parts JSON).
  • Compatible SSE can emit Usage + End together (good).
  • Unconditional stream_options.include_usage: true is fine for Ollama/Groq today, but some strict proxies still 400 — consider gating.
  • ACP persists usage with cost hardcoded 0.0 and pushes extra parts instead of merging.
  • cargo fmt --check, aisdk boundary, cargo check --tests, and focused stats tests passed.

Checklist

  • Rebase onto main
  • Emit Anthropic usage without swallowing stop_reason (or attach usage only to the final event)
  • Tests that include the usage object: max_tokens / end_turn, and no message_start double-count
  • Optional: gate include_usage; ACP cost parity

Fix the Anthropic stop_reason + double-count with those tests → 4/5. Rebase + that → merge-ready from my side.

Thanks for the OpenCode-style layout — the CLI aggregation itself is in good shape.

@yan-ad
yan-ad force-pushed the feat/stats-command branch from 2158611 to b1bce54 Compare September 2, 2026 01:38
@yan-ad

yan-ad commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Resolved the requested blockers in b1bce54 and rebased the branch onto current main.

  • Anthropic message_delta now emits final Usage followed by the original terminal chunk, preserving max_tokens, refusal, and finish reasons.
  • message_start usage is log-only, so partial + final usage is no longer double-counted.
  • Added realistic usage-bearing tests for max_tokens, end_turn, and no message_start emission.

Validation: cargo fmt --check, cargo check --tests, focused Anthropic/OpenAI/compatible/stats tests, and AISDK boundary equivalent all pass. Please re-review when convenient.

@Blankeos

Blankeos commented Sep 2, 2026

Copy link
Copy Markdown
Owner

PR Review — #40 feat(cli): add stats command (follow-up)

Branch: feat/stats-commandmain
PR: #40
HEAD: b1bce54 fix(aisdk): preserve Anthropic stop reasons
Prior score: 3/5

Merge confidence: 4/5

Anthropic blockers from the last review are fixed with tests. Remaining 1 point is a small rebase vs latest main plus non-blocking ACP/include_usage nits.


GitHub comment body

Thanks — b1bce54 addresses both Anthropic blockers.

message_delta now emits Usage then Incomplete/End, and message_start only logs partial usage (not persisted). Tests include the live usage object. Focused tests pass (max_tokens_delta_emits_final_usage_then_incomplete, end_turn_delta_emits_final_usage_then_terminal_reason, message_start_usage_is_not_emitted_or_double_counted).

Please rebase onto current main again. Latest main (4b41a3e #37 completions) conflicts in src/main.rs root_help_lists_completion (help string for completion vs new stats line). GitHub mergeable was MERGEABLE right after your rebase, then main moved.

Non-blocking leftovers: ACP still hardcodes cost 0.0 and pushes extra usage parts; stream_options.include_usage is still unconditional.

No DB migration. Stats CLI still looks good.

Score: 4/5 (was 3/5). Rebase the help test → merge-ready from my side.


What changed since last review

Item Last review Now
Rebase CONFLICTING vs main Rebased; then main moved again (#37) — small conflict in root_help_lists_completion
Anthropic stop_reason swallowed by Usage blocker Fixedanthropic_stream_chunks can emit both
message_start double-count blocker Fixed — start is log-only
Tests with usage payload missing Added
include_usage always on nit unchanged
ACP cost 0.0 / extra parts nit unchanged
DB migration none none

Checks run

Check Result
cargo fmt --all -- --check pass
scripts/check-aisdk-boundary.sh aisdk boundary ok
Anthropic usage tests + stats/CLI/record_usage 10 passed

Confidence bumps

If you do New score
Rebase onto main and fix root_help_lists_completion 4.5/5
Above + ACP merge/pricing parity, or gate include_usage 5/5

Keep the stats CLI from this PR and the real provider usage/cost
persistence from main. Anthropic now merges message_start + message_delta
so input/cache tokens are not dropped, and sessions.total_cost is rolled
up from usage parts.
Format sessions, messages, days, and per-model message counts with
thousands separators. Keep token totals compact (K/M/B) and leave
tool-usage counts ungrouped.
ACP was storing billed tokens with cost 0.0, so stats/history from ACP
sessions looked token-rich and cost-poor. Reuse the same host catalog
estimate as the TUI instead of leaving cost blank.
@Blankeos
Blankeos merged commit 7ea4048 into Blankeos:main Sep 2, 2026
6 checks passed
@Blankeos

Blankeos commented Sep 2, 2026

Copy link
Copy Markdown
Owner

LGTM thanks @yan-ad !

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