refactor(#1556): wire installRuntimeArtifacts to install plan (#1563)

This commit is contained in:
Tom Boucher
2026-06-21 21:15:11 -04:00
committed by GitHub
parent 405ae9b3b7
commit 3416dda9d4
2 changed files with 108 additions and 33 deletions

View File

@@ -363,6 +363,9 @@ const {
const { const {
resolveRuntimeArtifactLayout, resolveRuntimeArtifactLayout,
} = require(path.join(_gsdLibDir, 'runtime-artifact-layout.cjs')); } = require(path.join(_gsdLibDir, 'runtime-artifact-layout.cjs'));
const {
createRuntimeArtifactInstallPlan,
} = require(path.join(_gsdLibDir, 'runtime-artifact-install-plan.cjs'));
const { const {
planLegacyCleanup, planLegacyCleanup,
applyLegacyCleanup, applyLegacyCleanup,
@@ -7008,36 +7011,25 @@ function installRuntimeArtifacts(runtime, configDir, scope, resolvedProfile) {
_runLegacyInstallMigrations(runtime, configDir, scope); _runLegacyInstallMigrations(runtime, configDir, scope);
const layout = resolveRuntimeArtifactLayout(runtime, configDir, scope); const layout = resolveRuntimeArtifactLayout(runtime, configDir, scope);
const planResult = createRuntimeArtifactInstallPlan({
// Compute pathPrefix once for the rewrite step (same derivation as the layout,
// top-level install() function). resolvedProfile,
const _resolvedTarget = path.resolve(configDir).replace(/\\/g, '/'); homedir: () => os.homedir(),
const _homeDir = os.homedir().replace(/\\/g, '/'); platform: process.platform,
const pathPrefix = computePathPrefix({ resolveAttribution: getCommitAttribution,
isGlobal: scope === 'global',
isOpencode: runtime === 'opencode',
isWindowsHost: process.platform === 'win32',
resolvedTarget: _resolvedTarget,
homeDir: _homeDir,
}); });
for (const kind of layout.kinds) { const cleanupDirs = planResult.ok ? planResult.plan.cleanupDirs : planResult.cleanupDirs;
const staged = kind.stage(resolvedProfile); try {
// stagedForCopy: the directory to copy from (may differ from staged if rewrites if (!planResult.ok) {
// produce a temp copy — see applyRuntimeContentRewritesForCommandsInPlace). throw new Error(planResult.message);
let stagedForCopy = staged;
const isGlobal = scope === 'global';
if (kind.kind === 'skills' || kind.kind === 'kimi-agents') {
applyRuntimeContentRewritesInPlace(staged, runtime, pathPrefix, isGlobal, getCommitAttribution(runtime));
} else if (kind.kind === 'commands') {
// Returns a temp dir with rewritten content so source files are never mutated.
stagedForCopy = applyRuntimeContentRewritesForCommandsInPlace(staged, runtime, pathPrefix, isGlobal, getCommitAttribution(runtime));
} }
// applyRuntimeContentRewritesForCommandsInPlace() returns a fresh mkdtemp dir under
// os.tmpdir() (gsd-cmd-rewrites-*); remove it once copied so it does not accumulate (#856). const kindsByName = new Map(layout.kinds.map((kind) => [kind.kind, kind]));
const tempToClean = stagedForCopy !== staged ? stagedForCopy : null; for (const item of planResult.plan.items) {
try { const kind = kindsByName.get(item.kind);
const dest = path.join(layout.configDir, kind.destSubpath); if (!kind) throw new Error(`Install plan returned unknown artifact kind: ${item.kind}`);
const dest = item.destDir;
fs.mkdirSync(dest, { recursive: true }); fs.mkdirSync(dest, { recursive: true });
if (kind.kind === 'skills' && fs.existsSync(dest)) { if (kind.kind === 'skills' && fs.existsSync(dest)) {
// Pre-prune: snapshot user-owned content before _removeGsdEntries wipes it, // Pre-prune: snapshot user-owned content before _removeGsdEntries wipes it,
@@ -7064,7 +7056,7 @@ function installRuntimeArtifacts(runtime, configDir, scope, resolvedProfile) {
} }
_removeGsdEntries(dest, kind); _removeGsdEntries(dest, kind);
_copyStaged(stagedForCopy, dest, kind); _copyStaged(item.sourceDir, dest, kind);
// Restore user-owned dirs after the prune+copy // Restore user-owned dirs after the prune+copy
for (const [dirName, snap] of toPreserve) { for (const [dirName, snap] of toPreserve) {
@@ -7074,13 +7066,13 @@ function installRuntimeArtifacts(runtime, configDir, scope, resolvedProfile) {
// For non-skills kinds (commands, agents): no user content to preserve; // For non-skills kinds (commands, agents): no user content to preserve;
// just prune stale gsd-* entries and copy new ones. // just prune stale gsd-* entries and copy new ones.
_removeGsdEntries(dest, kind); _removeGsdEntries(dest, kind);
_copyStaged(stagedForCopy, dest, kind); _copyStaged(item.sourceDir, dest, kind);
}
} finally {
if (tempToClean) {
try { fs.rmSync(tempToClean, { recursive: true, force: true }); } catch { /* best-effort */ }
} }
} }
} finally {
for (const dir of cleanupDirs) {
try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* best-effort */ }
}
} }
// Hermes: after the install loop has written all gsd-<stem>/ dirs to // Hermes: after the install loop has written all gsd-<stem>/ dirs to

View File

@@ -46,8 +46,91 @@ const REAL_COMMANDS_DIR = path.join(__dirname, '..', 'commands', 'gsd');
const MANIFEST = loadSkillsManifest(REAL_COMMANDS_DIR); const MANIFEST = loadSkillsManifest(REAL_COMMANDS_DIR);
const RESOLVED_CORE = resolveProfile({ modes: ['core'], manifest: MANIFEST }); const RESOLVED_CORE = resolveProfile({ modes: ['core'], manifest: MANIFEST });
function loadFreshInstallerWithInstallPlanStub(stub) {
const installPath = require.resolve('../bin/install.js');
const planPath = require.resolve('../gsd-core/bin/lib/runtime-artifact-install-plan.cjs');
const planModule = require(planPath);
const original = planModule.createRuntimeArtifactInstallPlan;
planModule.createRuntimeArtifactInstallPlan = stub;
delete require.cache[installPath];
const installer = require('../bin/install.js');
return {
installer,
restore() {
planModule.createRuntimeArtifactInstallPlan = original;
delete require.cache[installPath];
},
};
}
// ─── Section 6: installRuntimeArtifacts — parameterised layout loop ────────── // ─── Section 6: installRuntimeArtifacts — parameterised layout loop ──────────
describe('installRuntimeArtifacts — consumes Runtime Artifact Install Plan Module', () => {
test('executes returned copy items and cleanup obligations', (t) => {
const configDir = createTempDir('gsd-install-plan-adapter-');
const sourceDir = createTempDir('gsd-install-plan-source-');
const cleanupDir = createTempDir('gsd-install-plan-cleanup-');
t.after(() => {
cleanup(configDir);
cleanup(sourceDir);
cleanup(cleanupDir);
});
fs.writeFileSync(path.join(sourceDir, 'proof.md'), '# proof\n');
fs.writeFileSync(path.join(cleanupDir, 'temp.md'), '# cleanup\n');
let planArgs;
const { installer, restore } = loadFreshInstallerWithInstallPlanStub((args) => {
planArgs = args;
return {
ok: true,
plan: {
cleanupDirs: [cleanupDir],
items: [
{ kind: 'commands', sourceDir, destDir: path.join(configDir, 'commands', 'gsd') },
],
},
};
});
t.after(restore);
installer.installRuntimeArtifacts('gemini', configDir, 'global', RESOLVED_CORE);
assert.strictEqual(planArgs.layout.runtime, 'gemini');
assert.strictEqual(planArgs.layout.configDir, configDir);
assert.strictEqual(planArgs.layout.scope, 'global');
assert.strictEqual(planArgs.resolvedProfile, RESOLVED_CORE);
assert.strictEqual(planArgs.resolveAttribution('gemini'), undefined);
assert.ok(fs.existsSync(path.join(configDir, 'commands', 'gsd', 'proof.md')));
assert.ok(!fs.existsSync(cleanupDir), 'returned cleanup dir must be removed after copy');
});
test('cleans returned obligations when planning fails', (t) => {
const configDir = createTempDir('gsd-install-plan-fail-');
const cleanupDir = createTempDir('gsd-install-plan-fail-cleanup-');
t.after(() => {
cleanup(configDir);
cleanup(cleanupDir);
});
fs.writeFileSync(path.join(cleanupDir, 'temp.md'), '# cleanup\n');
const { installer, restore } = loadFreshInstallerWithInstallPlanStub(() => ({
ok: false,
kind: 'rewrite_failed',
failedKind: 'commands',
message: 'planned failure',
cleanupDirs: [cleanupDir],
}));
t.after(restore);
assert.throws(
() => installer.installRuntimeArtifacts('gemini', configDir, 'global', RESOLVED_CORE),
/planned failure/,
);
assert.ok(!fs.existsSync(cleanupDir), 'failure cleanup dir must be removed');
});
});
const SKILLS_RUNTIMES_LAYOUT = [ const SKILLS_RUNTIMES_LAYOUT = [
'claude', 'cursor', 'codex', 'copilot', 'antigravity', 'claude', 'cursor', 'codex', 'copilot', 'antigravity',
'windsurf', 'augment', 'trae', 'qwen', 'kimi', 'codebuddy', 'windsurf', 'augment', 'trae', 'qwen', 'kimi', 'codebuddy',