@sanqianx/project-knowledge 5.1.3 → 5.1.5
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/.claude-plugin/marketplace.json +1 -1
- package/CHANGELOG.md +13 -0
- package/_site/_test/agent-capture-mcp-ui-test.js +21 -7
- package/_site/_test/agent-capture-ui-test.js +4 -3
- package/_site/_test/bin-cli-test.js +3 -2
- package/_site/_test/capture-hook-dedup-test.js +41 -1
- package/_site/_test/cli-version-report-test.js +22 -0
- package/_site/_test/codex-notify-upgrade-test.js +7 -0
- package/_site/_test/fixtures/integration-busy-worker.cjs +11 -3
- package/_site/_test/helpers/parallel-memory-shell.js +13 -2
- package/_site/_test/hook-relocation-rebind-test.js +120 -0
- package/_site/_test/integration-auto-repair-test.js +3 -1
- package/_site/_test/integration-intent-test.js +6 -2
- package/_site/_test/integration-runtime-binding-test.js +4 -4
- package/_site/_test/integration-status-scan-test.js +138 -7
- package/_site/_test/legacy-relocation-recovery-test.js +258 -0
- package/_site/_test/parallel-retrieval-followup-corpus-test.js +6 -4
- package/_site/_test/project-team-ui-test.js +124 -0
- package/_site/_test/recovery-label-contract-test.js +45 -0
- package/_site/lib/capture-hook-config.js +25 -9
- package/_site/lib/claude-plugin-status.js +60 -0
- package/_site/lib/commit-prompt.js +205 -202
- package/_site/lib/commit-reconciler.js +551 -444
- package/_site/lib/contracts.js +1 -0
- package/_site/lib/evidence-bundle.js +32 -5
- package/_site/lib/integration-auto-repair.js +3 -2
- package/_site/lib/integration-installer.js +153 -71
- package/_site/lib/integration-intent.js +12 -5
- package/_site/lib/integration-status-scan.js +101 -41
- package/_site/lib/integration-status-worker.js +4 -2
- package/_site/lib/knowledge-promotion.js +547 -435
- package/_site/lib/module-bridge.js +1 -1
- package/_site/lib/scanner.js +5 -3
- package/_site/lib/server-app.js +34 -7
- package/_site/scripts/refresh-runtime-manifest.js +1 -1
- package/bin/project-knowledge.js +51 -1
- package/module-runtime-manifest.json +84 -84
- package/package.json +6 -6
- 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/ui/product.css +1 -0
- package/ui/product.js +125 -30
|
@@ -20,8 +20,15 @@ function bridgeBinding(args, options) {
|
|
|
20
20
|
if (args.length !== 2 && !(args.length === 4 && args[2] === '--source' && ['claude-code', 'claude', 'zcode', 'codex', 'opencode'].includes(args[3]))) return '';
|
|
21
21
|
const actual = normalized(args[1]);
|
|
22
22
|
const current = normalized(path.join(options.bridgeHomeDir, 'bin', 'bridge-hook.cjs'));
|
|
23
|
-
const
|
|
24
|
-
|
|
23
|
+
const homeDir = options.homeDir || os.homedir();
|
|
24
|
+
// The default data-dir rename does not rewrite absolute client hooks.
|
|
25
|
+
// Match only our two historical defaults in this account; custom Bridge
|
|
26
|
+
// homes and other accounts remain untouched. An explicit v5 home is current.
|
|
27
|
+
const legacy = [
|
|
28
|
+
path.join(homeDir, '.ai-coding-event-bridge', 'bin', 'bridge-hook.cjs'),
|
|
29
|
+
path.join(homeDir, '.project-knowledge-v5', 'events', 'bin', 'bridge-hook.cjs'),
|
|
30
|
+
].map(normalized);
|
|
31
|
+
return actual === current ? 'current' : legacy.includes(actual) ? 'legacy' : '';
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
function hookArgs(hook) {
|
|
@@ -32,11 +39,20 @@ function hookArgs(hook) {
|
|
|
32
39
|
return match ? [match[1] || match[2] || match[3], match[4] || match[5] || match[6], ...(match[7] ? ['--source', match[7]] : [])] : null;
|
|
33
40
|
}
|
|
34
41
|
|
|
42
|
+
function invalidAgentConfig(message) {
|
|
43
|
+
return Object.assign(new Error(message), { code: 'AGENT_CONFIG_INVALID' });
|
|
44
|
+
}
|
|
45
|
+
|
|
35
46
|
function configuration(file) {
|
|
36
|
-
|
|
47
|
+
let source;
|
|
48
|
+
try { source = fs.readFileSync(file, 'utf8'); }
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (error.code === 'ENOENT') return { source: '', config: {} };
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
37
53
|
let config;
|
|
38
|
-
try { config =
|
|
39
|
-
if (!config || typeof config !== 'object' || Array.isArray(config)) throw
|
|
54
|
+
try { config = JSON.parse(source); } catch { throw invalidAgentConfig('Agent 配置不是有效 JSON,未修改。'); }
|
|
55
|
+
if (!config || typeof config !== 'object' || Array.isArray(config)) throw invalidAgentConfig('Agent 配置必须是 JSON 对象,未修改。');
|
|
40
56
|
return { source, config };
|
|
41
57
|
}
|
|
42
58
|
|
|
@@ -44,17 +60,17 @@ function planCaptureHooks(file, client, options, removeCurrent = false) {
|
|
|
44
60
|
if (!['claude', 'zcode'].includes(client)) throw new Error('不支持的采集配置类型。');
|
|
45
61
|
const { source, config } = configuration(file);
|
|
46
62
|
const hooks = config.hooks;
|
|
47
|
-
if (hooks != null && (typeof hooks !== 'object' || Array.isArray(hooks))) throw
|
|
63
|
+
if (hooks != null && (typeof hooks !== 'object' || Array.isArray(hooks))) throw invalidAgentConfig('Agent hooks 配置格式异常,未修改。');
|
|
48
64
|
const events = client === 'zcode' ? hooks?.events : hooks;
|
|
49
|
-
if (events != null && (typeof events !== 'object' || Array.isArray(events))) throw
|
|
65
|
+
if (events != null && (typeof events !== 'object' || Array.isArray(events))) throw invalidAgentConfig('Agent 事件配置格式异常,未修改。');
|
|
50
66
|
const removed = [], counts = {};
|
|
51
67
|
for (const event of EVENTS) {
|
|
52
68
|
const groups = events?.[event];
|
|
53
69
|
if (groups == null) { counts[event] = { current: 0, legacy: 0 }; continue; }
|
|
54
|
-
if (!Array.isArray(groups)) throw
|
|
70
|
+
if (!Array.isArray(groups)) throw invalidAgentConfig('Agent 事件列表格式异常,未修改。');
|
|
55
71
|
const count = { current: 0, legacy: 0 };
|
|
56
72
|
for (const group of groups) {
|
|
57
|
-
if (group?.hooks != null && !Array.isArray(group.hooks)) throw
|
|
73
|
+
if (group?.hooks != null && !Array.isArray(group.hooks)) throw invalidAgentConfig('Agent Hook 列表格式异常,未修改。');
|
|
58
74
|
for (const hook of group?.hooks || []) { const binding = bridgeBinding(hookArgs(hook), options); if (binding) count[binding]++; }
|
|
59
75
|
}
|
|
60
76
|
counts[event] = count;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const { parse } = require('jsonc-parser');
|
|
6
|
+
|
|
7
|
+
function isObject(value) {
|
|
8
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function readObject(file) {
|
|
12
|
+
let contents;
|
|
13
|
+
try { contents = fs.readFileSync(file, 'utf8'); }
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error.code === 'ENOENT') return null;
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
const errors = [];
|
|
19
|
+
const value = parse(contents.replace(/^\uFEFF/, ''), errors, { allowTrailingComma: true });
|
|
20
|
+
if (errors.length || !isObject(value)) {
|
|
21
|
+
throw Object.assign(new Error('Invalid Claude Code settings or plugin inventory.'), { code: 'CLAUDE_CONFIG_INVALID' });
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function workspaceKey(directory) {
|
|
27
|
+
if (typeof directory !== 'string' || !path.isAbsolute(directory)) return null;
|
|
28
|
+
const key = path.resolve(directory);
|
|
29
|
+
return process.platform === 'win32' ? key.toLowerCase() : key;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readClaudePluginStatus({ configDir, settingsFile, pluginId, cwd }) {
|
|
33
|
+
const index = readObject(path.join(configDir, 'plugins', 'installed_plugins.json'));
|
|
34
|
+
const records = index?.plugins?.[pluginId] ?? [];
|
|
35
|
+
if ((index && (!isObject(index.plugins) || ![undefined, 1, 2].includes(index.version)))
|
|
36
|
+
|| !Array.isArray(records) || records.some(record => !isObject(record)
|
|
37
|
+
|| typeof record.version !== 'string' || !workspaceKey(record.installPath)
|
|
38
|
+
|| (record.scope && !['user', 'project', 'local'].includes(record.scope)))) {
|
|
39
|
+
throw Object.assign(new Error('Unrecognized Claude Code plugin inventory.'), { code: 'CLAUDE_PLUGIN_INDEX_UNSUPPORTED' });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const workspace = workspaceKey(cwd);
|
|
43
|
+
const applicable = records.filter(record => !record.scope || record.scope === 'user'
|
|
44
|
+
|| (workspace && workspace === workspaceKey(record.projectPath)));
|
|
45
|
+
const record = applicable.find(row => row.scope === 'local')
|
|
46
|
+
|| applicable.find(row => row.scope === 'project') || applicable[0];
|
|
47
|
+
const files = [settingsFile];
|
|
48
|
+
if (workspace) files.push(path.join(cwd, '.claude', 'settings.json'), path.join(cwd, '.claude', 'settings.local.json'));
|
|
49
|
+
let enabled = true;
|
|
50
|
+
let hooksDisabled = false;
|
|
51
|
+
for (const file of files) {
|
|
52
|
+
const settings = readObject(file);
|
|
53
|
+
const pluginEnabled = settings?.enabledPlugins?.[pluginId];
|
|
54
|
+
if (typeof pluginEnabled === 'boolean') enabled = pluginEnabled;
|
|
55
|
+
if (typeof settings?.disableAllHooks === 'boolean') hooksDisabled = settings.disableAllHooks;
|
|
56
|
+
}
|
|
57
|
+
return { plugin: record ? { ...record, enabled } : null, hooksDisabled };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = { readClaudePluginStatus };
|
|
@@ -1,202 +1,205 @@
|
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
`
|
|
93
|
-
`
|
|
94
|
-
|
|
95
|
-
'
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
+
// The frozen prompt stays valid across data-directory relocations: it names
|
|
88
|
+
// the manifest relative to the evidence root, never an absolute path. The
|
|
89
|
+
// execution contract delivers the current absolute location for this run.
|
|
90
|
+
return [
|
|
91
|
+
'[EXACT PATCH AVAILABLE AS READ-ONLY CHUNKS]',
|
|
92
|
+
`fullPatchHash: ${evidence.patchHash}`,
|
|
93
|
+
`fullPatchBytes: ${evidence.patchBytes}`,
|
|
94
|
+
`patchManifestHash: ${bundle.manifestHash || '(unavailable)'}`,
|
|
95
|
+
'patchManifestPath: patch-manifest.json (inside this run\'s frozen evidence root; the execution contract states its absolute location)',
|
|
96
|
+
`chunkCount: ${bundle.chunkCount == null ? '(unavailable)' : bundle.chunkCount}`,
|
|
97
|
+
'Read patch-manifest.json, then Read exact patch chunks (patches/NNNNNN.patch) in manifest sequence from the frozen evidence root. The chunks cover the full Git diff.',
|
|
98
|
+
'Do not read the source worktree and do not infer unread evidence.',
|
|
99
|
+
].join('\n');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function formatExistingKnowledge(existing) {
|
|
103
|
+
if (!existing || !Array.isArray(existing.entries) || existing.entries.length === 0) {
|
|
104
|
+
return '(no existing project knowledge was available)';
|
|
105
|
+
}
|
|
106
|
+
const sections = existing.entries.map(entry => `--- ${entry.path} (${entry.hash}) ---\n${entry.content}`);
|
|
107
|
+
if (existing.omitted && existing.omitted.length) {
|
|
108
|
+
sections.push(`[EXISTING KNOWLEDGE OMITTED: ${existing.omitted.join(', ')}]`);
|
|
109
|
+
}
|
|
110
|
+
return sections.join('\n\n');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function ensureRequiredSections(template, rendered, vars) {
|
|
114
|
+
const missing = REQUIRED_SECTIONS.filter(([name]) => !new RegExp(`\\{\\{\\s*${name}\\s*\\}\\}`, 'i').test(template));
|
|
115
|
+
if (!missing.length) return rendered;
|
|
116
|
+
const footer = missing.map(([name, label]) => `${label}:\n${vars[name]}`).join('\n\n');
|
|
117
|
+
return `${rendered.trimEnd()}\n\n${footer}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function renderCommitPrompt(input = {}) {
|
|
121
|
+
const config = input.config || {};
|
|
122
|
+
const evidence = input.evidence || {};
|
|
123
|
+
const automation = normalizeAutomationConfig(config.automation);
|
|
124
|
+
const template = automation.commitPromptTemplate || DEFAULT_COMMIT_PROMPT_TEMPLATE;
|
|
125
|
+
const vars = {
|
|
126
|
+
userRequirement: input.conversationSnapshot
|
|
127
|
+
? formatConversationUserTruth(input.conversationSnapshot)
|
|
128
|
+
: formatRequirements(input.requirements),
|
|
129
|
+
aiConversationEvidence: formatAssistantEvidence(input.conversationSnapshot),
|
|
130
|
+
commitMetadata: formatCommitMetadata(evidence),
|
|
131
|
+
actualChanges: formatChanges(evidence),
|
|
132
|
+
actualPatch: formatPatch(evidence),
|
|
133
|
+
existingKnowledge: formatExistingKnowledge(input.existingKnowledge),
|
|
134
|
+
projectId: config.projectId || input.projectId || '',
|
|
135
|
+
displayName: config.displayName || config.projectId || '',
|
|
136
|
+
knowledgePath: config.knowledgePath || '',
|
|
137
|
+
repoPath: config.repoPath || '',
|
|
138
|
+
commitHash: evidence.commitSha || '',
|
|
139
|
+
commitSubject: evidence.subject || '',
|
|
140
|
+
commitAuthor: evidence.author || '',
|
|
141
|
+
commitDate: evidence.date || '',
|
|
142
|
+
branch: evidence.branch || '',
|
|
143
|
+
changedFiles: formatChanges(evidence),
|
|
144
|
+
diffSummary: formatPatch(evidence),
|
|
145
|
+
};
|
|
146
|
+
const rendered = ensureRequiredSections(template, renderTemplate(template, vars), vars).trimEnd() + '\n';
|
|
147
|
+
return {
|
|
148
|
+
prompt: rendered,
|
|
149
|
+
promptHash: sha256(rendered),
|
|
150
|
+
promptTemplateVersion: PROMPT_TEMPLATE_VERSION,
|
|
151
|
+
promptLength: rendered.length,
|
|
152
|
+
vars,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
class KnowledgeEvidenceReader {
|
|
157
|
+
constructor(options = {}) {
|
|
158
|
+
this.maxBytes = Number(options.maxBytes || 256 * 1024);
|
|
159
|
+
this.maxFiles = Number(options.maxFiles || 24);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
read(config) {
|
|
163
|
+
const root = config && config.knowledgePath ? path.resolve(config.knowledgePath) : '';
|
|
164
|
+
if (!root || !fs.existsSync(root) || !fs.statSync(root).isDirectory()) return { entries: [], omitted: [] };
|
|
165
|
+
const realRoot = fs.realpathSync(root);
|
|
166
|
+
const files = [];
|
|
167
|
+
const walk = current => {
|
|
168
|
+
for (const entry of fs.readdirSync(current, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
169
|
+
if (entry.isSymbolicLink()) continue;
|
|
170
|
+
const absolute = path.join(current, entry.name);
|
|
171
|
+
if (entry.isDirectory()) walk(absolute);
|
|
172
|
+
else if (entry.isFile() && entry.name.toLowerCase().endsWith('.md')) files.push(absolute);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
walk(root);
|
|
176
|
+
let used = 0;
|
|
177
|
+
const entries = [];
|
|
178
|
+
const omitted = [];
|
|
179
|
+
for (const file of files) {
|
|
180
|
+
const realFile = fs.realpathSync(file);
|
|
181
|
+
const relativeBoundary = path.relative(realRoot, realFile);
|
|
182
|
+
if (relativeBoundary.startsWith('..') || path.isAbsolute(relativeBoundary)) continue;
|
|
183
|
+
const relativePath = path.relative(root, file).replace(/\\/g, '/');
|
|
184
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
185
|
+
const bytes = Buffer.byteLength(content, 'utf8');
|
|
186
|
+
if (entries.length >= this.maxFiles || used + bytes > this.maxBytes) {
|
|
187
|
+
omitted.push(relativePath);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
used += bytes;
|
|
191
|
+
entries.push({ path: relativePath, hash: sha256(content), content });
|
|
192
|
+
}
|
|
193
|
+
return { entries, omitted, totalBytes: used, limitBytes: this.maxBytes, limitFiles: this.maxFiles };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = {
|
|
198
|
+
REQUIREMENT_UNAVAILABLE,
|
|
199
|
+
KnowledgeEvidenceReader,
|
|
200
|
+
formatRequirements,
|
|
201
|
+
formatConversationUserTruth,
|
|
202
|
+
formatAssistantEvidence,
|
|
203
|
+
renderCommitPrompt,
|
|
204
|
+
sha256,
|
|
205
|
+
};
|