skillfit 0.15.3 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -0
- package/README.md +33 -25
- package/README.zh-CN.md +33 -25
- package/benches/code-review/ground-truth/r1.md +3 -3
- package/benches/code-review/verifiers/seeded-bugs.mjs +25 -10
- package/dist/agents/adapters/cli.js +4 -2
- package/dist/agents/mcp-probe.js +138 -13
- package/dist/bench/authoring.js +111 -3
- package/dist/cli/definition.js +31 -10
- package/dist/cli/doctor.js +33 -14
- package/dist/cli/eval-report.js +1 -1
- package/dist/cli/main.js +112 -6
- package/dist/cli/mcp.js +23 -7
- package/dist/cli/report.js +4 -1
- package/dist/composition.js +20 -0
- package/dist/configuration/bundle.js +150 -47
- package/dist/configuration/install.js +109 -23
- package/dist/configuration/lock.js +28 -0
- package/dist/configuration/mcp-write.js +311 -0
- package/dist/configuration/uninstall.js +241 -0
- package/dist/configuration/upstream.js +3 -0
- package/dist/evaluation/application/preparation.js +150 -0
- package/dist/evaluation/records/audit.js +3 -2
- package/dist/evaluation/records/session.js +16 -8
- package/docs/README.md +7 -7
- package/docs/guides/experiment-workflow.md +25 -0
- package/docs/guides/selectable-codex.md +35 -4
- package/docs/maintainers/architecture.md +1 -1
- package/docs/reference/eval-component.md +41 -2
- package/docs/reference/evaluation-reports.md +3 -1
- package/docs/schemas/doctor-report.schema.json +92 -0
- package/package.json +1 -1
- package/skills/skillfit/SKILL.md +33 -15
|
@@ -4,30 +4,40 @@ import { promises as fs } from "node:fs";
|
|
|
4
4
|
import * as os from "node:os";
|
|
5
5
|
import * as path from "node:path";
|
|
6
6
|
import { getAgent, skillUserDirSpecs } from "../agents/matrix.js";
|
|
7
|
+
import { selectAgentMcpProbe } from "../agents/mcp-probe.js";
|
|
7
8
|
import { collectReceipts } from "../usage/receipts.js";
|
|
8
9
|
import { parseFrontmatter } from "../resources/frontmatter.js";
|
|
9
10
|
import { isImplicitInvocationDisabled } from "../resources/invocation.js";
|
|
10
11
|
import { BACKUP_SUFFIX, loadProfile, MARKER_END, MARKER_START } from "./install.js";
|
|
12
|
+
import { portableMcpSpec } from "./mcp-write.js";
|
|
11
13
|
import { readUpstreamLock } from "./upstream.js";
|
|
12
14
|
const NAME_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
|
|
13
15
|
function expandHome(spec, homeDir) {
|
|
14
16
|
return spec.startsWith("~/") ? path.join(homeDir, spec.slice(2)) : path.resolve(spec);
|
|
15
17
|
}
|
|
16
|
-
function
|
|
17
|
-
const
|
|
18
|
+
async function userRulesFile(agentId, homeDir, env) {
|
|
19
|
+
const agent = getAgent(agentId);
|
|
20
|
+
const rules = agent.rules;
|
|
18
21
|
const override = rules.userHomeOverride;
|
|
19
22
|
const envHome = override === undefined ? undefined : env[override.env];
|
|
23
|
+
let candidates = rules.userFiles.map((spec) => expandHome(spec, homeDir));
|
|
20
24
|
if (override !== undefined && envHome)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
candidates = [path.resolve(envHome, override.relativePath)];
|
|
26
|
+
if (candidates.length === 0)
|
|
27
|
+
throw new Error(`${agent.displayName} has no user-level rules path`);
|
|
28
|
+
const existing = [];
|
|
29
|
+
for (const candidate of candidates)
|
|
30
|
+
if ((await readOptional(candidate)) !== null)
|
|
31
|
+
existing.push(candidate);
|
|
32
|
+
if (existing.length > 1)
|
|
33
|
+
throw new Error(`Multiple user-level rules files exist for ${agent.displayName}; refusing to choose implicitly: ${existing.join(", ")}`);
|
|
34
|
+
return existing[0] ?? candidates[0];
|
|
26
35
|
}
|
|
27
|
-
function
|
|
28
|
-
const
|
|
36
|
+
function userSkillDirs(agentId, homeDir, env) {
|
|
37
|
+
const agent = getAgent(agentId);
|
|
38
|
+
const specs = skillUserDirSpecs(agent, env);
|
|
29
39
|
if (specs.length === 0)
|
|
30
|
-
throw new Error(
|
|
40
|
+
throw new Error(`${agent.displayName} has no user-level skills path`);
|
|
31
41
|
return specs.map((spec) => expandHome(spec, homeDir));
|
|
32
42
|
}
|
|
33
43
|
async function readOptional(file) {
|
|
@@ -102,23 +112,98 @@ async function sha256(file) {
|
|
|
102
112
|
.update(await fs.readFile(file))
|
|
103
113
|
.digest("hex");
|
|
104
114
|
}
|
|
105
|
-
|
|
115
|
+
function pathInside(root, target) {
|
|
116
|
+
const relative = path.relative(root, target);
|
|
117
|
+
return (relative === "" ||
|
|
118
|
+
(relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)));
|
|
119
|
+
}
|
|
120
|
+
async function approvedSkillLinkRoots(inputs, homeDir) {
|
|
121
|
+
const roots = new Set();
|
|
122
|
+
if (inputs.length === 0)
|
|
123
|
+
return [];
|
|
124
|
+
const canonicalHome = await fs.realpath(path.resolve(homeDir));
|
|
125
|
+
for (const input of inputs) {
|
|
126
|
+
const root = await fs.realpath(path.resolve(input));
|
|
127
|
+
if (!(await fs.stat(root)).isDirectory())
|
|
128
|
+
throw new Error(`--allow-symlink-root is not a directory: ${root}`);
|
|
129
|
+
if (root === path.parse(root).root || root === canonicalHome)
|
|
130
|
+
throw new Error("--allow-symlink-root must name a specific Skill source directory, not a home or filesystem root");
|
|
131
|
+
roots.add(root);
|
|
132
|
+
}
|
|
133
|
+
return [...roots];
|
|
134
|
+
}
|
|
135
|
+
function assertApprovedLinkTarget(target, roots) {
|
|
136
|
+
if (!roots.some((root) => pathInside(root, target)))
|
|
137
|
+
throw new Error(`Skill symlink target is outside --allow-symlink-root: ${target}`);
|
|
138
|
+
}
|
|
139
|
+
async function locateSkill(dirs, name, allowedRoots) {
|
|
106
140
|
for (const dir of dirs) {
|
|
107
141
|
const src = path.join(dir, name);
|
|
142
|
+
let stat;
|
|
108
143
|
try {
|
|
109
|
-
|
|
110
|
-
throw new Error(`Symlinked skill cannot be exported safely: ${src}`);
|
|
111
|
-
}
|
|
112
|
-
return src;
|
|
144
|
+
stat = await fs.lstat(src);
|
|
113
145
|
}
|
|
114
146
|
catch (error) {
|
|
115
|
-
if (error.code
|
|
116
|
-
|
|
147
|
+
if (error.code === "ENOENT")
|
|
148
|
+
continue;
|
|
149
|
+
throw error;
|
|
117
150
|
}
|
|
151
|
+
if (stat.isSymbolicLink() && allowedRoots.length === 0)
|
|
152
|
+
throw new Error(`Symlinked skill cannot be exported safely: ${src}`);
|
|
153
|
+
const target = await fs.realpath(src);
|
|
154
|
+
if (stat.isSymbolicLink())
|
|
155
|
+
assertApprovedLinkTarget(target, allowedRoots);
|
|
156
|
+
if (!(await fs.stat(target)).isDirectory())
|
|
157
|
+
throw new Error(`Skill source is not a directory: ${target}`);
|
|
158
|
+
return target;
|
|
118
159
|
}
|
|
119
160
|
return null;
|
|
120
161
|
}
|
|
121
|
-
async function
|
|
162
|
+
async function walkSkillFiles(root, allowedRoots) {
|
|
163
|
+
const files = new Map();
|
|
164
|
+
const visit = async (dir, prefix, ancestors) => {
|
|
165
|
+
const realDir = await fs.realpath(dir);
|
|
166
|
+
if (!pathInside(root, realDir))
|
|
167
|
+
throw new Error(`Skill directory escapes its root: ${realDir}`);
|
|
168
|
+
if (ancestors.has(realDir))
|
|
169
|
+
throw new Error(`Skill symlink cycle detected at ${realDir}`);
|
|
170
|
+
const nextAncestors = new Set(ancestors).add(realDir);
|
|
171
|
+
for (const entry of await fs.readdir(realDir, { withFileTypes: true })) {
|
|
172
|
+
const absolute = path.join(realDir, entry.name);
|
|
173
|
+
const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
174
|
+
if (entry.isSymbolicLink()) {
|
|
175
|
+
if (allowedRoots.length === 0)
|
|
176
|
+
throw new Error(`Symlink in skill cannot be exported safely: ${absolute}`);
|
|
177
|
+
const target = await fs.realpath(absolute);
|
|
178
|
+
if (!pathInside(root, target))
|
|
179
|
+
throw new Error(`Skill symlink escapes the selected Skill: ${target}`);
|
|
180
|
+
assertApprovedLinkTarget(target, allowedRoots);
|
|
181
|
+
const stat = await fs.stat(target);
|
|
182
|
+
if (stat.isDirectory())
|
|
183
|
+
await visit(target, relative, nextAncestors);
|
|
184
|
+
else if (stat.isFile())
|
|
185
|
+
files.set(relative, target);
|
|
186
|
+
else
|
|
187
|
+
throw new Error(`Unsupported file in Skill: ${target}`);
|
|
188
|
+
}
|
|
189
|
+
else if (entry.isDirectory()) {
|
|
190
|
+
await visit(absolute, relative, nextAncestors);
|
|
191
|
+
}
|
|
192
|
+
else if (entry.isFile()) {
|
|
193
|
+
const target = await fs.realpath(absolute);
|
|
194
|
+
if (!pathInside(root, target))
|
|
195
|
+
throw new Error(`Skill file escapes its root: ${target}`);
|
|
196
|
+
files.set(relative, target);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
throw new Error(`Unsupported file in Skill: ${absolute}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
await visit(root, "", new Set());
|
|
204
|
+
return new Map([...files].sort(([a], [b]) => a.localeCompare(b)));
|
|
205
|
+
}
|
|
206
|
+
async function verifyUpstreamExport(dir, manifest, lock, rules, sourceFiles) {
|
|
122
207
|
const actualManifest = JSON.parse(await fs.readFile(path.join(dir, "profile.json"), "utf8"));
|
|
123
208
|
const actualLock = await readUpstreamLock(path.join(dir, "upstream.lock.json"));
|
|
124
209
|
if (JSON.stringify(actualManifest) !== JSON.stringify(manifest) ||
|
|
@@ -132,26 +217,24 @@ async function verifyUpstreamExport(dir, manifest, lock, rules, sourceFiles, ski
|
|
|
132
217
|
throw new Error("Portable export verification failed: setup script is missing");
|
|
133
218
|
}
|
|
134
219
|
for (const [name, files] of sourceFiles) {
|
|
135
|
-
for (const rel of files) {
|
|
136
|
-
|
|
137
|
-
if (!source ||
|
|
138
|
-
(await sha256(path.join(dir, "skills", name, rel))) !==
|
|
139
|
-
(await sha256(path.join(source, rel)))) {
|
|
220
|
+
for (const [rel, source] of files) {
|
|
221
|
+
if ((await sha256(path.join(dir, "skills", name, rel))) !== (await sha256(source))) {
|
|
140
222
|
throw new Error(`Portable export verification failed for ${name}/${rel}`);
|
|
141
223
|
}
|
|
142
224
|
}
|
|
143
225
|
}
|
|
144
226
|
}
|
|
145
|
-
async function defaultUsage(homeDir, env) {
|
|
146
|
-
const receipt = (await collectReceipts({ agent
|
|
227
|
+
async function defaultUsage(agent, homeDir, env) {
|
|
228
|
+
const receipt = (await collectReceipts({ agent, homeDir, env }))[0];
|
|
147
229
|
const installed = new Set(receipt?.installed ?? []);
|
|
148
230
|
return (receipt?.skills
|
|
149
231
|
.filter(({ name }) => installed.has(name))
|
|
150
232
|
.map(({ name, sessionCount }) => ({ name, sessionCount })) ?? []);
|
|
151
233
|
}
|
|
152
|
-
/** Create a self-contained profile from user-level
|
|
234
|
+
/** Create a self-contained profile from user-level Skills and active global guidance. */
|
|
153
235
|
export async function runBundleExport(opts) {
|
|
154
236
|
const log = opts.log ?? (() => { });
|
|
237
|
+
const sourceAgent = getAgent(opts.agent ?? "codex");
|
|
155
238
|
const homeDir = opts.homeDir ?? os.homedir();
|
|
156
239
|
const env = opts.env ?? (opts.homeDir === undefined ? process.env : {});
|
|
157
240
|
const outputDir = path.resolve(opts.outputDir);
|
|
@@ -159,12 +242,13 @@ export async function runBundleExport(opts) {
|
|
|
159
242
|
if (!NAME_PATTERN.test(profileName)) {
|
|
160
243
|
throw new Error("Bundle directory name must use lowercase letters, digits and dashes");
|
|
161
244
|
}
|
|
162
|
-
const skillDirs =
|
|
245
|
+
const skillDirs = userSkillDirs(sourceAgent.id, homeDir, env);
|
|
246
|
+
const linkRoots = await approvedSkillLinkRoots(opts.allowedSkillLinkRoots ?? [], homeDir);
|
|
163
247
|
const upstreamLock = opts.upstreamLockPath
|
|
164
248
|
? await readUpstreamLock(path.resolve(opts.upstreamLockPath))
|
|
165
249
|
: null;
|
|
166
250
|
const usage = opts.skillNames === undefined && !upstreamLock
|
|
167
|
-
? await (opts.loadUsage ?? (() => defaultUsage(homeDir, env)))()
|
|
251
|
+
? await (opts.loadUsage ?? (() => defaultUsage(sourceAgent.id, homeDir, env)))()
|
|
168
252
|
: [];
|
|
169
253
|
const minSessions = opts.minSessions ?? 2;
|
|
170
254
|
if (!Number.isInteger(minSessions) || minSessions < 1)
|
|
@@ -183,7 +267,6 @@ export async function runBundleExport(opts) {
|
|
|
183
267
|
const explicitOnly = [];
|
|
184
268
|
const skipped = [];
|
|
185
269
|
const sourceFiles = new Map();
|
|
186
|
-
const skillSources = new Map();
|
|
187
270
|
const upstreamByName = new Map((upstreamLock?.skills ?? []).map((skill) => [skill.name, skill]));
|
|
188
271
|
const selectedUpstream = [];
|
|
189
272
|
for (const name of candidates) {
|
|
@@ -197,7 +280,7 @@ export async function runBundleExport(opts) {
|
|
|
197
280
|
explicitOnly.push(name);
|
|
198
281
|
continue;
|
|
199
282
|
}
|
|
200
|
-
const src = await locateSkill(skillDirs, name);
|
|
283
|
+
const src = await locateSkill(skillDirs, name, linkRoots);
|
|
201
284
|
if (src === null) {
|
|
202
285
|
if (opts.skillNames === undefined && !upstreamLock) {
|
|
203
286
|
skipped.push(name);
|
|
@@ -205,29 +288,45 @@ export async function runBundleExport(opts) {
|
|
|
205
288
|
}
|
|
206
289
|
throw new Error(`Skill "${name}" is not installed in ${skillDirs.join(", ")}`);
|
|
207
290
|
}
|
|
208
|
-
const files = (await
|
|
291
|
+
const files = new Map([...(await walkSkillFiles(src, linkRoots))].filter(([file]) => !isInstallerArtifact(file)));
|
|
209
292
|
const manifest = await readOptional(path.join(src, "SKILL.md"));
|
|
210
293
|
const frontmatter = manifest === null ? null : parseFrontmatter(manifest);
|
|
211
294
|
if (frontmatter?.name !== name || !frontmatter.description) {
|
|
212
295
|
throw new Error(`Skill "${name}" has an invalid SKILL.md`);
|
|
213
296
|
}
|
|
214
297
|
names.push(name);
|
|
215
|
-
skillSources.set(name, src);
|
|
216
298
|
const codexMetadata = await readOptional(path.join(src, "agents", "openai.yaml"));
|
|
217
299
|
if (isImplicitInvocationDisabled(codexMetadata))
|
|
218
300
|
explicitOnly.push(name);
|
|
219
301
|
sourceFiles.set(name, files);
|
|
220
302
|
}
|
|
221
|
-
const
|
|
222
|
-
const
|
|
303
|
+
const mcpSpecs = [];
|
|
304
|
+
for (const name of [...new Set(opts.mcpServerNames ?? [])].sort()) {
|
|
305
|
+
if (!NAME_PATTERN.test(name))
|
|
306
|
+
throw new Error(`Invalid portable MCP server name: ${name}`);
|
|
307
|
+
const selected = selectAgentMcpProbe({
|
|
308
|
+
agent: sourceAgent.id,
|
|
309
|
+
server: name,
|
|
310
|
+
homeDir,
|
|
311
|
+
cwd: homeDir,
|
|
312
|
+
env,
|
|
313
|
+
});
|
|
314
|
+
mcpSpecs.push({ name, spec: portableMcpSpec(selected.spec, selected.entry) });
|
|
315
|
+
}
|
|
316
|
+
const rulesFile = await userRulesFile(sourceAgent.id, homeDir, env);
|
|
317
|
+
const overrideName = sourceAgent.rules.userHomeOverride?.overrideRelativePath;
|
|
223
318
|
const activeOverride = overrideName ? path.join(path.dirname(rulesFile), overrideName) : null;
|
|
224
319
|
const overrideRules = activeOverride ? await readOptional(activeOverride) : null;
|
|
225
320
|
const useOverride = overrideRules !== null && overrideRules.trim().length > 0;
|
|
226
321
|
const sourceRules = useOverride ? overrideRules : await readOptional(rulesFile);
|
|
227
|
-
const rules = renderRules(sourceRules
|
|
228
|
-
log(`Portable
|
|
322
|
+
const rules = renderRules(sourceRules ?? `# Global ${sourceAgent.displayName} guidance`, names, explicitOnly);
|
|
323
|
+
log(`Portable ${sourceAgent.displayName} profile: ${outputDir}`);
|
|
229
324
|
log(`Global rules source: ${sourceRules === null ? "generated defaults" : useOverride ? activeOverride : rulesFile}`);
|
|
230
325
|
log(`Skills (${names.length}): ${names.join(", ") || "(none)"}`);
|
|
326
|
+
if (linkRoots.length > 0)
|
|
327
|
+
log(`Allowed Skill symlink roots: ${linkRoots.join(", ")}`);
|
|
328
|
+
if (mcpSpecs.length > 0)
|
|
329
|
+
log(`MCP declarations (${mcpSpecs.length}): ${mcpSpecs.map((entry) => entry.name).join(", ")}`);
|
|
231
330
|
if (explicitOnly.length > 0)
|
|
232
331
|
log(`User-invoked-only skills (${explicitOnly.length}): ${explicitOnly.join(", ")}`);
|
|
233
332
|
if (skipped.length > 0)
|
|
@@ -264,11 +363,14 @@ export async function runBundleExport(opts) {
|
|
|
264
363
|
const manifest = {
|
|
265
364
|
name: profileName,
|
|
266
365
|
version: "1.0.0",
|
|
267
|
-
description:
|
|
268
|
-
agents: [
|
|
366
|
+
description: `Portable ${sourceAgent.displayName} setup exported from a user environment.`,
|
|
367
|
+
agents: [sourceAgent.id],
|
|
269
368
|
scope: "user",
|
|
270
369
|
rules: { template: "rules/global.md" },
|
|
271
370
|
skills: names.map((name) => ({ name, source: `skills/${name}` })),
|
|
371
|
+
...(mcpSpecs.length > 0
|
|
372
|
+
? { mcp: mcpSpecs.map(({ name }) => ({ name, source: `mcp/${name}.json` })) }
|
|
373
|
+
: {}),
|
|
272
374
|
};
|
|
273
375
|
const exportedLock = {
|
|
274
376
|
version: 1,
|
|
@@ -284,6 +386,11 @@ export async function runBundleExport(opts) {
|
|
|
284
386
|
await fs.writeFile(path.join(stage, "rules", "global.md"), rules, "utf8");
|
|
285
387
|
await fs.writeFile(path.join(stage, "profile.json"), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
286
388
|
await fs.writeFile(path.join(stage, "package.json"), '{"private":true,"type":"module"}\n');
|
|
389
|
+
for (const { name, spec } of mcpSpecs) {
|
|
390
|
+
const target = path.join(stage, "mcp", `${name}.json`);
|
|
391
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
392
|
+
await fs.writeFile(target, `${JSON.stringify(spec, null, 2)}\n`);
|
|
393
|
+
}
|
|
287
394
|
if (upstreamLock) {
|
|
288
395
|
await fs.writeFile(path.join(stage, "upstream.lock.json"), `${JSON.stringify(exportedLock, null, 2)}\n`);
|
|
289
396
|
await fs.writeFile(path.join(stage, "setup.mjs"), [
|
|
@@ -295,7 +402,7 @@ export async function runBundleExport(opts) {
|
|
|
295
402
|
"const { values } = parseArgs({ args: process.argv.slice(2), options: { 'dry-run': { type: 'boolean' }, yes: { type: 'boolean' }, strict: { type: 'boolean' } } });",
|
|
296
403
|
"const prepared = await prepareUpstreamProfile(profileDir, { log: console.log });",
|
|
297
404
|
"try {",
|
|
298
|
-
|
|
405
|
+
` await runInstall({ profile: 'portable', profilePath: prepared.profileDir, agent: ${JSON.stringify(sourceAgent.id)}, dryRun: values['dry-run'] ?? false, yes: values.yes ?? false, strict: values.strict ?? false });`,
|
|
299
406
|
"} finally { await prepared.cleanup(); }",
|
|
300
407
|
"",
|
|
301
408
|
].join("\n"));
|
|
@@ -305,7 +412,7 @@ export async function runBundleExport(opts) {
|
|
|
305
412
|
"import { fileURLToPath } from 'node:url';",
|
|
306
413
|
"const profileDir = fileURLToPath(new URL('.', import.meta.url));",
|
|
307
414
|
"const cli = fileURLToPath(new URL('./runtime/cli.js', import.meta.url));",
|
|
308
|
-
|
|
415
|
+
`process.argv = [process.argv[0], cli, 'install', '--profile-path', profileDir, '--agent', ${JSON.stringify(sourceAgent.id)}, ...process.argv.slice(2)];`,
|
|
309
416
|
"await import('./runtime/cli.js');",
|
|
310
417
|
"",
|
|
311
418
|
].join("\n"));
|
|
@@ -320,11 +427,7 @@ export async function runBundleExport(opts) {
|
|
|
320
427
|
throw new Error(`Runtime export verification failed for ${rel}`);
|
|
321
428
|
}
|
|
322
429
|
for (const [name, files] of sourceFiles) {
|
|
323
|
-
for (const rel of files) {
|
|
324
|
-
const root = skillSources.get(name);
|
|
325
|
-
if (!root)
|
|
326
|
-
throw new Error(`Export is missing the source directory for ${name}`);
|
|
327
|
-
const src = path.join(root, rel);
|
|
430
|
+
for (const [rel, src] of files) {
|
|
328
431
|
const dst = path.join(stage, "skills", name, rel);
|
|
329
432
|
await fs.mkdir(path.dirname(dst), { recursive: true });
|
|
330
433
|
await fs.copyFile(src, dst);
|
|
@@ -333,7 +436,7 @@ export async function runBundleExport(opts) {
|
|
|
333
436
|
}
|
|
334
437
|
}
|
|
335
438
|
if (upstreamLock)
|
|
336
|
-
await verifyUpstreamExport(stage, manifest, exportedLock, rules, sourceFiles
|
|
439
|
+
await verifyUpstreamExport(stage, manifest, exportedLock, rules, sourceFiles);
|
|
337
440
|
else
|
|
338
441
|
await loadProfile(stageRoot, profileName);
|
|
339
442
|
await fs.rename(stage, outputDir);
|
|
@@ -344,7 +447,7 @@ export async function runBundleExport(opts) {
|
|
|
344
447
|
}
|
|
345
448
|
await fs.rmdir(stageRoot);
|
|
346
449
|
if (upstreamLock)
|
|
347
|
-
await verifyUpstreamExport(outputDir, manifest, exportedLock, rules, sourceFiles
|
|
450
|
+
await verifyUpstreamExport(outputDir, manifest, exportedLock, rules, sourceFiles);
|
|
348
451
|
else
|
|
349
452
|
await loadProfile(path.dirname(outputDir), profileName);
|
|
350
453
|
log(upstreamLock
|
|
@@ -7,11 +7,13 @@ import * as path from "node:path";
|
|
|
7
7
|
import { getAgent } from "../agents/matrix.js";
|
|
8
8
|
import { parseFrontmatter } from "../resources/frontmatter.js";
|
|
9
9
|
import { BACKUP_SUFFIX } from "./constants.js";
|
|
10
|
+
import { LOCKFILE_NAME, readLockfile } from "./lock.js";
|
|
11
|
+
import { loadPortableMcpSpec, planMcpApply } from "./mcp-write.js";
|
|
10
12
|
import { executeTransaction } from "./transaction.js";
|
|
11
13
|
export const MARKER_START = "<!-- SKILLFIT_START -->";
|
|
12
14
|
export const MARKER_END = "<!-- SKILLFIT_END -->";
|
|
13
15
|
export { BACKUP_SUFFIX } from "./constants.js";
|
|
14
|
-
export
|
|
16
|
+
export { LOCKFILE_NAME, readLockfile } from "./lock.js";
|
|
15
17
|
const NAME_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
|
|
16
18
|
export async function runInstall(opts) {
|
|
17
19
|
const log = opts.log ?? (() => { });
|
|
@@ -38,7 +40,11 @@ export async function runInstall(opts) {
|
|
|
38
40
|
for (const skill of manifest.skills ?? []) {
|
|
39
41
|
items.push(await planSkillItem(agent, skill, profileDir, scope, homeDir, projectDir, lock));
|
|
40
42
|
}
|
|
43
|
+
for (const mcp of manifest.mcp ?? []) {
|
|
44
|
+
items.push(await planMcpItem(agent, mcp, profileDir, scope, homeDir, projectDir, env));
|
|
45
|
+
}
|
|
41
46
|
}
|
|
47
|
+
mergeMcpFiles(items);
|
|
42
48
|
printPlan(log, manifest, scope, scopeDir, items);
|
|
43
49
|
const conflicts = items.filter((i) => i.action === "conflict");
|
|
44
50
|
if (conflicts.length > 0) {
|
|
@@ -72,11 +78,22 @@ export async function runInstall(opts) {
|
|
|
72
78
|
}
|
|
73
79
|
recordEntries(lock, items, manifest, now().toISOString());
|
|
74
80
|
const lockContent = `${JSON.stringify(lock, null, 2)}\n`;
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
const filesByPath = new Map();
|
|
82
|
+
for (const item of writes) {
|
|
83
|
+
for (const file of item.files) {
|
|
84
|
+
const next = {
|
|
85
|
+
path: file.path,
|
|
86
|
+
content: file.content,
|
|
87
|
+
backup: file.backup ? "first" : "none",
|
|
88
|
+
};
|
|
89
|
+
const previous = filesByPath.get(file.path);
|
|
90
|
+
if (previous && !Buffer.from(previous.content).equals(Buffer.from(next.content)))
|
|
91
|
+
throw new Error(`Install plan has conflicting writes for ${file.path}`);
|
|
92
|
+
if (!previous)
|
|
93
|
+
filesByPath.set(file.path, next);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const files = [...filesByPath.values()];
|
|
80
97
|
// Metadata is committed last and backs up its immediately previous version.
|
|
81
98
|
files.push({
|
|
82
99
|
path: lockPath,
|
|
@@ -160,6 +177,21 @@ function validateManifest(manifest, expectedName) {
|
|
|
160
177
|
if (typeof skill.source !== "string")
|
|
161
178
|
throw new Error(`Skill "${skill.name}" must declare a source path`);
|
|
162
179
|
}
|
|
180
|
+
if (manifest.mcp !== undefined && !Array.isArray(manifest.mcp)) {
|
|
181
|
+
throw new Error("profile.json mcp must be an array");
|
|
182
|
+
}
|
|
183
|
+
const mcpNames = new Set();
|
|
184
|
+
for (const mcp of manifest.mcp ?? []) {
|
|
185
|
+
if (typeof mcp !== "object" || mcp === null)
|
|
186
|
+
throw new Error("profile.json mcp must contain objects");
|
|
187
|
+
if (!NAME_PATTERN.test(mcp.name))
|
|
188
|
+
throw new Error(`Invalid MCP name "${mcp.name}" in profile.json`);
|
|
189
|
+
if (mcpNames.has(mcp.name))
|
|
190
|
+
throw new Error(`Duplicate MCP "${mcp.name}" in profile.json`);
|
|
191
|
+
mcpNames.add(mcp.name);
|
|
192
|
+
if (typeof mcp.source !== "string")
|
|
193
|
+
throw new Error(`MCP "${mcp.name}" must declare a source path`);
|
|
194
|
+
}
|
|
163
195
|
}
|
|
164
196
|
async function validateProfileFiles(dir, manifest) {
|
|
165
197
|
if (manifest.rules)
|
|
@@ -178,6 +210,11 @@ async function validateProfileFiles(dir, manifest) {
|
|
|
178
210
|
throw new Error(`Skill "${skill.name}" SKILL.md declares name "${fm.name}"`);
|
|
179
211
|
}
|
|
180
212
|
}
|
|
213
|
+
for (const mcp of manifest.mcp ?? []) {
|
|
214
|
+
const spec = await loadPortableMcpSpec(await safeProfileFile(dir, mcp.source));
|
|
215
|
+
if (spec.name !== mcp.name)
|
|
216
|
+
throw new Error(`MCP "${mcp.name}" source declares name "${spec.name}"`);
|
|
217
|
+
}
|
|
181
218
|
}
|
|
182
219
|
async function readProfileFile(dir, rel) {
|
|
183
220
|
return await fs.readFile(await safeProfileFile(dir, rel), "utf8");
|
|
@@ -255,6 +292,14 @@ function extractBlock(content, targetPath) {
|
|
|
255
292
|
}
|
|
256
293
|
return content.slice(start, end + MARKER_END.length);
|
|
257
294
|
}
|
|
295
|
+
export function removeManagedBlock(content) {
|
|
296
|
+
const block = extractBlock(content, "managed rules");
|
|
297
|
+
if (block === null)
|
|
298
|
+
throw new Error("No skillfit-managed rules block was found");
|
|
299
|
+
const start = content.indexOf(MARKER_START);
|
|
300
|
+
const end = content.indexOf(MARKER_END) + MARKER_END.length;
|
|
301
|
+
return `${content.slice(0, start)}${content.slice(end)}`;
|
|
302
|
+
}
|
|
258
303
|
function classifyBlock(existing, block, targetPath) {
|
|
259
304
|
if (existing === null)
|
|
260
305
|
return {
|
|
@@ -314,7 +359,7 @@ function resolveScoped(rel, scope, homeDir, projectDir) {
|
|
|
314
359
|
return expandHome(rel, homeDir);
|
|
315
360
|
return path.resolve(projectDir, rel);
|
|
316
361
|
}
|
|
317
|
-
function rulesTarget(agent, scope, homeDir, projectDir, env) {
|
|
362
|
+
export function rulesTarget(agent, scope, homeDir, projectDir, env) {
|
|
318
363
|
const rel = scope === "user" ? agent.rules.userFiles[0] : agent.rules.projectFiles[0];
|
|
319
364
|
if (!rel)
|
|
320
365
|
throw new Error(`${agent.displayName} has no ${scope}-level rules file in the matrix`);
|
|
@@ -332,7 +377,7 @@ function rulesTarget(agent, scope, homeDir, projectDir, env) {
|
|
|
332
377
|
importLine: bridge.importLine,
|
|
333
378
|
};
|
|
334
379
|
}
|
|
335
|
-
function skillsTargetDir(agent, scope, homeDir, projectDir) {
|
|
380
|
+
export function skillsTargetDir(agent, scope, homeDir, projectDir) {
|
|
336
381
|
const shared = agent.skills.sharedDirs.find((dir) => scope === "user" ? dir.startsWith("~/") : !dir.startsWith("~"));
|
|
337
382
|
if (shared)
|
|
338
383
|
return resolveScoped(shared, scope, homeDir, projectDir);
|
|
@@ -361,21 +406,62 @@ async function readBufferIfExists(p) {
|
|
|
361
406
|
throw error;
|
|
362
407
|
}
|
|
363
408
|
}
|
|
364
|
-
async function
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
409
|
+
async function planMcpItem(agent, mcp, profileDir, scope, homeDir, projectDir, env) {
|
|
410
|
+
const plan = await planMcpApply({
|
|
411
|
+
agent: agent.id,
|
|
412
|
+
server: mcp.name,
|
|
413
|
+
from: await safeProfileFile(profileDir, mcp.source),
|
|
414
|
+
portable: true,
|
|
415
|
+
project: scope === "project",
|
|
416
|
+
homeDir,
|
|
417
|
+
projectDir,
|
|
418
|
+
env,
|
|
419
|
+
dryRun: true,
|
|
420
|
+
});
|
|
421
|
+
if (plan.action === "remove")
|
|
422
|
+
throw new Error("Profile MCP planning cannot remove an entry");
|
|
423
|
+
return {
|
|
424
|
+
agent: agent.id,
|
|
425
|
+
kind: "mcp",
|
|
426
|
+
label: `mcp "${mcp.name}"`,
|
|
427
|
+
targetPath: plan.path,
|
|
428
|
+
lockKey: plan.lockKey,
|
|
429
|
+
sha256: plan.entryHash,
|
|
430
|
+
action: plan.action,
|
|
431
|
+
detail: `MCP "${mcp.name}": ${plan.detail}`,
|
|
432
|
+
files: plan.nextContent === undefined
|
|
433
|
+
? []
|
|
434
|
+
: [
|
|
435
|
+
{
|
|
436
|
+
path: plan.path,
|
|
437
|
+
content: plan.nextContent,
|
|
438
|
+
backup: (await readIfExists(plan.path)) !== null,
|
|
439
|
+
},
|
|
440
|
+
],
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
function mergeMcpFiles(items) {
|
|
444
|
+
const byTarget = new Map();
|
|
445
|
+
for (const item of items) {
|
|
446
|
+
if (item.kind !== "mcp")
|
|
447
|
+
continue;
|
|
448
|
+
for (const file of item.files) {
|
|
449
|
+
const files = byTarget.get(file.path) ?? [];
|
|
450
|
+
files.push(file);
|
|
451
|
+
byTarget.set(file.path, files);
|
|
452
|
+
}
|
|
374
453
|
}
|
|
375
|
-
|
|
376
|
-
|
|
454
|
+
for (const files of byTarget.values()) {
|
|
455
|
+
if (files.length < 2)
|
|
456
|
+
continue;
|
|
457
|
+
const documents = files.map((file) => JSON.parse(String(file.content)));
|
|
458
|
+
let servers = { ...documents[0].mcpServers };
|
|
459
|
+
for (const document of documents.slice(1))
|
|
460
|
+
servers = { ...servers, ...document.mcpServers };
|
|
461
|
+
const content = `${JSON.stringify({ ...documents[0], mcpServers: servers }, null, 2)}\n`;
|
|
462
|
+
for (const file of files)
|
|
463
|
+
file.content = content;
|
|
377
464
|
}
|
|
378
|
-
return { version: 1, entries: parsed.entries ?? {} };
|
|
379
465
|
}
|
|
380
466
|
async function planRulesItems(agent, manifest, profileDir, scope, homeDir, projectDir, env) {
|
|
381
467
|
if (!manifest.rules)
|
|
@@ -559,7 +645,7 @@ async function walkFiles(dir, prefix = "") {
|
|
|
559
645
|
}
|
|
560
646
|
return out.sort();
|
|
561
647
|
}
|
|
562
|
-
function hashFiles(entries) {
|
|
648
|
+
export function hashFiles(entries) {
|
|
563
649
|
const hash = createHash("sha256");
|
|
564
650
|
for (const [rel, content] of [...entries].sort(([a], [b]) => a.localeCompare(b))) {
|
|
565
651
|
hash.update(rel, "utf8");
|
|
@@ -605,7 +691,7 @@ async function validateWrites(writes) {
|
|
|
605
691
|
problems.push(`${skillMd}: SKILL.md is missing or its frontmatter lacks name/description`);
|
|
606
692
|
}
|
|
607
693
|
}
|
|
608
|
-
else {
|
|
694
|
+
else if (item.kind !== "mcp") {
|
|
609
695
|
const content = await readIfExists(item.targetPath);
|
|
610
696
|
if (content === null || !content.includes(MARKER_START) || !content.includes(MARKER_END)) {
|
|
611
697
|
problems.push(`${item.targetPath}: managed block missing after write`);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
export const LOCKFILE_NAME = "skillfit.lock.json";
|
|
3
|
+
async function readIfExists(path) {
|
|
4
|
+
try {
|
|
5
|
+
return await fs.readFile(path, "utf8");
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
if (error.code === "ENOENT")
|
|
9
|
+
return null;
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export async function readLockfile(lockPath) {
|
|
14
|
+
const raw = await readIfExists(lockPath);
|
|
15
|
+
if (raw === null)
|
|
16
|
+
return { version: 1, entries: {} };
|
|
17
|
+
let parsed;
|
|
18
|
+
try {
|
|
19
|
+
parsed = JSON.parse(raw);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new Error(`Lockfile at ${lockPath} is not valid JSON; fix or delete it.`);
|
|
23
|
+
}
|
|
24
|
+
if (typeof parsed !== "object" || parsed === null || typeof parsed.entries !== "object") {
|
|
25
|
+
throw new Error(`Lockfile at ${lockPath} has an unexpected shape; fix or delete it.`);
|
|
26
|
+
}
|
|
27
|
+
return { version: 1, entries: parsed.entries ?? {} };
|
|
28
|
+
}
|