Skip to content

Commit b464edc

Browse files
committed
fix(opencode): count only completed v2 compaction rows
Compaction rows were accepted by type alone. Running and failed compactions stayed out only because the existing parser drops rows without a model, and that was incidental. OpenCode's failed compaction still records usage: compaction.ts publishes Compaction.Failed with the cost and tokens of every call already billed, and the CompactionFailed schema keeps those fields while omitting the model. A failed row that gained a model, from another OpenCode version or a provider that records one, would have been counted, even though attributing failed compactions is outside this change. Pass the row kind to the parser and accept a compaction only when its status is "completed". Group the row fields into a SessionMessageRow so the parser signature stays within clippy's argument limit.
1 parent 2585e04 commit b464edc

2 files changed

Lines changed: 89 additions & 20 deletions

File tree

‎rust/adapters/opencode/src/loader.rs‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use jiff::tz::TimeZone as JiffTimeZone;
99

1010
use super::{
1111
parser::{
12-
OpenCodeMessage, OpenCodeSessionAggregate, message_value_to_entry,
13-
session_message_value_to_entry, session_value_to_entry,
12+
OpenCodeMessage, OpenCodeSessionAggregate, SessionMessageKind, SessionMessageRow,
13+
message_value_to_entry, session_message_value_to_entry, session_value_to_entry,
1414
},
1515
paths::paths,
1616
};
@@ -443,12 +443,8 @@ fn load_entries_from_database(
443443
let Ok(message_type) = statement.read::<String, _>(2) else {
444444
continue;
445445
};
446-
// A compaction is a model request of its own, billed apart
447-
// from assistant steps, so its usage counts too.
448-
let is_compaction = match message_type.as_str() {
449-
"assistant" => false,
450-
"compaction" => true,
451-
_ => continue,
446+
let Some(kind) = SessionMessageKind::from_row_type(&message_type) else {
447+
continue;
452448
};
453449
let Ok(data) = statement.read::<String, _>(3) else {
454450
continue;
@@ -472,14 +468,23 @@ fn load_entries_from_database(
472468
continue;
473469
}
474470
let Some(linked) = session_message_value_to_entry(
475-
&data, id, session_id, created, tz, mode, pricing,
471+
SessionMessageRow {
472+
kind,
473+
data: &data,
474+
id,
475+
session_id,
476+
created,
477+
},
478+
tz,
479+
mode,
480+
pricing,
476481
) else {
477482
continue;
478483
};
479484
v2_entries += 1;
480485
// The session counters already include compaction usage,
481486
// so a compaction row alone must not suppress them.
482-
if is_compaction {
487+
if kind == SessionMessageKind::Compaction {
483488
push_unique_entry(
484489
&mut compaction_entries,
485490
&mut seen_message_ids,
@@ -1669,6 +1674,30 @@ mod tests {
16691674
r#"{"status":"failed","reason":"auto","error":{"type":"unknown","message":"x"},"time":{"created":1767312000000},"tokens":{"input":50,"output":0},"cost":0.5}"#,
16701675
true,
16711676
);
1677+
// Only the status keeps these out: each has a model and usage.
1678+
for (id, status) in [
1679+
("msg-compaction-failed-with-model", "failed"),
1680+
("msg-compaction-running-with-usage", "running"),
1681+
("msg-compaction-without-status", ""),
1682+
] {
1683+
let status = if status.is_empty() {
1684+
String::new()
1685+
} else {
1686+
format!(r#""status":"{status}","#)
1687+
};
1688+
insert_db_session_message(
1689+
&db_path,
1690+
id,
1691+
"v2-session",
1692+
"compaction",
1693+
1_767_312_000_004,
1694+
format!(
1695+
r#"{{{status}"reason":"auto","model":{{"id":"gpt-test","providerID":"openai"}},"time":{{"created":1767312000000}},"tokens":{{"input":40,"output":4}},"cost":0.4}}"#
1696+
)
1697+
.as_str(),
1698+
true,
1699+
);
1700+
}
16721701

16731702
let entries = load_entries_from_directory(
16741703
fixture.root(),

‎rust/adapters/opencode/src/parser.rs‎

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ struct OpenCodeV2Message {
113113
time: Option<OpenCodeTime>,
114114
#[serde(default, deserialize_with = "jsonl::lenient_f64")]
115115
cost: Option<f64>,
116+
#[serde(default, deserialize_with = "jsonl::non_empty_string")]
117+
status: Option<String>,
118+
}
119+
120+
#[derive(Clone, Copy, PartialEq, Eq)]
121+
pub(crate) enum SessionMessageKind {
122+
Assistant,
123+
/// A compaction request, billed apart from assistant steps.
124+
Compaction,
125+
}
126+
127+
impl SessionMessageKind {
128+
pub(crate) fn from_row_type(row_type: &str) -> Option<Self> {
129+
match row_type {
130+
"assistant" => Some(Self::Assistant),
131+
"compaction" => Some(Self::Compaction),
132+
_ => None,
133+
}
134+
}
116135
}
117136

118137
struct ContentField;
@@ -312,16 +331,33 @@ fn message_value_to_entry_inner(
312331
})
313332
}
314333

334+
pub(crate) struct SessionMessageRow<'a> {
335+
pub(crate) kind: SessionMessageKind,
336+
pub(crate) data: &'a str,
337+
pub(crate) id: String,
338+
pub(crate) session_id: String,
339+
pub(crate) created: i64,
340+
}
341+
315342
pub(crate) fn session_message_value_to_entry(
316-
data: &str,
317-
id: String,
318-
session_id: String,
319-
created: i64,
343+
row: SessionMessageRow<'_>,
320344
tz: Option<&JiffTimeZone>,
321345
mode: CostMode,
322346
pricing: Option<&PricingMap>,
323347
) -> Option<(LoadedEntry, Option<Arc<str>>)> {
348+
let SessionMessageRow {
349+
kind,
350+
data,
351+
id,
352+
session_id,
353+
created,
354+
} = row;
324355
let value = serde_json::from_str::<OpenCodeV2Message>(data).ok()?;
356+
// Running and failed compactions are out of scope: a failed one records
357+
// the usage it was billed but no model to attribute it to.
358+
if kind == SessionMessageKind::Compaction && value.status.as_deref() != Some("completed") {
359+
return None;
360+
}
325361
let native_session = (value.provider() == Some("claude-agent"))
326362
.then(|| native_session_id(data))
327363
.flatten();
@@ -501,17 +537,21 @@ mod tests {
501537
use serde_json::json;
502538

503539
use super::{
504-
OpenCodeMessage, OpenCodeSessionAggregate, message_value_to_entry,
505-
open_code_model_candidates, session_message_value_to_entry, session_value_to_entry,
540+
OpenCodeMessage, OpenCodeSessionAggregate, SessionMessageKind, SessionMessageRow,
541+
message_value_to_entry, open_code_model_candidates, session_message_value_to_entry,
542+
session_value_to_entry,
506543
};
507544
use crate::{LoadedEntry, PricingMap, cli::CostMode};
508545

509546
fn v2_native_session(data: &str) -> Option<String> {
510547
session_message_value_to_entry(
511-
data,
512-
"msg-v2".to_string(),
513-
"v2-session".to_string(),
514-
1_767_312_000_000,
548+
SessionMessageRow {
549+
kind: SessionMessageKind::Assistant,
550+
data,
551+
id: "msg-v2".to_string(),
552+
session_id: "v2-session".to_string(),
553+
created: 1_767_312_000_000,
554+
},
515555
None,
516556
CostMode::Display,
517557
None,

0 commit comments

Comments
 (0)