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,26 @@
1
+ @echo off
2
+ setlocal
3
+
4
+ REM KB Management Site launcher — double-click to start
5
+ set KB_SITE=%~dp0
6
+ set PORT=7777
7
+
8
+ REM Check if already running
9
+ netstat -ano | findstr ":%PORT% " | findstr "LISTENING" >nul 2>&1
10
+ if %ERRORLEVEL%==0 (
11
+ echo [kb-site] Already running on port %PORT%. Opening browser...
12
+ start "" "http://localhost:%PORT%/"
13
+ goto :eof
14
+ )
15
+
16
+ echo [kb-site] Starting server on port %PORT%...
17
+ start "KB-Site" /min cmd /c "cd /d "%KB_SITE%" && node server.js"
18
+ timeout /t 2 /nobreak >nul
19
+
20
+ REM Open browser
21
+ start "" "http://localhost:%PORT%/"
22
+
23
+ echo.
24
+ echo [kb-site] Server started. To stop: close the "KB-Site" window, or run stop.bat
25
+ echo.
26
+ pause
package/_site/stop.bat ADDED
@@ -0,0 +1,11 @@
1
+ @echo off
2
+ setlocal
3
+ set PORT=7777
4
+
5
+ echo [kb-site] Stopping server on port %PORT%...
6
+ for /f "tokens=5" %%P in ('netstat -ano ^| findstr ":%PORT% " ^| findstr "LISTENING"') do (
7
+ taskkill /F /PID %%P >nul 2>&1
8
+ echo [kb-site] Killed PID %%P
9
+ )
10
+ echo Done.
11
+ pause
@@ -0,0 +1,18 @@
1
+ {
2
+ "schema": "ai-profiles/v1",
3
+ "defaultProfileId": "mock-agent",
4
+ "profiles": [
5
+ {
6
+ "id": "mock-agent",
7
+ "name": "Mock",
8
+ "enabled": true,
9
+ "implementation": "mock-agent"
10
+ },
11
+ {
12
+ "id": "claude-code-agent",
13
+ "name": "Claude Code",
14
+ "enabled": false,
15
+ "implementation": "claude-code-agent"
16
+ }
17
+ ]
18
+ }
@@ -0,0 +1,395 @@
1
+ # AI Project Knowledge Base System Design
2
+
3
+ Status: draft for implementation planning
4
+ Date: 2026-06-11
5
+ Owner: SanQian.Xu
6
+ Project goal source: `docs/project-goal.md`
7
+
8
+ ## 1. Purpose
9
+
10
+ This project will evolve from a local knowledge-base management site into an AI-assisted project supervision system driven by Git history.
11
+
12
+ The system must import selected local projects, verify that they are Git repositories, understand project goals, analyze the current implementation, watch new commits, and keep the project's knowledge base accurate enough for a future PR-review project to use as its source of truth.
13
+
14
+ The central product requirement is:
15
+
16
+ - A project goal is the highest-priority human-controlled truth.
17
+ - Project analysis is an AI-generated explanation of the current implementation.
18
+ - Git commit analysis must explain code changes, affected features/modules, project-wide impact, and relationship to the project goal.
19
+ - AI output must be testable, reviewable, and evidence-backed before it becomes trusted knowledge.
20
+
21
+ ## 2. Current State
22
+
23
+ The repository currently contains:
24
+
25
+ - A local management site under `_site/`.
26
+ - Project registry data in `projects.json`.
27
+ - A PowerShell generator at `scripts/gen-commit-doc.ps1`.
28
+ - Project knowledge folders under `projects/<slug>/`.
29
+
30
+ The current generator reads Git logs and creates or appends Markdown files mainly from commit subjects. It does not yet deeply inspect diffs, read related source files, run an AI analysis loop, maintain a project goal, or produce draft review workflows.
31
+
32
+ ## 3. Design Principles
33
+
34
+ 1. Project goal first.
35
+ `project-goal.md` is the authority. AI may propose edits, but must not overwrite the accepted goal automatically.
36
+
37
+ 2. Current implementation is not the same as the goal.
38
+ `project-analysis.md` describes what the code currently does. It may change often. It is evidence, not intent.
39
+
40
+ 3. Draft before apply.
41
+ AI-generated changes are written to `_ai/drafts/` first. Formal knowledge files are updated only after an apply step.
42
+
43
+ 4. Evidence required.
44
+ AI claims must include supporting files, commits, or command outputs. Future PR review must be able to trace a conclusion back to evidence.
45
+
46
+ 5. Machine-readable plus human-readable.
47
+ Markdown is for people. `kb-manifest.json`, run records, and dependency maps are for automation.
48
+
49
+ 6. Tests define completion.
50
+ A task is not complete until its test plan passes. Failed tests send the work back to implementation.
51
+
52
+ ## 4. Target Knowledge Base Layout
53
+
54
+ Each imported project should use this layout:
55
+
56
+ ```text
57
+ projects/<slug>/
58
+ ├─ README.md
59
+ ├─ project-goal.md
60
+ ├─ project-analysis.md
61
+ ├─ kb-manifest.json
62
+ ├─ architecture/
63
+ │ ├─ overview.md
64
+ │ ├─ data-flow.md
65
+ │ └─ decisions/
66
+ ├─ requirements/
67
+ │ ├─ roadmap.md
68
+ │ ├─ constraints.md
69
+ │ └─ success-criteria.md
70
+ ├─ features/
71
+ │ ├─ 00-index.md
72
+ │ └─ <feature-slug>.md
73
+ ├─ modules/
74
+ │ ├─ 00-index.md
75
+ │ └─ <module-slug>.md
76
+ ├─ changes/
77
+ │ ├─ 00-index.md
78
+ │ └─ <commit-hash>.md
79
+ ├─ quality/
80
+ │ ├─ review-rules.md
81
+ │ ├─ test-strategy.md
82
+ │ └─ risk-register.md
83
+ ├─ references/
84
+ │ ├─ source-map.md
85
+ │ └─ dependency-map.json
86
+ └─ _ai/
87
+ ├─ drafts/
88
+ ├─ runs/
89
+ └─ context-packs/
90
+ ```
91
+
92
+ ### 4.1 `project-goal.md`
93
+
94
+ This file captures the true goal of the project. It is the future PR-review project's highest-priority input.
95
+
96
+ Required sections:
97
+
98
+ - One-sentence goal
99
+ - Target users
100
+ - Core scenarios
101
+ - Success criteria
102
+ - Non-goals
103
+ - Priority principles
104
+ - PR review principles
105
+ - Human revision history
106
+
107
+ Suggested frontmatter:
108
+
109
+ ```yaml
110
+ schema: project-goal/v1
111
+ project: <slug>
112
+ status: accepted
113
+ source: human-reviewed
114
+ updatedAt: <YYYY-MM-DD>
115
+ lastReviewedBy: SanQian.Xu
116
+ ```
117
+
118
+ ### 4.2 `project-analysis.md`
119
+
120
+ This file describes the current implementation and how it serves the project goal.
121
+
122
+ Required sections:
123
+
124
+ - AI-inferred project purpose
125
+ - Current implemented capabilities
126
+ - Current architecture
127
+ - Important modules
128
+ - Data/control flow
129
+ - How current implementation supports the project goal
130
+ - Gaps between implementation and project goal
131
+ - Evidence list
132
+
133
+ This file may be regenerated after full-project analysis.
134
+
135
+ ### 4.3 `features/`
136
+
137
+ Feature documents are long-lived knowledge. They should not be one file per commit. A feature document may be created by the first relevant change and then updated by later commits.
138
+
139
+ Each feature document must explain:
140
+
141
+ - What the feature does
142
+ - Why it exists
143
+ - Which project-goal scenario it supports
144
+ - Main user/system behavior
145
+ - Source files and modules involved
146
+ - Important commits
147
+ - Known limitations
148
+ - Tests or verification points
149
+
150
+ ### 4.4 `changes/`
151
+
152
+ Change documents are commit- or batch-level records. They answer:
153
+
154
+ - What changed?
155
+ - Which files changed?
156
+ - Which feature/module did the change affect?
157
+ - Is this a new feature, existing feature update, bug fix, refactor, or infrastructure change?
158
+ - What is the impact on the project goal?
159
+ - What evidence supports the analysis?
160
+
161
+ The existing `commits/` directory can remain for compatibility during migration, but new AI-driven change records should target `changes/`.
162
+
163
+ ### 4.5 `kb-manifest.json`
164
+
165
+ This file is the machine-readable contract for future tools.
166
+
167
+ Example shape:
168
+
169
+ ```json
170
+ {
171
+ "schema": "kb-manifest/v1",
172
+ "project": "example-project",
173
+ "goal": {
174
+ "path": "project-goal.md",
175
+ "status": "accepted",
176
+ "updatedAt": "2026-06-11"
177
+ },
178
+ "analysis": {
179
+ "path": "project-analysis.md",
180
+ "lastAnalyzedCommit": "abc1234",
181
+ "updatedAt": "2026-06-11"
182
+ },
183
+ "indexes": {
184
+ "features": "features/00-index.md",
185
+ "modules": "modules/00-index.md",
186
+ "changes": "changes/00-index.md",
187
+ "sourceMap": "references/source-map.md"
188
+ },
189
+ "trustedKnowledge": [
190
+ "project-goal.md",
191
+ "features/",
192
+ "modules/",
193
+ "architecture/"
194
+ ]
195
+ }
196
+ ```
197
+
198
+ ## 5. Project Registry Model
199
+
200
+ `projects.json` should be extended gradually. Target fields:
201
+
202
+ ```json
203
+ {
204
+ "displayName": "Example Project",
205
+ "localPath": "D:\\SanQian.Xu\\Example",
206
+ "gitPath": "D:\\SanQian.Xu\\Example",
207
+ "kbPath": "D:\\SanQian.Xu\\project-knowledge-base\\projects\\example",
208
+ "enabled": true,
209
+ "repoStatus": "ok",
210
+ "defaultBranch": "main",
211
+ "currentBranch": "main",
212
+ "remoteUrl": "https://...",
213
+ "headCommit": "abc1234",
214
+ "lastSeenCommit": "abc1234",
215
+ "lastAnalyzedCommit": "def5678",
216
+ "aiProfileId": "default-agent",
217
+ "kbSchemaVersion": "v2",
218
+ "goalStatus": "accepted"
219
+ }
220
+ ```
221
+
222
+ The import flow must validate:
223
+
224
+ - `localPath` exists.
225
+ - `gitPath` exists.
226
+ - `git -C <gitPath> rev-parse --is-inside-work-tree` succeeds.
227
+ - `git -C <gitPath> rev-parse HEAD` succeeds, or the UI clearly reports that the repository has no commits.
228
+ - The knowledge base directory is initialized with the v2 layout.
229
+
230
+ ## 6. Runtime Architecture
231
+
232
+ ```mermaid
233
+ flowchart LR
234
+ UI["Local Web UI"] --> API["Node Local API"]
235
+ API --> Registry["Project Registry"]
236
+ API --> Git["Git Inspector"]
237
+ API --> Orchestrator["Analysis Orchestrator"]
238
+ Orchestrator --> Context["Context Pack Builder"]
239
+ Orchestrator --> Agent["AI Agent Adapter"]
240
+ Agent --> Drafts["Draft Writer"]
241
+ Drafts --> Review["Review / Apply"]
242
+ Review --> KB["Knowledge Base Files"]
243
+ KB --> Manifest["kb-manifest.json"]
244
+ ```
245
+
246
+ ### 6.1 Local API
247
+
248
+ The current `_site/server.js` can stay as the local API host initially. As complexity grows, extract route handlers into modules.
249
+
250
+ New endpoint candidates:
251
+
252
+ - `POST /api/projects/:slug/validate-git`
253
+ - `POST /api/projects/:slug/analyze/initial`
254
+ - `POST /api/projects/:slug/analyze/commits`
255
+ - `GET /api/projects/:slug/goal`
256
+ - `PUT /api/projects/:slug/goal`
257
+ - `GET /api/projects/:slug/drafts`
258
+ - `POST /api/projects/:slug/drafts/:draftId/apply`
259
+ - `GET /api/projects/:slug/manifest`
260
+ - `GET /api/ai-profiles`
261
+ - `PUT /api/ai-profiles`
262
+
263
+ ### 6.2 Analysis Orchestrator
264
+
265
+ The orchestrator coordinates:
266
+
267
+ 1. Identify the project and Git range.
268
+ 2. Build context packs.
269
+ 3. Start the selected AI adapter.
270
+ 4. Validate model output against schema.
271
+ 5. Write draft documents.
272
+ 6. Record run metadata.
273
+ 7. Update project analysis state only after successful apply.
274
+
275
+ ### 6.3 Context Pack Builder
276
+
277
+ The system should automatically collect context. The user should not manually choose files.
278
+
279
+ Inputs:
280
+
281
+ - Git diff and stats
282
+ - Changed files
283
+ - Neighboring source files when needed
284
+ - Existing `project-goal.md`
285
+ - Existing feature/module/source-map docs
286
+ - Package/config files
287
+ - Test files near changed code
288
+
289
+ The context pack must record why each file was included.
290
+
291
+ ### 6.4 AI Agent Adapter
292
+
293
+ The default strategy should be agent-first for deep analysis:
294
+
295
+ - Use a Claude Code / agent-style analyzer for repository-wide reading, command execution, and cross-file reasoning.
296
+ - Use ordinary cloud model APIs for smaller structured tasks only when the context pack is already sufficient.
297
+ - Keep the adapter interface provider-neutral so different analyzers can be swapped later.
298
+
299
+ The adapter must return structured output that includes:
300
+
301
+ - Summary
302
+ - Classification
303
+ - Affected goals/features/modules
304
+ - Project-goal impact
305
+ - Evidence
306
+ - Proposed file operations
307
+ - Confidence
308
+ - Questions or human-review flags
309
+
310
+ ### 6.5 Draft Writer and Apply Step
311
+
312
+ AI output should first become draft files under:
313
+
314
+ ```text
315
+ projects/<slug>/_ai/drafts/<run-id>/
316
+ ```
317
+
318
+ Apply is a separate step that:
319
+
320
+ - Validates draft schema.
321
+ - Shows file changes.
322
+ - Creates backups or patch records.
323
+ - Writes formal knowledge files.
324
+ - Updates `kb-manifest.json`.
325
+ - Updates `lastAnalyzedCommit` only after all required writes pass.
326
+
327
+ ## 7. Main Workflows
328
+
329
+ ### 7.1 Import Project
330
+
331
+ ```mermaid
332
+ flowchart TD
333
+ A["User enters path"] --> B["Validate path"]
334
+ B --> C["Validate Git repository"]
335
+ C --> D["Create or update projects.json"]
336
+ D --> E["Initialize KB v2 layout"]
337
+ E --> F["Show repository status in UI"]
338
+ ```
339
+
340
+ ### 7.2 Initial Project Analysis
341
+
342
+ ```mermaid
343
+ flowchart TD
344
+ A["Start initial analysis"] --> B["Read repository structure"]
345
+ B --> C["Read README/config/package files"]
346
+ C --> D["Infer project goal draft"]
347
+ D --> E["Generate project-analysis draft"]
348
+ E --> F["Generate modules/features/source map drafts"]
349
+ F --> G["User reviews project-goal.md"]
350
+ G --> H["Apply accepted knowledge"]
351
+ ```
352
+
353
+ ### 7.3 Incremental Commit Analysis
354
+
355
+ ```mermaid
356
+ flowchart TD
357
+ A["Detect new commits"] --> B["Build commit diff context"]
358
+ B --> C["Read existing KB"]
359
+ C --> D["AI classifies change"]
360
+ D --> E{"New or existing feature?"}
361
+ E -->|New| F["Draft new feature doc"]
362
+ E -->|Existing| G["Draft feature/module update"]
363
+ D --> H["Draft change record"]
364
+ F --> I["Review/apply"]
365
+ G --> I
366
+ H --> I
367
+ ```
368
+
369
+ ## 8. Quality Gates
370
+
371
+ A task is not done unless:
372
+
373
+ - Required automated tests pass.
374
+ - Generated knowledge has evidence links.
375
+ - AI drafts are not applied directly without validation.
376
+ - `project-goal.md` is never overwritten automatically.
377
+ - `lastAnalyzedCommit` is updated only after successful knowledge write.
378
+ - Failures are visible in the UI or run records.
379
+
380
+ ## 9. Migration Strategy
381
+
382
+ Phase 1 should not delete existing `commits/` files. Instead:
383
+
384
+ 1. Add v2 layout to new or reinitialized projects.
385
+ 2. Keep old `commits/` readable.
386
+ 3. Write new AI-driven change records to `changes/`.
387
+ 4. Add `kb-manifest.json`.
388
+ 5. Add migration tests before changing existing project data in bulk.
389
+
390
+ ## 10. Open Decisions
391
+
392
+ 1. Which agent runtime will be used first: Claude Code CLI, Claude Agent SDK, or a local command wrapper?
393
+ 2. Should AI-generated project goal drafts require explicit user approval before any later analysis uses them as authoritative?
394
+ 3. How large can a context pack be before the analyzer must split work into multiple passes?
395
+ 4. Should the future PR-review project read only `kb-manifest.json` and accepted docs, or also read AI run history?
@@ -0,0 +1,198 @@
1
+ # PR Consumer Contract (TASK-011)
2
+
3
+ Status: stable
4
+ Date: 2026-06-11
5
+
6
+ This document is the contract that the future PR-review project (and any other
7
+ external tool) can rely on when reading the knowledge base. It is intentionally
8
+ narrow: if you can satisfy these rules, you can build a consumer without
9
+ negotiating with the KB site maintainers.
10
+
11
+ ## 1. Where the KB lives
12
+
13
+ For a project with slug `foo`:
14
+
15
+ ```
16
+ project-knowledge-base/
17
+ └ projects/
18
+ └ foo/ ← kbPath (also in projects.json)
19
+ ├ README.md ← required, seed entry
20
+ ├ framework.md ← optional, seed entry
21
+ ├ project-goal.md ← THE goal (only if accepted)
22
+ ├ project-analysis.md ← only if accepted
23
+ ├ kb-manifest.json ← THE contract
24
+ ├ architecture/ ← trusted dir (TASK-003 seed)
25
+ ├ modules/ ← trusted dir
26
+ ├ features/ ← trusted dir; one file per feature
27
+ ├ changes/ ← trusted dir; one file per change
28
+ ├ quality/ ← trusted dir
29
+ ├ requirements/ ← trusted dir
30
+ ├ references/ ← trusted dir
31
+ └ _ai/ ← NEVER trusted
32
+ ├ drafts/<runId>/... ← AI-generated drafts
33
+ ├ runs/<runId>.json ← AI run records
34
+ ├ backups/<runId>/... ← backups taken at apply time
35
+ └ context-packs/<runId>/context-pack.json
36
+ ```
37
+
38
+ The registry entry in `projects.json` carries `kbPath` (the absolute path
39
+ to the project folder). The KB root is `path.dirname(kbPath)`; the registry
40
+ file is at `<KB_ROOT>/projects.json`.
41
+
42
+ ## 2. The manifest
43
+
44
+ `kb-manifest.json` is the only file external tools MUST parse. Its schema is
45
+ `kb-manifest/v1`. The current shape:
46
+
47
+ ```json
48
+ {
49
+ "schema": "kb-manifest/v1",
50
+ "project": "foo",
51
+ "kbRoot": "projects/foo",
52
+ "generatedAt": "2026-06-11",
53
+ "goal": { "path": "project-goal.md", "status": "accepted", "updatedAt": "2026-06-11" },
54
+ "analysis": {
55
+ "path": "project-analysis.md",
56
+ "lastAnalyzedCommit": "abc1234…",
57
+ "updatedAt": "2026-06-11"
58
+ },
59
+ "indexes": {
60
+ "features": "features/00-index.md",
61
+ "modules": "modules/00-index.md",
62
+ "changes": "changes/00-index.md",
63
+ "sourceMap": "references/source-map.md"
64
+ },
65
+ "trustedKnowledge": [
66
+ "README.md", "framework.md",
67
+ "architecture/", "modules/", "features/", "changes/", "quality/",
68
+ "requirements/", "references/"
69
+ ],
70
+ "draftAreas": [
71
+ "_ai/drafts/", "_ai/runs/", "_ai/context-packs/", "_ai/backups/"
72
+ ]
73
+ }
74
+ ```
75
+
76
+ ### 2.1 Required fields for consumers
77
+
78
+ | Field | Type | Notes |
79
+ |-------|------|-------|
80
+ | `schema` | string | MUST be `kb-manifest/v1` |
81
+ | `project` | string | matches the slug in `projects.json` |
82
+ | `goal.path` | string\|null | relative to `kbPath`. `null` (or absent) means "no accepted goal yet" — the consumer must NOT invent one. |
83
+ | `goal.status` | string | `accepted` \| `not-created` \| `pending`. Consumers should treat anything other than `accepted` as "no goal". |
84
+ | `analysis.path` | string\|null | relative to `kbPath`. `null` means "no accepted analysis yet". |
85
+ | `analysis.lastAnalyzedCommit` | string\|null | the commit that produced the current accepted analysis. `null` if the project was never analyzed. |
86
+ | `trustedKnowledge` | string[] | relative paths. Each is either a file or a directory ending in `/`. The consumer may treat these as authoritative. |
87
+ | `draftAreas` | string[] | relative paths. The consumer MUST NOT read from these as authoritative content. |
88
+
89
+ ### 2.2 Knowledge priority order
90
+
91
+ When assembling a review context, read in this order so the human-aligned
92
+ "goal" is always the loudest voice:
93
+
94
+ 1. `project-goal.md` (if `goal.status === 'accepted'`)
95
+ 2. accepted feature docs under `features/`
96
+ 3. accepted module docs under `modules/`
97
+ 4. accepted architecture under `architecture/`
98
+ 5. accepted analysis under `project-analysis.md`
99
+ 6. accepted change history under `changes/`
100
+ 7. run history under `_ai/runs/` (audit only; never treated as authoritative)
101
+
102
+ Drafts under `_ai/drafts/` are NEVER part of the trusted story. They are a
103
+ queue waiting for human review at the Drafts tab.
104
+
105
+ ## 3. The apply gate
106
+
107
+ Nothing under `_ai/` becomes trusted by accident. The only way for a draft
108
+ to leave `_ai/drafts/` and land in the formal KB is the `applyDrafts` flow
109
+ in `_site/lib/draft-apply.js`, which:
110
+
111
+ 1. Refuses to overwrite `project-goal.md` without `allowGoalEdit: true`.
112
+ 2. Refuses to write anywhere under `_ai/`.
113
+ 3. Backs up any file it overwrites into `_ai/backups/<runId>/`.
114
+ 4. Updates the manifest in place.
115
+ 5. Marks the run record as `applyStatus: 'applied'`.
116
+
117
+ A consumer that wants to verify a path is "really trusted" should call
118
+ `POST /api/projects/<slug>/validate-kb` (server-side validation) or
119
+ run `_site/lib/kb-validator.js` directly.
120
+
121
+ ## 4. PR context pack
122
+
123
+ `GET /api/projects/<slug>/pr-context` returns a JSON pack conforming to
124
+ `pr-context-pack/v1`:
125
+
126
+ ```json
127
+ {
128
+ "schema": "pr-context-pack/v1",
129
+ "generatedAt": "2026-06-11T...",
130
+ "project": "foo",
131
+ "goal": { "path": "project-goal.md", "content": "…", "size": 1234 },
132
+ "analysis": { "path": "project-analysis.md", "content": "…", "size": 5678 },
133
+ "indexes": {
134
+ "features": { "path": "features/00-index.md", "content": "…", "size": 200 },
135
+ "modules": { "path": "modules/00-index.md", "content": "…", "size": 200 },
136
+ "changes": { "path": "changes/00-index.md", "content": "…", "size": 200 },
137
+ "sourceMap": { "path": "references/source-map.md", "content": "…", "size": 100 }
138
+ },
139
+ "trustedKnowledge": [
140
+ { "path": "README.md", "content": "…", "size": 80 },
141
+ { "path": "framework.md", "content": "…", "size": 200 }
142
+ ]
143
+ }
144
+ ```
145
+
146
+ The pack never includes anything under `_ai/`. The consumer can serialize
147
+ this directly into its own context window.
148
+
149
+ ## 5. Validation
150
+
151
+ A consumer can rely on the validator at `_site/lib/kb-validator.js`. It
152
+ exposes `validateKb(kbPath)` and `buildPrContextPack(kbPath)`. The server
153
+ exposes the same logic at `POST /api/projects/<slug>/validate-kb` and
154
+ `GET /api/projects/<slug>/pr-context`.
155
+
156
+ `validateKb` returns:
157
+
158
+ ```json
159
+ {
160
+ "ok": true,
161
+ "status": 200,
162
+ "info": {
163
+ "kbPath": "...",
164
+ "manifestExists": true,
165
+ "goalStatus": "accepted",
166
+ "analysisStatus": "present",
167
+ "trustedKnowledgeEntries": 9,
168
+ "draftAreasEntries": 4,
169
+ "aiSubdirsPresent": ["_ai/drafts/", "_ai/runs/", "_ai/context-packs/"]
170
+ },
171
+ "warnings": [],
172
+ "errors": []
173
+ }
174
+ ```
175
+
176
+ When `ok: false`, `errors` is a list of human-readable strings.
177
+
178
+ ## 6. Versioning
179
+
180
+ The `schema` field on both the manifest and the PR context pack is the
181
+ contract version. The server is currently on `v1` for both. If a breaking
182
+ change is needed, the server will move to `v2` and keep `v1` endpoints
183
+ working for at least one major release.
184
+
185
+ ## 7. Test fixtures
186
+
187
+ The PR consumer project can drop these fixtures into its own test suite:
188
+
189
+ - `_site/_test/fixtures/pr-context/healthy/` — a healthy v2 KB, should
190
+ validate clean and produce a PR context pack.
191
+ - `_site/_test/fixtures/pr-context/missing-goal/` — accepted analysis,
192
+ no goal. Should validate (the validator reports `goalStatus: 'not-created'`)
193
+ and the PR context pack's `goal` field is `null`.
194
+ - `_site/_test/fixtures/pr-context/draft-leak/` — manifest trusts a path
195
+ inside `_ai/`. The validator MUST refuse.
196
+
197
+ These fixtures are byte-for-byte reproducible: every file is checked in
198
+ and the test that consumes them just reads from disk.