Skip to content

fix: scanAgents ignores active project context when no path is supplied #1630

Closed

Description

@avoidwork

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:

  1. 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).
  2. Invoke the scanAgents tool without supplying a path argument.
  3. 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 getProjectCwd from ../../workspace/project.js; the module has no reference to the active project at all.
  • src/tools/file/index.js:28-30 — Reference implementation: resolveBaseDir() returns getProjectCwd() || 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() and setProjectCwd(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

  1. Import the project context — In src/tools/scanAgents/index.js, add import { getProjectCwd } from "../../workspace/project.js"; (mirror the import in src/tools/file/index.js).
  2. Remove the frozen capture — Delete line 9 const cwd = loadConfig().cwd; so the cwd is no longer captured once at module load.
  3. Resolve against the active project — At line 25, change const targetPath = input.path || cwd; to const targetPath = input.path || getProjectCwd() || config.cwd || process.cwd(); (or add a local resolveBaseDir() helper matching the sibling tools).
  4. 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 calls setProjectCwd(<projectDir>) and asserts the scan targets the project directory.
  5. Verify — Run npm run test and npm run coverage to confirm the new test passes and no regressions occur.

Activity

  1. added
    bugSomething isn't working
    approvedAn identifier for Madz to take action.
    and removed on Nov 1, 2026
  2. added a commit that references this issue on Nov 1, 2026
    05dadc0
  3. avoidwork commented on Nov 1, 2026

    @avoidwork
    OwnerAuthor

    Fixed in #1631.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedAn identifier for Madz to take action.bugSomething isn't workingin progress

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions