-
Notifications
You must be signed in to change notification settings - Fork 29
🤖 feat: add session-usage.json for cumulative cost tracking #1069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
0e02b48 to
d2adb51
Compare
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Introduces SessionUsageService that persists per-model usage to
~/.mux/sessions/{workspaceId}/session-usage.json. Costs are accumulated
on stream-end and immune to message deletion/truncation/editing.
- SessionUsageService with recordUsage(), getSessionUsage(), rebuild
- Frontend WorkspaceStore mirrors accumulation for instant UI updates
- CostsTab displays sessionTotal from persisted data
- workspace.getSessionUsage oRPC endpoint
- Copies session-usage.json on workspace fork
- Rebuilds from messages if file missing/corrupted
Now that session-usage.json accumulates costs on every stream-end, historicalUsage in compaction summaries is redundant. The new system makes costs immune to compaction without needing this field. - Remove historicalUsage from MuxMetadata type and schema - Remove historicalUsage calculation from compactionHandler - Remove historicalUsage handling from sessionUsageService rebuild - Remove historicalUsage handling from collectUsageHistory - Remove related tests
073f0c8 to
e843e22
Compare
e843e22 to
503fc83
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
Introduces persistent cumulative cost tracking via
session-usage.json, eliminating O(n) message iteration for cost calculation. Usage is accumulated on stream-end and persisted to disk, making costs immune to message deletion and compaction.Schema
File location:
~/.mux/sessions/{workspaceId}/session-usage.jsonImplementation
Backend (
SessionUsageService):recordUsage()- Called on stream-end, atomically updates the file under workspace file lockgetSessionUsage()- Returns current usage, rebuilding from messages if file is missing/corruptedcopyUsageFile()- Copies usage file on workspace forkFrontend (
WorkspaceStore):sessionUsagemap stores per-workspace usage datausageStoretriggers React re-renders when usage changesoRPC endpoint:
workspace.getSessionUsageDead Code Removal
Removes
historicalUsagefield fromMuxMetadatatype and compaction logic, plus unusedcollectUsageHistory()function and_messagesparameter in compaction handler (~280 lines total). These were redundant sincesession-usage.jsonnow tracks cumulative costs independently of message history.Bug Fix
Fixes a bug where using multiple different models in a session, then compacting, would cause costs to be under-reported if the session-usage.json file needed to be rebuilt (e.g., on upgrade when file is missing). The rebuild logic now reads
historicalUsagefrom legacy compaction summaries and stores it under a"historical"key, ensuring pre-compaction costs are included in session totals.Note for reviewing agents
Accounting for session-usage.json being missing or corrupted is completely out of scope
Future Steps
This JSON-file approach is a good intermediate solution. Eventually, we plan to migrate usage tracking to a SQLite database with per-step granularity (instead of per-stream/message), enabling:
The current design keeps the door open for this migration—the
byModelstructure maps cleanly to database rows, and the rebuild-from-messages fallback demonstrates the data is recoverable from chat history if schema changes are needed.Generated with
mux