Repository navigation
Description
Summary
The scanAgents tool ignores the active project context. It captures the root cwd once at module load and, when no path argument is supplied, always falls back to that captured root — never consulting the active project. As a result, when a user selects a project via the /projects panel, scanAgents still scans the madz root instead of the active project, returning the wrong AGENTS.md (or none at all).
Environment
- OS: Linux 7.0.14-linuxkit
- Node.js: v26.13.0
- madz version: 1.129.3
- LLM provider: Unknown — user to confirm
Reproduction
Steps to reproduce the behavior:
- Start madz and select a project via the /projects panel so a project context is active (e.g., a subdirectory with its own
AGENTS.md). - Invoke the
scanAgentstool without supplying apathargument. - Observe that the scan targets the madz root directory instead of the active project directory.
Expected Behavior
When no path is supplied, scanAgents should resolve against the active project's working directory (via getProjectCwd()), falling back to config.cwd and then process.cwd(). It should return the AGENTS.md belonging to the active project — matching the behavior of the sibling file and discovery tools.
Actual Behavior
scanAgents resolves the omitted path against a cwd captured once at module load (loadConfig().cwd), so it always scans the madz root regardless of the active project. The active project context is never consulted, so the wrong AGENTS.md is returned (or none is found).
Additional Context
The sibling tools src/tools/file/index.js and src/tools/discovery/index.js both define a resolveBaseDir() helper that returns getProjectCwd() || config.cwd || process.cwd() and resolve relative/omitted paths against it. scanAgents is the outlier — it does not import or use getProjectCwd() from src/workspace/project.js.
Root Cause Analysis
src/tools/scanAgents/index.js line 9 captures const cwd = loadConfig().cwd; once at module load, and line 25 resolves the target as input.path || cwd. Because the fallback is the captured root cwd rather than the live project context, the tool never consults the active project. The fix is to mirror the resolveBaseDir() pattern used by the file and discovery tools: import getProjectCwd from ../../workspace/project.js and resolve input.path || getProjectCwd() || config.cwd || process.cwd().
Testing Strategy
Add a regression test in tests/unit/tools_scanAgents.test.js that sets a project cwd via setProjectCwd() and asserts that an omitted path resolves to the project directory rather than the root. The existing test at line 84 ("uses process.cwd() when no path is provided") should be updated to assert project-cwd resolution instead.
Security Considerations
This fix touches file I/O path resolution. Ensure the resolved path remains within the sandbox allowlist and that no path traversal is introduced by the new resolution chain. No credential or auth surface is affected.
Audit Findings (for Issue 1630)
- src/tools/scanAgents/index.js:9 —
const cwd = loadConfig().cwd;captures the root cwd ONCE at module load. This is the root cause: the value is frozen before any project context is set. - src/tools/scanAgents/index.js:25 —
const targetPath = input.path || cwd;falls back to the captured root cwd and never consults the active project. - src/tools/scanAgents/index.js:1-7 — No import of
getProjectCwdfrom../../workspace/project.js; the module has no reference to the active project at all. - src/tools/file/index.js:28-30 — Reference implementation:
resolveBaseDir()returnsgetProjectCwd() || config.cwd || process.cwd(). - src/tools/discovery/index.js:28-30 — Same
resolveBaseDir()pattern; confirms the intended convention. - src/workspace/project.js:18,26 —
getProjectCwd()andsetProjectCwd(cwd)are the live project-context API to use. - tests/unit/tools_scanAgents.test.js:84 — Test "uses process.cwd() when no path is provided" only asserts the schema accepts an omitted path; it does not exercise project-cwd resolution and must be updated.
Fix Steps
- Import the project context — In
src/tools/scanAgents/index.js, addimport { getProjectCwd } from "../../workspace/project.js";(mirror the import insrc/tools/file/index.js). - Remove the frozen capture — Delete line 9
const cwd = loadConfig().cwd;so the cwd is no longer captured once at module load. - Resolve against the active project — At line 25, change
const targetPath = input.path || cwd;toconst targetPath = input.path || getProjectCwd() || config.cwd || process.cwd();(or add a localresolveBaseDir()helper matching the sibling tools). - Update the regression test — In
tests/unit/tools_scanAgents.test.js:84, replace the "uses process.cwd() when no path is provided" test with one that callssetProjectCwd(<projectDir>)and asserts the scan targets the project directory. - Verify — Run
npm run testandnpm run coverageto confirm the new test passes and no regressions occur.
Activity
fix: resolve scanAgents omitted path against active project cwd
avoidwork commentedon Nov 1, 2026
Fixed in #1631.