project-knowledge 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.
Files changed (59) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/INDEX.md +53 -0
  3. package/README.md +79 -0
  4. package/_site/README.md +63 -0
  5. package/_site/_test/ai-profile-test.js +199 -0
  6. package/_site/_test/baseline-schema-test.js +132 -0
  7. package/_site/_test/commit-analysis-test.js +184 -0
  8. package/_site/_test/context-pack-test.js +199 -0
  9. package/_site/_test/draft-apply-test.js +363 -0
  10. package/_site/_test/git-validation-test.js +171 -0
  11. package/_site/_test/hook-trigger-test.js +257 -0
  12. package/_site/_test/initial-analysis-test.js +228 -0
  13. package/_site/_test/job-orchestrator-test.js +297 -0
  14. package/_site/_test/kb-v2-templates-test.js +189 -0
  15. package/_site/_test/pr-consumer-contract-test.js +236 -0
  16. package/_site/_test/run-all-tests.js +135 -0
  17. package/_site/_test/scanner-test.js +206 -0
  18. package/_site/_test/ui-smoke-test.js +237 -0
  19. package/_site/_test/ui-test.js +237 -0
  20. package/_site/index.html +1166 -0
  21. package/_site/lib/ai-adapter.js +287 -0
  22. package/_site/lib/analysis-orchestrator.js +433 -0
  23. package/_site/lib/context-pack-builder.js +290 -0
  24. package/_site/lib/draft-apply.js +219 -0
  25. package/_site/lib/git-runner.js +26 -0
  26. package/_site/lib/hook-manager.js +148 -0
  27. package/_site/lib/job-orchestrator.js +231 -0
  28. package/_site/lib/kb-validator.js +224 -0
  29. package/_site/lib/llm-client.js +126 -0
  30. package/_site/lib/scanner.js +94 -0
  31. package/_site/scripts/hook-trigger.js +133 -0
  32. package/_site/scripts/safe-runner.js +151 -0
  33. package/_site/server.js +1058 -0
  34. package/_site/start.bat +26 -0
  35. package/_site/stop.bat +11 -0
  36. package/ai-profiles.json +18 -0
  37. package/docs/ai-knowledge-base-system-design.md +395 -0
  38. package/docs/pr-consumer-contract.md +198 -0
  39. package/docs/project-goal.md +72 -0
  40. package/docs/project-registry-schema.md +46 -0
  41. package/docs/testing-strategy.md +169 -0
  42. package/iterations.json +23 -0
  43. package/package.json +47 -0
  44. package/scripts/gen-commit-doc.ps1 +178 -0
  45. package/scripts/gen-commit-doc.sh +197 -0
  46. package/scripts/list-features.ps1 +41 -0
  47. package/scripts/register-scheduled-task.bat +5 -0
  48. package/templates/change.md +59 -0
  49. package/templates/commit-feature.md +56 -0
  50. package/templates/feature.md +44 -0
  51. package/templates/framework.md +80 -0
  52. package/templates/index-header.md +3 -0
  53. package/templates/kb-manifest.json +38 -0
  54. package/templates/module.md +58 -0
  55. package/templates/project-analysis.md +48 -0
  56. package/templates/project-goal.md +55 -0
  57. package/templates/project-readme.md +60 -0
  58. package/templates/quality-review-rules.md +37 -0
  59. package/templates/update-entry.md +7 -0
@@ -0,0 +1,290 @@
1
+ // Context Pack Builder (TASK-006)
2
+ // Collects Git diff / stats, project goal, related feature/module docs,
3
+ // package/config files, neighbouring source, and tests into a machine-readable
4
+ // context pack written to projects/<slug>/_ai/context-packs/<run-id>/.
5
+ //
6
+ // Path safety: every path is normalized and verified to live inside the project root
7
+ // before it is added to the pack. Outside-the-project paths are rejected.
8
+
9
+ const fs = require('fs');
10
+ const path = require('path');
11
+ const crypto = require('crypto');
12
+ const { execGit } = require('./git-runner');
13
+
14
+ const MAX_FILE_BYTES = 200 * 1024; // Skip files larger than 200 KB
15
+ const MAX_EXCERPT_BYTES = 8 * 1024; // Excerpt limited to 8 KB
16
+ const BINARY_DETECT_BYTES = 4096; // Read up to 4 KB to detect binary
17
+
18
+ const PACKAGE_CONFIG_FILES = [
19
+ 'package.json', 'package-lock.json', 'pnpm-workspace.yaml', 'pnpm-lock.yaml',
20
+ 'tsconfig.json', 'tsconfig.base.json', 'pyproject.toml', 'setup.py', 'requirements.txt',
21
+ 'Cargo.toml', 'Cargo.lock', 'go.mod', 'go.sum', 'pom.xml', 'build.gradle',
22
+ '.eslintrc.json', '.eslintrc.js', '.prettierrc.json',
23
+ ];
24
+
25
+ function shortHash(input) {
26
+ return crypto.createHash('sha1').update(input).digest('hex').slice(0, 12);
27
+ }
28
+
29
+ function isSafePath(projectRoot, target) {
30
+ const resolved = path.resolve(projectRoot, target);
31
+ const root = path.resolve(projectRoot);
32
+ // Use a separator-aware check so 'proj-other' does not pass 'proj' as parent.
33
+ return resolved === root || resolved.startsWith(root + path.sep) || resolved.startsWith(root + '/');
34
+ }
35
+
36
+ function isBinaryBuffer(buf) {
37
+ const limit = Math.min(BINARY_DETECT_BYTES, buf.length);
38
+ for (let i = 0; i < limit; i++) {
39
+ if (buf[i] === 0) return true;
40
+ }
41
+ return false;
42
+ }
43
+
44
+ function readSafeExcerpt(absPath) {
45
+ let stat;
46
+ try { stat = fs.statSync(absPath); } catch { return null; }
47
+ if (!stat.isFile()) return null;
48
+ if (stat.size > MAX_FILE_BYTES) {
49
+ return { excerpt: null, size: stat.size, binary: false, skipped: 'too-large' };
50
+ }
51
+ let buf;
52
+ try { buf = fs.readFileSync(absPath); } catch { return null; }
53
+ if (isBinaryBuffer(buf)) {
54
+ return { excerpt: null, size: stat.size, binary: true, skipped: 'binary' };
55
+ }
56
+ const text = buf.toString('utf-8');
57
+ if (text.length > MAX_EXCERPT_BYTES) {
58
+ return { excerpt: text.slice(0, MAX_EXCERPT_BYTES) + '\n...[truncated]', size: stat.size, binary: false, truncated: true };
59
+ }
60
+ return { excerpt: text, size: stat.size, binary: false, truncated: false };
61
+ }
62
+
63
+ function getNeighborPaths(projectRoot, changedFiles) {
64
+ const neighbors = new Set();
65
+ for (const file of changedFiles) {
66
+ if (!file) continue;
67
+ const dir = path.dirname(file);
68
+ if (!dir || dir === '.') continue;
69
+ try {
70
+ const entries = fs.readdirSync(path.join(projectRoot, dir), { withFileTypes: true });
71
+ for (const e of entries) {
72
+ if (!e.isFile()) continue;
73
+ if (e.name.startsWith('.')) continue;
74
+ const rel = path.posix.join(dir, e.name).replace(/\\/g, '/');
75
+ if (changedFiles.includes(rel)) continue;
76
+ neighbors.add(rel);
77
+ }
78
+ } catch {}
79
+ }
80
+ return [...neighbors];
81
+ }
82
+
83
+ function getTestPaths(projectRoot, changedFiles) {
84
+ const tests = new Set();
85
+ const isTestName = (name) => /\.(test|spec)\.[a-z]+$/i.test(name) || /^test[s]?[\/\\]/i.test(name) || /__tests__[\/\\]/i.test(name);
86
+ // Tests in the same directory tree, and direct "test" / "tests" siblings
87
+ for (const file of changedFiles) {
88
+ if (!file) continue;
89
+ const dir = path.dirname(file);
90
+ // Same-dir tests
91
+ try {
92
+ const entries = fs.readdirSync(path.join(projectRoot, dir), { withFileTypes: true });
93
+ for (const e of entries) {
94
+ if (!e.isFile()) continue;
95
+ if (isTestName(e.name)) {
96
+ tests.add(path.posix.join(dir, e.name).replace(/\\/g, '/'));
97
+ }
98
+ }
99
+ } catch {}
100
+ // Direct test/ folder sibling
101
+ for (const candidate of ['tests', 'test', '__tests__']) {
102
+ const testDir = path.join(projectRoot, dir, candidate);
103
+ if (!fs.existsSync(testDir)) continue;
104
+ try {
105
+ const entries = fs.readdirSync(testDir, { withFileTypes: true });
106
+ for (const e of entries) {
107
+ if (!e.isFile()) continue;
108
+ if (isTestName(e.name)) {
109
+ tests.add(path.posix.join(dir, candidate, e.name).replace(/\\/g, '/'));
110
+ }
111
+ }
112
+ } catch {}
113
+ }
114
+ }
115
+ return [...tests];
116
+ }
117
+
118
+ function findRelatedDocs(projectRoot, changedFiles) {
119
+ // Map files to existing feature/module docs by directory match.
120
+ // Lightweight heuristic: any feature or module doc whose frontmatter lists paths
121
+ // that intersect the changed files is "related". If frontmatter does not exist
122
+ // (legacy docs), fall back to filename match.
123
+ const out = new Set();
124
+ const featuresDir = path.join(projectRoot, 'features');
125
+ const modulesDir = path.join(projectRoot, 'modules');
126
+ for (const dir of [featuresDir, modulesDir]) {
127
+ if (!fs.existsSync(dir)) continue;
128
+ let entries;
129
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { continue; }
130
+ for (const e of entries) {
131
+ if (!e.isFile() || !e.name.endsWith('.md')) continue;
132
+ const full = path.join(dir, e.name);
133
+ let text;
134
+ try { text = fs.readFileSync(full, 'utf-8'); } catch { continue; }
135
+ const rel = path.posix.join(path.basename(dir), e.name).replace(/\\/g, '/');
136
+ // Heuristic: if any changed file path is mentioned in the doc body, treat as related.
137
+ for (const f of changedFiles) {
138
+ if (f && text.includes(f)) { out.add(rel); break; }
139
+ }
140
+ }
141
+ }
142
+ return [...out];
143
+ }
144
+
145
+ async function buildContextPack({ project, runId, trigger, commits = [], options = {} }) {
146
+ if (!project || !project.slug) throw new Error('project required');
147
+ const slug = project.slug;
148
+ if (!project.kbPath) throw new Error('project.kbPath is required');
149
+ const projectRoot = path.resolve(project.kbPath);
150
+ if (!isSafePath(projectRoot, '')) throw new Error('unsafe project root');
151
+
152
+ const gitPath = project.gitPath || project.localPath;
153
+ if (!gitPath) throw new Error('project has no git/local path');
154
+ const sourceRoot = path.resolve(gitPath); // source files live in the git repo, not the KB
155
+
156
+ const maxFiles = options.maxFiles || 80;
157
+ const entries = [];
158
+ const seenAbs = new Set();
159
+ const safeSeen = (rel) => entries.find(e => e.path === rel);
160
+
161
+ function addEntry(rel, kind, reason) {
162
+ if (entries.length >= maxFiles) return;
163
+ // Decide which root to read from: KB files are anchored at projectRoot,
164
+ // source files are anchored at sourceRoot.
165
+ const isKbKind = ['goal', 'analysis', 'feature-doc', 'module-doc'].includes(kind);
166
+ const root = isKbKind ? projectRoot : sourceRoot;
167
+ if (!isSafePath(root, rel)) return; // path traversal guard
168
+ if (safeSeen(rel)) return;
169
+ const abs = path.join(root, rel);
170
+ if (seenAbs.has(abs)) return;
171
+ if (!fs.existsSync(abs)) return;
172
+ let stat;
173
+ try { stat = fs.statSync(abs); } catch { return; }
174
+ if (!stat.isFile()) return;
175
+ const data = readSafeExcerpt(abs);
176
+ if (!data) return;
177
+ entries.push({
178
+ path: rel.replace(/\\/g, '/'),
179
+ kind,
180
+ reason,
181
+ size: data.size,
182
+ binary: data.binary,
183
+ truncated: !!data.truncated,
184
+ skipped: data.skipped || null,
185
+ excerpt: data.excerpt,
186
+ });
187
+ seenAbs.add(abs);
188
+ }
189
+
190
+ // 1. project-goal.md (highest-priority context)
191
+ if (fs.existsSync(path.join(projectRoot, 'project-goal.md'))) {
192
+ addEntry('project-goal.md', 'goal', 'highest-priority human-controlled truth');
193
+ }
194
+
195
+ // 2. project-analysis.md
196
+ if (fs.existsSync(path.join(projectRoot, 'project-analysis.md'))) {
197
+ addEntry('project-analysis.md', 'analysis', 'current AI-generated description');
198
+ }
199
+
200
+ let changedFiles = [];
201
+ let range = null;
202
+ let diffStat = null;
203
+
204
+ if (trigger === 'commits' && commits.length > 0) {
205
+ // 3a. Collect every file touched by any of the commits (use git show --name-only).
206
+ // This is safer than `diff first^..last` because the first commit has no parent.
207
+ const seen = new Set();
208
+ for (const c of commits) {
209
+ const r = await execGit(gitPath, ['show', '--name-only', '--format=', c.hash]);
210
+ if (r.ok) {
211
+ for (const line of (r.stdout || '').split('\n')) {
212
+ const f = line.trim();
213
+ if (f) seen.add(f);
214
+ }
215
+ }
216
+ }
217
+ changedFiles = [...seen];
218
+ range = `${commits[0].hash.slice(0, 7)}..${commits[commits.length - 1].hash.slice(0, 7)} (${commits.length} commit${commits.length === 1 ? '' : 's'})`;
219
+ // shortstat across the union of commits
220
+ const stat = await execGit(gitPath, ['diff', '--shortstat', `${commits[0].hash}^`, commits[commits.length - 1].hash]);
221
+ if (stat.ok) diffStat = (stat.stdout || '').trim();
222
+ } else {
223
+ // initial: enumerate tracked files (top N by size is overkill — just take first maxFiles)
224
+ const ls = await execGit(gitPath, ['ls-files']);
225
+ if (ls.ok) {
226
+ changedFiles = (ls.stdout || '').split('\n').map(s => s.trim()).filter(Boolean);
227
+ }
228
+ range = 'all-tracked-files';
229
+ }
230
+
231
+ // 3b. Changed files (as evidence)
232
+ for (const f of changedFiles) addEntry(f, 'git-changed', trigger === 'commits' ? `changed in ${range}` : 'tracked file');
233
+
234
+ // 4. Package / config files (look in source root)
235
+ for (const f of PACKAGE_CONFIG_FILES) {
236
+ if (fs.existsSync(path.join(sourceRoot, f))) addEntry(f, 'package-config', 'package or build configuration');
237
+ }
238
+
239
+ // 5. Neighbouring source files (in source root)
240
+ for (const f of getNeighborPaths(sourceRoot, changedFiles)) {
241
+ addEntry(f, 'neighbor', 'sibling of a changed file');
242
+ }
243
+
244
+ // 6. Tests near changed files (in source root)
245
+ for (const f of getTestPaths(sourceRoot, changedFiles)) {
246
+ addEntry(f, 'test-nearby', 'test for a changed file');
247
+ }
248
+
249
+ // 7. Related feature / module docs (look in KB root)
250
+ for (const f of findRelatedDocs(projectRoot, changedFiles)) {
251
+ addEntry(f, 'feature-doc', 'mentions a changed file');
252
+ }
253
+
254
+ const pack = {
255
+ schema: 'context-pack/v1',
256
+ runId: runId || shortHash(`${slug}:${Date.now()}:${Math.random()}`),
257
+ project: slug,
258
+ createdAt: new Date().toISOString(),
259
+ trigger: trigger || 'initial',
260
+ gitPath,
261
+ range,
262
+ diffStat,
263
+ commitCount: commits.length,
264
+ commits: commits.map(c => ({ hash: c.hash, short: c.short, subject: c.subject, date: c.date, author: c.author })),
265
+ entries,
266
+ limits: {
267
+ maxFiles,
268
+ maxFileBytes: MAX_FILE_BYTES,
269
+ maxExcerptBytes: MAX_EXCERPT_BYTES,
270
+ },
271
+ };
272
+
273
+ // 8. Write to disk
274
+ const outDir = path.join(projectRoot, '_ai', 'context-packs', pack.runId);
275
+ if (!isSafePath(projectRoot, path.relative(projectRoot, outDir))) {
276
+ throw new Error('unsafe output path');
277
+ }
278
+ fs.mkdirSync(outDir, { recursive: true });
279
+ fs.writeFileSync(path.join(outDir, 'context-pack.json'), JSON.stringify(pack, null, 2), 'utf-8');
280
+
281
+ return pack;
282
+ }
283
+
284
+ module.exports = {
285
+ buildContextPack,
286
+ isSafePath,
287
+ PACKAGE_CONFIG_FILES,
288
+ MAX_FILE_BYTES,
289
+ MAX_EXCERPT_BYTES,
290
+ };
@@ -0,0 +1,219 @@
1
+ // Draft Apply (TASK-009)
2
+ //
3
+ // Validates a draft, optionally creates backups of any file it would overwrite,
4
+ // writes the new content into the formal KB, updates kb-manifest.json, and (for
5
+ // commit-analysis runs) advances lastAnalyzedCommit to the head commit captured
6
+ // at run time.
7
+ //
8
+ // Hard rules:
9
+ // * `project-goal.md` is only written when the caller passes `allowGoalEdit: true`.
10
+ // Any other call that would touch the goal returns 409.
11
+ // * All file writes are best-effort transactional: we write to a backup
12
+ // directory first, then swap into place. If a partial failure occurs, the
13
+ // run is marked `applyStatus: failed` and no KB file is left in a half-written state.
14
+
15
+ const fs = require('fs');
16
+ const path = require('path');
17
+
18
+ const TRUSTED_GOAL_REL = 'project-goal.md';
19
+
20
+ function readManifest(kbPath) {
21
+ const p = path.join(kbPath, 'kb-manifest.json');
22
+ if (!fs.existsSync(p)) return null;
23
+ try { return JSON.parse(fs.readFileSync(p, 'utf-8')); } catch { return null; }
24
+ }
25
+
26
+ function writeManifest(kbPath, manifest) {
27
+ fs.writeFileSync(path.join(kbPath, 'kb-manifest.json'), JSON.stringify(manifest, null, 2) + '\n', 'utf-8');
28
+ }
29
+
30
+ function isSafeApplyPath(kbPath, rel) {
31
+ if (typeof rel !== 'string' || rel.length === 0) return false;
32
+ if (path.isAbsolute(rel)) return false;
33
+ const resolved = path.resolve(kbPath, rel);
34
+ const root = path.resolve(kbPath);
35
+ // Reject anything that resolves outside the KB root, even with sibling-prefix bypass
36
+ // (e.g. "/kb-other" passes a naive "startsWith(/kb)" check).
37
+ const relPath = path.relative(root, resolved);
38
+ if (relPath.startsWith('..') || path.isAbsolute(relPath)) return false;
39
+ // Forbid writes to internal AI areas (we never want an apply to write to _ai/ from a draft)
40
+ const norm = rel.replace(/\\/g, '/');
41
+ if (norm.startsWith('_ai/') || norm.split('/').includes('_ai')) return false;
42
+ return true;
43
+ }
44
+
45
+ function listDraftFiles(kbPath, runId) {
46
+ const dir = path.join(kbPath, '_ai', 'drafts', runId);
47
+ if (!fs.existsSync(dir)) return [];
48
+ const out = [];
49
+ const walk = (d) => {
50
+ for (const e of fs.readdirSync(d, { withFileTypes: true })) {
51
+ const full = path.join(d, e.name);
52
+ if (e.isDirectory()) walk(full);
53
+ else out.push({ path: path.relative(dir, full).replace(/\\/g, '/'), full });
54
+ }
55
+ };
56
+ walk(dir);
57
+ return out;
58
+ }
59
+
60
+ function validateDraftSchema(draft) {
61
+ // A draft is a single file produced by the orchestrator. It must:
62
+ // * have a non-empty path (relative to KB root, no leading slash)
63
+ // * have non-empty text content
64
+ // * be a known safe extension (.md, .json)
65
+ if (!draft || typeof draft !== 'object') return { valid: false, errors: ['draft must be an object'] };
66
+ if (!draft.path || typeof draft.path !== 'string') return { valid: false, errors: ['draft.path required'] };
67
+ if (draft.path.startsWith('/') || draft.path.startsWith('\\')) return { valid: false, errors: ['draft.path must be relative'] };
68
+ const ext = path.extname(draft.path).toLowerCase();
69
+ if (ext !== '.md' && ext !== '.json') return { valid: false, errors: ['draft.path must be .md or .json'] };
70
+ if (typeof draft.content !== 'string' || draft.content.length === 0) return { valid: false, errors: ['draft.content required'] };
71
+ return { valid: true };
72
+ }
73
+
74
+ function backupExisting(kbPath, backupsDir, rel) {
75
+ const src = path.join(kbPath, rel);
76
+ if (!fs.existsSync(src)) return null;
77
+ const dest = path.join(backupsDir, rel);
78
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
79
+ fs.copyFileSync(src, dest);
80
+ return dest;
81
+ }
82
+
83
+ function applyDrafts({ kbPath, slug, runId, drafts, allowGoalEdit, headCommitAtRun }) {
84
+ if (!kbPath || !slug || !runId) return { ok: false, status: 400, error: 'kbPath, slug, runId required' };
85
+ if (!Array.isArray(drafts) || drafts.length === 0) return { ok: false, status: 400, error: 'drafts must be a non-empty array' };
86
+
87
+ // 1. Validate every draft before touching anything
88
+ const errors = [];
89
+ const prepared = [];
90
+ for (const d of drafts) {
91
+ const v = validateDraftSchema(d);
92
+ if (!v.valid) { errors.push({ draft: d && d.path, errors: v.errors }); continue; }
93
+ if (!isSafeApplyPath(kbPath, d.path)) { errors.push({ draft: d.path, errors: ['unsafe path'] }); continue; }
94
+ if (d.path === TRUSTED_GOAL_REL && !allowGoalEdit) {
95
+ errors.push({ draft: d.path, errors: ['refusing to overwrite project-goal.md without allowGoalEdit=true'] });
96
+ continue;
97
+ }
98
+ prepared.push(d);
99
+ }
100
+ if (errors.length) return { ok: false, status: 422, error: 'invalid drafts', errors };
101
+ if (prepared.length === 0) return { ok: false, status: 400, error: 'no valid drafts after validation' };
102
+
103
+ // 2. Backup everything we will overwrite, into _ai/backups/<runId>/
104
+ const backupsDir = path.join(kbPath, '_ai', 'backups', runId);
105
+ fs.mkdirSync(backupsDir, { recursive: true });
106
+ const backups = [];
107
+ for (const d of prepared) {
108
+ const b = backupExisting(kbPath, backupsDir, d.path);
109
+ if (b) backups.push({ path: d.path, backup: path.relative(kbPath, b).replace(/\\/g, '/') });
110
+ }
111
+
112
+ // 3. Write each draft. Track failures for rollback.
113
+ const writes = [];
114
+ const failed = [];
115
+ for (const d of prepared) {
116
+ const dest = path.join(kbPath, d.path);
117
+ try {
118
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
119
+ fs.writeFileSync(dest, d.content, 'utf-8');
120
+ writes.push(d.path);
121
+ } catch (e) {
122
+ failed.push({ path: d.path, error: e.message });
123
+ }
124
+ }
125
+ if (failed.length) {
126
+ // Roll back the writes that did succeed
127
+ for (const rel of writes) {
128
+ const dest = path.join(kbPath, rel);
129
+ const backup = path.join(backupsDir, rel);
130
+ if (fs.existsSync(backup)) {
131
+ try { fs.copyFileSync(backup, dest); } catch {}
132
+ } else {
133
+ try { fs.rmSync(dest, { force: true }); } catch {}
134
+ }
135
+ }
136
+ return { ok: false, status: 500, error: 'partial write failure, rolled back', failed, backups };
137
+ }
138
+
139
+ // 4. Update manifest
140
+ const manifest = readManifest(kbPath) || { schema: 'kb-manifest/v1', project: slug, trustedKnowledge: [], draftAreas: [] };
141
+ const today = new Date().toISOString().slice(0, 10);
142
+ if (!manifest.trustedKnowledge.includes('README.md')) manifest.trustedKnowledge.push('README.md');
143
+ for (const d of prepared) {
144
+ if (d.path === TRUSTED_GOAL_REL) {
145
+ manifest.goal = { path: d.path, status: 'accepted', updatedAt: today };
146
+ } else if (d.path === 'project-analysis.md') {
147
+ manifest.analysis = manifest.analysis || {};
148
+ manifest.analysis.path = d.path;
149
+ manifest.analysis.updatedAt = today;
150
+ if (headCommitAtRun) manifest.analysis.lastAnalyzedCommit = headCommitAtRun;
151
+ } else if (d.path.startsWith('features/')) {
152
+ if (!manifest.trustedKnowledge.includes('features/')) manifest.trustedKnowledge.push('features/');
153
+ } else if (d.path.startsWith('modules/')) {
154
+ if (!manifest.trustedKnowledge.includes('modules/')) manifest.trustedKnowledge.push('modules/');
155
+ } else if (d.path.startsWith('changes/')) {
156
+ if (!manifest.trustedKnowledge.includes('changes/')) manifest.trustedKnowledge.push('changes/');
157
+ } else if (d.path.startsWith('architecture/')) {
158
+ if (!manifest.trustedKnowledge.includes('architecture/')) manifest.trustedKnowledge.push('architecture/');
159
+ }
160
+ }
161
+ writeManifest(kbPath, manifest);
162
+
163
+ // 5. Mark the run as applied (caller is responsible for advancing
164
+ // lastAnalyzedCommit since they know the commit-batch size).
165
+ const runsDir = path.join(kbPath, '_ai', 'runs');
166
+ const runPath = path.join(runsDir, `${runId}.json`);
167
+ if (fs.existsSync(runPath)) {
168
+ try {
169
+ const run = JSON.parse(fs.readFileSync(runPath, 'utf-8'));
170
+ run.applyStatus = 'applied';
171
+ run.appliedAt = new Date().toISOString();
172
+ run.appliedPaths = writes;
173
+ run.backups = backups.map(b => b.backup);
174
+ if (headCommitAtRun) run.advancedLastAnalyzedCommit = headCommitAtRun;
175
+ fs.writeFileSync(runPath, JSON.stringify(run, null, 2), 'utf-8');
176
+ } catch {}
177
+ }
178
+
179
+ return { ok: true, applied: writes, backups, manifest };
180
+ }
181
+
182
+ function rejectDrafts({ kbPath, runId, reason }) {
183
+ if (!kbPath || !runId) return { ok: false, status: 400, error: 'kbPath and runId required' };
184
+ const runsDir = path.join(kbPath, '_ai', 'runs');
185
+ const runPath = path.join(runsDir, `${runId}.json`);
186
+ if (!fs.existsSync(runPath)) return { ok: false, status: 404, error: 'run not found' };
187
+ const run = JSON.parse(fs.readFileSync(runPath, 'utf-8'));
188
+ run.applyStatus = 'rejected';
189
+ run.rejectedAt = new Date().toISOString();
190
+ run.rejectionReason = reason || null;
191
+ fs.writeFileSync(runPath, JSON.stringify(run, null, 2), 'utf-8');
192
+ return { ok: true, run };
193
+ }
194
+
195
+ function readDraftContent(kbPath, runId, rel) {
196
+ if (typeof rel !== 'string' || rel.length === 0) return null;
197
+ if (path.isAbsolute(rel)) return null;
198
+ const norm = rel.replace(/\\/g, '/');
199
+ if (norm.startsWith('../') || norm.includes('/../') || norm.endsWith('/..')) return null;
200
+ const dir = path.join(kbPath, '_ai', 'drafts', runId);
201
+ const target = path.join(dir, rel);
202
+ // Reject if the resolved target escapes the drafts dir
203
+ const relPath = path.relative(dir, target);
204
+ if (relPath.startsWith('..') || path.isAbsolute(relPath)) return null;
205
+ if (!fs.existsSync(target)) return null;
206
+ return fs.readFileSync(target, 'utf-8');
207
+ }
208
+
209
+ module.exports = {
210
+ applyDrafts,
211
+ rejectDrafts,
212
+ validateDraftSchema,
213
+ listDraftFiles,
214
+ readDraftContent,
215
+ readManifest,
216
+ writeManifest,
217
+ isSafeApplyPath,
218
+ TRUSTED_GOAL_REL,
219
+ };
@@ -0,0 +1,26 @@
1
+ // Shared git runner used by server.js, context-pack-builder.js, and tests.
2
+ // Wraps spawn() with a hard timeout and returns { ok, code, stdout, stderr, error }.
3
+
4
+ const { spawn } = require('child_process');
5
+ const fs = require('fs');
6
+
7
+ function execGit(cwd, args, timeoutMs = 8000) {
8
+ return new Promise((resolve) => {
9
+ if (!cwd || !fs.existsSync(cwd)) {
10
+ return resolve({ ok: false, code: -1, stdout: '', stderr: 'missing path', error: 'missing path' });
11
+ }
12
+ const child = spawn('git', args, { cwd, windowsHide: true, timeout: timeoutMs });
13
+ let out = '', err = '';
14
+ child.stdout.on('data', d => out += d.toString('utf-8'));
15
+ child.stderr.on('data', d => err += d.toString('utf-8'));
16
+ let timedOut = false;
17
+ const killer = setTimeout(() => { timedOut = true; child.kill(); }, timeoutMs);
18
+ child.on('error', e => { clearTimeout(killer); resolve({ ok: false, code: -1, stdout: out, stderr: err, error: e.message }); });
19
+ child.on('close', code => {
20
+ clearTimeout(killer);
21
+ resolve({ ok: code === 0 && !timedOut, code, stdout: out, stderr: err, error: timedOut ? 'timeout' : null });
22
+ });
23
+ });
24
+ }
25
+
26
+ module.exports = { execGit };
@@ -0,0 +1,148 @@
1
+ // _site/lib/hook-manager.js
2
+ //
3
+ // Install/uninstall a post-commit hook in a project's git repo so that
4
+ // commits automatically trigger a `safe` KB run. The hook is a tiny shim
5
+ // that calls `node <siteRoot>/scripts/hook-trigger.js` with the project
6
+ // repo path.
7
+ //
8
+ // Safety contract:
9
+ // * The hook is installed as `<repo>/.git/hooks/post-commit`.
10
+ // * It is a real, standalone script — it does NOT depend on the KB
11
+ // server being up to commit; it always exits 0.
12
+ // * The script points at the absolute path of the KB site scripts dir,
13
+ // so moving either the repo or the KB root is safe only when both
14
+ // are reinstalled.
15
+ // * If `<repo>/.git/hooks/post-commit` already exists, we refuse
16
+ // unless the caller passes `overwrite: true`. This protects any
17
+ // pre-existing hook the user might rely on.
18
+
19
+ const fs = require('fs');
20
+ const path = require('path');
21
+ const os = require('os');
22
+
23
+ const HOOK_NAME = 'post-commit';
24
+ const HOOK_MARKER = '# KB-HOOK-MANAGED';
25
+
26
+ function isWindows() { return process.platform === 'win32'; }
27
+
28
+ function repoGitDir(repoPath) {
29
+ // We expect repoPath to be the work-tree root, but accept either.
30
+ const direct = path.join(repoPath, '.git');
31
+ if (fs.existsSync(direct)) {
32
+ const stat = fs.statSync(direct);
33
+ if (stat.isDirectory()) {
34
+ // Normal repo: hooks live in <repo>/.git/hooks
35
+ return direct;
36
+ }
37
+ // Submodule / linked work-tree: .git is a file containing "gitdir: ..."
38
+ if (stat.isFile()) {
39
+ const text = fs.readFileSync(direct, 'utf-8').trim();
40
+ const m = /^gitdir:\s*(.+)$/m.exec(text);
41
+ if (m) return path.resolve(repoPath, m[1].trim());
42
+ }
43
+ }
44
+ return null;
45
+ }
46
+
47
+ function gitHooksDir(gitDir) {
48
+ // Git's hooks directory is `<gitDir>/hooks`, unless core.hooksPath is set.
49
+ // We respect the env var `GIT_HOOKS_PATH` and `core.hooksPath` by reading
50
+ // the repo's config; otherwise default to the conventional location.
51
+ if (!gitDir) return null;
52
+ const env = process.env.GIT_HOOKS_PATH;
53
+ if (env) return env;
54
+ // Try to read the worktree's config: search upward for the git toplevel.
55
+ // We don't need to do a full `git rev-parse` — we trust that the standard
56
+ // hooks dir exists.
57
+ return path.join(gitDir, 'hooks');
58
+ }
59
+
60
+ function buildHookBody({ siteRoot, host, port }) {
61
+ // The script is a plain POSIX shell that defers to Node. This works in
62
+ // Git for Windows (git-bash) and on macOS / Linux.
63
+ const trigger = path.join(siteRoot, 'scripts', 'hook-trigger.js');
64
+ // Use forward slashes inside the shim for portability; Windows git-bash
65
+ // accepts them. Quote with single-quotes for paths that contain spaces.
66
+ const triggerPosix = trigger.replace(/\\/g, '/');
67
+ const repoPath = '$REPO_PATH_PLACEHOLDER';
68
+ const hostLine = host ? `--host ${host}` : '';
69
+ const portLine = Number.isFinite(port) ? `--port ${port}` : '';
70
+ return `#!/bin/sh
71
+ ${HOOK_MARKER} v1 — auto-installed by KB manager
72
+ # This hook is generated. Editing the marker line will break updates.
73
+ # To remove: 'git hooks uninstall' from the KB manager, or delete this file.
74
+ set -e
75
+ REPO_ROOT="$(git rev-parse --show-toplevel)"
76
+ node '${triggerPosix}' \\
77
+ --kb-root '${siteRoot.replace(/\\/g, '/')}' \\
78
+ --repo "${repoPath}" \\
79
+ ${hostLine} ${portLine} &
80
+ HOOK_PID=$!
81
+ # Wait up to 4 seconds for the trigger to dispatch; otherwise let it run free.
82
+ ( sleep 4 && kill -0 "$HOOK_PID" 2>/dev/null && kill "$HOOK_PID" 2>/dev/null ) &
83
+ WATCHER=$!
84
+ wait "$HOOK_PID" 2>/dev/null || true
85
+ kill "$WATCHER" 2>/dev/null || true
86
+ exit 0
87
+ `;
88
+ }
89
+
90
+ function installHook({ repoPath, siteRoot, host, port, overwrite = false }) {
91
+ if (!repoPath) return { ok: false, status: 400, error: 'repoPath required' };
92
+ if (!siteRoot) return { ok: false, status: 400, error: 'siteRoot required' };
93
+ const abs = path.resolve(repoPath);
94
+ const gitDir = repoGitDir(abs);
95
+ if (!gitDir) return { ok: false, status: 400, error: `no .git directory under ${abs}` };
96
+ const hooksDir = gitHooksDir(gitDir);
97
+ if (!fs.existsSync(hooksDir)) fs.mkdirSync(hooksDir, { recursive: true });
98
+ const hookPath = path.join(hooksDir, HOOK_NAME);
99
+ if (fs.existsSync(hookPath) && !overwrite) {
100
+ const existing = fs.readFileSync(hookPath, 'utf-8');
101
+ if (!existing.includes(HOOK_MARKER)) {
102
+ return { ok: false, status: 409, error: `${HOOK_NAME} already exists and is not KB-managed. Pass overwrite:true to replace.` };
103
+ }
104
+ }
105
+ let body = buildHookBody({ siteRoot, host, port });
106
+ body = body.replace('$REPO_PATH_PLACEHOLDER', abs.replace(/\\/g, '/'));
107
+ fs.writeFileSync(hookPath, body, { mode: 0o755 });
108
+ return { ok: true, hookPath, repoPath: abs };
109
+ }
110
+
111
+ function uninstallHook({ repoPath }) {
112
+ if (!repoPath) return { ok: false, status: 400, error: 'repoPath required' };
113
+ const abs = path.resolve(repoPath);
114
+ const gitDir = repoGitDir(abs);
115
+ if (!gitDir) return { ok: false, status: 400, error: `no .git directory under ${abs}` };
116
+ const hooksDir = gitHooksDir(gitDir);
117
+ const hookPath = path.join(hooksDir, HOOK_NAME);
118
+ if (!fs.existsSync(hookPath)) return { ok: true, removed: false, hookPath };
119
+ const text = fs.readFileSync(hookPath, 'utf-8');
120
+ if (!text.includes(HOOK_MARKER)) {
121
+ return { ok: false, status: 409, error: `${HOOK_NAME} is not KB-managed; refusing to delete. Remove it manually.` };
122
+ }
123
+ fs.unlinkSync(hookPath);
124
+ return { ok: true, removed: true, hookPath };
125
+ }
126
+
127
+ function readHookStatus({ repoPath }) {
128
+ if (!repoPath) return { ok: false, status: 400, error: 'repoPath required' };
129
+ const abs = path.resolve(repoPath);
130
+ const gitDir = repoGitDir(abs);
131
+ if (!gitDir) return { ok: true, installed: false, repoPath: abs, reason: 'no .git directory' };
132
+ const hooksDir = gitHooksDir(gitDir);
133
+ const hookPath = path.join(hooksDir, HOOK_NAME);
134
+ if (!fs.existsSync(hookPath)) return { ok: true, installed: false, hookPath, repoPath: abs };
135
+ const text = fs.readFileSync(hookPath, 'utf-8');
136
+ if (!text.includes(HOOK_MARKER)) {
137
+ return { ok: true, installed: false, kbManaged: false, hookPath, repoPath: abs, reason: 'pre-existing hook (not KB-managed)' };
138
+ }
139
+ return { ok: true, installed: true, kbManaged: true, hookPath, repoPath: abs, bytes: text.length };
140
+ }
141
+
142
+ module.exports = {
143
+ HOOK_NAME,
144
+ HOOK_MARKER,
145
+ installHook,
146
+ uninstallHook,
147
+ readHookStatus,
148
+ };