feat: agent skill injection via config (#1355)

Add agent_skills config section that maps agent types to skill directory
paths. At spawn time, workflows load configured skills and inject them
as <agent_skills> blocks in Task() prompts, giving subagents access to
project-specific skill files.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-03-24 16:26:13 -04:00
parent 4ef309e0e4
commit db3eeb8fe4
22 changed files with 491 additions and 14 deletions

View File

@@ -548,6 +548,11 @@ async function runCommand(command, args, cwd, raw) {
break;
}
case 'agent-skills': {
init.cmdAgentSkills(cwd, args[1], raw);
break;
}
case 'history-digest': {
commands.cmdHistoryDigest(cwd, raw);
break;

View File

@@ -27,6 +27,18 @@ const VALID_CONFIG_KEYS = new Set([
'hooks.context_warnings',
]);
/**
* Check whether a config key path is valid.
* Supports exact matches from VALID_CONFIG_KEYS plus dynamic patterns
* like `agent_skills.<agent-type>` where the sub-key is freeform.
*/
function isValidConfigKey(keyPath) {
if (VALID_CONFIG_KEYS.has(keyPath)) return true;
// Allow agent_skills.<agent-type> with any agent type string
if (/^agent_skills\.[a-zA-Z0-9_-]+$/.test(keyPath)) return true;
return false;
}
const CONFIG_KEY_SUGGESTIONS = {
'workflow.nyquist_validation_enabled': 'workflow.nyquist_validation',
'agents.nyquist_validation_enabled': 'workflow.nyquist_validation',
@@ -120,6 +132,7 @@ function buildNewProjectConfig(userChoices) {
hooks: {
context_warnings: true,
},
agent_skills: {},
};
// Three-level deep merge: hardcoded <- userDefaults <- choices
@@ -142,6 +155,11 @@ function buildNewProjectConfig(userChoices) {
...(userDefaults.hooks || {}),
...(choices.hooks || {}),
},
agent_skills: {
...hardcoded.agent_skills,
...(userDefaults.agent_skills || {}),
...(choices.agent_skills || {}),
},
};
}
@@ -298,15 +316,18 @@ function cmdConfigSet(cwd, keyPath, value, raw) {
validateKnownConfigKeyPath(keyPath);
if (!VALID_CONFIG_KEYS.has(keyPath)) {
error(`Unknown config key: "${keyPath}". Valid keys: ${[...VALID_CONFIG_KEYS].sort().join(', ')}`);
if (!isValidConfigKey(keyPath)) {
error(`Unknown config key: "${keyPath}". Valid keys: ${[...VALID_CONFIG_KEYS].sort().join(', ')}, agent_skills.<agent-type>`);
}
// Parse value (handle booleans and numbers)
// Parse value (handle booleans, numbers, and JSON arrays/objects)
let parsedValue = value;
if (value === 'true') parsedValue = true;
else if (value === 'false') parsedValue = false;
else if (!isNaN(value) && value !== '') parsedValue = Number(value);
else if (typeof value === 'string' && (value.startsWith('[') || value.startsWith('{'))) {
try { parsedValue = JSON.parse(value); } catch { /* keep as string */ }
}
const setConfigValueResult = setConfigValue(cwd, keyPath, parsedValue);
output(setConfigValueResult, raw, `${keyPath}=${parsedValue}`);

View File

@@ -309,6 +309,7 @@ function loadConfig(cwd) {
context_window: get('context_window') ?? defaults.context_window,
phase_naming: get('phase_naming') ?? defaults.phase_naming,
model_overrides: parsed.model_overrides || null,
agent_skills: parsed.agent_skills || {},
};
} catch {
return defaults;

View File

@@ -1342,6 +1342,77 @@ function cmdInitRemoveWorkspace(cwd, name, raw) {
output(result, raw);
}
/**
* Build a formatted agent skills block for injection into Task() prompts.
*
* Reads `config.agent_skills[agentType]` and validates each skill path exists
* within the project root. Returns a formatted `<agent_skills>` block or empty
* string if no skills are configured.
*
* @param {object} config - Loaded project config
* @param {string} agentType - The agent type (e.g., 'gsd-executor', 'gsd-planner')
* @param {string} projectRoot - Absolute path to project root (for path validation)
* @returns {string} Formatted skills block or empty string
*/
function buildAgentSkillsBlock(config, agentType, projectRoot) {
const { validatePath } = require('./security.cjs');
if (!config || !config.agent_skills || !agentType) return '';
let skillPaths = config.agent_skills[agentType];
if (!skillPaths) return '';
// Normalize single string to array
if (typeof skillPaths === 'string') skillPaths = [skillPaths];
if (!Array.isArray(skillPaths) || skillPaths.length === 0) return '';
const validPaths = [];
for (const skillPath of skillPaths) {
if (typeof skillPath !== 'string') continue;
// Validate path safety — must resolve within project root
const pathCheck = validatePath(skillPath, projectRoot);
if (!pathCheck.safe) {
process.stderr.write(`[agent-skills] WARNING: Skipping unsafe path "${skillPath}": ${pathCheck.error}\n`);
continue;
}
// Check that the skill directory and SKILL.md exist
const skillMdPath = path.join(projectRoot, skillPath, 'SKILL.md');
if (!fs.existsSync(skillMdPath)) {
process.stderr.write(`[agent-skills] WARNING: Skill not found at "${skillPath}/SKILL.md" — skipping\n`);
continue;
}
validPaths.push(skillPath);
}
if (validPaths.length === 0) return '';
const lines = validPaths.map(p => `- @${p}/SKILL.md`).join('\n');
return `<agent_skills>\nRead these user-configured skills:\n${lines}\n</agent_skills>`;
}
/**
* Command: output the agent skills block for a given agent type.
* Used by workflows: SKILLS=$(node "$TOOLS" agent-skills gsd-executor 2>/dev/null)
*/
function cmdAgentSkills(cwd, agentType, raw) {
if (!agentType) {
// No agent type — output empty string silently
output('', raw, '');
return;
}
const config = loadConfig(cwd);
const block = buildAgentSkillsBlock(config, agentType, cwd);
// Output raw text (not JSON) so workflows can embed it directly
if (block) {
process.stdout.write(block);
}
process.exit(0);
}
module.exports = {
cmdInitExecutePhase,
cmdInitPlanPhase,
@@ -1360,4 +1431,6 @@ module.exports = {
cmdInitListWorkspaces,
cmdInitRemoveWorkspace,
detectChildRepos,
buildAgentSkillsBlock,
cmdAgentSkills,
};