fix(3799): detect project-local agents in init.* (local-first resolution) (#82)

* fix(3799): detect project-local agents in init.* (local-first resolution)

Reverses resolution order in resolveAgentsDir() so project-local
<projectDir>/.claude/agents is checked BEFORE the global runtime dir.
Claude Code auto-creates ~/.claude/agents at startup (empty); the old
global-first check returned that empty dir over a populated local dir.

Adds 5 tests to tests/bug-3751-init-local-agents.test.cjs:
- Structural: local-first ordering in function body (#3799 RED gate)
- Runtime: local+empty-global → agents_dir = local
- Runtime: global-only (no local) → agents_dir = global (no regression)
- Runtime: both present → local wins (Claude Code parity)
- Updated Contract 3 assertion to reflect new local-first ordering

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore: add changeset for fix(3799)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-22 11:23:41 -04:00
committed by GitHub
parent cda3d7a5ab
commit 2b02786f50
3 changed files with 226 additions and 17 deletions

View File

@@ -109,27 +109,31 @@ export function detectRuntime(config?: { runtime?: unknown }): Runtime {
*
* Precedence:
* 1. `GSD_AGENTS_DIR` — explicit SDK override (wins over runtime selection)
* 2. `<getRuntimeConfigDir(runtime)>/agents` — installer-parity default (when the dir exists)
* 3. `<projectDir>/.claude/agents` — repo-local fallback for `--local` Claude installs
* (only probed when the global runtime dir is absent or empty, and `projectDir` is given)
* 2. `<projectDir>/.claude/agents` — project-local install (Claude Code `--local`,
* `npx get-shit-done-cc@latest`); checked first to match Claude Code's own
* local-first agent resolution at spawn (#3799)
* 3. `<getRuntimeConfigDir(runtime)>/agents` — global installer-parity default
*
* Defaults to Claude when no runtime is passed, matching prior behavior
* (see `init-runner.ts`, which is Claude-only by design).
*
* The repo-local fallback was added for bug #3751: Claude Code `--local` installs
* place agent definitions under `./.claude/agents` rather than `~/.claude/agents`,
* but the SDK agent-detection path only probed the global directory.
* Local-first resolution was introduced for bug #3799: when both a project-local
* `.claude/agents/` and a global `~/.claude/agents/` exist (the global may be
* auto-created empty by Claude Code), the SDK must resolve to the project-local
* directory to match where the agents are actually installed.
*
* The repo-local fallback itself was added for bug #3751.
*/
export function resolveAgentsDir(runtime: Runtime = 'claude', projectDir?: string): string {
if (process.env.GSD_AGENTS_DIR) return process.env.GSD_AGENTS_DIR;
const globalDir = join(getRuntimeConfigDir(runtime), 'agents');
if (existsSync(globalDir)) return globalDir;
// Repo-local fallback: <projectDir>/.claude/agents for --local Claude installs (#3751).
// Project-local first: <projectDir>/.claude/agents for --local / npx installs (#3799).
// Only applicable when a projectDir is known; other runtimes don't use .claude/.
if (projectDir && runtime === 'claude') {
const localDir = join(projectDir, '.claude', 'agents');
if (existsSync(localDir)) return localDir;
}
const globalDir = join(getRuntimeConfigDir(runtime), 'agents');
if (existsSync(globalDir)) return globalDir;
return globalDir;
}