Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

refactor(agent): drop worktree discovery roots; the project store already maps worktrees

thinhlpg3e32bd55ad6b
diff --git a/src-tauri/src/core/agent/git.rs b/src-tauri/src/core/agent/git.rs
index 4178e005e..a020f1614 100644
--- a/src-tauri/src/core/agent/git.rs
+++ b/src-tauri/src/core/agent/git.rs
@@ -23,7 +23,9 @@
//! ref. The user's `git status`, current branch, and staged changes are never
//! touched. Shelling out keeps us free of a libgit2 dependency.
-use std::path::{Path, PathBuf};
+use std::path::Path;
+#[cfg(feature = "cli")]
+use std::path::PathBuf;
use std::process::Command;
@@ -138,37 +140,6 @@ pub(crate) fn current_branch(path: &Path) -> Option<String> {
.filter(|s| !s.is_empty() && s != "HEAD")
}
-/// The main worktree's root when `path` sits in a *linked* worktree, `None`
-/// when `path` is the main worktree itself (or not a git repo at all). Skills
-/// and plugins are installed under `<repo>/.jan/agent` in whatever checkout ran
-/// the install; a linked worktree has its own working tree but shares the
-/// repository, so agent setup should follow the repo, not the checkout.
-/// `--git-common-dir` points at the shared `<main>/.git`; the main worktree
-/// root is its parent. `--path-format=absolute` (git 2.31+) keeps the answer
-/// usable no matter which cwd git would otherwise resolve against.
-pub(crate) fn worktree_primary_root(path: &Path) -> Option<std::path::PathBuf> {
- let p = path.to_string_lossy();
- let common = git(&[
- "-C",
- &p,
- "rev-parse",
- "--path-format=absolute",
- "--git-common-dir",
- ])
- .ok()
- .map(|s| s.trim().to_string())
- .filter(|s| !s.is_empty())?;
- let primary = PathBuf::from(&common).parent()?.to_path_buf();
- // Only a linked worktree shares: the main worktree's common dir lives
- // *inside* its own root, so this filter also keeps callers from merging a
- // directory with itself (duplicate entries).
- if primary == path {
- None
- } else {
- Some(primary)
- }
-}
-
/// The canonical empty tree object every git repo has, without needing a
/// commit to hash it from -- used as the base tree when `HEAD` is unborn.
#[cfg(feature = "cli")]
@@ -630,23 +601,4 @@ mod tests {
let _ = std::fs::remove_dir_all(&root);
}
-
- /// A linked worktree shares the repo's common dir but has its own working
- /// tree: `worktree_primary_root` must resolve to the main checkout there,
- /// and to `None` in the main checkout itself (no self-merge).
- #[test]
- fn worktree_primary_root_resolves_linked_worktrees() {
- let Some(root) = init_repo() else { return };
- assert_eq!(worktree_primary_root(&root), None, "main worktree has no primary");
-
- let linked = std::env::temp_dir().join(format!("jan_wt_linked_{}", COUNTER.fetch_add(1, Ordering::SeqCst)));
- let r = root.to_string_lossy().to_string();
- let l = linked.to_string_lossy().to_string();
- git(&["-C", &r, "worktree", "add", &l, "-b", "jan-test-linked"])
- .expect("add worktree");
- assert_eq!(worktree_primary_root(&linked).as_deref(), Some(root.as_path()));
-
- let _ = std::fs::remove_dir_all(&linked);
- let _ = std::fs::remove_dir_all(&root);
- }
}
diff --git a/src-tauri/src/core/agent/plugins.rs b/src-tauri/src/core/agent/plugins.rs
index 99606ef8b..b0e822b09 100644
--- a/src-tauri/src/core/agent/plugins.rs
+++ b/src-tauri/src/core/agent/plugins.rs
@@ -2016,7 +2016,10 @@ mod tests {
/// one required env var, plus the store dir tests use instead of `~/.jan`.
fn plugin_with_env_requirement(tag: &str, var: &str, url: &str) -> (PathBuf, PathBuf) {
let root = unique_root(tag);
- let dir = root.join(".jan/agent/plugins/acme");
+ // Create the root first: the store is keyed on the canonical path,
+ // which differs from the raw temp path on macOS (/var -> /private/var).
+ std::fs::create_dir_all(&root).unwrap();
+ let dir = crate::core::agent::skills::plugins_dir(&root).join("acme");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("plugin.toml"),
@@ -2121,7 +2124,8 @@ mod tests {
let unrelated = temp.path().join("unrelated");
std::fs::create_dir_all(&unrelated).unwrap();
for (root, value) in [(&first, "first-secret"), (&second, "second-secret")] {
- let plugin = root.join(".jan/agent/plugins/acme");
+ std::fs::create_dir_all(root).unwrap();
+ let plugin = crate::core::agent::skills::plugins_dir(root).join("acme");
std::fs::create_dir_all(&plugin).unwrap();
std::fs::write(
plugin.join("plugin.toml"),
@@ -2146,7 +2150,7 @@ mod tests {
assert_eq!(String::from_utf8(output.stdout).unwrap(), expected);
}
// Dropping a manifest requirement must revoke the next shell's key.
- std::fs::write(first.join(".jan/agent/plugins/acme/plugin.toml"), "name = \"acme\"\n").unwrap();
+ std::fs::write(crate::core::agent::skills::plugins_dir(&first).join("acme").join("plugin.toml"), "name = \"acme\"\n").unwrap();
sync_env_registry_in(&first, &first.join("key-store"));
let child = proc::spawn(
proc::shell(), "printf '%s' \"${JAN_PLUGIN_ISOLATION_TOKEN-unset}\"", &first, None,
diff --git a/src-tauri/src/core/agent/skills.rs b/src-tauri/src/core/agent/skills.rs
index deae66015..25325af6b 100644
--- a/src-tauri/src/core/agent/skills.rs
+++ b/src-tauri/src/core/agent/skills.rs
@@ -226,16 +226,12 @@ pub(crate) fn plugins_dir(root: &Path) -> PathBuf {
crate::core::agent::project::store_root(root).join("plugins")
}
-/// Project roots whose stores this project sees, in shadowing order:
-/// the project root first, then the main worktree root when the project is a
-/// linked git worktree. Skills and plugins install into whichever checkout ran
-/// the install; a linked worktree shares the repository but not the working
-/// tree, so discovery follows the repo, not the checkout. Earlier roots win:
-/// a project-local entry shadows a same-named shared one.
+/// Roots whose plugin and skill stores this project sees. Only the project
+/// itself: [`crate::core::agent::project::store_root`] already maps a linked
+/// git worktree onto its main checkout's store, so worktrees share skills and
+/// plugins without a second root (or a `git` call per lookup).
pub(crate) fn discovery_roots(root: &Path) -> Vec<PathBuf> {
- let mut roots = vec![root.to_path_buf()];
- roots.extend(crate::core::agent::git::worktree_primary_root(root));
- roots
+ vec![root.to_path_buf()]
}
/// Locate an installed plugin's directory by name across the discovery roots
@@ -1301,8 +1297,7 @@ mod tests {
}
/// Skills and plugins installed in the main worktree are visible from a
- /// linked worktree (agent setup follows the repo, not the checkout), and a
- /// project-local skill shadows a same-named shared one.
+ /// linked worktree: the store follows the repo, not the checkout.
#[test]
fn linked_worktree_sees_main_worktree_skills_and_plugins() {
let Some((main, linked)) = repo_with_linked_worktree("discover") else {
@@ -1324,20 +1319,10 @@ mod tests {
plugins.iter().map(qualified_name).collect::<Vec<_>>(),
vec!["shared-plugin:prep".to_string()]
);
- // Reading a shared plugin skill resolves through the linked root too.
+ // Both checkouts resolve to one store, so the file lives in it.
let entry = resolve_readable(&linked, "shared-plugin:prep").unwrap();
- assert!(entry.file.starts_with(&main));
-
- // Project-local shadows shared: the linked worktree's own copy wins.
- project_skill(&linked, "shared", "local\n");
- let entry = discover(&linked)
- .into_iter()
- .find(|e| e.name == "shared")
- .unwrap();
- assert!(entry.file.starts_with(&linked), "local must win");
-
- // The main worktree does not merge with itself (no duplicates).
- assert_eq!(discover(&main).iter().filter(|e| e.name == "shared").count(), 1);
+ assert!(entry.file.starts_with(plugins_dir(&main)), "{:?}", entry.file);
+ assert_eq!(plugins_dir(&linked), plugins_dir(&main));
let _ = std::fs::remove_dir_all(&main);
let _ = std::fs::remove_dir_all(&linked);
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 7e1254eac..b3a2ae61b 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -38055,7 +38055,7 @@ mod tests {
/// A plugin whose manifest declares one required env var.
fn plugin_with_setup_requirement(root: &std::path::Path, var: &str, url: &str) {
- let dir = root.join(".jan/agent/plugins/acme");
+ let dir = crate::core::agent::skills::plugins_dir(root).join("acme");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("plugin.toml"),
@@ -38071,7 +38071,7 @@ mod tests {
("beta", "shared", "Second"),
("gamma", "beta", "Third"),
] {
- let dir = root.join(".jan/agent/plugins").join(directory);
+ let dir = crate::core::agent::skills::plugins_dir(&root).join(directory);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("plugin.toml"),
@@ -38103,7 +38103,7 @@ mod tests {
("figma", "Canvas design tools with a long description that must not hide the plugin name"),
("qjk-tools", "Keyboard tools"),
] {
- let dir = root.join(".jan/agent/plugins").join(name);
+ let dir = crate::core::agent::skills::plugins_dir(&root).join(name);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("plugin.toml"),
@@ -38159,7 +38159,7 @@ mod tests {
async fn plugin_setup_without_name_waits_for_one_selection() {
let (mut app, root) = skill_test_app("deploy", "How to deploy.");
for name in ["alpha", "example-plugin", "figma"] {
- let dir = root.join(".jan/agent/plugins").join(name);
+ let dir = crate::core::agent::skills::plugins_dir(&root).join(name);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("plugin.toml"), format!("name = \"{name}\"\n")).unwrap();
if name != "alpha" {
@@ -38241,7 +38241,7 @@ mod tests {
rt().block_on(run_command(&mut app, "plugin setup design", &no_mcp()));
assert!(transcript_text(&app).contains("is not installed"));
assert!(app.plugin_setup.is_none());
- let dir = root.join(".jan/agent/plugins/design");
+ let dir = crate::core::agent::skills::plugins_dir(&root).join("design");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("plugin.toml"), "name = \"design\"\n").unwrap();
std::fs::write(
diff --git a/src-tauri/src/core/mcp/oauth.rs b/src-tauri/src/core/mcp/oauth.rs
index c861d9a8a..dcdb2c4ea 100644
--- a/src-tauri/src/core/mcp/oauth.rs
+++ b/src-tauri/src/core/mcp/oauth.rs
@@ -729,8 +729,16 @@ mod tests {
"access_token": if refreshed { "refreshed" } else { "issued" },
"token_type": "Bearer", "expires_in": 3600, "refresh_token": "refresh",
}).to_string())
+ } else if bytes.starts_with(b"GET /mcp ") {
+ // A 200 here is read as the RFC 9728 protected-resource
+ // document, whose `resource` rmcp 3.2 requires to match.
+ (200, json!({
+ "resource": format!("{origin}/mcp"),
+ "authorization_servers": [origin.clone()],
+ }).to_string())
} else {
(200, json!({
+ "issuer": origin.clone(),
"authorization_endpoint": format!("{origin}/authorize"),
"token_endpoint": format!("{origin}/token"),
"registration_endpoint": format!("{origin}/register"),