@sanqianx/project-knowledge 4.6.1 → 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.
- package/.agents/plugins/marketplace.json +17 -17
- package/.claude-plugin/marketplace.json +1 -1
- package/CHANGELOG.md +817 -780
- package/README.md +48 -36
- package/_modules/knowledge-engine/README.md +88 -0
- package/_modules/knowledge-engine/package.json +38 -0
- package/_modules/knowledge-engine/src/analysis-contract.js +25 -0
- package/_modules/knowledge-engine/src/analysis-worker.js +54 -0
- package/_modules/knowledge-engine/src/bin.js +52 -0
- package/_modules/knowledge-engine/src/bridge-client.js +24 -0
- package/_modules/knowledge-engine/src/coordinator.js +255 -0
- package/_modules/knowledge-engine/src/headless-analyzer.js +75 -0
- package/_modules/knowledge-engine/src/memories.js +85 -0
- package/_modules/knowledge-engine/src/migration.js +88 -0
- package/_modules/knowledge-engine/src/retrieval.js +116 -0
- package/_modules/knowledge-engine/src/runtime/README.md +7 -0
- package/_modules/knowledge-engine/src/runtime/atomic-file.js +204 -0
- package/_modules/knowledge-engine/src/runtime/automation-config.js +140 -0
- package/_modules/knowledge-engine/src/runtime/commit-conversation-binder.js +249 -0
- package/_modules/knowledge-engine/src/runtime/commit-processing-ledger.js +26 -0
- package/_modules/knowledge-engine/src/runtime/commit-prompt.js +202 -0
- package/_modules/knowledge-engine/src/runtime/commit-reconciler.js +432 -0
- package/_modules/knowledge-engine/src/runtime/content-hash.js +4 -0
- package/_modules/knowledge-engine/src/runtime/contracts.js +279 -0
- package/_modules/knowledge-engine/src/runtime/conversation-exclusions.js +204 -0
- package/_modules/knowledge-engine/src/runtime/conversation-store.js +387 -0
- package/_modules/knowledge-engine/src/runtime/evidence-bundle.js +228 -0
- package/_modules/knowledge-engine/src/runtime/git-runner.js +62 -0
- package/_modules/knowledge-engine/src/runtime/knowledge-promotion.js +444 -0
- package/_modules/knowledge-engine/src/runtime/knowledge-retrieval-service.js +402 -0
- package/_modules/knowledge-engine/src/runtime/layout.js +44 -0
- package/_modules/knowledge-engine/src/runtime/markdown-knowledge-indexer.js +230 -0
- package/_modules/knowledge-engine/src/runtime/scanner.js +298 -0
- package/_modules/knowledge-engine/src/server.js +155 -0
- package/_modules/knowledge-engine/src/store.js +239 -0
- package/_modules/knowledge-engine/ui/app.css +172 -0
- package/_modules/knowledge-engine/ui/app.js +297 -0
- package/_modules/knowledge-engine/ui/index.html +95 -0
- package/_site/README.md +30 -30
- package/_site/_test/ai-profile-resolver-test.js +137 -137
- package/_site/_test/automation-queue-test.js +14 -14
- package/_site/_test/automation-ui-test.js +40 -40
- package/_site/_test/background-task-registry-test.js +43 -43
- package/_site/_test/baseline-schema-test.js +91 -91
- package/_site/_test/bridge-adapter-test.js +1 -1
- package/_site/_test/bridge-consumer-service-test.js +1 -1
- package/_site/_test/chat-claudecodeui-match-test.js +92 -92
- package/_site/_test/claude-executable-discovery-test.js +62 -62
- package/_site/_test/claude-workbench-test.js +170 -170
- package/_site/_test/client-capture-restart-e2e-test.js +4 -4
- package/_site/_test/codex-conversation-projection-test.js +2 -1
- package/_site/_test/commit-boundary-freeze-test.js +1 -1
- package/_site/_test/commit-conversation-binding-test.js +95 -95
- package/_site/_test/commit-evidence-test.js +110 -110
- package/_site/_test/conversation-api-test.js +109 -109
- package/_site/_test/conversation-store-test.js +136 -136
- package/_site/_test/cross-repo-isolation-e2e-test.js +2 -2
- package/_site/_test/data-dir-migration-test.js +57 -57
- package/_site/_test/desktop-browser-compat-test.js +28 -28
- package/_site/_test/desktop-hook-runtime-regression-test.js +204 -204
- package/_site/_test/explicit-commit-processor-test.js +37 -37
- package/_site/_test/fixtures/make-git-repos.js +12 -1
- package/_site/_test/folder-picker-output-test.js +19 -19
- package/_site/_test/full-integration-e2e-test.js +165 -165
- package/_site/_test/git-validation-test.js +2 -2
- package/_site/_test/hook-runtime-endpoint-test.js +88 -88
- package/_site/_test/hook-status-repair-api-test.js +152 -152
- package/_site/_test/hook-trigger-test.js +85 -85
- package/_site/_test/import-preflight-api-test.js +234 -233
- package/_site/_test/index-writer-concurrency-test.js +135 -135
- package/_site/_test/integration-adapters-test.js +171 -171
- package/_site/_test/integration-capture-setup-test.js +1 -1
- package/_site/_test/integration-surface-coverage-test.js +164 -164
- package/_site/_test/knowledge-language-control-test.js +170 -170
- package/_site/_test/knowledge-promotion-recovery-test.js +260 -260
- package/_site/_test/knowledge-query-test.js +54 -54
- package/_site/_test/knowledge-retrieval-service-test.js +77 -77
- package/_site/_test/knowledge-storage-startup-test.js +52 -52
- package/_site/_test/knowledge-store-logs-supervision-test.js +89 -89
- package/_site/_test/legacy-forward-compat-test.js +257 -257
- package/_site/_test/legacy-project-upgrade-e2e-test.js +363 -363
- package/_site/_test/logging-api-test.js +89 -89
- package/_site/_test/logging-sse-no-gap-test.js +88 -88
- package/_site/_test/logging-ui-test.js +106 -104
- package/_site/_test/markdown-delta-overlay-test.js +85 -85
- package/_site/_test/markdown-maintenance-api-test.js +75 -75
- package/_site/_test/mcp-server-test.js +149 -149
- package/_site/_test/module-artifact-integrity-test.js +27 -0
- package/_site/_test/module-boundary-test.js +30 -0
- package/_site/_test/module-bridge-eventbridge-test.js +7 -0
- package/_site/_test/module-model-configuration-test.js +43 -0
- package/_site/_test/module-process-lifecycle-test.js +24 -0
- package/_site/_test/module-stream-proxy-test.js +34 -0
- package/_site/_test/non-release-ci-test.js +34 -34
- package/_site/_test/offline-boundary-isolation-test.js +32 -32
- package/_site/_test/p0-data-migration-characterization-test.js +37 -37
- package/_site/_test/p0-e2e-gate-test.js +347 -347
- package/_site/_test/packaged-ui-smoke-test.js +19 -10
- package/_site/_test/path-consistency-test.js +151 -151
- package/_site/_test/pending-sweep-test.js +9 -9
- package/_site/_test/post-commit-automation-test.js +87 -87
- package/_site/_test/product-diagnostics-ui-test.js +75 -0
- package/_site/_test/product-import-ui-test.js +92 -0
- package/_site/_test/project-delete-recovery-test.js +64 -64
- package/_site/_test/project-goal-editor-test.js +144 -144
- package/_site/_test/project-layout-v2-migration-test.js +127 -127
- package/_site/_test/project-lifecycle-transaction-test.js +106 -106
- package/_site/_test/prompt-settings-test.js +115 -115
- package/_site/_test/protected-architecture-gate-test.js +122 -122
- package/_site/_test/refactor-characterization-test.js +36 -36
- package/_site/_test/release-version-sync-test.js +1 -1
- package/_site/_test/requirement-recorder-test.js +173 -173
- package/_site/_test/run-all-tests.js +159 -156
- package/_site/_test/server-runtime-migration-safety-test.js +27 -27
- package/_site/_test/shared-contracts-test.js +58 -58
- package/_site/_test/startup-analysis-disabled-test.js +10 -10
- package/_site/_test/storage-foundation-test.js +78 -78
- package/_site/_test/structured-logger-test.js +101 -101
- package/_site/_test/tracking-start-test.js +108 -108
- package/_site/_test/v4122-upgrade-data-contract-test.js +56 -56
- package/_site/_test/workbench-permission-test.js +117 -117
- package/_site/_test/workspace-ui-contract-test.js +28 -43
- package/_site/lib/ai-profile-resolver.js +78 -78
- package/_site/lib/ai-workspace.js +101 -101
- package/_site/lib/automation-config.js +140 -140
- package/_site/lib/bridge-adapter.js +5 -1
- package/_site/lib/bridge-consumer-service.js +402 -402
- package/_site/lib/claude-cli-runner.js +1510 -1510
- package/_site/lib/commit-conversation-binder.js +234 -234
- package/_site/lib/commit-processing-ledger.js +27 -27
- package/_site/lib/commit-prompt.js +202 -202
- package/_site/lib/commit-reconciler.js +406 -406
- package/_site/lib/contracts.js +284 -279
- package/_site/lib/conversation-query-service.js +144 -144
- package/_site/lib/conversation-store.js +385 -385
- package/_site/lib/data-dir.js +74 -74
- package/_site/lib/data-state-classifier.js +40 -40
- package/_site/lib/engine-cutover.js +97 -0
- package/_site/lib/evidence-bundle.js +228 -228
- package/_site/lib/folder-picker-output.js +21 -21
- package/_site/lib/git-runner.js +62 -62
- package/_site/lib/github-team-store.js +1172 -1172
- package/_site/lib/hook-manager.js +203 -202
- package/_site/lib/index-service.js +173 -173
- package/_site/lib/integration-installer.js +907 -907
- package/_site/lib/kb-framework.js +176 -176
- package/_site/lib/kb-validator.js +124 -124
- package/_site/lib/knowledge-promotion.js +435 -435
- package/_site/lib/knowledge-retrieval-service.js +401 -401
- package/_site/lib/knowledge-tool-runtime.js +359 -359
- package/_site/lib/legacy-data-manifest.js +26 -26
- package/_site/lib/llm-client.js +161 -161
- package/_site/lib/markdown-knowledge-indexer.js +230 -230
- package/_site/lib/migration-service.js +442 -442
- package/_site/lib/module-bridge.js +198 -33
- package/_site/lib/post-commit-automation.js +87 -87
- package/_site/lib/project-lifecycle-service.js +519 -497
- package/_site/lib/project-store.js +268 -262
- package/_site/lib/requirement-recorder.js +278 -278
- package/_site/lib/runtime-endpoint.js +155 -155
- package/_site/lib/scanner.js +298 -298
- package/_site/lib/server-app.js +1590 -1520
- package/_site/lib/settings-store.js +133 -113
- package/_site/lib/storage-layout.js +162 -162
- package/_site/lib/structured-logger.js +574 -574
- package/_site/scripts/folder-picker.ps1 +156 -156
- package/_site/scripts/hook-trigger.js +157 -156
- package/_site/scripts/install-module-candidates.js +69 -0
- package/_site/scripts/pack-module-candidates.js +72 -0
- package/_site/scripts/sync-release-version.js +2 -2
- package/_site/scripts/vendor-modules.js +47 -0
- package/_site/scripts/verify-module-runtime.js +45 -0
- package/_site/scripts/verify-product-runtime.js +34 -0
- package/bin/project-knowledge-mcp.js +194 -194
- package/docs/README.zh-CN.md +34 -25
- package/docs/project-registry-schema.md +22 -22
- package/docs/testing-strategy.md +33 -33
- package/module-runtime-manifest.json +220 -0
- package/package.json +13 -10
- package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.mcp.json +1 -1
- package/plugins/project-knowledge/opencode/project-knowledge.md +3 -3
- package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -28
- package/templates/project-readme.md +34 -34
- package/ui/favicon.svg +38 -38
- package/ui/index.html +51 -149
- package/ui/product.css +9 -0
- package/ui/product.js +386 -0
- package/vendor-manifest.json +46 -0
- package/_site/_test/import-ui-flow-test.js +0 -171
- package/_site/_test/project-control-panel-task14-test.js +0 -63
- package/_site/_test/task15-20-ui-flow-test.js +0 -148
- package/_site/_test/ui-i18n-toggle-test.js +0 -114
- package/_site/_test/ui-smoke-test.js +0 -73
- package/ui/app.css +0 -58
- package/ui/app.js +0 -607
- package/ui/i18n.js +0 -146
|
@@ -1,175 +1,175 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const AtomicFile = require('./atomic-file');
|
|
4
|
-
const {
|
|
5
|
-
SCHEMAS,
|
|
6
|
-
DomainError,
|
|
7
|
-
createId,
|
|
8
|
-
validateProjectId,
|
|
9
|
-
validateTrigger,
|
|
10
|
-
} = require('./contracts');
|
|
11
|
-
const { StorageLayout } = require('./storage-layout');
|
|
12
|
-
const { ProjectRegistryStore } = require('./project-registry-store');
|
|
13
|
-
const { ProjectStore } = require('./project-store');
|
|
14
|
-
const { CommitScanner, validateSha } = require('./scanner');
|
|
15
|
-
const { ConversationStore } = require('./conversation-store');
|
|
16
|
-
const { CommitConversationBinder, snapshotRequirementRecords } = require('./commit-conversation-binder');
|
|
17
|
-
const { renderCommitPrompt, sha256 } = require('./commit-prompt');
|
|
18
|
-
const { KnowledgeRetrievalService } = require('./knowledge-retrieval-service');
|
|
19
|
-
const { manifestHash: calculateRetrievalManifestHash } = require('./knowledge-retrieval-service');
|
|
20
|
-
const { sha256: knowledgeContentHash } = require('./knowledge-schema');
|
|
21
|
-
const { resolveEffectiveAiProfile } = require('./ai-profile-resolver');
|
|
22
|
-
const { CommitProcessingLedger } = require('./commit-processing-ledger');
|
|
23
|
-
|
|
24
|
-
const explicitQueues = new Map();
|
|
25
|
-
|
|
26
|
-
function publicFailure(error, phase) {
|
|
27
|
-
return {
|
|
28
|
-
code: error && error.code || 'INVALID_ARGUMENT',
|
|
29
|
-
message: String(error && error.message || 'Commit reconciliation failed.'),
|
|
30
|
-
phase,
|
|
31
|
-
retryable: error && error.retryable === true,
|
|
32
|
-
ts: new Date().toISOString(),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function validateClaim(claim, projectId, commitSha) {
|
|
37
|
-
if (!claim || typeof claim !== 'object' || claim.schema !== SCHEMAS.commitClaim) {
|
|
38
|
-
throw new DomainError('DATA_CORRUPT', 'Active commit claim is corrupt.', { status: 500 });
|
|
39
|
-
}
|
|
40
|
-
if (claim.projectId !== projectId || claim.commitSha !== commitSha) {
|
|
41
|
-
throw new DomainError('DATA_CORRUPT', 'Active commit claim identity does not match the pending commit.', { status: 500 });
|
|
42
|
-
}
|
|
43
|
-
if (!Array.isArray(claim.requirementIds) || !claim.patchHash || !claim.promptHash || !claim.evidenceHash
|
|
44
|
-
|| !claim.evidenceManifestHash || !claim.retrievalManifestHash || !claim.knowledgePath || !claim.runId) {
|
|
45
|
-
throw new DomainError('DATA_CORRUPT', 'Active commit claim is incomplete.', { status: 500 });
|
|
46
|
-
}
|
|
47
|
-
return claim;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function claimFingerprint(claim) {
|
|
51
|
-
return sha256(JSON.stringify({
|
|
52
|
-
schema: claim.schema,
|
|
53
|
-
projectId: claim.projectId,
|
|
54
|
-
commitSha: claim.commitSha,
|
|
55
|
-
parents: claim.parents,
|
|
56
|
-
requirementIds: claim.requirementIds,
|
|
57
|
-
requirementBinding: claim.requirementBinding,
|
|
58
|
-
conversationSnapshotHash: claim.conversationSnapshotHash || '',
|
|
59
|
-
promptTemplateVersion: claim.promptTemplateVersion,
|
|
60
|
-
promptHash: claim.promptHash,
|
|
61
|
-
patchHash: claim.patchHash,
|
|
62
|
-
evidenceHash: claim.evidenceHash,
|
|
63
|
-
evidenceManifestHash: claim.evidenceManifestHash,
|
|
64
|
-
retrievalManifestHash: claim.retrievalManifestHash,
|
|
65
|
-
knowledgePath: claim.knowledgePath,
|
|
66
|
-
}));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
class CommitClaimStore {
|
|
70
|
-
constructor(options = {}) {
|
|
71
|
-
this.layout = options.layout || new StorageLayout(options);
|
|
72
|
-
this.atomic = options.atomic || AtomicFile;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
filePath(projectId, commitSha) {
|
|
76
|
-
return this.layout.getRuntimePath('claims', validateProjectId(projectId), `${validateSha(commitSha)}.json`);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
write(projectId, commitSha, snapshot) {
|
|
80
|
-
const file = this.filePath(projectId, commitSha);
|
|
81
|
-
this.atomic.writeJsonAtomic(file, { schema: 'commit-claim-snapshot/v1', ...snapshot });
|
|
82
|
-
return file;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
read(projectId, commitSha) {
|
|
86
|
-
return this.atomic.readJsonStrict(this.filePath(projectId, commitSha), {
|
|
87
|
-
category: 'commit-claim-snapshot',
|
|
88
|
-
validate: snapshot => {
|
|
89
|
-
if (!snapshot || snapshot.schema !== 'commit-claim-snapshot/v1' || snapshot.projectId !== projectId || snapshot.commitSha !== commitSha) {
|
|
90
|
-
throw new DomainError('DATA_CORRUPT', 'Commit claim snapshot is corrupt.', { status: 500 });
|
|
91
|
-
}
|
|
92
|
-
return snapshot;
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
remove(projectId, commitSha) {
|
|
98
|
-
const file = this.filePath(projectId, commitSha);
|
|
99
|
-
try { fs.unlinkSync(file); } catch (error) { if (!error || error.code !== 'ENOENT') throw error; }
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
class CommitReconciler {
|
|
104
|
-
constructor(options = {}) {
|
|
105
|
-
this.layout = options.layout || new StorageLayout(options);
|
|
106
|
-
this.registryStore = options.registryStore || new ProjectRegistryStore({ layout: this.layout });
|
|
107
|
-
this.projectStore = options.projectStore || new ProjectStore({ layout: this.layout });
|
|
108
|
-
this.scanner = options.scanner || new CommitScanner(options);
|
|
109
|
-
this.conversationStore = options.conversationStore || new ConversationStore({
|
|
110
|
-
layout: this.layout,
|
|
111
|
-
projectStore: this.projectStore,
|
|
112
|
-
logger: options.logger,
|
|
113
|
-
});
|
|
114
|
-
this.binder = options.conversationBinder || new CommitConversationBinder({
|
|
115
|
-
layout: this.layout,
|
|
116
|
-
projectStore: this.projectStore,
|
|
117
|
-
conversationStore: this.conversationStore,
|
|
118
|
-
logger: options.logger,
|
|
119
|
-
});
|
|
120
|
-
this.retrievalService = options.retrievalService || new KnowledgeRetrievalService({
|
|
121
|
-
layout: this.layout,
|
|
122
|
-
registryStore: this.registryStore,
|
|
123
|
-
projectStore: this.projectStore,
|
|
124
|
-
databaseProvider: options.databaseProvider,
|
|
125
|
-
embedderProvider: options.embedderProvider,
|
|
126
|
-
logger: options.logger,
|
|
127
|
-
});
|
|
128
|
-
this.claimStore = options.claimStore || new CommitClaimStore({ layout: this.layout });
|
|
129
|
-
this.processingLedger = options.processingLedger || new CommitProcessingLedger({ layout: this.layout });
|
|
130
|
-
this.claimProcessor = options.claimProcessor || null;
|
|
131
|
-
this.logger = options.logger || null;
|
|
132
|
-
this.settingsStore = options.settingsStore || null;
|
|
133
|
-
this.batchSize = Math.max(1, Number(options.batchSize || 200));
|
|
134
|
-
this.requireAiProfile = options.requireAiProfile !== false;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async log(level, event, message, context) {
|
|
138
|
-
if (this.logger && typeof this.logger[level] === 'function') await this.logger[level](event, message, context);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async reconcile(projectId, trigger, context = {}) {
|
|
142
|
-
validateProjectId(projectId);
|
|
143
|
-
validateTrigger(trigger);
|
|
144
|
-
if (!context.commitSha) throw new DomainError('INVALID_ARGUMENT', 'Commit reconciliation requires an explicit Hook commit SHA.');
|
|
145
|
-
return this.processCommitEvent({ projectId, commitSha: context.commitSha, branch: context.branch || '', operationId: context.operationId || createId('op') });
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async processCommitEvent({ projectId, commitSha, branch = '', operationId = '' } = {}) {
|
|
149
|
-
validateProjectId(projectId);
|
|
150
|
-
validateSha(commitSha);
|
|
151
|
-
const existingQueue = explicitQueues.get(projectId) || { tail: Promise.resolve(), pending: new Map() };
|
|
152
|
-
explicitQueues.set(projectId, existingQueue);
|
|
153
|
-
if (existingQueue.pending.has(commitSha)) return existingQueue.pending.get(commitSha);
|
|
154
|
-
const queued = existingQueue.tail.catch(() => {}).then(async () => {
|
|
155
|
-
const completed = this.processingLedger.read(projectId, commitSha);
|
|
156
|
-
if (completed) return { ok: true, projectId, commitSha, status: 'already-completed', processed: [], ledger: completed };
|
|
157
|
-
const config = this.projectStore.readConfig(projectId);
|
|
158
|
-
if (config.enabled === false) return { ok: true, projectId, commitSha, status: 'disabled', processed: [] };
|
|
159
|
-
const result = await this.processCommit(projectId, 'git-hook', operationId || createId('op'), config, branch, commitSha);
|
|
160
|
-
if (!result.ok) return { ok: false, projectId, commitSha, status: 'failed', processed: [result], error: result.error };
|
|
161
|
-
const ledger = this.processingLedger.complete(projectId, commitSha, { runId: result.runId, claimFingerprint: result.claimFingerprint || '' });
|
|
162
|
-
return { ok: true, projectId, commitSha, status: 'completed', processed: [result], ledger };
|
|
163
|
-
});
|
|
164
|
-
existingQueue.pending.set(commitSha, queued);
|
|
165
|
-
existingQueue.tail = queued.catch(() => {});
|
|
166
|
-
try { return await queued; }
|
|
167
|
-
finally {
|
|
168
|
-
existingQueue.pending.delete(commitSha);
|
|
169
|
-
if (!existingQueue.pending.size) explicitQueues.delete(projectId);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const AtomicFile = require('./atomic-file');
|
|
4
|
+
const {
|
|
5
|
+
SCHEMAS,
|
|
6
|
+
DomainError,
|
|
7
|
+
createId,
|
|
8
|
+
validateProjectId,
|
|
9
|
+
validateTrigger,
|
|
10
|
+
} = require('./contracts');
|
|
11
|
+
const { StorageLayout } = require('./storage-layout');
|
|
12
|
+
const { ProjectRegistryStore } = require('./project-registry-store');
|
|
13
|
+
const { ProjectStore } = require('./project-store');
|
|
14
|
+
const { CommitScanner, validateSha } = require('./scanner');
|
|
15
|
+
const { ConversationStore } = require('./conversation-store');
|
|
16
|
+
const { CommitConversationBinder, snapshotRequirementRecords } = require('./commit-conversation-binder');
|
|
17
|
+
const { renderCommitPrompt, sha256 } = require('./commit-prompt');
|
|
18
|
+
const { KnowledgeRetrievalService } = require('./knowledge-retrieval-service');
|
|
19
|
+
const { manifestHash: calculateRetrievalManifestHash } = require('./knowledge-retrieval-service');
|
|
20
|
+
const { sha256: knowledgeContentHash } = require('./knowledge-schema');
|
|
21
|
+
const { resolveEffectiveAiProfile } = require('./ai-profile-resolver');
|
|
22
|
+
const { CommitProcessingLedger } = require('./commit-processing-ledger');
|
|
23
|
+
|
|
24
|
+
const explicitQueues = new Map();
|
|
25
|
+
|
|
26
|
+
function publicFailure(error, phase) {
|
|
27
|
+
return {
|
|
28
|
+
code: error && error.code || 'INVALID_ARGUMENT',
|
|
29
|
+
message: String(error && error.message || 'Commit reconciliation failed.'),
|
|
30
|
+
phase,
|
|
31
|
+
retryable: error && error.retryable === true,
|
|
32
|
+
ts: new Date().toISOString(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function validateClaim(claim, projectId, commitSha) {
|
|
37
|
+
if (!claim || typeof claim !== 'object' || claim.schema !== SCHEMAS.commitClaim) {
|
|
38
|
+
throw new DomainError('DATA_CORRUPT', 'Active commit claim is corrupt.', { status: 500 });
|
|
39
|
+
}
|
|
40
|
+
if (claim.projectId !== projectId || claim.commitSha !== commitSha) {
|
|
41
|
+
throw new DomainError('DATA_CORRUPT', 'Active commit claim identity does not match the pending commit.', { status: 500 });
|
|
42
|
+
}
|
|
43
|
+
if (!Array.isArray(claim.requirementIds) || !claim.patchHash || !claim.promptHash || !claim.evidenceHash
|
|
44
|
+
|| !claim.evidenceManifestHash || !claim.retrievalManifestHash || !claim.knowledgePath || !claim.runId) {
|
|
45
|
+
throw new DomainError('DATA_CORRUPT', 'Active commit claim is incomplete.', { status: 500 });
|
|
46
|
+
}
|
|
47
|
+
return claim;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function claimFingerprint(claim) {
|
|
51
|
+
return sha256(JSON.stringify({
|
|
52
|
+
schema: claim.schema,
|
|
53
|
+
projectId: claim.projectId,
|
|
54
|
+
commitSha: claim.commitSha,
|
|
55
|
+
parents: claim.parents,
|
|
56
|
+
requirementIds: claim.requirementIds,
|
|
57
|
+
requirementBinding: claim.requirementBinding,
|
|
58
|
+
conversationSnapshotHash: claim.conversationSnapshotHash || '',
|
|
59
|
+
promptTemplateVersion: claim.promptTemplateVersion,
|
|
60
|
+
promptHash: claim.promptHash,
|
|
61
|
+
patchHash: claim.patchHash,
|
|
62
|
+
evidenceHash: claim.evidenceHash,
|
|
63
|
+
evidenceManifestHash: claim.evidenceManifestHash,
|
|
64
|
+
retrievalManifestHash: claim.retrievalManifestHash,
|
|
65
|
+
knowledgePath: claim.knowledgePath,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
class CommitClaimStore {
|
|
70
|
+
constructor(options = {}) {
|
|
71
|
+
this.layout = options.layout || new StorageLayout(options);
|
|
72
|
+
this.atomic = options.atomic || AtomicFile;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
filePath(projectId, commitSha) {
|
|
76
|
+
return this.layout.getRuntimePath('claims', validateProjectId(projectId), `${validateSha(commitSha)}.json`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
write(projectId, commitSha, snapshot) {
|
|
80
|
+
const file = this.filePath(projectId, commitSha);
|
|
81
|
+
this.atomic.writeJsonAtomic(file, { schema: 'commit-claim-snapshot/v1', ...snapshot });
|
|
82
|
+
return file;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
read(projectId, commitSha) {
|
|
86
|
+
return this.atomic.readJsonStrict(this.filePath(projectId, commitSha), {
|
|
87
|
+
category: 'commit-claim-snapshot',
|
|
88
|
+
validate: snapshot => {
|
|
89
|
+
if (!snapshot || snapshot.schema !== 'commit-claim-snapshot/v1' || snapshot.projectId !== projectId || snapshot.commitSha !== commitSha) {
|
|
90
|
+
throw new DomainError('DATA_CORRUPT', 'Commit claim snapshot is corrupt.', { status: 500 });
|
|
91
|
+
}
|
|
92
|
+
return snapshot;
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
remove(projectId, commitSha) {
|
|
98
|
+
const file = this.filePath(projectId, commitSha);
|
|
99
|
+
try { fs.unlinkSync(file); } catch (error) { if (!error || error.code !== 'ENOENT') throw error; }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class CommitReconciler {
|
|
104
|
+
constructor(options = {}) {
|
|
105
|
+
this.layout = options.layout || new StorageLayout(options);
|
|
106
|
+
this.registryStore = options.registryStore || new ProjectRegistryStore({ layout: this.layout });
|
|
107
|
+
this.projectStore = options.projectStore || new ProjectStore({ layout: this.layout });
|
|
108
|
+
this.scanner = options.scanner || new CommitScanner(options);
|
|
109
|
+
this.conversationStore = options.conversationStore || new ConversationStore({
|
|
110
|
+
layout: this.layout,
|
|
111
|
+
projectStore: this.projectStore,
|
|
112
|
+
logger: options.logger,
|
|
113
|
+
});
|
|
114
|
+
this.binder = options.conversationBinder || new CommitConversationBinder({
|
|
115
|
+
layout: this.layout,
|
|
116
|
+
projectStore: this.projectStore,
|
|
117
|
+
conversationStore: this.conversationStore,
|
|
118
|
+
logger: options.logger,
|
|
119
|
+
});
|
|
120
|
+
this.retrievalService = options.retrievalService || new KnowledgeRetrievalService({
|
|
121
|
+
layout: this.layout,
|
|
122
|
+
registryStore: this.registryStore,
|
|
123
|
+
projectStore: this.projectStore,
|
|
124
|
+
databaseProvider: options.databaseProvider,
|
|
125
|
+
embedderProvider: options.embedderProvider,
|
|
126
|
+
logger: options.logger,
|
|
127
|
+
});
|
|
128
|
+
this.claimStore = options.claimStore || new CommitClaimStore({ layout: this.layout });
|
|
129
|
+
this.processingLedger = options.processingLedger || new CommitProcessingLedger({ layout: this.layout });
|
|
130
|
+
this.claimProcessor = options.claimProcessor || null;
|
|
131
|
+
this.logger = options.logger || null;
|
|
132
|
+
this.settingsStore = options.settingsStore || null;
|
|
133
|
+
this.batchSize = Math.max(1, Number(options.batchSize || 200));
|
|
134
|
+
this.requireAiProfile = options.requireAiProfile !== false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async log(level, event, message, context) {
|
|
138
|
+
if (this.logger && typeof this.logger[level] === 'function') await this.logger[level](event, message, context);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async reconcile(projectId, trigger, context = {}) {
|
|
142
|
+
validateProjectId(projectId);
|
|
143
|
+
validateTrigger(trigger);
|
|
144
|
+
if (!context.commitSha) throw new DomainError('INVALID_ARGUMENT', 'Commit reconciliation requires an explicit Hook commit SHA.');
|
|
145
|
+
return this.processCommitEvent({ projectId, commitSha: context.commitSha, branch: context.branch || '', operationId: context.operationId || createId('op') });
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async processCommitEvent({ projectId, commitSha, branch = '', operationId = '' } = {}) {
|
|
149
|
+
validateProjectId(projectId);
|
|
150
|
+
validateSha(commitSha);
|
|
151
|
+
const existingQueue = explicitQueues.get(projectId) || { tail: Promise.resolve(), pending: new Map() };
|
|
152
|
+
explicitQueues.set(projectId, existingQueue);
|
|
153
|
+
if (existingQueue.pending.has(commitSha)) return existingQueue.pending.get(commitSha);
|
|
154
|
+
const queued = existingQueue.tail.catch(() => {}).then(async () => {
|
|
155
|
+
const completed = this.processingLedger.read(projectId, commitSha);
|
|
156
|
+
if (completed) return { ok: true, projectId, commitSha, status: 'already-completed', processed: [], ledger: completed };
|
|
157
|
+
const config = this.projectStore.readConfig(projectId);
|
|
158
|
+
if (config.enabled === false) return { ok: true, projectId, commitSha, status: 'disabled', processed: [] };
|
|
159
|
+
const result = await this.processCommit(projectId, 'git-hook', operationId || createId('op'), config, branch, commitSha);
|
|
160
|
+
if (!result.ok) return { ok: false, projectId, commitSha, status: 'failed', processed: [result], error: result.error };
|
|
161
|
+
const ledger = this.processingLedger.complete(projectId, commitSha, { runId: result.runId, claimFingerprint: result.claimFingerprint || '' });
|
|
162
|
+
return { ok: true, projectId, commitSha, status: 'completed', processed: [result], ledger };
|
|
163
|
+
});
|
|
164
|
+
existingQueue.pending.set(commitSha, queued);
|
|
165
|
+
existingQueue.tail = queued.catch(() => {});
|
|
166
|
+
try { return await queued; }
|
|
167
|
+
finally {
|
|
168
|
+
existingQueue.pending.delete(commitSha);
|
|
169
|
+
if (!existingQueue.pending.size) explicitQueues.delete(projectId);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
173
|
async prepareClaim(projectId, trigger, operationId, config, branch, commitSha) {
|
|
174
174
|
const state = this.projectStore.readState(projectId);
|
|
175
175
|
if (state.analysis.activeClaim) {
|
|
@@ -208,237 +208,237 @@ class CommitReconciler {
|
|
|
208
208
|
return this.prepareClaim(projectId, trigger, operationId, config, branch, commitSha);
|
|
209
209
|
}
|
|
210
210
|
const claim = validateClaim(activeClaim, projectId, commitSha);
|
|
211
|
-
const snapshot = this.claimStore.read(projectId, commitSha);
|
|
212
|
-
if (snapshot.claimFingerprint !== claimFingerprint(claim)
|
|
213
|
-
|| snapshot.evidence.patchHash !== claim.patchHash
|
|
214
|
-
|| snapshot.evidence.evidenceHash !== claim.evidenceHash
|
|
215
|
-
|| !snapshot.evidence.evidenceBundle
|
|
216
|
-
|| snapshot.evidence.evidenceBundle.manifestHash !== claim.evidenceManifestHash
|
|
217
|
-
|| !snapshot.existingKnowledge || snapshot.existingKnowledge.manifestHash !== claim.retrievalManifestHash
|
|
218
|
-
|| sha256(snapshot.prompt) !== claim.promptHash
|
|
219
|
-
|| (claim.conversationSnapshotHash && (!snapshot.conversationSnapshot || snapshot.conversationSnapshot.snapshotHash !== claim.conversationSnapshotHash))) {
|
|
220
|
-
throw new DomainError('DATA_CORRUPT', 'Frozen commit claim snapshot does not match active state.', { status: 500 });
|
|
221
|
-
}
|
|
222
|
-
this.scanner.verifyEvidence(snapshot.evidence);
|
|
223
|
-
const retrievalManifest = this.claimStore.atomic.readJsonStrict(snapshot.existingKnowledge.manifestPath, {
|
|
224
|
-
category: 'knowledge-retrieval-manifest',
|
|
225
|
-
validate: value => {
|
|
226
|
-
if (!value || value.schema !== 'knowledge-retrieval-manifest/v1'
|
|
227
|
-
|| value.manifestHash !== claim.retrievalManifestHash
|
|
228
|
-
|| calculateRetrievalManifestHash(value) !== value.manifestHash) {
|
|
229
|
-
throw new DomainError('DATA_CORRUPT', 'Frozen knowledge retrieval manifest is corrupt.', { status: 500 });
|
|
230
|
-
}
|
|
231
|
-
return value;
|
|
232
|
-
},
|
|
233
|
-
});
|
|
234
|
-
const selectedById = new Map(retrievalManifest.selected.map(item => [`${item.spaceId}:${item.chunkId}`, item]));
|
|
235
|
-
for (const entry of snapshot.existingKnowledge.entries || []) {
|
|
236
|
-
const selected = selectedById.get(`${entry.projectId === projectId ? `project:${projectId}` : `project:${entry.projectId}`}:${entry.chunkId}`);
|
|
237
|
-
if (!selected || knowledgeContentHash(entry.content) !== entry.hash || selected.contentHash !== entry.hash) {
|
|
238
|
-
throw new DomainError('DATA_CORRUPT', 'Frozen authoritative Markdown context does not match its retrieval manifest.', { status: 500 });
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
claim.attempt = Number(claim.attempt || 1) + 1;
|
|
242
|
-
claim.phase = 'evidence.prepared';
|
|
243
|
-
await this.projectStore.updateState(projectId, draft => {
|
|
244
|
-
draft.analysis.activeClaim = claim;
|
|
245
|
-
draft.analysis.status = 'evidence.prepared';
|
|
246
|
-
draft.analysis.lastError = null;
|
|
247
|
-
});
|
|
248
|
-
return {
|
|
249
|
-
claim,
|
|
250
|
-
evidence: snapshot.evidence,
|
|
251
|
-
prompt: snapshot.prompt,
|
|
252
|
-
requirements: snapshot.requirements || [],
|
|
253
|
-
conversationSnapshot: snapshot.conversationSnapshot || null,
|
|
254
|
-
existingKnowledge: snapshot.existingKnowledge,
|
|
255
|
-
retry: true,
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const runId = createId('run');
|
|
260
|
-
const evidenceRoot = this.layout.getRuntimePath('runs', projectId, runId, 'input', 'evidence');
|
|
261
|
-
const evidence = await this.scanner.collectEvidence(config, commitSha, { branch, evidenceRoot });
|
|
262
|
-
this.scanner.verifyEvidence(evidence);
|
|
263
|
-
const conversationSnapshot = await this.binder.bind({ projectId, commitSha, branch });
|
|
264
|
-
const requirements = snapshotRequirementRecords(conversationSnapshot);
|
|
265
|
-
const existingKnowledge = await this.retrievalService.retrieveForCommit({
|
|
266
|
-
projectId,
|
|
267
|
-
conversationSnapshot,
|
|
268
|
-
commitEvidence: evidence,
|
|
269
|
-
});
|
|
270
|
-
existingKnowledge.manifestPath = this.layout.getRuntimePath('runs', projectId, runId, 'input', 'retrieval', 'manifest.json');
|
|
271
|
-
this.claimStore.atomic.writeJsonAtomic(existingKnowledge.manifestPath, existingKnowledge.manifest);
|
|
272
|
-
const rendered = renderCommitPrompt({ projectId, config, evidence, requirements, conversationSnapshot, existingKnowledge });
|
|
273
|
-
const claim = {
|
|
274
|
-
schema: SCHEMAS.commitClaim,
|
|
275
|
-
projectId,
|
|
276
|
-
commitSha,
|
|
277
|
-
parents: evidence.parents,
|
|
278
|
-
triggerFirstSeen: trigger,
|
|
279
|
-
requirementIds: requirements.map(requirement => requirement.id),
|
|
280
|
-
requirementBinding: conversationSnapshot.status,
|
|
281
|
-
conversationSnapshotHash: conversationSnapshot.snapshotHash,
|
|
282
|
-
conversationTurnIds: conversationSnapshot.turns.map(turn => turn.turnId),
|
|
283
|
-
conversationEventIds: conversationSnapshot.turns.flatMap(turn => [...turn.userEvents, ...turn.assistantEvents].map(event => event.eventId)),
|
|
284
|
-
promptTemplateVersion: rendered.promptTemplateVersion,
|
|
285
|
-
promptHash: rendered.promptHash,
|
|
286
|
-
patchHash: evidence.patchHash,
|
|
287
|
-
evidenceHash: evidence.evidenceHash,
|
|
288
|
-
evidenceManifestHash: evidence.evidenceBundle.manifestHash,
|
|
289
|
-
retrievalManifestHash: existingKnowledge.manifestHash,
|
|
290
|
-
knowledgePath: config.knowledgePath,
|
|
291
|
-
runId,
|
|
292
|
-
operationId,
|
|
293
|
-
phase: 'evidence.prepared',
|
|
294
|
-
attempt: 1,
|
|
295
|
-
};
|
|
296
|
-
claim.fingerprint = claimFingerprint(claim);
|
|
297
|
-
this.claimStore.write(projectId, commitSha, {
|
|
298
|
-
projectId,
|
|
299
|
-
commitSha,
|
|
300
|
-
claimFingerprint: claim.fingerprint,
|
|
301
|
-
evidence,
|
|
302
|
-
prompt: rendered.prompt,
|
|
303
|
-
requirements,
|
|
304
|
-
conversationSnapshot,
|
|
305
|
-
existingKnowledge,
|
|
306
|
-
createdAt: new Date().toISOString(),
|
|
307
|
-
});
|
|
308
|
-
try {
|
|
309
|
-
await this.projectStore.updateState(projectId, draft => {
|
|
310
|
-
if (draft.analysis.activeClaim) throw new DomainError('PROJECT_BUSY', 'Another active claim already exists.', { status: 409, retryable: true });
|
|
311
|
-
draft.analysis.activeClaim = claim;
|
|
312
|
-
draft.analysis.status = 'evidence.prepared';
|
|
313
|
-
draft.analysis.lastError = null;
|
|
314
|
-
});
|
|
315
|
-
} catch (error) {
|
|
316
|
-
this.claimStore.remove(projectId, commitSha);
|
|
317
|
-
throw error;
|
|
318
|
-
}
|
|
319
|
-
await this.log('info', 'reconcile.claim_prepared', 'Commit evidence and claim prepared.', {
|
|
320
|
-
projectId,
|
|
321
|
-
operationId,
|
|
322
|
-
runId: claim.runId,
|
|
323
|
-
commitSha,
|
|
324
|
-
phase: claim.phase,
|
|
325
|
-
promptHash: claim.promptHash,
|
|
326
|
-
patchHash: claim.patchHash,
|
|
327
|
-
patchBytes: evidence.patchBytes,
|
|
328
|
-
evidenceManifestHash: claim.evidenceManifestHash,
|
|
329
|
-
evidenceChunkCount: evidence.evidenceBundle.chunkCount,
|
|
330
|
-
evidenceChunkBytes: evidence.evidenceBundle.chunkBytes,
|
|
331
|
-
retrievalManifestHash: claim.retrievalManifestHash,
|
|
332
|
-
retrievalSelectedCount: existingKnowledge.entries.length,
|
|
333
|
-
retrievalBytes: existingKnowledge.totalBytes,
|
|
334
|
-
fileCount: evidence.files.length,
|
|
335
|
-
requirementBinding: claim.requirementBinding,
|
|
336
|
-
requirementCount: claim.requirementIds.length,
|
|
337
|
-
});
|
|
338
|
-
return { claim, evidence, prompt: rendered.prompt, requirements, conversationSnapshot, existingKnowledge, retry: false };
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
async processCommit(projectId, trigger, operationId, config, branch, commitSha) {
|
|
342
|
-
let prepared;
|
|
343
|
-
try {
|
|
344
|
-
prepared = await this.prepareClaim(projectId, trigger, operationId, config, branch, commitSha);
|
|
345
|
-
if (this.requireAiProfile) {
|
|
346
|
-
// Use the shared effective-profile resolver so a project with no explicit
|
|
347
|
-
// aiProfileId can still analyze via the global default or first-usable
|
|
348
|
-
// profile. The resolver throws AI_PROFILE_REQUIRED if nothing usable
|
|
349
|
-
// exists, which becomes a clear actionable error rather than a silent
|
|
350
|
-
// null deref.
|
|
351
|
-
resolveEffectiveAiProfile(this.settingsStore ? this.settingsStore.read() : null, config);
|
|
352
|
-
}
|
|
353
|
-
if (!this.claimProcessor || typeof this.claimProcessor.processClaim !== 'function') {
|
|
354
|
-
throw new DomainError('INVALID_ARGUMENT', 'Knowledge claim processor is unavailable.', { status: 503, retryable: true });
|
|
355
|
-
}
|
|
356
|
-
const result = await this.claimProcessor.processClaim({
|
|
357
|
-
projectId,
|
|
358
|
-
config,
|
|
359
|
-
claim: prepared.claim,
|
|
360
|
-
evidence: prepared.evidence,
|
|
361
|
-
prompt: prepared.prompt,
|
|
362
|
-
requirements: prepared.requirements,
|
|
363
|
-
conversationSnapshot: prepared.conversationSnapshot,
|
|
364
|
-
existingKnowledge: prepared.existingKnowledge,
|
|
365
|
-
operationId,
|
|
366
|
-
});
|
|
367
|
-
if (!result || result.stateAdvanced !== true) {
|
|
368
|
-
throw new DomainError('INVALID_ARGUMENT', 'Knowledge promotion did not prove state advancement.', { status: 500, retryable: true });
|
|
369
|
-
}
|
|
370
|
-
const advanced = this.projectStore.readState(projectId);
|
|
371
|
-
if (advanced.lastAnalyzedCommit !== commitSha || advanced.analysis.activeClaim) {
|
|
372
|
-
throw new DomainError('DATA_CORRUPT', 'Claim processor reported success without atomically advancing project state.', { status: 500 });
|
|
373
|
-
}
|
|
374
|
-
this.claimStore.remove(projectId, commitSha);
|
|
375
|
-
await this.log('info', 'reconcile.commit_completed', 'Commit knowledge was promoted and state advanced.', {
|
|
376
|
-
projectId,
|
|
377
|
-
operationId,
|
|
378
|
-
runId: prepared.claim.runId,
|
|
379
|
-
commitSha,
|
|
380
|
-
phase: 'state.advanced',
|
|
381
|
-
});
|
|
382
|
-
return { ok: true, commitSha, runId: prepared.claim.runId, claimFingerprint: prepared.claim.fingerprint, retry: prepared.retry };
|
|
383
|
-
} catch (error) {
|
|
384
|
-
const failure = publicFailure(error, prepared && prepared.claim && prepared.claim.phase || 'evidence.prepared');
|
|
385
|
-
let stateUpdateError = null;
|
|
386
|
-
try {
|
|
387
|
-
await this.projectStore.updateState(projectId, draft => {
|
|
388
|
-
const active = draft.analysis.activeClaim;
|
|
389
|
-
if (active && active.commitSha === commitSha) {
|
|
390
|
-
active.phase = 'failed';
|
|
391
|
-
active.error = failure;
|
|
392
|
-
}
|
|
393
|
-
draft.analysis.status = 'failed';
|
|
394
|
-
draft.analysis.lastError = failure;
|
|
395
|
-
});
|
|
396
|
-
} catch (updateError) {
|
|
397
|
-
stateUpdateError = updateError;
|
|
398
|
-
}
|
|
399
|
-
await this.log('error', 'reconcile.commit_failed', 'Commit reconciliation stopped at the first failed commit.', {
|
|
400
|
-
projectId,
|
|
401
|
-
operationId,
|
|
402
|
-
runId: prepared && prepared.claim && prepared.claim.runId,
|
|
403
|
-
commitSha,
|
|
404
|
-
phase: failure.phase,
|
|
405
|
-
error,
|
|
406
|
-
context: stateUpdateError ? { failureStateWriteError: stateUpdateError } : {},
|
|
407
|
-
});
|
|
408
|
-
return { ok: false, commitSha, error: failure, retry: prepared && prepared.retry || false };
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
async markDiverged(projectId, operationId, message, processed = []) {
|
|
413
|
-
const error = new DomainError('HISTORY_DIVERGED', message || 'Git history diverged from the stored baseline.', { status: 409 });
|
|
414
|
-
await this.projectStore.updateState(projectId, state => {
|
|
415
|
-
state.analysis.status = 'history-diverged';
|
|
416
|
-
state.analysis.lastError = publicFailure(error, 'history-diverged');
|
|
417
|
-
});
|
|
418
|
-
await this.log('error', 'reconcile.history_diverged', 'Git history diverged; reconciliation stopped without moving pointers.', { projectId, operationId, phase: 'history-diverged', error });
|
|
419
|
-
return { ok: false, status: 'history-diverged', processed, error: publicFailure(error, 'history-diverged') };
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
async failWithoutClaim(projectId, operationId, error, phase, processed = []) {
|
|
423
|
-
await this.projectStore.updateState(projectId, state => {
|
|
424
|
-
state.analysis.status = 'failed';
|
|
425
|
-
state.analysis.lastError = publicFailure(error, phase);
|
|
426
|
-
});
|
|
427
|
-
await this.log('error', 'reconcile.failed', 'Reconciliation failed before claim processing.', { projectId, operationId, phase, error });
|
|
428
|
-
return { ok: false, status: 'failed', processed, error: publicFailure(error, phase) };
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
async function reconcileProjectCommits(projectId, trigger, deps = {}) {
|
|
433
|
-
const reconciler = deps.reconciler instanceof CommitReconciler ? deps.reconciler : new CommitReconciler(deps);
|
|
434
|
-
return reconciler.reconcile(projectId, trigger, { operationId: deps.operationId || '', commitSha: deps.commitSha || '', branch: deps.branch || '' });
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
module.exports = {
|
|
438
|
-
CommitClaimStore,
|
|
439
|
-
CommitReconciler,
|
|
440
|
-
CommitProcessingLedger,
|
|
441
|
-
claimFingerprint,
|
|
442
|
-
reconcileProjectCommits,
|
|
443
|
-
validateClaim,
|
|
444
|
-
};
|
|
211
|
+
const snapshot = this.claimStore.read(projectId, commitSha);
|
|
212
|
+
if (snapshot.claimFingerprint !== claimFingerprint(claim)
|
|
213
|
+
|| snapshot.evidence.patchHash !== claim.patchHash
|
|
214
|
+
|| snapshot.evidence.evidenceHash !== claim.evidenceHash
|
|
215
|
+
|| !snapshot.evidence.evidenceBundle
|
|
216
|
+
|| snapshot.evidence.evidenceBundle.manifestHash !== claim.evidenceManifestHash
|
|
217
|
+
|| !snapshot.existingKnowledge || snapshot.existingKnowledge.manifestHash !== claim.retrievalManifestHash
|
|
218
|
+
|| sha256(snapshot.prompt) !== claim.promptHash
|
|
219
|
+
|| (claim.conversationSnapshotHash && (!snapshot.conversationSnapshot || snapshot.conversationSnapshot.snapshotHash !== claim.conversationSnapshotHash))) {
|
|
220
|
+
throw new DomainError('DATA_CORRUPT', 'Frozen commit claim snapshot does not match active state.', { status: 500 });
|
|
221
|
+
}
|
|
222
|
+
this.scanner.verifyEvidence(snapshot.evidence);
|
|
223
|
+
const retrievalManifest = this.claimStore.atomic.readJsonStrict(snapshot.existingKnowledge.manifestPath, {
|
|
224
|
+
category: 'knowledge-retrieval-manifest',
|
|
225
|
+
validate: value => {
|
|
226
|
+
if (!value || value.schema !== 'knowledge-retrieval-manifest/v1'
|
|
227
|
+
|| value.manifestHash !== claim.retrievalManifestHash
|
|
228
|
+
|| calculateRetrievalManifestHash(value) !== value.manifestHash) {
|
|
229
|
+
throw new DomainError('DATA_CORRUPT', 'Frozen knowledge retrieval manifest is corrupt.', { status: 500 });
|
|
230
|
+
}
|
|
231
|
+
return value;
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
const selectedById = new Map(retrievalManifest.selected.map(item => [`${item.spaceId}:${item.chunkId}`, item]));
|
|
235
|
+
for (const entry of snapshot.existingKnowledge.entries || []) {
|
|
236
|
+
const selected = selectedById.get(`${entry.projectId === projectId ? `project:${projectId}` : `project:${entry.projectId}`}:${entry.chunkId}`);
|
|
237
|
+
if (!selected || knowledgeContentHash(entry.content) !== entry.hash || selected.contentHash !== entry.hash) {
|
|
238
|
+
throw new DomainError('DATA_CORRUPT', 'Frozen authoritative Markdown context does not match its retrieval manifest.', { status: 500 });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
claim.attempt = Number(claim.attempt || 1) + 1;
|
|
242
|
+
claim.phase = 'evidence.prepared';
|
|
243
|
+
await this.projectStore.updateState(projectId, draft => {
|
|
244
|
+
draft.analysis.activeClaim = claim;
|
|
245
|
+
draft.analysis.status = 'evidence.prepared';
|
|
246
|
+
draft.analysis.lastError = null;
|
|
247
|
+
});
|
|
248
|
+
return {
|
|
249
|
+
claim,
|
|
250
|
+
evidence: snapshot.evidence,
|
|
251
|
+
prompt: snapshot.prompt,
|
|
252
|
+
requirements: snapshot.requirements || [],
|
|
253
|
+
conversationSnapshot: snapshot.conversationSnapshot || null,
|
|
254
|
+
existingKnowledge: snapshot.existingKnowledge,
|
|
255
|
+
retry: true,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const runId = createId('run');
|
|
260
|
+
const evidenceRoot = this.layout.getRuntimePath('runs', projectId, runId, 'input', 'evidence');
|
|
261
|
+
const evidence = await this.scanner.collectEvidence(config, commitSha, { branch, evidenceRoot });
|
|
262
|
+
this.scanner.verifyEvidence(evidence);
|
|
263
|
+
const conversationSnapshot = await this.binder.bind({ projectId, commitSha, branch });
|
|
264
|
+
const requirements = snapshotRequirementRecords(conversationSnapshot);
|
|
265
|
+
const existingKnowledge = await this.retrievalService.retrieveForCommit({
|
|
266
|
+
projectId,
|
|
267
|
+
conversationSnapshot,
|
|
268
|
+
commitEvidence: evidence,
|
|
269
|
+
});
|
|
270
|
+
existingKnowledge.manifestPath = this.layout.getRuntimePath('runs', projectId, runId, 'input', 'retrieval', 'manifest.json');
|
|
271
|
+
this.claimStore.atomic.writeJsonAtomic(existingKnowledge.manifestPath, existingKnowledge.manifest);
|
|
272
|
+
const rendered = renderCommitPrompt({ projectId, config, evidence, requirements, conversationSnapshot, existingKnowledge });
|
|
273
|
+
const claim = {
|
|
274
|
+
schema: SCHEMAS.commitClaim,
|
|
275
|
+
projectId,
|
|
276
|
+
commitSha,
|
|
277
|
+
parents: evidence.parents,
|
|
278
|
+
triggerFirstSeen: trigger,
|
|
279
|
+
requirementIds: requirements.map(requirement => requirement.id),
|
|
280
|
+
requirementBinding: conversationSnapshot.status,
|
|
281
|
+
conversationSnapshotHash: conversationSnapshot.snapshotHash,
|
|
282
|
+
conversationTurnIds: conversationSnapshot.turns.map(turn => turn.turnId),
|
|
283
|
+
conversationEventIds: conversationSnapshot.turns.flatMap(turn => [...turn.userEvents, ...turn.assistantEvents].map(event => event.eventId)),
|
|
284
|
+
promptTemplateVersion: rendered.promptTemplateVersion,
|
|
285
|
+
promptHash: rendered.promptHash,
|
|
286
|
+
patchHash: evidence.patchHash,
|
|
287
|
+
evidenceHash: evidence.evidenceHash,
|
|
288
|
+
evidenceManifestHash: evidence.evidenceBundle.manifestHash,
|
|
289
|
+
retrievalManifestHash: existingKnowledge.manifestHash,
|
|
290
|
+
knowledgePath: config.knowledgePath,
|
|
291
|
+
runId,
|
|
292
|
+
operationId,
|
|
293
|
+
phase: 'evidence.prepared',
|
|
294
|
+
attempt: 1,
|
|
295
|
+
};
|
|
296
|
+
claim.fingerprint = claimFingerprint(claim);
|
|
297
|
+
this.claimStore.write(projectId, commitSha, {
|
|
298
|
+
projectId,
|
|
299
|
+
commitSha,
|
|
300
|
+
claimFingerprint: claim.fingerprint,
|
|
301
|
+
evidence,
|
|
302
|
+
prompt: rendered.prompt,
|
|
303
|
+
requirements,
|
|
304
|
+
conversationSnapshot,
|
|
305
|
+
existingKnowledge,
|
|
306
|
+
createdAt: new Date().toISOString(),
|
|
307
|
+
});
|
|
308
|
+
try {
|
|
309
|
+
await this.projectStore.updateState(projectId, draft => {
|
|
310
|
+
if (draft.analysis.activeClaim) throw new DomainError('PROJECT_BUSY', 'Another active claim already exists.', { status: 409, retryable: true });
|
|
311
|
+
draft.analysis.activeClaim = claim;
|
|
312
|
+
draft.analysis.status = 'evidence.prepared';
|
|
313
|
+
draft.analysis.lastError = null;
|
|
314
|
+
});
|
|
315
|
+
} catch (error) {
|
|
316
|
+
this.claimStore.remove(projectId, commitSha);
|
|
317
|
+
throw error;
|
|
318
|
+
}
|
|
319
|
+
await this.log('info', 'reconcile.claim_prepared', 'Commit evidence and claim prepared.', {
|
|
320
|
+
projectId,
|
|
321
|
+
operationId,
|
|
322
|
+
runId: claim.runId,
|
|
323
|
+
commitSha,
|
|
324
|
+
phase: claim.phase,
|
|
325
|
+
promptHash: claim.promptHash,
|
|
326
|
+
patchHash: claim.patchHash,
|
|
327
|
+
patchBytes: evidence.patchBytes,
|
|
328
|
+
evidenceManifestHash: claim.evidenceManifestHash,
|
|
329
|
+
evidenceChunkCount: evidence.evidenceBundle.chunkCount,
|
|
330
|
+
evidenceChunkBytes: evidence.evidenceBundle.chunkBytes,
|
|
331
|
+
retrievalManifestHash: claim.retrievalManifestHash,
|
|
332
|
+
retrievalSelectedCount: existingKnowledge.entries.length,
|
|
333
|
+
retrievalBytes: existingKnowledge.totalBytes,
|
|
334
|
+
fileCount: evidence.files.length,
|
|
335
|
+
requirementBinding: claim.requirementBinding,
|
|
336
|
+
requirementCount: claim.requirementIds.length,
|
|
337
|
+
});
|
|
338
|
+
return { claim, evidence, prompt: rendered.prompt, requirements, conversationSnapshot, existingKnowledge, retry: false };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async processCommit(projectId, trigger, operationId, config, branch, commitSha) {
|
|
342
|
+
let prepared;
|
|
343
|
+
try {
|
|
344
|
+
prepared = await this.prepareClaim(projectId, trigger, operationId, config, branch, commitSha);
|
|
345
|
+
if (this.requireAiProfile) {
|
|
346
|
+
// Use the shared effective-profile resolver so a project with no explicit
|
|
347
|
+
// aiProfileId can still analyze via the global default or first-usable
|
|
348
|
+
// profile. The resolver throws AI_PROFILE_REQUIRED if nothing usable
|
|
349
|
+
// exists, which becomes a clear actionable error rather than a silent
|
|
350
|
+
// null deref.
|
|
351
|
+
resolveEffectiveAiProfile(this.settingsStore ? this.settingsStore.read() : null, config);
|
|
352
|
+
}
|
|
353
|
+
if (!this.claimProcessor || typeof this.claimProcessor.processClaim !== 'function') {
|
|
354
|
+
throw new DomainError('INVALID_ARGUMENT', 'Knowledge claim processor is unavailable.', { status: 503, retryable: true });
|
|
355
|
+
}
|
|
356
|
+
const result = await this.claimProcessor.processClaim({
|
|
357
|
+
projectId,
|
|
358
|
+
config,
|
|
359
|
+
claim: prepared.claim,
|
|
360
|
+
evidence: prepared.evidence,
|
|
361
|
+
prompt: prepared.prompt,
|
|
362
|
+
requirements: prepared.requirements,
|
|
363
|
+
conversationSnapshot: prepared.conversationSnapshot,
|
|
364
|
+
existingKnowledge: prepared.existingKnowledge,
|
|
365
|
+
operationId,
|
|
366
|
+
});
|
|
367
|
+
if (!result || result.stateAdvanced !== true) {
|
|
368
|
+
throw new DomainError('INVALID_ARGUMENT', 'Knowledge promotion did not prove state advancement.', { status: 500, retryable: true });
|
|
369
|
+
}
|
|
370
|
+
const advanced = this.projectStore.readState(projectId);
|
|
371
|
+
if (advanced.lastAnalyzedCommit !== commitSha || advanced.analysis.activeClaim) {
|
|
372
|
+
throw new DomainError('DATA_CORRUPT', 'Claim processor reported success without atomically advancing project state.', { status: 500 });
|
|
373
|
+
}
|
|
374
|
+
this.claimStore.remove(projectId, commitSha);
|
|
375
|
+
await this.log('info', 'reconcile.commit_completed', 'Commit knowledge was promoted and state advanced.', {
|
|
376
|
+
projectId,
|
|
377
|
+
operationId,
|
|
378
|
+
runId: prepared.claim.runId,
|
|
379
|
+
commitSha,
|
|
380
|
+
phase: 'state.advanced',
|
|
381
|
+
});
|
|
382
|
+
return { ok: true, commitSha, runId: prepared.claim.runId, claimFingerprint: prepared.claim.fingerprint, retry: prepared.retry };
|
|
383
|
+
} catch (error) {
|
|
384
|
+
const failure = publicFailure(error, prepared && prepared.claim && prepared.claim.phase || 'evidence.prepared');
|
|
385
|
+
let stateUpdateError = null;
|
|
386
|
+
try {
|
|
387
|
+
await this.projectStore.updateState(projectId, draft => {
|
|
388
|
+
const active = draft.analysis.activeClaim;
|
|
389
|
+
if (active && active.commitSha === commitSha) {
|
|
390
|
+
active.phase = 'failed';
|
|
391
|
+
active.error = failure;
|
|
392
|
+
}
|
|
393
|
+
draft.analysis.status = 'failed';
|
|
394
|
+
draft.analysis.lastError = failure;
|
|
395
|
+
});
|
|
396
|
+
} catch (updateError) {
|
|
397
|
+
stateUpdateError = updateError;
|
|
398
|
+
}
|
|
399
|
+
await this.log('error', 'reconcile.commit_failed', 'Commit reconciliation stopped at the first failed commit.', {
|
|
400
|
+
projectId,
|
|
401
|
+
operationId,
|
|
402
|
+
runId: prepared && prepared.claim && prepared.claim.runId,
|
|
403
|
+
commitSha,
|
|
404
|
+
phase: failure.phase,
|
|
405
|
+
error,
|
|
406
|
+
context: stateUpdateError ? { failureStateWriteError: stateUpdateError } : {},
|
|
407
|
+
});
|
|
408
|
+
return { ok: false, commitSha, error: failure, retry: prepared && prepared.retry || false };
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async markDiverged(projectId, operationId, message, processed = []) {
|
|
413
|
+
const error = new DomainError('HISTORY_DIVERGED', message || 'Git history diverged from the stored baseline.', { status: 409 });
|
|
414
|
+
await this.projectStore.updateState(projectId, state => {
|
|
415
|
+
state.analysis.status = 'history-diverged';
|
|
416
|
+
state.analysis.lastError = publicFailure(error, 'history-diverged');
|
|
417
|
+
});
|
|
418
|
+
await this.log('error', 'reconcile.history_diverged', 'Git history diverged; reconciliation stopped without moving pointers.', { projectId, operationId, phase: 'history-diverged', error });
|
|
419
|
+
return { ok: false, status: 'history-diverged', processed, error: publicFailure(error, 'history-diverged') };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async failWithoutClaim(projectId, operationId, error, phase, processed = []) {
|
|
423
|
+
await this.projectStore.updateState(projectId, state => {
|
|
424
|
+
state.analysis.status = 'failed';
|
|
425
|
+
state.analysis.lastError = publicFailure(error, phase);
|
|
426
|
+
});
|
|
427
|
+
await this.log('error', 'reconcile.failed', 'Reconciliation failed before claim processing.', { projectId, operationId, phase, error });
|
|
428
|
+
return { ok: false, status: 'failed', processed, error: publicFailure(error, phase) };
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async function reconcileProjectCommits(projectId, trigger, deps = {}) {
|
|
433
|
+
const reconciler = deps.reconciler instanceof CommitReconciler ? deps.reconciler : new CommitReconciler(deps);
|
|
434
|
+
return reconciler.reconcile(projectId, trigger, { operationId: deps.operationId || '', commitSha: deps.commitSha || '', branch: deps.branch || '' });
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
module.exports = {
|
|
438
|
+
CommitClaimStore,
|
|
439
|
+
CommitReconciler,
|
|
440
|
+
CommitProcessingLedger,
|
|
441
|
+
claimFingerprint,
|
|
442
|
+
reconcileProjectCommits,
|
|
443
|
+
validateClaim,
|
|
444
|
+
};
|