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,66 @@
|
|
|
1
|
+
const ENV_EXAMPLE = /\.env\.example$/i;
|
|
2
|
+
const ENV_PATH = /(^|[\\/])\.env(\.|$)/i;
|
|
3
|
+
const ENV_COMMAND = /(^|[\s/'"])\.env(\.|\s|$)/i;
|
|
4
|
+
|
|
5
|
+
const SHELL_RULES = [
|
|
6
|
+
{ pattern: /(^|\s)git\s+push(\s|$)/, reason: 'git push is denied by default; run it yourself' },
|
|
7
|
+
{ pattern: /(^|\s)git\s+reset\s+--hard(\s|$)/, reason: 'git reset --hard discards working changes' },
|
|
8
|
+
{ pattern: /(^|\s)git\s+clean\s+-[a-z]*f/i, reason: 'git clean -f deletes untracked files, including secrets' },
|
|
9
|
+
{
|
|
10
|
+
pattern: /(^|\s)rm\s+-[a-z]*r[a-z]*f[a-z]*\s+(\/|~|\.\.|\.git)(\s|$|\/)/i,
|
|
11
|
+
reason: 'recursive delete outside the workspace or of .git is denied',
|
|
12
|
+
},
|
|
13
|
+
{ pattern: /(^|\s)(sudo|doas)(\s|$)/, reason: 'privilege escalation is denied' },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
export function classifyPermission(action, resources = []) {
|
|
17
|
+
const list = (resources ?? []).map((resource) => String(resource));
|
|
18
|
+
if (action === 'read' || action === 'edit') {
|
|
19
|
+
for (const resource of list) {
|
|
20
|
+
if (ENV_EXAMPLE.test(resource)) continue;
|
|
21
|
+
if (ENV_PATH.test(resource)) {
|
|
22
|
+
return {
|
|
23
|
+
effect: 'deny',
|
|
24
|
+
message: `kitskill policy: ${action} of .env is denied`,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
if (action === 'shell') {
|
|
31
|
+
for (const resource of list) {
|
|
32
|
+
if (!ENV_EXAMPLE.test(resource) && ENV_COMMAND.test(resource)) {
|
|
33
|
+
return {
|
|
34
|
+
effect: 'deny',
|
|
35
|
+
message: 'kitskill policy: shell access to .env is denied',
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
for (const rule of SHELL_RULES) {
|
|
39
|
+
if (rule.pattern.test(resource)) {
|
|
40
|
+
return { effect: 'deny', message: `kitskill policy: ${rule.reason}` };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default {
|
|
49
|
+
id: 'kitskill.policy',
|
|
50
|
+
async setup(ctx) {
|
|
51
|
+
await ctx.permission.hook('evaluate', (event) => {
|
|
52
|
+
const decision = classifyPermission(event.action, event.resources ?? []);
|
|
53
|
+
if (!decision) return;
|
|
54
|
+
event.effect = decision.effect;
|
|
55
|
+
event.message = decision.message;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await ctx.tool.hook('execute.before', (event) => {
|
|
59
|
+
if (event.tool !== 'shell') return;
|
|
60
|
+
const command = event.input?.command;
|
|
61
|
+
if (typeof command !== 'string') return;
|
|
62
|
+
const decision = classifyPermission('shell', [command]);
|
|
63
|
+
if (decision) throw new Error(decision.message);
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
7
|
+
const DEFAULT_GRAPH = 'graphify-out/graph.json';
|
|
8
|
+
const DEFAULT_MANIFEST = 'graphify-out/manifest.json';
|
|
9
|
+
|
|
10
|
+
function parseArgs(argv) {
|
|
11
|
+
const options = {
|
|
12
|
+
command: argv[0] ?? 'status',
|
|
13
|
+
term: null,
|
|
14
|
+
graph: DEFAULT_GRAPH,
|
|
15
|
+
manifest: DEFAULT_MANIFEST,
|
|
16
|
+
root: process.cwd(),
|
|
17
|
+
json: false,
|
|
18
|
+
limit: 10,
|
|
19
|
+
strict: false,
|
|
20
|
+
};
|
|
21
|
+
for (let i = 1; i < argv.length; i += 1) {
|
|
22
|
+
const arg = argv[i];
|
|
23
|
+
if (arg === '--json') options.json = true;
|
|
24
|
+
else if (arg === '--strict') options.strict = true;
|
|
25
|
+
else if (arg === '--graph') options.graph = argv[++i];
|
|
26
|
+
else if (arg === '--manifest') options.manifest = argv[++i];
|
|
27
|
+
else if (arg === '--root') options.root = path.resolve(argv[++i]);
|
|
28
|
+
else if (arg === '--limit') options.limit = Number.parseInt(argv[++i], 10);
|
|
29
|
+
else if (!arg.startsWith('-') && !options.term) options.term = arg;
|
|
30
|
+
else throw new Error(`unknown option: ${arg}`);
|
|
31
|
+
}
|
|
32
|
+
return options;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function loadJson(file) {
|
|
36
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function loadGraph(options) {
|
|
40
|
+
const file = path.resolve(options.root, options.graph);
|
|
41
|
+
if (!existsSync(file)) return { file, graph: null };
|
|
42
|
+
return { file, graph: loadJson(file) };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function freshness(options) {
|
|
46
|
+
const manifestFile = path.resolve(options.root, options.manifest);
|
|
47
|
+
if (!existsSync(manifestFile)) return { fresh: null, stale: [], checked: 0 };
|
|
48
|
+
let manifest;
|
|
49
|
+
try {
|
|
50
|
+
manifest = loadJson(manifestFile);
|
|
51
|
+
} catch {
|
|
52
|
+
return { fresh: null, stale: [], checked: 0 };
|
|
53
|
+
}
|
|
54
|
+
const stale = [];
|
|
55
|
+
let checked = 0;
|
|
56
|
+
for (const [file, entry] of Object.entries(manifest)) {
|
|
57
|
+
checked += 1;
|
|
58
|
+
const abs = path.resolve(options.root, file);
|
|
59
|
+
if (!existsSync(abs)) {
|
|
60
|
+
stale.push(file);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const mtime = statSync(abs).mtimeMs / 1000;
|
|
64
|
+
if (typeof entry.mtime === 'number' && Math.abs(mtime - entry.mtime) > 1) {
|
|
65
|
+
stale.push(file);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { fresh: stale.length === 0, stale, checked };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function neighbors(graph, node) {
|
|
72
|
+
const byId = new Map(graph.nodes.map((item) => [item.id, item]));
|
|
73
|
+
return graph.links
|
|
74
|
+
.filter((link) => link.source === node.id || link.target === node.id)
|
|
75
|
+
.map((link) => {
|
|
76
|
+
const other = link.source === node.id ? link.target : link.source;
|
|
77
|
+
return {
|
|
78
|
+
relation: link.relation,
|
|
79
|
+
id: other,
|
|
80
|
+
label: byId.get(other)?.label ?? other,
|
|
81
|
+
file: byId.get(other)?.source_file,
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function score(node, term) {
|
|
87
|
+
const value = term.toLowerCase();
|
|
88
|
+
const label = String(node.label ?? '').toLowerCase();
|
|
89
|
+
const id = String(node.id ?? '').toLowerCase();
|
|
90
|
+
const file = String(node.source_file ?? '').toLowerCase();
|
|
91
|
+
const community = String(node.community_name ?? '').toLowerCase();
|
|
92
|
+
if (label === value || id === value) return 3;
|
|
93
|
+
if (label.startsWith(value) || id.startsWith(value)) return 2;
|
|
94
|
+
if (label.includes(value) || id.includes(value) || file.includes(value) || community.includes(value)) return 1;
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function fallbackGrep(options, term) {
|
|
99
|
+
const args = [
|
|
100
|
+
'-n',
|
|
101
|
+
'--hidden',
|
|
102
|
+
'--glob',
|
|
103
|
+
'!graphify-out/**',
|
|
104
|
+
'--glob',
|
|
105
|
+
'!.git/**',
|
|
106
|
+
term,
|
|
107
|
+
'.',
|
|
108
|
+
];
|
|
109
|
+
let run = spawnSync('rg', args, { cwd: options.root, encoding: 'utf8' });
|
|
110
|
+
if (run.error) {
|
|
111
|
+
run = spawnSync(
|
|
112
|
+
'grep',
|
|
113
|
+
['-rn', '--exclude-dir=graphify-out', '--exclude-dir=.git', term, '.'],
|
|
114
|
+
{ cwd: options.root, encoding: 'utf8' },
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const lines = (run.stdout ?? '').split('\n').filter(Boolean).slice(0, 40);
|
|
118
|
+
return { command: run.error ? 'grep' : 'rg', lines };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function status(options) {
|
|
122
|
+
const { file, graph } = loadGraph(options);
|
|
123
|
+
const fresh = freshness(options);
|
|
124
|
+
const result = {
|
|
125
|
+
graph: file,
|
|
126
|
+
exists: graph !== null,
|
|
127
|
+
nodes: graph?.nodes?.length ?? 0,
|
|
128
|
+
links: graph?.links?.length ?? 0,
|
|
129
|
+
directed: graph?.directed ?? null,
|
|
130
|
+
...fresh,
|
|
131
|
+
};
|
|
132
|
+
if (options.json) {
|
|
133
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
if (!graph) {
|
|
137
|
+
process.stdout.write(`graph: ${file}\nmissing; fall back to grep and direct reads\n`);
|
|
138
|
+
return options.strict ? 1 : 0;
|
|
139
|
+
}
|
|
140
|
+
process.stdout.write(`graph: ${file}\n`);
|
|
141
|
+
process.stdout.write(`nodes: ${result.nodes}, links: ${result.links}, directed: ${result.directed}\n`);
|
|
142
|
+
process.stdout.write(
|
|
143
|
+
`freshness: ${result.fresh === null ? 'unknown' : result.fresh ? 'fresh' : 'stale'}\n`,
|
|
144
|
+
);
|
|
145
|
+
for (const stale of result.stale.slice(0, 20)) process.stdout.write(` stale: ${stale}\n`);
|
|
146
|
+
const failed = !result.exists || result.fresh === false;
|
|
147
|
+
return options.strict && failed ? 1 : 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function query(options) {
|
|
151
|
+
if (!options.term) throw new Error('usage: graph query <term>');
|
|
152
|
+
const { file, graph } = loadGraph(options);
|
|
153
|
+
const fresh = freshness(options);
|
|
154
|
+
if (!graph) {
|
|
155
|
+
const fallback = fallbackGrep(options, options.term);
|
|
156
|
+
if (options.json) {
|
|
157
|
+
process.stdout.write(`${JSON.stringify({ graph: file, matches: [], fallback }, null, 2)}\n`);
|
|
158
|
+
} else {
|
|
159
|
+
process.stdout.write(`graph missing; fallback ${fallback.command}:\n`);
|
|
160
|
+
for (const line of fallback.lines) process.stdout.write(`${line}\n`);
|
|
161
|
+
}
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
const matches = graph.nodes
|
|
165
|
+
.map((node) => ({ node, score: score(node, options.term) }))
|
|
166
|
+
.filter((item) => item.score > 0)
|
|
167
|
+
.sort((a, b) => b.score - a.score || String(a.node.label).localeCompare(String(b.node.label)))
|
|
168
|
+
.slice(0, options.limit)
|
|
169
|
+
.map((item) => ({ ...item.node, neighbors: neighbors(graph, item.node).slice(0, 5) }));
|
|
170
|
+
if (options.json) {
|
|
171
|
+
process.stdout.write(
|
|
172
|
+
`${JSON.stringify({ graph: file, fresh: fresh.fresh, matches, fallback: matches.length ? null : fallbackGrep(options, options.term) }, null, 2)}\n`,
|
|
173
|
+
);
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
process.stdout.write(`graph: ${file} (${fresh.fresh === false ? 'stale' : 'fresh'})\n`);
|
|
177
|
+
if (matches.length === 0) {
|
|
178
|
+
const fallback = fallbackGrep(options, options.term);
|
|
179
|
+
process.stdout.write(`no node match; fallback ${fallback.command}:\n`);
|
|
180
|
+
for (const line of fallback.lines) process.stdout.write(`${line}\n`);
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
for (const node of matches) {
|
|
184
|
+
process.stdout.write(`${node.label} — ${node.source_file ?? '?'}:${node.source_location ?? '?'} [${node.community_name ?? ''}]\n`);
|
|
185
|
+
for (const near of node.neighbors) {
|
|
186
|
+
process.stdout.write(` ${near.relation} → ${near.label}${near.file ? ` (${near.file})` : ''}\n`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
process.stdout.write('Graph is a lead; confirm in the source before editing.\n');
|
|
190
|
+
return 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function explain(options) {
|
|
194
|
+
if (!options.term) throw new Error('usage: graph explain <node>');
|
|
195
|
+
const { file, graph } = loadGraph(options);
|
|
196
|
+
if (!graph) throw new Error(`graph missing: ${file}; use grep and direct reads`);
|
|
197
|
+
const term = options.term.toLowerCase();
|
|
198
|
+
const node =
|
|
199
|
+
graph.nodes.find((item) => String(item.id).toLowerCase() === term) ??
|
|
200
|
+
graph.nodes.find((item) => String(item.label).toLowerCase() === term) ??
|
|
201
|
+
graph.nodes.find((item) => String(item.label).toLowerCase().includes(term));
|
|
202
|
+
if (!node) throw new Error(`node not found: ${options.term}`);
|
|
203
|
+
const result = { graph: file, node, neighbors: neighbors(graph, node) };
|
|
204
|
+
if (options.json) {
|
|
205
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
process.stdout.write(`${node.label} — ${node.source_file ?? '?'}:${node.source_location ?? '?'}\n`);
|
|
209
|
+
for (const near of result.neighbors) {
|
|
210
|
+
process.stdout.write(` ${near.relation} → ${near.label}${near.file ? ` (${near.file})` : ''}\n`);
|
|
211
|
+
}
|
|
212
|
+
process.stdout.write('Graph is a lead; confirm in the source before editing.\n');
|
|
213
|
+
return 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function main() {
|
|
217
|
+
const options = parseArgs(process.argv.slice(2));
|
|
218
|
+
if (options.command === 'status') return status(options);
|
|
219
|
+
if (options.command === 'query') return query(options);
|
|
220
|
+
if (options.command === 'explain') return explain(options);
|
|
221
|
+
throw new Error(`unknown graph command: ${options.command}`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url))) {
|
|
225
|
+
try {
|
|
226
|
+
process.exit(main());
|
|
227
|
+
} catch (error) {
|
|
228
|
+
process.stderr.write(`${error.message}\n`);
|
|
229
|
+
process.exit(2);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
|
|
7
|
+
const RISK_SURFACES = [
|
|
8
|
+
['auth', /(^|\/)(auth|authn|authz|session|login|logout|oauth|jwt|permission|rbac|acl)(\/|\.|$)/i],
|
|
9
|
+
['secrets', /(^|\/)(\.env|secrets?|credentials?|private[-_]?keys?)(\/|\.|$)/i],
|
|
10
|
+
['data/schema', /(^|\/)(migrations?|schema|models?|db|database|sql|repositories)(\/|\.|$)/i],
|
|
11
|
+
['payments', /(^|\/)(billing|payments?|stripe|checkout|invoices?)(\/|\.|$)/i],
|
|
12
|
+
['public-contract', /(^|\/)(api|routes?|controllers?|openapi|swagger|graphql|proto|handlers?)(\/|\.|$)/i],
|
|
13
|
+
['agent-config', /(^|\/)(AGENTS\.md|CLAUDE\.md|GEMINI\.md|opencode\.jsonc?|\.mcp\.json|\.claude|\.opencode|\.agents|\.kitskill)(\/|$)/],
|
|
14
|
+
['ci/infra', /(^|\/)(\.github\/workflows|Dockerfile|docker-compose|terraform|k8s|helm|infra)(\/|\.|$)/i],
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const SECRET_LINE = /(sk-[A-Za-z0-9]{20,}|ghp_[A-Za-z0-9]{20,}|gho_[A-Za-z0-9]{20,}|AKIA[0-9A-Z]{16}|BEGIN (?:RSA|OPENSSH|EC) PRIVATE KEY)/;
|
|
18
|
+
|
|
19
|
+
function runGit(cwd, args) {
|
|
20
|
+
return execFileSync('git', args, { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function scanSecrets(diffText) {
|
|
24
|
+
let count = 0;
|
|
25
|
+
const files = new Set();
|
|
26
|
+
let current = null;
|
|
27
|
+
for (const line of String(diffText).split('\n')) {
|
|
28
|
+
if (line.startsWith('+++ b/')) current = line.slice(6);
|
|
29
|
+
else if (line.startsWith('+') && !line.startsWith('+++') && SECRET_LINE.test(line)) {
|
|
30
|
+
count += 1;
|
|
31
|
+
if (current) files.add(current);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { count, files: [...files] };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function redactSecrets(text) {
|
|
38
|
+
return String(text)
|
|
39
|
+
.split('\n')
|
|
40
|
+
.map((line) => (SECRET_LINE.test(line) ? line.replace(SECRET_LINE, '[REDACTED]') : line))
|
|
41
|
+
.join('\n');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function detectRisks(files, diffText = '') {
|
|
45
|
+
const surfaces = new Map();
|
|
46
|
+
for (const file of files) {
|
|
47
|
+
for (const [name, pattern] of RISK_SURFACES) {
|
|
48
|
+
if (pattern.test(file)) {
|
|
49
|
+
if (!surfaces.has(name)) surfaces.set(name, { files: new Set(), note: null });
|
|
50
|
+
surfaces.get(name).files.add(file);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const secrets = scanSecrets(diffText);
|
|
55
|
+
if (secrets.count > 0) {
|
|
56
|
+
surfaces.set('secret-like content', {
|
|
57
|
+
files: new Set(secrets.files),
|
|
58
|
+
note: `${secrets.count} added line(s); values are not printed`,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return [...surfaces.entries()].map(([name, value]) => ({
|
|
62
|
+
surface: name,
|
|
63
|
+
files: [...value.files].sort(),
|
|
64
|
+
note: value.note,
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function renderPacket({
|
|
69
|
+
base,
|
|
70
|
+
files,
|
|
71
|
+
diff,
|
|
72
|
+
numstat,
|
|
73
|
+
untracked,
|
|
74
|
+
risks,
|
|
75
|
+
maxLines,
|
|
76
|
+
maxFileLines,
|
|
77
|
+
includeContent = true,
|
|
78
|
+
}) {
|
|
79
|
+
const tracked = files.filter((file) => !file.status.includes('?')).length;
|
|
80
|
+
const untrackedCount = files.length - tracked;
|
|
81
|
+
const lines = [];
|
|
82
|
+
|
|
83
|
+
lines.push('# Review packet', '');
|
|
84
|
+
lines.push(`- Base: ${base ?? 'none (no merge base found)'}`);
|
|
85
|
+
lines.push(`- Changed files: ${files.length} (tracked ${tracked}, untracked ${untrackedCount})`);
|
|
86
|
+
lines.push(`- Diff: +${numstat.added} -${numstat.removed}`);
|
|
87
|
+
lines.push('');
|
|
88
|
+
|
|
89
|
+
lines.push('## Risk surfaces', '');
|
|
90
|
+
if (risks.length === 0) {
|
|
91
|
+
lines.push('None detected by path or diff heuristics.');
|
|
92
|
+
} else {
|
|
93
|
+
lines.push('| Surface | Files | Note |');
|
|
94
|
+
lines.push('|---|---|---|');
|
|
95
|
+
for (const risk of risks) {
|
|
96
|
+
lines.push(`| ${risk.surface} | ${risk.files.join('<br>') || '—'} | ${risk.note ?? '—'} |`);
|
|
97
|
+
}
|
|
98
|
+
lines.push('', 'Heuristics only. Confirm in the source before acting.');
|
|
99
|
+
}
|
|
100
|
+
lines.push('');
|
|
101
|
+
|
|
102
|
+
lines.push('## Files', '');
|
|
103
|
+
for (const file of files) lines.push(`${file.status}\t${file.path}`);
|
|
104
|
+
lines.push('');
|
|
105
|
+
|
|
106
|
+
if (!includeContent) return lines.join('\n');
|
|
107
|
+
|
|
108
|
+
const diffLines = redactSecrets(diff).split('\n');
|
|
109
|
+
const kept = diffLines.slice(0, maxLines);
|
|
110
|
+
const omitted = diffLines.length - kept.length;
|
|
111
|
+
lines.push('## Diff', '', '```diff', ...kept);
|
|
112
|
+
if (omitted > 0) lines.push(`[kitskill] ... ${omitted} diff line(s) omitted ...`);
|
|
113
|
+
lines.push('```', '');
|
|
114
|
+
|
|
115
|
+
if (untracked.length > 0) {
|
|
116
|
+
lines.push('## Untracked files', '');
|
|
117
|
+
for (const item of untracked) {
|
|
118
|
+
lines.push(`### ${item.path}`, '');
|
|
119
|
+
if (item.binary) {
|
|
120
|
+
lines.push('_binary file omitted_', '');
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const body = item.lines.slice(0, maxFileLines).map((line) =>
|
|
124
|
+
SECRET_LINE.test(line) ? line.replace(SECRET_LINE, '[REDACTED]') : line,
|
|
125
|
+
);
|
|
126
|
+
lines.push('```text', ...body);
|
|
127
|
+
if (item.lines.length > body.length) {
|
|
128
|
+
lines.push(`[kitskill] ... ${item.lines.length - body.length} line(s) omitted ...`);
|
|
129
|
+
}
|
|
130
|
+
lines.push('```', '');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return lines.join('\n');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function resolveBase(cwd) {
|
|
138
|
+
for (const ref of ['origin/HEAD', 'origin/main', 'origin/master', 'main', 'master']) {
|
|
139
|
+
try {
|
|
140
|
+
return runGit(cwd, ['merge-base', 'HEAD', ref]).trim();
|
|
141
|
+
} catch {
|
|
142
|
+
// try the next candidate
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function parseStatus(cwd) {
|
|
149
|
+
const out = runGit(cwd, ['status', '--porcelain=v1', '-z', '--untracked-files=all']);
|
|
150
|
+
const parts = out.split('\0');
|
|
151
|
+
const files = [];
|
|
152
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
153
|
+
const record = parts[i];
|
|
154
|
+
if (!record) continue;
|
|
155
|
+
const status = record.slice(0, 2);
|
|
156
|
+
const file = record.slice(3);
|
|
157
|
+
if (status[0] === 'R' || status[0] === 'C') i += 1;
|
|
158
|
+
files.push({ status, path: file });
|
|
159
|
+
}
|
|
160
|
+
return files;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function readUntracked(cwd, files) {
|
|
164
|
+
const result = [];
|
|
165
|
+
for (const file of files) {
|
|
166
|
+
const full = path.join(cwd, file.path);
|
|
167
|
+
try {
|
|
168
|
+
const buffer = readFileSync(full);
|
|
169
|
+
if (buffer.includes(0)) {
|
|
170
|
+
result.push({ path: file.path, binary: true, lines: [] });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
result.push({
|
|
174
|
+
path: file.path,
|
|
175
|
+
binary: false,
|
|
176
|
+
lines: buffer.toString('utf8').replace(/\r\n/g, '\n').split('\n'),
|
|
177
|
+
});
|
|
178
|
+
} catch {
|
|
179
|
+
result.push({ path: file.path, binary: true, lines: [] });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function buildPacket(cwd, { base = null, maxLines = 400, maxFileLines = 80 } = {}) {
|
|
186
|
+
const resolved = base ?? resolveBase(cwd);
|
|
187
|
+
const files = parseStatus(cwd);
|
|
188
|
+
const diff = resolved ? runGit(cwd, ['diff', resolved]) : runGit(cwd, ['diff', 'HEAD']);
|
|
189
|
+
const numstatRaw = resolved
|
|
190
|
+
? runGit(cwd, ['diff', '--numstat', resolved])
|
|
191
|
+
: runGit(cwd, ['diff', '--numstat', 'HEAD']);
|
|
192
|
+
const numstat = { added: 0, removed: 0 };
|
|
193
|
+
for (const line of numstatRaw.split('\n')) {
|
|
194
|
+
const [add, remove] = line.split('\t');
|
|
195
|
+
if (add !== '-' && remove !== '-') {
|
|
196
|
+
numstat.added += Number.parseInt(add, 10) || 0;
|
|
197
|
+
numstat.removed += Number.parseInt(remove, 10) || 0;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const risks = detectRisks(files.map((file) => file.path), diff);
|
|
201
|
+
const untracked = readUntracked(cwd, files.filter((file) => file.status === '??'));
|
|
202
|
+
return { base: resolved, files, diff, numstat, risks, untracked, maxLines, maxFileLines };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function parseArgs(argv) {
|
|
206
|
+
const options = { base: null, maxLines: 400, maxFileLines: 80, riskOnly: false, failOnRisk: false };
|
|
207
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
208
|
+
const arg = argv[i];
|
|
209
|
+
if (arg === '--base') options.base = argv[++i];
|
|
210
|
+
else if (arg === '--max-lines') options.maxLines = Number.parseInt(argv[++i], 10) || 400;
|
|
211
|
+
else if (arg === '--max-file-lines') options.maxFileLines = Number.parseInt(argv[++i], 10) || 80;
|
|
212
|
+
else if (arg === '--risk-only') options.riskOnly = true;
|
|
213
|
+
else if (arg === '--fail-on-risk') options.failOnRisk = true;
|
|
214
|
+
else if (arg === '--help' || arg === '-h') options.help = true;
|
|
215
|
+
}
|
|
216
|
+
return options;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function main() {
|
|
220
|
+
const options = parseArgs(process.argv.slice(2));
|
|
221
|
+
if (options.help) {
|
|
222
|
+
process.stdout.write(
|
|
223
|
+
'Usage: node review-packet.mjs [--base REF] [--max-lines N] [--max-file-lines N] [--risk-only] [--fail-on-risk]\n',
|
|
224
|
+
);
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
const cwd = process.cwd();
|
|
228
|
+
let packet;
|
|
229
|
+
try {
|
|
230
|
+
packet = buildPacket(cwd, options);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
process.stderr.write(`review-packet: ${error.message}\n`);
|
|
233
|
+
return 3;
|
|
234
|
+
}
|
|
235
|
+
if (options.riskOnly) {
|
|
236
|
+
process.stdout.write(`${renderPacket({ ...packet, includeContent: false })}\n`);
|
|
237
|
+
} else {
|
|
238
|
+
process.stdout.write(`${renderPacket(packet)}\n`);
|
|
239
|
+
}
|
|
240
|
+
return options.failOnRisk && packet.risks.length > 0 ? 1 : 0;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
244
|
+
try {
|
|
245
|
+
process.exit(main());
|
|
246
|
+
} catch (error) {
|
|
247
|
+
process.stderr.write(`${error.stack ?? error.message}\n`);
|
|
248
|
+
process.exit(1);
|
|
249
|
+
}
|
|
250
|
+
}
|