kitskill 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +397 -2
- package/THIRD-PARTY-NOTICES.md +24 -0
- package/banner.jpeg +0 -0
- package/bin/cli.mjs +464 -0
- package/lib/fs-safe.mjs +136 -0
- package/lib/hook.mjs +79 -0
- package/lib/installer.mjs +449 -0
- package/lib/manifest.mjs +47 -0
- package/lib/permissions.mjs +175 -0
- package/lib/profile.mjs +124 -0
- package/lib/project-scan.mjs +186 -0
- package/lib/validator.mjs +72 -0
- package/logo.png +0 -0
- package/logohead.png +0 -0
- package/package.json +54 -4
- package/templates/core/memory/PROJECT.md +33 -0
- package/templates/core/memory/decisions/ADR.md +22 -0
- package/templates/core/profile.json +17 -0
- package/templates/core/router.md +89 -0
- package/templates/core/state/STATE.md +32 -0
- package/templates/opencode/policy-plugin.js +66 -0
- package/templates/scripts/graph-client.mjs +231 -0
- package/templates/scripts/review-packet.mjs +250 -0
- package/templates/scripts/shrink-output.mjs +159 -0
- package/templates/scripts/ui-audit.mjs +639 -0
- package/templates/scripts/verify.mjs +293 -0
- package/templates/skills/ks-api/SKILL.md +87 -0
- package/templates/skills/ks-api/references/api-contract.md +131 -0
- package/templates/skills/ks-build/SKILL.md +92 -0
- package/templates/skills/ks-data/SKILL.md +81 -0
- package/templates/skills/ks-data/references/data-migrations.md +120 -0
- package/templates/skills/ks-deliver/SKILL.md +99 -0
- package/templates/skills/ks-deliver/references/doc-patterns.md +180 -0
- package/templates/skills/ks-design/SKILL.md +147 -0
- package/templates/skills/ks-design/references/app-models.md +216 -0
- package/templates/skills/ks-design/references/design-contract.md +98 -0
- package/templates/skills/ks-design/references/visual-directions.md +159 -0
- package/templates/skills/ks-plan/SKILL.md +84 -0
- package/templates/skills/ks-review/SKILL.md +71 -0
- package/templates/skills/ks-secure/SKILL.md +55 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
import { cp, mkdir, readdir, readFile, rm, symlink, unlink } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
createManifest,
|
|
6
|
+
hashFile,
|
|
7
|
+
hashText,
|
|
8
|
+
readManifest,
|
|
9
|
+
writeManifest,
|
|
10
|
+
} from './manifest.mjs';
|
|
11
|
+
import {
|
|
12
|
+
assertRealInside,
|
|
13
|
+
pathExists,
|
|
14
|
+
pruneEmptyParents,
|
|
15
|
+
readLinkTarget,
|
|
16
|
+
readText,
|
|
17
|
+
removeEntry,
|
|
18
|
+
resolveInside,
|
|
19
|
+
writeFileSafe,
|
|
20
|
+
} from './fs-safe.mjs';
|
|
21
|
+
import { validateRouter, validateSkill } from './validator.mjs';
|
|
22
|
+
|
|
23
|
+
export const SUPPORTED_TARGETS = ['opencode', 'claude', 'codex', 'cursor'];
|
|
24
|
+
export const BLOCK_START = '<!-- kitskill:start -->';
|
|
25
|
+
export const BLOCK_END = '<!-- kitskill:end -->';
|
|
26
|
+
|
|
27
|
+
const CORE_FILES = [
|
|
28
|
+
{ source: 'core/router.md', target: '.kitskill/router.md' },
|
|
29
|
+
{ source: 'core/state/STATE.md', target: '.kitskill/state/STATE.md', preserve: true },
|
|
30
|
+
{ source: 'core/memory/PROJECT.md', target: '.kitskill/memory/PROJECT.md', preserve: true },
|
|
31
|
+
{ source: 'core/profile.json', target: '.kitskill/profile.json', preserve: true },
|
|
32
|
+
{
|
|
33
|
+
source: 'core/memory/decisions/ADR.md',
|
|
34
|
+
target: '.kitskill/memory/decisions/ADR.template.md',
|
|
35
|
+
preserve: true,
|
|
36
|
+
},
|
|
37
|
+
{ source: 'scripts/shrink-output.mjs', target: '.kitskill/scripts/shrink-output.mjs' },
|
|
38
|
+
{ source: 'scripts/review-packet.mjs', target: '.kitskill/scripts/review-packet.mjs' },
|
|
39
|
+
{ source: 'scripts/ui-audit.mjs', target: '.kitskill/scripts/ui-audit.mjs' },
|
|
40
|
+
{ source: 'scripts/graph-client.mjs', target: '.kitskill/scripts/graph-client.mjs' },
|
|
41
|
+
{ source: 'scripts/verify.mjs', target: '.kitskill/scripts/verify.mjs' },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const TARGET_BLOCK_FILE = {
|
|
45
|
+
opencode: 'AGENTS.md',
|
|
46
|
+
claude: 'CLAUDE.md',
|
|
47
|
+
codex: 'AGENTS.md',
|
|
48
|
+
cursor: 'AGENTS.md',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
function normalizeTargets(targets) {
|
|
52
|
+
const list = targets.includes('all') ? [...SUPPORTED_TARGETS] : [...targets];
|
|
53
|
+
const unique = [...new Set(list)];
|
|
54
|
+
for (const target of unique) {
|
|
55
|
+
if (!SUPPORTED_TARGETS.includes(target)) {
|
|
56
|
+
throw new Error(`unsupported target "${target}"`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return unique;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function blockContent(version) {
|
|
63
|
+
return [
|
|
64
|
+
BLOCK_START,
|
|
65
|
+
'## kitskill',
|
|
66
|
+
'',
|
|
67
|
+
'Read `.kitskill/router.md` and follow it every turn. It defines task triage,',
|
|
68
|
+
'verification, security, and memory rules.',
|
|
69
|
+
'',
|
|
70
|
+
'Before claiming done, run `node .kitskill/scripts/verify.mjs`.',
|
|
71
|
+
'Read `.kitskill/profile.json` and match explanations to the domain level.',
|
|
72
|
+
'Install the pre-commit gate with `kitskill hook install`.',
|
|
73
|
+
'',
|
|
74
|
+
`Installed by kitskill ${version}. Do not edit this block; edit the router instead.`,
|
|
75
|
+
BLOCK_END,
|
|
76
|
+
].join('\n');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function upsertBlock(text, content) {
|
|
80
|
+
const source = text ?? '';
|
|
81
|
+
const pattern = /<!-- kitskill:start -->[\s\S]*?<!-- kitskill:end -->/;
|
|
82
|
+
if (pattern.test(source)) return source.replace(pattern, content);
|
|
83
|
+
if (source.trim() === '') return `${content}\n`;
|
|
84
|
+
return `${source.replace(/\s+$/, '')}\n\n${content}\n`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function stripBlock(text) {
|
|
88
|
+
const source = String(text ?? '');
|
|
89
|
+
const pattern = /\n?<!-- kitskill:start -->[\s\S]*?<!-- kitskill:end -->\n?/;
|
|
90
|
+
return source.replace(pattern, '\n').replace(/\n{3,}/g, '\n\n').trimEnd();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function walkFiles(dir, base = dir) {
|
|
94
|
+
const out = [];
|
|
95
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
96
|
+
for (const entry of entries) {
|
|
97
|
+
const abs = path.join(dir, entry.name);
|
|
98
|
+
if (entry.isDirectory()) out.push(...(await walkFiles(abs, base)));
|
|
99
|
+
else if (entry.isFile()) out.push({ abs, rel: path.relative(base, abs) });
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function listSkills(templatesDir) {
|
|
105
|
+
const skillsDir = path.join(templatesDir, 'skills');
|
|
106
|
+
if (!(await pathExists(skillsDir))) return [];
|
|
107
|
+
const entries = await readdir(skillsDir, { withFileTypes: true });
|
|
108
|
+
const skills = [];
|
|
109
|
+
for (const entry of entries) {
|
|
110
|
+
if (!entry.isDirectory()) continue;
|
|
111
|
+
const dir = path.join(skillsDir, entry.name);
|
|
112
|
+
const files = await walkFiles(dir);
|
|
113
|
+
skills.push({ id: entry.name, dir, files });
|
|
114
|
+
}
|
|
115
|
+
return skills.sort((a, b) => a.id.localeCompare(b.id));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function planInstall({ templatesDir, targets, version }) {
|
|
119
|
+
const normalized = normalizeTargets(targets);
|
|
120
|
+
const files = CORE_FILES.map((item) => ({
|
|
121
|
+
kind: 'file',
|
|
122
|
+
source: item.source,
|
|
123
|
+
sourceAbs: path.join(templatesDir, item.source),
|
|
124
|
+
target: item.target,
|
|
125
|
+
preserve: Boolean(item.preserve),
|
|
126
|
+
}));
|
|
127
|
+
|
|
128
|
+
const skills = await listSkills(templatesDir);
|
|
129
|
+
for (const skill of skills) {
|
|
130
|
+
for (const file of skill.files) {
|
|
131
|
+
files.push({
|
|
132
|
+
kind: 'file',
|
|
133
|
+
source: path.join('skills', skill.id, file.rel),
|
|
134
|
+
sourceAbs: file.abs,
|
|
135
|
+
target: path.posix.join('.agents/skills', skill.id, file.rel.split(path.sep).join('/')),
|
|
136
|
+
preserve: false,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const links = normalized.includes('claude')
|
|
142
|
+
? skills.map((skill) => ({
|
|
143
|
+
kind: 'symlink',
|
|
144
|
+
target: path.posix.join('.claude/skills', skill.id),
|
|
145
|
+
linkTarget: path.posix.join('..', '..', '.agents', 'skills', skill.id),
|
|
146
|
+
source: path.posix.join('.agents/skills', skill.id),
|
|
147
|
+
}))
|
|
148
|
+
: [];
|
|
149
|
+
|
|
150
|
+
const blocks = [];
|
|
151
|
+
const seenBlocks = new Set();
|
|
152
|
+
for (const target of normalized) {
|
|
153
|
+
const file = TARGET_BLOCK_FILE[target];
|
|
154
|
+
if (seenBlocks.has(file)) continue;
|
|
155
|
+
seenBlocks.add(file);
|
|
156
|
+
blocks.push({
|
|
157
|
+
kind: 'block',
|
|
158
|
+
id: target,
|
|
159
|
+
path: file,
|
|
160
|
+
content: blockContent(version),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return { targets: normalized, files, links, blocks, skills };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function preflightPaths(root, paths, options) {
|
|
168
|
+
for (const rel of paths) {
|
|
169
|
+
await assertRealInside(root, resolveInside(root, rel), undefined, options);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function initProject(root, { templatesDir, targets, version, dryRun = false }) {
|
|
174
|
+
const manifestPath = resolveInside(root, '.kitskill/manifest.json');
|
|
175
|
+
if (await pathExists(manifestPath)) {
|
|
176
|
+
return { status: 'already-installed', manifest: await readManifest(manifestPath) };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const plan = await planInstall({ templatesDir, targets, version });
|
|
180
|
+
await preflightPaths(root, [
|
|
181
|
+
...plan.files.map((item) => item.target),
|
|
182
|
+
...plan.links.map((item) => item.target),
|
|
183
|
+
...plan.blocks.map((item) => item.path),
|
|
184
|
+
'.kitskill/manifest.json',
|
|
185
|
+
]);
|
|
186
|
+
const installed = [];
|
|
187
|
+
const skipped = [];
|
|
188
|
+
|
|
189
|
+
for (const item of plan.files) {
|
|
190
|
+
const abs = resolveInside(root, item.target);
|
|
191
|
+
if (await pathExists(abs)) {
|
|
192
|
+
skipped.push({ path: item.target, reason: 'exists' });
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const content = await readFile(item.sourceAbs);
|
|
196
|
+
if (!dryRun) {
|
|
197
|
+
await mkdir(path.dirname(abs), { recursive: true });
|
|
198
|
+
await writeFileSafe(root, item.target, content);
|
|
199
|
+
}
|
|
200
|
+
installed.push({
|
|
201
|
+
path: item.target,
|
|
202
|
+
kind: 'file',
|
|
203
|
+
source: item.source,
|
|
204
|
+
hash: hashText(content.toString('utf8')),
|
|
205
|
+
preserve: item.preserve,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (const link of plan.links) {
|
|
210
|
+
const abs = resolveInside(root, link.target);
|
|
211
|
+
if (await pathExists(abs)) {
|
|
212
|
+
skipped.push({ path: link.target, reason: 'exists' });
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (!dryRun) {
|
|
216
|
+
await mkdir(path.dirname(abs), { recursive: true });
|
|
217
|
+
try {
|
|
218
|
+
await symlink(link.linkTarget, abs, 'dir');
|
|
219
|
+
installed.push({ path: link.target, kind: 'symlink', target: link.linkTarget });
|
|
220
|
+
} catch {
|
|
221
|
+
const source = resolveInside(root, link.source);
|
|
222
|
+
await cp(source, abs, { recursive: true });
|
|
223
|
+
installed.push({ path: link.target, kind: 'copy', target: link.linkTarget });
|
|
224
|
+
}
|
|
225
|
+
} else {
|
|
226
|
+
installed.push({ path: link.target, kind: 'symlink', target: link.linkTarget });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const blocks = [];
|
|
231
|
+
for (const block of plan.blocks) {
|
|
232
|
+
const abs = resolveInside(root, block.path);
|
|
233
|
+
const existed = await pathExists(abs);
|
|
234
|
+
const current = await readText(abs);
|
|
235
|
+
if (!dryRun) {
|
|
236
|
+
await writeFileSafe(root, block.path, upsertBlock(current, block.content));
|
|
237
|
+
}
|
|
238
|
+
blocks.push({ path: block.path, id: block.id, created: !existed });
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (dryRun) {
|
|
242
|
+
return { status: 'dry-run', plan, installed, skipped };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const manifest = createManifest({ version, targets: plan.targets, files: installed, blocks });
|
|
246
|
+
await writeManifest(manifestPath, manifest);
|
|
247
|
+
return { status: 'installed', manifest, installed, skipped };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export async function updateProject(root, { templatesDir, version, dryRun = false }) {
|
|
251
|
+
const manifestPath = resolveInside(root, '.kitskill/manifest.json');
|
|
252
|
+
const manifest = await readManifest(manifestPath);
|
|
253
|
+
await preflightPaths(root, [
|
|
254
|
+
'.kitskill/manifest.json',
|
|
255
|
+
...manifest.files.map((entry) => entry.path),
|
|
256
|
+
...manifest.blocks.map((block) => block.path),
|
|
257
|
+
]);
|
|
258
|
+
const updated = [];
|
|
259
|
+
const preserved = [];
|
|
260
|
+
|
|
261
|
+
for (const entry of manifest.files) {
|
|
262
|
+
if (entry.kind !== 'file' || !entry.source) continue;
|
|
263
|
+
const abs = resolveInside(root, entry.path);
|
|
264
|
+
if (!(await pathExists(abs))) {
|
|
265
|
+
preserved.push({ path: entry.path, reason: 'missing' });
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (entry.preserve) {
|
|
269
|
+
preserved.push({ path: entry.path, reason: 'preserved' });
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
const currentHash = await hashFile(abs);
|
|
273
|
+
if (currentHash !== entry.hash) {
|
|
274
|
+
preserved.push({ path: entry.path, reason: 'modified' });
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const sourceAbs = path.join(templatesDir, entry.source);
|
|
278
|
+
if (!(await pathExists(sourceAbs))) continue;
|
|
279
|
+
const nextContent = await readFile(sourceAbs);
|
|
280
|
+
const nextHash = hashText(nextContent.toString('utf8'));
|
|
281
|
+
if (nextHash === entry.hash) continue;
|
|
282
|
+
if (!dryRun) {
|
|
283
|
+
await writeFileSafe(root, entry.path, nextContent, {
|
|
284
|
+
backupRoot: path.join(root, '.kitskill/backups'),
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
entry.hash = nextHash;
|
|
288
|
+
updated.push(entry.path);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
for (const block of manifest.blocks) {
|
|
292
|
+
const abs = resolveInside(root, block.path);
|
|
293
|
+
const current = await readText(abs);
|
|
294
|
+
if (current == null) continue;
|
|
295
|
+
if (!dryRun) await writeFileSafe(root, block.path, upsertBlock(current, blockContent(version)));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (!dryRun) {
|
|
299
|
+
manifest.version = version;
|
|
300
|
+
await writeManifest(manifestPath, manifest);
|
|
301
|
+
}
|
|
302
|
+
return { status: dryRun ? 'dry-run' : 'updated', updated, preserved, manifest };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export async function uninstallProject(root, { dryRun = false, force = false, purgeState = false } = {}) {
|
|
306
|
+
const manifestPath = resolveInside(root, '.kitskill/manifest.json');
|
|
307
|
+
const manifest = await readManifest(manifestPath);
|
|
308
|
+
await preflightPaths(
|
|
309
|
+
root,
|
|
310
|
+
['.kitskill/manifest.json', ...manifest.files.map((entry) => entry.path)],
|
|
311
|
+
{ followLast: false },
|
|
312
|
+
);
|
|
313
|
+
await preflightPaths(root, manifest.blocks.map((block) => block.path));
|
|
314
|
+
const removed = [];
|
|
315
|
+
const preserved = [];
|
|
316
|
+
|
|
317
|
+
for (const entry of manifest.files) {
|
|
318
|
+
if (entry.preserve && !purgeState) {
|
|
319
|
+
preserved.push({ path: entry.path, reason: 'state-preserved' });
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const abs = resolveInside(root, entry.path);
|
|
323
|
+
if (!(await pathExists(abs))) continue;
|
|
324
|
+
|
|
325
|
+
if (entry.kind === 'symlink') {
|
|
326
|
+
const target = await readLinkTarget(abs);
|
|
327
|
+
if (target !== entry.target && !force) {
|
|
328
|
+
preserved.push({ path: entry.path, reason: 'symlink-target-changed' });
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (!dryRun) await removeEntry(abs);
|
|
332
|
+
removed.push(entry.path);
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (entry.kind === 'copy') {
|
|
337
|
+
if (!force) {
|
|
338
|
+
preserved.push({ path: entry.path, reason: 'copied-fallback' });
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (!dryRun) await rm(abs, { recursive: true, force: true });
|
|
342
|
+
removed.push(entry.path);
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const currentHash = await hashFile(abs);
|
|
347
|
+
if (currentHash !== entry.hash && !force) {
|
|
348
|
+
preserved.push({ path: entry.path, reason: 'modified' });
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
if (!dryRun) {
|
|
352
|
+
await removeEntry(abs);
|
|
353
|
+
await pruneEmptyParents(root, abs, path.resolve(root));
|
|
354
|
+
}
|
|
355
|
+
removed.push(entry.path);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const blocks = [];
|
|
359
|
+
for (const block of manifest.blocks) {
|
|
360
|
+
const abs = resolveInside(root, block.path);
|
|
361
|
+
const current = await readText(abs);
|
|
362
|
+
if (current == null) continue;
|
|
363
|
+
const next = stripBlock(current);
|
|
364
|
+
if (!dryRun) {
|
|
365
|
+
if (next.trim() === '' && block.created) await unlink(abs);
|
|
366
|
+
else await writeFileSafe(root, block.path, `${next}\n`);
|
|
367
|
+
}
|
|
368
|
+
blocks.push(block.path);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (!dryRun) await unlink(manifestPath);
|
|
372
|
+
return { status: dryRun ? 'dry-run' : 'uninstalled', removed, preserved, blocks, manifest };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export async function doctorProject(root) {
|
|
376
|
+
const manifestPath = resolveInside(root, '.kitskill/manifest.json');
|
|
377
|
+
const errors = [];
|
|
378
|
+
const warnings = [];
|
|
379
|
+
let manifest;
|
|
380
|
+
try {
|
|
381
|
+
manifest = await readManifest(manifestPath);
|
|
382
|
+
} catch (error) {
|
|
383
|
+
return { errors: [`manifest: ${error.message}`], warnings, manifest: null };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
for (const entry of manifest.files) {
|
|
387
|
+
const abs = resolveInside(root, entry.path);
|
|
388
|
+
if (!(await pathExists(abs))) {
|
|
389
|
+
errors.push(`missing: ${entry.path}`);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (entry.kind === 'file') {
|
|
393
|
+
if (entry.preserve) continue;
|
|
394
|
+
const hash = await hashFile(abs);
|
|
395
|
+
if (hash !== entry.hash) warnings.push(`modified: ${entry.path}`);
|
|
396
|
+
} else if (entry.kind === 'symlink') {
|
|
397
|
+
const target = await readLinkTarget(abs);
|
|
398
|
+
if (target !== entry.target) errors.push(`symlink changed: ${entry.path}`);
|
|
399
|
+
} else if (entry.kind === 'copy') {
|
|
400
|
+
warnings.push(`copied fallback: ${entry.path}`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
for (const block of manifest.blocks) {
|
|
405
|
+
const abs = resolveInside(root, block.path);
|
|
406
|
+
const text = await readText(abs);
|
|
407
|
+
if (text == null || !text.includes(BLOCK_START)) {
|
|
408
|
+
errors.push(`instruction block missing: ${block.path}`);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const router = await readText(resolveInside(root, '.kitskill/router.md'));
|
|
413
|
+
if (router != null) {
|
|
414
|
+
const result = validateRouter(router);
|
|
415
|
+
errors.push(...result.errors);
|
|
416
|
+
warnings.push(...result.warnings);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const skillsDir = resolveInside(root, '.agents/skills');
|
|
420
|
+
if (await pathExists(skillsDir)) {
|
|
421
|
+
const entries = await readdir(skillsDir, { withFileTypes: true });
|
|
422
|
+
const ids = new Set();
|
|
423
|
+
for (const entry of entries) {
|
|
424
|
+
if (!entry.isDirectory()) continue;
|
|
425
|
+
const skillFile = path.join(skillsDir, entry.name, 'SKILL.md');
|
|
426
|
+
if (!(await pathExists(skillFile))) {
|
|
427
|
+
errors.push(`skill without SKILL.md: ${entry.name}`);
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
ids.add(entry.name);
|
|
431
|
+
const result = validateSkill(entry.name, await readFile(skillFile, 'utf8'));
|
|
432
|
+
errors.push(...result.errors.map((error) => `${entry.name}: ${error}`));
|
|
433
|
+
warnings.push(...result.warnings.map((warning) => `${entry.name}: ${warning}`));
|
|
434
|
+
}
|
|
435
|
+
const claudeDir = resolveInside(root, '.claude/skills');
|
|
436
|
+
if (await pathExists(claudeDir)) {
|
|
437
|
+
for (const entry of await readdir(claudeDir, { withFileTypes: true })) {
|
|
438
|
+
if (!ids.has(entry.name)) continue;
|
|
439
|
+
if (entry.isSymbolicLink()) {
|
|
440
|
+
const target = await readLinkTarget(path.join(claudeDir, entry.name));
|
|
441
|
+
if (target === path.posix.join('..', '..', '.agents', 'skills', entry.name)) continue;
|
|
442
|
+
}
|
|
443
|
+
warnings.push(`duplicate skill id across roots: ${entry.name}`);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return { errors, warnings, manifest };
|
|
449
|
+
}
|
package/lib/manifest.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
|
+
|
|
4
|
+
export const MANIFEST_SCHEMA = 1;
|
|
5
|
+
|
|
6
|
+
export function hashBuffer(buffer) {
|
|
7
|
+
return createHash('sha256').update(buffer).digest('hex');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function hashText(text) {
|
|
11
|
+
return hashBuffer(Buffer.from(text, 'utf8'));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function hashFile(file) {
|
|
15
|
+
return hashBuffer(await readFile(file));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createManifest({ version, targets, files = [], blocks = [] }) {
|
|
19
|
+
const now = new Date().toISOString();
|
|
20
|
+
return {
|
|
21
|
+
schema: MANIFEST_SCHEMA,
|
|
22
|
+
version,
|
|
23
|
+
targets,
|
|
24
|
+
createdAt: now,
|
|
25
|
+
updatedAt: now,
|
|
26
|
+
files: [...files].sort((a, b) => a.path.localeCompare(b.path)),
|
|
27
|
+
blocks: [...blocks].sort((a, b) => a.path.localeCompare(b.path)),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function readManifest(file) {
|
|
32
|
+
const raw = await readFile(file, 'utf8');
|
|
33
|
+
const data = JSON.parse(raw);
|
|
34
|
+
if (data.schema !== MANIFEST_SCHEMA) {
|
|
35
|
+
throw new Error(`unsupported manifest schema: ${data.schema}`);
|
|
36
|
+
}
|
|
37
|
+
if (!Array.isArray(data.files) || !Array.isArray(data.blocks)) {
|
|
38
|
+
throw new Error('malformed manifest');
|
|
39
|
+
}
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function writeManifest(file, manifest) {
|
|
44
|
+
const next = { ...manifest, updatedAt: new Date().toISOString() };
|
|
45
|
+
await writeFile(file, `${JSON.stringify(next, null, 2)}\n`, 'utf8');
|
|
46
|
+
return next;
|
|
47
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { copyFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { pathExists, readText, writeFileSafe } from './fs-safe.mjs';
|
|
5
|
+
|
|
6
|
+
export const OPENCODE_CONFIG_FILES = ['opencode.json', 'opencode.jsonc'];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Least-privilege profile for OpenCode V2.
|
|
10
|
+
* Rules are ordered: later rules win.
|
|
11
|
+
*/
|
|
12
|
+
export function recommendedRules() {
|
|
13
|
+
return [
|
|
14
|
+
{ action: 'external_directory', resource: '*', effect: 'ask' },
|
|
15
|
+
{ action: 'read', resource: '*.env', effect: 'deny' },
|
|
16
|
+
{ action: 'read', resource: '*.env.*', effect: 'deny' },
|
|
17
|
+
{ action: 'read', resource: '*.env.example', effect: 'allow' },
|
|
18
|
+
{ action: 'edit', resource: '*.env', effect: 'deny' },
|
|
19
|
+
{ action: 'edit', resource: '*.env.*', effect: 'deny' },
|
|
20
|
+
{ action: 'shell', resource: '*', effect: 'ask' },
|
|
21
|
+
{ action: 'shell', resource: 'git status *', effect: 'allow' },
|
|
22
|
+
{ action: 'shell', resource: 'git diff *', effect: 'allow' },
|
|
23
|
+
{ action: 'shell', resource: 'git log *', effect: 'allow' },
|
|
24
|
+
{ action: 'shell', resource: 'git show *', effect: 'allow' },
|
|
25
|
+
{ action: 'shell', resource: 'ls *', effect: 'allow' },
|
|
26
|
+
{ action: 'shell', resource: 'pwd', effect: 'allow' },
|
|
27
|
+
{ action: 'shell', resource: 'cat *', effect: 'allow' },
|
|
28
|
+
{ action: 'shell', resource: 'git push *', effect: 'deny' },
|
|
29
|
+
{ action: 'shell', resource: 'git reset --hard *', effect: 'deny' },
|
|
30
|
+
{ action: 'shell', resource: 'git clean -f*', effect: 'deny' },
|
|
31
|
+
{ action: 'shell', resource: 'rm -rf /*', effect: 'deny' },
|
|
32
|
+
{ action: 'shell', resource: 'rm -rf ~*', effect: 'deny' },
|
|
33
|
+
{ action: 'shell', resource: 'rm -rf ..*', effect: 'deny' },
|
|
34
|
+
{ action: 'shell', resource: 'rm -rf .git*', effect: 'deny' },
|
|
35
|
+
{ action: 'shell', resource: 'cat *.env*', effect: 'deny' },
|
|
36
|
+
{ action: 'shell', resource: 'grep * .env*', effect: 'deny' },
|
|
37
|
+
{ action: 'shell', resource: 'cp *.env* *', effect: 'deny' },
|
|
38
|
+
{ action: 'skill', resource: '*', effect: 'ask' },
|
|
39
|
+
{ action: 'skill', resource: 'ks-*', effect: 'allow' },
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function ruleKey(rule) {
|
|
44
|
+
return `${rule.action}\u0000${rule.resource}\u0000${rule.effect}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function mergeRules(existing = [], recommended = recommendedRules()) {
|
|
48
|
+
const merged = [...existing];
|
|
49
|
+
const seen = new Set(merged.map(ruleKey));
|
|
50
|
+
for (const rule of recommended) {
|
|
51
|
+
const key = ruleKey(rule);
|
|
52
|
+
if (seen.has(key)) continue;
|
|
53
|
+
merged.push(rule);
|
|
54
|
+
seen.add(key);
|
|
55
|
+
}
|
|
56
|
+
return merged;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function recommendedConfig() {
|
|
60
|
+
return {
|
|
61
|
+
$schema: 'https://opencode.ai/config.json',
|
|
62
|
+
permissions: recommendedRules(),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function readProjectConfig(root) {
|
|
67
|
+
for (const file of OPENCODE_CONFIG_FILES) {
|
|
68
|
+
const abs = path.join(root, file);
|
|
69
|
+
if (await pathExists(abs)) {
|
|
70
|
+
return { file, abs, text: await readText(abs) };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function applyPermissions(root, { dryRun = false, merge = false } = {}) {
|
|
77
|
+
const existing = await readProjectConfig(root);
|
|
78
|
+
if (existing && !merge) {
|
|
79
|
+
return { status: 'conflict', file: existing.file };
|
|
80
|
+
}
|
|
81
|
+
let config = recommendedConfig();
|
|
82
|
+
let backup = null;
|
|
83
|
+
if (existing) {
|
|
84
|
+
if (existing.file.endsWith('.jsonc')) {
|
|
85
|
+
return { status: 'conflict', file: existing.file, reason: 'jsonc' };
|
|
86
|
+
}
|
|
87
|
+
let current;
|
|
88
|
+
try {
|
|
89
|
+
current = JSON.parse(existing.text);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return { status: 'conflict', file: existing.file, reason: error.message };
|
|
92
|
+
}
|
|
93
|
+
config = {
|
|
94
|
+
...current,
|
|
95
|
+
permissions: mergeRules(Array.isArray(current.permissions) ? current.permissions : [], recommendedRules()),
|
|
96
|
+
};
|
|
97
|
+
if (!dryRun) {
|
|
98
|
+
backup = `${existing.abs}.bak-${Date.now()}`;
|
|
99
|
+
await copyFile(existing.abs, backup);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!dryRun) {
|
|
103
|
+
await writeFileSafe(root, existing?.file ?? 'opencode.json', `${JSON.stringify(config, null, 2)}\n`);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
status: dryRun ? 'dry-run' : existing ? 'merged' : 'applied',
|
|
107
|
+
file: existing?.file ?? 'opencode.json',
|
|
108
|
+
config,
|
|
109
|
+
backup,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function auditPermissions(config) {
|
|
114
|
+
const warnings = [];
|
|
115
|
+
const rules = Array.isArray(config?.permissions) ? config.permissions : [];
|
|
116
|
+
if (rules.length === 0) {
|
|
117
|
+
warnings.push('no project permission rules; OpenCode defaults apply');
|
|
118
|
+
return { errors: [], warnings };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const broadShell = rules.some(
|
|
122
|
+
(rule) => (rule.action === 'shell' || rule.action === '*') && rule.resource === '*' && rule.effect === 'allow',
|
|
123
|
+
);
|
|
124
|
+
if (broadShell) warnings.push('broad shell allow: shell * allow');
|
|
125
|
+
|
|
126
|
+
const envGuard = rules.some(
|
|
127
|
+
(rule) => rule.action === 'read' && /\.env/.test(rule.resource) && (rule.effect === 'deny' || rule.effect === 'ask'),
|
|
128
|
+
);
|
|
129
|
+
if (!envGuard) warnings.push('no .env read guard');
|
|
130
|
+
const envEditGuard = rules.some(
|
|
131
|
+
(rule) => rule.action === 'edit' && /\.env/.test(rule.resource) && rule.effect === 'deny',
|
|
132
|
+
);
|
|
133
|
+
if (!envEditGuard) warnings.push('no .env edit guard');
|
|
134
|
+
const forcePushGuard = rules.some(
|
|
135
|
+
(rule) => rule.action === 'shell' && /git push/.test(rule.resource) && rule.effect === 'deny',
|
|
136
|
+
);
|
|
137
|
+
if (!forcePushGuard) warnings.push('no force-push deny rule');
|
|
138
|
+
const destructiveGuard = rules.some(
|
|
139
|
+
(rule) => rule.action === 'shell' && /rm -rf (\/|~|\.\.)/.test(rule.resource) && rule.effect === 'deny',
|
|
140
|
+
);
|
|
141
|
+
if (!destructiveGuard) warnings.push('no rm -rf root/home deny rule');
|
|
142
|
+
|
|
143
|
+
const externalGuard = rules.some(
|
|
144
|
+
(rule) => rule.action === 'external_directory' && (rule.effect === 'deny' || rule.effect === 'ask'),
|
|
145
|
+
);
|
|
146
|
+
if (!externalGuard) warnings.push('external_directory is not guarded');
|
|
147
|
+
|
|
148
|
+
const skillsAllowed = rules.some(
|
|
149
|
+
(rule) => rule.action === 'skill' && rule.resource === 'ks-*' && rule.effect === 'allow',
|
|
150
|
+
);
|
|
151
|
+
if (!skillsAllowed) warnings.push('kitskill skills are not explicitly allowed');
|
|
152
|
+
|
|
153
|
+
return { errors: [], warnings };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function auditProjectPermissions(root) {
|
|
157
|
+
const existing = await readProjectConfig(root);
|
|
158
|
+
if (!existing) {
|
|
159
|
+
return {
|
|
160
|
+
errors: [],
|
|
161
|
+
warnings: ['no project permission profile; run init --apply-permissions opencode'],
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (existing.file.endsWith('.jsonc')) {
|
|
165
|
+
return {
|
|
166
|
+
errors: [],
|
|
167
|
+
warnings: [`${existing.file}: JSONC is not parsed by doctor; audit it manually`],
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
return auditPermissions(JSON.parse(existing.text));
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return { errors: [`${existing.file}: ${error.message}`], warnings: [] };
|
|
174
|
+
}
|
|
175
|
+
}
|