Repository navigation
Commit 72b8ad4
fix(all): count OpenCode Claude Agent turns once in unified reports (#2007)
* fix(opencode): count Claude Agent sessions once in unified reports
OpenCode's claude-agent provider runs Claude Code, which writes its own
transcript, so unified reports counted every Claude Agent turn twice.
OpenCode v2 rows record the Claude Code session as
content[].native.sessionID. Unified reports now skip OpenCode rows whose
native session has a Claude transcript on disk. The Claude transcript
holds the same tokens plus the resolved model ID, while OpenCode stores
only the `opus` alias, which ccusage priced as an older Opus.
Skipped sessions still count as covered, so the cumulative session_v2
aggregate cannot bring the usage back. When the transcript is gone, the
OpenCode rows are counted. `ccusage opencode` keeps every row.
Refs #2004
* docs(opencode): explain Claude Agent sessions in unified reports
Refs #2004
* fix(all): skip Claude Agent rows only where Claude counted them
The first fix skipped an OpenCode claude-agent row whenever a Claude
transcript file with the row's native session ID existed on disk. A
file existing is not the same as Claude counting the usage: a
transcript with no usage lines, one holding only subagent usage, an
unreadable one, or one outside the Claude source's date range made
the OpenCode rows disappear while Claude counted nothing, so unified
reports lost usage.
Claude's unified load now reports the (session ID, date) pairs of the
main-thread entries it counts, after its date filter, and sends them
to the OpenCode load over a channel. OpenCode loads in parallel, waits
for that set, and drops a claude-agent row only when its native
session was counted on the row's date in the Claude source's timezone.
A failed Claude load sends nothing, which keeps every row. Sidechain
entries are left out because OpenCode only records main-thread turns.
This also removes the second walk of the Claude projects directories.
OpenCode now returns rows paired with their native session instead of
dropping them while loading. The rows stay message entries during the
load, so the cumulative session_v2 aggregate stays suppressed for
their sessions in every data directory, which the in-loader skip
leaked when the aggregate sat in a second OPENCODE_DATA_DIR root.
The native link is read in the same parse as the row and only for the
claude-agent provider. The match is per session day, not per turn:
OpenCode sets native.messageID to the transcript line uuid, not the
API message id, and one OpenCode step sums several API calls, so rows
do not map one-to-one to Claude entries.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): describe when unified reports skip Claude Agent rows
The docs said OpenCode rows count only when the Claude Code log is
gone. Rows now count unless the Claude source counted the same session
on the same day, so describe that rule and the cases it keeps: a log
with no usage for the day, subagent-only usage, and days outside the
Claude source's date range. The adapter README records why the match
is per session day rather than per turn, and the assumption it rests
on: the Claude transcript holds every main-thread turn OpenCode
records for that session.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* refactor(all): carry Claude Agent links on each entry and share the main-thread rule
The OpenCode loader kept each row's Claude Code session in a side map
keyed by entry id and joined it back after the load, so the link could
drift from its entry and needed extra clones and accumulators. Entries now
travel as (entry, native session) pairs from the database read onward;
legacy JSON and aggregate rows carry no link.
The sidechain filter and (session, date) mapping existed twice, in the
daily and session paths. One helper now owns the main-thread rule and
dedups borrowed pairs before owning them, so a date string is cloned once
per session day instead of once per entry. The daily loader takes the
collector as a function instead of a boolean flag.
The channel comment claimed a failed Claude load keeps every OpenCode
row. It does not: the dropped sender only stops OpenCode from blocking,
and the unified report still fails on the Claude error, as on main.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* test(all): cover Claude Agent dedup across timezones and midnight
Every unified test ran both sources in UTC, so dating an OpenCode row in
the Claude source's timezone was untested. A Tokyo-configured Claude
source must drop the row whose Tokyo date it counted and keep the row
whose Tokyo date it did not, even though both share a UTC date with
Claude usage.
The midnight test pins the known limit of per-session-day matching: a
row is dated by its turn's start, Claude dates each call, so a turn that
starts before midnight and is logged after it is counted by both sources.
Matching on the transcript uuid would not fix this: Claude's message-id
dedup keeps one of several lines sharing an API message, which need not
be the line whose uuid OpenCode recorded, and one OpenCode row sums
several API calls.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): note the limits of Claude Agent dedup
State that unified reports count Claude Agent turns under Claude only
when Claude Code's own log has them, that a turn crossing midnight in the
Claude source's timezone can be counted twice, and that only v2
session_message rows can carry the link, since OpenCode added the
provider after moving to SQLite v2.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* perf(opencode): read Claude Agent links only for claude-agent rows
Every v2 row deserialized its whole content array through
serde_json::Value, because the lenient helpers build a Value per field,
so message text was materialized just to find content[].native.sessionID
on the few rows that carry one. The link is now read in a second, typed
pass that only claude-agent rows take, and derived structs skip every
other field without building it. A malformed part leaves the row
unlinked instead of dropping it.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* fix(all): skip Claude Agent rows of every session Claude counted
Claude dedups a turn repeated across sessions, such as a resumed or
forked transcript, down to one surviving entry, so the counted set held
only the survivor's session. An OpenCode row linked to the other copy's
session was kept and its turn counted twice. The push functions now hand
back the entry they discard, and the loaders keep main-thread copies from
other sessions, so every session of a counted dedup group matches.
Session reports matched per session day, but the Claude session row holds
all of that session's usage, including days outside the Claude window.
They now skip every row whose Claude session the report includes, which
also covers turns that cross midnight; daily-family reports keep matching
per day in the Claude source's timezone.
load_entries is no longer used outside the OpenCode crate, which the hawk
check flagged, so it is crate-private now. The pair type it returned is
exported as LinkedEntry, which the unified loader names.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): qualify how unified reports match Claude Agent rows
Matching is per Claude Code session, and per day in the daily family, so
the docs no longer promise exactly-once counting. They spell out the
session-report rule and the midnight cases: a turn that starts before
midnight can be counted twice, and --until on its start day leaves out
the calls Claude logs after midnight.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* fix(claude): skip Claude Agent rows only for fully counted sessions
Session reports marked a copy's session covered as soon as the session
dedup kept was reported, without checking the copy's own session. Claude
session reports keep or drop whole sessions by last activity, so a
resumed or forked session running past --until was left out while the
session it continues stayed in, and every OpenCode row linked to it,
including its own turns, was dropped with nobody counting them. A
session is now covered only when every session its main-thread usage
lives under is reported. Daily-family reports were not affected, since
their pairs are filtered by the date each usage is counted on.
The daily loader kept each discarded cross-session copy whole, even for
plain `ccusage claude daily`. It now keeps only the survivor index and
the copy's session, and dates the copy on the day its survivor counts,
which is when the turn's usage is reported. Both loaders now take the
sidechain flag from the survivor, so copies and survivors follow one
rule.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* fix(opencode): keep Claude Agent links past malformed content parts
The typed link pass failed as a whole when any content part, its native
value, or sessionID had an unexpected shape, so one bad part unlinked a
row that carried a valid link and its turn was counted twice. Lenient
wrappers now read each part, native value, and sessionID on their own
and skip any other shape through IgnoredAny, still without building a
serde_json::Value.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): explain Claude Agent matching for resumed sessions
Session reports now skip a resumed or forked session's OpenCode rows only
when the session it continues is reported too, so the guide and adapter
README say so.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* 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]>
* fix(opencode): skip non-string Claude Agent session IDs without unlinking
The lenient link pass read sessionID as a String inside an object, so an
object-valued ID failed the deserialize mid-object, which cannot be
recovered, and the whole row lost a valid link from another part. A
string-only lenient type now drains objects and arrays through
IgnoredAny and reads them as absent.
The lenient object, string, and array wrappers move to the shared jsonl
module next to lenient_vec, since they are generic serde helpers rather
than OpenCode parsing.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): state the resumed-session rule and its residual undercount
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): describe both directions of the resumed-session residual
The docs said Claude's dedup often keeps a repeated turn under the parent
and that the only residual error is an undercount. Among equal copies
dedup keeps the first read, files are sorted by path, and transcripts are
named by UUID, so which session keeps the turn is effectively arbitrary.
With a Claude date window that includes one session but not the other, a
repeated turn can be left out or counted twice, depending on where the
kept copy lives and which session OpenCode logged it under.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* fix(claude): match Claude Agent rows by the recorded session ID
OpenCode links a claude-agent row to the session ID Claude Code records
in the transcript, but the session-report coverage set used the
path-derived session ID, which differs for a renamed or copied
transcript, so the linked row was kept and counted twice. Session rows
stay keyed by path, so the reported check still uses it, while the
covered set and copy tracking now use the recorded ID, as the daily
loader already does.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* fix(opencode): tolerate repeated keys in Claude Agent links
Derived structs reject a repeated field, so a content part with two
sessionID or native keys, or a row with two content keys, failed the
whole link pass and unlinked the row. A shared FirstField wrapper now
reads the first usable value of one field, skips the rest and any other
shape through IgnoredAny, and still builds no serde_json::Value. It
replaces LenientObject, which nothing else used.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): tie session-report skips to counted main-thread usage
A session report can include a Claude row with only subagent usage,
which does not cover OpenCode rows, so the guide now says the session
must have counted main-thread usage. It also says which session keeps a
repeated turn only matters when one of them is left out, and links the
unified reports guide.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): state the session-report rule in terms of transcripts
The docs said a session with its own turns is skipped exactly when the
report includes it. A resumed transcript repeats earlier turns under the
original session ID, so one session's kept turns can span several
transcripts, and the session is skipped only when every one of them is
included. When a Claude window includes only some of them, the session's
OpenCode rows are kept and the turns Claude counts in the included
transcript are counted twice.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): state both branches of the session-report coverage rule
The docs merged the two branches of main_thread_sessions into one rule
and dropped the second from the README. A session whose own turns
survive is decided only by the transcripts holding them; only a session
with no surviving turns of its own falls back to the transcripts holding
its kept copies. The docs also said dedup keeps the first copy read,
though it prefers main-thread copies and more tokens first, and they
promised a fork's own turns are never counted twice, which the
split-transcript case contradicts. The left-out and counted-twice
consequences now follow from the stated rule.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): complete the dedup tiebreaks and Claude Agent coverage wording
The README listed only the main-thread and token tiebreaks, though both
loaders then prefer a copy with speed metadata and the daily loader
compares cost first. The guide said a session is skipped where it means
the session's OpenCode rows, the copies-branch sentence missed that
sidechain survivors do not count as a session's own, and the double
count is now stated for both branches of the rule.
Refs #2004
Co-authored-by: igorovh <[email protected]>
* docs(opencode): spell out which turns decide a Claude Agent skip
The double-count clause said "the kept turns a session is judged by",
which was hard to parse. Name them: a session's own surviving
main-thread turns, or its kept copies when it has none.
Refs #2004
Co-authored-by: igorovh <[email protected]>
---------
Co-authored-by: igorovh <[email protected]>
Co-authored-by: igorovh <[email protected]>1 parent 66e1857 commit 72b8ad4
11 files changed
Lines changed: 1618 additions & 111 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
73 | 84 | | |
74 | 85 | | |
75 | 86 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
| 44 | + | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
| |||
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
63 | 62 | | |
64 | 63 | | |
65 | 64 | | |
| |||
72 | 71 | | |
73 | 72 | | |
74 | 73 | | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
79 | 108 | | |
80 | 109 | | |
81 | 110 | | |
82 | | - | |
83 | 111 | | |
84 | 112 | | |
85 | | - | |
| 113 | + | |
86 | 114 | | |
87 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
88 | 119 | | |
89 | 120 | | |
90 | 121 | | |
| |||
98 | 129 | | |
99 | 130 | | |
100 | 131 | | |
| 132 | + | |
101 | 133 | | |
102 | 134 | | |
103 | 135 | | |
104 | 136 | | |
105 | 137 | | |
106 | 138 | | |
107 | 139 | | |
108 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
109 | 147 | | |
110 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
111 | 154 | | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
112 | 159 | | |
113 | 160 | | |
114 | | - | |
| 161 | + | |
115 | 162 | | |
116 | 163 | | |
117 | 164 | | |
| |||
129 | 176 | | |
130 | 177 | | |
131 | 178 | | |
132 | | - | |
| 179 | + | |
133 | 180 | | |
134 | 181 | | |
135 | 182 | | |
| |||
152 | 199 | | |
153 | 200 | | |
154 | 201 | | |
155 | | - | |
| 202 | + | |
156 | 203 | | |
157 | 204 | | |
158 | 205 | | |
| |||
454 | 501 | | |
455 | 502 | | |
456 | 503 | | |
457 | | - | |
| 504 | + | |
458 | 505 | | |
459 | 506 | | |
460 | 507 | | |
| |||
519 | 566 | | |
520 | 567 | | |
521 | 568 | | |
522 | | - | |
| 569 | + | |
523 | 570 | | |
524 | 571 | | |
525 | 572 | | |
| |||
544 | 591 | | |
545 | 592 | | |
546 | 593 | | |
547 | | - | |
548 | | - | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
549 | 599 | | |
550 | 600 | | |
551 | 601 | | |
| |||
569 | 619 | | |
570 | 620 | | |
571 | 621 | | |
| 622 | + | |
572 | 623 | | |
573 | 624 | | |
574 | 625 | | |
| |||
677 | 728 | | |
678 | 729 | | |
679 | 730 | | |
680 | | - | |
681 | | - | |
682 | | - | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
683 | 767 | | |
684 | 768 | | |
685 | 769 | | |
| |||
1066 | 1150 | | |
1067 | 1151 | | |
1068 | 1152 | | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
1069 | 1185 | | |
1070 | 1186 | | |
1071 | 1187 | | |
| |||
0 commit comments