Skip to content

Better conversation usage tracking#37

Merged
ScriptSmith merged 2 commits into
mainfrom
conversation-tracking
May 25, 2026
Merged

Better conversation usage tracking#37
ScriptSmith merged 2 commits into
mainfrom
conversation-tracking

Conversation

@ScriptSmith

Copy link
Copy Markdown
Member

No description provided.

@greptile-apps

greptile-apps Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces session-scoped tracking of discarded assistant responses — tokens spent on replies later removed by edit-and-rerun or regeneration — and exposes them in a new spentTotal field on TotalUsageResult. The ChatHeader tooltip is updated to separate "context (current)", "edited / regenerated", "title generation", and "total spent" breakdowns.

  • conversationStore gains discardedUsage / discardedResponseCount state, accumulated in deleteMessagesAfter and replaceAssistantMessage, and reset on every conversation switch, clear, or setMessages.
  • useTotalUsage now reads those fields plus titleGenerationUsage from the store and derives spentTotal = grandTotal + discarded + titleGeneration; it returns null only when spentTotal.totalTokens === 0.
  • A new vitest suite covers the happy path, mode-overhead accumulation, zero-usage skipping, and multi-discard accumulation; the Storybook helper is updated to mirror the real computation.

Confidence Score: 5/5

Safe to merge — the discard tally is accumulated correctly, reset on every conversation boundary, and never mutates existing state in place.

The accumulation logic in mergeDiscarded always starts from a fresh emptyUsage copy of the current state, so store updates cannot corrupt the tally. Both entry-points capture the old message reference before reassigning the array slot. Reset paths cover all conversation-change surfaces and test coverage is thorough.

No files require special attention.

Important Files Changed

Filename Overview
ui/src/stores/conversationStore.ts Adds discardedUsage/discardedResponseCount state with correct accumulation in deleteMessagesAfter and replaceAssistantMessage, proper resets, and a refactored useTotalUsage that derives spentTotal from all spend sources.
ui/src/components/ChatHeader/ChatHeader.tsx Tooltip updated to show context, discarded, title-gen, and total-spent breakdowns using the new TotalUsageResult fields; chip now reads spentTotal directly.
ui/src/stores/tests/conversationStore.usage.test.ts New test file covering discard tally for deleteMessagesAfter, replaceAssistantMessage, mode overhead, zero-usage skipping, multi-discard accumulation, and reset on clearMessages/setMessages/setCurrentConversation.
ui/src/components/ChatHeader/ChatHeader.stories.tsx makeUsage helper refactored to accept discarded/titleGeneration params and now mirrors useTotalUsage's spentTotal computation exactly using a proper reduce with an initial value.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[User edits or regenerates] --> B{Which action?}
    B -->|deleteMessagesAfter| C[Slice removed messages]
    B -->|replaceAssistantMessage| D[Capture old message ref]
    C --> E[mergeDiscarded: fold usage + mode overhead]
    D --> E
    E --> F[discardedUsage accumulates\ndiscardedResponseCount++]
    F --> G[Store update: new discardedUsage object]
    H[setMessages / clearMessages\n/ setCurrentConversation] --> I[Reset discardedUsage = emptyUsage\ndiscardedResponseCount = 0]
    G --> J[useTotalUsage re-runs]
    I --> J
    J --> K[grandTotal = total + modeOverhead\nspentTotal = grandTotal + discarded + titleGeneration]
    K --> L{spentTotal.totalTokens == 0?}
    L -->|Yes| M[return null — chip hidden]
    L -->|No| N[TotalUsageResult with spentTotal]
    N --> O[ChatHeader chip shows spentTotal\nTooltip breaks down context / discarded / title / total]
Loading

Reviews (3): Last reviewed commit: "Review fixes" | Re-trigger Greptile

Comment thread ui/src/components/ChatHeader/ChatHeader.stories.tsx
Comment thread ui/src/components/ChatHeader/ChatHeader.stories.tsx Outdated
@ScriptSmith

Copy link
Copy Markdown
Member Author

@greptile-apps

@ScriptSmith ScriptSmith merged commit 1f2307b into main May 25, 2026
21 checks passed
@ScriptSmith ScriptSmith deleted the conversation-tracking branch May 30, 2026 12:34
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.

1 participant