Skip to content

fix(session): reset usage stats when forking a session - #15391

Open
markjoin123 wants to merge 2 commits into
Kilo-Org:mainfrom
markjoin123:cursor/fix-fork-usage-stats-87d2
Open

markjoin123 wants to merge 2 commits into
Kilo-Org:mainfrom
markjoin123:cursor/fix-fork-usage-stats-87d2

Conversation

@markjoin123

Copy link
Copy Markdown

Issue

Fixes #15384

Context

Forking a chat copied the parent's assistant turns into the new session, and usage reports treated that copy as new work. Cost stayed the same because fork already zeroed it, but agent runs, tokens, and active time were counted again on every fork, including a fork of a fork.

Implementation

  • Copied assistant messages are marked forked and left out of the agent-usage query (task-child copies included).
  • Copied step-finish rows drop their token counts the same way they already dropped cost, so session and model totals stay with the work that actually ran. kilo stats skips those copied rows when rolling up per-model tokens.
  • The transcript, timestamps, and the message's own token report are kept, so the forked chat still looks finished and context compaction still sees the real prompt size. A later turn in the fork is counted normally.

Screenshots / Video

N/A (no visual change)

How to Test

Manual/local verification

From packages/opencode/:

  • bun test ./test/kilocode/agent-usage/agent-usage.test.ts --timeout 30000: the new test forking a session does not recount copied runs, tokens, or active time failed before the fix (one free-model turn became 3 runs, 3x tokens, 3000ms active time after two forks). After the fix: 11 pass, 0 fail.
  • bun test ./test/kilocode/session-fork-remap.test.ts --timeout 30000: 8 pass, 0 fail
  • bun test ./test/kilocode/stats-subagent-cost.test.ts --timeout 30000: 1 pass, 0 fail
  • bun run typecheck in packages/opencode/ and packages/schema/: pass
  • bun turbo typecheck --filter=!@kilocode/kilo-jetbrains (29/29) and JetBrains typecheck: pass

Reviewer test steps

  1. Run a turn in a session, then fork it (and fork the fork).
  2. Check agent usage / kilo stats: runs, tokens, and active time should match the original work only.
  3. Send a new turn in the fork and confirm it is counted.

Blocked checks and substitute verification

  • None.

This change was made with AI assistance and was reviewed and tested before submission.

Copied assistant turns were counted again as agent runs, tokens, and
active time. Mark that history as prior context and clear copied
step-finish spend so project totals stay with the work that actually ran.
@github-actions github-actions Bot added area:cli CLI, TUI and core engine community Opened by someone outside the Kilo team needs-triage Waiting for a maintainer to look priority:p1 High: fixes and regressions. Set from the title. Change by hand. labels Oct 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli CLI, TUI and core engine community Opened by someone outside the Kilo team needs-triage Waiting for a maintainer to look priority:p1 High: fixes and regressions. Set from the title. Change by hand.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Forking a chat inflates reported agent runs, tokens, and active time

2 participants