@sanqianx/project-knowledge 4.8.0 → 5.0.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/.claude-plugin/marketplace.json +1 -1
- package/CHANGELOG.md +19 -0
- package/README.md +11 -6
- package/_modules/knowledge-engine/README.md +9 -6
- package/_modules/knowledge-engine/package.json +1 -1
- package/_modules/knowledge-engine/src/coordinator.js +11 -3
- package/_modules/knowledge-engine/src/server.js +9 -10
- package/_modules/knowledge-engine/src/store.js +9 -12
- package/_modules/knowledge-engine/ui/app.js +1 -1
- package/_site/_test/fresh-hook-reimport-test.js +53 -0
- package/_site/_test/fresh-product-data-test.js +42 -0
- package/_site/_test/module-model-configuration-test.js +2 -1
- package/_site/_test/tracking-start-test.js +1 -0
- package/_site/lib/data-dir.js +13 -81
- package/_site/lib/engine-connection.js +67 -0
- package/_site/lib/hook-manager.js +1 -1
- package/_site/lib/integration-installer.js +2 -1
- package/_site/lib/module-bridge.js +6 -16
- package/_site/lib/product-data.js +24 -0
- package/_site/lib/project-lifecycle-service.js +18 -2
- package/_site/lib/server-app.js +11 -22
- package/docs/README.zh-CN.md +8 -4
- package/docs/fresh-v5-start.md +28 -0
- package/module-runtime-manifest.json +2 -2
- package/package.json +2 -1
- 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/index.html +2 -1
- package/ui/product.js +41 -15
- package/vendor-manifest.json +7 -8
- package/_modules/knowledge-engine/src/migration.js +0 -88
- package/_site/_test/data-dir-migration-test.js +0 -77
- package/_site/_test/knowledge-migration-test.js +0 -75
- package/_site/_test/legacy-forward-compat-test.js +0 -257
- package/_site/_test/legacy-project-upgrade-e2e-test.js +0 -363
- package/_site/_test/p0-data-migration-characterization-test.js +0 -37
- package/_site/_test/p0-e2e-gate-test.js +0 -347
- package/_site/_test/project-layout-v2-migration-test.js +0 -127
- package/_site/_test/server-runtime-migration-safety-test.js +0 -27
- package/_site/_test/v4122-upgrade-data-contract-test.js +0 -56
- package/_site/lib/data-state-classifier.js +0 -40
- package/_site/lib/engine-cutover.js +0 -97
- package/_site/lib/legacy-data-manifest.js +0 -26
- package/_site/lib/migration-service.js +0 -442
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const fs = require('node:fs');
|
|
3
|
-
const path = require('node:path');
|
|
4
|
-
const crypto = require('node:crypto');
|
|
5
|
-
const AtomicFile = require('./runtime/atomic-file');
|
|
6
|
-
const { validateClaim } = require('./runtime/commit-reconciler');
|
|
7
|
-
const { validateSha } = require('./runtime/scanner');
|
|
8
|
-
const { initialRuntime } = require('./store');
|
|
9
|
-
|
|
10
|
-
// Only frozen analysis artifacts cross this boundary, never shell settings,
|
|
11
|
-
// registries or credentials. Canonical knowledge files stay where they are.
|
|
12
|
-
async function importLegacyState(coordinator, input) {
|
|
13
|
-
const { store, layout } = coordinator;
|
|
14
|
-
if (!/^cutover-[a-f0-9-]{36}$/.test(input.migrationId || '') || !path.isAbsolute(input.sourceDataDir || '') || !Array.isArray(input.projects)) throw failure('INVALID_ARGUMENT');
|
|
15
|
-
if (store.state.migrations?.includes(input.migrationId)) return { reused: true };
|
|
16
|
-
if (coordinator.executionEnabled || coordinator.active.size) throw failure('ENGINE_NOT_PAUSED');
|
|
17
|
-
const backup = path.join(input.sourceDataDir, 'recovery', 'engine-cutover', input.migrationId);
|
|
18
|
-
if (!fs.existsSync(path.join(backup, 'snapshot.json'))) throw failure('MIGRATION_BACKUP_MISSING');
|
|
19
|
-
const snapshot = AtomicFile.readJsonStrict(path.join(backup, 'snapshot.json'));
|
|
20
|
-
if (snapshot.migrationId !== input.migrationId || snapshot.sourceDataDir !== input.sourceDataDir) throw failure('MIGRATION_BACKUP_MISMATCH');
|
|
21
|
-
input = snapshot;
|
|
22
|
-
const mappings = ['runs', 'claims', 'promotions', 'processed-commits'].map(kind => [path.join(input.sourceDataDir, 'runtime', kind), layout.getRuntimePath(kind)]);
|
|
23
|
-
mappings.push([path.join(input.sourceDataDir, 'recovery', 'promotions'), layout.getRecoveryPath('promotions')]);
|
|
24
|
-
for (const projected of input.projects) mappings.push([path.join(input.sourceDataDir, 'projects', projected.registration.projectId), layout.getProjectMetadataDir(projected.registration.projectId)]);
|
|
25
|
-
function remap(value) {
|
|
26
|
-
if (Array.isArray(value)) return value.map(remap);
|
|
27
|
-
if (value && typeof value === 'object') return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, remap(item)]));
|
|
28
|
-
if (typeof value === 'string' && path.isAbsolute(value) && !value.includes('\n')) {
|
|
29
|
-
const mapping = mappings.find(([from]) => layout.isPathInside(from, value));
|
|
30
|
-
if (mapping) return path.join(mapping[1], path.relative(mapping[0], value));
|
|
31
|
-
}
|
|
32
|
-
return value;
|
|
33
|
-
}
|
|
34
|
-
function copy(source, target) {
|
|
35
|
-
if (!fs.existsSync(source)) return;
|
|
36
|
-
const stat = fs.lstatSync(source);
|
|
37
|
-
if (stat.isSymbolicLink() || !layout.isPathInside(backup, source, { realpath: true }) || !layout.isPathInside(store.dataDir, target, { realpath: true })) throw failure('MIGRATION_UNSAFE_PATH');
|
|
38
|
-
if (stat.isDirectory()) {
|
|
39
|
-
for (const entry of fs.readdirSync(source)) copy(path.join(source, entry), path.join(target, entry));
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (!stat.isFile()) throw failure('MIGRATION_UNSAFE_PATH');
|
|
43
|
-
let bytes = fs.readFileSync(source);
|
|
44
|
-
if (source.endsWith('.json')) bytes = Buffer.from(JSON.stringify(remap(JSON.parse(bytes.toString('utf8'))), null, 2));
|
|
45
|
-
if (fs.existsSync(target)) {
|
|
46
|
-
if (!fs.readFileSync(target).equals(bytes)) throw failure('MIGRATION_ARTIFACT_CONFLICT');
|
|
47
|
-
} else AtomicFile.writeFileAtomic(target, bytes);
|
|
48
|
-
}
|
|
49
|
-
for (const kind of ['runs', 'claims', 'promotions', 'processed-commits']) copy(path.join(backup, 'runtime', kind), layout.getRuntimePath(kind));
|
|
50
|
-
copy(path.join(backup, 'recovery', 'promotions'), layout.getRecoveryPath('promotions'));
|
|
51
|
-
for (const { registration, runtime } of input.projects) {
|
|
52
|
-
const project = store.addProject(registration).project;
|
|
53
|
-
if (project.migrationId === input.migrationId) continue;
|
|
54
|
-
if (store.state.runs.some(run => run.projectId === project.projectId)) throw failure('MIGRATION_PROJECT_ALREADY_ACTIVE');
|
|
55
|
-
const next = initialRuntime(registration);
|
|
56
|
-
if (runtime.lastAnalyzedCommit) next.lastAnalyzedCommit = validateSha(runtime.lastAnalyzedCommit);
|
|
57
|
-
next.conversationBaselineCursor = runtime.conversationBaselineCursor ?? next.conversationBaselineCursor;
|
|
58
|
-
if (runtime.conversationBaselineCursors) {
|
|
59
|
-
if (Object.entries(runtime.conversationBaselineCursors).some(([journal, cursor]) => !/^(global|sha256:[a-f0-9]{64})$/.test(journal) || !Number.isSafeInteger(cursor) || cursor < 0)) throw failure('MIGRATION_INVALID_BASELINE');
|
|
60
|
-
next.conversationBaselineCursors = { ...runtime.conversationBaselineCursors };
|
|
61
|
-
}
|
|
62
|
-
next.analysis.consumedRequirementIds = (runtime.analysis?.consumedRequirementIds || []).filter(id => typeof id === 'string');
|
|
63
|
-
next.index.dirty = runtime.index?.dirty === true;
|
|
64
|
-
const claim = runtime.analysis?.activeClaim;
|
|
65
|
-
if (claim) {
|
|
66
|
-
validateClaim(claim, project.projectId, validateSha(claim.commitSha));
|
|
67
|
-
const claimKeys = ['schema', 'projectId', 'commitSha', 'parents', 'triggerFirstSeen', 'requirementIds', 'requirementBinding', 'conversationSnapshotHash', 'conversationTurnIds', 'conversationEventIds', 'promptTemplateVersion', 'promptHash', 'patchHash', 'evidenceHash', 'evidenceManifestHash', 'retrievalManifestHash', 'knowledgePath', 'runId', 'operationId', 'phase', 'attempt', 'fingerprint'];
|
|
68
|
-
next.analysis.activeClaim = remap(Object.fromEntries(claimKeys.filter(key => claim[key] !== undefined).map(key => [key, claim[key]])));
|
|
69
|
-
next.analysis.status = claim.phase;
|
|
70
|
-
}
|
|
71
|
-
for (const name of ['conversation-events.jsonl', 'commit-boundaries', 'commit-conversations']) copy(path.join(backup, 'projects', project.projectId, name), path.join(layout.getProjectMetadataDir(project.projectId), name));
|
|
72
|
-
project.runtime = next;
|
|
73
|
-
project.migrationId = input.migrationId;
|
|
74
|
-
store.save();
|
|
75
|
-
if (claim) {
|
|
76
|
-
const run = store.addRun({ projectId: project.projectId, commitSha: claim.commitSha, trigger: 'upgrade-recovery' });
|
|
77
|
-
store.updateRun(run.runId, { analysisRunId: claim.runId, phase: 'recovering' });
|
|
78
|
-
} else if (next.index.dirty && next.lastAnalyzedCommit) {
|
|
79
|
-
const run = store.addRun({ projectId: project.projectId, commitSha: next.lastAnalyzedCommit, trigger: 'upgrade-index-recovery' });
|
|
80
|
-
store.updateRun(run.runId, { promotedAt: new Date().toISOString(), phase: 'index-pending', state: 'index-pending', indexState: 'failed' });
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
store.state.migrations = [...(store.state.migrations || []), input.migrationId];
|
|
84
|
-
store.save();
|
|
85
|
-
return { migrated: input.projects.length, fingerprint: crypto.createHash('sha256').update(JSON.stringify(input.projects.map(p => [p.registration.projectId, p.runtime.lastAnalyzedCommit]))).digest('hex') };
|
|
86
|
-
}
|
|
87
|
-
function failure(code) { return Object.assign(new Error('运行迁移未完成,原数据与升级备份已保留。'), { code, status: 409 }); }
|
|
88
|
-
module.exports = { importLegacyState };
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const os = require('os');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const dataDir = require('../lib/data-dir');
|
|
6
|
-
const { LEGACY_ASSETS } = require('../lib/legacy-data-manifest');
|
|
7
|
-
|
|
8
|
-
function temp(name) { return fs.mkdtempSync(path.join(os.tmpdir(), `pk-data-dir-${name}-`)); }
|
|
9
|
-
function clean(dir) { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
10
|
-
function write(file, value = '{}') { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, value); }
|
|
11
|
-
|
|
12
|
-
(() => {
|
|
13
|
-
// Resolution must be diagnostic-safe: resolving never creates the candidate.
|
|
14
|
-
const missing = path.join(temp('resolve'), 'not-created');
|
|
15
|
-
try {
|
|
16
|
-
assert.equal(dataDir.resolveDataDirPath({ dataDir: missing }), path.resolve(missing));
|
|
17
|
-
assert.equal(fs.existsSync(missing), false);
|
|
18
|
-
dataDir.ensureDataDir(missing);
|
|
19
|
-
assert.equal(fs.existsSync(missing), true);
|
|
20
|
-
} finally { clean(path.dirname(missing)); }
|
|
21
|
-
|
|
22
|
-
const legacy = temp('legacy');
|
|
23
|
-
const target = temp('target');
|
|
24
|
-
const saved = process.env.KB_DATA_DIR;
|
|
25
|
-
try {
|
|
26
|
-
write(path.join(legacy, 'projects.json'), JSON.stringify({ old: { repoPath: 'C:/legacy' } }));
|
|
27
|
-
write(path.join(legacy, 'ai-profiles.json'), JSON.stringify({ profiles: [{ id: 'primary', apiKey: 'test-secret' }] }));
|
|
28
|
-
write(path.join(legacy, 'knowledge-store.json'), JSON.stringify({ rootPath: 'C:/knowledge' }));
|
|
29
|
-
write(path.join(legacy, 'embedding-config.json'), JSON.stringify({ modelId: 'model', localModelPath: 'C:/models', localFilesOnly: true }));
|
|
30
|
-
write(path.join(legacy, 'knowledge-scopes.json'), JSON.stringify({ scopes: ['p'] }));
|
|
31
|
-
write(path.join(legacy, 'models', 'nested', 'model.onnx'), 'model-bytes');
|
|
32
|
-
// An empty target registry is non-authoritative and must not suppress legacy data.
|
|
33
|
-
write(path.join(target, 'projects.json'), '{}');
|
|
34
|
-
process.env.KB_DATA_DIR = target;
|
|
35
|
-
dataDir._resetCache();
|
|
36
|
-
const result = dataDir.migrateFromLegacy({ legacyRoot: legacy });
|
|
37
|
-
assert.equal(result.ok, true);
|
|
38
|
-
assert.equal(result.migrated, true);
|
|
39
|
-
assert.equal(JSON.parse(fs.readFileSync(path.join(target, 'projects.json'))).old.repoPath, 'C:/legacy');
|
|
40
|
-
assert.equal(JSON.parse(fs.readFileSync(path.join(target, 'ai-profiles.json'))).profiles.length, 1);
|
|
41
|
-
assert.equal(JSON.parse(fs.readFileSync(path.join(target, 'embedding-config.json'))).modelId, 'model');
|
|
42
|
-
assert.equal(fs.readFileSync(path.join(target, 'models', 'nested', 'model.onnx'), 'utf8'), 'model-bytes');
|
|
43
|
-
assert(LEGACY_ASSETS.some(asset => asset.source === 'embedding-config.json'));
|
|
44
|
-
assert(LEGACY_ASSETS.some(asset => asset.source === 'models'));
|
|
45
|
-
} finally { process.env.KB_DATA_DIR = saved; dataDir._resetCache(); clean(legacy); clean(target); }
|
|
46
|
-
|
|
47
|
-
const source = temp('conflict-source');
|
|
48
|
-
const destination = temp('conflict-destination');
|
|
49
|
-
try {
|
|
50
|
-
write(path.join(source, 'projects.json'), JSON.stringify({ old: { repoPath: 'C:/old' } }));
|
|
51
|
-
write(path.join(destination, 'projects.json'), JSON.stringify({ newer: { repoPath: 'C:/new' } }));
|
|
52
|
-
process.env.KB_DATA_DIR = destination;
|
|
53
|
-
dataDir._resetCache();
|
|
54
|
-
const result = dataDir.migrateFromLegacy({ legacyRoot: source });
|
|
55
|
-
assert.equal(result.ok, false);
|
|
56
|
-
assert.equal(result.requiresManualRecovery, true);
|
|
57
|
-
assert.equal(JSON.parse(fs.readFileSync(path.join(destination, 'projects.json'))).newer.repoPath, 'C:/new');
|
|
58
|
-
} finally { process.env.KB_DATA_DIR = saved; dataDir._resetCache(); clean(source); clean(destination); }
|
|
59
|
-
|
|
60
|
-
// The installed package can contain empty scaffolding directories (for
|
|
61
|
-
// example projects/) while the user data directory contains real projects.
|
|
62
|
-
// Empty package scaffolding is not a legacy source and must not block startup.
|
|
63
|
-
const packageRoot = temp('empty-package-root');
|
|
64
|
-
const userData = temp('existing-user-data');
|
|
65
|
-
try {
|
|
66
|
-
fs.mkdirSync(path.join(packageRoot, 'projects'), { recursive: true });
|
|
67
|
-
write(path.join(userData, 'projects', 'project-real', 'config.json'), JSON.stringify({ id: 'project-real' }));
|
|
68
|
-
process.env.KB_DATA_DIR = userData;
|
|
69
|
-
dataDir._resetCache();
|
|
70
|
-
const result = dataDir.migrateFromLegacy({ legacyRoot: packageRoot });
|
|
71
|
-
assert.equal(result.ok, true, 'empty package scaffolding must not be a migration conflict');
|
|
72
|
-
assert.equal(result.migrated, false);
|
|
73
|
-
assert.equal(result.reason, 'no legacy assets found');
|
|
74
|
-
assert.equal(fs.existsSync(path.join(userData, 'projects', 'project-real', 'config.json')), true);
|
|
75
|
-
} finally { process.env.KB_DATA_DIR = saved; dataDir._resetCache(); clean(packageRoot); clean(userData); }
|
|
76
|
-
console.log('data-dir-migration-test PASS');
|
|
77
|
-
})();
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const os = require('os');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const { StorageLayout } = require('../lib/storage-layout');
|
|
6
|
-
const { SettingsStore } = require('../lib/settings-store');
|
|
7
|
-
const { ProjectStore } = require('../lib/project-store');
|
|
8
|
-
const { MigrationService } = require('../lib/migration-service');
|
|
9
|
-
const { KnowledgeDatabase } = require('../lib/knowledge-db');
|
|
10
|
-
const { EMBEDDING_DIMENSIONS } = require('../lib/knowledge-schema');
|
|
11
|
-
|
|
12
|
-
(async () => {
|
|
13
|
-
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'knowledge-migration-v2-'));
|
|
14
|
-
const oldKnowledgeRoot = path.join(dataDir, 'old-knowledge');
|
|
15
|
-
const fixedKnowledgePath = path.join(oldKnowledgeRoot, 'api-fixed');
|
|
16
|
-
const legacyIndex = path.join(dataDir, 'knowledge.lancedb');
|
|
17
|
-
fs.mkdirSync(fixedKnowledgePath, { recursive: true });
|
|
18
|
-
fs.writeFileSync(path.join(fixedKnowledgePath, 'GOAL.md'), '# API goal\n\nAuthoritative Markdown remains external.\n', 'utf8');
|
|
19
|
-
fs.writeFileSync(path.join(dataDir, 'projects.json'), JSON.stringify({
|
|
20
|
-
api: {
|
|
21
|
-
displayName: 'API', localPath: dataDir, gitPath: dataDir, kbPath: fixedKnowledgePath,
|
|
22
|
-
trackingStartCommit: 'track-before', lastAnalyzedCommit: 'commit-before', enabled: true,
|
|
23
|
-
},
|
|
24
|
-
}, null, 2));
|
|
25
|
-
fs.writeFileSync(path.join(dataDir, 'knowledge-store.json'), JSON.stringify({ rootPath: oldKnowledgeRoot, configured: true }));
|
|
26
|
-
fs.writeFileSync(path.join(dataDir, 'ai-profiles.json'), JSON.stringify({ schema: 'ai-profiles/v1', profiles: [{ id: 'primary', apiKey: 'migration-secret-value' }] }));
|
|
27
|
-
fs.writeFileSync(path.join(dataDir, 'logging.json'), JSON.stringify({ levels: ['debug', 'info', 'error'], retentionDays: 0, maxTotalSizeMB: 128 }));
|
|
28
|
-
fs.writeFileSync(path.join(dataDir, '.hook-trigger-errors.log'), '2026-01-01 legacy hook error\n', 'utf8');
|
|
29
|
-
|
|
30
|
-
const vector = new Array(EMBEDDING_DIMENSIONS).fill(0);
|
|
31
|
-
vector[9] = 1;
|
|
32
|
-
const oldDatabase = new KnowledgeDatabase({ dbPath: legacyIndex });
|
|
33
|
-
await oldDatabase.replaceEntry('project:api', 'GOAL.md', [{ chunkOrder: 0, title: 'Legacy index', chunkText: 'A migrated derived row.', vector }]);
|
|
34
|
-
await oldDatabase.close();
|
|
35
|
-
const oldIndexRows = await (async () => {
|
|
36
|
-
const db = new KnowledgeDatabase({ dbPath: legacyIndex });
|
|
37
|
-
try { return await db.count(); } finally { await db.close(); }
|
|
38
|
-
})();
|
|
39
|
-
|
|
40
|
-
const layout = new StorageLayout({ dataDir });
|
|
41
|
-
const migration = new MigrationService({
|
|
42
|
-
layout,
|
|
43
|
-
legacyDataDir: dataDir,
|
|
44
|
-
indexValidator: async candidate => {
|
|
45
|
-
const db = new KnowledgeDatabase({ dbPath: candidate });
|
|
46
|
-
try { await db.open(); return { ok: (await db.count()) === oldIndexRows }; }
|
|
47
|
-
finally { await db.close(); }
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
const result = await migration.migrateIfNeeded({ migrationRunId: 'knowledge-index-v2' });
|
|
51
|
-
assert(result.ok && result.completed && result.migrated, JSON.stringify(result));
|
|
52
|
-
const projectId = result.projectMap.api;
|
|
53
|
-
const projectStore = new ProjectStore({ layout });
|
|
54
|
-
const config = projectStore.readConfig(projectId);
|
|
55
|
-
const state = projectStore.readState(projectId);
|
|
56
|
-
assert.strictEqual(config.knowledgePath, fixedKnowledgePath, 'legacy kbPath must be preserved exactly');
|
|
57
|
-
assert.strictEqual(state.trackingStartCommit, 'track-before');
|
|
58
|
-
assert.strictEqual(state.lastAnalyzedCommit, 'commit-before');
|
|
59
|
-
assert.strictEqual(new SettingsStore({ layout }).read().ai.profiles[0].apiKey, 'migration-secret-value');
|
|
60
|
-
const migratedDatabase = new KnowledgeDatabase({ dbPath: layout.getIndexPath() });
|
|
61
|
-
assert.strictEqual(await migratedDatabase.count(), 1, 'the validated legacy index should be copied to the one internal index path');
|
|
62
|
-
await migratedDatabase.close();
|
|
63
|
-
assert(fs.existsSync(legacyIndex), 'legacy index source must remain for rollback');
|
|
64
|
-
assert(fs.existsSync(path.join(result.recoveryDir, 'backup', 'knowledge.lancedb')), 'recovery must retain an index backup');
|
|
65
|
-
assert(fs.existsSync(path.join(dataDir, '.hook-trigger-errors.log')), 'legacy hook logs must remain at the source');
|
|
66
|
-
assert(fs.existsSync(path.join(result.recoveryDir, 'backup', '.hook-trigger-errors.log')));
|
|
67
|
-
|
|
68
|
-
const settings = new SettingsStore({ layout });
|
|
69
|
-
await settings.updatePatch({ knowledge: { rootPath: path.join(dataDir, 'new-knowledge-root') } });
|
|
70
|
-
assert.strictEqual(projectStore.readConfig(projectId).knowledgePath, fixedKnowledgePath, 'changing the global root must not move an imported project');
|
|
71
|
-
assert.strictEqual(layout.getIndexPath(), path.join(dataDir, 'index', 'knowledge.lancedb'), 'global root changes must not move the derived index');
|
|
72
|
-
const second = await migration.migrateIfNeeded();
|
|
73
|
-
assert.strictEqual(second.migrated, false);
|
|
74
|
-
console.log('knowledge-migration-test: PASS');
|
|
75
|
-
})().catch(error => { console.error(error); process.exitCode = 1; });
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
// _site/_test/legacy-forward-compat-test.js
|
|
2
|
-
//
|
|
3
|
-
// T08: v4.1.22 -> main forward-compatibility migration. Builds a realistic
|
|
4
|
-
// two-project legacy fixture with team binding + legacy hook + tracking
|
|
5
|
-
// state, runs MigrationService, and asserts every required field is
|
|
6
|
-
// preserved or deterministically derived. Also runs migrateManagedHooks
|
|
7
|
-
// to confirm the T03 hook state machine is the post-migration reality.
|
|
8
|
-
|
|
9
|
-
const assert = require('assert');
|
|
10
|
-
const fs = require('fs');
|
|
11
|
-
const os = require('os');
|
|
12
|
-
const path = require('path');
|
|
13
|
-
const { spawnSync } = require('child_process');
|
|
14
|
-
|
|
15
|
-
const { StorageLayout } = require('../lib/storage-layout');
|
|
16
|
-
const { MigrationService } = require('../lib/migration-service');
|
|
17
|
-
const { ProjectStore } = require('../lib/project-store');
|
|
18
|
-
const { ProjectRegistryStore } = require('../lib/project-registry-store');
|
|
19
|
-
const { Logger } = require('../lib/structured-logger');
|
|
20
|
-
const { migrateManagedHooks } = require('../lib/server-app');
|
|
21
|
-
const { LEGACY_HOOK_MARKER, readHookStatus } = require('../lib/hook-manager');
|
|
22
|
-
|
|
23
|
-
const ROOT = path.resolve(__dirname, '..', '..');
|
|
24
|
-
const TRIGGER = path.join(ROOT, '_site', 'scripts', 'hook-trigger.js');
|
|
25
|
-
|
|
26
|
-
function git(repo, args) {
|
|
27
|
-
const result = spawnSync('git', ['-C', repo, ...args], {
|
|
28
|
-
encoding: 'utf8', windowsHide: true,
|
|
29
|
-
env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
|
|
30
|
-
});
|
|
31
|
-
if (result.status !== 0) throw new Error(result.stderr || `git ${args.join(' ')} failed: ${result.status}`);
|
|
32
|
-
return String(result.stdout || '').trim();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function fixture({ withTeamBinding, withLegacyHook, knowledgeLanguage, aiProfileId, enabled, repoPath: customRepoPath } = {}) {
|
|
36
|
-
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pk-legacy-forward-compat-'));
|
|
37
|
-
const repoPath = customRepoPath || path.join(dataDir, 'repos', 'legacy-team');
|
|
38
|
-
fs.mkdirSync(repoPath, { recursive: true });
|
|
39
|
-
git(repoPath, ['init', '-q', '-b', 'main']);
|
|
40
|
-
git(repoPath, ['config', 'user.email', 'legacy-fc@example.local']);
|
|
41
|
-
git(repoPath, ['config', 'user.name', 'Legacy FC']);
|
|
42
|
-
fs.writeFileSync(path.join(repoPath, 'README.md'), '# legacy\n');
|
|
43
|
-
git(repoPath, ['add', 'README.md']);
|
|
44
|
-
git(repoPath, ['commit', '-q', '-m', 'initial']);
|
|
45
|
-
const initialSha = git(repoPath, ['rev-parse', 'HEAD']);
|
|
46
|
-
|
|
47
|
-
if (withLegacyHook) {
|
|
48
|
-
const gitDir = git(repoPath, ['rev-parse', '--path-format=absolute', '--git-dir']);
|
|
49
|
-
const hooksDir = path.join(path.resolve(gitDir), 'hooks');
|
|
50
|
-
fs.mkdirSync(hooksDir, { recursive: true });
|
|
51
|
-
fs.writeFileSync(path.join(hooksDir, 'post-commit'), (
|
|
52
|
-
`#!/bin/sh\n${LEGACY_HOOK_MARKER} — auto-installed by KB manager (legacy)\n` +
|
|
53
|
-
`node 'C:/legacy/site/_site/scripts/hook-trigger.js' \\\n` +
|
|
54
|
-
` --kb-root 'C:/legacy/site' --repo "${repoPath.replace(/\\/g, '/')}" >/dev/null 2>&1 || true\n` +
|
|
55
|
-
`exit 0\n`
|
|
56
|
-
), { mode: 0o755 });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Knowledge path
|
|
60
|
-
const kbPath = path.join(dataDir, 'knowledge', 'legacy-team');
|
|
61
|
-
fs.mkdirSync(kbPath, { recursive: true });
|
|
62
|
-
|
|
63
|
-
// Team binding
|
|
64
|
-
const teamBinding = withTeamBinding ? {
|
|
65
|
-
teamKbId: 'team-store/team-a',
|
|
66
|
-
provider: 'github',
|
|
67
|
-
} : null;
|
|
68
|
-
|
|
69
|
-
const projectEntry = {
|
|
70
|
-
schema: 'kb-project/v1',
|
|
71
|
-
displayName: 'Legacy Team',
|
|
72
|
-
localPath: repoPath,
|
|
73
|
-
gitPath: repoPath,
|
|
74
|
-
kbPath,
|
|
75
|
-
trackingStartCommit: initialSha,
|
|
76
|
-
lastAnalyzedCommit: initialSha,
|
|
77
|
-
enabled: enabled !== false,
|
|
78
|
-
aiProfileId: aiProfileId !== undefined ? aiProfileId : 'primary',
|
|
79
|
-
knowledgeLanguage: knowledgeLanguage || 'zh-CN',
|
|
80
|
-
teamBinding,
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
fs.writeFileSync(path.join(dataDir, 'projects.json'), JSON.stringify({
|
|
84
|
-
'legacy-team': projectEntry,
|
|
85
|
-
}, null, 2));
|
|
86
|
-
fs.writeFileSync(path.join(dataDir, 'knowledge-store.json'), JSON.stringify({
|
|
87
|
-
schema: 'knowledge-store/v1',
|
|
88
|
-
rootPath: path.join(dataDir, 'knowledge'),
|
|
89
|
-
configured: true,
|
|
90
|
-
}));
|
|
91
|
-
fs.writeFileSync(path.join(dataDir, 'ai-profiles.json'), JSON.stringify({
|
|
92
|
-
schema: 'ai-profiles/v1',
|
|
93
|
-
profiles: [{ id: 'primary', name: 'Primary', enabled: true, vendor: 'anthropic', model: 'claude-sonnet' }],
|
|
94
|
-
defaultProfileId: 'primary',
|
|
95
|
-
}));
|
|
96
|
-
return { dataDir, repoPath, kbPath, initialSha };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async function run() {
|
|
100
|
-
// Case 1: deterministic projectId + repoPath + knowledgePath + tracking
|
|
101
|
-
{
|
|
102
|
-
const { dataDir, repoPath, kbPath, initialSha } = fixture();
|
|
103
|
-
const layout = new StorageLayout({ dataDir });
|
|
104
|
-
const migration = new MigrationService({ layout, legacyDataDir: dataDir });
|
|
105
|
-
const result = await migration.migrateIfNeeded({ migrationRunId: 'fc-det' });
|
|
106
|
-
assert.strictEqual(result.ok, true);
|
|
107
|
-
const projectId = result.projectMap['legacy-team'];
|
|
108
|
-
assert(projectId, 'deterministic projectId must be produced');
|
|
109
|
-
const config = JSON.parse(fs.readFileSync(layout.getProjectConfigPath(projectId), 'utf8'));
|
|
110
|
-
const state = JSON.parse(fs.readFileSync(layout.getProjectStatePath(projectId), 'utf8'));
|
|
111
|
-
assert.strictEqual(config.repoPath, path.resolve(repoPath), 'repoPath must be preserved (not data-dir fallback)');
|
|
112
|
-
assert.strictEqual(config.knowledgePath, path.resolve(kbPath), 'knowledgePath must be preserved');
|
|
113
|
-
assert.strictEqual(config.enabled, true, 'enabled must be preserved');
|
|
114
|
-
assert.strictEqual(state.trackingStartCommit, initialSha, 'trackingStartCommit must be preserved');
|
|
115
|
-
assert.strictEqual(state.lastAnalyzedCommit, initialSha, 'lastAnalyzedCommit must be preserved');
|
|
116
|
-
// Idempotence: second migration must not re-migrate
|
|
117
|
-
const second = await migration.migrateIfNeeded();
|
|
118
|
-
assert.strictEqual(second.completed, true);
|
|
119
|
-
assert.strictEqual(second.migrated, false);
|
|
120
|
-
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Case 2: knowledgeLanguage + aiProfileId + teamBinding preservation
|
|
124
|
-
{
|
|
125
|
-
const { dataDir, repoPath, kbPath } = fixture({
|
|
126
|
-
withTeamBinding: true, knowledgeLanguage: 'en-US', aiProfileId: 'primary',
|
|
127
|
-
});
|
|
128
|
-
const layout = new StorageLayout({ dataDir });
|
|
129
|
-
const migration = new MigrationService({ layout, legacyDataDir: dataDir });
|
|
130
|
-
const result = await migration.migrateIfNeeded({ migrationRunId: 'fc-team' });
|
|
131
|
-
assert.strictEqual(result.ok, true);
|
|
132
|
-
const projectId = result.projectMap['legacy-team'];
|
|
133
|
-
const config = JSON.parse(fs.readFileSync(layout.getProjectConfigPath(projectId), 'utf8'));
|
|
134
|
-
assert.strictEqual(config.knowledgeLanguage, 'en-US', 'knowledgeLanguage must be preserved');
|
|
135
|
-
assert.strictEqual(config.aiProfileId, 'primary', 'aiProfileId must be preserved');
|
|
136
|
-
assert(config.teamBinding && config.teamBinding.teamKbId === 'team-store/team-a', 'teamBinding must be preserved');
|
|
137
|
-
assert.strictEqual(config.teamBinding.provider, 'github');
|
|
138
|
-
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Case 3: AI profile semantics via T01 — stale project aiProfileId
|
|
142
|
-
// falls back to default or first-usable. Here legacy has aiProfileId='stale'
|
|
143
|
-
// but the global default is 'primary'; the resolver must surface 'primary'
|
|
144
|
-
// when CommitReconciler runs. (The migration itself preserves the stale
|
|
145
|
-
// id; the resolver is what fixes it at runtime.)
|
|
146
|
-
{
|
|
147
|
-
const { dataDir, repoPath } = fixture({ aiProfileId: 'stale-deleted-profile' });
|
|
148
|
-
const layout = new StorageLayout({ dataDir });
|
|
149
|
-
const migration = new MigrationService({ layout, legacyDataDir: dataDir });
|
|
150
|
-
const result = await migration.migrateIfNeeded({ migrationRunId: 'fc-stale-profile' });
|
|
151
|
-
const projectId = result.projectMap['legacy-team'];
|
|
152
|
-
const config = JSON.parse(fs.readFileSync(layout.getProjectConfigPath(projectId), 'utf8'));
|
|
153
|
-
assert.strictEqual(config.aiProfileId, 'stale-deleted-profile', 'migration preserves whatever was in the legacy record; resolver handles fallback at runtime');
|
|
154
|
-
const { resolveEffectiveAiProfile } = require('../lib/ai-profile-resolver');
|
|
155
|
-
const settings = JSON.parse(fs.readFileSync(layout.getSettingsPath(), 'utf8'));
|
|
156
|
-
const resolved = resolveEffectiveAiProfile(settings, config);
|
|
157
|
-
assert.strictEqual(resolved.profileId, 'primary', 'T01 resolver must fall back to default when project id is stale');
|
|
158
|
-
assert.strictEqual(resolved.source, 'default');
|
|
159
|
-
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Case 4: hook migration via T03 — legacy v1 hook present, must be replaced
|
|
163
|
-
// and project state must report verified v2 after migrateManagedHooks.
|
|
164
|
-
{
|
|
165
|
-
const { dataDir, repoPath } = fixture({ withLegacyHook: true });
|
|
166
|
-
const layout = new StorageLayout({ dataDir });
|
|
167
|
-
const migration = new MigrationService({ layout, legacyDataDir: dataDir });
|
|
168
|
-
const result = await migration.migrateIfNeeded({ migrationRunId: 'fc-hook' });
|
|
169
|
-
const projectId = result.projectMap['legacy-team'];
|
|
170
|
-
const projectStore = new ProjectStore({ layout });
|
|
171
|
-
const registryStore = new ProjectRegistryStore({ layout });
|
|
172
|
-
await registryStore.initialize();
|
|
173
|
-
const logger = new Logger({ layout, settingsProvider: () => ({ levels: [] }), context: { component: 'fc' } });
|
|
174
|
-
const runtime = { registryStore, projectStore, logger, layout, analysisOwner: 'legacy', bridgeAdapter: { hookBinding: () => ({ dataDir: layout.getDataDir() }) } };
|
|
175
|
-
const hooks = await migrateManagedHooks(runtime);
|
|
176
|
-
const entry = hooks.find(item => item.projectId === projectId);
|
|
177
|
-
assert(entry, 'migrateManagedHooks must process the migrated project');
|
|
178
|
-
assert.strictEqual(entry.status, 'verified');
|
|
179
|
-
assert.strictEqual(entry.reason, 'v1-migrated');
|
|
180
|
-
const status = readHookStatus({ repoPath, projectId });
|
|
181
|
-
assert.strictEqual(status.kbManaged, true);
|
|
182
|
-
assert.strictEqual(status.managedVersion, 2);
|
|
183
|
-
const finalState = projectStore.readState(projectId);
|
|
184
|
-
assert.strictEqual(finalState.hook.migrationVersion, 2);
|
|
185
|
-
await logger.close();
|
|
186
|
-
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Case 5: disabled=false preservation
|
|
190
|
-
{
|
|
191
|
-
const { dataDir } = fixture({ enabled: false });
|
|
192
|
-
const layout = new StorageLayout({ dataDir });
|
|
193
|
-
const migration = new MigrationService({ layout, legacyDataDir: dataDir });
|
|
194
|
-
const result = await migration.migrateIfNeeded({ migrationRunId: 'fc-disabled' });
|
|
195
|
-
const projectId = result.projectMap['legacy-team'];
|
|
196
|
-
const config = JSON.parse(fs.readFileSync(layout.getProjectConfigPath(projectId), 'utf8'));
|
|
197
|
-
assert.strictEqual(config.enabled, false, 'enabled=false must be preserved');
|
|
198
|
-
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Case 6: repoPath MUST NOT be replaced with a data-dir fallback when a
|
|
202
|
-
// valid legacy repoPath exists. (RC-style protection: a buggy migration
|
|
203
|
-
// would substitute knowledgeDir/projects/<id> for repoPath.)
|
|
204
|
-
{
|
|
205
|
-
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pk-legacy-no-repo-fallback-'));
|
|
206
|
-
const repoPath = path.join(dataDir, 'real', 'project');
|
|
207
|
-
fs.mkdirSync(repoPath, { recursive: true });
|
|
208
|
-
git(repoPath, ['init', '-q', '-b', 'main']);
|
|
209
|
-
git(repoPath, ['config', 'user.email', 'legacy-fc@example.local']);
|
|
210
|
-
git(repoPath, ['config', 'user.name', 'Legacy FC']);
|
|
211
|
-
fs.writeFileSync(path.join(repoPath, 'README.md'), '# legacy\n');
|
|
212
|
-
git(repoPath, ['add', 'README.md']);
|
|
213
|
-
git(repoPath, ['commit', '-q', '-m', 'initial']);
|
|
214
|
-
const initialSha = git(repoPath, ['rev-parse', 'HEAD']);
|
|
215
|
-
const kbPath = path.join(dataDir, 'knowledge', 'legacy-team');
|
|
216
|
-
fs.mkdirSync(kbPath, { recursive: true });
|
|
217
|
-
fs.writeFileSync(path.join(dataDir, 'projects.json'), JSON.stringify({
|
|
218
|
-
'legacy-team': {
|
|
219
|
-
schema: 'kb-project/v1',
|
|
220
|
-
displayName: 'Legacy Team',
|
|
221
|
-
localPath: repoPath,
|
|
222
|
-
gitPath: repoPath,
|
|
223
|
-
kbPath,
|
|
224
|
-
trackingStartCommit: initialSha,
|
|
225
|
-
lastAnalyzedCommit: initialSha,
|
|
226
|
-
enabled: true,
|
|
227
|
-
aiProfileId: 'primary',
|
|
228
|
-
knowledgeLanguage: 'zh-CN',
|
|
229
|
-
},
|
|
230
|
-
}, null, 2));
|
|
231
|
-
fs.writeFileSync(path.join(dataDir, 'knowledge-store.json'), JSON.stringify({
|
|
232
|
-
schema: 'knowledge-store/v1',
|
|
233
|
-
rootPath: path.join(dataDir, 'knowledge'),
|
|
234
|
-
configured: true,
|
|
235
|
-
}));
|
|
236
|
-
fs.writeFileSync(path.join(dataDir, 'ai-profiles.json'), JSON.stringify({
|
|
237
|
-
schema: 'ai-profiles/v1',
|
|
238
|
-
profiles: [{ id: 'primary', name: 'Primary', enabled: true, vendor: 'anthropic', model: 'claude-sonnet' }],
|
|
239
|
-
defaultProfileId: 'primary',
|
|
240
|
-
}));
|
|
241
|
-
const layout = new StorageLayout({ dataDir });
|
|
242
|
-
const migration = new MigrationService({ layout, legacyDataDir: dataDir });
|
|
243
|
-
const result = await migration.migrateIfNeeded({ migrationRunId: 'fc-no-fallback' });
|
|
244
|
-
assert(result.projectMap && result.projectMap['legacy-team'], 'migration must produce projectMap');
|
|
245
|
-
const projectId = result.projectMap['legacy-team'];
|
|
246
|
-
const config = JSON.parse(fs.readFileSync(layout.getProjectConfigPath(projectId), 'utf8'));
|
|
247
|
-
assert.strictEqual(config.repoPath, path.resolve(repoPath), 'repoPath must NOT be silently rewritten to a data-dir fallback');
|
|
248
|
-
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
console.log('legacy-forward-compat-test PASS');
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
run().catch(error => {
|
|
255
|
-
console.error(error.stack || error.message);
|
|
256
|
-
process.exit(1);
|
|
257
|
-
});
|