Skip to content

Commit 7d75be6

Browse files
ryoppippiigorovh
andcommitted
fix(opencode): load Claude session days without pricing or swallowing errors
`ccusage opencode` loads the Claude source only to learn which session days it counts, so it can skip the `claude-agent` turns Claude already reports. That load inherited the caller's cost mode, so in auto or calculate mode it fetched LiteLLM pricing and priced all Claude history just to read session IDs and dates. Force `CostMode::Display` for this load. It also mapped every Claude load error to an empty session set, so a real I/O or parse failure, or an invalid CLAUDE_CONFIG_DIR, silently kept every `claude-agent` row and double-counted them against `ccusage claude`. Missing Claude directories already load as no usage, so the only errors left are real ones; propagate them, matching the unified report. Co-authored-by: igorovh <[email protected]>
1 parent 120267d commit 7d75be6

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎docs/guide/opencode/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ccusage matches OpenCode rows to Claude usage by Claude Code session, not by ind
8181

8282
Because OpenCode dates a turn by when it started and Claude Code dates each model call, a turn that runs across midnight can be off in the daily family. It is counted twice when Claude has no usage from that session on the day the turn started. With `--until` on that day, the calls Claude logs after midnight are left out, even though OpenCode dates the whole turn on the earlier day.
8383

84-
`ccusage opencode` skips the rows that `ccusage claude` counts, matched by Claude Code session and date in every report, session reports included. This way `ccusage claude` and `ccusage opencode` add up to `ccusage daily`, and tools that read each source separately count these turns once. These turns appear under Claude. Pass `--include-claude-agent` to show every `claude-agent` row in the OpenCode report:
84+
`ccusage opencode` skips the rows that `ccusage claude` counts. Every `ccusage opencode` report, session reports included, matches them by Claude Code session and date, because `ccusage claude` counts only usage dated inside its date range. This way the totals of `ccusage claude` and `ccusage opencode` for the same range add up to the matching unified report, such as `ccusage daily`, and tools that read each source separately count these turns once. These turns appear under Claude. Pass `--include-claude-agent` to show every `claude-agent` row in the OpenCode report:
8585

8686
```bash
8787
ccusage opencode daily --include-claude-agent

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,12 @@ pub(super) fn load_opencode_entries(
867867
}
868868
let claude_shared = SharedArgs {
869869
json: true,
870+
mode: CostMode::Display,
870871
..shared.clone()
871872
};
872873
// Focused Claude reports, sessions included, count only in-window days, so match by
873-
// session day. With no readable Claude source, nothing counts these turns and they stay.
874-
let claude_counted = load_claude_rows(AgentReportKind::Daily, &claude_shared, None)
875-
.map_or(ClaudeCounted::Sessions(HashSet::new()), |(_, counted)| {
876-
counted
877-
});
874+
// session day. Missing Claude directories load as no session days, so those turns stay.
875+
let (_, claude_counted) = load_claude_rows(AgentReportKind::Daily, &claude_shared, None)?;
878876
Ok(without_turns_counted_by_claude(
879877
entries,
880878
&claude_counted,

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,13 @@ fn focused_opencode_session_skips_turns_of_a_claude_session_active_past_until()
10041004
fn focused_opencode_reports_keep_claude_agent_turns_without_a_claude_source() {
10051005
let fixture = fs_fixture!({});
10061006
write_claude_agent_fixture(&fixture);
1007-
let _env = claude_opencode_env(&fixture, fixture.path("missing-claude").into_os_string());
1007+
let _env = isolated_agents_env(
1008+
&fixture,
1009+
[(
1010+
"OPENCODE_DATA_DIR",
1011+
fixture.path("opencode").into_os_string(),
1012+
)],
1013+
);
10081014
let shared = claude_agent_shared("20990101", "20990103");
10091015

10101016
for kind in [AgentReportKind::Daily, AgentReportKind::Session] {
@@ -1016,6 +1022,29 @@ fn focused_opencode_reports_keep_claude_agent_turns_without_a_claude_source() {
10161022
}
10171023
}
10181024

1025+
#[test]
1026+
fn focused_opencode_reports_fail_like_unified_reports_on_claude_errors() {
1027+
let fixture = fs_fixture!({});
1028+
write_claude_agent_fixture(&fixture);
1029+
let _env = claude_opencode_env(&fixture, fixture.path("missing-claude").into_os_string());
1030+
let shared = claude_agent_shared("20990101", "20990103");
1031+
1032+
for kind in [AgentReportKind::Daily, AgentReportKind::Session] {
1033+
assert!(
1034+
loader::load_opencode_entries(&shared, kind, false).is_err(),
1035+
"{kind:?}"
1036+
);
1037+
assert!(
1038+
load_rows(kind, &shared, &NO_SOURCES, WeekDay::Monday).is_err(),
1039+
"{kind:?}"
1040+
);
1041+
assert!(
1042+
loader::load_opencode_entries(&shared, kind, true).is_ok(),
1043+
"{kind:?}"
1044+
);
1045+
}
1046+
}
1047+
10191048
#[test]
10201049
fn unified_reports_keep_claude_agent_rows_whose_transcript_has_no_usage() {
10211050
let fixture = fs_fixture!({

0 commit comments

Comments
 (0)