Skip to content

Commit 99331a3

Browse files
ryoppippiigorovh
andcommitted
fix(claude): let a session's own counted turns decide its coverage
The round-3 rule left a session uncovered whenever any of its usage lived under an unreported session. Files are read in path order, so a parent's copy of an inherited turn often survives dedup; with a --since that leaves the parent out and keeps the fork, the fork landed in that set, OpenCode kept the fork's own rows, and Claude counted the same turns under the fork's session row, double counting them. A session's own surviving main-thread usage now decides: it is covered exactly when the report includes it. Only a session whose usage all survives under other sessions needs every one of those included. The remaining error is an undercount of a fork's rows for repeated turns when the fork is reported without its parent, which is smaller than double counting its own turns, since OpenCode already drops its own fork copies. Refs #2004 Co-authored-by: igorovh <[email protected]>
1 parent 04844fb commit 99331a3

2 files changed

Lines changed: 72 additions & 16 deletions

File tree

‎rust/adapters/claude/src/lib.rs‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,35 @@ pub struct DedupedEntries {
7575
}
7676

7777
impl DedupedEntries {
78-
/// Sessions whose main-thread usage is all counted when the sessions
79-
/// `is_counted` accepts are reported. A session's copies count under the
80-
/// session dedup kept, so a session is covered only when every session its
81-
/// usage lives under is reported; a tool wrapping Claude Code links its row
82-
/// to the session it ran in, which need not be the one dedup kept.
78+
/// Sessions whose OpenCode-style wrapper rows the Claude source already
79+
/// counts when it reports the sessions `is_counted` accepts. A session with
80+
/// its own surviving main-thread usage is covered exactly when it is
81+
/// reported, because its own turns count only under it. A session whose
82+
/// usage dedup kept entirely under other sessions is covered only when
83+
/// every one of those is reported. A wrapper links its row to the session
84+
/// it ran in, which need not be the one dedup kept.
8385
pub fn main_thread_sessions(&self, is_counted: impl Fn(&str) -> bool) -> HashSet<Arc<str>> {
84-
let (counted, uncounted): (Vec<_>, Vec<_>) = self
86+
let own = self
8587
.entries
8688
.iter()
8789
.filter(|entry| entry.data.is_sidechain != Some(true))
88-
.map(|entry| (&entry.session_id, &entry.session_id))
89-
.chain(self.copies.iter().map(|(survivor, copy)| (survivor, copy)))
90-
.partition(|(survivor, _)| is_counted(survivor));
91-
let uncounted = uncounted
92-
.into_iter()
93-
.map(|(_, session_id)| session_id)
90+
.map(|entry| &entry.session_id)
9491
.collect::<HashSet<_>>();
95-
counted
96-
.into_iter()
97-
.map(|(_, session_id)| session_id)
98-
.filter(|session_id| !uncounted.contains(session_id))
92+
let held_by_uncounted = self
93+
.copies
94+
.iter()
95+
.filter(|(survivor, _)| !is_counted(survivor))
96+
.map(|(_, copy)| copy)
97+
.collect::<HashSet<_>>();
98+
own.iter()
99+
.copied()
100+
.filter(|session_id| is_counted(session_id))
101+
.chain(
102+
self.copies
103+
.iter()
104+
.map(|(_, copy)| copy)
105+
.filter(|copy| !own.contains(copy) && !held_by_uncounted.contains(copy)),
106+
)
99107
.map(Arc::clone)
100108
.collect()
101109
}

‎rust/crates/ccusage-adapter-all/src/tests.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,54 @@ fn unified_reports_keep_claude_agent_rows_of_a_session_the_claude_window_leaves_
13491349
}
13501350
}
13511351

1352+
#[test]
1353+
fn unified_reports_skip_claude_agent_rows_of_a_fork_counted_without_its_parent() {
1354+
let fixture = fs_fixture!({});
1355+
let _ = fixture.write_file(
1356+
"claude/projects/project-a/claude-native-a.jsonl",
1357+
claude_usage_line("claude-native-a", "2099-01-02T00:00:00.000Z", "msg-a", 100),
1358+
);
1359+
let _ = fixture.write_file(
1360+
"claude/projects/project-a/claude-native-b.jsonl",
1361+
[
1362+
claude_usage_line("claude-native-b", "2099-01-02T00:00:00.000Z", "msg-a", 100),
1363+
claude_usage_line("claude-native-b", "2099-01-03T00:00:00.000Z", "msg-own", 7),
1364+
]
1365+
.join("\n"),
1366+
);
1367+
write_opencode_rows(
1368+
&fixture.create_dir_all("opencode").join("opencode.db"),
1369+
&[(
1370+
"m-own",
1371+
"ses-agent-b",
1372+
CLAUDE_AGENT_DAY_TWO,
1373+
claude_agent_row("claude-native-b", CLAUDE_AGENT_DAY_TWO, 7),
1374+
)],
1375+
);
1376+
let _env = claude_opencode_env(&fixture, fixture.path("claude").into_os_string());
1377+
let shared = claude_agent_shared("20990101", "20990106");
1378+
let sources = std::collections::BTreeMap::from([(
1379+
"claude".to_string(),
1380+
SourceArgs {
1381+
shared: SharedArgs {
1382+
since: Some("20990103".to_string()),
1383+
..shared.clone()
1384+
},
1385+
..SourceArgs::default()
1386+
},
1387+
)]);
1388+
1389+
// Dedup keeps the parent's copy of msg-a, and the Claude window leaves the
1390+
// parent out; the fork's own turn still counts under the fork, once.
1391+
for kind in [AgentReportKind::Daily, AgentReportKind::Session] {
1392+
assert_eq!(
1393+
unified_totals(kind, &shared, &sources)["inputTokens"],
1394+
7,
1395+
"{kind:?}"
1396+
);
1397+
}
1398+
}
1399+
13521400
#[test]
13531401
fn multi_section_pi_load_uses_source_options_for_daily_and_session_rows() {
13541402
let fixture = fs_fixture!({

0 commit comments

Comments
 (0)