@sanqianx/project-knowledge 4.7.0 → 4.8.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 (192) hide show
  1. package/.agents/plugins/marketplace.json +17 -17
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/CHANGELOG.md +817 -799
  4. package/README.md +48 -36
  5. package/_modules/knowledge-engine/README.md +88 -0
  6. package/_modules/knowledge-engine/package.json +38 -0
  7. package/_modules/knowledge-engine/src/analysis-contract.js +25 -0
  8. package/_modules/knowledge-engine/src/analysis-worker.js +54 -0
  9. package/_modules/knowledge-engine/src/bin.js +52 -0
  10. package/_modules/knowledge-engine/src/bridge-client.js +24 -0
  11. package/_modules/knowledge-engine/src/coordinator.js +255 -0
  12. package/_modules/knowledge-engine/src/headless-analyzer.js +75 -0
  13. package/_modules/knowledge-engine/src/memories.js +85 -0
  14. package/_modules/knowledge-engine/src/migration.js +88 -0
  15. package/_modules/knowledge-engine/src/retrieval.js +116 -0
  16. package/_modules/knowledge-engine/src/runtime/README.md +7 -0
  17. package/_modules/knowledge-engine/src/runtime/atomic-file.js +204 -0
  18. package/_modules/knowledge-engine/src/runtime/automation-config.js +140 -0
  19. package/_modules/knowledge-engine/src/runtime/commit-conversation-binder.js +249 -0
  20. package/_modules/knowledge-engine/src/runtime/commit-processing-ledger.js +26 -0
  21. package/_modules/knowledge-engine/src/runtime/commit-prompt.js +202 -0
  22. package/_modules/knowledge-engine/src/runtime/commit-reconciler.js +432 -0
  23. package/_modules/knowledge-engine/src/runtime/content-hash.js +4 -0
  24. package/_modules/knowledge-engine/src/runtime/contracts.js +279 -0
  25. package/_modules/knowledge-engine/src/runtime/conversation-exclusions.js +204 -0
  26. package/_modules/knowledge-engine/src/runtime/conversation-store.js +387 -0
  27. package/_modules/knowledge-engine/src/runtime/evidence-bundle.js +228 -0
  28. package/_modules/knowledge-engine/src/runtime/git-runner.js +62 -0
  29. package/_modules/knowledge-engine/src/runtime/knowledge-promotion.js +444 -0
  30. package/_modules/knowledge-engine/src/runtime/knowledge-retrieval-service.js +402 -0
  31. package/_modules/knowledge-engine/src/runtime/layout.js +44 -0
  32. package/_modules/knowledge-engine/src/runtime/markdown-knowledge-indexer.js +230 -0
  33. package/_modules/knowledge-engine/src/runtime/scanner.js +298 -0
  34. package/_modules/knowledge-engine/src/server.js +155 -0
  35. package/_modules/knowledge-engine/src/store.js +239 -0
  36. package/_modules/knowledge-engine/ui/app.css +172 -0
  37. package/_modules/knowledge-engine/ui/app.js +297 -0
  38. package/_modules/knowledge-engine/ui/index.html +95 -0
  39. package/_site/README.md +30 -30
  40. package/_site/_test/ai-profile-resolver-test.js +137 -137
  41. package/_site/_test/automation-queue-test.js +14 -14
  42. package/_site/_test/automation-ui-test.js +40 -40
  43. package/_site/_test/background-task-registry-test.js +43 -43
  44. package/_site/_test/baseline-schema-test.js +91 -91
  45. package/_site/_test/chat-claudecodeui-match-test.js +92 -92
  46. package/_site/_test/claude-executable-discovery-test.js +62 -62
  47. package/_site/_test/claude-workbench-test.js +170 -170
  48. package/_site/_test/codex-conversation-projection-test.js +2 -1
  49. package/_site/_test/commit-conversation-binding-test.js +95 -95
  50. package/_site/_test/commit-evidence-test.js +110 -110
  51. package/_site/_test/conversation-api-test.js +109 -109
  52. package/_site/_test/conversation-store-test.js +136 -136
  53. package/_site/_test/data-dir-migration-test.js +57 -57
  54. package/_site/_test/desktop-browser-compat-test.js +28 -28
  55. package/_site/_test/desktop-hook-runtime-regression-test.js +204 -204
  56. package/_site/_test/explicit-commit-processor-test.js +37 -37
  57. package/_site/_test/fixtures/make-git-repos.js +12 -1
  58. package/_site/_test/folder-picker-output-test.js +19 -19
  59. package/_site/_test/full-integration-e2e-test.js +165 -165
  60. package/_site/_test/git-validation-test.js +2 -2
  61. package/_site/_test/hook-runtime-endpoint-test.js +88 -88
  62. package/_site/_test/hook-status-repair-api-test.js +152 -152
  63. package/_site/_test/hook-trigger-test.js +85 -85
  64. package/_site/_test/import-preflight-api-test.js +234 -233
  65. package/_site/_test/index-writer-concurrency-test.js +135 -135
  66. package/_site/_test/integration-adapters-test.js +171 -171
  67. package/_site/_test/integration-surface-coverage-test.js +164 -164
  68. package/_site/_test/knowledge-language-control-test.js +170 -170
  69. package/_site/_test/knowledge-promotion-recovery-test.js +260 -260
  70. package/_site/_test/knowledge-query-test.js +54 -54
  71. package/_site/_test/knowledge-retrieval-service-test.js +77 -77
  72. package/_site/_test/knowledge-storage-startup-test.js +52 -52
  73. package/_site/_test/knowledge-store-logs-supervision-test.js +89 -89
  74. package/_site/_test/legacy-forward-compat-test.js +257 -257
  75. package/_site/_test/legacy-project-upgrade-e2e-test.js +363 -363
  76. package/_site/_test/logging-api-test.js +89 -89
  77. package/_site/_test/logging-sse-no-gap-test.js +88 -88
  78. package/_site/_test/logging-ui-test.js +106 -104
  79. package/_site/_test/markdown-delta-overlay-test.js +85 -85
  80. package/_site/_test/markdown-maintenance-api-test.js +75 -75
  81. package/_site/_test/mcp-server-test.js +149 -149
  82. package/_site/_test/module-artifact-integrity-test.js +27 -0
  83. package/_site/_test/module-boundary-test.js +30 -0
  84. package/_site/_test/module-bridge-eventbridge-test.js +7 -0
  85. package/_site/_test/module-model-configuration-test.js +43 -0
  86. package/_site/_test/module-process-lifecycle-test.js +24 -0
  87. package/_site/_test/module-stream-proxy-test.js +34 -0
  88. package/_site/_test/non-release-ci-test.js +34 -34
  89. package/_site/_test/offline-boundary-isolation-test.js +32 -32
  90. package/_site/_test/p0-data-migration-characterization-test.js +37 -37
  91. package/_site/_test/p0-e2e-gate-test.js +347 -347
  92. package/_site/_test/packaged-ui-smoke-test.js +11 -9
  93. package/_site/_test/path-consistency-test.js +151 -151
  94. package/_site/_test/pending-sweep-test.js +9 -9
  95. package/_site/_test/post-commit-automation-test.js +87 -87
  96. package/_site/_test/product-diagnostics-ui-test.js +75 -0
  97. package/_site/_test/product-import-ui-test.js +92 -0
  98. package/_site/_test/project-delete-recovery-test.js +64 -64
  99. package/_site/_test/project-goal-editor-test.js +144 -144
  100. package/_site/_test/project-layout-v2-migration-test.js +127 -127
  101. package/_site/_test/project-lifecycle-transaction-test.js +106 -106
  102. package/_site/_test/prompt-settings-test.js +115 -115
  103. package/_site/_test/protected-architecture-gate-test.js +122 -122
  104. package/_site/_test/refactor-characterization-test.js +36 -36
  105. package/_site/_test/release-version-sync-test.js +1 -1
  106. package/_site/_test/requirement-recorder-test.js +173 -173
  107. package/_site/_test/run-all-tests.js +159 -156
  108. package/_site/_test/server-runtime-migration-safety-test.js +27 -27
  109. package/_site/_test/shared-contracts-test.js +58 -58
  110. package/_site/_test/startup-analysis-disabled-test.js +10 -10
  111. package/_site/_test/storage-foundation-test.js +78 -78
  112. package/_site/_test/structured-logger-test.js +101 -101
  113. package/_site/_test/tracking-start-test.js +108 -108
  114. package/_site/_test/v4122-upgrade-data-contract-test.js +56 -56
  115. package/_site/_test/workbench-permission-test.js +117 -117
  116. package/_site/_test/workspace-ui-contract-test.js +28 -43
  117. package/_site/lib/ai-profile-resolver.js +78 -78
  118. package/_site/lib/ai-workspace.js +101 -101
  119. package/_site/lib/automation-config.js +140 -140
  120. package/_site/lib/bridge-adapter.js +1 -0
  121. package/_site/lib/bridge-consumer-service.js +402 -402
  122. package/_site/lib/claude-cli-runner.js +1510 -1510
  123. package/_site/lib/commit-conversation-binder.js +234 -234
  124. package/_site/lib/commit-processing-ledger.js +27 -27
  125. package/_site/lib/commit-prompt.js +202 -202
  126. package/_site/lib/commit-reconciler.js +406 -406
  127. package/_site/lib/contracts.js +284 -279
  128. package/_site/lib/conversation-query-service.js +144 -144
  129. package/_site/lib/conversation-store.js +385 -385
  130. package/_site/lib/data-dir.js +74 -74
  131. package/_site/lib/data-state-classifier.js +40 -40
  132. package/_site/lib/engine-cutover.js +97 -0
  133. package/_site/lib/evidence-bundle.js +228 -228
  134. package/_site/lib/folder-picker-output.js +21 -21
  135. package/_site/lib/git-runner.js +62 -62
  136. package/_site/lib/github-team-store.js +1172 -1172
  137. package/_site/lib/hook-manager.js +203 -202
  138. package/_site/lib/index-service.js +173 -173
  139. package/_site/lib/integration-installer.js +907 -907
  140. package/_site/lib/kb-framework.js +176 -176
  141. package/_site/lib/kb-validator.js +124 -124
  142. package/_site/lib/knowledge-promotion.js +435 -435
  143. package/_site/lib/knowledge-retrieval-service.js +401 -401
  144. package/_site/lib/knowledge-tool-runtime.js +359 -359
  145. package/_site/lib/legacy-data-manifest.js +26 -26
  146. package/_site/lib/llm-client.js +161 -161
  147. package/_site/lib/markdown-knowledge-indexer.js +230 -230
  148. package/_site/lib/migration-service.js +442 -442
  149. package/_site/lib/module-bridge.js +198 -33
  150. package/_site/lib/post-commit-automation.js +87 -87
  151. package/_site/lib/project-lifecycle-service.js +519 -497
  152. package/_site/lib/project-store.js +268 -262
  153. package/_site/lib/requirement-recorder.js +278 -278
  154. package/_site/lib/runtime-endpoint.js +155 -155
  155. package/_site/lib/scanner.js +298 -298
  156. package/_site/lib/server-app.js +1590 -1520
  157. package/_site/lib/settings-store.js +133 -113
  158. package/_site/lib/storage-layout.js +162 -162
  159. package/_site/lib/structured-logger.js +574 -574
  160. package/_site/scripts/folder-picker.ps1 +156 -156
  161. package/_site/scripts/hook-trigger.js +157 -156
  162. package/_site/scripts/install-module-candidates.js +69 -0
  163. package/_site/scripts/pack-module-candidates.js +72 -0
  164. package/_site/scripts/sync-release-version.js +2 -2
  165. package/_site/scripts/vendor-modules.js +47 -0
  166. package/_site/scripts/verify-module-runtime.js +45 -0
  167. package/_site/scripts/verify-product-runtime.js +34 -0
  168. package/bin/project-knowledge-mcp.js +194 -194
  169. package/docs/README.zh-CN.md +34 -25
  170. package/docs/project-registry-schema.md +22 -22
  171. package/docs/testing-strategy.md +33 -33
  172. package/module-runtime-manifest.json +220 -0
  173. package/package.json +13 -9
  174. package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
  175. package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
  176. package/plugins/project-knowledge/.mcp.json +1 -1
  177. package/plugins/project-knowledge/opencode/project-knowledge.md +3 -3
  178. package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -28
  179. package/templates/project-readme.md +34 -34
  180. package/ui/favicon.svg +38 -38
  181. package/ui/index.html +51 -149
  182. package/ui/product.css +9 -0
  183. package/ui/product.js +386 -0
  184. package/vendor-manifest.json +46 -0
  185. package/_site/_test/import-ui-flow-test.js +0 -171
  186. package/_site/_test/project-control-panel-task14-test.js +0 -63
  187. package/_site/_test/task15-20-ui-flow-test.js +0 -148
  188. package/_site/_test/ui-i18n-toggle-test.js +0 -114
  189. package/_site/_test/ui-smoke-test.js +0 -73
  190. package/ui/app.css +0 -58
  191. package/ui/app.js +0 -607
  192. package/ui/i18n.js +0 -146
@@ -1,435 +1,435 @@
1
- const crypto = require('crypto');
2
- const fs = require('fs');
3
- const path = require('path');
4
- const { TextDecoder } = require('util');
5
- const AtomicFile = require('./atomic-file');
6
- const { DomainError, createId, validateProjectId } = require('./contracts');
7
- const { StorageLayout } = require('./storage-layout');
8
- const { ProjectStore } = require('./project-store');
9
- const { buildAutomationToolPolicy } = require('./automation-config');
10
- const { execGit } = require('./git-runner');
11
-
12
- const MANIFEST_SCHEMA = 'knowledge-staging-manifest/v1';
13
- const JOURNAL_SCHEMA = 'promotion-journal/v1';
14
- const MAX_KNOWLEDGE_FILE_BYTES = 1024 * 1024;
15
- const WINDOWS_RESERVED = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i;
16
-
17
- function hashBuffer(value) {
18
- return `sha256:${crypto.createHash('sha256').update(value).digest('hex')}`;
19
- }
20
-
21
- function fileHash(filePath) {
22
- return fs.existsSync(filePath) && fs.statSync(filePath).isFile()
23
- ? hashBuffer(fs.readFileSync(filePath))
24
- : null;
25
- }
26
-
27
- function canonicalRelativePath(value) {
28
- const raw = String(value || '');
29
- if (!raw || raw.includes('\\') || raw.includes('\u0000') || path.posix.isAbsolute(raw) || path.win32.isAbsolute(raw)) {
30
- throw new DomainError('PATH_OUTSIDE_ROOT', 'Manifest path must be a canonical relative POSIX path.');
31
- }
32
- const segments = raw.split('/');
33
- if (segments.some(segment => !segment || segment === '.' || segment === '..' || segment.includes(':') || /[. ]$/.test(segment) || WINDOWS_RESERVED.test(segment))) {
34
- throw new DomainError('PATH_OUTSIDE_ROOT', 'Manifest path contains an unsafe segment.');
35
- }
36
- if (!raw.toLowerCase().endsWith('.md') || segments.some(segment => segment.toLowerCase() === '00-index.md')) {
37
- throw new DomainError('INVALID_ARGUMENT', 'Knowledge operations may target non-derived Markdown files only.');
38
- }
39
- const topLevel = segments.length === 1 && ['README.md', 'GOAL.md', 'ARCHITECTURE.md'].includes(segments[0]);
40
- const collection = segments.length >= 2 && ['modules', 'changes'].includes(segments[0]);
41
- if (!topLevel && !collection) throw new DomainError('PATH_OUTSIDE_ROOT', 'Manifest path is outside the allowed knowledge structure.');
42
- return raw;
43
- }
44
-
45
- function decodeUtf8(buffer) {
46
- try { return new TextDecoder('utf-8', { fatal: true }).decode(buffer); }
47
- catch (error) { throw new DomainError('INVALID_ARGUMENT', 'Staged Markdown must be valid UTF-8.', { cause: error }); }
48
- }
49
-
50
- function validateMarkdown(content, relativePath) {
51
- if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(content)) {
52
- throw new DomainError('INVALID_ARGUMENT', `Staged Markdown contains disallowed control characters: ${relativePath}.`);
53
- }
54
- if (!content.trim() || !/^#\s+\S+/m.test(content)) throw new DomainError('INVALID_ARGUMENT', `Staged Markdown must contain a title: ${relativePath}.`);
55
- if (/(?:\[TODO:|\bTODO:\s|\bTBD\b|PLACEHOLDER)/i.test(content)) throw new DomainError('INVALID_ARGUMENT', `Placeholder knowledge is not allowed: ${relativePath}.`);
56
- const fences = content.match(/^```/gm) || [];
57
- if (fences.length % 2 !== 0) throw new DomainError('INVALID_ARGUMENT', `Markdown code fences are unbalanced: ${relativePath}.`);
58
- if (content.startsWith('---\n') && !/^---\n[\s\S]*?\n---\n/.test(content)) {
59
- throw new DomainError('INVALID_ARGUMENT', `Markdown frontmatter is incomplete: ${relativePath}.`);
60
- }
61
- return true;
62
- }
63
-
64
- async function gitStatus(repoPath) {
65
- if (!repoPath || !fs.existsSync(repoPath)) return null;
66
- const result = await execGit(repoPath, ['status', '--porcelain=v1', '-z'], 15_000);
67
- return result && result.ok ? String(result.stdout || '') : null;
68
- }
69
-
70
- class KnowledgePromotionService {
71
- constructor(options = {}) {
72
- this.layout = options.layout || new StorageLayout(options);
73
- this.projectStore = options.projectStore || new ProjectStore({ layout: this.layout });
74
- this.analyzer = options.analyzer || null;
75
- this.indexService = options.indexService || null;
76
- this.atomic = options.atomic || AtomicFile;
77
- this.logger = options.logger || null;
78
- this.maxFileBytes = Number(options.maxFileBytes || MAX_KNOWLEDGE_FILE_BYTES);
79
- this.fault = options.fault || (() => {});
80
- }
81
-
82
- runRoot(projectId, runId) { return this.layout.getRuntimePath('runs', validateProjectId(projectId), String(runId)); }
83
- stagingPath(projectId, runId) { return path.join(this.runRoot(projectId, runId), 'output'); }
84
- evidenceRoot(projectId, runId) { return path.join(this.runRoot(projectId, runId), 'input', 'evidence'); }
85
-
86
- manifestPath(projectId, runId) { return path.join(this.stagingPath(projectId, runId), 'manifest.json'); }
87
- journalPath(projectId, runId) { return this.layout.getRuntimePath('promotions', validateProjectId(projectId), `${runId}.json`); }
88
- backupRoot(projectId, runId) { return this.layout.getRecoveryPath('promotions', validateProjectId(projectId), String(runId)); }
89
-
90
- async log(level, event, message, context) {
91
- if (this.logger && typeof this.logger[level] === 'function') await this.logger[level](event, message, context);
92
- }
93
-
94
- prepareStaging(projectId, runId) {
95
- const staging = this.stagingPath(projectId, runId);
96
- if (fs.existsSync(staging)) {
97
- const entries = fs.readdirSync(staging);
98
- if (entries.length) {
99
- const journalFile = this.journalPath(projectId, runId);
100
- if (!fs.existsSync(journalFile)) {
101
- fs.rmSync(staging, { recursive: true, force: true });
102
- } else {
103
- const journal = this.atomic.readJsonStrict(journalFile, { category: 'promotion-journal' });
104
- if (journal.phase === 'rolled-back') fs.rmSync(staging, { recursive: true, force: true });
105
- else throw new DomainError('MIGRATION_TARGET_CONFLICT', 'Run staging is owned by an unfinished promotion journal.', { status: 409 });
106
- }
107
- }
108
- }
109
- fs.mkdirSync(path.join(staging, 'files'), { recursive: true });
110
- return staging;
111
- }
112
-
113
- readManifest(projectId, runId) {
114
- return this.atomic.readJsonStrict(this.manifestPath(projectId, runId), {
115
- category: 'knowledge-staging-manifest',
116
- validate: manifest => {
117
- if (!manifest || manifest.schema !== MANIFEST_SCHEMA || manifest.projectId !== projectId || manifest.runId !== runId || !Array.isArray(manifest.operations)) {
118
- throw new DomainError('DATA_CORRUPT', 'Knowledge staging manifest is invalid.', { status: 500 });
119
- }
120
- return manifest;
121
- },
122
- });
123
- }
124
-
125
- validateManifest(input) {
126
- const { projectId, config, claim } = input;
127
- const manifest = this.readManifest(projectId, claim.runId);
128
- if (manifest.commitSha !== claim.commitSha || manifest.operations.length === 0) {
129
- throw new DomainError('INVALID_ARGUMENT', 'Manifest must contain operations for the active commit.');
130
- }
131
- const seen = new Set();
132
- const operations = [];
133
- for (const [index, raw] of manifest.operations.entries()) {
134
- const relativePath = canonicalRelativePath(raw.path);
135
- if (seen.has(relativePath.toLowerCase())) throw new DomainError('INVALID_ARGUMENT', `Duplicate manifest path: ${relativePath}.`);
136
- seen.add(relativePath.toLowerCase());
137
- const operation = String(raw.operation || '');
138
- if (!['create', 'replace', 'delete'].includes(operation)) throw new DomainError('INVALID_ARGUMENT', `Invalid manifest operation: ${operation}.`);
139
- if (!String(raw.reason || '').trim() || !Array.isArray(raw.evidenceReferences) || raw.evidenceReferences.length === 0) {
140
- throw new DomainError('INVALID_ARGUMENT', `Manifest operation lacks reason/evidence: ${relativePath}.`);
141
- }
142
- const evidenceReferences = raw.evidenceReferences.map(String);
143
- const requiredEvidence = [
144
- `commit:${claim.commitSha}`,
145
- claim.patchHash && `patch:${claim.patchHash}`,
146
- claim.conversationSnapshotHash && `conversation:${claim.conversationSnapshotHash}`,
147
- claim.retrievalManifestHash && `retrieval:${claim.retrievalManifestHash}`,
148
- ].filter(Boolean);
149
- const missingEvidence = requiredEvidence.filter(reference => !evidenceReferences.includes(reference));
150
- if (missingEvidence.length) {
151
- throw new DomainError('INVALID_ARGUMENT', `Manifest operation lacks frozen claim evidence: ${relativePath}.`, {
152
- details: { missingEvidence },
153
- });
154
- }
155
- const target = path.resolve(config.knowledgePath, ...relativePath.split('/'));
156
- if (!this.layout.isPathInside(config.knowledgePath, target, { realpath: true })) throw new DomainError('PATH_OUTSIDE_ROOT', `Final knowledge path escapes its project: ${relativePath}.`);
157
- const targetExists = fs.existsSync(target);
158
- if (targetExists && fs.lstatSync(target).isSymbolicLink()) throw new DomainError('PATH_OUTSIDE_ROOT', `Final knowledge target is a symlink: ${relativePath}.`);
159
- if (operation === 'create' && targetExists) throw new DomainError('MIGRATION_TARGET_CONFLICT', `Create target already exists: ${relativePath}.`, { status: 409 });
160
- if ((operation === 'replace' || operation === 'delete') && (!targetExists || !fs.statSync(target).isFile())) {
161
- throw new DomainError('INVALID_ARGUMENT', `${operation} target is not an owned project knowledge file: ${relativePath}.`);
162
- }
163
- let stagedPath = null;
164
- let newHash = null;
165
- if (operation !== 'delete') {
166
- stagedPath = path.join(this.stagingPath(projectId, claim.runId), 'files', ...relativePath.split('/'));
167
- if (!fs.existsSync(stagedPath) || !fs.statSync(stagedPath).isFile() || fs.lstatSync(stagedPath).isSymbolicLink()) {
168
- throw new DomainError('INVALID_ARGUMENT', `Staged Markdown is missing or unsafe: ${relativePath}.`);
169
- }
170
- const realStaged = fs.realpathSync(stagedPath);
171
- if (!this.layout.isPathInside(this.stagingPath(projectId, claim.runId), realStaged, { realpath: true })) {
172
- throw new DomainError('PATH_OUTSIDE_ROOT', `Staged file escapes run staging: ${relativePath}.`);
173
- }
174
- const buffer = fs.readFileSync(stagedPath);
175
- if (buffer.length > this.maxFileBytes) throw new DomainError('INVALID_ARGUMENT', `Staged Markdown exceeds size limit: ${relativePath}.`);
176
- const content = decodeUtf8(buffer);
177
- validateMarkdown(content, relativePath);
178
- newHash = hashBuffer(buffer);
179
- if (raw.sha256 !== newHash) throw new DomainError('DATA_CORRUPT', `Staged Markdown hash mismatch: ${relativePath}.`, { status: 409 });
180
- } else if (raw.sha256 != null) {
181
- throw new DomainError('INVALID_ARGUMENT', `Delete operation must not declare a new hash: ${relativePath}.`);
182
- }
183
- operations.push({
184
- index,
185
- path: relativePath,
186
- operation,
187
- reason: String(raw.reason),
188
- evidenceReferences,
189
- target,
190
- stagedPath,
191
- originalExists: targetExists,
192
- originalHash: targetExists ? fileHash(target) : null,
193
- newHash,
194
- });
195
- }
196
- return { manifest, operations: operations.sort((left, right) => left.path.localeCompare(right.path)) };
197
- }
198
-
199
- writeJournal(journal) {
200
- journal.updatedAt = new Date().toISOString();
201
- this.atomic.writeJsonAtomic(this.journalPath(journal.projectId, journal.runId), journal);
202
- }
203
-
204
- prepareJournal(projectId, config, claim, validated) {
205
- const backupRoot = this.backupRoot(projectId, claim.runId);
206
- const journal = {
207
- schema: JOURNAL_SCHEMA,
208
- projectId,
209
- runId: claim.runId,
210
- commitSha: claim.commitSha,
211
- claimFingerprint: claim.fingerprint,
212
- evidence: {
213
- promptHash: claim.promptHash || null,
214
- patchHash: claim.patchHash || null,
215
- evidenceManifestHash: claim.evidenceManifestHash || null,
216
- conversationSnapshotHash: claim.conversationSnapshotHash || null,
217
- retrievalManifestHash: claim.retrievalManifestHash || null,
218
- },
219
- knowledgePath: config.knowledgePath,
220
- phase: 'preparing',
221
- createdAt: new Date().toISOString(),
222
- operations: validated.operations.map((operation, index) => ({
223
- ...operation,
224
- backupPath: operation.originalExists ? path.join(backupRoot, `${String(index).padStart(4, '0')}.backup`) : null,
225
- applied: false,
226
- })),
227
- };
228
- this.writeJournal(journal);
229
- for (const operation of journal.operations) {
230
- if (!operation.originalExists) continue;
231
- const currentHash = fileHash(operation.target);
232
- if (currentHash !== operation.originalHash) throw new DomainError('PROJECT_BUSY', `Knowledge changed before backup: ${operation.path}.`, { status: 409, retryable: true });
233
- this.atomic.writeFileAtomic(operation.backupPath, fs.readFileSync(operation.target));
234
- if (fileHash(operation.backupPath) !== operation.originalHash) throw new DomainError('DATA_CORRUPT', `Knowledge backup verification failed: ${operation.path}.`, { status: 500 });
235
- }
236
- journal.phase = 'prepared';
237
- this.writeJournal(journal);
238
- this.fault('journal-prepared', journal);
239
- return journal;
240
- }
241
-
242
- applyJournal(journal) {
243
- journal.phase = 'applying';
244
- this.writeJournal(journal);
245
- for (let index = 0; index < journal.operations.length; index += 1) {
246
- const operation = journal.operations[index];
247
- const currentHash = fileHash(operation.target);
248
- if (currentHash !== operation.originalHash) {
249
- throw new DomainError('PROJECT_BUSY', `Knowledge changed before promotion: ${operation.path}.`, { status: 409, retryable: true });
250
- }
251
- if (operation.operation === 'delete') fs.unlinkSync(operation.target);
252
- else this.atomic.writeFileAtomic(operation.target, fs.readFileSync(operation.stagedPath));
253
- operation.applied = true;
254
- this.writeJournal(journal);
255
- this.fault(`operation-applied:${index}`, journal);
256
- }
257
- this.verifyPromoted(journal);
258
- journal.phase = 'awaiting-state-advance';
259
- this.writeJournal(journal);
260
- this.fault('promotion-verified', journal);
261
- return journal;
262
- }
263
-
264
- verifyPromoted(journal) {
265
- for (const operation of journal.operations) {
266
- const actual = fileHash(operation.target);
267
- if (operation.operation === 'delete' ? actual !== null : actual !== operation.newHash) {
268
- throw new DomainError('DATA_CORRUPT', `Promoted knowledge verification failed: ${operation.path}.`, { status: 500 });
269
- }
270
- }
271
- return true;
272
- }
273
-
274
- async advanceState(journal) {
275
- const current = this.projectStore.readState(journal.projectId);
276
- if (current.lastAnalyzedCommit === journal.commitSha && !current.analysis.activeClaim) return current;
277
- const advanced = await this.projectStore.updateState(journal.projectId, state => {
278
- const active = state.analysis.activeClaim;
279
- if (!active || active.runId !== journal.runId || active.commitSha !== journal.commitSha || active.fingerprint !== journal.claimFingerprint) {
280
- throw new DomainError('PROJECT_BUSY', 'Promotion journal no longer matches the active claim.', { status: 409, retryable: true });
281
- }
282
- state.lastAnalyzedCommit = journal.commitSha;
283
- state.analysis.consumedRequirementIds = [...new Set([
284
- ...(state.analysis.consumedRequirementIds || []),
285
- ...(active.requirementIds || []),
286
- ])];
287
- state.analysis.activeClaim = null;
288
- state.analysis.status = 'state.advanced';
289
- state.analysis.lastError = null;
290
- state.index.dirty = true;
291
- state.index.sinceCommit = state.index.sinceCommit || journal.commitSha;
292
- state.index.generation = Number(state.index.generation || 0) + 1;
293
- state.index.lastError = null;
294
- });
295
- this.fault('state-advanced', journal);
296
- return advanced;
297
- }
298
-
299
- async processClaim(input = {}) {
300
- const projectId = validateProjectId(input.projectId);
301
- const config = input.config || this.projectStore.readConfig(projectId);
302
- const claim = input.claim;
303
- if (!claim || claim.projectId !== projectId || !claim.runId || !claim.commitSha) throw new DomainError('INVALID_ARGUMENT', 'Active claim is required for knowledge promotion.');
304
- const operationId = input.operationId || claim.operationId || createId('op');
305
- const stagingPath = this.prepareStaging(projectId, claim.runId);
306
- const beforeSource = await gitStatus(config.repoPath);
307
- try {
308
- if (!this.analyzer || typeof this.analyzer.runClaim !== 'function') throw new DomainError('INVALID_ARGUMENT', 'Knowledge analyzer is unavailable.', { status: 503, retryable: true });
309
- const analyzerResult = await this.analyzer.runClaim({
310
- ...input,
311
- projectId,
312
- config,
313
- claim,
314
- stagingPath,
315
- manifestPath: this.manifestPath(projectId, claim.runId),
316
- evidenceRoot: this.evidenceRoot(projectId, claim.runId),
317
- safetyPolicy: buildAutomationToolPolicy({
318
- stagingPath,
319
- evidenceRoot: this.evidenceRoot(projectId, claim.runId),
320
- }),
321
- });
322
- if (analyzerResult && analyzerResult.ok === false) throw new DomainError('INVALID_ARGUMENT', analyzerResult.error || 'Knowledge analyzer failed.', { status: 500, retryable: true });
323
- const afterSource = await gitStatus(config.repoPath);
324
- if (beforeSource != null && afterSource != null && beforeSource !== afterSource) {
325
- throw new DomainError('PROJECT_BUSY', 'Source repository changed during knowledge analysis.', { status: 409, retryable: true });
326
- }
327
- this.fault('analysis-completed', { projectId, claim, stagingPath });
328
- const validated = this.validateManifest({ projectId, config, claim });
329
- this.fault('manifest-validated', validated);
330
- const journal = this.prepareJournal(projectId, config, claim, validated);
331
- this.applyJournal(journal);
332
- await this.advanceState(journal);
333
- journal.phase = 'completed';
334
- journal.completedAt = new Date().toISOString();
335
- this.writeJournal(journal);
336
- await this.log('info', 'knowledge.promotion_completed', 'Validated knowledge was promoted and state advanced.', {
337
- projectId,
338
- operationId,
339
- runId: claim.runId,
340
- commitSha: claim.commitSha,
341
- phase: 'state.advanced',
342
- operationCount: journal.operations.length,
343
- });
344
- if (this.indexService && typeof this.indexService.enqueue === 'function') this.indexService.enqueue(projectId).catch(() => {});
345
- return { ok: true, stateAdvanced: true, projectId, runId: claim.runId, commitSha: claim.commitSha, operations: journal.operations.length };
346
- } catch (error) {
347
- await this.log('error', 'knowledge.promotion_failed', 'Knowledge analysis/promotion failed without advancing state.', {
348
- projectId,
349
- operationId,
350
- runId: claim.runId,
351
- commitSha: claim.commitSha,
352
- phase: 'promotion',
353
- error,
354
- });
355
- throw error;
356
- }
357
- }
358
-
359
- rollbackJournal(journal) {
360
- let conflict = null;
361
- for (const operation of [...journal.operations].reverse()) {
362
- if (!operation.applied) continue;
363
- const currentHash = fileHash(operation.target);
364
- const expectedApplied = operation.operation === 'delete' ? null : operation.newHash;
365
- if (currentHash !== expectedApplied) {
366
- conflict = operation.path;
367
- break;
368
- }
369
- if (operation.originalExists) this.atomic.writeFileAtomic(operation.target, fs.readFileSync(operation.backupPath));
370
- else if (fs.existsSync(operation.target)) fs.unlinkSync(operation.target);
371
- }
372
- if (conflict) {
373
- journal.phase = 'recovery-conflict';
374
- journal.error = `User-visible knowledge changed after crash: ${conflict}`;
375
- } else {
376
- journal.phase = 'rolled-back';
377
- journal.rolledBackAt = new Date().toISOString();
378
- }
379
- this.writeJournal(journal);
380
- return { ok: !conflict, phase: journal.phase, conflict };
381
- }
382
-
383
- async recoverJournal(journal) {
384
- if (journal.phase === 'completed' || journal.phase === 'rolled-back') return { ok: true, phase: journal.phase, skipped: true };
385
- const appliedCount = journal.operations.filter(operation => operation.applied).length;
386
- const allApplied = appliedCount === journal.operations.length;
387
- if (journal.phase === 'awaiting-state-advance' || (allApplied && ['applying', 'prepared'].includes(journal.phase))) {
388
- this.verifyPromoted(journal);
389
- await this.advanceState(journal);
390
- journal.phase = 'completed';
391
- journal.recoveredAt = new Date().toISOString();
392
- this.writeJournal(journal);
393
- if (this.indexService && typeof this.indexService.enqueue === 'function') this.indexService.enqueue(journal.projectId).catch(() => {});
394
- return { ok: true, phase: 'completed', stateAdvanced: true };
395
- }
396
- return this.rollbackJournal(journal);
397
- }
398
-
399
- async recoverAll() {
400
- const root = this.layout.getRuntimePath('promotions');
401
- if (!fs.existsSync(root)) return [];
402
- const files = [];
403
- const walk = current => {
404
- for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
405
- const absolute = path.join(current, entry.name);
406
- if (entry.isDirectory()) walk(absolute);
407
- else if (entry.isFile() && entry.name.endsWith('.json')) files.push(absolute);
408
- }
409
- };
410
- walk(root);
411
- const results = [];
412
- for (const file of files.sort()) {
413
- const journal = this.atomic.readJsonStrict(file, {
414
- category: 'promotion-journal',
415
- validate: value => {
416
- if (!value || value.schema !== JOURNAL_SCHEMA || !Array.isArray(value.operations)) throw new DomainError('DATA_CORRUPT', 'Promotion journal is corrupt.', { status: 500 });
417
- return value;
418
- },
419
- });
420
- results.push({ file, ...(await this.recoverJournal(journal)) });
421
- }
422
- return results;
423
- }
424
- }
425
-
426
- module.exports = {
427
- JOURNAL_SCHEMA,
428
- MANIFEST_SCHEMA,
429
- MAX_KNOWLEDGE_FILE_BYTES,
430
- KnowledgePromotionService,
431
- canonicalRelativePath,
432
- fileHash,
433
- hashBuffer,
434
- validateMarkdown,
435
- };
1
+ const crypto = require('crypto');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const { TextDecoder } = require('util');
5
+ const AtomicFile = require('./atomic-file');
6
+ const { DomainError, createId, validateProjectId } = require('./contracts');
7
+ const { StorageLayout } = require('./storage-layout');
8
+ const { ProjectStore } = require('./project-store');
9
+ const { buildAutomationToolPolicy } = require('./automation-config');
10
+ const { execGit } = require('./git-runner');
11
+
12
+ const MANIFEST_SCHEMA = 'knowledge-staging-manifest/v1';
13
+ const JOURNAL_SCHEMA = 'promotion-journal/v1';
14
+ const MAX_KNOWLEDGE_FILE_BYTES = 1024 * 1024;
15
+ const WINDOWS_RESERVED = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i;
16
+
17
+ function hashBuffer(value) {
18
+ return `sha256:${crypto.createHash('sha256').update(value).digest('hex')}`;
19
+ }
20
+
21
+ function fileHash(filePath) {
22
+ return fs.existsSync(filePath) && fs.statSync(filePath).isFile()
23
+ ? hashBuffer(fs.readFileSync(filePath))
24
+ : null;
25
+ }
26
+
27
+ function canonicalRelativePath(value) {
28
+ const raw = String(value || '');
29
+ if (!raw || raw.includes('\\') || raw.includes('\u0000') || path.posix.isAbsolute(raw) || path.win32.isAbsolute(raw)) {
30
+ throw new DomainError('PATH_OUTSIDE_ROOT', 'Manifest path must be a canonical relative POSIX path.');
31
+ }
32
+ const segments = raw.split('/');
33
+ if (segments.some(segment => !segment || segment === '.' || segment === '..' || segment.includes(':') || /[. ]$/.test(segment) || WINDOWS_RESERVED.test(segment))) {
34
+ throw new DomainError('PATH_OUTSIDE_ROOT', 'Manifest path contains an unsafe segment.');
35
+ }
36
+ if (!raw.toLowerCase().endsWith('.md') || segments.some(segment => segment.toLowerCase() === '00-index.md')) {
37
+ throw new DomainError('INVALID_ARGUMENT', 'Knowledge operations may target non-derived Markdown files only.');
38
+ }
39
+ const topLevel = segments.length === 1 && ['README.md', 'GOAL.md', 'ARCHITECTURE.md'].includes(segments[0]);
40
+ const collection = segments.length >= 2 && ['modules', 'changes'].includes(segments[0]);
41
+ if (!topLevel && !collection) throw new DomainError('PATH_OUTSIDE_ROOT', 'Manifest path is outside the allowed knowledge structure.');
42
+ return raw;
43
+ }
44
+
45
+ function decodeUtf8(buffer) {
46
+ try { return new TextDecoder('utf-8', { fatal: true }).decode(buffer); }
47
+ catch (error) { throw new DomainError('INVALID_ARGUMENT', 'Staged Markdown must be valid UTF-8.', { cause: error }); }
48
+ }
49
+
50
+ function validateMarkdown(content, relativePath) {
51
+ if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(content)) {
52
+ throw new DomainError('INVALID_ARGUMENT', `Staged Markdown contains disallowed control characters: ${relativePath}.`);
53
+ }
54
+ if (!content.trim() || !/^#\s+\S+/m.test(content)) throw new DomainError('INVALID_ARGUMENT', `Staged Markdown must contain a title: ${relativePath}.`);
55
+ if (/(?:\[TODO:|\bTODO:\s|\bTBD\b|PLACEHOLDER)/i.test(content)) throw new DomainError('INVALID_ARGUMENT', `Placeholder knowledge is not allowed: ${relativePath}.`);
56
+ const fences = content.match(/^```/gm) || [];
57
+ if (fences.length % 2 !== 0) throw new DomainError('INVALID_ARGUMENT', `Markdown code fences are unbalanced: ${relativePath}.`);
58
+ if (content.startsWith('---\n') && !/^---\n[\s\S]*?\n---\n/.test(content)) {
59
+ throw new DomainError('INVALID_ARGUMENT', `Markdown frontmatter is incomplete: ${relativePath}.`);
60
+ }
61
+ return true;
62
+ }
63
+
64
+ async function gitStatus(repoPath) {
65
+ if (!repoPath || !fs.existsSync(repoPath)) return null;
66
+ const result = await execGit(repoPath, ['status', '--porcelain=v1', '-z'], 15_000);
67
+ return result && result.ok ? String(result.stdout || '') : null;
68
+ }
69
+
70
+ class KnowledgePromotionService {
71
+ constructor(options = {}) {
72
+ this.layout = options.layout || new StorageLayout(options);
73
+ this.projectStore = options.projectStore || new ProjectStore({ layout: this.layout });
74
+ this.analyzer = options.analyzer || null;
75
+ this.indexService = options.indexService || null;
76
+ this.atomic = options.atomic || AtomicFile;
77
+ this.logger = options.logger || null;
78
+ this.maxFileBytes = Number(options.maxFileBytes || MAX_KNOWLEDGE_FILE_BYTES);
79
+ this.fault = options.fault || (() => {});
80
+ }
81
+
82
+ runRoot(projectId, runId) { return this.layout.getRuntimePath('runs', validateProjectId(projectId), String(runId)); }
83
+ stagingPath(projectId, runId) { return path.join(this.runRoot(projectId, runId), 'output'); }
84
+ evidenceRoot(projectId, runId) { return path.join(this.runRoot(projectId, runId), 'input', 'evidence'); }
85
+
86
+ manifestPath(projectId, runId) { return path.join(this.stagingPath(projectId, runId), 'manifest.json'); }
87
+ journalPath(projectId, runId) { return this.layout.getRuntimePath('promotions', validateProjectId(projectId), `${runId}.json`); }
88
+ backupRoot(projectId, runId) { return this.layout.getRecoveryPath('promotions', validateProjectId(projectId), String(runId)); }
89
+
90
+ async log(level, event, message, context) {
91
+ if (this.logger && typeof this.logger[level] === 'function') await this.logger[level](event, message, context);
92
+ }
93
+
94
+ prepareStaging(projectId, runId) {
95
+ const staging = this.stagingPath(projectId, runId);
96
+ if (fs.existsSync(staging)) {
97
+ const entries = fs.readdirSync(staging);
98
+ if (entries.length) {
99
+ const journalFile = this.journalPath(projectId, runId);
100
+ if (!fs.existsSync(journalFile)) {
101
+ fs.rmSync(staging, { recursive: true, force: true });
102
+ } else {
103
+ const journal = this.atomic.readJsonStrict(journalFile, { category: 'promotion-journal' });
104
+ if (journal.phase === 'rolled-back') fs.rmSync(staging, { recursive: true, force: true });
105
+ else throw new DomainError('MIGRATION_TARGET_CONFLICT', 'Run staging is owned by an unfinished promotion journal.', { status: 409 });
106
+ }
107
+ }
108
+ }
109
+ fs.mkdirSync(path.join(staging, 'files'), { recursive: true });
110
+ return staging;
111
+ }
112
+
113
+ readManifest(projectId, runId) {
114
+ return this.atomic.readJsonStrict(this.manifestPath(projectId, runId), {
115
+ category: 'knowledge-staging-manifest',
116
+ validate: manifest => {
117
+ if (!manifest || manifest.schema !== MANIFEST_SCHEMA || manifest.projectId !== projectId || manifest.runId !== runId || !Array.isArray(manifest.operations)) {
118
+ throw new DomainError('DATA_CORRUPT', 'Knowledge staging manifest is invalid.', { status: 500 });
119
+ }
120
+ return manifest;
121
+ },
122
+ });
123
+ }
124
+
125
+ validateManifest(input) {
126
+ const { projectId, config, claim } = input;
127
+ const manifest = this.readManifest(projectId, claim.runId);
128
+ if (manifest.commitSha !== claim.commitSha || manifest.operations.length === 0) {
129
+ throw new DomainError('INVALID_ARGUMENT', 'Manifest must contain operations for the active commit.');
130
+ }
131
+ const seen = new Set();
132
+ const operations = [];
133
+ for (const [index, raw] of manifest.operations.entries()) {
134
+ const relativePath = canonicalRelativePath(raw.path);
135
+ if (seen.has(relativePath.toLowerCase())) throw new DomainError('INVALID_ARGUMENT', `Duplicate manifest path: ${relativePath}.`);
136
+ seen.add(relativePath.toLowerCase());
137
+ const operation = String(raw.operation || '');
138
+ if (!['create', 'replace', 'delete'].includes(operation)) throw new DomainError('INVALID_ARGUMENT', `Invalid manifest operation: ${operation}.`);
139
+ if (!String(raw.reason || '').trim() || !Array.isArray(raw.evidenceReferences) || raw.evidenceReferences.length === 0) {
140
+ throw new DomainError('INVALID_ARGUMENT', `Manifest operation lacks reason/evidence: ${relativePath}.`);
141
+ }
142
+ const evidenceReferences = raw.evidenceReferences.map(String);
143
+ const requiredEvidence = [
144
+ `commit:${claim.commitSha}`,
145
+ claim.patchHash && `patch:${claim.patchHash}`,
146
+ claim.conversationSnapshotHash && `conversation:${claim.conversationSnapshotHash}`,
147
+ claim.retrievalManifestHash && `retrieval:${claim.retrievalManifestHash}`,
148
+ ].filter(Boolean);
149
+ const missingEvidence = requiredEvidence.filter(reference => !evidenceReferences.includes(reference));
150
+ if (missingEvidence.length) {
151
+ throw new DomainError('INVALID_ARGUMENT', `Manifest operation lacks frozen claim evidence: ${relativePath}.`, {
152
+ details: { missingEvidence },
153
+ });
154
+ }
155
+ const target = path.resolve(config.knowledgePath, ...relativePath.split('/'));
156
+ if (!this.layout.isPathInside(config.knowledgePath, target, { realpath: true })) throw new DomainError('PATH_OUTSIDE_ROOT', `Final knowledge path escapes its project: ${relativePath}.`);
157
+ const targetExists = fs.existsSync(target);
158
+ if (targetExists && fs.lstatSync(target).isSymbolicLink()) throw new DomainError('PATH_OUTSIDE_ROOT', `Final knowledge target is a symlink: ${relativePath}.`);
159
+ if (operation === 'create' && targetExists) throw new DomainError('MIGRATION_TARGET_CONFLICT', `Create target already exists: ${relativePath}.`, { status: 409 });
160
+ if ((operation === 'replace' || operation === 'delete') && (!targetExists || !fs.statSync(target).isFile())) {
161
+ throw new DomainError('INVALID_ARGUMENT', `${operation} target is not an owned project knowledge file: ${relativePath}.`);
162
+ }
163
+ let stagedPath = null;
164
+ let newHash = null;
165
+ if (operation !== 'delete') {
166
+ stagedPath = path.join(this.stagingPath(projectId, claim.runId), 'files', ...relativePath.split('/'));
167
+ if (!fs.existsSync(stagedPath) || !fs.statSync(stagedPath).isFile() || fs.lstatSync(stagedPath).isSymbolicLink()) {
168
+ throw new DomainError('INVALID_ARGUMENT', `Staged Markdown is missing or unsafe: ${relativePath}.`);
169
+ }
170
+ const realStaged = fs.realpathSync(stagedPath);
171
+ if (!this.layout.isPathInside(this.stagingPath(projectId, claim.runId), realStaged, { realpath: true })) {
172
+ throw new DomainError('PATH_OUTSIDE_ROOT', `Staged file escapes run staging: ${relativePath}.`);
173
+ }
174
+ const buffer = fs.readFileSync(stagedPath);
175
+ if (buffer.length > this.maxFileBytes) throw new DomainError('INVALID_ARGUMENT', `Staged Markdown exceeds size limit: ${relativePath}.`);
176
+ const content = decodeUtf8(buffer);
177
+ validateMarkdown(content, relativePath);
178
+ newHash = hashBuffer(buffer);
179
+ if (raw.sha256 !== newHash) throw new DomainError('DATA_CORRUPT', `Staged Markdown hash mismatch: ${relativePath}.`, { status: 409 });
180
+ } else if (raw.sha256 != null) {
181
+ throw new DomainError('INVALID_ARGUMENT', `Delete operation must not declare a new hash: ${relativePath}.`);
182
+ }
183
+ operations.push({
184
+ index,
185
+ path: relativePath,
186
+ operation,
187
+ reason: String(raw.reason),
188
+ evidenceReferences,
189
+ target,
190
+ stagedPath,
191
+ originalExists: targetExists,
192
+ originalHash: targetExists ? fileHash(target) : null,
193
+ newHash,
194
+ });
195
+ }
196
+ return { manifest, operations: operations.sort((left, right) => left.path.localeCompare(right.path)) };
197
+ }
198
+
199
+ writeJournal(journal) {
200
+ journal.updatedAt = new Date().toISOString();
201
+ this.atomic.writeJsonAtomic(this.journalPath(journal.projectId, journal.runId), journal);
202
+ }
203
+
204
+ prepareJournal(projectId, config, claim, validated) {
205
+ const backupRoot = this.backupRoot(projectId, claim.runId);
206
+ const journal = {
207
+ schema: JOURNAL_SCHEMA,
208
+ projectId,
209
+ runId: claim.runId,
210
+ commitSha: claim.commitSha,
211
+ claimFingerprint: claim.fingerprint,
212
+ evidence: {
213
+ promptHash: claim.promptHash || null,
214
+ patchHash: claim.patchHash || null,
215
+ evidenceManifestHash: claim.evidenceManifestHash || null,
216
+ conversationSnapshotHash: claim.conversationSnapshotHash || null,
217
+ retrievalManifestHash: claim.retrievalManifestHash || null,
218
+ },
219
+ knowledgePath: config.knowledgePath,
220
+ phase: 'preparing',
221
+ createdAt: new Date().toISOString(),
222
+ operations: validated.operations.map((operation, index) => ({
223
+ ...operation,
224
+ backupPath: operation.originalExists ? path.join(backupRoot, `${String(index).padStart(4, '0')}.backup`) : null,
225
+ applied: false,
226
+ })),
227
+ };
228
+ this.writeJournal(journal);
229
+ for (const operation of journal.operations) {
230
+ if (!operation.originalExists) continue;
231
+ const currentHash = fileHash(operation.target);
232
+ if (currentHash !== operation.originalHash) throw new DomainError('PROJECT_BUSY', `Knowledge changed before backup: ${operation.path}.`, { status: 409, retryable: true });
233
+ this.atomic.writeFileAtomic(operation.backupPath, fs.readFileSync(operation.target));
234
+ if (fileHash(operation.backupPath) !== operation.originalHash) throw new DomainError('DATA_CORRUPT', `Knowledge backup verification failed: ${operation.path}.`, { status: 500 });
235
+ }
236
+ journal.phase = 'prepared';
237
+ this.writeJournal(journal);
238
+ this.fault('journal-prepared', journal);
239
+ return journal;
240
+ }
241
+
242
+ applyJournal(journal) {
243
+ journal.phase = 'applying';
244
+ this.writeJournal(journal);
245
+ for (let index = 0; index < journal.operations.length; index += 1) {
246
+ const operation = journal.operations[index];
247
+ const currentHash = fileHash(operation.target);
248
+ if (currentHash !== operation.originalHash) {
249
+ throw new DomainError('PROJECT_BUSY', `Knowledge changed before promotion: ${operation.path}.`, { status: 409, retryable: true });
250
+ }
251
+ if (operation.operation === 'delete') fs.unlinkSync(operation.target);
252
+ else this.atomic.writeFileAtomic(operation.target, fs.readFileSync(operation.stagedPath));
253
+ operation.applied = true;
254
+ this.writeJournal(journal);
255
+ this.fault(`operation-applied:${index}`, journal);
256
+ }
257
+ this.verifyPromoted(journal);
258
+ journal.phase = 'awaiting-state-advance';
259
+ this.writeJournal(journal);
260
+ this.fault('promotion-verified', journal);
261
+ return journal;
262
+ }
263
+
264
+ verifyPromoted(journal) {
265
+ for (const operation of journal.operations) {
266
+ const actual = fileHash(operation.target);
267
+ if (operation.operation === 'delete' ? actual !== null : actual !== operation.newHash) {
268
+ throw new DomainError('DATA_CORRUPT', `Promoted knowledge verification failed: ${operation.path}.`, { status: 500 });
269
+ }
270
+ }
271
+ return true;
272
+ }
273
+
274
+ async advanceState(journal) {
275
+ const current = this.projectStore.readState(journal.projectId);
276
+ if (current.lastAnalyzedCommit === journal.commitSha && !current.analysis.activeClaim) return current;
277
+ const advanced = await this.projectStore.updateState(journal.projectId, state => {
278
+ const active = state.analysis.activeClaim;
279
+ if (!active || active.runId !== journal.runId || active.commitSha !== journal.commitSha || active.fingerprint !== journal.claimFingerprint) {
280
+ throw new DomainError('PROJECT_BUSY', 'Promotion journal no longer matches the active claim.', { status: 409, retryable: true });
281
+ }
282
+ state.lastAnalyzedCommit = journal.commitSha;
283
+ state.analysis.consumedRequirementIds = [...new Set([
284
+ ...(state.analysis.consumedRequirementIds || []),
285
+ ...(active.requirementIds || []),
286
+ ])];
287
+ state.analysis.activeClaim = null;
288
+ state.analysis.status = 'state.advanced';
289
+ state.analysis.lastError = null;
290
+ state.index.dirty = true;
291
+ state.index.sinceCommit = state.index.sinceCommit || journal.commitSha;
292
+ state.index.generation = Number(state.index.generation || 0) + 1;
293
+ state.index.lastError = null;
294
+ });
295
+ this.fault('state-advanced', journal);
296
+ return advanced;
297
+ }
298
+
299
+ async processClaim(input = {}) {
300
+ const projectId = validateProjectId(input.projectId);
301
+ const config = input.config || this.projectStore.readConfig(projectId);
302
+ const claim = input.claim;
303
+ if (!claim || claim.projectId !== projectId || !claim.runId || !claim.commitSha) throw new DomainError('INVALID_ARGUMENT', 'Active claim is required for knowledge promotion.');
304
+ const operationId = input.operationId || claim.operationId || createId('op');
305
+ const stagingPath = this.prepareStaging(projectId, claim.runId);
306
+ const beforeSource = await gitStatus(config.repoPath);
307
+ try {
308
+ if (!this.analyzer || typeof this.analyzer.runClaim !== 'function') throw new DomainError('INVALID_ARGUMENT', 'Knowledge analyzer is unavailable.', { status: 503, retryable: true });
309
+ const analyzerResult = await this.analyzer.runClaim({
310
+ ...input,
311
+ projectId,
312
+ config,
313
+ claim,
314
+ stagingPath,
315
+ manifestPath: this.manifestPath(projectId, claim.runId),
316
+ evidenceRoot: this.evidenceRoot(projectId, claim.runId),
317
+ safetyPolicy: buildAutomationToolPolicy({
318
+ stagingPath,
319
+ evidenceRoot: this.evidenceRoot(projectId, claim.runId),
320
+ }),
321
+ });
322
+ if (analyzerResult && analyzerResult.ok === false) throw new DomainError('INVALID_ARGUMENT', analyzerResult.error || 'Knowledge analyzer failed.', { status: 500, retryable: true });
323
+ const afterSource = await gitStatus(config.repoPath);
324
+ if (beforeSource != null && afterSource != null && beforeSource !== afterSource) {
325
+ throw new DomainError('PROJECT_BUSY', 'Source repository changed during knowledge analysis.', { status: 409, retryable: true });
326
+ }
327
+ this.fault('analysis-completed', { projectId, claim, stagingPath });
328
+ const validated = this.validateManifest({ projectId, config, claim });
329
+ this.fault('manifest-validated', validated);
330
+ const journal = this.prepareJournal(projectId, config, claim, validated);
331
+ this.applyJournal(journal);
332
+ await this.advanceState(journal);
333
+ journal.phase = 'completed';
334
+ journal.completedAt = new Date().toISOString();
335
+ this.writeJournal(journal);
336
+ await this.log('info', 'knowledge.promotion_completed', 'Validated knowledge was promoted and state advanced.', {
337
+ projectId,
338
+ operationId,
339
+ runId: claim.runId,
340
+ commitSha: claim.commitSha,
341
+ phase: 'state.advanced',
342
+ operationCount: journal.operations.length,
343
+ });
344
+ if (this.indexService && typeof this.indexService.enqueue === 'function') this.indexService.enqueue(projectId).catch(() => {});
345
+ return { ok: true, stateAdvanced: true, projectId, runId: claim.runId, commitSha: claim.commitSha, operations: journal.operations.length };
346
+ } catch (error) {
347
+ await this.log('error', 'knowledge.promotion_failed', 'Knowledge analysis/promotion failed without advancing state.', {
348
+ projectId,
349
+ operationId,
350
+ runId: claim.runId,
351
+ commitSha: claim.commitSha,
352
+ phase: 'promotion',
353
+ error,
354
+ });
355
+ throw error;
356
+ }
357
+ }
358
+
359
+ rollbackJournal(journal) {
360
+ let conflict = null;
361
+ for (const operation of [...journal.operations].reverse()) {
362
+ if (!operation.applied) continue;
363
+ const currentHash = fileHash(operation.target);
364
+ const expectedApplied = operation.operation === 'delete' ? null : operation.newHash;
365
+ if (currentHash !== expectedApplied) {
366
+ conflict = operation.path;
367
+ break;
368
+ }
369
+ if (operation.originalExists) this.atomic.writeFileAtomic(operation.target, fs.readFileSync(operation.backupPath));
370
+ else if (fs.existsSync(operation.target)) fs.unlinkSync(operation.target);
371
+ }
372
+ if (conflict) {
373
+ journal.phase = 'recovery-conflict';
374
+ journal.error = `User-visible knowledge changed after crash: ${conflict}`;
375
+ } else {
376
+ journal.phase = 'rolled-back';
377
+ journal.rolledBackAt = new Date().toISOString();
378
+ }
379
+ this.writeJournal(journal);
380
+ return { ok: !conflict, phase: journal.phase, conflict };
381
+ }
382
+
383
+ async recoverJournal(journal) {
384
+ if (journal.phase === 'completed' || journal.phase === 'rolled-back') return { ok: true, phase: journal.phase, skipped: true };
385
+ const appliedCount = journal.operations.filter(operation => operation.applied).length;
386
+ const allApplied = appliedCount === journal.operations.length;
387
+ if (journal.phase === 'awaiting-state-advance' || (allApplied && ['applying', 'prepared'].includes(journal.phase))) {
388
+ this.verifyPromoted(journal);
389
+ await this.advanceState(journal);
390
+ journal.phase = 'completed';
391
+ journal.recoveredAt = new Date().toISOString();
392
+ this.writeJournal(journal);
393
+ if (this.indexService && typeof this.indexService.enqueue === 'function') this.indexService.enqueue(journal.projectId).catch(() => {});
394
+ return { ok: true, phase: 'completed', stateAdvanced: true };
395
+ }
396
+ return this.rollbackJournal(journal);
397
+ }
398
+
399
+ async recoverAll() {
400
+ const root = this.layout.getRuntimePath('promotions');
401
+ if (!fs.existsSync(root)) return [];
402
+ const files = [];
403
+ const walk = current => {
404
+ for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
405
+ const absolute = path.join(current, entry.name);
406
+ if (entry.isDirectory()) walk(absolute);
407
+ else if (entry.isFile() && entry.name.endsWith('.json')) files.push(absolute);
408
+ }
409
+ };
410
+ walk(root);
411
+ const results = [];
412
+ for (const file of files.sort()) {
413
+ const journal = this.atomic.readJsonStrict(file, {
414
+ category: 'promotion-journal',
415
+ validate: value => {
416
+ if (!value || value.schema !== JOURNAL_SCHEMA || !Array.isArray(value.operations)) throw new DomainError('DATA_CORRUPT', 'Promotion journal is corrupt.', { status: 500 });
417
+ return value;
418
+ },
419
+ });
420
+ results.push({ file, ...(await this.recoverJournal(journal)) });
421
+ }
422
+ return results;
423
+ }
424
+ }
425
+
426
+ module.exports = {
427
+ JOURNAL_SCHEMA,
428
+ MANIFEST_SCHEMA,
429
+ MAX_KNOWLEDGE_FILE_BYTES,
430
+ KnowledgePromotionService,
431
+ canonicalRelativePath,
432
+ fileHash,
433
+ hashBuffer,
434
+ validateMarkdown,
435
+ };