CodeWhale icon indicating copy to clipboard operation
CodeWhale copied to clipboard

bug(session): /clear leaves stale total_tokens, session_cost, and cache history

Open peixl opened this issue 6 months ago • 0 comments

Description

/clear only resets a subset of session-level telemetry, so /cost, /tokens, and /cache continue to display pre-clear values for the same logical session that the user just cleared.

This was first surfaced by Copilot's review of PR #967 (https://github.com/Hmbown/DeepSeek-TUI/pull/967#discussion -- the commands/core.rs L56 / L68 comments) and is reproducible on current main without any of that PR's other changes.

Steps to reproduce

  1. Start a fresh session: cargo run --bin deepseek.
  2. Send one or two prompts that hit cache and produce non-zero cost (any DeepSeek V4 model in thinking mode is fine).
  3. /cost -- note session_cost is non-zero.
  4. /cache -- note the per-turn cache history shows recorded turns with cache hit / miss tokens.
  5. /tokens -- note total_tokens and last prompt cache hit/miss are populated.
  6. Run /clear.
  7. Run /cost, /cache, /tokens again.

Expected behavior

After /clear, all session-level telemetry that conceptually belongs to "this conversation" should also be cleared:

  • session.total_tokens reset to 0
  • session.session_cost (and CNY mirror) reset to 0.0
  • session.last_prompt_cache_hit_tokens / last_prompt_cache_miss_tokens reset to None
  • session.turn_cache_history cleared

This matches the reset semantics already applied to total_conversation_tokens, total_reasoning_tokens, last_prompt_tokens, last_completion_tokens, last_reasoning_tokens, and last_reasoning_replay_tokens inside crates/tui/src/commands/core.rs::clear.

Actual behavior

session.total_tokens, session.session_cost*, the last_prompt_cache_* fields, and the entire turn_cache_history ring are left untouched by clear(), so the next /tokens, /cost, and /cache still show pre-clear data while /cost arithmetic also becomes inconsistent (cost accrued under tokens that have just been zeroed).

Proposed scope (single-invariant PR)

One small PR limited to:

  • crates/tui/src/commands/core.rs -- extend clear() with the five resets listed above (no other behavior change).
  • a single new regression test in crates/tui/src/commands/core.rs (or crates/tui/tests/) that seeds the missing fields, calls clear, and asserts they are all back to defaults.

No localization, no new public API, no telemetry semantic change.

Environment

  • Branch: current Hmbown/DeepSeek-TUI main (c7ed05a).
  • Rust: 1.88+ (stable).

AI-use disclosure / human-owner statement

This issue and its proposed fix were drafted with AI assistance (GitHub Copilot / Claude). The human owner is @peixl, who reviews each change line-by-line, owns the resulting PR, and is responsible for the test plan.

Test plan for the eventual PR

  • cargo test -p deepseek-tui clear_resets_session_telemetry --locked (new test).
  • cargo test -p deepseek-tui --locked.
  • cargo fmt --all -- --check.
  • cargo clippy -p deepseek-tui --all-targets --all-features --locked -- -D warnings.
  • Manual: reproduce the steps above and confirm /cost, /cache, /tokens are all zeroed after /clear.

Refs: PR #967 (closed) review comments on crates/tui/src/commands/core.rs lines 56 and 68.

peixl avatar May 07 '26 08:05 peixl