-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(hooks): scope pre-tool-use enrichment to the current project #1220
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
Open
devon3000
wants to merge
1
commit into
rohitg00:main
Choose a base branch
from
devon3000:fix/enrich-project-scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { describe, it, expect, beforeAll, afterAll } from "vitest"; | ||
| import { spawn } from "node:child_process"; | ||
| import { createServer, type Server } from "node:http"; | ||
| import { execFileSync } from "node:child_process"; | ||
| import { mkdtempSync, mkdirSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| // Claude Code's PreToolUse payload has no `project` field — only session_id, | ||
| // cwd, tool_name and tool_input. The hook used to read data.project only, so | ||
| // every /agentmemory/enrich call went out unscoped and mem::enrich searched | ||
| // the whole corpus, injecting other projects' observations into this | ||
| // project's tool turns. It must resolve the project from cwd like every | ||
| // other project-aware hook. | ||
|
|
||
| let server: Server; | ||
| let port: number; | ||
| let posts: Array<{ path: string; body: Record<string, unknown> }> = []; | ||
|
|
||
| const REPO_NAME = "amem-pretool-fixture"; | ||
|
|
||
| function runHook( | ||
| payload: Record<string, unknown>, | ||
| env: Record<string, string> = {}, | ||
| ): Promise<number> { | ||
| return new Promise((resolve) => { | ||
| const child = spawn("node", ["plugin/scripts/pre-tool-use.mjs"], { | ||
| env: { | ||
| ...process.env, | ||
| AGENTMEMORY_URL: `http://127.0.0.1:${port}`, | ||
| AGENTMEMORY_INJECT_CONTEXT: "true", | ||
| // Keep the fixture's basename as the identity regardless of whether | ||
| // the developer running the suite has remote-identity mode on. | ||
| AGENTMEMORY_PROJECT_FROM_REMOTE: "0", | ||
| AGENTMEMORY_PROJECT_NAME: "", | ||
| ...env, | ||
| }, | ||
| }); | ||
| child.on("exit", (code) => resolve(code ?? 1)); | ||
| child.stdin.write(JSON.stringify(payload)); | ||
| child.stdin.end(); | ||
| }); | ||
| } | ||
|
|
||
| describe("pre-tool-use enrich is project-scoped", () => { | ||
| let tmpRoot: string; | ||
| let repoDir: string; | ||
| let nestedDir: string; | ||
|
|
||
| beforeAll(async () => { | ||
| tmpRoot = mkdtempSync(join(tmpdir(), "am-pretool-")); | ||
| repoDir = join(tmpRoot, REPO_NAME); | ||
| nestedDir = join(repoDir, "src", "deep"); | ||
| mkdirSync(nestedDir, { recursive: true }); | ||
| execFileSync("git", ["init", "--quiet"], { cwd: repoDir, stdio: "ignore" }); | ||
|
|
||
| server = createServer((req, res) => { | ||
| let body = ""; | ||
| req.on("data", (c) => (body += c)); | ||
| req.on("end", () => { | ||
| try { | ||
| posts.push({ path: req.url ?? "", body: JSON.parse(body || "{}") }); | ||
| } catch { | ||
| posts.push({ path: req.url ?? "", body: {} }); | ||
| } | ||
| res.writeHead(200, { "Content-Type": "application/json" }); | ||
| res.end(JSON.stringify({ context: "" })); | ||
| }); | ||
| }); | ||
| await new Promise<void>((r) => server.listen(0, "127.0.0.1", r)); | ||
| port = (server.address() as { port: number }).port; | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| server.close(); | ||
| rmSync(tmpRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("derives project from the payload cwd", async () => { | ||
| posts = []; | ||
| await runHook({ | ||
| session_id: "s1", | ||
| cwd: repoDir, | ||
| tool_name: "Read", | ||
| tool_input: { file_path: join(repoDir, "src", "thing.ts") }, | ||
| }); | ||
|
|
||
| expect(posts.length).toBe(1); | ||
| expect(posts[0]!.path).toContain("/agentmemory/enrich"); | ||
| expect(posts[0]!.body.project).toBe(REPO_NAME); | ||
| }); | ||
|
|
||
| it("resolves to the git toplevel from a nested cwd", async () => { | ||
| posts = []; | ||
| await runHook({ | ||
| session_id: "s2", | ||
| cwd: nestedDir, | ||
| tool_name: "Edit", | ||
| tool_input: { file_path: join(nestedDir, "x.ts") }, | ||
| }); | ||
|
|
||
| expect(posts.length).toBe(1); | ||
| expect(posts[0]!.body.project).toBe(REPO_NAME); | ||
| }); | ||
|
|
||
| it("never sends an unscoped enrich request", async () => { | ||
| posts = []; | ||
| await runHook({ | ||
| session_id: "s3", | ||
| cwd: repoDir, | ||
| tool_name: "Grep", | ||
| tool_input: { pattern: "handleError", path: repoDir }, | ||
| }); | ||
|
|
||
| expect(posts.length).toBe(1); | ||
| const project = posts[0]!.body.project; | ||
| expect(project).toBeTruthy(); | ||
| expect(typeof project).toBe("string"); | ||
| }); | ||
|
|
||
| it("an explicit payload project still wins (hosts that do supply one)", async () => { | ||
| posts = []; | ||
| await runHook({ | ||
| session_id: "s4", | ||
| cwd: repoDir, | ||
| project: "explicit-project", | ||
| tool_name: "Read", | ||
| tool_input: { file_path: join(repoDir, "y.ts") }, | ||
| }); | ||
|
|
||
| expect(posts.length).toBe(1); | ||
| expect(posts[0]!.body.project).toBe("explicit-project"); | ||
| }); | ||
|
|
||
| it("stays a no-op when AGENTMEMORY_INJECT_CONTEXT is not true", async () => { | ||
| posts = []; | ||
| await runHook( | ||
| { | ||
| session_id: "s5", | ||
| cwd: repoDir, | ||
| tool_name: "Read", | ||
| tool_input: { file_path: join(repoDir, "z.ts") }, | ||
| }, | ||
| { AGENTMEMORY_INJECT_CONTEXT: "false" }, | ||
| ); | ||
|
|
||
| expect(posts.length).toBe(0); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove the explanatory comments.
Lines 99-105 explain payload behavior and prior failures. Remove them. Keep this rationale in external documentation if it is needed.
As per coding guidelines,
src/**/*.tsmust not add comments that explain what code does; use clear naming instead.🤖 Prompt for AI Agents
Source: Coding guidelines