Skip to main content

agent_top_core/harness/
gemini.rs

1//! Gemini CLI: `~/.gemini/tmp/<project>/chats/session-<YYYY-MM-DDTHH-MM>-<id8>.jsonl`.
2//!
3//! Format notes (read from the recorder in `@google/gemini-cli-core` 0.58.0,
4//! `services/chatRecordingService.js`, 2026-09-05; the fixture in
5//! `tests/fixtures/gemini-0.58.jsonl` was written by that recorder, driven
6//! with a scripted conversation, not captured from a live session):
7//! * `<project>` is a slug of the working directory's basename (`agent-top`,
8//!   `agent-top-1` on a clash); older versions used a SHA-256 of the path.
9//!   `<project>/.project_root` holds the working directory the slug stands
10//!   for. `~/.gemini/projects.json` maps paths to slugs too, but the marker
11//!   is enough and is also what the CLI trusts when the two disagree.
12//! * The first line is metadata: `sessionId`, `projectHash` (a SHA-256 of
13//!   the working directory, not the slug), `startTime`, `lastUpdated`,
14//!   `kind` (`main` or `subagent`). `{"$set": {...}}` lines update it;
15//!   `lastUpdated` is rewritten after every message.
16//! * A message is `{"id", "timestamp", "type", "content", ...}`, `type` one of
17//!   `user`, `gemini`, `info`, `error`, `warning`. A `gemini` message carries
18//!   `model`, `tokens` and, once its tool calls have completed, `toolCalls`.
19//!   A message is appended again in full whenever the recorder updates it,
20//!   so the same `id` appears more than once and the latest line wins.
21//! * `tokens` is one API response's usage: `input` (`promptTokenCount`,
22//!   which includes the cached part), `cached`, `output`
23//!   (`candidatesTokenCount`, thoughts excluded), `thoughts`, `tool`
24//!   (`toolUsePromptTokenCount`) and `total`. Google bills thoughts as
25//!   output and tool-use prompt tokens as input, so that is how they are
26//!   folded here; the resulting total equals `total`.
27//! * A tool result is recorded as a `user` message whose content parts carry
28//!   `functionResponse`; a human prompt has `text` parts. Only the part keys
29//!   are looked at, never the text.
30//! * `toolCalls[]` entries have `id`, `name`, `status` (`success`, `error`,
31//!   `cancelled`, ...) and one `timestamp`, stamped when the call completed.
32//!   The span runs from the `gemini` message that issued the call to that
33//!   completion, so a call still running is not visible until it returns.
34//!   `google_web_search` is the built-in web search and is counted as one.
35//! * `{"$rewindTo": "<id>"}` removes that message and everything after it
36//!   from the conversation. The tokens were spent regardless, so nothing is
37//!   subtracted; messages are deduplicated by id so a `$set.messages`
38//!   checkpoint that re-lists them does not double count either.
39//! * A subagent gets its own file at `chats/<parent sessionId>/<id>.jsonl`
40//!   with `kind: "subagent"`. Those are tailed and folded into the parent,
41//!   as Claude Code's are.
42//! * Turns and inferences are reconstructed from line order: a prompt starts
43//!   a turn and an inference, a tool result starts an inference, and each
44//!   `gemini` message ends the inference and moves the end of the turn.
45//!   There is no end-of-turn marker, so a turn is as long as its last reply.
46//! * Legacy `session-*.json` files (one JSON document, rewritten on every
47//!   update) are not read; the CLI converts them on resume.
48//!
49//! Gemini CLI does not hold the file open between writes and publishes no
50//! registry of its processes, so a process is matched to its conversation by
51//! working directory and start time, and the row says so.
52
53use super::{
54    AttributeContext, HarnessAdapter, REFRESH_BUDGET_BYTES, SessionSummary, SessionTracker, SpanLog, SpanRetention, parse_rfc3339_utc,
55};
56use crate::jsonl::TailReader;
57use crate::model::{Activity, Attribution, CostBreakdown, Harness, ProcNode, SpanKind, TokenUsage};
58use crate::pricing::{self, Table};
59use crate::process::RawProc;
60use serde_json::Value;
61use std::collections::{BTreeMap, HashSet};
62use std::path::{Path, PathBuf};
63use std::time::{Duration, SystemTime};
64
65/// `~/.gemini`, or `$GEMINI_CLI_HOME/.gemini`, which is the CLI's own override.
66pub fn gemini_dir() -> Option<PathBuf> {
67    let home = std::env::var_os("GEMINI_CLI_HOME").or_else(|| std::env::var_os("HOME"))?;
68    Some(PathBuf::from(home).join(".gemini"))
69}
70
71/// Where every project's chats live: one directory per project slug.
72pub fn tmp_dir() -> Option<PathBuf> {
73    gemini_dir().map(|d| d.join("tmp"))
74}
75
76/// One main conversation on disk: its file, the working directory its project
77/// directory stands for (absent for a pre-slug hashed directory) and the start
78/// time from its header.
79#[derive(Debug, Clone, PartialEq, Eq)]
80pub struct Session {
81    pub path: PathBuf,
82    pub cwd: Option<PathBuf>,
83    pub started: Option<SystemTime>,
84    pub session_id: Option<String>,
85}
86
87/// Every main conversation written since `since`.
88pub fn recent_sessions(since: SystemTime) -> Vec<Session> {
89    let Some(root) = tmp_dir() else { return Vec::new() };
90    sessions_under(&root, since)
91}
92
93/// Walk `<root>/<project>/chats/*.jsonl`. Subagent files sit one level deeper
94/// under the parent's id and are not sessions of their own, so the walk does
95/// not descend. `.project_root` names the working directory.
96pub(crate) fn sessions_under(root: &Path, since: SystemTime) -> Vec<Session> {
97    let mut out = Vec::new();
98    let Ok(projects) = std::fs::read_dir(root) else { return out };
99    for proj in projects.flatten() {
100        let dir = proj.path();
101        let Ok(chats) = std::fs::read_dir(dir.join("chats")) else { continue };
102        let cwd = project_root(&dir);
103        for f in chats.flatten() {
104            let p = f.path();
105            let Ok(md) = f.metadata() else { continue };
106            if !md.is_file() || p.extension().and_then(|x| x.to_str()) != Some("jsonl") {
107                continue;
108            }
109            if !md.modified().map(|m| m >= since).unwrap_or(false) {
110                continue;
111            }
112            let (session_id, started) = match read_meta(&p) {
113                Some((id, ts)) => (Some(id), Some(ts)),
114                None => (None, None),
115            };
116            out.push(Session { path: p, cwd: cwd.clone(), started, session_id });
117        }
118    }
119    out
120}
121
122/// The working directory a project directory stands for.
123pub fn project_root(project_dir: &Path) -> Option<PathBuf> {
124    let s = std::fs::read_to_string(project_dir.join(".project_root")).ok()?;
125    let s = s.trim();
126    if s.is_empty() { None } else { Some(PathBuf::from(s)) }
127}
128
129/// Cheap header read: session id and start time from the first line only.
130pub fn read_meta(path: &Path) -> Option<(String, SystemTime)> {
131    use std::io::{BufRead, BufReader};
132    let f = std::fs::File::open(path).ok()?;
133    let mut first = String::new();
134    BufReader::new(f).read_line(&mut first).ok()?;
135    let v: Value = serde_json::from_str(&first).ok()?;
136    v.get("projectHash")?;
137    let id = v.get("sessionId").and_then(Value::as_str)?.to_string();
138    let ts = v.get("startTime").and_then(Value::as_str).and_then(parse_rfc3339_utc)?;
139    Some((id, ts))
140}
141
142/// Where a session's subagent transcripts live: a directory named after the
143/// session id, next to the session file.
144pub fn subagents_dir(transcript: &Path, session_id: &str) -> Option<PathBuf> {
145    Some(transcript.parent()?.join(session_id))
146}
147
148/// Same path, as written. `.project_root` holds `path.resolve(cwd)`, and the
149/// process table reports the cwd the kernel knows, which on macOS may be the
150/// resolved form of a symlinked path; so both are canonicalised when they can
151/// be and compared as given when they cannot.
152fn same_dir(a: &Path, b: &Path) -> bool {
153    if a == b {
154        return true;
155    }
156    let ca = std::fs::canonicalize(a).unwrap_or_else(|_| a.to_path_buf());
157    let cb = std::fs::canonicalize(b).unwrap_or_else(|_| b.to_path_buf());
158    ca == cb
159}
160
161/// The conversation belonging to a Gemini CLI process: the newest session in
162/// the process's working directory that started after the process did and
163/// that no other process has claimed. One process runs one conversation; a
164/// `/chat resume` keeps writing to the resumed file, which the mtime order
165/// picks up.
166pub(crate) fn attribute(
167    cwd: Option<&Path>,
168    proc_start: SystemTime,
169    recent: &[Session],
170    taken: &HashSet<PathBuf>,
171) -> (Vec<PathBuf>, Attribution) {
172    let Some(cwd) = cwd else { return (Vec::new(), Attribution::None) };
173    let slack = Duration::from_secs(60);
174    let mut mine: Vec<&Session> = recent
175        .iter()
176        .filter(|s| !taken.contains(&s.path))
177        .filter(|s| s.cwd.as_deref().is_some_and(|c| same_dir(c, cwd)))
178        .filter(|s| s.started.is_none_or(|ts| ts + slack >= proc_start))
179        .collect();
180    mine.sort_by_key(|s| std::cmp::Reverse(std::fs::metadata(&s.path).and_then(|m| m.modified()).ok()));
181    match mine.first() {
182        Some(s) => (vec![s.path.clone()], Attribution::CwdHeuristic),
183        None => (Vec::new(), Attribution::None),
184    }
185}
186
187/// The Gemini CLI adapter. See the module notes for the layout it reads.
188#[derive(Default)]
189pub struct GeminiAdapter {
190    recent: Vec<Session>,
191}
192
193impl HarnessAdapter for GeminiAdapter {
194    fn harness(&self) -> Harness {
195        Harness::Gemini
196    }
197
198    fn rescan(&mut self, since: SystemTime) {
199        self.recent = recent_sessions(since);
200    }
201
202    fn attribute(&self, _root: &ProcNode, _raw: Option<&RawProc>, ctx: &AttributeContext) -> (Vec<PathBuf>, Attribution) {
203        attribute(ctx.cwd, ctx.proc_start, &self.recent, ctx.attached)
204    }
205
206    fn unowned(&self, attached: &HashSet<PathBuf>) -> Vec<PathBuf> {
207        self.recent.iter().filter(|s| !attached.contains(&s.path)).map(|s| s.path.clone()).collect()
208    }
209
210    fn open(&self, path: &Path, spans: SpanRetention) -> Box<dyn SessionTracker> {
211        Box::new(GeminiTranscript::new(path).with_spans(spans))
212    }
213
214    fn detect(&self, path: &Path) -> bool {
215        read_meta(path).is_some()
216    }
217
218    fn transcripts(&self) -> Vec<(String, PathBuf)> {
219        recent_sessions(SystemTime::UNIX_EPOCH)
220            .into_iter()
221            .map(|s| {
222                let id = s.session_id.unwrap_or_else(|| s.path.file_stem().map(|x| x.to_string_lossy().into_owned()).unwrap_or_default());
223                (id, s.path)
224            })
225            .collect()
226    }
227}
228
229/// What one `gemini` message added to the summary, so a later line for the
230/// same id can replace it.
231#[derive(Debug, Clone, Copy, Default)]
232struct Contrib {
233    usage: TokenUsage,
234    cost: CostBreakdown,
235    unpriced: u64,
236    has_record: bool,
237    empty_record: bool,
238}
239
240/// One JSONL file being tailed into a `SessionSummary`: the main
241/// conversation, or one subagent's.
242struct Parser {
243    reader: TailReader,
244    summary: SessionSummary,
245    /// Whether this file is a subagent's, which marks its spans as sidechain.
246    subagent: bool,
247    /// Every `gemini` message seen, by id, with what it contributed. A message
248    /// is appended again when its tool calls complete, and re-listed by a
249    /// checkpoint, so this is what stops double counting.
250    messages: BTreeMap<String, Contrib>,
251    /// `user` message ids already seen, so a re-appended prompt is not a
252    /// second prompt.
253    prompts: HashSet<String>,
254    /// Tool call ids already turned into spans.
255    calls: HashSet<String>,
256    inference: Option<String>,
257    turn: Option<String>,
258    inferences: u64,
259    turns: u64,
260    prev_ts: Option<SystemTime>,
261}
262
263impl Parser {
264    fn new(path: impl Into<PathBuf>, spans: SpanRetention, subagent: bool) -> Self {
265        Parser {
266            reader: TailReader::new(path),
267            summary: SessionSummary { harness: Some(Harness::Gemini), spans: spans.log(), ..Default::default() },
268            subagent,
269            messages: BTreeMap::new(),
270            prompts: HashSet::new(),
271            calls: HashSet::new(),
272            inference: None,
273            turn: None,
274            inferences: 0,
275            turns: 0,
276            prev_ts: None,
277        }
278    }
279
280    /// Returns how many lines were ingested and whether more are waiting.
281    fn refresh(&mut self, prices: &Table) -> anyhow::Result<(usize, bool)> {
282        let (lines, more) = self.reader.read_new_lines(REFRESH_BUDGET_BYTES)?;
283        for l in &lines {
284            self.ingest(l, prices);
285        }
286        Ok((lines.len(), more))
287    }
288
289    fn ingest(&mut self, line: &str, prices: &Table) {
290        let Ok(v) = serde_json::from_str::<Value>(line) else { return };
291        if let Some(set) = v.get("$set").filter(|s| s.is_object()) {
292            self.ingest_metadata(set);
293            if let Some(msgs) = set.get("messages").and_then(Value::as_array) {
294                for m in msgs {
295                    self.ingest_message(m, prices);
296                }
297            }
298            return;
299        }
300        if v.get("$rewindTo").is_some() {
301            // The conversation was cut back; the model owes nothing until the
302            // next prompt.
303            self.summary.activity = Activity::Waiting;
304            return;
305        }
306        if v.get("projectHash").is_some() {
307            self.ingest_metadata(&v);
308            if let Some(msgs) = v.get("messages").and_then(Value::as_array) {
309                for m in msgs {
310                    self.ingest_message(m, prices);
311                }
312            }
313            return;
314        }
315        if v.get("id").and_then(Value::as_str).is_some() {
316            self.ingest_message(&v, prices);
317        }
318    }
319
320    fn ingest_metadata(&mut self, m: &Value) {
321        if let Some(id) = m.get("sessionId").and_then(Value::as_str) {
322            self.summary.session_id = Some(id.to_string());
323        }
324        if let Some(ts) = m.get("startTime").and_then(Value::as_str).and_then(parse_rfc3339_utc) {
325            self.summary.started_at = Some(self.summary.started_at.map_or(ts, |s| s.min(ts)));
326            if self.summary.last_activity.is_none() {
327                self.summary.last_activity = Some(ts);
328            }
329        }
330        if m.get("kind").and_then(Value::as_str) == Some("subagent") {
331            self.subagent = true;
332        }
333    }
334
335    fn ingest_message(&mut self, m: &Value, prices: &Table) {
336        let Some(id) = m.get("id").and_then(Value::as_str) else { return };
337        let ts = m.get("timestamp").and_then(Value::as_str).and_then(parse_rfc3339_utc);
338        if let Some(ts) = ts {
339            if self.summary.started_at.is_none_or(|s| ts < s) {
340                self.summary.started_at = Some(ts);
341            }
342            if self.summary.last_activity.is_none_or(|l| ts > l) {
343                self.summary.last_activity = Some(ts);
344            }
345        }
346        match m.get("type").and_then(Value::as_str).unwrap_or("") {
347            "user" => self.ingest_user(id, m, ts),
348            "gemini" => self.ingest_gemini(id, m, ts, prices),
349            _ => {}
350        }
351        if ts.is_some() {
352            self.prev_ts = ts;
353        }
354    }
355
356    /// A prompt or a tool result: in both cases the model owes a response.
357    fn ingest_user(&mut self, id: &str, m: &Value, ts: Option<SystemTime>) {
358        if !self.prompts.insert(id.to_string()) {
359            return;
360        }
361        self.summary.activity = Activity::Working;
362        let Some(ts) = ts else { return };
363        if !is_tool_result(m) {
364            self.begin_turn(ts);
365        }
366        self.begin_inference(ts);
367    }
368
369    fn ingest_gemini(&mut self, id: &str, m: &Value, ts: Option<SystemTime>, prices: &Table) {
370        if let Some(model) = m.get("model").and_then(Value::as_str).filter(|s| !s.is_empty()) {
371            self.summary.model = Some(model.to_string());
372        }
373        let first_time = !self.messages.contains_key(id);
374        if first_time {
375            self.summary.health.billable_messages += 1;
376            self.summary.turns += 1;
377            if self.subagent {
378                self.summary.subagent_turns += 1;
379            }
380            // The reply is written in one line, so the inference ends here
381            // and the turn now reaches at least this far.
382            self.summary.activity = Activity::Waiting;
383            if let Some(ts) = ts {
384                if let Some(inf) = self.inference.take() {
385                    self.summary.spans.end_at(&inf, ts);
386                }
387                if let Some(turn) = self.turn.as_deref() {
388                    self.summary.spans.end_at(turn, ts);
389                }
390            }
391        }
392        self.account(id, m, prices);
393        self.ingest_tool_calls(m, ts);
394    }
395
396    /// Replace whatever this message contributed before with what it says now.
397    fn account(&mut self, id: &str, m: &Value, prices: &Table) {
398        let model = m.get("model").and_then(Value::as_str).map(str::to_string).or_else(|| self.summary.model.clone());
399        let mut c = Contrib::default();
400        if let Some(t) = m.get("tokens").filter(|t| t.is_object()) {
401            c.has_record = true;
402            c.usage = parse_tokens(t);
403            c.empty_record = c.usage.total() == 0;
404            match model.as_deref().and_then(|m| prices.lookup(m)) {
405                Some(p) => c.cost = p.breakdown(&c.usage),
406                None => c.unpriced = c.usage.total(),
407            }
408        }
409        let s = &mut self.summary;
410        if let Some(old) = self.messages.insert(id.to_string(), c) {
411            s.usage.sub(&old.usage);
412            s.cost_usd -= old.cost.total();
413            s.cost_breakdown.sub(&old.cost);
414            s.unpriced_tokens = s.unpriced_tokens.saturating_sub(old.unpriced);
415            s.health.usage_records -= u64::from(old.has_record);
416            s.health.empty_usage_records -= u64::from(old.empty_record);
417        }
418        s.usage.add(&c.usage);
419        s.cost_usd += c.cost.total();
420        s.cost_breakdown.add(&c.cost);
421        s.unpriced_tokens += c.unpriced;
422        s.health.usage_records += u64::from(c.has_record);
423        s.health.empty_usage_records += u64::from(c.empty_record);
424    }
425
426    /// Completed tool calls, appended to the message that issued them. Each
427    /// becomes a closed span from the message to the call's own timestamp,
428    /// and the tool results are about to be submitted, so the model is owed
429    /// a response again.
430    fn ingest_tool_calls(&mut self, m: &Value, msg_ts: Option<SystemTime>) {
431        let Some(calls) = m.get("toolCalls").and_then(Value::as_array) else { return };
432        for c in calls {
433            let Some(id) = c.get("id").and_then(Value::as_str).filter(|s| !s.is_empty()) else { continue };
434            if !self.calls.insert(id.to_string()) {
435                continue;
436            }
437            self.summary.tool_calls += 1;
438            self.summary.activity = Activity::Working;
439            let name = c.get("name").and_then(Value::as_str).unwrap_or("tool");
440            if name == "google_web_search" {
441                self.summary.web_searches += 1;
442            }
443            let ended = c.get("timestamp").and_then(Value::as_str).and_then(parse_rfc3339_utc);
444            let Some(started) = msg_ts.or(ended) else { continue };
445            let ended = ended.unwrap_or(started);
446            let error = c.get("status").and_then(Value::as_str) == Some("error");
447            self.summary.spans.open(id.to_string(), name.to_string(), started.min(ended), self.subagent);
448            self.summary.spans.close(id, ended, error);
449            if self.summary.last_activity.is_none_or(|l| ended > l) {
450                self.summary.last_activity = Some(ended);
451            }
452        }
453    }
454
455    /// A prompt starts a turn. One the model never answered is ended where
456    /// the last line before this prompt was written.
457    fn begin_turn(&mut self, ts: SystemTime) {
458        if let Some(id) = self.turn.take()
459            && self.summary.spans.open_of_kind(SpanKind::Turn).is_some_and(|s| s.id == id)
460        {
461            let ended = self.prev_ts.unwrap_or(ts).min(ts);
462            self.summary.spans.end_at(&id, ended);
463        }
464        self.turns += 1;
465        let id = format!("turn:{}", self.turns);
466        self.summary.spans.open_kind(id.clone(), "turn".into(), ts, self.subagent, SpanKind::Turn);
467        self.turn = Some(id);
468    }
469
470    /// A submission that got no reply before the next one was not an
471    /// inference and is dropped.
472    fn begin_inference(&mut self, ts: SystemTime) {
473        if let Some(id) = self.inference.take() {
474            self.summary.spans.discard_open(&id);
475        }
476        self.inferences += 1;
477        let id = format!("inference:{}", self.inferences);
478        self.summary.spans.open_kind(id.clone(), "inference".into(), ts, self.subagent, SpanKind::Inference);
479        self.inference = Some(id);
480    }
481}
482
483/// A `user` message answering tool calls carries `functionResponse` parts.
484/// Only the keys are inspected.
485fn is_tool_result(m: &Value) -> bool {
486    m.get("content").and_then(Value::as_array).is_some_and(|parts| parts.iter().any(|p| p.get("functionResponse").is_some()))
487}
488
489/// Gemini's usage, folded the way Google bills it: thoughts are output,
490/// tool-use prompt tokens are input, and `input` already includes the cached
491/// part, which is priced separately.
492fn parse_tokens(t: &Value) -> TokenUsage {
493    let g = |k: &str| t.get(k).and_then(Value::as_u64).unwrap_or(0);
494    let cached = g("cached");
495    TokenUsage {
496        input: g("input").saturating_sub(cached) + g("tool"),
497        cache_read: cached,
498        output: g("output") + g("thoughts"),
499        ..Default::default()
500    }
501}
502
503/// A Gemini CLI session: the main conversation plus every subagent file
504/// under `chats/<sessionId>/`, folded into one summary. A subagent may run a
505/// different model from its parent; each message is priced by the model it
506/// names.
507pub struct GeminiTranscript {
508    main: Parser,
509    subagents: BTreeMap<PathBuf, Parser>,
510    prices: &'static Table,
511    retention: SpanRetention,
512    summary: SessionSummary,
513}
514
515impl GeminiTranscript {
516    pub fn new(path: impl Into<PathBuf>) -> Self {
517        let retention = SpanRetention::Recent;
518        let path = path.into();
519        let mut main = Parser::new(&path, retention, false);
520        // The transcript never names its working directory; the project
521        // directory two levels up does, in `.project_root`.
522        main.summary.cwd = path.parent().and_then(Path::parent).and_then(project_root);
523        GeminiTranscript {
524            main,
525            subagents: BTreeMap::new(),
526            prices: pricing::table(),
527            retention,
528            summary: SessionSummary { harness: Some(Harness::Gemini), ..Default::default() },
529        }
530    }
531
532    /// See `ClaudeTranscript::with_prices`.
533    pub fn with_prices(mut self, prices: &'static Table) -> Self {
534        self.prices = prices;
535        self
536    }
537
538    /// Keep every span instead of the newest `MAX_SPANS`. See `SpanRetention`.
539    pub fn with_spans(mut self, retention: SpanRetention) -> Self {
540        self.retention = retention;
541        self.main.summary.spans = retention.log();
542        for p in self.subagents.values_mut() {
543            p.summary.spans = retention.log();
544        }
545        self
546    }
547
548    /// Pick up subagent files that appeared since the last look: one
549    /// directory listing per refresh, and for most sessions a single failed
550    /// `open`.
551    fn discover_subagents(&mut self) {
552        let Some(id) = self.main.summary.session_id.as_deref() else { return };
553        let Some(dir) = subagents_dir(self.main.reader.path(), id) else { return };
554        let Ok(rd) = std::fs::read_dir(&dir) else { return };
555        for e in rd.flatten() {
556            let p = e.path();
557            if p.extension().and_then(|x| x.to_str()) != Some("jsonl") || self.subagents.contains_key(&p) {
558                continue;
559            }
560            let parser = Parser::new(&p, self.retention, true);
561            self.subagents.insert(p, parser);
562        }
563    }
564
565    fn fold(&mut self) {
566        let mut s = self.main.summary.clone();
567        for c in self.subagents.values() {
568            let t = &c.summary;
569            s.usage.add(&t.usage);
570            s.cost_usd += t.cost_usd;
571            s.cost_breakdown.add(&t.cost_breakdown);
572            s.unpriced_tokens += t.unpriced_tokens;
573            s.turns += t.turns;
574            s.subagent_turns += t.subagent_turns;
575            s.tool_calls += t.tool_calls;
576            s.web_searches += t.web_searches;
577            s.health.billable_messages += t.health.billable_messages;
578            s.health.usage_records += t.health.usage_records;
579            s.health.empty_usage_records += t.health.empty_usage_records;
580            s.last_activity = s.last_activity.max(t.last_activity);
581            for (server, u) in &t.mcp {
582                s.mcp.entry(server.clone()).or_default().add(u);
583            }
584        }
585        if !self.subagents.is_empty() {
586            let logs = std::iter::once(&self.main.summary.spans).chain(self.subagents.values().map(|c| &c.summary.spans));
587            s.spans = SpanLog::merged(logs, self.main.summary.spans.cap());
588        }
589        self.summary = s;
590    }
591}
592
593impl SessionTracker for GeminiTranscript {
594    fn refresh(&mut self) -> anyhow::Result<bool> {
595        let (mut ingested, mut more) = self.main.refresh(self.prices)?;
596        self.discover_subagents();
597        for c in self.subagents.values_mut() {
598            // One unreadable subagent file must not take the session with it.
599            if let Ok((n, m)) = c.refresh(self.prices) {
600                ingested += n;
601                more |= m;
602            }
603        }
604        if ingested > 0 || self.summary.session_id.is_none() {
605            self.fold();
606        }
607        Ok(more)
608    }
609
610    fn summary(&self) -> &SessionSummary {
611        &self.summary
612    }
613
614    fn path(&self) -> &Path {
615        self.main.reader.path()
616    }
617}
618
619#[cfg(test)]
620mod tests {
621    use super::*;
622    use std::io::Write;
623
624    fn scratch(name: &str) -> PathBuf {
625        let dir = std::env::temp_dir().join(format!("agent-top-gemini-{name}-{}", std::process::id()));
626        let _ = std::fs::remove_dir_all(&dir);
627        std::fs::create_dir_all(&dir).unwrap();
628        dir
629    }
630
631    const META: &str = r#"{"sessionId":"0a1b2c3d-0000-4000-8000-000000000001","projectHash":"604d","startTime":"2026-09-05T09:00:00.000Z","lastUpdated":"2026-09-05T09:00:00.000Z","kind":"main"}"#;
632
633    #[test]
634    fn folds_tokens_the_way_google_bills_them_and_dedupes_by_id() {
635        let dir = scratch("tokens");
636        let path = dir.join("session-2026-09-05T09-00-0a1b2c3d.jsonl");
637        let mut f = std::fs::File::create(&path).unwrap();
638        writeln!(f, "{META}").unwrap();
639        writeln!(f, r#"{{"id":"u1","timestamp":"2026-09-05T09:00:02.000Z","type":"user","content":[{{"text":"p"}}]}}"#).unwrap();
640        writeln!(f, r#"{{"$set":{{"lastUpdated":"2026-09-05T09:00:02.000Z"}}}}"#).unwrap();
641        let g1 = r#"{"id":"g1","timestamp":"2026-09-05T09:00:05.000Z","type":"gemini","content":"","tokens":{"input":12000,"output":40,"cached":9000,"thoughts":300,"tool":0,"total":12340},"model":"gemini-2.5-pro"}"#;
642        writeln!(f, "{g1}").unwrap();
643        // The same message again, now carrying its completed tool call.
644        let with_call = g1.replace(
645            r#""model":"gemini-2.5-pro"}"#,
646            r#""model":"gemini-2.5-pro","toolCalls":[{"id":"call-1","name":"read_file","status":"success","timestamp":"2026-09-05T09:00:09.000Z"}]}"#,
647        );
648        writeln!(f, "{with_call}").unwrap();
649        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
650        t.refresh().unwrap();
651        let s = t.summary();
652        assert_eq!(s.session_id.as_deref(), Some("0a1b2c3d-0000-4000-8000-000000000001"));
653        assert_eq!(s.model.as_deref(), Some("gemini-2.5-pro"));
654        assert_eq!(s.turns, 1, "one reply, appended twice");
655        assert_eq!(s.usage.input, 3000, "prompt tokens minus the cached part");
656        assert_eq!(s.usage.cache_read, 9000);
657        assert_eq!(s.usage.output, 340, "thoughts are billed as output");
658        assert_eq!(s.usage.total(), 12340, "and the fold adds back up to Gemini's total");
659        // gemini-2.5-pro: 3000*1.25 + 9000*0.125 + 340*10 = 3750 + 1125 + 3400 micro-dollars
660        assert!((s.cost_usd - 0.008275).abs() < 1e-9, "{}", s.cost_usd);
661        assert_eq!(s.unpriced_tokens, 0);
662        assert_eq!(s.tool_calls, 1);
663        assert_eq!(s.activity, Activity::Working, "a completed call means results are about to be submitted");
664        let tools: Vec<_> = s.spans.iter().filter(|sp| sp.kind == SpanKind::Tool).collect();
665        assert_eq!(tools.len(), 1);
666        assert_eq!(tools[0].name, "read_file");
667        assert_eq!(tools[0].duration_ms, Some(4_000), "from the message that issued it to its completion");
668        assert_eq!(read_meta(&path).unwrap().0, "0a1b2c3d-0000-4000-8000-000000000001");
669        let _ = std::fs::remove_dir_all(&dir);
670    }
671
672    #[test]
673    fn reconstructs_turns_inferences_and_counts_searches() {
674        let dir = scratch("turns");
675        let path = dir.join("session.jsonl");
676        let mut f = std::fs::File::create(&path).unwrap();
677        writeln!(f, "{META}").unwrap();
678        writeln!(f, r#"{{"id":"u1","timestamp":"2026-09-05T09:00:00.000Z","type":"user","content":[{{"text":"p"}}]}}"#).unwrap();
679        writeln!(f, r#"{{"id":"g1","timestamp":"2026-09-05T09:00:03.000Z","type":"gemini","content":"","tokens":{{"input":10,"output":1,"cached":0,"thoughts":0,"tool":0,"total":11}},"model":"gemini-2.5-flash"}}"#).unwrap();
680        writeln!(f, r#"{{"id":"g1","timestamp":"2026-09-05T09:00:03.000Z","type":"gemini","content":"","tokens":{{"input":10,"output":1,"cached":0,"thoughts":0,"tool":0,"total":11}},"model":"gemini-2.5-flash","toolCalls":[{{"id":"c1","name":"google_web_search","status":"success","timestamp":"2026-09-05T09:00:06.000Z"}},{{"id":"c2","name":"run_shell_command","status":"error","timestamp":"2026-09-05T09:00:10.000Z"}}]}}"#).unwrap();
681        writeln!(f, r#"{{"id":"u2","timestamp":"2026-09-05T09:00:10.100Z","type":"user","content":[{{"functionResponse":{{"id":"c1"}}}},{{"functionResponse":{{"id":"c2"}}}}]}}"#).unwrap();
682        writeln!(f, r#"{{"id":"g2","timestamp":"2026-09-05T09:00:15.000Z","type":"gemini","content":"done","tokens":{{"input":20,"output":5,"cached":10,"thoughts":0,"tool":0,"total":25}},"model":"gemini-2.5-flash"}}"#).unwrap();
683        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
684        t.refresh().unwrap();
685        let s = t.summary();
686        assert_eq!(s.turns, 2);
687        assert_eq!(s.tool_calls, 2);
688        assert_eq!(s.web_searches, 1);
689        assert_eq!(s.activity, Activity::Waiting);
690        let all = s.spans.to_vec();
691        let tools: Vec<_> = all.iter().filter(|sp| sp.kind == SpanKind::Tool).collect();
692        assert_eq!(tools.len(), 2);
693        assert_eq!(tools[0].duration_ms, Some(3_000));
694        assert!(!tools[0].error);
695        assert_eq!(tools[1].duration_ms, Some(7_000));
696        assert!(tools[1].error);
697        let inf: Vec<_> = all.iter().filter(|sp| sp.kind == SpanKind::Inference).collect();
698        assert_eq!(inf.len(), 2);
699        assert_eq!(inf[0].duration_ms, Some(3_000), "prompt at :00, reply at :03");
700        assert_eq!(inf[1].duration_ms, Some(4_900), "tool results at :10.1, reply at :15");
701        let turns: Vec<_> = all.iter().filter(|sp| sp.kind == SpanKind::Turn).collect();
702        assert_eq!(turns.len(), 1, "one prompt; the tool results are not a new turn");
703        assert_eq!(turns[0].duration_ms, Some(15_000), "the turn reaches the last reply");
704        assert_eq!(s.health.billable_messages, 2);
705        assert_eq!(s.health.usage_records, 2);
706        assert!(!s.health.fields_unrecognised());
707        let _ = std::fs::remove_dir_all(&dir);
708    }
709
710    #[test]
711    fn folds_subagent_files_named_after_the_parent() {
712        let dir = scratch("sub");
713        let path = dir.join("session-2026-09-05T09-00-0a1b2c3d.jsonl");
714        let mut f = std::fs::File::create(&path).unwrap();
715        writeln!(f, "{META}").unwrap();
716        writeln!(f, r#"{{"id":"u1","timestamp":"2026-09-05T09:00:00.000Z","type":"user","content":[{{"text":"p"}}]}}"#).unwrap();
717        writeln!(f, r#"{{"id":"g1","timestamp":"2026-09-05T09:00:03.000Z","type":"gemini","content":"","tokens":{{"input":100,"output":10,"cached":0,"thoughts":0,"tool":0,"total":110}},"model":"gemini-2.5-pro"}}"#).unwrap();
718        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
719        t.refresh().unwrap();
720        assert_eq!(t.summary().usage.total(), 110);
721        assert_eq!(t.summary().subagent_turns, 0);
722
723        let sub = subagents_dir(&path, "0a1b2c3d-0000-4000-8000-000000000001").unwrap();
724        std::fs::create_dir_all(&sub).unwrap();
725        let mut g = std::fs::File::create(sub.join("b2c3.jsonl")).unwrap();
726        writeln!(g, r#"{{"sessionId":"b2c3","projectHash":"604d","startTime":"2026-09-05T09:00:04.000Z","lastUpdated":"2026-09-05T09:00:04.000Z","kind":"subagent"}}"#).unwrap();
727        writeln!(g, r#"{{"id":"su1","timestamp":"2026-09-05T09:00:04.000Z","type":"user","content":[{{"text":"t"}}]}}"#).unwrap();
728        writeln!(g, r#"{{"id":"sg1","timestamp":"2026-09-05T09:00:08.000Z","type":"gemini","content":"","tokens":{{"input":50,"output":5,"cached":0,"thoughts":0,"tool":0,"total":55}},"model":"gemini-2.5-flash","toolCalls":[{{"id":"sc1","name":"grep_search","status":"success","timestamp":"2026-09-05T09:00:09.000Z"}}]}}"#).unwrap();
729        t.refresh().unwrap();
730        let s = t.summary();
731        assert_eq!(s.usage.total(), 165);
732        assert_eq!(s.turns, 2);
733        assert_eq!(s.subagent_turns, 1);
734        assert_eq!(s.tool_calls, 1);
735        assert_eq!(s.model.as_deref(), Some("gemini-2.5-pro"), "the parent's model, not the subagent's");
736        let sidechain: Vec<_> = s.spans.iter().filter(|sp| sp.sidechain).collect();
737        assert_eq!(sidechain.len(), 3, "the subagent's turn, inference and tool call");
738        // 100*1.25 + 10*10 (pro) + 50*0.30 + 5*2.5 (flash) = 125 + 100 + 15 + 12.5 micro-dollars
739        assert!((s.cost_usd - 0.0002525).abs() < 1e-9, "{}", s.cost_usd);
740        let _ = std::fs::remove_dir_all(&dir);
741    }
742
743    #[test]
744    fn a_rewind_and_a_checkpoint_do_not_change_what_was_spent() {
745        let dir = scratch("rewind");
746        let path = dir.join("session.jsonl");
747        let mut f = std::fs::File::create(&path).unwrap();
748        writeln!(f, "{META}").unwrap();
749        let g1 = r#"{"id":"g1","timestamp":"2026-09-05T09:00:03.000Z","type":"gemini","content":"","tokens":{"input":100,"output":10,"cached":0,"thoughts":0,"tool":0,"total":110},"model":"gemini-2.5-pro"}"#;
750        writeln!(f, "{g1}").unwrap();
751        writeln!(f, r#"{{"$rewindTo":"g1"}}"#).unwrap();
752        writeln!(f, r#"{{"$set":{{"messages":[{g1}]}}}}"#).unwrap();
753        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
754        t.refresh().unwrap();
755        assert_eq!(t.summary().usage.total(), 110);
756        assert_eq!(t.summary().turns, 1);
757        assert_eq!(t.summary().activity, Activity::Waiting);
758        let _ = std::fs::remove_dir_all(&dir);
759    }
760
761    #[test]
762    fn finds_sessions_and_attributes_them_by_working_directory() {
763        let root = scratch("tmp");
764        let one = root.join("example");
765        let chats = one.join("chats");
766        std::fs::create_dir_all(chats.join("0a1b2c3d-0000-4000-8000-000000000001")).unwrap();
767        std::fs::write(one.join(".project_root"), "/Users/dev/code/example\n").unwrap();
768        let main = chats.join("session-2026-09-05T09-00-0a1b2c3d.jsonl");
769        std::fs::write(&main, format!("{META}\n")).unwrap();
770        // A subagent file, one level down: not a session of its own.
771        std::fs::write(chats.join("0a1b2c3d-0000-4000-8000-000000000001").join("b2c3.jsonl"), format!("{META}\n")).unwrap();
772        // A legacy hashed directory with no marker and a legacy .json file.
773        let legacy = root.join("5b2dd62b9d0bddd4");
774        std::fs::create_dir_all(legacy.join("chats")).unwrap();
775        std::fs::write(legacy.join("chats").join("session-2026-01-01T00-00-abcd1234.json"), "{}").unwrap();
776
777        let mut t = GeminiTranscript::new(&main);
778        t.refresh().unwrap();
779        assert_eq!(t.summary().cwd.as_deref(), Some(Path::new("/Users/dev/code/example")), "from the marker two levels up");
780
781        let found = sessions_under(&root, SystemTime::UNIX_EPOCH);
782        assert_eq!(found.len(), 1);
783        assert_eq!(found[0].path, main);
784        assert_eq!(found[0].cwd.as_deref(), Some(Path::new("/Users/dev/code/example")));
785        assert_eq!(found[0].session_id.as_deref(), Some("0a1b2c3d-0000-4000-8000-000000000001"));
786        let started = found[0].started.unwrap();
787
788        let (paths, a) = attribute(Some(Path::new("/Users/dev/code/example")), started - Duration::from_secs(5), &found, &HashSet::new());
789        assert_eq!(paths, vec![main.clone()]);
790        assert_eq!(a, Attribution::CwdHeuristic, "labelled as the guess it is");
791        // Another directory, or a process that started after the session, gets nothing.
792        assert!(attribute(Some(Path::new("/Users/dev/code/other")), started, &found, &HashSet::new()).0.is_empty());
793        assert!(
794            attribute(Some(Path::new("/Users/dev/code/example")), started + Duration::from_secs(120), &found, &HashSet::new()).0.is_empty()
795        );
796        // Nor does a process claim a session another one already has.
797        let taken: HashSet<PathBuf> = [main.clone()].into_iter().collect();
798        assert_eq!(attribute(Some(Path::new("/Users/dev/code/example")), started, &found, &taken).1, Attribution::None);
799        assert_eq!(attribute(None, started, &found, &HashSet::new()).1, Attribution::None);
800        let _ = std::fs::remove_dir_all(&root);
801    }
802}