@agentic-sdlc-kit/cli 0.8.0 → 0.9.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/dist/application/agent/execution-store.d.ts.map +1 -1
- package/dist/application/agent/execution-store.js +2 -1
- package/dist/application/agent/execution-store.js.map +1 -1
- package/dist/application/agent/orchestrator.d.ts.map +1 -1
- package/dist/application/agent/orchestrator.js +2 -2
- package/dist/application/agent/orchestrator.js.map +1 -1
- package/dist/application/dispatcher.d.ts.map +1 -1
- package/dist/application/dispatcher.js +12 -0
- package/dist/application/dispatcher.js.map +1 -1
- package/dist/application/version.d.ts +1 -1
- package/dist/application/version.js +1 -1
- package/dist/commands/attach-exclude.d.ts +8 -0
- package/dist/commands/attach-exclude.d.ts.map +1 -0
- package/dist/commands/attach-exclude.js +48 -0
- package/dist/commands/attach-exclude.js.map +1 -0
- package/dist/commands/attach-git.d.ts +11 -0
- package/dist/commands/attach-git.d.ts.map +1 -0
- package/dist/commands/attach-git.js +40 -0
- package/dist/commands/attach-git.js.map +1 -0
- package/dist/commands/attach-store.d.ts +10 -0
- package/dist/commands/attach-store.d.ts.map +1 -0
- package/dist/commands/attach-store.js +107 -0
- package/dist/commands/attach-store.js.map +1 -0
- package/dist/commands/attach.d.ts +8 -0
- package/dist/commands/attach.d.ts.map +1 -0
- package/dist/commands/attach.js +124 -0
- package/dist/commands/attach.js.map +1 -0
- package/dist/commands/fix.d.ts +1 -0
- package/dist/commands/fix.d.ts.map +1 -1
- package/dist/commands/fix.js +42 -50
- package/dist/commands/fix.js.map +1 -1
- package/dist/commands/host-apply.d.ts +12 -0
- package/dist/commands/host-apply.d.ts.map +1 -0
- package/dist/commands/host-apply.js +43 -0
- package/dist/commands/host-apply.js.map +1 -0
- package/dist/commands/host.d.ts +3 -0
- package/dist/commands/host.d.ts.map +1 -0
- package/dist/commands/host.js +107 -0
- package/dist/commands/host.js.map +1 -0
- package/dist/commands/index.d.ts +4 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +4 -0
- package/dist/commands/index.js.map +1 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +5 -3
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/install.d.ts.map +1 -1
- package/dist/commands/install.js +15 -7
- package/dist/commands/install.js.map +1 -1
- package/dist/commands/plugin-linker.d.ts +10 -1
- package/dist/commands/plugin-linker.d.ts.map +1 -1
- package/dist/commands/plugin-linker.js +53 -14
- package/dist/commands/plugin-linker.js.map +1 -1
- package/dist/commands/skills-install-commit.d.ts +34 -0
- package/dist/commands/skills-install-commit.d.ts.map +1 -0
- package/dist/commands/skills-install-commit.js +113 -0
- package/dist/commands/skills-install-commit.js.map +1 -0
- package/dist/commands/skills-install-manifest.d.ts +35 -0
- package/dist/commands/skills-install-manifest.d.ts.map +1 -0
- package/dist/commands/skills-install-manifest.js +89 -0
- package/dist/commands/skills-install-manifest.js.map +1 -0
- package/dist/commands/skills-install-report.d.ts +12 -0
- package/dist/commands/skills-install-report.d.ts.map +1 -0
- package/dist/commands/skills-install-report.js +44 -0
- package/dist/commands/skills-install-report.js.map +1 -0
- package/dist/commands/skills-install-secure.d.ts +14 -0
- package/dist/commands/skills-install-secure.d.ts.map +1 -0
- package/dist/commands/skills-install-secure.js +96 -0
- package/dist/commands/skills-install-secure.js.map +1 -0
- package/dist/commands/skills-install.d.ts +15 -0
- package/dist/commands/skills-install.d.ts.map +1 -0
- package/dist/commands/skills-install.js +182 -0
- package/dist/commands/skills-install.js.map +1 -0
- package/dist/commands/skills.d.ts +3 -0
- package/dist/commands/skills.d.ts.map +1 -0
- package/dist/commands/skills.js +217 -0
- package/dist/commands/skills.js.map +1 -0
- package/dist/commands/uninstall.d.ts +3 -0
- package/dist/commands/uninstall.d.ts.map +1 -0
- package/dist/commands/uninstall.js +113 -0
- package/dist/commands/uninstall.js.map +1 -0
- package/package.json +4 -4
- package/src/application/agent/execution-store.ts +2 -1
- package/src/application/agent/orchestrator.ts +2 -1
- package/src/application/dispatcher.test.ts +22 -3
- package/src/application/dispatcher.ts +8 -0
- package/src/application/version.ts +1 -1
- package/src/commands/attach-exclude.ts +50 -0
- package/src/commands/attach-git.ts +50 -0
- package/src/commands/attach-store.ts +107 -0
- package/src/commands/attach.test.ts +192 -0
- package/src/commands/attach.ts +143 -0
- package/src/commands/doctor.test.ts +1 -1
- package/src/commands/fix.test.ts +95 -13
- package/src/commands/fix.ts +44 -55
- package/src/commands/host-apply.ts +61 -0
- package/src/commands/host.test.ts +120 -0
- package/src/commands/host.ts +125 -0
- package/src/commands/index.ts +4 -0
- package/src/commands/init.test.ts +22 -0
- package/src/commands/init.ts +10 -3
- package/src/commands/install.test.ts +31 -0
- package/src/commands/install.ts +21 -7
- package/src/commands/plugin-linker.test.ts +80 -0
- package/src/commands/plugin-linker.ts +62 -14
- package/src/commands/skills-install-commit.ts +149 -0
- package/src/commands/skills-install-manifest.ts +124 -0
- package/src/commands/skills-install-report.ts +60 -0
- package/src/commands/skills-install-secure.ts +113 -0
- package/src/commands/skills-install.ts +245 -0
- package/src/commands/skills.test.ts +449 -0
- package/src/commands/skills.ts +247 -0
- package/src/commands/uninstall.test.ts +122 -0
- package/src/commands/uninstall.ts +116 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// packages/cli/src/commands/skills-install-commit.ts
|
|
2
|
+
//
|
|
3
|
+
// The staged-commit half of `kit skills install` (see ./skills-install.ts).
|
|
4
|
+
//
|
|
5
|
+
// Order of operations: plan (decide per skill, nothing written yet) →
|
|
6
|
+
// stage (copy into .ai-kit/tmp staging and validate) → commit (rename into
|
|
7
|
+
// place, replaced directories moved aside first). A commit failure rolls
|
|
8
|
+
// back everything this commit changed; if the rollback itself fails, the
|
|
9
|
+
// staging directory is kept for manual recovery instead of being deleted.
|
|
10
|
+
|
|
11
|
+
import { promises as fs } from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import {
|
|
14
|
+
type SkillInstallStatus,
|
|
15
|
+
type SkillsManifest,
|
|
16
|
+
ownedSkillNames,
|
|
17
|
+
} from './skills-install-manifest.js';
|
|
18
|
+
import { copyTreeSecure, validateSkillFrontmatter } from './skills-install-secure.js';
|
|
19
|
+
|
|
20
|
+
export type SkillAction = 'install' | 'skipped-existing' | 'skipped-unowned';
|
|
21
|
+
|
|
22
|
+
export interface SkillPlanEntry {
|
|
23
|
+
name: string;
|
|
24
|
+
action: SkillAction;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Commit-phase failure. `clean` records whether the rollback fully restored the project. */
|
|
28
|
+
export class CommitError extends Error {
|
|
29
|
+
constructor(
|
|
30
|
+
message: string,
|
|
31
|
+
readonly clean: boolean,
|
|
32
|
+
) {
|
|
33
|
+
super(message);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Decide the action per skill before anything is written. `--force` may
|
|
39
|
+
* replace only directories the kit owns (manifest `installed` records for
|
|
40
|
+
* this target directory); anything else that already exists is reported and
|
|
41
|
+
* left untouched.
|
|
42
|
+
*/
|
|
43
|
+
export async function planSkillInstalls(
|
|
44
|
+
names: readonly string[],
|
|
45
|
+
targetDir: string,
|
|
46
|
+
force: boolean,
|
|
47
|
+
manifest: SkillsManifest,
|
|
48
|
+
into: string,
|
|
49
|
+
): Promise<SkillPlanEntry[]> {
|
|
50
|
+
const owned = ownedSkillNames(manifest, into);
|
|
51
|
+
const plan: SkillPlanEntry[] = [];
|
|
52
|
+
for (const name of names) {
|
|
53
|
+
const dest = path.join(targetDir, name);
|
|
54
|
+
const exists = await fs.access(dest).then(
|
|
55
|
+
() => true,
|
|
56
|
+
() => false,
|
|
57
|
+
);
|
|
58
|
+
if (!exists) plan.push({ name, action: 'install' });
|
|
59
|
+
else if (!force) plan.push({ name, action: 'skipped-existing' });
|
|
60
|
+
else if (owned.has(name)) plan.push({ name, action: 'install' });
|
|
61
|
+
else plan.push({ name, action: 'skipped-unowned' });
|
|
62
|
+
}
|
|
63
|
+
return plan;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Copy every planned skill into the staging tree and validate it there.
|
|
68
|
+
* A validation failure leaves the project untouched: staging is discarded
|
|
69
|
+
* by the caller.
|
|
70
|
+
*/
|
|
71
|
+
export async function stageSkills(
|
|
72
|
+
skillsDir: string,
|
|
73
|
+
plan: SkillPlanEntry[],
|
|
74
|
+
stagingRoot: string,
|
|
75
|
+
): Promise<Map<string, string>> {
|
|
76
|
+
const staged = new Map<string, string>();
|
|
77
|
+
for (const entry of plan) {
|
|
78
|
+
if (entry.action !== 'install') continue;
|
|
79
|
+
const stage = path.join(stagingRoot, 'skills', entry.name);
|
|
80
|
+
await copyTreeSecure(path.join(skillsDir, entry.name), stage);
|
|
81
|
+
await validateSkillFrontmatter(path.join(stage, 'SKILL.md'), entry.name);
|
|
82
|
+
staged.set(entry.name, stage);
|
|
83
|
+
}
|
|
84
|
+
return staged;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Commit staged directories into the target. Replaced directories are moved
|
|
89
|
+
* aside first; on failure everything the kit changed in this commit is rolled
|
|
90
|
+
* back (best effort, honestly reported), so a partial install never stands.
|
|
91
|
+
* If the rollback itself fails, staging is kept for manual recovery.
|
|
92
|
+
*/
|
|
93
|
+
export async function commitStagedSkills(
|
|
94
|
+
staged: ReadonlyMap<string, string>,
|
|
95
|
+
targetDir: string,
|
|
96
|
+
stagingRoot: string,
|
|
97
|
+
): Promise<void> {
|
|
98
|
+
const applied: string[] = [];
|
|
99
|
+
const movedAside: Array<{ dest: string; backup: string }> = [];
|
|
100
|
+
try {
|
|
101
|
+
await fs.mkdir(targetDir, { recursive: true });
|
|
102
|
+
for (const [name, stage] of staged) {
|
|
103
|
+
const dest = path.join(targetDir, name);
|
|
104
|
+
const exists = await fs.access(dest).then(
|
|
105
|
+
() => true,
|
|
106
|
+
() => false,
|
|
107
|
+
);
|
|
108
|
+
if (exists) {
|
|
109
|
+
const backup = path.join(stagingRoot, 'backups', name);
|
|
110
|
+
await fs.mkdir(path.dirname(backup), { recursive: true });
|
|
111
|
+
await fs.rename(dest, backup);
|
|
112
|
+
movedAside.push({ dest, backup });
|
|
113
|
+
}
|
|
114
|
+
await fs.rename(stage, dest);
|
|
115
|
+
applied.push(dest);
|
|
116
|
+
}
|
|
117
|
+
} catch (err) {
|
|
118
|
+
let clean = true;
|
|
119
|
+
const markDirty = (): void => {
|
|
120
|
+
clean = false;
|
|
121
|
+
};
|
|
122
|
+
for (const dest of applied) {
|
|
123
|
+
await fs.rm(dest, { recursive: true, force: true }).catch(markDirty);
|
|
124
|
+
}
|
|
125
|
+
for (const { dest, backup } of movedAside) {
|
|
126
|
+
const present = await fs.access(dest).then(
|
|
127
|
+
() => true,
|
|
128
|
+
() => false,
|
|
129
|
+
);
|
|
130
|
+
if (!present) await fs.rename(backup, dest).catch(markDirty);
|
|
131
|
+
}
|
|
132
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
133
|
+
throw new CommitError(
|
|
134
|
+
clean
|
|
135
|
+
? `staged commit failed and was rolled back: ${detail}`
|
|
136
|
+
: `staged commit failed; ROLLBACK INCOMPLETE — the staging directory ${stagingRoot} ` +
|
|
137
|
+
`was kept for manual recovery: ${detail}`,
|
|
138
|
+
clean,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Derive the manifest statuses from the plan. */
|
|
144
|
+
export function statusesFromPlan(plan: SkillPlanEntry[]): SkillInstallStatus[] {
|
|
145
|
+
return plan.map((p) => ({
|
|
146
|
+
name: p.name,
|
|
147
|
+
status: p.action === 'install' ? 'installed' : p.action,
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// packages/cli/src/commands/skills-install-manifest.ts
|
|
2
|
+
//
|
|
3
|
+
// The skills manifest (`.ai-kit/skills-manifest.json`) is the ownership
|
|
4
|
+
// record for `kit skills install`: it is the only evidence of which skill
|
|
5
|
+
// directories the kit placed into a project, so `--force` may replace
|
|
6
|
+
// exactly those directories and nothing else.
|
|
7
|
+
//
|
|
8
|
+
// Reading is fail-closed: a manifest that exists but cannot be parsed is a
|
|
9
|
+
// hard error, never a silent reset — overwriting it with a fresh one would
|
|
10
|
+
// erase the ownership records of every previously installed pack.
|
|
11
|
+
|
|
12
|
+
import { promises as fs } from 'node:fs';
|
|
13
|
+
import { SecureInstallError } from './skills-install-secure.js';
|
|
14
|
+
|
|
15
|
+
export interface SkillInstallStatus {
|
|
16
|
+
name: string;
|
|
17
|
+
status: 'installed' | 'skipped-existing' | 'skipped-unowned';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PackRecord {
|
|
21
|
+
repository: string;
|
|
22
|
+
url: string;
|
|
23
|
+
ref: string;
|
|
24
|
+
commit: string;
|
|
25
|
+
into: string;
|
|
26
|
+
installedAt: string;
|
|
27
|
+
skills: SkillInstallStatus[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SkillsManifest {
|
|
31
|
+
schemaVersion: 1;
|
|
32
|
+
packs: Record<string, PackRecord>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const RECOVERY_HINT =
|
|
36
|
+
'kit refuses to overwrite a manifest it cannot read — earlier pack records would be lost.\n' +
|
|
37
|
+
'Restore the file from version control, repair it by hand, or delete it to forget\n' +
|
|
38
|
+
'all recorded installs and start fresh.';
|
|
39
|
+
|
|
40
|
+
const isSkillStatus = (v: unknown): v is SkillInstallStatus =>
|
|
41
|
+
typeof v === 'object' &&
|
|
42
|
+
v !== null &&
|
|
43
|
+
typeof (v as Record<string, unknown>).name === 'string' &&
|
|
44
|
+
typeof (v as Record<string, unknown>).status === 'string';
|
|
45
|
+
|
|
46
|
+
const isPackRecord = (v: unknown): v is PackRecord =>
|
|
47
|
+
typeof v === 'object' &&
|
|
48
|
+
v !== null &&
|
|
49
|
+
typeof (v as Record<string, unknown>).repository === 'string' &&
|
|
50
|
+
typeof (v as Record<string, unknown>).url === 'string' &&
|
|
51
|
+
typeof (v as Record<string, unknown>).ref === 'string' &&
|
|
52
|
+
typeof (v as Record<string, unknown>).commit === 'string' &&
|
|
53
|
+
typeof (v as Record<string, unknown>).into === 'string' &&
|
|
54
|
+
typeof (v as Record<string, unknown>).installedAt === 'string' &&
|
|
55
|
+
Array.isArray((v as Record<string, unknown>).skills) &&
|
|
56
|
+
((v as Record<string, unknown>).skills as unknown[]).every(isSkillStatus);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Read the skills manifest. A missing file is a fresh start; a file that
|
|
60
|
+
* exists but is malformed fails closed instead of being silently reset.
|
|
61
|
+
*/
|
|
62
|
+
export async function readSkillsManifest(manifestPath: string): Promise<SkillsManifest> {
|
|
63
|
+
let raw: string;
|
|
64
|
+
try {
|
|
65
|
+
raw = await fs.readFile(manifestPath, 'utf8');
|
|
66
|
+
} catch {
|
|
67
|
+
return { schemaVersion: 1, packs: {} };
|
|
68
|
+
}
|
|
69
|
+
const where = ` (at ${manifestPath})`;
|
|
70
|
+
let parsed: unknown;
|
|
71
|
+
try {
|
|
72
|
+
parsed = JSON.parse(raw);
|
|
73
|
+
} catch {
|
|
74
|
+
throw new SecureInstallError(`skills manifest is not valid JSON${where}\n${RECOVERY_HINT}`);
|
|
75
|
+
}
|
|
76
|
+
const shape = parsed as { schemaVersion?: unknown; packs?: unknown };
|
|
77
|
+
const packsOk =
|
|
78
|
+
typeof shape.packs === 'object' &&
|
|
79
|
+
shape.packs !== null &&
|
|
80
|
+
!Array.isArray(shape.packs) &&
|
|
81
|
+
Object.values(shape.packs as Record<string, unknown>).every(isPackRecord);
|
|
82
|
+
if (shape.schemaVersion !== 1 || !packsOk) {
|
|
83
|
+
throw new SecureInstallError(
|
|
84
|
+
`skills manifest has an unexpected shape${where} ` +
|
|
85
|
+
`(expected schemaVersion 1 with a packs object of pack records)\n${RECOVERY_HINT}`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return parsed as SkillsManifest;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Skill directories the kit previously installed into this exact target
|
|
93
|
+
* directory. Only `installed` statuses count: `skipped-existing` means the
|
|
94
|
+
* directory was already there (possibly user-authored) and was never touched.
|
|
95
|
+
*/
|
|
96
|
+
export function ownedSkillNames(manifest: SkillsManifest, into: string): Set<string> {
|
|
97
|
+
const owned = new Set<string>();
|
|
98
|
+
for (const record of Object.values(manifest.packs)) {
|
|
99
|
+
if (record.into !== into) continue;
|
|
100
|
+
for (const s of record.skills) {
|
|
101
|
+
if (s.status === 'installed') owned.add(s.name);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return owned;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Merge the run's statuses with the previous record so ownership survives
|
|
109
|
+
* idempotent re-runs: a skill the kit installed once stays `installed` when
|
|
110
|
+
* a later no-force run merely reports it as `skipped-existing`.
|
|
111
|
+
*/
|
|
112
|
+
export function mergeOwnership(
|
|
113
|
+
previous: PackRecord | undefined,
|
|
114
|
+
statuses: SkillInstallStatus[],
|
|
115
|
+
): SkillInstallStatus[] {
|
|
116
|
+
const wasInstalled = new Set(
|
|
117
|
+
(previous?.skills ?? []).filter((s) => s.status === 'installed').map((s) => s.name),
|
|
118
|
+
);
|
|
119
|
+
return statuses.map((s) =>
|
|
120
|
+
s.status === 'skipped-existing' && wasInstalled.has(s.name)
|
|
121
|
+
? { name: s.name, status: 'installed' }
|
|
122
|
+
: s,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// packages/cli/src/commands/skills-install-report.ts
|
|
2
|
+
//
|
|
3
|
+
// Human- and machine-facing result formatting for `kit skills install`.
|
|
4
|
+
// Text output keeps the original layout (Target / Installed / Skipped) and
|
|
5
|
+
// adds Refused for directories --force declined to touch because the kit
|
|
6
|
+
// did not install them.
|
|
7
|
+
|
|
8
|
+
import type { SkillInstallStatus } from './skills-install-manifest.js';
|
|
9
|
+
|
|
10
|
+
export function formatResult(
|
|
11
|
+
packName: string,
|
|
12
|
+
into: string,
|
|
13
|
+
statuses: SkillInstallStatus[],
|
|
14
|
+
manifestPath: string,
|
|
15
|
+
): string {
|
|
16
|
+
const installed = statuses.filter((s) => s.status === 'installed');
|
|
17
|
+
const skipped = statuses.filter((s) => s.status === 'skipped-existing');
|
|
18
|
+
const refused = statuses.filter((s) => s.status === 'skipped-unowned');
|
|
19
|
+
const lines: string[] = [`kit skills install: SUCCESS (${packName})`];
|
|
20
|
+
lines.push(` Target: ${into}`);
|
|
21
|
+
lines.push(` Installed: ${installed.length} skill(s)`);
|
|
22
|
+
for (const s of installed) lines.push(` + ${s.name}`);
|
|
23
|
+
if (skipped.length > 0) {
|
|
24
|
+
lines.push(` Skipped: ${skipped.length} existing skill(s) (use --force to overwrite)`);
|
|
25
|
+
for (const s of skipped) lines.push(` ~ ${s.name}`);
|
|
26
|
+
}
|
|
27
|
+
if (refused.length > 0) {
|
|
28
|
+
lines.push(
|
|
29
|
+
` Refused: ${refused.length} unowned skill(s) (--force replaces only kit-installed skills)`,
|
|
30
|
+
);
|
|
31
|
+
for (const s of refused) lines.push(` ! ${s.name}`);
|
|
32
|
+
}
|
|
33
|
+
lines.push(` Manifest: ${manifestPath}`);
|
|
34
|
+
lines.push('');
|
|
35
|
+
return lines.join('\n');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface DryRunPlan {
|
|
39
|
+
pack: string;
|
|
40
|
+
repository: string;
|
|
41
|
+
ref: string;
|
|
42
|
+
target: string;
|
|
43
|
+
expectedSkills: number;
|
|
44
|
+
dryRun: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function formatDryRun(plan: DryRunPlan): string {
|
|
48
|
+
const lines: string[] = [
|
|
49
|
+
'kit skills install --dry-run',
|
|
50
|
+
` Pack: ${plan.pack}`,
|
|
51
|
+
` Repository: ${plan.repository}`,
|
|
52
|
+
` Ref: ${plan.ref}`,
|
|
53
|
+
` Target: ${plan.target}`,
|
|
54
|
+
` Expected: ${plan.expectedSkills} skill(s) from pack metadata`,
|
|
55
|
+
'',
|
|
56
|
+
'No files written and no repository cloned. Run without --dry-run to execute.',
|
|
57
|
+
'',
|
|
58
|
+
];
|
|
59
|
+
return lines.join('\n');
|
|
60
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// packages/cli/src/commands/skills-install-secure.ts
|
|
2
|
+
//
|
|
3
|
+
// Fail-closed filesystem primitives for `kit skills install`
|
|
4
|
+
// (docs/security/SECURITY.md threat model: an external skills pack is
|
|
5
|
+
// untrusted content; a skills destination is untrusted until proven clean).
|
|
6
|
+
//
|
|
7
|
+
// Every rule here is a hard error, never a warning and never a silent skip:
|
|
8
|
+
// 1. `--into` must resolve inside the target project (path traversal).
|
|
9
|
+
// 2. No component of the destination path may be a symlink, so a planted
|
|
10
|
+
// link can never redirect kit writes outside the project.
|
|
11
|
+
// 3. Pack content is copied without ever following symlinks; a symlink
|
|
12
|
+
// anywhere in a skill directory is rejected, not skipped.
|
|
13
|
+
// 4. Every SKILL.md must carry YAML frontmatter whose `name` matches its
|
|
14
|
+
// directory and that declares a `description` (Agent Skills format).
|
|
15
|
+
|
|
16
|
+
import { promises as fs } from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
|
|
19
|
+
/** Hard installer error. The message is user-facing; there is never a default. */
|
|
20
|
+
export class SecureInstallError extends Error {}
|
|
21
|
+
|
|
22
|
+
const SKILL_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
23
|
+
|
|
24
|
+
/** Resolve `--into` against the project root and enforce containment. */
|
|
25
|
+
export function resolveInstallTarget(targetRoot: string, into: string): string {
|
|
26
|
+
const root = path.resolve(targetRoot);
|
|
27
|
+
const target = path.resolve(root, into);
|
|
28
|
+
if (target === root) {
|
|
29
|
+
throw new SecureInstallError(
|
|
30
|
+
`--into must name a subdirectory of the project, not the project itself: '${into}'`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
if (!target.startsWith(root + path.sep)) {
|
|
34
|
+
throw new SecureInstallError(
|
|
35
|
+
`--into escapes the project directory: '${into}' ` +
|
|
36
|
+
`(resolved to ${target}; the project is ${root})`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return target;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Reject a destination whose path traverses a symlinked component. */
|
|
43
|
+
export async function assertPathSymlinkFree(root: string, target: string): Promise<void> {
|
|
44
|
+
const rel = path.relative(path.resolve(root), path.resolve(target));
|
|
45
|
+
let current = path.resolve(root);
|
|
46
|
+
for (const part of rel.split(path.sep)) {
|
|
47
|
+
if (!part || part === '.') continue;
|
|
48
|
+
current = path.join(current, part);
|
|
49
|
+
const stat = await fs.lstat(current).catch(() => null);
|
|
50
|
+
if (stat?.isSymbolicLink()) {
|
|
51
|
+
throw new SecureInstallError(
|
|
52
|
+
`refusing to install through a symlinked path component: ${current}`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Skill directory names become filesystem path components: validate them. */
|
|
59
|
+
export function assertSafeSkillName(name: string): void {
|
|
60
|
+
if (!SKILL_NAME_PATTERN.test(name)) {
|
|
61
|
+
throw new SecureInstallError(`unsafe skill directory name in pack: '${name}'`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Copy a directory tree, rejecting symlinks and special file types. */
|
|
66
|
+
export async function copyTreeSecure(src: string, dest: string): Promise<void> {
|
|
67
|
+
const entries = await fs.readdir(src, { withFileTypes: true });
|
|
68
|
+
await fs.mkdir(dest, { recursive: true });
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
const from = path.join(src, entry.name);
|
|
71
|
+
if (entry.isSymbolicLink()) {
|
|
72
|
+
throw new SecureInstallError(`symlink in pack content (nothing installed): ${from}`);
|
|
73
|
+
}
|
|
74
|
+
if (!entry.isFile() && !entry.isDirectory()) {
|
|
75
|
+
throw new SecureInstallError(`unsupported file type in pack (nothing installed): ${from}`);
|
|
76
|
+
}
|
|
77
|
+
if (entry.isDirectory()) {
|
|
78
|
+
await copyTreeSecure(from, path.join(dest, entry.name));
|
|
79
|
+
} else {
|
|
80
|
+
await fs.copyFile(from, path.join(dest, entry.name));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Validate a staged SKILL.md against the Agent Skills frontmatter contract. */
|
|
86
|
+
export async function validateSkillFrontmatter(
|
|
87
|
+
skillMd: string,
|
|
88
|
+
expectedName: string,
|
|
89
|
+
): Promise<void> {
|
|
90
|
+
const raw = await fs.readFile(skillMd, 'utf8');
|
|
91
|
+
const lines = raw.replace(/^\uFEFF/, '').split(/\r?\n/);
|
|
92
|
+
if (lines[0]?.trim() !== '---') {
|
|
93
|
+
throw new SecureInstallError(`SKILL.md has no YAML frontmatter: ${skillMd}`);
|
|
94
|
+
}
|
|
95
|
+
const close = lines.findIndex((line, i) => i > 0 && line.trim() === '---');
|
|
96
|
+
if (close === -1) {
|
|
97
|
+
throw new SecureInstallError(`SKILL.md frontmatter is never closed: ${skillMd}`);
|
|
98
|
+
}
|
|
99
|
+
const frontmatter = lines.slice(1, close);
|
|
100
|
+
const nameLine = frontmatter.find((l) => l.startsWith('name:'));
|
|
101
|
+
const name = nameLine?.slice('name:'.length).trim();
|
|
102
|
+
const hasDescription = frontmatter.some((l) => l.startsWith('description:'));
|
|
103
|
+
if (!name || !hasDescription) {
|
|
104
|
+
throw new SecureInstallError(
|
|
105
|
+
`SKILL.md frontmatter must declare name and description: ${skillMd}`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (name !== expectedName) {
|
|
109
|
+
throw new SecureInstallError(
|
|
110
|
+
`SKILL.md name '${name}' does not match its directory '${expectedName}': ${skillMd}`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// packages/cli/src/commands/skills-install.ts
|
|
2
|
+
//
|
|
3
|
+
// Executor for `kit skills install <pack>`. Fetches an external skills pack
|
|
4
|
+
// from its GitHub repository (shallow clone, default branch), copies the
|
|
5
|
+
// pack's `skills/*` directories into the target project, and records the
|
|
6
|
+
// installation in `.ai-kit/skills-manifest.json`.
|
|
7
|
+
//
|
|
8
|
+
// Failure policy (kit convention): never fabricate success. A failed clone or
|
|
9
|
+
// a missing skills/ directory returns exit code 1 with a fix instruction, and
|
|
10
|
+
// nothing is written to the project. Already-present skill directories are
|
|
11
|
+
// skipped unless `--force`; `--force` replaces only directories the kit
|
|
12
|
+
// previously installed (the manifest is the ownership record), so re-running
|
|
13
|
+
// is idempotent and user-authored skills are never deleted.
|
|
14
|
+
//
|
|
15
|
+
// Install path security is split across focused modules:
|
|
16
|
+
// ./skills-install-secure.ts containment, symlink refusal, frontmatter
|
|
17
|
+
// ./skills-install-manifest.ts fail-closed manifest reads and ownership
|
|
18
|
+
// ./skills-install-commit.ts staged plan → validate → atomic commit
|
|
19
|
+
|
|
20
|
+
import { execFile } from 'node:child_process';
|
|
21
|
+
import { promises as fs } from 'node:fs';
|
|
22
|
+
import os from 'node:os';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
import { promisify } from 'node:util';
|
|
25
|
+
import { resolveSkillsPack } from '@agentic-sdlc-kit/core';
|
|
26
|
+
import type { CommandContext, CommandResult } from './types.js';
|
|
27
|
+
import {
|
|
28
|
+
SecureInstallError,
|
|
29
|
+
assertPathSymlinkFree,
|
|
30
|
+
assertSafeSkillName,
|
|
31
|
+
resolveInstallTarget,
|
|
32
|
+
} from './skills-install-secure.js';
|
|
33
|
+
import {
|
|
34
|
+
type PackRecord,
|
|
35
|
+
type SkillsManifest,
|
|
36
|
+
mergeOwnership,
|
|
37
|
+
readSkillsManifest,
|
|
38
|
+
} from './skills-install-manifest.js';
|
|
39
|
+
import {
|
|
40
|
+
CommitError,
|
|
41
|
+
commitStagedSkills,
|
|
42
|
+
planSkillInstalls,
|
|
43
|
+
stageSkills,
|
|
44
|
+
statusesFromPlan,
|
|
45
|
+
} from './skills-install-commit.js';
|
|
46
|
+
import { formatDryRun, formatResult } from './skills-install-report.js';
|
|
47
|
+
|
|
48
|
+
const execFileAsync = promisify(execFile);
|
|
49
|
+
|
|
50
|
+
const DEFAULT_INTO = '.claude/skills';
|
|
51
|
+
|
|
52
|
+
export interface SkillsInstallOptions {
|
|
53
|
+
packId: string;
|
|
54
|
+
targetRoot: string;
|
|
55
|
+
into?: string;
|
|
56
|
+
ref?: string;
|
|
57
|
+
force: boolean;
|
|
58
|
+
dryRun: boolean;
|
|
59
|
+
json: boolean;
|
|
60
|
+
/** Install from a local checkout of the pack instead of cloning GitHub. */
|
|
61
|
+
source?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function clonePackSource(
|
|
65
|
+
packUrl: string,
|
|
66
|
+
ref: string | undefined,
|
|
67
|
+
dest: string,
|
|
68
|
+
): Promise<string> {
|
|
69
|
+
const args = ['clone', '--depth', '1', '--quiet'];
|
|
70
|
+
if (ref) args.push('--branch', ref);
|
|
71
|
+
args.push(packUrl, dest);
|
|
72
|
+
await execFileAsync('git', args, { maxBuffer: 8 * 1024 * 1024 });
|
|
73
|
+
const { stdout } = await execFileAsync('git', ['-C', dest, 'rev-parse', 'HEAD']);
|
|
74
|
+
return stdout.trim();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Locate the pack source (local --source or a shallow clone) and its commit. */
|
|
78
|
+
async function acquireSource(
|
|
79
|
+
packUrl: string,
|
|
80
|
+
skillsPath: string,
|
|
81
|
+
opts: SkillsInstallOptions,
|
|
82
|
+
): Promise<{ sourceRoot: string; commit: string; isTemp: boolean }> {
|
|
83
|
+
if (opts.source) {
|
|
84
|
+
const present = await fs.access(path.join(opts.source, skillsPath)).then(
|
|
85
|
+
() => true,
|
|
86
|
+
() => false,
|
|
87
|
+
);
|
|
88
|
+
if (!present) {
|
|
89
|
+
throw new SecureInstallError(
|
|
90
|
+
`--source directory has no '${skillsPath}/' directory: ${opts.source}`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return { sourceRoot: opts.source, commit: 'local-source', isTemp: false };
|
|
94
|
+
}
|
|
95
|
+
const sourceRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-skills-'));
|
|
96
|
+
const commit = await clonePackSource(packUrl, opts.ref, sourceRoot);
|
|
97
|
+
return { sourceRoot, commit, isTemp: true };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function listSkillDirs(skillsDir: string): Promise<string[]> {
|
|
101
|
+
const entries = await fs.readdir(skillsDir, { withFileTypes: true });
|
|
102
|
+
const names: string[] = [];
|
|
103
|
+
for (const e of entries) {
|
|
104
|
+
if (e.isSymbolicLink()) {
|
|
105
|
+
throw new SecureInstallError(
|
|
106
|
+
`symlinked entry in pack skills/ (nothing installed): ${path.join(skillsDir, e.name)}`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (!e.isDirectory()) continue;
|
|
110
|
+
const hasSkillMd = await fs.access(path.join(skillsDir, e.name, 'SKILL.md')).then(
|
|
111
|
+
() => true,
|
|
112
|
+
() => false,
|
|
113
|
+
);
|
|
114
|
+
if (!hasSkillMd) continue;
|
|
115
|
+
assertSafeSkillName(e.name);
|
|
116
|
+
names.push(e.name);
|
|
117
|
+
}
|
|
118
|
+
return names.sort();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Resolve and validate the install target, or fail closed with exit 1. */
|
|
122
|
+
async function resolveTarget(
|
|
123
|
+
opts: SkillsInstallOptions,
|
|
124
|
+
into: string,
|
|
125
|
+
ctx: CommandContext,
|
|
126
|
+
): Promise<string | null> {
|
|
127
|
+
try {
|
|
128
|
+
const targetDir = resolveInstallTarget(opts.targetRoot, into);
|
|
129
|
+
await assertPathSymlinkFree(opts.targetRoot, targetDir);
|
|
130
|
+
return targetDir;
|
|
131
|
+
} catch (err) {
|
|
132
|
+
if (!(err instanceof SecureInstallError)) throw err;
|
|
133
|
+
ctx.stderr(`kit skills: ${err.message}\n`);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Run the install subcommand. Never throws; failures return exitCode 1. */
|
|
139
|
+
export async function runSkillsInstall(
|
|
140
|
+
opts: SkillsInstallOptions,
|
|
141
|
+
ctx: CommandContext,
|
|
142
|
+
): Promise<CommandResult> {
|
|
143
|
+
const pack = resolveSkillsPack(opts.packId);
|
|
144
|
+
if (!pack) {
|
|
145
|
+
ctx.stderr(`kit skills: unknown skills pack '${opts.packId}'\n`);
|
|
146
|
+
return { exitCode: 1, message: `unknown skills pack: ${opts.packId}` };
|
|
147
|
+
}
|
|
148
|
+
const into = opts.into ?? DEFAULT_INTO;
|
|
149
|
+
const targetDir = await resolveTarget(opts, into, ctx);
|
|
150
|
+
if (targetDir === null) return { exitCode: 1, message: `unsafe install target: ${into}` };
|
|
151
|
+
const manifestPath = path.join(opts.targetRoot, '.ai-kit', 'skills-manifest.json');
|
|
152
|
+
|
|
153
|
+
if (opts.dryRun) {
|
|
154
|
+
const plan = {
|
|
155
|
+
pack: pack.id,
|
|
156
|
+
repository: pack.repository,
|
|
157
|
+
ref: opts.ref ?? 'default branch',
|
|
158
|
+
target: into,
|
|
159
|
+
expectedSkills: pack.skillCount,
|
|
160
|
+
dryRun: true,
|
|
161
|
+
};
|
|
162
|
+
const text = opts.json ? JSON.stringify(plan, null, 2) + '\n' : formatDryRun(plan);
|
|
163
|
+
ctx.stdout(text);
|
|
164
|
+
return { exitCode: 0 };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let stagingRoot = '';
|
|
168
|
+
let keepStaging = false;
|
|
169
|
+
try {
|
|
170
|
+
let manifest: SkillsManifest;
|
|
171
|
+
try {
|
|
172
|
+
manifest = await readSkillsManifest(manifestPath);
|
|
173
|
+
} catch (err) {
|
|
174
|
+
if (!(err instanceof SecureInstallError)) throw err;
|
|
175
|
+
ctx.stderr(`kit skills: ${err.message}\n`);
|
|
176
|
+
return { exitCode: 1, message: 'skills manifest is unreadable' };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const { sourceRoot, commit, isTemp } = await acquireSource(pack.url, pack.skillsPath, opts);
|
|
180
|
+
try {
|
|
181
|
+
const skillsDir = path.join(sourceRoot, pack.skillsPath);
|
|
182
|
+
const present = await fs.access(skillsDir).then(
|
|
183
|
+
() => true,
|
|
184
|
+
() => false,
|
|
185
|
+
);
|
|
186
|
+
if (!present) {
|
|
187
|
+
ctx.stderr(
|
|
188
|
+
`kit skills: ${pack.id} has no '${pack.skillsPath}/' directory at ${sourceRoot}\n`,
|
|
189
|
+
);
|
|
190
|
+
return { exitCode: 1, message: 'pack skills directory missing' };
|
|
191
|
+
}
|
|
192
|
+
const names = await listSkillDirs(skillsDir);
|
|
193
|
+
const plan = await planSkillInstalls(names, targetDir, opts.force, manifest, into);
|
|
194
|
+
|
|
195
|
+
const tmpRoot = path.join(opts.targetRoot, '.ai-kit', 'tmp');
|
|
196
|
+
await fs.mkdir(tmpRoot, { recursive: true });
|
|
197
|
+
stagingRoot = await fs.mkdtemp(path.join(tmpRoot, 'skills-'));
|
|
198
|
+
const staged = await stageSkills(skillsDir, plan, stagingRoot);
|
|
199
|
+
await commitStagedSkills(staged, targetDir, stagingRoot);
|
|
200
|
+
|
|
201
|
+
const statuses = statusesFromPlan(plan);
|
|
202
|
+
const previous: PackRecord | undefined = manifest.packs[pack.id];
|
|
203
|
+
manifest.packs[pack.id] = {
|
|
204
|
+
repository: pack.repository,
|
|
205
|
+
url: pack.url,
|
|
206
|
+
ref: opts.ref ?? 'default-branch',
|
|
207
|
+
commit,
|
|
208
|
+
into,
|
|
209
|
+
installedAt: new Date().toISOString(),
|
|
210
|
+
skills: mergeOwnership(previous, statuses),
|
|
211
|
+
};
|
|
212
|
+
await fs.mkdir(path.dirname(manifestPath), { recursive: true });
|
|
213
|
+
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2) + '\n', 'utf8');
|
|
214
|
+
const text = opts.json
|
|
215
|
+
? JSON.stringify(
|
|
216
|
+
{
|
|
217
|
+
installed: true,
|
|
218
|
+
pack: pack.id,
|
|
219
|
+
commit,
|
|
220
|
+
into,
|
|
221
|
+
skills: statuses,
|
|
222
|
+
manifest: '.ai-kit/skills-manifest.json',
|
|
223
|
+
},
|
|
224
|
+
null,
|
|
225
|
+
2,
|
|
226
|
+
) + '\n'
|
|
227
|
+
: formatResult(pack.id, into, statuses, '.ai-kit/skills-manifest.json');
|
|
228
|
+
ctx.stdout(text);
|
|
229
|
+
return { exitCode: 0 };
|
|
230
|
+
} finally {
|
|
231
|
+
if (isTemp && sourceRoot.startsWith(os.tmpdir())) {
|
|
232
|
+
await fs.rm(sourceRoot, { recursive: true, force: true }).catch(() => undefined);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
} catch (err) {
|
|
236
|
+
if (err instanceof CommitError && !err.clean) keepStaging = true;
|
|
237
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
238
|
+
ctx.stderr(`kit skills: failed to install '${opts.packId}': ${detail}\n`);
|
|
239
|
+
return { exitCode: 1, message: `install failed: ${opts.packId}` };
|
|
240
|
+
} finally {
|
|
241
|
+
if (stagingRoot !== '' && !keepStaging) {
|
|
242
|
+
await fs.rm(stagingRoot, { recursive: true, force: true }).catch(() => undefined);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|