From ecd0d19f423b632459a0ef5e150f695df3634178 Mon Sep 17 00:00:00 2001 From: igorjs <2278164+igorjs@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:47:33 +1100 Subject: [PATCH] feat(sessions): cache the pull request in sessions.db gather skips the gh lookup when the stored PR was checked under five minutes ago and the branch is unchanged. Otherwise it looks the PR up and writes it back to an existing database with one UPDATE. A lookup that finds nothing keeps the stored PR and stamps the check time. A forced lookup ignores the cache for the apply command that comes later. Refs #536 Signed-off-by: igorjs <2278164+igorjs@users.noreply.github.com> --- crates/cli/src/sessions/links.rs | 403 ++++++++++++++++++++++++++++++- crates/cli/src/sessions/mod.rs | 10 +- crates/cli/src/sessions/store.rs | 126 +++++++++- 3 files changed, 517 insertions(+), 22 deletions(-) diff --git a/crates/cli/src/sessions/links.rs b/crates/cli/src/sessions/links.rs index 569b6eadf..814a66a0c 100644 --- a/crates/cli/src/sessions/links.rs +++ b/crates/cli/src/sessions/links.rs @@ -4,9 +4,12 @@ //! The slow, fallible lookups behind a session record: the working directory //! a transcript recorded, what git says about that directory, and the pull //! request of the branch. Every call is bounded by a timeout and every failure -//! reads as "unknown". Nothing here writes or changes anything. +//! reads as "unknown". The only write is the pull request cache: `gather` +//! stores a lookup's outcome in an existing `sessions.db` through the injected +//! write-back, and falls back to the cached pull request when a lookup finds +//! none. -use super::{is_trunk, Place, Pr, PrKind}; +use super::{is_trunk, store, Place, Pr, PrKind}; use crate::common::git; use crate::common::proc::run_with_timeout; use crate::usage::run::Paths; @@ -20,6 +23,24 @@ use std::time::Duration; /// Lines of a transcript read while looking for its working directory. const HEAD_LINES: usize = 200; const GH_TIMEOUT: Duration = Duration::from_secs(8); +/// Seconds a looked-up pull request stays trusted before `gh` is asked again. +pub(crate) const PR_REFRESH_SECS: i64 = 300; + +/// A pull request as `gh` reports it, with the commit and base the closer +/// compares against. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PrFound { + pub number: u64, + pub kind: PrKind, + pub head_oid: Option, + pub base: Option, +} + +/// The pull request lookup of `gather`, injected so tests need no `gh`. +pub(crate) type PrLookup<'a> = &'a (dyn Fn(&Path, &str) -> Option + Sync); +/// Stores a lookup's outcome for a session; a failure is the caller's to ignore. +pub(crate) type WriteBack<'a> = + &'a (dyn Fn(&Path, &str, &store::CacheUpdate) -> Result<(), String> + Sync); /// The `cwd` of the first transcript line that has one, for the Claude Code /// transcript of `session_id` under `projects`. The prompt text on those lines @@ -139,19 +160,23 @@ fn convention_label(c: crate::worktree::Convention) -> &'static str { /// The newest pull request of `branch`, read with `gh` from `dir`. `None` when /// `gh` is missing, offline or finds none. -pub fn pr_of(dir: &Path, branch: &str) -> Option { +pub fn pr_of(dir: &Path, branch: &str) -> Option { let mut command = Command::new("gh"); command .current_dir(dir) .args([ "pr", "list", "--head", branch, "--state", "all", "--limit", "1", ]) - .args(["--json", "number,state"]); + .args(["--json", "number,state,headRefOid,baseRefName"]); let out = run_with_timeout(&mut command, GH_TIMEOUT).filter(|o| o.status.success())?; parse_pr_list(&String::from_utf8_lossy(&out.stdout)) } -pub fn parse_pr_list(json: &str) -> Option { +fn text_field(value: &serde_json::Value, key: &str) -> Option { + value.get(key)?.as_str().map(str::to_string) +} + +pub fn parse_pr_list(json: &str) -> Option { let value: serde_json::Value = serde_json::from_str(json).ok()?; let first = value.as_array()?.first()?; let kind = match first.get("state")?.as_str()? { @@ -160,19 +185,29 @@ pub fn parse_pr_list(json: &str) -> Option { "CLOSED" => PrKind::Closed, _ => return None, }; - Some(Pr { + Some(PrFound { number: first.get("number")?.as_u64()?, kind, + head_oid: text_field(first, "headRefOid"), + base: text_field(first, "baseRefName"), }) } /// Looks up the place and the PR of every distinct session in `newest`, one /// scoped thread each so slow `gh` calls overlap. Keyed by session id. -pub fn gather( +pub(crate) fn gather( newest: &[&UsageEvent], paths: &Paths, home: &Path, + now: i64, + force: bool, + pr_of: PrLookup, + write_back: WriteBack, ) -> HashMap)> { + let ids: Vec = newest.iter().map(|e| e.session_id.clone()).collect(); + let db = store::db_path_from(home); + let cache = store::read_pr_cache(&db, &ids); + let (db, cache) = (&db, &cache); std::thread::scope(|scope| { let handles: Vec<_> = newest .iter() @@ -180,12 +215,46 @@ pub fn gather( scope.spawn(move || { let cwd = transcript_cwd(&paths.claude_projects, &e.session_id); let place = place_of(cwd, home, Dirty::Check); - let pr = match (&place.cwd, place.gone) { - (Some(dir), false) if !is_trunk(&e.branch) => { - pr_of(Path::new(dir), &e.branch) - } + let row = cache.get(&e.session_id); + // A session that changed branch must not show the old branch's PR. + let cached = row.filter(|c| c.branch.as_deref() == Some(e.branch.as_str())); + let dir = match (&place.cwd, place.gone) { + (Some(dir), false) if !is_trunk(&e.branch) => Some(dir), _ => None, }; + let Some(dir) = dir else { + return (e.session_id.clone(), (place, None)); + }; + let is_fresh = cached + .and_then(|c| c.checked_at) + .is_some_and(|at| !force && now - at < PR_REFRESH_SECS); + let looked_up = (!is_fresh).then(|| pr_of(Path::new(dir), &e.branch)); + let cached_pr = cached.and_then(|c| c.pr); + let pr = match &looked_up { + // No PR found keeps the cached one: `gh` also fails offline. + Some(found) => found + .as_ref() + .map(|f| Pr { + number: f.number, + kind: f.kind, + }) + .or(cached_pr), + None => cached_pr, + }; + let mut update = store::CacheUpdate::default(); + if let Some(found) = &looked_up { + update.pr_checked_at = Some(now); + if let Some(f) = found { + update.pr_number = i64::try_from(f.number).ok(); + update.pr_kind = Some(f.kind.label()); + update.pr_head_oid = f.head_oid.clone(); + update.pr_base = f.base.clone(); + } + } + if row.is_some() && update != store::CacheUpdate::default() { + // A failed write only costs the next read a lookup. + let _ = write_back(db, &e.session_id, &update); + } (e.session_id.clone(), (place, pr)) }) }) @@ -198,10 +267,320 @@ pub fn gather( mod tests { use super::*; + use crate::common::test_support::scratch_dir; + use std::sync::atomic::{AtomicUsize, Ordering}; + + const OLD_AGE_SECS: i64 = PR_REFRESH_SECS; + + fn merged() -> PrFound { + PrFound { + number: PR_NUMBER, + kind: PrKind::Merged, + head_oid: Some("abc".into()), + base: Some("main".into()), + } + } + + const NOW: i64 = 1_788_400_000; + const SESSION: &str = "s1"; + const BRANCH: &str = "feat/x"; + const PR_NUMBER: u64 = 7; + const SEEDED_OID: &str = "seeded-oid"; + const SEEDED_BASE: &str = "seeded-base"; + const OTHER_BRANCH: &str = "feat/old"; + + fn no_write(_: &Path, _: &str, _: &store::CacheUpdate) -> Result<(), String> { + Ok(()) + } + + /// A scratch home with a transcript whose cwd is an existing folder and, + /// once seeded, a `sessions.db` row for [`SESSION`]. + struct Fixture { + home: PathBuf, + work: PathBuf, + branch: &'static str, + } + + impl Fixture { + fn new(tag: &str) -> Self { + let home = scratch_dir(tag); + let work = home.join("work"); + let projects = home.join(".claude").join("projects").join("-p"); + std::fs::create_dir_all(&work).unwrap(); + std::fs::create_dir_all(&projects).unwrap(); + std::fs::write( + projects.join(format!("{SESSION}.jsonl")), + format!("{{\"cwd\":\"{}\"}}\n", work.display()), + ) + .unwrap(); + Fixture { + home, + work, + branch: BRANCH, + } + } + + fn db(&self) -> PathBuf { + store::db_path_from(&self.home) + } + + fn seed(&self, checked_at: Option, kind: Option<&str>) { + self.seed_on(BRANCH, checked_at, kind); + } + + fn seed_on(&self, branch: &str, checked_at: Option, kind: Option<&str>) { + let conn = store::open_at(&self.db()).unwrap(); + let number = kind.map(|_| PR_NUMBER as i64); + conn.execute( + "INSERT INTO sessions (session_id, cwd, branch, pr_number, pr_kind, \ + pr_head_oid, pr_base, pr_checked_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)", + playbook_store::params![ + SESSION, + self.work.to_str(), + branch, + number, + kind, + SEEDED_OID, + SEEDED_BASE, + checked_at + ], + ) + .unwrap(); + } + + fn column(&self, name: &str) -> Option { + let conn = store::open_at(&self.db()).unwrap(); + conn.query_row( + &format!("SELECT CAST({name} AS TEXT) FROM sessions WHERE session_id = ?1"), + [SESSION], + |r| r.get(0), + ) + .unwrap() + } + + fn gather( + &self, + now: i64, + force: bool, + lookup: PrLookup, + write: WriteBack, + ) -> (Place, Option) { + let event = UsageEvent { + session_id: SESSION.into(), + branch: self.branch.into(), + ..UsageEvent::default() + }; + let paths = Paths::from_home(&self.home); + let mut found = gather(&[&event], &paths, &self.home, now, force, lookup, write); + found.remove(SESSION).expect("the session is gathered") + } + } + + impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.home); + } + } + + #[test] + fn a_fresh_cached_pr_does_not_call_the_lookup() { + // Arrange + let fixture = Fixture::new("fresh"); + fixture.seed(Some(NOW - (PR_REFRESH_SECS - 1)), Some("open")); + let lookup = |_: &Path, _: &str| -> Option { panic!("the lookup must not run") }; + + // Act + let (_, pr) = fixture.gather(NOW, false, &lookup, &no_write); + + // Assert + assert_eq!( + pr, + Some(Pr { + number: PR_NUMBER, + kind: PrKind::Open + }) + ); + } + + #[test] + fn the_lookup_runs_and_is_written_back_when_the_cache_is_old_or_empty() { + // Arrange + let cases = [("old", Some(NOW - OLD_AGE_SECS)), ("never_checked", None)]; + for (tag, checked_at) in cases { + let fixture = Fixture::new(tag); + fixture.seed(checked_at, Some("open")); + let calls = AtomicUsize::new(0); + let lookup = |_: &Path, _: &str| { + calls.fetch_add(1, Ordering::SeqCst); + Some(merged()) + }; + + // Act + let (_, pr) = fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 1, "{tag}"); + assert_eq!(pr.map(|p| p.kind), Some(PrKind::Merged), "{tag}"); + assert_eq!( + fixture.column("pr_checked_at"), + Some(NOW.to_string()), + "{tag}" + ); + } + } + + fn counting<'a>( + calls: &'a AtomicUsize, + found: Option, + ) -> impl Fn(&Path, &str) -> Option + Sync + 'a { + move |_, _| { + calls.fetch_add(1, Ordering::SeqCst); + found.clone() + } + } + + #[test] + fn without_a_database_the_lookup_runs_and_no_file_is_created() { + // Arrange + let fixture = Fixture::new("no-db"); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, Some(merged())); + + // Act + let (_, pr) = fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert_eq!(pr.map(|p| p.kind), Some(PrKind::Merged)); + assert!(!fixture.db().exists()); + } + + #[test] + fn a_trunk_branch_or_a_missing_or_gone_place_skips_the_lookup_and_shows_no_pr() { + // Arrange + let cases = [ + ("trunk", "main", None), + ("no_place", BRANCH, Some(".claude")), + ("gone_place", BRANCH, Some("work")), + ]; + for (tag, branch, remove) in cases { + let mut fixture = Fixture::new(tag); + fixture.branch = branch; + fixture.seed_on(branch, Some(NOW - 10), Some("open")); + if let Some(name) = remove { + std::fs::remove_dir_all(fixture.home.join(name)).unwrap(); + } + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, Some(merged())); + + // Act + let (_, pr) = fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 0, "{tag}"); + assert_eq!(pr, None, "{tag}"); + } + } + + #[test] + fn a_forced_lookup_replaces_a_fresh_cached_pr() { + // Arrange + let fixture = Fixture::new("force"); + fixture.seed(Some(NOW - 10), Some("open")); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, Some(merged())); + + // Act + let (_, pr) = fixture.gather(NOW, true, &lookup, &store::write_back); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert_eq!(pr.map(|p| p.kind), Some(PrKind::Merged)); + assert_eq!(fixture.column("pr_kind").as_deref(), Some("merged")); + assert_eq!(fixture.column("pr_head_oid").as_deref(), Some("abc")); + assert_eq!(fixture.column("pr_base").as_deref(), Some("main")); + } + + #[test] + fn a_forced_lookup_that_finds_nothing_keeps_the_cached_pr() { + // Arrange + let fixture = Fixture::new("force-none"); + fixture.seed(Some(NOW - 10), Some("open")); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, None); + + // Act + let (_, pr) = fixture.gather(NOW, true, &lookup, &store::write_back); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert_eq!(pr.map(|p| p.kind), Some(PrKind::Open)); + assert_eq!(fixture.column("pr_kind").as_deref(), Some("open")); + assert_eq!(fixture.column("pr_head_oid").as_deref(), Some(SEEDED_OID)); + assert_eq!(fixture.column("pr_base").as_deref(), Some(SEEDED_BASE)); + } + + #[test] + fn a_pr_cached_on_another_branch_is_never_shown() { + // Arrange + let cases = [("fresh", NOW - 10), ("old", NOW - OLD_AGE_SECS)]; + for (tag, checked_at) in cases { + let fixture = Fixture::new(tag); + fixture.seed_on(OTHER_BRANCH, Some(checked_at), Some("open")); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, None); + + // Act + let (_, pr) = fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 1, "{tag}"); + assert_eq!(pr, None, "{tag}"); + } + } + + #[test] + fn a_failed_write_back_still_returns_the_looked_up_pr() { + // Arrange + let fixture = Fixture::new("write-fails"); + fixture.seed(None, None); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, Some(merged())); + let failing = |_: &Path, _: &str, _: &store::CacheUpdate| Err("disk full".to_string()); + + // Act + let (_, pr) = fixture.gather(NOW, false, &lookup, &failing); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert_eq!(pr.map(|p| p.kind), Some(PrKind::Merged)); + } + + #[test] + fn a_branch_with_no_pr_is_marked_checked_and_keeps_its_columns() { + // Arrange + let fixture = Fixture::new("no-pr"); + fixture.seed(None, None); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, None); + + // Act + fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + assert_eq!(fixture.column("pr_kind"), None); + assert_eq!(fixture.column("pr_checked_at"), Some(NOW.to_string())); + } + #[test] fn a_pr_list_reads_the_newest_state() { - let pr = parse_pr_list(r#"[{"number":540,"state":"MERGED"}]"#).unwrap(); + let json = r#"[{"number":540,"state":"MERGED","headRefOid":"abc","baseRefName":"main"}]"#; + let pr = parse_pr_list(json).unwrap(); assert_eq!((pr.number, pr.kind), (540, PrKind::Merged)); + assert_eq!( + (pr.head_oid.as_deref(), pr.base.as_deref()), + (Some("abc"), Some("main")) + ); assert_eq!(parse_pr_list("[]"), None); assert_eq!(parse_pr_list("not json"), None); assert_eq!(parse_pr_list(r#"[{"number":1,"state":"DRAFT"}]"#), None); diff --git a/crates/cli/src/sessions/mod.rs b/crates/cli/src/sessions/mod.rs index baef8217c..9a3e4d648 100644 --- a/crates/cli/src/sessions/mod.rs +++ b/crates/cli/src/sessions/mod.rs @@ -302,7 +302,15 @@ pub fn load( } } let heads: Vec<&UsageEvent> = newest.into_values().collect(); - let found = links::gather(&heads, paths, home); + let found = links::gather( + &heads, + paths, + home, + now, + false, + &links::pr_of, + &store::write_back, + ); let lookup = |e: &UsageEvent| found.get(&e.session_id).cloned().unwrap_or_default(); let ids: Vec = heads.iter().map(|e| e.session_id.clone()).collect(); let all = crate::usage::db::load_usage_events_of_sessions(&conn, &ids)?; diff --git a/crates/cli/src/sessions/store.rs b/crates/cli/src/sessions/store.rs index 2edd101a7..fa51e97bf 100644 --- a/crates/cli/src/sessions/store.rs +++ b/crates/cli/src/sessions/store.rs @@ -7,7 +7,7 @@ //! transaction that upgrades from read to write fails at once with //! SQLITE_BUSY in WAL mode, and `busy_timeout` does not retry it. -use super::{state, ACTIVE_SECONDS}; +use super::{state, Pr, PrKind, ACTIVE_SECONDS}; use crate::common::atomic::make_db_file_private; use crate::common::sqlite::{retry_busy, OPEN_BUDGET}; use playbook_store::{params, schema, Connection, OpenFlags, OptionalExtension}; @@ -78,6 +78,47 @@ CREATE TABLE IF NOT EXISTS lifecycle_actions ( ); "; +/// What a pull request lookup adds to a stored row. A `None` field leaves its +/// column as it is. +#[derive(Debug, Default, PartialEq, Eq)] +pub(crate) struct CacheUpdate { + pub pr_number: Option, + pub pr_kind: Option<&'static str>, + pub pr_head_oid: Option, + pub pr_base: Option, + pub pr_checked_at: Option, +} + +/// Stores `update` on the row of `id` in the database at `path`, in one +/// statement so a failure leaves the row as it was. Does nothing when the file +/// is absent: the reader never creates the database. +pub(crate) fn write_back(path: &Path, id: &str, update: &CacheUpdate) -> Result<(), String> { + if !path.exists() { + return Ok(()); + } + let conn = open_existing( + path, + OpenFlags::SQLITE_OPEN_READ_WRITE, + HOOK_BUSY_TIMEOUT_MS, + )?; + conn.execute( + "UPDATE sessions SET pr_number = COALESCE(?2, pr_number), \ + pr_kind = COALESCE(?3, pr_kind), pr_head_oid = COALESCE(?4, pr_head_oid), \ + pr_base = COALESCE(?5, pr_base), pr_checked_at = COALESCE(?6, pr_checked_at) \ + WHERE session_id = ?1", + params![ + id, + update.pr_number, + update.pr_kind, + update.pr_head_oid, + update.pr_base, + update.pr_checked_at + ], + ) + .map_err(|e| db_error("update", e))?; + Ok(()) +} + /// What a SessionStart hook knows about the session it starts. pub(crate) struct StartInfo { pub id: String, @@ -129,6 +170,78 @@ pub(crate) fn db_path_from(home: &Path) -> PathBuf { playbook_data::data_dir_from(home).join(DB_NAME) } +fn db_error(action: &str, e: playbook_store::Error) -> String { + format!("failed to {action} {DB_NAME}: {e}") +} + +/// Opens the database at `path` without creating it. +fn open_existing(path: &Path, flags: OpenFlags, timeout_ms: u64) -> Result { + let conn = Connection::open_with_flags(path, flags).map_err(|e| db_error("open", e))?; + conn.busy_timeout(Duration::from_millis(timeout_ms)) + .map_err(|e| db_error("set busy_timeout on", e))?; + Ok(conn) +} + +fn open_read_only(path: &Path) -> Result { + open_existing(path, OpenFlags::SQLITE_OPEN_READ_ONLY, READ_BUSY_TIMEOUT_MS) +} + +/// What a stored row remembers of its pull request and the branch it was +/// looked up for. +#[derive(Debug, Default)] +pub(crate) struct CachedPr { + pub branch: Option, + pub pr: Option, + pub checked_at: Option, +} + +/// The cached pull requests of `ids` in the database at `path`. Empty when +/// the file is absent or unreadable: the cache only saves lookups. +pub(crate) fn read_pr_cache( + path: &Path, + ids: &[String], +) -> std::collections::HashMap { + let mut cache = std::collections::HashMap::new(); + if !path.exists() { + return cache; + } + let Ok(conn) = open_read_only(path) else { + return cache; + }; + let Ok(mut stmt) = conn.prepare( + "SELECT branch, pr_number, pr_kind, pr_checked_at \ + FROM sessions WHERE session_id = ?1", + ) else { + return cache; + }; + for id in ids { + let row = stmt + .query_row([id], |r| { + let number: Option = r.get(1)?; + let kind: Option = r.get(2)?; + Ok(CachedPr { + branch: r.get(0)?, + pr: number + .and_then(|n| u64::try_from(n).ok()) + .zip(kind.as_deref().and_then(pr_kind_of)) + .map(|(number, kind)| Pr { number, kind }), + checked_at: r.get(3)?, + }) + }) + .optional(); + if let Ok(Some(row)) = row { + cache.insert(id.clone(), row); + } + } + cache +} + +fn pr_kind_of(label: &str) -> Option { + [PrKind::Open, PrKind::Merged, PrKind::Closed] + .into_iter() + .find(|k| k.label() == label) +} + /// The rows of `ids` in the database at `path`, read through a read only /// connection. It never creates the database file; on a valid WAL database /// SQLite may still create the `-wal` and `-shm` side files. Empty when the @@ -140,18 +253,13 @@ pub(crate) fn read_rows( if !path.exists() { return Ok(Default::default()); } - let failed = - |action: &str, e: playbook_store::Error| format!("failed to {action} {DB_NAME}: {e}"); - let conn = Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY) - .map_err(|e| failed("open", e))?; - conn.busy_timeout(Duration::from_millis(READ_BUSY_TIMEOUT_MS)) - .map_err(|e| failed("set busy_timeout on", e))?; + let conn = open_read_only(path)?; let mut stmt = conn .prepare( "SELECT started_at, last_prompt_at, last_stop_at, waiting_after_prompt, \ ended_at, crashed_at, closed_at FROM sessions WHERE session_id = ?1", ) - .map_err(|e| failed("read", e))?; + .map_err(|e| db_error("read", e))?; let mut rows = std::collections::HashMap::new(); for id in ids { let row = stmt @@ -167,7 +275,7 @@ pub(crate) fn read_rows( }) }) .optional() - .map_err(|e| failed("read", e))?; + .map_err(|e| db_error("read", e))?; if let Some(row) = row { rows.insert(id.clone(), row); }