From eaf51fc734d055daea59af3b42c9fee436b96ad1 Mon Sep 17 00:00:00 2001 From: Robb Walters Date: Fri, 20 Nov 2026 07:43:57 +0000 Subject: [PATCH 1/2] fix: stop refusing private seats whose only credential is the daemon-owned App gh-config dir The ancestor-.git refusal in start_with fired for the daemon itself on an App-only host: credential_startup activates /.loom/gh-config as the process-global GH_CONFIG_DIR, and the workspace is a checkout, so every unattended session_reconcile pass refused to create a seat (#14744). Resolve the config dir through resolve_gh_config, which recognises the daemon-owned App directories under the workspace's own .loom/ (the primary gh-config and per-owner gh-config-by-owner/) and records None for them: the seat keeps no bind mount of the directory, and its token already arrives per exec as GH_TOKEN (forge_auth, #13916). Any other directory at or under a git checkout keeps the existing refusal. Closes #14744 --- .../private_workspace/lifecycle.rs | 80 ++++++++++++++++--- .../tokens_pool/private_workspace/tests.rs | 77 ++++++++++++++++++ 2 files changed, 144 insertions(+), 13 deletions(-) diff --git a/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs b/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs index ec7ba9bd2f..4714091a3e 100644 --- a/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs +++ b/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs @@ -211,19 +211,7 @@ fn start_with( register(workspace, &mut existing, &repository, base)?; existing } else { - let gh_config = std::env::var_os("GH_CONFIG_DIR") - .map(PathBuf::from) - .or_else(|| dirs::home_dir().map(|home| home.join(".config/gh"))) - .filter(|path| path.is_dir()) - .map(|path| path.canonicalize()) - .transpose()?; - if let Some(path) = &gh_config { - for parent in path.ancestors() { - if parent.join(".git").exists() { - bail!("forge credentials must be outside every repository/worktree"); - } - } - } + let gh_config = resolve_gh_config(workspace)?; Config { schema_version: 1, account: account.id.name.clone(), @@ -332,6 +320,72 @@ fn start_with( snapshot(config, &dir) } +/// The forge configuration directory a NEW private workspace records and +/// binds read-only at [`GH_CONFIG`], or `None` when none may be mounted. +/// +/// The daemon-owned GitHub App directories under the workspace root's own +/// `.loom/` are deliberately not recorded ([`daemon_owned_gh_config`]): an +/// App-only host delivers its installation token per exec as `GH_TOKEN` +/// (`forge_auth`, #13916), so binding the directory would only re-expose +/// the credential inside the seat — and it lives inside the daemon's own +/// checkout, which the ancestor check below rightly refuses. Before #14744 +/// that refusal kept unattended `session_reconcile` from ever creating a +/// seat on an App-only host, because the daemon activates that directory +/// as its process-global `GH_CONFIG_DIR` at startup. Every other directory +/// at or under a git checkout keeps the refusal. +fn resolve_gh_config(workspace: &Path) -> Result> { + resolve_gh_config_with( + workspace, + std::env::var_os("GH_CONFIG_DIR"), + dirs::home_dir().map(|home| home.join(".config/gh")), + ) +} + +/// [`resolve_gh_config`] over an injected `GH_CONFIG_DIR` value and +/// home-default directory (tests): the same resolution and the same +/// ancestor refusal for every directory the daemon does not own. +pub(super) fn resolve_gh_config_with( + workspace: &Path, + gh_config_dir: Option, + home_default: Option, +) -> Result> { + let resolved = gh_config_dir + .map(PathBuf::from) + .or(home_default) + .filter(|path| path.is_dir()) + .map(|path| path.canonicalize()) + .transpose()? + .filter(|path| !daemon_owned_gh_config(workspace, path)); + if let Some(path) = &resolved { + for parent in path.ancestors() { + if parent.join(".git").exists() { + bail!("forge credentials must be outside every repository/worktree"); + } + } + } + Ok(resolved) +} + +/// `true` when `path` (already canonicalized) is one of the daemon-owned +/// GitHub App credential directories under `workspace`'s own `.loom/`: the +/// process-global `GH_CONFIG_DIR` the daemon activates at startup +/// ([`crate::credential_preflight::github_app_gh_config_dir`]) or a +/// per-owner sibling ([`github_app_gh_config_dir_for_owner`]). A symlinked +/// spelling of either resolves to the same canonical path and matches; a +/// workspace that cannot be resolved owns nothing, so the caller's refusal +/// stands. +fn daemon_owned_gh_config(workspace: &Path, path: &Path) -> bool { + let Some(root) = workspace.canonicalize().ok() else { + return false; + }; + if path == crate::credential_preflight::github_app_gh_config_dir(&root) { + return true; + } + let owners = root.join(".loom").join("gh-config-by-owner"); + path.strip_prefix(&owners) + .is_ok_and(|rest| rest.components().count() == 1) +} + /// Unattended recovery of an already-configured private session whose /// container is stopped or gone (#12772). Unlike [`start`] it never provisions: /// it resolves the persisted identity, resumes a stopped container or recreates diff --git a/loom-daemon/src/tokens_pool/private_workspace/tests.rs b/loom-daemon/src/tokens_pool/private_workspace/tests.rs index c990787e88..7c886d1ca4 100644 --- a/loom-daemon/src/tokens_pool/private_workspace/tests.rs +++ b/loom-daemon/src/tokens_pool/private_workspace/tests.rs @@ -728,6 +728,83 @@ fn https_remote_rejects_credentials_and_unsupported_transports() { } } +#[test] +fn daemon_owned_gh_config_dir_under_a_checkout_is_not_recorded() { + let ws = tempfile::tempdir().expect("tempdir"); + let dir = crate::credential_preflight::github_app_gh_config_dir(ws.path()); + std::fs::create_dir_all(&dir).expect("app dir"); + std::fs::create_dir_all(ws.path().join(".git")).expect("daemon checkout"); + assert!(matches!( + lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None), + Ok(None) + )); +} + +#[test] +fn a_per_owner_daemon_gh_config_dir_under_a_checkout_is_not_recorded() { + let ws = tempfile::tempdir().expect("tempdir"); + let dir = crate::credential_preflight::github_app_gh_config_dir_for_owner(ws.path(), "org"); + std::fs::create_dir_all(&dir).expect("owner dir"); + std::fs::create_dir_all(ws.path().join(".git")).expect("daemon checkout"); + assert!(matches!( + lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None), + Ok(None) + )); +} + +#[test] +fn a_symlinked_spelling_of_the_daemon_owned_gh_config_dir_is_not_recorded() { + use std::os::unix::fs::symlink; + let ws = tempfile::tempdir().expect("tempdir"); + let dir = crate::credential_preflight::github_app_gh_config_dir(ws.path()); + std::fs::create_dir_all(&dir).expect("app dir"); + symlink(&dir, ws.path().join("linked-gh-config")).expect("symlink"); + let linked = ws.path().join("linked-gh-config"); + assert!(matches!( + lifecycle::resolve_gh_config_with(ws.path(), Some(linked.into_os_string()), None), + Ok(None) + )); +} + +#[test] +fn a_gh_config_dir_under_a_checkout_the_daemon_does_not_own_is_refused() { + let ws = tempfile::tempdir().expect("tempdir"); + std::fs::create_dir_all(ws.path().join(".git")).expect("checkout"); + let dir = ws.path().join("credentials/gh"); + std::fs::create_dir_all(&dir).expect("gh dir"); + let error = lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None) + .expect_err("a foreign dir under a checkout must be refused"); + assert!(error + .to_string() + .contains("forge credentials must be outside every repository/worktree")); +} + +#[test] +fn a_home_default_gh_dir_under_a_checkout_is_refused() { + let ws = tempfile::tempdir().expect("tempdir"); + std::fs::create_dir_all(ws.path().join(".git")).expect("checkout"); + let home_default = ws.path().join(".config/gh"); + std::fs::create_dir_all(&home_default).expect("gh dir"); + let error = lifecycle::resolve_gh_config_with(ws.path(), None, Some(home_default)) + .expect_err("the home-default dir under a checkout must be refused"); + assert!(error + .to_string() + .contains("forge credentials must be outside every repository/worktree")); +} + +#[test] +fn a_gh_config_dir_outside_every_checkout_is_recorded() { + let ws = tempfile::tempdir().expect("tempdir"); + let elsewhere = tempfile::tempdir().expect("tempdir"); + let dir = elsewhere.path().join("gh"); + std::fs::create_dir_all(&dir).expect("gh dir"); + assert_eq!( + lifecycle::resolve_gh_config_with(ws.path(), Some(dir.clone().into_os_string()), None) + .expect("an ordinary dir is recorded"), + Some(dir.canonicalize().expect("canonical")) + ); +} + fn config() -> Config { Config { schema_version: 1, From 0bbf0a36b3807a606632f07ff96523870cb38923 Mon Sep 17 00:00:00 2001 From: "loom-fleet-dispatch[bot]" <313063438+loom-fleet-dispatch[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 08:48:35 +0000 Subject: [PATCH 2/2] fix: treat the primary workspace App dir as daemon-owned and compare canonical locations Co-Authored-By: Claude Sonnet 5.5 --- .../private_workspace/lifecycle.rs | 45 ++++++----- .../tokens_pool/private_workspace/tests.rs | 81 +++++++++++++++++-- 2 files changed, 100 insertions(+), 26 deletions(-) diff --git a/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs b/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs index 4714091a3e..25d78be765 100644 --- a/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs +++ b/loom-daemon/src/tokens_pool/private_workspace/lifecycle.rs @@ -338,6 +338,7 @@ fn resolve_gh_config(workspace: &Path) -> Result> { workspace, std::env::var_os("GH_CONFIG_DIR"), dirs::home_dir().map(|home| home.join(".config/gh")), + crate::credential_preflight::primary_workspace_root(), ) } @@ -348,6 +349,7 @@ pub(super) fn resolve_gh_config_with( workspace: &Path, gh_config_dir: Option, home_default: Option, + primary_root: Option, ) -> Result> { let resolved = gh_config_dir .map(PathBuf::from) @@ -355,7 +357,7 @@ pub(super) fn resolve_gh_config_with( .filter(|path| path.is_dir()) .map(|path| path.canonicalize()) .transpose()? - .filter(|path| !daemon_owned_gh_config(workspace, path)); + .filter(|path| !daemon_owned_gh_config(workspace, primary_root.as_deref(), path)); if let Some(path) = &resolved { for parent in path.ancestors() { if parent.join(".git").exists() { @@ -367,23 +369,30 @@ pub(super) fn resolve_gh_config_with( } /// `true` when `path` (already canonicalized) is one of the daemon-owned -/// GitHub App credential directories under `workspace`'s own `.loom/`: the -/// process-global `GH_CONFIG_DIR` the daemon activates at startup -/// ([`crate::credential_preflight::github_app_gh_config_dir`]) or a -/// per-owner sibling ([`github_app_gh_config_dir_for_owner`]). A symlinked -/// spelling of either resolves to the same canonical path and matches; a -/// workspace that cannot be resolved owns nothing, so the caller's refusal -/// stands. -fn daemon_owned_gh_config(workspace: &Path, path: &Path) -> bool { - let Some(root) = workspace.canonicalize().ok() else { - return false; - }; - if path == crate::credential_preflight::github_app_gh_config_dir(&root) { - return true; - } - let owners = root.join(".loom").join("gh-config-by-owner"); - path.strip_prefix(&owners) - .is_ok_and(|rest| rest.components().count() == 1) +/// GitHub App credential directories: the process-global `GH_CONFIG_DIR` the +/// daemon activates at startup +/// ([`crate::credential_preflight::github_app_gh_config_dir`]) or a per-owner +/// sibling ([`github_app_gh_config_dir_for_owner`]), under either the daemon's +/// primary workspace root (the credential owner: startup config belongs to it, +/// whichever registered checkout acts) or the acting `workspace`. +/// +/// Both sides are compared canonicalized: `path` already is, so each expected +/// location is canonicalized too (an aliased `.loom` or `gh-config` resolves +/// to the same place). A root that cannot be resolved owns nothing, so the +/// caller's refusal stands. +fn daemon_owned_gh_config(workspace: &Path, primary_root: Option<&Path>, path: &Path) -> bool { + std::iter::once(workspace) + .chain(primary_root) + .filter_map(|root| root.canonicalize().ok()) + .any(|root| { + let canon = |p: PathBuf| p.canonicalize().unwrap_or(p); + if path == canon(crate::credential_preflight::github_app_gh_config_dir(&root)) { + return true; + } + let owners = canon(root.join(".loom").join("gh-config-by-owner")); + path.strip_prefix(&owners) + .is_ok_and(|rest| rest.components().count() == 1) + }) } /// Unattended recovery of an already-configured private session whose diff --git a/loom-daemon/src/tokens_pool/private_workspace/tests.rs b/loom-daemon/src/tokens_pool/private_workspace/tests.rs index 7c886d1ca4..b12ebf9124 100644 --- a/loom-daemon/src/tokens_pool/private_workspace/tests.rs +++ b/loom-daemon/src/tokens_pool/private_workspace/tests.rs @@ -735,7 +735,7 @@ fn daemon_owned_gh_config_dir_under_a_checkout_is_not_recorded() { std::fs::create_dir_all(&dir).expect("app dir"); std::fs::create_dir_all(ws.path().join(".git")).expect("daemon checkout"); assert!(matches!( - lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None), + lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None), Ok(None) )); } @@ -747,7 +747,7 @@ fn a_per_owner_daemon_gh_config_dir_under_a_checkout_is_not_recorded() { std::fs::create_dir_all(&dir).expect("owner dir"); std::fs::create_dir_all(ws.path().join(".git")).expect("daemon checkout"); assert!(matches!( - lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None), + lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None), Ok(None) )); } @@ -761,7 +761,7 @@ fn a_symlinked_spelling_of_the_daemon_owned_gh_config_dir_is_not_recorded() { symlink(&dir, ws.path().join("linked-gh-config")).expect("symlink"); let linked = ws.path().join("linked-gh-config"); assert!(matches!( - lifecycle::resolve_gh_config_with(ws.path(), Some(linked.into_os_string()), None), + lifecycle::resolve_gh_config_with(ws.path(), Some(linked.into_os_string()), None, None), Ok(None) )); } @@ -772,8 +772,9 @@ fn a_gh_config_dir_under_a_checkout_the_daemon_does_not_own_is_refused() { std::fs::create_dir_all(ws.path().join(".git")).expect("checkout"); let dir = ws.path().join("credentials/gh"); std::fs::create_dir_all(&dir).expect("gh dir"); - let error = lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None) - .expect_err("a foreign dir under a checkout must be refused"); + let error = + lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None) + .expect_err("a foreign dir under a checkout must be refused"); assert!(error .to_string() .contains("forge credentials must be outside every repository/worktree")); @@ -785,7 +786,7 @@ fn a_home_default_gh_dir_under_a_checkout_is_refused() { std::fs::create_dir_all(ws.path().join(".git")).expect("checkout"); let home_default = ws.path().join(".config/gh"); std::fs::create_dir_all(&home_default).expect("gh dir"); - let error = lifecycle::resolve_gh_config_with(ws.path(), None, Some(home_default)) + let error = lifecycle::resolve_gh_config_with(ws.path(), None, Some(home_default), None) .expect_err("the home-default dir under a checkout must be refused"); assert!(error .to_string() @@ -799,12 +800,76 @@ fn a_gh_config_dir_outside_every_checkout_is_recorded() { let dir = elsewhere.path().join("gh"); std::fs::create_dir_all(&dir).expect("gh dir"); assert_eq!( - lifecycle::resolve_gh_config_with(ws.path(), Some(dir.clone().into_os_string()), None) - .expect("an ordinary dir is recorded"), + lifecycle::resolve_gh_config_with( + ws.path(), + Some(dir.clone().into_os_string()), + None, + None + ) + .expect("an ordinary dir is recorded"), Some(dir.canonicalize().expect("canonical")) ); } +#[test] +fn the_primary_workspace_app_dir_is_not_recorded_for_a_distinct_acting_checkout() { + let primary = tempfile::tempdir().expect("tempdir"); + let acting = tempfile::tempdir().expect("tempdir"); + let dir = crate::credential_preflight::github_app_gh_config_dir(primary.path()); + std::fs::create_dir_all(&dir).expect("app dir"); + std::fs::create_dir_all(primary.path().join(".git")).expect("daemon checkout"); + std::fs::create_dir_all(acting.path().join(".git")).expect("acting checkout"); + let primary_root = Some(primary.path().to_path_buf()); + assert!(matches!( + lifecycle::resolve_gh_config_with( + acting.path(), + Some(dir.clone().into_os_string()), + None, + primary_root + ), + Ok(None) + )); + // Without the primary root the acting checkout does not own it: refused. + assert!(lifecycle::resolve_gh_config_with( + acting.path(), + Some(dir.into_os_string()), + None, + None + ) + .is_err()); +} + +#[test] +fn an_aliased_app_dir_location_is_not_recorded() { + use std::os::unix::fs::symlink; + let ws = tempfile::tempdir().expect("tempdir"); + let real = ws.path().join("real-gh-config"); + std::fs::create_dir_all(&real).expect("real dir"); + std::fs::create_dir_all(ws.path().join(".loom")).expect(".loom"); + symlink(&real, ws.path().join(".loom/gh-config")).expect("symlink"); + std::fs::create_dir_all(ws.path().join(".git")).expect("checkout"); + let dir = crate::credential_preflight::github_app_gh_config_dir(ws.path()); + assert!(matches!( + lifecycle::resolve_gh_config_with(ws.path(), Some(dir.into_os_string()), None, None), + Ok(None) + )); +} + +#[test] +fn an_unrelated_dir_under_the_primary_checkout_is_still_refused() { + let primary = tempfile::tempdir().expect("tempdir"); + std::fs::create_dir_all(primary.path().join(".git")).expect("checkout"); + let dir = primary.path().join("credentials/gh"); + std::fs::create_dir_all(&dir).expect("gh dir"); + assert!(lifecycle::resolve_gh_config_with( + primary.path(), + Some(dir.into_os_string()), + None, + Some(primary.path().to_path_buf()) + ) + .is_err()); +} + fn config() -> Config { Config { schema_version: 1,