tokmesh-core 0.1.2

Core library for tokmesh — session parsing, scanning, aggregation, pricing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! OpenCodeReview session parser
//!
//! OpenCodeReview stores sessions as JSONL files under
//! `~/.opencodereview/sessions/<encoded-repo-path>/<session-id>.jsonl`.

use super::utils::{back_anchor_timestamp, file_modified_timestamp_ms, parse_timestamp_value};
use super::UnifiedMessage;
use crate::{pricing, provider_identity, TokenBreakdown};
use serde_json::Value;
use std::collections::HashSet;
use std::io::{BufRead, BufReader};
use std::path::Path;

pub fn parse_opencodereview_file(path: &Path) -> Vec<UnifiedMessage> {
    let file = match std::fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return Vec::new(),
    };

    let session_id = session_id_from_path(path);
    let fallback_timestamp = file_modified_timestamp_ms(path);

    let mut workspace: Option<String> = None;
    let mut messages = Vec::new();
    let mut seen = HashSet::new();

    for (line_index, line) in BufReader::new(file).lines().enumerate() {
        let Ok(line) = line else { continue };

        if !line.contains("llm_response") && !line.contains("session_start") {
            continue;
        }

        let Ok(value) = serde_json::from_str::<Value>(&line) else {
            continue;
        };

        let record_type = value.get("type").and_then(Value::as_str).unwrap_or("");

        if record_type == "session_start" {
            if workspace.is_none() {
                workspace = value.get("cwd").and_then(Value::as_str).map(str::to_string);
            }
            continue;
        }

        if record_type != "llm_response" {
            continue;
        }

        let usage = match value.get("usage") {
            Some(u) => u,
            None => continue,
        };

        let tokens = tokens_from_usage(usage);
        if tokens.total() == 0 {
            continue;
        }

        // `explicit_timestamp` is this record's own recorded `timestamp`
        // field, as opposed to `fallback_timestamp` (a file-mtime fallback
        // used when it's absent or unparseable).
        let explicit_timestamp = value.get("timestamp").and_then(parse_timestamp_value);
        let recorded_timestamp = explicit_timestamp.unwrap_or(fallback_timestamp);

        let model_raw = value
            .get("model")
            .and_then(Value::as_str)
            .unwrap_or("unknown");
        let model_id = pricing::aliases::resolve_alias(model_raw)
            .unwrap_or(model_raw)
            .to_string();

        let provider_id = provider_identity::inferred_provider_from_model(&model_id)
            .map(str::to_string)
            .unwrap_or_else(|| "opencodereview".to_string());

        let duration_ms = value
            .get("duration_ms")
            .and_then(Value::as_i64)
            .filter(|d| *d > 0);

        // The `llm_response` record's `timestamp` is written when the
        // response is logged, i.e. the call's *end*, not its start.
        // `duration_ms` is that call's elapsed time, so sessionize()'s
        // `[timestamp, timestamp + duration_ms]` span would otherwise
        // project forward past the actual completion into phantom idle time.
        // Back-calculate the start anchor the same way used for Copilot's
        // `endTime`-only records.
        //
        // Only do this when `explicit_timestamp` is a real recorded end
        // timestamp: when it's absent, `recorded_timestamp` is
        // `fallback_timestamp` (the file's mtime), not this record's own end
        // time, and subtracting `duration_ms` from it would shift the
        // message into the wrong day rather than anchor it correctly.
        let timestamp = match (explicit_timestamp, duration_ms) {
            (Some(end), Some(duration)) => back_anchor_timestamp(end, duration),
            _ => recorded_timestamp,
        };

        // Timestamped duplicate rows are replayed writes of the same call and
        // retain the historical key. Timestampless rows all share file mtime,
        // so use their stable file line to keep distinct calls distinct.
        let line_discriminator = match explicit_timestamp {
            Some(_) => String::new(),
            None => format!(":line{line_index}"),
        };
        let dedup_key = format!(
            "opencodereview:{session_id}:{recorded_timestamp}:{model_id}:{}:{}:{}:{}{line_discriminator}",
            tokens.input, tokens.output, tokens.cache_read, tokens.cache_write,
        );
        if !seen.insert(dedup_key.clone()) {
            continue;
        }

        let mut msg = UnifiedMessage::new(
            "opencodereview",
            model_id,
            provider_id,
            &session_id,
            timestamp,
            tokens,
            0.0,
        );
        msg.dedup_key = Some(dedup_key);
        msg.duration_ms = duration_ms;

        if let Some(ws) = &workspace {
            if let Some(key) = super::normalize_workspace_key(ws) {
                msg.workspace_label = super::workspace_label_from_key(&key);
                msg.workspace_key = Some(key);
            }
        }

        messages.push(msg);
    }

    messages
}

fn session_id_from_path(path: &Path) -> String {
    path.file_stem()
        .and_then(|s| s.to_str())
        .filter(|s| !s.trim().is_empty())
        .unwrap_or("unknown")
        .to_string()
}

fn tokens_from_usage(usage: &Value) -> TokenBreakdown {
    TokenBreakdown {
        input: number_field(usage, "prompt_tokens"),
        output: number_field(usage, "completion_tokens"),
        cache_read: number_field(usage, "cache_read_tokens"),
        cache_write: number_field(usage, "cache_write_tokens"),
        reasoning: 0,
    }
}

fn number_field(value: &Value, field: &str) -> i64 {
    value
        .get(field)
        .and_then(|v| {
            v.as_i64()
                .or_else(|| v.as_u64().map(|u| i64::try_from(u).unwrap_or(i64::MAX)))
        })
        .unwrap_or(0)
        .max(0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    fn parse_events(content: &str) -> Vec<UnifiedMessage> {
        let dir = TempDir::new().unwrap();
        let repo_dir = dir.path().join("test-repo");
        std::fs::create_dir_all(&repo_dir).unwrap();
        let path = repo_dir.join("test-session-123.jsonl");
        let mut file = std::fs::File::create(&path).unwrap();
        file.write_all(content.as_bytes()).unwrap();
        file.flush().unwrap();
        parse_opencodereview_file(&path)
    }

    fn session_start(cwd: &str) -> String {
        format!(
            r#"{{"type":"session_start","sessionId":"test-session-123","timestamp":"2026-01-15T10:00:00Z","cwd":"{cwd}","model":"claude-sonnet-4-20250514"}}"#
        )
    }

    fn llm_response(
        timestamp: &str,
        model: &str,
        prompt: i64,
        completion: i64,
        cache_read: i64,
        cache_write: i64,
    ) -> String {
        format!(
            r#"{{"type":"llm_response","sessionId":"test-session-123","timestamp":"{timestamp}","model":"{model}","duration_ms":1500,"usage":{{"prompt_tokens":{prompt},"completion_tokens":{completion},"cache_read_tokens":{cache_read},"cache_write_tokens":{cache_write}}}}}"#
        )
    }

    fn llm_response_without_timestamp(
        model: &str,
        duration_ms: i64,
        prompt: i64,
        completion: i64,
    ) -> String {
        format!(
            r#"{{"type":"llm_response","sessionId":"test-session-123","model":"{model}","duration_ms":{duration_ms},"usage":{{"prompt_tokens":{prompt},"completion_tokens":{completion},"cache_read_tokens":0,"cache_write_tokens":0}}}}"#
        )
    }

    #[test]
    fn parses_single_llm_response() {
        let content = format!(
            "{}\n{}\n",
            session_start("/home/user/project"),
            llm_response(
                "2026-01-15T10:00:05Z",
                "claude-sonnet-4-20250514",
                1000,
                200,
                500,
                100
            ),
        );
        let msgs = parse_events(&content);

        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].client, "opencodereview");
        assert_eq!(msgs[0].tokens.input, 1000);
        assert_eq!(msgs[0].tokens.output, 200);
        assert_eq!(msgs[0].tokens.cache_read, 500);
        assert_eq!(msgs[0].tokens.cache_write, 100);
        assert_eq!(msgs[0].tokens.reasoning, 0);
        assert_eq!(msgs[0].duration_ms, Some(1500));
        assert_eq!(msgs[0].session_id, "test-session-123");
        assert!(msgs[0].workspace_key.is_some());
    }

    #[test]
    fn clamps_extreme_unsigned_usage_and_keeps_the_message() {
        let content = format!(
            "{}\n{}\n",
            session_start("/home/user/project"),
            r#"{"type":"llm_response","sessionId":"test-session-123","timestamp":"2026-01-15T10:00:05Z","model":"gpt-4o","duration_ms":1500,"usage":{"prompt_tokens":18446744073709551615,"completion_tokens":9223372036854775807,"cache_read_tokens":-1,"cache_write_tokens":0}}"#,
        );
        let msgs = parse_events(&content);

        assert_eq!(
            msgs.len(),
            1,
            "one extreme bucket must not drop the message"
        );
        assert_eq!(msgs[0].tokens.input, i64::MAX);
        assert_eq!(msgs[0].tokens.output, i64::MAX);
        assert_eq!(msgs[0].tokens.cache_read, 0);
        assert_eq!(msgs[0].tokens.cache_write, 0);
    }

    #[test]
    fn parses_multiple_responses() {
        let content = format!(
            "{}\n{}\n{}\n",
            session_start("/home/user/project"),
            llm_response(
                "2026-01-15T10:00:05Z",
                "claude-sonnet-4-20250514",
                1000,
                200,
                0,
                0
            ),
            llm_response("2026-01-15T10:01:00Z", "gpt-4o", 500, 100, 0, 0),
        );
        let msgs = parse_events(&content);
        assert_eq!(msgs.len(), 2);
    }

    #[test]
    fn deduplicates_identical_records() {
        let resp = llm_response(
            "2026-01-15T10:00:05Z",
            "claude-sonnet-4-20250514",
            1000,
            200,
            0,
            0,
        );
        let content = format!(
            "{}\n{}\n{}\n",
            session_start("/home/user/project"),
            resp,
            resp,
        );
        let msgs = parse_events(&content);
        assert_eq!(msgs.len(), 1, "duplicate records should be collapsed");
    }

    #[test]
    fn timestampless_records_with_identical_usage_stay_distinct() {
        let content = format!(
            "{}\n{}\n{}\n",
            session_start("/home/user/project"),
            llm_response_without_timestamp("gpt-4o", 1200, 100, 50),
            llm_response_without_timestamp("gpt-4o", 3400, 100, 50),
        );
        let msgs = parse_events(&content);

        assert_eq!(msgs.len(), 2);
        assert_ne!(msgs[0].dedup_key, msgs[1].dedup_key);
        assert_eq!(msgs[0].duration_ms, Some(1200));
        assert_eq!(msgs[1].duration_ms, Some(3400));
    }

    #[test]
    fn reparsing_timestampless_records_reproduces_dedup_keys() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test-session-123.jsonl");
        std::fs::write(
            &path,
            format!(
                "{}\n{}\n{}\n",
                session_start("/home/user/project"),
                llm_response_without_timestamp("gpt-4o", 1200, 100, 50),
                llm_response_without_timestamp("gpt-4o", 3400, 100, 50),
            ),
        )
        .unwrap();

        let parse_keys = || {
            parse_opencodereview_file(&path)
                .into_iter()
                .map(|msg| msg.dedup_key)
                .collect::<Vec<_>>()
        };
        let first = parse_keys();
        let second = parse_keys();

        assert_eq!(first.len(), 2);
        assert_eq!(first, second);
    }

    #[test]
    fn skips_zero_token_records() {
        let content = format!(
            "{}\n{}\n",
            session_start("/home/user/project"),
            llm_response(
                "2026-01-15T10:00:05Z",
                "claude-sonnet-4-20250514",
                0,
                0,
                0,
                0
            ),
        );
        let msgs = parse_events(&content);
        assert_eq!(msgs.len(), 0, "zero-token records should be skipped");
    }

    #[test]
    fn works_without_session_start() {
        let content = format!(
            "{}\n",
            llm_response(
                "2026-01-15T10:00:05Z",
                "claude-sonnet-4-20250514",
                1000,
                200,
                0,
                0
            ),
        );
        let msgs = parse_events(&content);
        assert_eq!(msgs.len(), 1);
        assert!(msgs[0].workspace_key.is_none());
    }

    #[test]
    fn session_id_derived_from_filename() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("my-unique-session.jsonl");
        let content = llm_response("2026-01-15T10:00:05Z", "gpt-4o", 100, 50, 0, 0);
        std::fs::write(&path, format!("{content}\n")).unwrap();

        let msgs = parse_opencodereview_file(&path);
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].session_id, "my-unique-session");
    }

    #[test]
    fn test_llm_response_timestamp_is_start_anchored() {
        // An `llm_response` record's
        // `timestamp` is written when the response is logged, i.e. the
        // call's *end*, not its start. `duration_ms` is that call's elapsed
        // time, so sessionize()'s `[timestamp, timestamp + duration_ms]`
        // span would otherwise project forward past the actual completion
        // into phantom idle time. The parser must back-calculate the start
        // anchor instead.
        let content = llm_response("2026-01-15T10:00:05Z", "gpt-4o", 100, 50, 0, 0);
        let msgs = parse_events(&format!("{content}\n"));

        assert_eq!(msgs.len(), 1);
        let expected_end =
            parse_timestamp_value(&Value::String("2026-01-15T10:00:05Z".to_string())).unwrap();
        assert_eq!(
            msgs[0].timestamp,
            expected_end - 1500,
            "timestamp must be back-calculated to the call start (end - duration)"
        );
        assert_eq!(
            msgs[0].duration_ms,
            Some(1500),
            "duration_ms must still span from start to the recorded end timestamp"
        );
    }
}