Skip to content

Commit 011a3ed

Browse files
committed
Merge remote-tracking branch 'origin/refactor/unify-adapter-jsonl-parsing' into refactor/unify-adapter-jsonl-parsing
2 parents d8cbfcc + 6eb56ca commit 011a3ed

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

  • rust/crates/ccusage/src/adapter/openclaw

‎rust/crates/ccusage/src/adapter/openclaw/parser.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ struct OpenClawLine {
3333
model: Option<String>,
3434
#[serde(default, deserialize_with = "jsonl::non_empty_string")]
3535
provider: Option<String>,
36+
// A non-object `message` previously navigated to no usage without dropping
37+
// the line, so deserialize it leniently. Otherwise a malformed `message` on
38+
// a model-tracking line would fail the whole record and lose the
39+
// model/provider state update for subsequent usage entries.
40+
#[serde(default, deserialize_with = "jsonl::lenient_object")]
3641
message: Option<OpenClawMessage>,
3742
timestamp: Option<Value>,
3843
}
@@ -381,8 +386,33 @@ pub(super) fn entry_id(entry: &LoadedEntry) -> String {
381386

382387
#[cfg(test)]
383388
mod tests {
389+
use ccusage_test_support::fs_fixture;
390+
384391
use super::*;
385392

393+
#[test]
394+
fn malformed_message_does_not_drop_model_tracking_line() {
395+
// A `model_change` line whose `message` field is a non-object must still
396+
// deserialize and update the tracked model/provider, instead of being
397+
// dropped by `records().ok()` and losing state for later usage entries.
398+
let fixture = fs_fixture!({
399+
"session.jsonl": concat!(
400+
r#"{"type":"model_change","modelId":"gpt-5.2","provider":"openai","message":"not-an-object"}"#,
401+
"\n",
402+
r#"{"type":"message","message":{"role":"assistant","usage":{"input":10,"output":20}}}"#,
403+
"\n",
404+
),
405+
});
406+
let file = fixture.path("session.jsonl");
407+
408+
let entries = parse_session_file(&file, None, CostMode::Auto, None).unwrap();
409+
410+
assert_eq!(entries.len(), 1);
411+
assert_eq!(entries[0].model.as_deref(), Some("[openclaw] gpt-5.2"));
412+
assert_eq!(entries[0].data.version.as_deref(), Some("openai"));
413+
assert_eq!(entries[0].data.message.usage.input_tokens, 10);
414+
}
415+
386416
#[test]
387417
fn falls_back_to_total_tokens_when_openclaw_parts_are_missing() {
388418
let record = serde_json::from_value::<OpenClawLine>(serde_json::json!({

0 commit comments

Comments
 (0)