-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Stable project identity from git remote + default remember to project scope #738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
059e41e
739b629
a2da423
a83618a
90640b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,10 +2,7 @@ | |||||
| import { execSync } from "node:child_process"; | ||||||
| import { basename } from "node:path"; | ||||||
| //#region src/hooks/_project.ts | ||||||
| function resolveProject(cwd) { | ||||||
| const explicit = process.env["AGENTMEMORY_PROJECT_NAME"]; | ||||||
| if (explicit && explicit.trim()) return explicit.trim(); | ||||||
| const dir = cwd && cwd.trim() ? cwd : process.cwd(); | ||||||
| function gitToplevelBasename(dir) { | ||||||
| try { | ||||||
| const top = execSync("git rev-parse --show-toplevel", { | ||||||
| cwd: dir, | ||||||
|
|
@@ -16,9 +13,60 @@ function resolveProject(cwd) { | |||||
| ], | ||||||
| timeout: 500 | ||||||
| }).toString().trim(); | ||||||
| if (top) return basename(top); | ||||||
| } catch {} | ||||||
| return basename(dir); | ||||||
| return top ? basename(top) : null; | ||||||
| } catch { | ||||||
| return null; | ||||||
| } | ||||||
| } | ||||||
| function normalizeGitRemote(url) { | ||||||
| const raw = (url ?? "").trim(); | ||||||
| if (!raw) return null; | ||||||
| let host = ""; | ||||||
| let path = ""; | ||||||
| const scp = raw.match(/^[^@/]+@([^:/]+):(.+)$/); | ||||||
| if (scp) { | ||||||
| host = scp[1]; | ||||||
| path = scp[2]; | ||||||
| } else { | ||||||
| const noCreds = raw.replace(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//, "").replace(/^[^@/]*@/, ""); | ||||||
| const slash = noCreds.indexOf("/"); | ||||||
| if (slash === -1) return null; | ||||||
| host = noCreds.slice(0, slash); | ||||||
| path = noCreds.slice(slash + 1); | ||||||
| } | ||||||
| host = host.toLowerCase().replace(/:\d+$/, ""); | ||||||
| path = path.replace(/^\/+/, "").replace(/\/+$/, "").replace(/\.git$/i, "").toLowerCase(); | ||||||
| if (!host || !path) return null; | ||||||
| return `${host}/${path}`; | ||||||
| } | ||||||
| function gitRemoteIdentity(dir) { | ||||||
| try { | ||||||
| return normalizeGitRemote(execSync("git config --get remote.origin.url", { | ||||||
| cwd: dir, | ||||||
| stdio: [ | ||||||
| "ignore", | ||||||
| "pipe", | ||||||
| "ignore" | ||||||
| ], | ||||||
| timeout: 500 | ||||||
| }).toString().trim()); | ||||||
| } catch { | ||||||
| return null; | ||||||
| } | ||||||
| } | ||||||
| function remoteIdentityEnabled() { | ||||||
| const flag = process.env["AGENTMEMORY_PROJECT_FROM_REMOTE"]; | ||||||
| return flag === "1" || flag === "true"; | ||||||
| } | ||||||
| function resolveProject(cwd) { | ||||||
| const explicit = process.env["AGENTMEMORY_PROJECT_NAME"]; | ||||||
| if (explicit && explicit.trim()) return explicit.trim(); | ||||||
| const dir = cwd && cwd.trim() ? cwd : process.cwd(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard Line 65 can throw when Suggested patch- const dir = cwd && cwd.trim() ? cwd : process.cwd();
+ const dir = typeof cwd === "string" && cwd.trim() ? cwd : process.cwd();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| if (remoteIdentityEnabled()) { | ||||||
| const id = gitRemoteIdentity(dir); | ||||||
| if (id) return id; | ||||||
| } | ||||||
| return gitToplevelBasename(dir) ?? basename(dir); | ||||||
| } | ||||||
| function hookCwd(data) { | ||||||
| if (!data || typeof data !== "object") return void 0; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return
nullfor local-path remotes instead of treating them as canonical IDs.With
AGENTMEMORY_PROJECT_FROM_REMOTE=1, a local origin likeC:/work/repo.gitcurrently normalizes toc:/work/repo, which is machine-specific and reintroduces the fragmentation this feature is meant to eliminate. These cases should fail normalization soresolveProject()falls back to the git toplevel/cwd basename.Suggested fix
function normalizeGitRemote(url) { const raw = (url ?? "").trim(); if (!raw) return null; + if (/^(file:|\/|\.{1,2}[\\/]|[A-Za-z]:[\\/]|\\\\)/.test(raw)) return null; let host = ""; let path = ""; const scp = raw.match(/^[^`@/`]+@([^:/]+):(.+)$/); if (scp) { host = scp[1];📝 Committable suggestion
🤖 Prompt for AI Agents