Skip to content

fix(session): keep cost monotonic across compaction#3663

Merged
Sayt-0 merged 4 commits into
mainfrom
fix/session-cost-regression
Jul 15, 2026
Merged

fix(session): keep cost monotonic across compaction#3663
Sayt-0 merged 4 commits into
mainfrom
fix/session-cost-regression

Conversation

@Sayt-0

@Sayt-0 Sayt-0 commented Jul 15, 2026

Copy link
Copy Markdown
Member

Summary

Fix session cost snapshots that can drop after compaction, for example from $49 to $12, when restored sub-session costs are replaced by a live root-only usage event.

  • distinguish restored or embedded sub-session costs from live sub-session entries
  • emit authoritative final usage snapshots for detached background sub-sessions
  • persist compaction summary costs through runtime events, stores, HTTP transport, and SQLite migration 023
  • preserve cost accounting across reload, branch, fork, clone, cancellation, and failed delegation paths

Root cause and behavior

stage before after
restored parent usage total cost, including historical sub-sessions unchanged
next live usage event root OwnCost() replaces the restored total and can display a large drop root cost plus embedded historical sub-session cost
live delegation parent plus child can double-count if TotalCost() is used globally child remains a separate usage entry
SQLite reload after compaction summary item cost is lost summary cost survives reload

Compaction did not remove billed messages. It emitted the next token-usage snapshot, exposing the mismatch between restore-time TotalCost() and live OwnCost() accounting.

Implementation

  • add explicit live sub-session provenance with AddLiveSubSession
  • include only embedded or restored sub-session costs in SessionUsage
  • emit a final non-empty background-agent usage snapshot before live attachment
  • add session_items.cost with append-only migration 023_add_cost_to_session_items
  • carry summary cost through SessionSummaryEvent, PersistenceObserver, Store.AddSummary, and the remote HTTP API
  • reuse the configured model store when pricing compaction summaries

Regression coverage

  • restored session followed by compaction in full and lean TUI usage trackers
  • live delegation without double counting
  • in-memory store hydration provenance
  • clone, branch, and fork semantics
  • background success, error, and cancellation snapshots
  • compaction event cost propagation
  • SQLite message, summary, and embedded sub-session cost round-trip
  • legacy pre-migration summary rows defaulting to zero cost

Validation

  • task build
  • task lint
  • env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy task test
  • go test -race -count=1 ./pkg/session ./pkg/runtime ./pkg/acp ./pkg/tui/components/sidebar ./pkg/leantui/...
  • cancellation and compaction regressions repeated 20 times

The latest full-suite run has one recording-proxy fixture failure in TestExec_Anthropic_AgentsMd (requested interaction not found), reproduced unchanged on origin/main; all other packages pass. A full suite on the same patch before the upstream recording fixture changed passed completely.

Sayt-0 added 4 commits July 15, 2026 17:30
@Sayt-0
Sayt-0 requested a review from a team as a code owner July 15, 2026 15:59
@aheritier aheritier added area/api For features/issues/fixes related to the usage of the cagent API area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Jul 15, 2026
@Sayt-0
Sayt-0 merged commit 298f723 into main Jul 15, 2026
7 checks passed
@Sayt-0
Sayt-0 deleted the fix/session-cost-regression branch July 15, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api For features/issues/fixes related to the usage of the cagent API area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants