Skip to content

fix: stale stats and incorrect token breakdown after Compaction - #564

Open
licat2023 wants to merge 1 commit into
Opencode-DCP:masterfrom
licat2023:fix/stale-stats-after-compaction
Open

fix: stale stats and incorrect token breakdown after Compaction#564
licat2023 wants to merge 1 commit into
Opencode-DCP:masterfrom
licat2023:fix/stale-stats-after-compaction

Conversation

@licat2023

Copy link
Copy Markdown

Closes #563

What this PR does

Fixes two related bugs where DCP statistics become stale after OpenCode's /Compaction runs:

Bug 1: analyzeTokens ignores compaction boundary

  • firstAssistant/lastAssistant search loops now filter compacted messages via isMessageCompacted()
  • This prevents stale pre-compaction token data from inflating breakdown.system and corrupting all category percentages

Bug 2: resetOnCompaction misses state reset

  • Now resets state.stats, state.systemPromptTokens, and state.compressionTiming
  • Prevents stale pruned-token counts from appearing alongside zero tool/message counts

Changes

File Change
lib/commands/context.ts Add !isMessageCompacted(state, msg) to firstAssistant and lastAssistant search
lib/state/utils.ts Reset stats, systemPromptTokens, compressionTiming in resetOnCompaction

Verification

  • TypeScript compilation passes (tsc --noEmit)
  • Full build succeeds (npm run build)
  • Manually tested: /Compaction ? /dcp context now shows correct post-compaction stats
Two related bugs when /Compaction runs:

1. analyzeTokens() did not filter compacted messages when searching
   for firstAssistant/lastAssistant, causing system prompt tokens to
   be calculated from stale pre-compaction messages. This broke the
   token breakdown percentages in /dcp context.

2. resetOnCompaction() did not reset state.stats, systemPromptTokens,
   or compressionTiming, causing pruned-token counts and session
   statistics to show stale pre-compaction values.

Root cause & analysis in #5xx (issue to follow).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant