@sanqianx/project-knowledge 4.6.1
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 +20 -0
- package/.claude-plugin/marketplace.json +17 -0
- package/CHANGELOG.md +780 -0
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +266 -0
- package/_site/README.md +30 -0
- package/_site/_test/ai-profile-resolver-test.js +137 -0
- package/_site/_test/ai-profile-test.js +75 -0
- package/_site/_test/ai-vendor-presets-test.js +91 -0
- package/_site/_test/automation-queue-test.js +14 -0
- package/_site/_test/automation-ui-test.js +40 -0
- package/_site/_test/background-task-registry-test.js +43 -0
- package/_site/_test/baseline-schema-test.js +91 -0
- package/_site/_test/bin-cli-test.js +218 -0
- package/_site/_test/bridge-adapter-test.js +95 -0
- package/_site/_test/bridge-consumer-service-test.js +310 -0
- package/_site/_test/cdp-browser-failure-test.js +100 -0
- package/_site/_test/chat-claudecodeui-match-test.js +92 -0
- package/_site/_test/claude-executable-discovery-test.js +90 -0
- package/_site/_test/claude-session-lifecycle-sweep-test.js +252 -0
- package/_site/_test/claude-workbench-test.js +170 -0
- package/_site/_test/client-capture-restart-e2e-test.js +189 -0
- package/_site/_test/codex-conversation-projection-test.js +59 -0
- package/_site/_test/codex-notify-fanout-test.js +45 -0
- package/_site/_test/commit-boundary-freeze-test.js +147 -0
- package/_site/_test/commit-conversation-binding-test.js +99 -0
- package/_site/_test/commit-evidence-test.js +110 -0
- package/_site/_test/context-pack-test.js +198 -0
- package/_site/_test/conversation-api-test.js +119 -0
- package/_site/_test/conversation-store-test.js +136 -0
- package/_site/_test/cross-repo-isolation-e2e-test.js +187 -0
- package/_site/_test/data-dir-migration-test.js +77 -0
- package/_site/_test/desktop-browser-compat-test.js +28 -0
- package/_site/_test/desktop-hook-runtime-regression-test.js +204 -0
- package/_site/_test/embedding-config-test.js +32 -0
- package/_site/_test/explicit-commit-processor-test.js +37 -0
- package/_site/_test/fixtures/fake-browser-exit.js +10 -0
- package/_site/_test/fixtures/fake-browser-hang.js +10 -0
- package/_site/_test/fixtures/make-git-repos.js +89 -0
- package/_site/_test/folder-picker-output-test.js +19 -0
- package/_site/_test/full-integration-e2e-test.js +165 -0
- package/_site/_test/git-validation-test.js +81 -0
- package/_site/_test/hook-runtime-endpoint-test.js +88 -0
- package/_site/_test/hook-status-repair-api-test.js +152 -0
- package/_site/_test/hook-trigger-test.js +85 -0
- package/_site/_test/hook-worktree-test.js +42 -0
- package/_site/_test/import-preflight-api-test.js +233 -0
- package/_site/_test/import-ui-flow-test.js +171 -0
- package/_site/_test/index-builder-test.js +41 -0
- package/_site/_test/index-writer-concurrency-test.js +135 -0
- package/_site/_test/integration-adapters-test.js +243 -0
- package/_site/_test/integration-capture-setup-test.js +108 -0
- package/_site/_test/integration-surface-coverage-test.js +165 -0
- package/_site/_test/kb-framework-test.js +57 -0
- package/_site/_test/kbpath-follow-test.js +63 -0
- package/_site/_test/knowledge-db-test.js +144 -0
- package/_site/_test/knowledge-language-control-test.js +170 -0
- package/_site/_test/knowledge-maintenance-test.js +40 -0
- package/_site/_test/knowledge-migration-test.js +75 -0
- package/_site/_test/knowledge-promotion-recovery-test.js +260 -0
- package/_site/_test/knowledge-query-test.js +54 -0
- package/_site/_test/knowledge-retrieval-service-test.js +77 -0
- package/_site/_test/knowledge-scopes-test.js +37 -0
- package/_site/_test/knowledge-storage-location-test.js +28 -0
- package/_site/_test/knowledge-storage-startup-test.js +52 -0
- package/_site/_test/knowledge-store-logs-supervision-test.js +89 -0
- package/_site/_test/legacy-claude-session-alias-test.js +106 -0
- package/_site/_test/legacy-forward-compat-test.js +257 -0
- package/_site/_test/legacy-project-upgrade-e2e-test.js +363 -0
- package/_site/_test/legacy-routes-removed-test.js +47 -0
- package/_site/_test/legacy-workbench-exclusion-test.js +92 -0
- package/_site/_test/log-redaction-test.js +58 -0
- package/_site/_test/logging-api-test.js +89 -0
- package/_site/_test/logging-sse-no-gap-test.js +88 -0
- package/_site/_test/logging-ui-test.js +104 -0
- package/_site/_test/markdown-delta-overlay-test.js +85 -0
- package/_site/_test/markdown-maintenance-api-test.js +75 -0
- package/_site/_test/markdown-maintenance-test.js +11 -0
- package/_site/_test/mcp-server-test.js +149 -0
- package/_site/_test/model-context-windows-test.js +89 -0
- package/_site/_test/module-bridge-eventbridge-test.js +193 -0
- package/_site/_test/non-release-ci-test.js +34 -0
- package/_site/_test/offline-boundary-isolation-test.js +32 -0
- package/_site/_test/orphaned-claim-recovery-test.js +80 -0
- package/_site/_test/p0-data-migration-characterization-test.js +37 -0
- package/_site/_test/p0-e2e-gate-test.js +347 -0
- package/_site/_test/package-boundary-test.js +42 -0
- package/_site/_test/package-startup-test.js +56 -0
- package/_site/_test/packaged-ui-smoke-test.js +79 -0
- package/_site/_test/path-consistency-test.js +151 -0
- package/_site/_test/pending-sweep-test.js +9 -0
- package/_site/_test/post-commit-automation-test.js +87 -0
- package/_site/_test/pr-consumer-contract-test.js +55 -0
- package/_site/_test/project-control-panel-task14-test.js +63 -0
- package/_site/_test/project-delete-recovery-test.js +64 -0
- package/_site/_test/project-goal-editor-test.js +145 -0
- package/_site/_test/project-layout-v2-migration-test.js +127 -0
- package/_site/_test/project-lifecycle-transaction-test.js +106 -0
- package/_site/_test/project-remove-running-guard-test.js +79 -0
- package/_site/_test/project-store-test.js +90 -0
- package/_site/_test/prompt-settings-test.js +115 -0
- package/_site/_test/protected-architecture-gate-test.js +123 -0
- package/_site/_test/refactor-characterization-test.js +36 -0
- package/_site/_test/release-version-sync-test.js +98 -0
- package/_site/_test/requirement-binding-test.js +113 -0
- package/_site/_test/requirement-recorder-test.js +173 -0
- package/_site/_test/run-all-tests.js +156 -0
- package/_site/_test/runtime-endpoint-test.js +64 -0
- package/_site/_test/scanner-test.js +83 -0
- package/_site/_test/server-runtime-migration-safety-test.js +27 -0
- package/_site/_test/server-security-test.js +96 -0
- package/_site/_test/sessions-stream-test.js +92 -0
- package/_site/_test/shared-contracts-test.js +58 -0
- package/_site/_test/simple-import-test.js +97 -0
- package/_site/_test/startup-analysis-disabled-test.js +16 -0
- package/_site/_test/storage-foundation-test.js +78 -0
- package/_site/_test/structured-logger-test.js +101 -0
- package/_site/_test/task15-20-ui-flow-test.js +148 -0
- package/_site/_test/team-knowledge-store-sparse-checkout-test.js +196 -0
- package/_site/_test/team-knowledge-store-test.js +131 -0
- package/_site/_test/tracking-start-test.js +143 -0
- package/_site/_test/ui-i18n-toggle-test.js +114 -0
- package/_site/_test/ui-smoke-test.js +73 -0
- package/_site/_test/v4122-upgrade-data-contract-test.js +56 -0
- package/_site/_test/workbench-permission-test.js +118 -0
- package/_site/_test/workspace-ui-contract-test.js +43 -0
- package/_site/lib/ai-profile-resolver.js +78 -0
- package/_site/lib/ai-vendor-presets.js +83 -0
- package/_site/lib/ai-workspace.js +104 -0
- package/_site/lib/atomic-file.js +204 -0
- package/_site/lib/automation-config.js +140 -0
- package/_site/lib/bridge-adapter.js +256 -0
- package/_site/lib/bridge-consumer-service.js +402 -0
- package/_site/lib/claude-cli-runner.js +1599 -0
- package/_site/lib/commit-conversation-binder.js +246 -0
- package/_site/lib/commit-processing-ledger.js +27 -0
- package/_site/lib/commit-prompt.js +202 -0
- package/_site/lib/commit-reconciler.js +444 -0
- package/_site/lib/context-pack-builder.js +299 -0
- package/_site/lib/contracts.js +279 -0
- package/_site/lib/conversation-exclusions.js +204 -0
- package/_site/lib/conversation-query-service.js +161 -0
- package/_site/lib/conversation-store.js +385 -0
- package/_site/lib/data-dir.js +81 -0
- package/_site/lib/data-state-classifier.js +40 -0
- package/_site/lib/embedding-service.js +85 -0
- package/_site/lib/evidence-bundle.js +228 -0
- package/_site/lib/folder-picker-output.js +24 -0
- package/_site/lib/git-runner.js +62 -0
- package/_site/lib/github-team-store.js +1175 -0
- package/_site/lib/hook-manager.js +202 -0
- package/_site/lib/index-builder.js +157 -0
- package/_site/lib/index-service.js +173 -0
- package/_site/lib/integration-installer.js +907 -0
- package/_site/lib/kb-framework.js +191 -0
- package/_site/lib/kb-validator.js +124 -0
- package/_site/lib/knowledge-db.js +363 -0
- package/_site/lib/knowledge-promotion.js +435 -0
- package/_site/lib/knowledge-retrieval-service.js +401 -0
- package/_site/lib/knowledge-schema.js +121 -0
- package/_site/lib/knowledge-tool-runtime.js +359 -0
- package/_site/lib/legacy-data-manifest.js +26 -0
- package/_site/lib/llm-client.js +173 -0
- package/_site/lib/markdown-knowledge-indexer.js +230 -0
- package/_site/lib/migration-service.js +442 -0
- package/_site/lib/model-context-windows.js +82 -0
- package/_site/lib/module-bridge.js +496 -0
- package/_site/lib/post-commit-automation.js +141 -0
- package/_site/lib/project-lifecycle-service.js +511 -0
- package/_site/lib/project-registry-store.js +129 -0
- package/_site/lib/project-store.js +262 -0
- package/_site/lib/prompt-registry.js +148 -0
- package/_site/lib/requirement-adapters.js +10 -0
- package/_site/lib/requirement-binder.js +112 -0
- package/_site/lib/requirement-recorder.js +278 -0
- package/_site/lib/runtime-endpoint.js +160 -0
- package/_site/lib/scanner.js +298 -0
- package/_site/lib/server-app.js +1520 -0
- package/_site/lib/settings-store.js +113 -0
- package/_site/lib/storage-layout.js +162 -0
- package/_site/lib/structured-logger.js +574 -0
- package/_site/scripts/codex-notify-fanout.cjs +51 -0
- package/_site/scripts/folder-picker.ps1 +156 -0
- package/_site/scripts/hook-trigger.js +156 -0
- package/_site/scripts/sync-release-version.js +158 -0
- package/_site/server.js +12 -0
- package/_site/start.bat +26 -0
- package/_site/stop.bat +11 -0
- package/bin/project-knowledge-integrations.js +241 -0
- package/bin/project-knowledge-kb.js +65 -0
- package/bin/project-knowledge-mcp.js +195 -0
- package/bin/project-knowledge.js +439 -0
- package/claude-prompts.json +21 -0
- package/docs/README.zh-CN.md +175 -0
- package/docs/assets/architecture.svg +182 -0
- package/docs/assets/dashboard.png +0 -0
- package/docs/assets/draft-review.png +0 -0
- package/docs/assets/logo.svg +50 -0
- package/docs/assets/runs-drafts.png +0 -0
- package/docs/assets/settings.png +0 -0
- package/docs/project-registry-schema.md +33 -0
- package/docs/team-knowledge-mode-a-plan.md +73 -0
- package/docs/testing-strategy.md +33 -0
- package/package.json +100 -0
- package/plugins/project-knowledge/.claude-plugin/plugin.json +19 -0
- package/plugins/project-knowledge/.codex-plugin/plugin.json +38 -0
- package/plugins/project-knowledge/.mcp.json +14 -0
- package/plugins/project-knowledge/opencode/project-knowledge.md +3 -0
- package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -0
- package/plugins/project-knowledge/skills/project-knowledge/agents/openai.yaml +11 -0
- package/templates/change.md +30 -0
- package/templates/project-readme.md +52 -0
- package/ui/app.css +58 -0
- package/ui/app.js +607 -0
- package/ui/favicon.svg +38 -0
- package/ui/i18n.js +146 -0
- package/ui/index.html +162 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const { DomainError, validateProjectId } = require('./contracts');
|
|
3
|
+
const { ConversationStore, sha256 } = require('./conversation-store');
|
|
4
|
+
const { readDevelopmentEvents } = require('./conversation-exclusions');
|
|
5
|
+
const { StorageLayout } = require('./storage-layout');
|
|
6
|
+
|
|
7
|
+
function projectEvent(event) {
|
|
8
|
+
return {
|
|
9
|
+
eventId: event.eventId,
|
|
10
|
+
sequence: event.sequence,
|
|
11
|
+
content: event.content,
|
|
12
|
+
contentHash: event.contentHash,
|
|
13
|
+
capturedAt: event.capturedAt,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class CommitConversationBinder {
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.layout = options.layout || new StorageLayout(options);
|
|
20
|
+
this.projectStore = options.projectStore;
|
|
21
|
+
this.conversationStore = options.conversationStore || new ConversationStore({
|
|
22
|
+
layout: this.layout,
|
|
23
|
+
projectStore: this.projectStore,
|
|
24
|
+
logger: options.logger,
|
|
25
|
+
});
|
|
26
|
+
this.logger = options.logger || null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async log(level, event, message, context) {
|
|
30
|
+
if (this.logger && typeof this.logger[level] === 'function') await this.logger[level](event, message, context);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
repoMatches(left, right) {
|
|
34
|
+
if (!left || !right) return false;
|
|
35
|
+
// workspaceId is the canonical primary key (I-06). commonDir/repoId are
|
|
36
|
+
// read-compat fallbacks for records stored before repo-identity/v1.
|
|
37
|
+
if (left.workspaceId && right.workspaceId) return String(left.workspaceId) === String(right.workspaceId);
|
|
38
|
+
if (left.commonDir && right.commonDir) return this.layout.pathsEqual(left.commonDir, right.commonDir);
|
|
39
|
+
if (left.repoId && right.repoId) return String(left.repoId) === String(right.repoId);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
boundaryOrder(boundary) {
|
|
44
|
+
return Number.isInteger(boundary.journalSequence) ? boundary.journalSequence : boundary.bridgeCursorAtCommit;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
previousBoundary(projectId, boundary) {
|
|
48
|
+
const order = this.boundaryOrder(boundary);
|
|
49
|
+
return this.conversationStore.listBoundaries(projectId)
|
|
50
|
+
.filter(candidate => candidate.commitSha !== boundary.commitSha
|
|
51
|
+
&& this.repoMatches(candidate.repoIdentity, boundary.repoIdentity)
|
|
52
|
+
&& this.boundaryOrder(candidate) < order)
|
|
53
|
+
.sort((left, right) => this.boundaryOrder(right) - this.boundaryOrder(left))[0] || null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
snapshotExists(projectId, commitSha) {
|
|
57
|
+
return fs.existsSync(this.layout.getProjectCommitConversationPath(projectId, commitSha));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async bind(input = {}) {
|
|
61
|
+
const projectId = validateProjectId(input.projectId);
|
|
62
|
+
const commitSha = this.layout.validateCommitSha(input.commitSha);
|
|
63
|
+
if (this.snapshotExists(projectId, commitSha)) return this.conversationStore.readSnapshot(projectId, commitSha);
|
|
64
|
+
|
|
65
|
+
if (this.projectStore) await this.conversationStore.importLegacyRequirements(projectId);
|
|
66
|
+
// Development Conversation reads are centralized: exclusions and Codex
|
|
67
|
+
// turn repair are identical here and in the query service.
|
|
68
|
+
const events = readDevelopmentEvents(this.conversationStore, projectId);
|
|
69
|
+
let boundary = null;
|
|
70
|
+
try { boundary = this.conversationStore.readBoundary(projectId, commitSha); }
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (!error || error.code !== 'ENOENT') throw error;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const explicitEvents = events.filter(event => event.role === 'user' && event.explicitCommitSha === commitSha);
|
|
76
|
+
if (!boundary) {
|
|
77
|
+
const turns = this.buildExplicitTurns(explicitEvents);
|
|
78
|
+
return this.finalize(projectId, commitSha, {
|
|
79
|
+
repoIdentity: null,
|
|
80
|
+
parentSha: null,
|
|
81
|
+
boundaryStartCursor: null,
|
|
82
|
+
boundaryEndCursor: null,
|
|
83
|
+
status: turns.length ? 'available' : 'unavailable',
|
|
84
|
+
turns,
|
|
85
|
+
excludedFuturePromptCount: 0,
|
|
86
|
+
}, { reason: 'missing-commit-boundary' });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const previous = this.previousBoundary(projectId, boundary);
|
|
90
|
+
const state = this.projectStore ? this.projectStore.readState(projectId) : { conversationBaselineCursor: null };
|
|
91
|
+
const startCursor = previous ? previous.bridgeCursorAtCommit : state.conversationBaselineCursor;
|
|
92
|
+
const endCursor = boundary.bridgeCursorAtCommit;
|
|
93
|
+
const selected = new Map();
|
|
94
|
+
let ambiguousCount = 0;
|
|
95
|
+
let excludedFuturePromptCount = 0;
|
|
96
|
+
|
|
97
|
+
for (const event of explicitEvents) {
|
|
98
|
+
if (event.repoIdentity && boundary.repoIdentity && !this.repoMatches(event.repoIdentity, boundary.repoIdentity)) {
|
|
99
|
+
ambiguousCount += 1;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (!event.turnId) { ambiguousCount += 1; continue; }
|
|
103
|
+
selected.set(event.turnId, 'explicit');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (Number.isInteger(startCursor)) {
|
|
107
|
+
for (const event of events) {
|
|
108
|
+
if (event.role !== 'user' || event.sequence == null || event.explicitCommitSha) continue;
|
|
109
|
+
if (!this.repoMatches(event.repoIdentity, boundary.repoIdentity)) {
|
|
110
|
+
if (event.sequence > startCursor && event.sequence <= endCursor && !event.repoIdentity) ambiguousCount += 1;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (event.sequence > endCursor) {
|
|
114
|
+
excludedFuturePromptCount += 1;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (event.sequence <= startCursor) continue;
|
|
118
|
+
// Automatic direct evidence requires exact identity. Legacy stored
|
|
119
|
+
// 'high'/'trusted' translate to exact for old known data only;
|
|
120
|
+
// 'explicit' is the explicit-fallback tier (I-18: no synthetic).
|
|
121
|
+
const rawConfidence = String(event.identityConfidence || '');
|
|
122
|
+
const effectiveConfidence = rawConfidence === 'high' || rawConfidence === 'trusted' ? 'exact' : rawConfidence;
|
|
123
|
+
if (!event.turnId || !['exact', 'explicit'].includes(effectiveConfidence)) {
|
|
124
|
+
ambiguousCount += 1;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
selected.set(event.turnId, 'direct');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const turnId of boundary.openTurnIdsAtCommit) {
|
|
131
|
+
if (selected.has(turnId)) continue;
|
|
132
|
+
const user = events.find(event => event.role === 'user'
|
|
133
|
+
&& event.turnId === turnId
|
|
134
|
+
&& event.sequence != null
|
|
135
|
+
&& event.sequence <= startCursor
|
|
136
|
+
&& this.repoMatches(event.repoIdentity, boundary.repoIdentity));
|
|
137
|
+
const closedAt = user && user.developmentTurnClosedAtSequence;
|
|
138
|
+
if (user && !(Number.isInteger(closedAt) && closedAt <= startCursor)) {
|
|
139
|
+
selected.set(turnId, 'shared-spanning');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const turns = [];
|
|
145
|
+
for (const [turnId, bindingKind] of selected) {
|
|
146
|
+
const turnEvents = events.filter(event => event.turnId === turnId).sort((left, right) => {
|
|
147
|
+
if (left.sequence == null) return 1;
|
|
148
|
+
if (right.sequence == null) return -1;
|
|
149
|
+
return left.sequence - right.sequence;
|
|
150
|
+
});
|
|
151
|
+
const userEvents = turnEvents.filter(event => event.role === 'user'
|
|
152
|
+
&& (bindingKind === 'explicit' || (event.sequence != null && event.sequence <= endCursor)));
|
|
153
|
+
if (!userEvents.length) continue;
|
|
154
|
+
const first = userEvents[0];
|
|
155
|
+
// I-13: no event beyond the commit boundary may enter a frozen
|
|
156
|
+
// snapshot — including assistant replies that arrived after the
|
|
157
|
+
// boundary but before a delayed binder run.
|
|
158
|
+
const assistantEvents = turnEvents.filter(event => event.role === 'assistant'
|
|
159
|
+
&& (bindingKind === 'explicit' || (event.sequence != null && event.sequence <= endCursor)));
|
|
160
|
+
turns.push({
|
|
161
|
+
turnId,
|
|
162
|
+
source: first.source,
|
|
163
|
+
sessionId: first.sessionId,
|
|
164
|
+
bindingKind,
|
|
165
|
+
userEvents: userEvents.map(projectEvent),
|
|
166
|
+
assistantEvents: assistantEvents.map(projectEvent),
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
turns.sort((left, right) => {
|
|
170
|
+
const a = left.userEvents[0].sequence;
|
|
171
|
+
const b = right.userEvents[0].sequence;
|
|
172
|
+
if (a == null) return 1;
|
|
173
|
+
if (b == null) return -1;
|
|
174
|
+
return a - b;
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
let status = turns.length ? 'available' : 'no-new-user-prompt';
|
|
178
|
+
let reason = turns.length ? 'sequence-window' : 'no-new-user-prompt';
|
|
179
|
+
if (!Number.isInteger(startCursor)) {
|
|
180
|
+
status = explicitEvents.length && turns.length ? 'available' : 'unavailable';
|
|
181
|
+
reason = 'conversation-baseline-unavailable';
|
|
182
|
+
} else if (ambiguousCount > 0 && !turns.length) {
|
|
183
|
+
status = 'ambiguous';
|
|
184
|
+
reason = 'turn-or-repo-identity-ambiguous';
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return this.finalize(projectId, commitSha, {
|
|
188
|
+
repoIdentity: boundary.repoIdentity,
|
|
189
|
+
parentSha: boundary.parentShas[0] || null,
|
|
190
|
+
boundaryStartCursor: startCursor,
|
|
191
|
+
boundaryEndCursor: endCursor,
|
|
192
|
+
status,
|
|
193
|
+
turns,
|
|
194
|
+
excludedFuturePromptCount,
|
|
195
|
+
}, { reason, ambiguousCount, previousBoundaryCommit: previous && previous.commitSha || null });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
buildExplicitTurns(events) {
|
|
199
|
+
return events.filter(event => event.turnId).map(event => ({
|
|
200
|
+
turnId: event.turnId,
|
|
201
|
+
source: event.source,
|
|
202
|
+
sessionId: event.sessionId,
|
|
203
|
+
bindingKind: 'explicit',
|
|
204
|
+
userEvents: [projectEvent(event)],
|
|
205
|
+
assistantEvents: [],
|
|
206
|
+
}));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async finalize(projectId, commitSha, snapshotInput, decision) {
|
|
210
|
+
const snapshot = this.conversationStore.writeSnapshot(projectId, {
|
|
211
|
+
commitSha,
|
|
212
|
+
...snapshotInput,
|
|
213
|
+
finalizedAt: new Date().toISOString(),
|
|
214
|
+
});
|
|
215
|
+
await this.log(snapshot.status === 'ambiguous' || snapshot.status === 'unavailable' ? 'warn' : 'info', 'conversation.commit_snapshot_frozen', 'Commit conversation snapshot was frozen.', {
|
|
216
|
+
projectId,
|
|
217
|
+
commitSha,
|
|
218
|
+
component: 'commit-conversation-binder',
|
|
219
|
+
phase: 'snapshot-frozen',
|
|
220
|
+
context: {
|
|
221
|
+
snapshotHash: snapshot.snapshotHash,
|
|
222
|
+
status: snapshot.status,
|
|
223
|
+
turnCount: snapshot.turns.length,
|
|
224
|
+
userEventCount: snapshot.turns.reduce((total, turn) => total + turn.userEvents.length, 0),
|
|
225
|
+
assistantEventCount: snapshot.turns.reduce((total, turn) => total + turn.assistantEvents.length, 0),
|
|
226
|
+
decision,
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
return snapshot;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function snapshotRequirementRecords(snapshot) {
|
|
234
|
+
return snapshot.turns.flatMap(turn => turn.userEvents.map(event => ({
|
|
235
|
+
id: event.eventId,
|
|
236
|
+
client: turn.source,
|
|
237
|
+
sessionId: turn.sessionId,
|
|
238
|
+
ts: event.capturedAt,
|
|
239
|
+
requirement: event.content,
|
|
240
|
+
requirementHash: event.contentHash,
|
|
241
|
+
turnId: turn.turnId,
|
|
242
|
+
bindingKind: turn.bindingKind,
|
|
243
|
+
})));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
module.exports = { CommitConversationBinder, snapshotRequirementRecords, projectEvent, sha256 };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const { DomainError, validateProjectId } = require('./contracts');
|
|
3
|
+
const { validateSha } = require('./scanner');
|
|
4
|
+
const { StorageLayout } = require('./storage-layout');
|
|
5
|
+
const AtomicFile = require('./atomic-file');
|
|
6
|
+
|
|
7
|
+
class CommitProcessingLedger {
|
|
8
|
+
constructor(options = {}) { this.layout = options.layout || new StorageLayout(options); this.atomic = options.atomic || AtomicFile; }
|
|
9
|
+
filePath(projectId, commitSha) { return this.layout.getRuntimePath('processed-commits', validateProjectId(projectId), `${validateSha(commitSha)}.json`); }
|
|
10
|
+
read(projectId, commitSha) {
|
|
11
|
+
const file = this.filePath(projectId, commitSha);
|
|
12
|
+
if (!fs.existsSync(file)) return null;
|
|
13
|
+
return this.atomic.readJsonStrict(file, { category: 'commit-processing-record', validate: value => {
|
|
14
|
+
if (!value || value.schema !== 'commit-processing-record/v1' || value.projectId !== projectId || value.commitSha !== commitSha || value.status !== 'completed') {
|
|
15
|
+
throw new DomainError('DATA_CORRUPT', 'Commit processing ledger record is corrupt.', { status: 500 });
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
} });
|
|
19
|
+
}
|
|
20
|
+
complete(projectId, commitSha, fields = {}) {
|
|
21
|
+
const record = { schema: 'commit-processing-record/v1', projectId: validateProjectId(projectId), commitSha: validateSha(commitSha), status: 'completed', ...fields, completedAt: new Date().toISOString() };
|
|
22
|
+
this.atomic.writeJsonAtomic(this.filePath(projectId, commitSha), record);
|
|
23
|
+
return record;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = { CommitProcessingLedger };
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const {
|
|
5
|
+
DEFAULT_COMMIT_PROMPT_TEMPLATE,
|
|
6
|
+
PROMPT_TEMPLATE_VERSION,
|
|
7
|
+
normalizeAutomationConfig,
|
|
8
|
+
renderTemplate,
|
|
9
|
+
} = require('./automation-config');
|
|
10
|
+
|
|
11
|
+
const REQUIREMENT_UNAVAILABLE = '需求上下文未记录。只能记录代码可以证明的变化,不得猜测业务目的。';
|
|
12
|
+
const REQUIRED_SECTIONS = Object.freeze([
|
|
13
|
+
['userRequirement', '用户需求'],
|
|
14
|
+
['aiConversationEvidence', 'AI 对话证据'],
|
|
15
|
+
['commitMetadata', 'Commit 信息'],
|
|
16
|
+
['actualChanges', 'Commit 实际变更文件'],
|
|
17
|
+
['actualPatch', 'Commit 实际 Patch'],
|
|
18
|
+
['existingKnowledge', '现有相关知识'],
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
function sha256(value) {
|
|
22
|
+
return `sha256:${crypto.createHash('sha256').update(value, 'utf8').digest('hex')}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function formatRequirements(records) {
|
|
26
|
+
if (!Array.isArray(records) || records.length === 0) return REQUIREMENT_UNAVAILABLE;
|
|
27
|
+
return records.map(record => [
|
|
28
|
+
`- requirementId: ${record.id}`,
|
|
29
|
+
` client/session: ${record.client}/${record.sessionId}`,
|
|
30
|
+
` recordedAt: ${record.ts}`,
|
|
31
|
+
' requirement: |',
|
|
32
|
+
...String(record.requirement || '').split(/\r?\n/).map(line => ` ${line}`),
|
|
33
|
+
].join('\n')).join('\n');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function formatConversationUserTruth(snapshot) {
|
|
37
|
+
if (!snapshot || !Array.isArray(snapshot.turns) || snapshot.turns.length === 0) return REQUIREMENT_UNAVAILABLE;
|
|
38
|
+
return snapshot.turns.flatMap(turn => turn.userEvents.map(event => [
|
|
39
|
+
`- turnId: ${turn.turnId}`,
|
|
40
|
+
` eventId: ${event.eventId}`,
|
|
41
|
+
` source/session: ${turn.source}/${turn.sessionId || '(unavailable)'}`,
|
|
42
|
+
` binding: ${turn.bindingKind}`,
|
|
43
|
+
` capturedAt: ${event.capturedAt}`,
|
|
44
|
+
' userPrompt: |',
|
|
45
|
+
...String(event.content || '').split(/\r?\n/).map(line => ` ${line}`),
|
|
46
|
+
].join('\n'))).join('\n');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function formatAssistantEvidence(snapshot) {
|
|
50
|
+
if (!snapshot || !Array.isArray(snapshot.turns)) return '(no assistant conversation evidence was frozen)';
|
|
51
|
+
const entries = snapshot.turns.flatMap(turn => turn.assistantEvents.map(event => [
|
|
52
|
+
`- turnId: ${turn.turnId}`,
|
|
53
|
+
` eventId: ${event.eventId}`,
|
|
54
|
+
` capturedAt: ${event.capturedAt}`,
|
|
55
|
+
' assistantResponse: |',
|
|
56
|
+
...String(event.content || '').split(/\r?\n/).map(line => ` ${line}`),
|
|
57
|
+
].join('\n')));
|
|
58
|
+
return entries.length ? entries.join('\n') : '(no assistant conversation evidence was frozen)';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function formatCommitMetadata(evidence) {
|
|
62
|
+
return [
|
|
63
|
+
`fullSha: ${evidence.commitSha}`,
|
|
64
|
+
`parents: ${(evidence.parents || []).join(', ') || '(root commit)'}`,
|
|
65
|
+
`author: ${evidence.author || ''}`,
|
|
66
|
+
`date: ${evidence.date || ''}`,
|
|
67
|
+
`subject: ${evidence.subject || ''}`,
|
|
68
|
+
`branchAtScan: ${evidence.branch || '(detached/unavailable)'}`,
|
|
69
|
+
`patchMode: ${evidence.patchMode}`,
|
|
70
|
+
`patchHash: ${evidence.patchHash}`,
|
|
71
|
+
`patchBytes: ${evidence.patchBytes}`,
|
|
72
|
+
].join('\n');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function formatChanges(evidence) {
|
|
76
|
+
if (!evidence.files || evidence.files.length === 0) return '(no changed paths reported by Git)';
|
|
77
|
+
return evidence.files.map(file => {
|
|
78
|
+
const rename = file.oldPath ? ` ${file.oldPath} -> ${file.path}` : ` ${file.path}`;
|
|
79
|
+
const stats = file.binary ? ' binary' : ` +${file.added == null ? '?' : file.added}/-${file.deleted == null ? '?' : file.deleted}`;
|
|
80
|
+
return `- ${file.status}${rename}${stats}`;
|
|
81
|
+
}).join('\n');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function formatPatch(evidence) {
|
|
85
|
+
if (!evidence.patchChunked) return evidence.patch || '(empty patch)';
|
|
86
|
+
const bundle = evidence.evidenceBundle || {};
|
|
87
|
+
return [
|
|
88
|
+
'[EXACT PATCH AVAILABLE AS READ-ONLY CHUNKS]',
|
|
89
|
+
`fullPatchHash: ${evidence.patchHash}`,
|
|
90
|
+
`fullPatchBytes: ${evidence.patchBytes}`,
|
|
91
|
+
`patchManifestHash: ${bundle.manifestHash || '(unavailable)'}`,
|
|
92
|
+
`patchManifestPath: ${bundle.manifestPath || '(unavailable)'}`,
|
|
93
|
+
`chunkCount: ${bundle.chunkCount == null ? '(unavailable)' : bundle.chunkCount}`,
|
|
94
|
+
'Read patch-manifest.json, then Read exact patch chunks in manifest sequence. The chunks cover the full Git diff.',
|
|
95
|
+
'Do not read the source worktree and do not infer unread evidence.',
|
|
96
|
+
].join('\n');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function formatExistingKnowledge(existing) {
|
|
100
|
+
if (!existing || !Array.isArray(existing.entries) || existing.entries.length === 0) {
|
|
101
|
+
return '(no existing project knowledge was available)';
|
|
102
|
+
}
|
|
103
|
+
const sections = existing.entries.map(entry => `--- ${entry.path} (${entry.hash}) ---\n${entry.content}`);
|
|
104
|
+
if (existing.omitted && existing.omitted.length) {
|
|
105
|
+
sections.push(`[EXISTING KNOWLEDGE OMITTED: ${existing.omitted.join(', ')}]`);
|
|
106
|
+
}
|
|
107
|
+
return sections.join('\n\n');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function ensureRequiredSections(template, rendered, vars) {
|
|
111
|
+
const missing = REQUIRED_SECTIONS.filter(([name]) => !new RegExp(`\\{\\{\\s*${name}\\s*\\}\\}`, 'i').test(template));
|
|
112
|
+
if (!missing.length) return rendered;
|
|
113
|
+
const footer = missing.map(([name, label]) => `${label}:\n${vars[name]}`).join('\n\n');
|
|
114
|
+
return `${rendered.trimEnd()}\n\n${footer}`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function renderCommitPrompt(input = {}) {
|
|
118
|
+
const config = input.config || {};
|
|
119
|
+
const evidence = input.evidence || {};
|
|
120
|
+
const automation = normalizeAutomationConfig(config.automation);
|
|
121
|
+
const template = automation.commitPromptTemplate || DEFAULT_COMMIT_PROMPT_TEMPLATE;
|
|
122
|
+
const vars = {
|
|
123
|
+
userRequirement: input.conversationSnapshot
|
|
124
|
+
? formatConversationUserTruth(input.conversationSnapshot)
|
|
125
|
+
: formatRequirements(input.requirements),
|
|
126
|
+
aiConversationEvidence: formatAssistantEvidence(input.conversationSnapshot),
|
|
127
|
+
commitMetadata: formatCommitMetadata(evidence),
|
|
128
|
+
actualChanges: formatChanges(evidence),
|
|
129
|
+
actualPatch: formatPatch(evidence),
|
|
130
|
+
existingKnowledge: formatExistingKnowledge(input.existingKnowledge),
|
|
131
|
+
projectId: config.projectId || input.projectId || '',
|
|
132
|
+
displayName: config.displayName || config.projectId || '',
|
|
133
|
+
knowledgePath: config.knowledgePath || '',
|
|
134
|
+
repoPath: config.repoPath || '',
|
|
135
|
+
commitHash: evidence.commitSha || '',
|
|
136
|
+
commitSubject: evidence.subject || '',
|
|
137
|
+
commitAuthor: evidence.author || '',
|
|
138
|
+
commitDate: evidence.date || '',
|
|
139
|
+
branch: evidence.branch || '',
|
|
140
|
+
changedFiles: formatChanges(evidence),
|
|
141
|
+
diffSummary: formatPatch(evidence),
|
|
142
|
+
};
|
|
143
|
+
const rendered = ensureRequiredSections(template, renderTemplate(template, vars), vars).trimEnd() + '\n';
|
|
144
|
+
return {
|
|
145
|
+
prompt: rendered,
|
|
146
|
+
promptHash: sha256(rendered),
|
|
147
|
+
promptTemplateVersion: PROMPT_TEMPLATE_VERSION,
|
|
148
|
+
promptLength: rendered.length,
|
|
149
|
+
vars,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class KnowledgeEvidenceReader {
|
|
154
|
+
constructor(options = {}) {
|
|
155
|
+
this.maxBytes = Number(options.maxBytes || 256 * 1024);
|
|
156
|
+
this.maxFiles = Number(options.maxFiles || 24);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
read(config) {
|
|
160
|
+
const root = config && config.knowledgePath ? path.resolve(config.knowledgePath) : '';
|
|
161
|
+
if (!root || !fs.existsSync(root) || !fs.statSync(root).isDirectory()) return { entries: [], omitted: [] };
|
|
162
|
+
const realRoot = fs.realpathSync(root);
|
|
163
|
+
const files = [];
|
|
164
|
+
const walk = current => {
|
|
165
|
+
for (const entry of fs.readdirSync(current, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
166
|
+
if (entry.isSymbolicLink()) continue;
|
|
167
|
+
const absolute = path.join(current, entry.name);
|
|
168
|
+
if (entry.isDirectory()) walk(absolute);
|
|
169
|
+
else if (entry.isFile() && entry.name.toLowerCase().endsWith('.md')) files.push(absolute);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
walk(root);
|
|
173
|
+
let used = 0;
|
|
174
|
+
const entries = [];
|
|
175
|
+
const omitted = [];
|
|
176
|
+
for (const file of files) {
|
|
177
|
+
const realFile = fs.realpathSync(file);
|
|
178
|
+
const relativeBoundary = path.relative(realRoot, realFile);
|
|
179
|
+
if (relativeBoundary.startsWith('..') || path.isAbsolute(relativeBoundary)) continue;
|
|
180
|
+
const relativePath = path.relative(root, file).replace(/\\/g, '/');
|
|
181
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
182
|
+
const bytes = Buffer.byteLength(content, 'utf8');
|
|
183
|
+
if (entries.length >= this.maxFiles || used + bytes > this.maxBytes) {
|
|
184
|
+
omitted.push(relativePath);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
used += bytes;
|
|
188
|
+
entries.push({ path: relativePath, hash: sha256(content), content });
|
|
189
|
+
}
|
|
190
|
+
return { entries, omitted, totalBytes: used, limitBytes: this.maxBytes, limitFiles: this.maxFiles };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
module.exports = {
|
|
195
|
+
REQUIREMENT_UNAVAILABLE,
|
|
196
|
+
KnowledgeEvidenceReader,
|
|
197
|
+
formatRequirements,
|
|
198
|
+
formatConversationUserTruth,
|
|
199
|
+
formatAssistantEvidence,
|
|
200
|
+
renderCommitPrompt,
|
|
201
|
+
sha256,
|
|
202
|
+
};
|