diff --git a/coverage.txt b/coverage.txt index 227360eee..244eb2ac1 100644 --- a/coverage.txt +++ b/coverage.txt @@ -12,7 +12,7 @@ ℹ skillsMiddleware.js | 100.00 | 87.50 | 100.00 | ℹ config | | | | ℹ config.js | 100.00 | 100.00 | 100.00 | -ℹ loader.js | 96.54 | 87.40 | 90.48 | 105-106 114-115 408-409 468-470 495 497 561-565 575-578 +ℹ loader.js | 95.67 | 88.43 | 90.48 | 403-413 468-470 495 497 561-565 575-578 ℹ parse.js | 100.00 | 100.00 | 100.00 | ℹ patch.js | 100.00 | 100.00 | 100.00 | ℹ schemas | | | | @@ -31,7 +31,7 @@ ℹ tui.js | 100.00 | 100.00 | 100.00 | ℹ vector.js | 100.00 | 100.00 | 100.00 | ℹ memory | | | | -ℹ context.js | 95.42 | 88.10 | 100.00 | 103-104 125-126 150-152 +ℹ context.js | 94.12 | 80.95 | 100.00 | 103-104 125-126 148-152 ℹ expireEphemeralMemories.js | 94.19 | 86.36 | 100.00 | 37-39 81-82 ℹ gc.js | 100.00 | 100.00 | 100.00 | ℹ index.js | 100.00 | 100.00 | 100.00 | @@ -48,7 +48,7 @@ ℹ copilotAuth.js | 99.40 | 89.89 | 90.91 | 140-141 375 ℹ copilotRetryMiddleware.js | 100.00 | 80.00 | 75.00 | ℹ index.js | 100.00 | 100.00 | 100.00 | -ℹ modelInfo.js | 100.00 | 98.28 | 100.00 | +ℹ modelInfo.js | 100.00 | 97.01 | 100.00 | ℹ openai.js | 95.16 | 80.00 | 80.00 | 64-66 109-110 116-117 186 214-215 227-230 ℹ summarizationMiddleware.js | 100.00 | 100.00 | 100.00 | ℹ tokenBudget.js | 100.00 | 90.32 | 86.67 | @@ -172,7 +172,7 @@ ℹ sampling | | | | ℹ index.js | 94.97 | 81.82 | 80.00 | 27 180-188 ℹ scanAgents | | | | -ℹ index.js | 100.00 | 87.50 | 100.00 | +ℹ index.js | 100.00 | 83.33 | 100.00 | ℹ session | | | | ℹ index.js | 97.08 | 71.19 | 94.12 | 71-72 118-119 128 181-182 ℹ skills | | | | @@ -231,6 +231,6 @@ ℹ loadAgents.js | 88.00 | 42.86 | 100.00 | 17-18 32-33 38-39 ℹ project.js | 100.00 | 100.00 | 100.00 | ℹ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ℹ all files | 91.54 | 87.53 | 86.86 | +ℹ all files | 91.52 | 87.50 | 86.86 | ℹ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/.openspec.yaml b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/.openspec.yaml new file mode 100644 index 000000000..25002a8e8 --- /dev/null +++ b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-11-01 diff --git a/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/design.md b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/design.md new file mode 100644 index 000000000..cb0ff8009 --- /dev/null +++ b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/design.md @@ -0,0 +1,55 @@ +# Design + +## Context + +`scanAgents` (`src/tools/scanAgents/index.js`) captures the root cwd once at +module load: `const cwd = loadConfig().cwd;`, then resolves an omitted `path` as +`input.path || cwd`. Because the value is frozen before any project context is +set, the tool never consults the active project. The sibling tools `file` +(`src/tools/file/index.js`) and `discovery` (`src/tools/discovery/index.js`) +already solve this with a `resolveBaseDir()` helper returning +`getProjectCwd() || config.cwd || process.cwd()`, evaluated at call time. See +proposal.md for motivation. + +## Goals / Non-Goals + +**Goals:** +- Resolve an omitted `path` against the live active project cwd at call time. +- Preserve the `config.cwd` and `process.cwd()` fallbacks when no project is active. +- Keep the change confined to `scanAgents` and its test. + +**Non-Goals:** +- No change to explicit-`path` handling, `validatePath`/sandbox allowlist, or + other tools. +- No new dependency. + +## Decisions + +- **Reuse the `getProjectCwd()` API, not a new store.** `src/workspace/project.js` + already exposes `getProjectCwd()`/`setProjectCwd()`; the TUI writes to it on + project selection. Reusing it keeps a single source of truth for the active + project. +- **Inline fallback vs. `resolveBaseDir()` helper.** Two options: (a) inline + `input.path || getProjectCwd() || config.cwd || process.cwd()` inside + `scanAgentsImpl`, or (b) add a local `resolveBaseDir()` mirroring the siblings. + Chosen: **inline fallback** — `scanAgents` has a single resolution site and no + relative-path resolution (unlike `file`/`discovery`), so a helper adds no reuse + value and would be a YAGNI abstraction. The inline chain is identical in + behavior to the siblings' `resolveBaseDir()`. +- **Keep `config = loadConfig()` at module scope.** The `config.cwd` fallback + still needs `config`; only the frozen `cwd` capture is removed. + +## Risks / Trade-offs + +- [Path traversal via the new resolution chain] → The resolved path still flows + through `validatePath(targetPath, options.allowedPaths)`, so the sandbox + allowlist and traversal protection are unchanged. +- [Behavior drift when no project is active] → The fallback chain degrades to + `config.cwd || process.cwd()`, matching prior behavior; covered by a scenario. +- [Test coupling to module-load cwd] → The existing "uses process.cwd()" test is + replaced with a `setProjectCwd()`-driven regression test, removing the coupling. + +## Migration Plan + +Single commit on the feature branch. No data migration. Rollback is a revert of +the one-file source change plus its test. diff --git a/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/proposal.md b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/proposal.md new file mode 100644 index 000000000..1feac5f5b --- /dev/null +++ b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/proposal.md @@ -0,0 +1,50 @@ +# Proposal + +## Why + +The `scanAgents` tool ignores the active project context. It captures the root +`cwd` once at module load (`const cwd = loadConfig().cwd`) and, when no `path` +argument is supplied, always falls back to that frozen root — never consulting the +active project. 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). Its sibling tools `file` and `discovery` already +resolve omitted paths against the live active project cwd; `scanAgents` is the +outlier. + +## What Changes + +- `scanAgents` resolves an omitted `path` against the active project cwd via + `getProjectCwd()`, falling back to `config.cwd` and then `process.cwd()`. +- Remove the module-load frozen `const cwd = loadConfig().cwd` capture. +- Import `getProjectCwd` from `src/workspace/project.js` and evaluate the fallback + chain at call time inside `scanAgentsImpl`. +- Update the regression test that asserted `process.cwd()` defaulting to instead + assert active-project-cwd resolution. + +## Capabilities + +### New Capabilities + +(none) + +### Modified Capabilities + +- `scan-agents`: the "Tool discovers AGENTS.md in specified path" requirement + changes its default-path behavior — an omitted `path` now resolves against the + active project cwd (`getProjectCwd() || config.cwd || process.cwd()`) rather than + a frozen root cwd captured at module load. + +## Impact + +- `src/tools/scanAgents/index.js` — import `getProjectCwd`, drop the frozen cwd + capture, resolve the omitted path against the active project at call time. +- `tests/unit/tools_scanAgents.test.js` — replace the `process.cwd()` defaulting + test with an active-project-cwd regression test. +- No change to explicit-`path` behavior, `validatePath`/sandbox allowlist, or + other tools. No new dependencies. + +## Non-goals + +- Not changing how an explicit `path` argument is handled. +- Not changing `validatePath`/sandbox allowlist or path-traversal protection. +- Not touching other tools or the `file`/`discovery` `resolveBaseDir()` helpers. diff --git a/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/specs/scan-agents/spec.md b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/specs/scan-agents/spec.md new file mode 100644 index 000000000..2b64ed755 --- /dev/null +++ b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/specs/scan-agents/spec.md @@ -0,0 +1,33 @@ +# Spec Delta + +## MODIFIED Requirements + +### Requirement: Tool discovers AGENTS.md in specified path +The scanAgents tool SHALL accept an optional `path` parameter and check for the +existence of an `AGENTS.md` file at that location. If no path is provided, the +tool SHALL resolve the default against the active project working directory +(`getProjectCwd()`), falling back to `config.cwd` and then `process.cwd()`. The +active project context SHALL be consulted at call time, not captured once at +module load. + +#### Scenario: Scan current working directory +- **WHEN** the tool is called with no arguments +- **THEN** the tool checks for `AGENTS.md` in the active project working + directory, falling back to `config.cwd` and then `process.cwd()` + +#### Scenario: Scan active project when no path is provided +- **WHEN** the tool is called with no arguments and a project context is active +- **THEN** the tool checks for `AGENTS.md` in the active project working directory + +#### Scenario: Fall back to config.cwd when no project is active +- **WHEN** the tool is called with no arguments and no project context is active +- **THEN** the tool checks for `AGENTS.md` in `config.cwd` + +#### Scenario: Fall back to process.cwd when no project and no config.cwd +- **WHEN** the tool is called with no arguments, no project context is active, and + `config.cwd` is unset +- **THEN** the tool checks for `AGENTS.md` in `process.cwd()` + +#### Scenario: Scan specified path +- **WHEN** the tool is called with a `path` parameter +- **THEN** the tool checks for `AGENTS.md` in the specified directory diff --git a/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/tasks.md b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/tasks.md new file mode 100644 index 000000000..47ff82ce8 --- /dev/null +++ b/openspec/changes/archive/2026-11-01-scan-agents-active-project-cwd/tasks.md @@ -0,0 +1,27 @@ +# Tasks + +## 1. Resolve omitted path against active project cwd + +- [x] 1.1 In `src/tools/scanAgents/index.js`, add `import { getProjectCwd } from + "../../workspace/project.js";` and verify the import resolves (no lint error). +- [x] 1.2 Remove the module-load frozen capture `const cwd = loadConfig().cwd;` + while keeping `const config = loadConfig();` for the `config.cwd` fallback; + verify `npm run lint` passes. +- [x] 1.3 In `scanAgentsImpl`, change the omitted-path resolution from + `input.path || cwd` to `input.path || getProjectCwd() || config.cwd || + process.cwd()`; verify `npm run lint` passes. + +## 2. Regression test + +- [x] 2.1 In `tests/unit/tools_scanAgents.test.js`, replace the "uses + process.cwd() when no path is provided" test with one that calls + `setProjectCwd()` and asserts an omitted `path` resolves to the + project directory (reads that project's `AGENTS.md`); verify the new test + passes via `npm run test`. +- [x] 2.2 Run `npm run test` and `npm run coverage` and verify the full suite + passes with no regressions. + +## Workflow follow-up + +- Archive the change after the project's review requirements are satisfied. +- Verify the archived result. diff --git a/openspec/specs/scan-agents/spec.md b/openspec/specs/scan-agents/spec.md index f70a819f1..416217e02 100644 --- a/openspec/specs/scan-agents/spec.md +++ b/openspec/specs/scan-agents/spec.md @@ -6,11 +6,30 @@ The scanAgents tool enables agents to discover and load AGENTS.md files from a s ## Requirements ### Requirement: Tool discovers AGENTS.md in specified path -The scanAgents tool SHALL accept an optional `path` parameter and check for the existence of an `AGENTS.md` file at that location. If no path is provided, the tool SHALL use the current working directory (`process.cwd()`) as the default. +The scanAgents tool SHALL accept an optional `path` parameter and check for the +existence of an `AGENTS.md` file at that location. If no path is provided, the +tool SHALL resolve the default against the active project working directory +(`getProjectCwd()`), falling back to `config.cwd` and then `process.cwd()`. The +active project context SHALL be consulted at call time, not captured once at +module load. #### Scenario: Scan current working directory - **WHEN** the tool is called with no arguments -- **THEN** the tool checks for `AGENTS.md` in the current working directory +- **THEN** the tool checks for `AGENTS.md` in the active project working + directory, falling back to `config.cwd` and then `process.cwd()` + +#### Scenario: Scan active project when no path is provided +- **WHEN** the tool is called with no arguments and a project context is active +- **THEN** the tool checks for `AGENTS.md` in the active project working directory + +#### Scenario: Fall back to config.cwd when no project is active +- **WHEN** the tool is called with no arguments and no project context is active +- **THEN** the tool checks for `AGENTS.md` in `config.cwd` + +#### Scenario: Fall back to process.cwd when no project and no config.cwd +- **WHEN** the tool is called with no arguments, no project context is active, and + `config.cwd` is unset +- **THEN** the tool checks for `AGENTS.md` in `process.cwd()` #### Scenario: Scan specified path - **WHEN** the tool is called with a `path` parameter diff --git a/src/tools/scanAgents/index.js b/src/tools/scanAgents/index.js index 5755891b4..12f70775a 100644 --- a/src/tools/scanAgents/index.js +++ b/src/tools/scanAgents/index.js @@ -5,8 +5,9 @@ import { join } from "node:path"; import { validatePath, checkFileLimit } from "../common.js"; import { loadAgents } from "../../workspace/loadAgents.js"; import { loadConfig } from "../../config/loader.js"; +import { getProjectCwd } from "../../workspace/project.js"; -const cwd = loadConfig().cwd; +const config = loadConfig(); const ScanAgentsSchema = z.object({ path: z @@ -22,7 +23,7 @@ const ScanAgentsSchema = z.object({ * @returns {Promise<{ ok: boolean, agents?: string, error?: string }>} Structured result */ export async function scanAgentsImpl(input, options) { - const targetPath = input.path || cwd; + const targetPath = input.path || getProjectCwd() || config.cwd || process.cwd(); const resolved = validatePath(targetPath, options.allowedPaths); if (!resolved.allowed) { return { ok: false, error: resolved.error }; diff --git a/tests/unit/tools_scanAgents.test.js b/tests/unit/tools_scanAgents.test.js index 6af2299d0..b4a02f96a 100644 --- a/tests/unit/tools_scanAgents.test.js +++ b/tests/unit/tools_scanAgents.test.js @@ -3,7 +3,8 @@ import assert from "node:assert"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { writeFile, rm, mkdir } from "node:fs/promises"; -import { scanAgentsImpl, scanAgents } from "../../src/tools/scanAgents/index.js"; +import { scanAgentsImpl } from "../../src/tools/scanAgents/index.js"; +import { setProjectCwd } from "../../src/workspace/project.js"; import { buildToolConfig } from "../../src/tools/index.js"; // Test fixtures @@ -81,15 +82,27 @@ describe("scanAgents tool - scanAgentsImpl", () => { ); }); - it("uses process.cwd() when no path is provided", async () => { - // This test verifies the default path behavior - // We can't easily test process.cwd() without mocking, so we verify the schema accepts no path - const schema = scanAgents.schema; - const parsed = schema.parse({}); - assert.ok( - !parsed.path || typeof parsed.path === "undefined", - "Expected path to be undefined when not provided", - ); + it("resolves an omitted path against the active project cwd", async () => { + // A distinct project directory with its own AGENTS.md, under tmpdir() + // so it passes the sandbox allowlist. + const projectDir = join(tmpdir(), "scanAgents-project-" + Date.now()); + const projectContent = "# Project AGENTS.md\n\nProject-specific rules."; + await mkdir(projectDir, { recursive: true }); + await writeFile(join(projectDir, "AGENTS.md"), projectContent, "utf-8"); + + setProjectCwd(projectDir); + try { + const result = await scanAgentsImpl({}, options); + assert.strictEqual(result.ok, true); + assert.ok( + result.agents.includes("# Project AGENTS.md") && + result.agents.includes("Project-specific rules."), + `Expected project AGENTS.md content, got: ${result.agents.substring(0, 100)}`, + ); + } finally { + setProjectCwd(""); + await rm(projectDir, { recursive: true, force: true }); + } }); });