fix(#1415): loadConfig provenance + agent-skills diagnostic (Resolution Provenance P2) (#1424)

Implements ADR-1411 P2 / #1415, closing #1366.

Part 1 — config-loader.cts:
- Adds `loadConfigResolved(cwd, options) → ConfigResolution { config, source, degraded }`
  with six tagged branch return paths:
  A1 ws+wsconfig → source:'workstream', degraded:false
  A2 no-ws+config → source:'root', degraded:false
  B  ws requested, wsconfig absent → source:'root', degraded:true (intercepts recursive call)
  C  .planning/ exists, no config → source:'builtin-defaults', degraded:false
  D  no .planning/, global defaults readable → source:'global-defaults', degraded:false
  E  no .planning/, no global → source:'builtin-defaults', degraded:false
- `loadConfigResolved` calls `findProjectRoot` at entry to anchor resolution to the
  nearest .planning/ ancestor (cwd-drift fix, heuristic 4 from P1)
- `loadConfig` becomes a one-line delegation: `return loadConfigResolved(cwd, options).config`
- Exports `loadConfigResolved` in the `export =` block

Part 2 — init.cts cmdAgentSkills:
- Imports `findProjectRoot` from `./project-root.cjs`
- Anchors to project root before loading config (fixes #1366 cwd-drift)
- Uses `loadConfigResolved` for provenance; passes projectRoot to buildAgentSkillsBlock
- Computes `configured` + `reason` (AgentSkillsReason enum): 'resolved' |
  'not_configured' | 'configured_empty' | 'configured_unresolved'
- configured_empty and configured_unresolved emit stderr WARNING; not_configured is silent
- --json IR gains: configured, reason, source, degraded (in addition to existing
  agent_type, block, skills_count, warnings)

Tests (TDD):
- tests/config-loader.test.cjs: 8 new provenance tests (RED before impl, GREEN after)
- tests/agent-skills.test.cjs: 7 new diagnostic tests (RED before impl, GREEN after)
- All 306 tests across 4 suites pass (config-loader:33, agent-skills:72, init:105, workstream:96)

Docs:
- CONTEXT.md: Config Loader Module entry updated with loadConfigResolved interface;
  Resolution Provenance entry notes P2 is now implemented
- docs/CLI-TOOLS.md: --json field reference table added for agent-skills

Closes #1366
Part of #1411

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-06-18 07:05:15 -04:00
committed by GitHub
parent d220ba6fd6
commit 484a5b7b86
7 changed files with 473 additions and 96 deletions

View File

@@ -1423,3 +1423,151 @@ describe('bug #1243: plugin-namespaced agent skills', () => {
);
});
});
// ─── Resolution Provenance diagnostics (#1415 / #1366) ────────────────────────
//
// Verifies that cmdAgentSkills uses findProjectRoot (cwd-drift anchor) and
// loadConfigResolved (provenance-aware config loading), and that the --json IR
// includes the new fields: configured, reason, source, degraded.
describe('agent-skills — Resolution Provenance (#1415)', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempProject();
});
afterEach(() => {
cleanup(tmpDir);
});
test('--json IR includes configured, reason, source, degraded fields', () => {
// Minimal smoke: just the field presence
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
assert.ok(r.success, `Command failed: ${r.error}`);
assert.ok('configured' in r.ir, 'IR must include "configured" field');
assert.ok('reason' in r.ir, 'IR must include "reason" field');
assert.ok('source' in r.ir, 'IR must include "source" field');
assert.ok('degraded' in r.ir, 'IR must include "degraded" field');
});
test('not_configured: agent not in map → configured:false, reason:not_configured, no stderr warning', () => {
writeConfig(tmpDir, {
agent_skills: { 'gsd-executor': ['skills/foo'] },
});
const r = runGsdToolsWithStderr(['agent-skills', '--json', 'gsd-planner'], tmpDir, {
HOME: tmpDir,
USERPROFILE: tmpDir,
});
assert.ok(r.success, `Command failed: ${r.stderr}`);
const ir = JSON.parse(r.stdout);
assert.strictEqual(ir.configured, false);
assert.strictEqual(ir.reason, 'not_configured');
// No warning on stderr for not_configured
assert.ok(
!r.stderr.includes('WARNING'),
`Should NOT emit WARNING for not_configured agent, got stderr: ${r.stderr}`,
);
});
test('configured_empty: agent_skills[X]=[] → configured:true, reason:configured_empty, stderr WARNING, skills_count:0', () => {
writeConfig(tmpDir, {
agent_skills: { 'gsd-executor': [] },
});
const r = runGsdToolsWithStderr(['agent-skills', '--json', 'gsd-executor'], tmpDir, {
HOME: tmpDir,
USERPROFILE: tmpDir,
});
assert.ok(r.success, `Command failed: ${r.stderr}`);
const ir = JSON.parse(r.stdout);
assert.strictEqual(ir.configured, true);
assert.strictEqual(ir.reason, 'configured_empty');
assert.strictEqual(ir.skills_count, 0);
assert.strictEqual(ir.block, '');
assert.ok(
r.stderr.includes('WARNING') || r.stderr.toLowerCase().includes('warning'),
`Should emit WARNING for configured_empty, got stderr: ${r.stderr}`,
);
});
test('configured_unresolved: configured path that does not exist → reason:configured_unresolved, stderr WARNING', () => {
writeConfig(tmpDir, {
agent_skills: { 'gsd-executor': ['skills/nonexistent-1415'] },
});
const r = runGsdToolsWithStderr(['agent-skills', '--json', 'gsd-executor'], tmpDir, {
HOME: tmpDir,
USERPROFILE: tmpDir,
});
assert.ok(r.success, `Command failed: ${r.stderr}`);
const ir = JSON.parse(r.stdout);
assert.strictEqual(ir.configured, true);
assert.strictEqual(ir.reason, 'configured_unresolved');
assert.strictEqual(ir.block, '');
assert.ok(
r.stderr.includes('WARNING') || r.stderr.toLowerCase().includes('warning'),
`Should emit WARNING for configured_unresolved, got stderr: ${r.stderr}`,
);
});
test('resolved: valid configured path → configured:true, reason:resolved, block non-empty', () => {
const skillDir = path.join(tmpDir, 'skills', 'my-skill-1415');
fs.mkdirSync(skillDir, { recursive: true });
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '# My Skill\n');
writeConfig(tmpDir, {
agent_skills: { 'gsd-executor': ['skills/my-skill-1415'] },
});
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
assert.ok(r.success, `Command failed: ${r.error}`);
assert.strictEqual(r.ir.configured, true);
assert.strictEqual(r.ir.reason, 'resolved');
assert.ok(r.ir.block.includes('<agent_skills>'), 'block must be non-empty for resolved');
});
test('cwd-drift: invoking from descendant subdir resolves config from project root', () => {
const skillDir = path.join(tmpDir, 'skills', 'drift-skill');
fs.mkdirSync(skillDir, { recursive: true });
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '# Drift Skill\n');
writeConfig(tmpDir, {
agent_skills: { 'gsd-executor': ['skills/drift-skill'] },
});
// Invoke from a descendant subdirectory
const deepDir = path.join(tmpDir, 'src', 'feature');
fs.mkdirSync(deepDir, { recursive: true });
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], deepDir);
assert.ok(r.success, `Command failed: ${r.error}`);
assert.strictEqual(r.ir.configured, true);
assert.strictEqual(r.ir.reason, 'resolved');
assert.ok(r.ir.block.includes('<agent_skills>'), `block must be non-empty for drift test, got: ${r.ir.block}`);
});
test('source field matches config provenance (root when config.json present)', () => {
writeConfig(tmpDir, {
agent_skills: { 'gsd-executor': [] },
});
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
assert.ok(r.success, `Command failed: ${r.error}`);
assert.strictEqual(r.ir.source, 'root');
assert.strictEqual(r.ir.degraded, false);
});
test('Fix 3: agent_skills[X]="" (empty string) → configured_empty, skills_count:0, stderr WARNING', () => {
writeConfig(tmpDir, {
agent_skills: { 'gsd-executor': '' },
});
const r = runGsdToolsWithStderr(['agent-skills', '--json', 'gsd-executor'], tmpDir, {
HOME: tmpDir,
USERPROFILE: tmpDir,
});
assert.ok(r.success, `Command failed: ${r.stderr}`);
const ir = JSON.parse(r.stdout);
assert.strictEqual(ir.configured, true, 'should be configured');
assert.strictEqual(ir.reason, 'configured_empty',
`empty string must yield configured_empty, got: ${ir.reason}`);
assert.strictEqual(ir.skills_count, 0, 'skills_count must be 0 for empty string');
assert.strictEqual(ir.block, '', 'block must be empty');
assert.ok(
r.stderr.includes('WARNING') || r.stderr.toLowerCase().includes('warning'),
`Should emit WARNING for empty-string configured_empty, got stderr: ${r.stderr}`,
);
});
});