diff --git a/crates/cli/src/hooks/session_init.rs b/crates/cli/src/hooks/session_init.rs index 2eda9bd06..c5d05df07 100644 --- a/crates/cli/src/hooks/session_init.rs +++ b/crates/cli/src/hooks/session_init.rs @@ -174,13 +174,14 @@ fn record_session_start(payload: &Payload, home: &str) { crate::sessions::links::Dirty::Skip, ); let branch = git_branch(); + let columns = crate::sessions::links::place_columns(&place, &cwd); let info = crate::sessions::store::StartInfo { id: id.clone(), - cwd: place.cwd, - repo: crate::sessions::links::repo_of(&cwd).map(|p| p.to_string_lossy().into_owned()), - worktree: place.worktree, + cwd: columns.cwd, + repo: columns.repo, + worktree: columns.worktree, branch: (!branch.is_empty() && branch != "HEAD").then_some(branch), - convention: (!place.convention.is_empty()).then(|| place.convention.to_string()), + convention: columns.convention, }; let now = crate::common::time::now_secs(); let Ok(conn) = crate::sessions::store::open() else { diff --git a/crates/cli/src/sessions/links.rs b/crates/cli/src/sessions/links.rs index 814a66a0c..3a948ab3f 100644 --- a/crates/cli/src/sessions/links.rs +++ b/crates/cli/src/sessions/links.rs @@ -4,10 +4,10 @@ //! 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". 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. +//! reads as "unknown". The only write is the cache: `gather` stores a lookup's +//! outcome in an existing `sessions.db` through the injected write-back, fills +//! the place columns of a row the hook never filled, and falls back to the +//! cached pull request when a lookup finds none. use super::{is_trunk, store, Place, Pr, PrKind}; use crate::common::git; @@ -193,6 +193,42 @@ pub fn parse_pr_list(json: &str) -> Option { }) } +/// The four place columns a `sessions.db` row stores, as a hook and the +/// backfill both write them. +pub struct PlaceColumns { + pub cwd: Option, + pub repo: Option, + pub worktree: Option, + pub convention: Option, +} + +/// The place columns of `place`, which was read from the live directory `cwd`. +pub fn place_columns(place: &Place, cwd: &Path) -> PlaceColumns { + PlaceColumns { + cwd: place.cwd.clone(), + repo: repo_of(cwd).map(|p| p.to_string_lossy().into_owned()), + worktree: place.worktree.clone(), + convention: (!place.convention.is_empty()).then(|| place.convention.to_string()), + } +} + +/// The place columns of a row that has none, from the live directory its +/// events show. Empty when there is none, so nothing unproven is stored. They +/// share one `UPDATE` with the PR columns, so a failure stores neither. +fn place_update(place: &Place, live_cwd: Option<&str>) -> store::CacheUpdate { + let Some(cwd) = live_cwd else { + return store::CacheUpdate::default(); + }; + let columns = place_columns(place, Path::new(cwd)); + store::CacheUpdate { + cwd: columns.cwd, + repo: columns.repo, + worktree: columns.worktree, + convention: columns.convention, + ..store::CacheUpdate::default() + } +} + /// 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(crate) fn gather( @@ -218,18 +254,15 @@ pub(crate) fn gather( 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 live_cwd = place.cwd.as_deref().filter(|_| !place.gone); + let dir = live_cwd.filter(|_| !is_trunk(&e.branch)); 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 looked_up = dir + .filter(|_| !is_fresh) + .map(|dir| pr_of(Path::new(dir), &e.branch)); + let cached_pr = cached.and_then(|c| c.pr).filter(|_| dir.is_some()); let pr = match &looked_up { // No PR found keeps the cached one: `gh` also fails offline. Some(found) => found @@ -241,7 +274,10 @@ pub(crate) fn gather( .or(cached_pr), None => cached_pr, }; - let mut update = store::CacheUpdate::default(); + let mut update = row + .filter(|r| !r.has_cwd) + .map(|_| place_update(&place, live_cwd)) + .unwrap_or_default(); if let Some(found) = &looked_up { update.pr_checked_at = Some(now); if let Some(f) = found { @@ -348,6 +384,46 @@ mod tests { .unwrap(); } + /// Points the transcript at a linked worktree under the agent tool + /// convention of a fresh repository. + fn use_linked_worktree(&mut self) { + let repo = self.home.join("repo"); + let linked = repo.join(".claude").join("worktrees").join("agent-1"); + git_init(&repo, &[]); + for args in [ + vec!["commit", "-q", "--allow-empty", "-m", "i"], + vec![ + "worktree", + "add", + "-q", + "-b", + BRANCH, + linked.to_str().unwrap(), + ], + ] { + let ok = Command::new("git") + .current_dir(&repo) + .args(args) + .status() + .unwrap(); + assert!(ok.success()); + } + let projects = self.home.join(".claude").join("projects").join("-p"); + std::fs::write( + projects.join(format!("{SESSION}.jsonl")), + format!("{{\"cwd\":\"{}\"}}\n", linked.display()), + ) + .unwrap(); + self.work = linked; + } + + /// A row that a hook never filled: no place columns. + fn seed_without_place(&self, checked_at: Option, kind: Option<&str>) { + self.seed(checked_at, kind); + let conn = store::open_at(&self.db()).unwrap(); + conn.execute("UPDATE sessions SET cwd = NULL", []).unwrap(); + } + fn column(&self, name: &str) -> Option { let conn = store::open_at(&self.db()).unwrap(); conn.query_row( @@ -572,6 +648,188 @@ mod tests { assert_eq!(fixture.column("pr_checked_at"), Some(NOW.to_string())); } + #[test] + fn a_merged_lookup_also_stores_the_place_of_a_row_without_a_cwd() { + // Arrange + let mut fixture = Fixture::new("backfill"); + fixture.use_linked_worktree(); + fixture.seed_without_place(None, None); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, Some(merged())); + + // Act + fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + let worktree = fixture.work.canonicalize().unwrap(); + let repo = fixture.home.join("repo").canonicalize().unwrap(); + assert_eq!( + fixture.column("cwd"), + fixture.work.to_str().map(String::from) + ); + assert_eq!(fixture.column("repo"), repo.to_str().map(String::from)); + assert_eq!( + fixture.column("worktree"), + worktree.to_str().map(String::from) + ); + assert_eq!(fixture.column("convention").as_deref(), Some("agent")); + assert_eq!(fixture.column("pr_kind").as_deref(), Some("merged")); + } + + #[test] + fn the_null_cwd_backfill_follows_the_place_and_not_the_lookup() { + // Arrange + struct Case { + tag: &'static str, + has_place: bool, + dir_gone: bool, + found: Option, + checked_at: Option, + kind: Option<&'static str>, + stores_place: bool, + pr_kind: Option<&'static str>, + checked_after: Option, + lookups: usize, + } + let cases = [ + Case { + tag: "no_place_merged", + has_place: false, + dir_gone: false, + found: Some(merged()), + checked_at: None, + kind: None, + stores_place: false, + pr_kind: None, + checked_after: None, + lookups: 0, + }, + Case { + tag: "gone_place", + has_place: true, + dir_gone: true, + found: Some(merged()), + checked_at: None, + kind: None, + stores_place: false, + pr_kind: None, + checked_after: None, + lookups: 0, + }, + Case { + tag: "place_none", + has_place: true, + dir_gone: false, + found: None, + checked_at: None, + kind: None, + stores_place: true, + pr_kind: None, + checked_after: Some(NOW), + lookups: 1, + }, + Case { + tag: "place_in_window", + has_place: true, + dir_gone: false, + found: Some(merged()), + checked_at: Some(NOW - 10), + kind: Some("open"), + stores_place: true, + pr_kind: Some("open"), + checked_after: Some(NOW - 10), + lookups: 0, + }, + ]; + for case in cases { + let mut fixture = Fixture::new(case.tag); + fixture.use_linked_worktree(); + fixture.seed_without_place(case.checked_at, case.kind); + if !case.has_place { + std::fs::remove_dir_all(fixture.home.join(".claude")).unwrap(); + } + if case.dir_gone { + std::fs::remove_dir_all(&fixture.work).unwrap(); + } + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, case.found.clone()); + + // Act + fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + let tag = case.tag; + assert_eq!(calls.load(Ordering::SeqCst), case.lookups, "{tag}"); + assert_eq!(fixture.column("cwd").is_some(), case.stores_place, "{tag}"); + assert_eq!(fixture.column("pr_kind").as_deref(), case.pr_kind, "{tag}"); + assert_eq!( + fixture.column("pr_checked_at"), + case.checked_after.map(|t| t.to_string()), + "{tag}" + ); + } + } + + #[test] + fn the_place_and_the_pr_are_written_by_one_statement() { + // Arrange + let mut fixture = Fixture::new("one-statement"); + fixture.use_linked_worktree(); + fixture.seed_without_place(None, None); + let conn = store::open_at(&fixture.db()).unwrap(); + conn.execute_batch( + "CREATE TRIGGER refuse_cwd BEFORE UPDATE OF cwd ON sessions \ + WHEN NEW.cwd IS NOT NULL BEGIN SELECT RAISE(ABORT, 'no cwd'); END;", + ) + .unwrap(); + let calls = AtomicUsize::new(0); + let lookup = counting(&calls, Some(merged())); + + // Act + fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + assert_eq!(calls.load(Ordering::SeqCst), 1); + for column in ["pr_kind", "pr_checked_at", "cwd", "worktree"] { + assert_eq!(fixture.column(column), None, "{column}"); + } + } + + #[test] + fn a_place_a_hook_wrote_during_the_lookup_is_never_overwritten() { + // Arrange + const HOOK_CWD: &str = "/hook/cwd"; + const HOOK_REPO: &str = "/hook/repo"; + const HOOK_WORKTREE: &str = "/hook/worktree"; + const HOOK_CONVENTION: &str = "hook"; + let mut fixture = Fixture::new("hook-race"); + fixture.use_linked_worktree(); + fixture.seed_without_place(None, None); + let db = fixture.db(); + let lookup = move |_: &Path, _: &str| { + let conn = store::open_at(&db).unwrap(); + conn.execute( + "UPDATE sessions SET cwd = ?1, repo = ?2, worktree = ?3, convention = ?4", + playbook_store::params![HOOK_CWD, HOOK_REPO, HOOK_WORKTREE, HOOK_CONVENTION], + ) + .unwrap(); + Some(merged()) + }; + + // Act + fixture.gather(NOW, false, &lookup, &store::write_back); + + // Assert + assert_eq!(fixture.column("cwd").as_deref(), Some(HOOK_CWD)); + assert_eq!(fixture.column("repo").as_deref(), Some(HOOK_REPO)); + assert_eq!(fixture.column("worktree").as_deref(), Some(HOOK_WORKTREE)); + assert_eq!( + fixture.column("convention").as_deref(), + Some(HOOK_CONVENTION) + ); + assert_eq!(fixture.column("pr_kind").as_deref(), Some("merged")); + } + #[test] fn a_pr_list_reads_the_newest_state() { let json = r#"[{"number":540,"state":"MERGED","headRefOid":"abc","baseRefName":"main"}]"#; diff --git a/crates/cli/src/sessions/store.rs b/crates/cli/src/sessions/store.rs index fa51e97bf..3bd29b847 100644 --- a/crates/cli/src/sessions/store.rs +++ b/crates/cli/src/sessions/store.rs @@ -78,8 +78,8 @@ 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. +/// What a lookup adds to a stored row: the pull request and, for a row a hook +/// never filled, its place. A `None` field leaves its column as it is. #[derive(Debug, Default, PartialEq, Eq)] pub(crate) struct CacheUpdate { pub pr_number: Option, @@ -87,6 +87,10 @@ pub(crate) struct CacheUpdate { pub pr_head_oid: Option, pub pr_base: Option, pub pr_checked_at: Option, + pub cwd: Option, + pub repo: Option, + pub worktree: Option, + pub convention: Option, } /// Stores `update` on the row of `id` in the database at `path`, in one @@ -104,7 +108,11 @@ pub(crate) fn write_back(path: &Path, id: &str, update: &CacheUpdate) -> Result< 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) \ + pr_base = COALESCE(?5, pr_base), pr_checked_at = COALESCE(?6, pr_checked_at), \ + repo = CASE WHEN cwd IS NULL THEN COALESCE(?8, repo) ELSE repo END, \ + worktree = CASE WHEN cwd IS NULL THEN COALESCE(?9, worktree) ELSE worktree END, \ + convention = CASE WHEN cwd IS NULL THEN COALESCE(?10, convention) ELSE convention END, \ + cwd = CASE WHEN cwd IS NULL THEN COALESCE(?7, cwd) ELSE cwd END \ WHERE session_id = ?1", params![ id, @@ -112,7 +120,11 @@ pub(crate) fn write_back(path: &Path, id: &str, update: &CacheUpdate) -> Result< update.pr_kind, update.pr_head_oid, update.pr_base, - update.pr_checked_at + update.pr_checked_at, + update.cwd, + update.repo, + update.worktree, + update.convention ], ) .map_err(|e| db_error("update", e))?; @@ -193,6 +205,8 @@ pub(crate) struct CachedPr { pub branch: Option, pub pr: Option, pub checked_at: Option, + /// Whether a hook already stored the place, so the lookup need not. + pub has_cwd: bool, } /// The cached pull requests of `ids` in the database at `path`. Empty when @@ -209,7 +223,7 @@ pub(crate) fn read_pr_cache( return cache; }; let Ok(mut stmt) = conn.prepare( - "SELECT branch, pr_number, pr_kind, pr_checked_at \ + "SELECT branch, pr_number, pr_kind, pr_checked_at, cwd IS NOT NULL \ FROM sessions WHERE session_id = ?1", ) else { return cache; @@ -226,6 +240,7 @@ pub(crate) fn read_pr_cache( .zip(kind.as_deref().and_then(pr_kind_of)) .map(|(number, kind)| Pr { number, kind }), checked_at: r.get(3)?, + has_cwd: r.get(4)?, }) }) .optional();