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 error = c.get("status").and_then(Value::as_str) == Some("error");
445            if let Some(server) = mcp_server_of(name) {
446                let u = self.summary.mcp.entry(server.to_string()).or_default();
447                u.calls += 1;
448                u.errors += u64::from(error);
449                u.last_call = u.last_call.max(ended.or(msg_ts));
450            }
451            let Some(started) = msg_ts.or(ended) else { continue };
452            let ended = ended.unwrap_or(started);
453            self.summary.spans.open(id.to_string(), name.to_string(), started.min(ended), self.subagent);
454            self.summary.spans.close(id, ended, error);
455            if self.summary.last_activity.is_none_or(|l| ended > l) {
456                self.summary.last_activity = Some(ended);
457            }
458        }
459    }
460
461    /// A prompt starts a turn. One the model never answered is ended where
462    /// the last line before this prompt was written.
463    fn begin_turn(&mut self, ts: SystemTime) {
464        if let Some(id) = self.turn.take()
465            && self.summary.spans.open_of_kind(SpanKind::Turn).is_some_and(|s| s.id == id)
466        {
467            let ended = self.prev_ts.unwrap_or(ts).min(ts);
468            self.summary.spans.end_at(&id, ended);
469        }
470        self.turns += 1;
471        let id = format!("turn:{}", self.turns);
472        self.summary.spans.open_kind(id.clone(), "turn".into(), ts, self.subagent, SpanKind::Turn);
473        self.turn = Some(id);
474    }
475
476    /// A submission that got no reply before the next one was not an
477    /// inference and is dropped.
478    fn begin_inference(&mut self, ts: SystemTime) {
479        if let Some(id) = self.inference.take() {
480            self.summary.spans.discard_open(&id);
481        }
482        self.inferences += 1;
483        let id = format!("inference:{}", self.inferences);
484        self.summary.spans.open_kind(id.clone(), "inference".into(), ts, self.subagent, SpanKind::Inference);
485        self.inference = Some(id);
486    }
487}
488
489/// A `user` message answering tool calls carries `functionResponse` parts.
490/// Only the keys are inspected.
491fn is_tool_result(m: &Value) -> bool {
492    m.get("content").and_then(Value::as_array).is_some_and(|parts| parts.iter().any(|p| p.get("functionResponse").is_some()))
493}
494
495/// The server behind a Gemini MCP tool name. Gemini registers an MCP tool as
496/// `mcp_<server>_<tool>` (the `mcp_` prefix is forced, and characters the
497/// Gemini API rejects become `_`). Gemini's own parser takes the first
498/// underscore-separated segment after the prefix as the server, so this does
499/// the same; a server name that itself contains an underscore is split the
500/// same (wrong) way Gemini splits it, which keeps the grouping consistent
501/// with what the CLI shows.
502pub fn mcp_server_of(tool_name: &str) -> Option<&str> {
503    let rest = tool_name.strip_prefix("mcp_")?;
504    let server = rest.split_once('_').map(|(a, _)| a).unwrap_or(rest);
505    if server.is_empty() { None } else { Some(server) }
506}
507
508/// Gemini's usage, folded the way Google bills it: thoughts are output,
509/// tool-use prompt tokens are input, and `input` already includes the cached
510/// part, which is priced separately.
511fn parse_tokens(t: &Value) -> TokenUsage {
512    let g = |k: &str| t.get(k).and_then(Value::as_u64).unwrap_or(0);
513    let cached = g("cached");
514    TokenUsage {
515        input: g("input").saturating_sub(cached) + g("tool"),
516        cache_read: cached,
517        output: g("output") + g("thoughts"),
518        ..Default::default()
519    }
520}
521
522/// A Gemini CLI session: the main conversation plus every subagent file
523/// under `chats/<sessionId>/`, folded into one summary. A subagent may run a
524/// different model from its parent; each message is priced by the model it
525/// names.
526pub struct GeminiTranscript {
527    main: Parser,
528    subagents: BTreeMap<PathBuf, Parser>,
529    prices: &'static Table,
530    retention: SpanRetention,
531    summary: SessionSummary,
532}
533
534impl GeminiTranscript {
535    pub fn new(path: impl Into<PathBuf>) -> Self {
536        let retention = SpanRetention::Recent;
537        let path = path.into();
538        let mut main = Parser::new(&path, retention, false);
539        // The transcript never names its working directory; the project
540        // directory two levels up does, in `.project_root`.
541        main.summary.cwd = path.parent().and_then(Path::parent).and_then(project_root);
542        GeminiTranscript {
543            main,
544            subagents: BTreeMap::new(),
545            prices: pricing::table(),
546            retention,
547            summary: SessionSummary { harness: Some(Harness::Gemini), ..Default::default() },
548        }
549    }
550
551    /// See `ClaudeTranscript::with_prices`.
552    pub fn with_prices(mut self, prices: &'static Table) -> Self {
553        self.prices = prices;
554        self
555    }
556
557    /// Keep every span instead of the newest `MAX_SPANS`. See `SpanRetention`.
558    pub fn with_spans(mut self, retention: SpanRetention) -> Self {
559        self.retention = retention;
560        self.main.summary.spans = retention.log();
561        for p in self.subagents.values_mut() {
562            p.summary.spans = retention.log();
563        }
564        self
565    }
566
567    /// Pick up subagent files that appeared since the last look: one
568    /// directory listing per refresh, and for most sessions a single failed
569    /// `open`.
570    fn discover_subagents(&mut self) {
571        let Some(id) = self.main.summary.session_id.as_deref() else { return };
572        let Some(dir) = subagents_dir(self.main.reader.path(), id) else { return };
573        let Ok(rd) = std::fs::read_dir(&dir) else { return };
574        for e in rd.flatten() {
575            let p = e.path();
576            if p.extension().and_then(|x| x.to_str()) != Some("jsonl") || self.subagents.contains_key(&p) {
577                continue;
578            }
579            let parser = Parser::new(&p, self.retention, true);
580            self.subagents.insert(p, parser);
581        }
582    }
583
584    fn fold(&mut self) {
585        let mut s = self.main.summary.clone();
586        for c in self.subagents.values() {
587            let t = &c.summary;
588            s.usage.add(&t.usage);
589            s.cost_usd += t.cost_usd;
590            s.cost_breakdown.add(&t.cost_breakdown);
591            s.unpriced_tokens += t.unpriced_tokens;
592            s.turns += t.turns;
593            s.subagent_turns += t.subagent_turns;
594            s.tool_calls += t.tool_calls;
595            s.web_searches += t.web_searches;
596            s.health.billable_messages += t.health.billable_messages;
597            s.health.usage_records += t.health.usage_records;
598            s.health.empty_usage_records += t.health.empty_usage_records;
599            s.last_activity = s.last_activity.max(t.last_activity);
600            for (server, u) in &t.mcp {
601                s.mcp.entry(server.clone()).or_default().add(u);
602            }
603        }
604        if !self.subagents.is_empty() {
605            let logs = std::iter::once(&self.main.summary.spans).chain(self.subagents.values().map(|c| &c.summary.spans));
606            s.spans = SpanLog::merged(logs, self.main.summary.spans.cap());
607        }
608        self.summary = s;
609    }
610}
611
612impl SessionTracker for GeminiTranscript {
613    fn refresh(&mut self) -> anyhow::Result<bool> {
614        let (mut ingested, mut more) = self.main.refresh(self.prices)?;
615        self.discover_subagents();
616        for c in self.subagents.values_mut() {
617            // One unreadable subagent file must not take the session with it.
618            if let Ok((n, m)) = c.refresh(self.prices) {
619                ingested += n;
620                more |= m;
621            }
622        }
623        if ingested > 0 || self.summary.session_id.is_none() {
624            self.fold();
625        }
626        Ok(more)
627    }
628
629    fn summary(&self) -> &SessionSummary {
630        &self.summary
631    }
632
633    fn path(&self) -> &Path {
634        self.main.reader.path()
635    }
636}
637
638#[cfg(test)]
639mod tests {
640    use super::*;
641    use std::io::Write;
642
643    fn scratch(name: &str) -> PathBuf {
644        let dir = std::env::temp_dir().join(format!("agent-top-gemini-{name}-{}", std::process::id()));
645        let _ = std::fs::remove_dir_all(&dir);
646        std::fs::create_dir_all(&dir).unwrap();
647        dir
648    }
649
650    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"}"#;
651
652    #[test]
653    fn names_the_server_behind_a_gemini_mcp_tool() {
654        assert_eq!(mcp_server_of("mcp_filesystem_read_file"), Some("filesystem"));
655        assert_eq!(mcp_server_of("mcp_chrome-devtools_take_screenshot"), Some("chrome-devtools"));
656        // Gemini flattens with single underscores and splits on the first, so a
657        // server whose name has an underscore is split its way, not ours.
658        assert_eq!(mcp_server_of("mcp_google_workspace_search"), Some("google"));
659        assert_eq!(mcp_server_of("read_file"), None);
660        assert_eq!(mcp_server_of("mcp_"), None);
661    }
662
663    #[test]
664    fn counts_gemini_mcp_calls_per_server() {
665        let dir = scratch("mcp");
666        let path = dir.join("session.jsonl");
667        let mut f = std::fs::File::create(&path).unwrap();
668        writeln!(f, "{META}").unwrap();
669        writeln!(f, r#"{{"id":"u1","timestamp":"2026-09-05T09:00:00.000Z","type":"user","content":[{{"text":"p"}}]}}"#).unwrap();
670        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-pro","toolCalls":[{{"id":"c1","name":"mcp_filesystem_read_file","status":"success","timestamp":"2026-09-05T09:00:05.000Z"}},{{"id":"c2","name":"mcp_filesystem_list_directory","status":"error","timestamp":"2026-09-05T09:00:06.000Z"}},{{"id":"c3","name":"google_web_search","status":"success","timestamp":"2026-09-05T09:00:07.000Z"}}]}}"#).unwrap();
671        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
672        t.refresh().unwrap();
673        let s = t.summary();
674        assert_eq!(s.tool_calls, 3);
675        assert_eq!(s.web_searches, 1);
676        assert_eq!(s.mcp.len(), 1, "the web search is not an MCP server");
677        let fs = &s.mcp["filesystem"];
678        assert_eq!((fs.calls, fs.errors), (2, 1));
679        assert_eq!(fs.last_call, parse_rfc3339_utc("2026-09-05T09:00:06.000Z"));
680        let _ = std::fs::remove_dir_all(&dir);
681    }
682
683    #[test]
684    fn folds_tokens_the_way_google_bills_them_and_dedupes_by_id() {
685        let dir = scratch("tokens");
686        let path = dir.join("session-2026-09-05T09-00-0a1b2c3d.jsonl");
687        let mut f = std::fs::File::create(&path).unwrap();
688        writeln!(f, "{META}").unwrap();
689        writeln!(f, r#"{{"id":"u1","timestamp":"2026-09-05T09:00:02.000Z","type":"user","content":[{{"text":"p"}}]}}"#).unwrap();
690        writeln!(f, r#"{{"$set":{{"lastUpdated":"2026-09-05T09:00:02.000Z"}}}}"#).unwrap();
691        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"}"#;
692        writeln!(f, "{g1}").unwrap();
693        // The same message again, now carrying its completed tool call.
694        let with_call = g1.replace(
695            r#""model":"gemini-2.5-pro"}"#,
696            r#""model":"gemini-2.5-pro","toolCalls":[{"id":"call-1","name":"read_file","status":"success","timestamp":"2026-09-05T09:00:09.000Z"}]}"#,
697        );
698        writeln!(f, "{with_call}").unwrap();
699        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
700        t.refresh().unwrap();
701        let s = t.summary();
702        assert_eq!(s.session_id.as_deref(), Some("0a1b2c3d-0000-4000-8000-000000000001"));
703        assert_eq!(s.model.as_deref(), Some("gemini-2.5-pro"));
704        assert_eq!(s.turns, 1, "one reply, appended twice");
705        assert_eq!(s.usage.input, 3000, "prompt tokens minus the cached part");
706        assert_eq!(s.usage.cache_read, 9000);
707        assert_eq!(s.usage.output, 340, "thoughts are billed as output");
708        assert_eq!(s.usage.total(), 12340, "and the fold adds back up to Gemini's total");
709        // gemini-2.5-pro: 3000*1.25 + 9000*0.125 + 340*10 = 3750 + 1125 + 3400 micro-dollars
710        assert!((s.cost_usd - 0.008275).abs() < 1e-9, "{}", s.cost_usd);
711        assert_eq!(s.unpriced_tokens, 0);
712        assert_eq!(s.tool_calls, 1);
713        assert_eq!(s.activity, Activity::Working, "a completed call means results are about to be submitted");
714        let tools: Vec<_> = s.spans.iter().filter(|sp| sp.kind == SpanKind::Tool).collect();
715        assert_eq!(tools.len(), 1);
716        assert_eq!(tools[0].name, "read_file");
717        assert_eq!(tools[0].duration_ms, Some(4_000), "from the message that issued it to its completion");
718        assert_eq!(read_meta(&path).unwrap().0, "0a1b2c3d-0000-4000-8000-000000000001");
719        let _ = std::fs::remove_dir_all(&dir);
720    }
721
722    #[test]
723    fn reconstructs_turns_inferences_and_counts_searches() {
724        let dir = scratch("turns");
725        let path = dir.join("session.jsonl");
726        let mut f = std::fs::File::create(&path).unwrap();
727        writeln!(f, "{META}").unwrap();
728        writeln!(f, r#"{{"id":"u1","timestamp":"2026-09-05T09:00:00.000Z","type":"user","content":[{{"text":"p"}}]}}"#).unwrap();
729        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();
730        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();
731        writeln!(f, r#"{{"id":"u2","timestamp":"2026-09-05T09:00:10.100Z","type":"user","content":[{{"functionResponse":{{"id":"c1"}}}},{{"functionResponse":{{"id":"c2"}}}}]}}"#).unwrap();
732        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();
733        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
734        t.refresh().unwrap();
735        let s = t.summary();
736        assert_eq!(s.turns, 2);
737        assert_eq!(s.tool_calls, 2);
738        assert_eq!(s.web_searches, 1);
739        assert_eq!(s.activity, Activity::Waiting);
740        let all = s.spans.to_vec();
741        let tools: Vec<_> = all.iter().filter(|sp| sp.kind == SpanKind::Tool).collect();
742        assert_eq!(tools.len(), 2);
743        assert_eq!(tools[0].duration_ms, Some(3_000));
744        assert!(!tools[0].error);
745        assert_eq!(tools[1].duration_ms, Some(7_000));
746        assert!(tools[1].error);
747        let inf: Vec<_> = all.iter().filter(|sp| sp.kind == SpanKind::Inference).collect();
748        assert_eq!(inf.len(), 2);
749        assert_eq!(inf[0].duration_ms, Some(3_000), "prompt at :00, reply at :03");
750        assert_eq!(inf[1].duration_ms, Some(4_900), "tool results at :10.1, reply at :15");
751        let turns: Vec<_> = all.iter().filter(|sp| sp.kind == SpanKind::Turn).collect();
752        assert_eq!(turns.len(), 1, "one prompt; the tool results are not a new turn");
753        assert_eq!(turns[0].duration_ms, Some(15_000), "the turn reaches the last reply");
754        assert_eq!(s.health.billable_messages, 2);
755        assert_eq!(s.health.usage_records, 2);
756        assert!(!s.health.fields_unrecognised());
757        let _ = std::fs::remove_dir_all(&dir);
758    }
759
760    #[test]
761    fn folds_subagent_files_named_after_the_parent() {
762        let dir = scratch("sub");
763        let path = dir.join("session-2026-09-05T09-00-0a1b2c3d.jsonl");
764        let mut f = std::fs::File::create(&path).unwrap();
765        writeln!(f, "{META}").unwrap();
766        writeln!(f, r#"{{"id":"u1","timestamp":"2026-09-05T09:00:00.000Z","type":"user","content":[{{"text":"p"}}]}}"#).unwrap();
767        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();
768        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
769        t.refresh().unwrap();
770        assert_eq!(t.summary().usage.total(), 110);
771        assert_eq!(t.summary().subagent_turns, 0);
772
773        let sub = subagents_dir(&path, "0a1b2c3d-0000-4000-8000-000000000001").unwrap();
774        std::fs::create_dir_all(&sub).unwrap();
775        let mut g = std::fs::File::create(sub.join("b2c3.jsonl")).unwrap();
776        writeln!(g, r#"{{"sessionId":"b2c3","projectHash":"604d","startTime":"2026-09-05T09:00:04.000Z","lastUpdated":"2026-09-05T09:00:04.000Z","kind":"subagent"}}"#).unwrap();
777        writeln!(g, r#"{{"id":"su1","timestamp":"2026-09-05T09:00:04.000Z","type":"user","content":[{{"text":"t"}}]}}"#).unwrap();
778        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();
779        t.refresh().unwrap();
780        let s = t.summary();
781        assert_eq!(s.usage.total(), 165);
782        assert_eq!(s.turns, 2);
783        assert_eq!(s.subagent_turns, 1);
784        assert_eq!(s.tool_calls, 1);
785        assert_eq!(s.model.as_deref(), Some("gemini-2.5-pro"), "the parent's model, not the subagent's");
786        let sidechain: Vec<_> = s.spans.iter().filter(|sp| sp.sidechain).collect();
787        assert_eq!(sidechain.len(), 3, "the subagent's turn, inference and tool call");
788        // 100*1.25 + 10*10 (pro) + 50*0.30 + 5*2.5 (flash) = 125 + 100 + 15 + 12.5 micro-dollars
789        assert!((s.cost_usd - 0.0002525).abs() < 1e-9, "{}", s.cost_usd);
790        let _ = std::fs::remove_dir_all(&dir);
791    }
792
793    #[test]
794    fn a_rewind_and_a_checkpoint_do_not_change_what_was_spent() {
795        let dir = scratch("rewind");
796        let path = dir.join("session.jsonl");
797        let mut f = std::fs::File::create(&path).unwrap();
798        writeln!(f, "{META}").unwrap();
799        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"}"#;
800        writeln!(f, "{g1}").unwrap();
801        writeln!(f, r#"{{"$rewindTo":"g1"}}"#).unwrap();
802        writeln!(f, r#"{{"$set":{{"messages":[{g1}]}}}}"#).unwrap();
803        let mut t = GeminiTranscript::new(&path).with_prices(pricing::builtin_table());
804        t.refresh().unwrap();
805        assert_eq!(t.summary().usage.total(), 110);
806        assert_eq!(t.summary().turns, 1);
807        assert_eq!(t.summary().activity, Activity::Waiting);
808        let _ = std::fs::remove_dir_all(&dir);
809    }
810
811    #[test]
812    fn finds_sessions_and_attributes_them_by_working_directory() {
813        let root = scratch("tmp");
814        let one = root.join("example");
815        let chats = one.join("chats");
816        std::fs::create_dir_all(chats.join("0a1b2c3d-0000-4000-8000-000000000001")).unwrap();
817        std::fs::write(one.join(".project_root"), "/Users/dev/code/example\n").unwrap();
818        let main = chats.join("session-2026-09-05T09-00-0a1b2c3d.jsonl");
819        std::fs::write(&main, format!("{META}\n")).unwrap();
820        // A subagent file, one level down: not a session of its own.
821        std::fs::write(chats.join("0a1b2c3d-0000-4000-8000-000000000001").join("b2c3.jsonl"), format!("{META}\n")).unwrap();
822        // A legacy hashed directory with no marker and a legacy .json file.
823        let legacy = root.join("5b2dd62b9d0bddd4");
824        std::fs::create_dir_all(legacy.join("chats")).unwrap();
825        std::fs::write(legacy.join("chats").join("session-2026-01-01T00-00-abcd1234.json"), "{}").unwrap();
826
827        let mut t = GeminiTranscript::new(&main);
828        t.refresh().unwrap();
829        assert_eq!(t.summary().cwd.as_deref(), Some(Path::new("/Users/dev/code/example")), "from the marker two levels up");
830
831        let found = sessions_under(&root, SystemTime::UNIX_EPOCH);
832        assert_eq!(found.len(), 1);
833        assert_eq!(found[0].path, main);
834        assert_eq!(found[0].cwd.as_deref(), Some(Path::new("/Users/dev/code/example")));
835        assert_eq!(found[0].session_id.as_deref(), Some("0a1b2c3d-0000-4000-8000-000000000001"));
836        let started = found[0].started.unwrap();
837
838        let (paths, a) = attribute(Some(Path::new("/Users/dev/code/example")), started - Duration::from_secs(5), &found, &HashSet::new());
839        assert_eq!(paths, vec![main.clone()]);
840        assert_eq!(a, Attribution::CwdHeuristic, "labelled as the guess it is");
841        // Another directory, or a process that started after the session, gets nothing.
842        assert!(attribute(Some(Path::new("/Users/dev/code/other")), started, &found, &HashSet::new()).0.is_empty());
843        assert!(
844            attribute(Some(Path::new("/Users/dev/code/example")), started + Duration::from_secs(120), &found, &HashSet::new()).0.is_empty()
845        );
846        // Nor does a process claim a session another one already has.
847        let taken: HashSet<PathBuf> = [main.clone()].into_iter().collect();
848        assert_eq!(attribute(Some(Path::new("/Users/dev/code/example")), started, &found, &taken).1, Attribution::None);
849        assert_eq!(attribute(None, started, &found, &HashSet::new()).1, Attribution::None);
850        let _ = std::fs::remove_dir_all(&root);
851    }
852}