Skip to content

cline-cli: rollup fallback double-counts a session whose per-message calls were all deduped #894

Closed

Description

@iamtoruk

Follow-up from the adversarial review of #891 (which shipped). Not a blocker: it over-reports and needs an abnormal on-disk layout, but it is a real money-path edge case worth closing.

The bug

In src/providers/cline-cli.ts, the rollup fallback is gated on emitted > 0, but emitted is incremented only for calls that are actually yielded. The seenKeys dedup check runs before the increment:

if (seenKeys.has(deduplicationKey)) continue   // deduped: emitted NOT bumped
seenKeys.add(deduplicationKey)
...
emitted++
yield { ... }
...
if (emitted > 0) return
const rollup = parseMetrics(isRecord(metadata['usage']) ? metadata['usage'] : null)

So if a session has per-message metrics but every one of them is suppressed by the shared dedup, emitted stays 0 and the parser falls through to the metadata.usage rollup, emitting the session's cost a second time under cline-cli:<id>:rollup.

Repro

Two session directories whose internal session_id collides (e.g. a copied session dir), same message ids, the second carrying a metadata.usage rollup:

sessions/aaa/aaa.json + aaa.messages.json   session_id "shared", msg_0 cost 0.01
sessions/bbb/bbb.json + bbb.messages.json   session_id "shared", msg_0 cost 0.01, metadata.usage rollup

reported: 2 calls, cline-cli:shared:msg_0 + cline-cli:shared:rollup, total $0.02
true    : one session, $0.01

Fix

Gate the rollup on whether the session had any per-message metrics at all, set before the dedup check, not on the post-dedup emitted counter:

let hadMetrics = false
...
const metrics = parseMetrics(message['metrics'])
if (!metrics) continue
hadMetrics = true
...
if (seenKeys.has(deduplicationKey)) continue
...
// after the loop
if (hadMetrics) return   // instead of `if (emitted > 0) return`

A session whose messages were all deduped then correctly declines the rollup. Add a test with the duplicate-session_id layout above, mutation-checked against the current emitted-based gate.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions