garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

cartopolis-agent · 2026-11-26T11:48:11Z

fix(sessions): a project added to a session leaves main when its base is gone

A session started on another session's branch (s855 on session/s854)
keeps that branch as its base, and `add-project` fetched it for every
project added later: once that session had landed and its branch was
deleted, or for a project that never had it, the fetch failed and no
project could be added ("couldn't find remote ref session/s854"). A base
that is not the session's own branch and that origin does not have now
gives way to the project's main, where the work it carried has landed.
src/sessions.rs | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/sessions.rs b/src/sessions.rs
index e7e87a5..49721d9 100644
--- a/src/sessions.rs
+++ b/src/sessions.rs
@@ -3308,12 +3308,29 @@ fn project_worktree(home: &Path, dir: &Path, id: &str, repo: &str, base: &str) -
         work = dir.join(repo.replace('/', "-"));
     }
     let checkout = checkout(home, Some(repo))?;
-    let base = project_base(repo, base);
+    let mut base = project_base(repo, base);
+    // Another session's branch it was started on: gone from origin once that session
+    // landed (s855 on s854's, 2026-11-26), and never in a project added after, which
+    // built on another project's. Then it leaves the project's main, where that work is.
+    if base != format!("session/{id}") && !on_origin(&checkout, &base) {
+        base = project_base(repo, "main");
+    }
     add_worktree(&checkout, &work, id, &base)?;
     link_skills(dir, &work);
     Ok((work, base))
 }
 
+/// Whether `origin` of the checkout `repo` has branch `branch`.
+fn on_origin(repo: &Path, branch: &str) -> bool {
+    Command::new("git")
+        .arg("-C")
+        .arg(repo)
+        .args(["ls-remote", "--exit-code", "--heads", "origin", branch])
+        .stdin(Stdio::null())
+        .output()
+        .is_ok_and(|o| o.status.success())
+}
+
 /// The branch project `repo`'s worktree leaves and is compared with: the
 /// session's `base`, unless that is the default `main` and the project lands
 /// on another (hub.conf `projects.<repo>.main`, such as a deck's