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

@@ -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,
};