@sanqianx/project-knowledge 4.7.0 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/plugins/marketplace.json +17 -17
- package/.claude-plugin/marketplace.json +1 -1
- package/CHANGELOG.md +817 -799
- package/README.md +48 -36
- package/_modules/knowledge-engine/README.md +88 -0
- package/_modules/knowledge-engine/package.json +38 -0
- package/_modules/knowledge-engine/src/analysis-contract.js +25 -0
- package/_modules/knowledge-engine/src/analysis-worker.js +54 -0
- package/_modules/knowledge-engine/src/bin.js +52 -0
- package/_modules/knowledge-engine/src/bridge-client.js +24 -0
- package/_modules/knowledge-engine/src/coordinator.js +255 -0
- package/_modules/knowledge-engine/src/headless-analyzer.js +75 -0
- package/_modules/knowledge-engine/src/memories.js +85 -0
- package/_modules/knowledge-engine/src/migration.js +88 -0
- package/_modules/knowledge-engine/src/retrieval.js +116 -0
- package/_modules/knowledge-engine/src/runtime/README.md +7 -0
- package/_modules/knowledge-engine/src/runtime/atomic-file.js +204 -0
- package/_modules/knowledge-engine/src/runtime/automation-config.js +140 -0
- package/_modules/knowledge-engine/src/runtime/commit-conversation-binder.js +249 -0
- package/_modules/knowledge-engine/src/runtime/commit-processing-ledger.js +26 -0
- package/_modules/knowledge-engine/src/runtime/commit-prompt.js +202 -0
- package/_modules/knowledge-engine/src/runtime/commit-reconciler.js +432 -0
- package/_modules/knowledge-engine/src/runtime/content-hash.js +4 -0
- package/_modules/knowledge-engine/src/runtime/contracts.js +279 -0
- package/_modules/knowledge-engine/src/runtime/conversation-exclusions.js +204 -0
- package/_modules/knowledge-engine/src/runtime/conversation-store.js +387 -0
- package/_modules/knowledge-engine/src/runtime/evidence-bundle.js +228 -0
- package/_modules/knowledge-engine/src/runtime/git-runner.js +62 -0
- package/_modules/knowledge-engine/src/runtime/knowledge-promotion.js +444 -0
- package/_modules/knowledge-engine/src/runtime/knowledge-retrieval-service.js +402 -0
- package/_modules/knowledge-engine/src/runtime/layout.js +44 -0
- package/_modules/knowledge-engine/src/runtime/markdown-knowledge-indexer.js +230 -0
- package/_modules/knowledge-engine/src/runtime/scanner.js +298 -0
- package/_modules/knowledge-engine/src/server.js +155 -0
- package/_modules/knowledge-engine/src/store.js +239 -0
- package/_modules/knowledge-engine/ui/app.css +172 -0
- package/_modules/knowledge-engine/ui/app.js +297 -0
- package/_modules/knowledge-engine/ui/index.html +95 -0
- package/_site/README.md +30 -30
- package/_site/_test/ai-profile-resolver-test.js +137 -137
- package/_site/_test/automation-queue-test.js +14 -14
- package/_site/_test/automation-ui-test.js +40 -40
- package/_site/_test/background-task-registry-test.js +43 -43
- package/_site/_test/baseline-schema-test.js +91 -91
- package/_site/_test/chat-claudecodeui-match-test.js +92 -92
- package/_site/_test/claude-executable-discovery-test.js +62 -62
- package/_site/_test/claude-workbench-test.js +170 -170
- package/_site/_test/codex-conversation-projection-test.js +2 -1
- package/_site/_test/commit-conversation-binding-test.js +95 -95
- package/_site/_test/commit-evidence-test.js +110 -110
- package/_site/_test/conversation-api-test.js +109 -109
- package/_site/_test/conversation-store-test.js +136 -136
- package/_site/_test/data-dir-migration-test.js +57 -57
- package/_site/_test/desktop-browser-compat-test.js +28 -28
- package/_site/_test/desktop-hook-runtime-regression-test.js +204 -204
- package/_site/_test/explicit-commit-processor-test.js +37 -37
- package/_site/_test/fixtures/make-git-repos.js +12 -1
- package/_site/_test/folder-picker-output-test.js +19 -19
- package/_site/_test/full-integration-e2e-test.js +165 -165
- package/_site/_test/git-validation-test.js +2 -2
- package/_site/_test/hook-runtime-endpoint-test.js +88 -88
- package/_site/_test/hook-status-repair-api-test.js +152 -152
- package/_site/_test/hook-trigger-test.js +85 -85
- package/_site/_test/import-preflight-api-test.js +234 -233
- package/_site/_test/index-writer-concurrency-test.js +135 -135
- package/_site/_test/integration-adapters-test.js +171 -171
- package/_site/_test/integration-surface-coverage-test.js +164 -164
- package/_site/_test/knowledge-language-control-test.js +170 -170
- package/_site/_test/knowledge-promotion-recovery-test.js +260 -260
- package/_site/_test/knowledge-query-test.js +54 -54
- package/_site/_test/knowledge-retrieval-service-test.js +77 -77
- package/_site/_test/knowledge-storage-startup-test.js +52 -52
- package/_site/_test/knowledge-store-logs-supervision-test.js +89 -89
- package/_site/_test/legacy-forward-compat-test.js +257 -257
- package/_site/_test/legacy-project-upgrade-e2e-test.js +363 -363
- package/_site/_test/logging-api-test.js +89 -89
- package/_site/_test/logging-sse-no-gap-test.js +88 -88
- package/_site/_test/logging-ui-test.js +106 -104
- package/_site/_test/markdown-delta-overlay-test.js +85 -85
- package/_site/_test/markdown-maintenance-api-test.js +75 -75
- package/_site/_test/mcp-server-test.js +149 -149
- package/_site/_test/module-artifact-integrity-test.js +27 -0
- package/_site/_test/module-boundary-test.js +30 -0
- package/_site/_test/module-bridge-eventbridge-test.js +7 -0
- package/_site/_test/module-model-configuration-test.js +43 -0
- package/_site/_test/module-process-lifecycle-test.js +24 -0
- package/_site/_test/module-stream-proxy-test.js +34 -0
- package/_site/_test/non-release-ci-test.js +34 -34
- package/_site/_test/offline-boundary-isolation-test.js +32 -32
- package/_site/_test/p0-data-migration-characterization-test.js +37 -37
- package/_site/_test/p0-e2e-gate-test.js +347 -347
- package/_site/_test/packaged-ui-smoke-test.js +11 -9
- package/_site/_test/path-consistency-test.js +151 -151
- package/_site/_test/pending-sweep-test.js +9 -9
- package/_site/_test/post-commit-automation-test.js +87 -87
- package/_site/_test/product-diagnostics-ui-test.js +75 -0
- package/_site/_test/product-import-ui-test.js +92 -0
- package/_site/_test/project-delete-recovery-test.js +64 -64
- package/_site/_test/project-goal-editor-test.js +144 -144
- package/_site/_test/project-layout-v2-migration-test.js +127 -127
- package/_site/_test/project-lifecycle-transaction-test.js +106 -106
- package/_site/_test/prompt-settings-test.js +115 -115
- package/_site/_test/protected-architecture-gate-test.js +122 -122
- package/_site/_test/refactor-characterization-test.js +36 -36
- package/_site/_test/release-version-sync-test.js +1 -1
- package/_site/_test/requirement-recorder-test.js +173 -173
- package/_site/_test/run-all-tests.js +159 -156
- package/_site/_test/server-runtime-migration-safety-test.js +27 -27
- package/_site/_test/shared-contracts-test.js +58 -58
- package/_site/_test/startup-analysis-disabled-test.js +10 -10
- package/_site/_test/storage-foundation-test.js +78 -78
- package/_site/_test/structured-logger-test.js +101 -101
- package/_site/_test/tracking-start-test.js +108 -108
- package/_site/_test/v4122-upgrade-data-contract-test.js +56 -56
- package/_site/_test/workbench-permission-test.js +117 -117
- package/_site/_test/workspace-ui-contract-test.js +28 -43
- package/_site/lib/ai-profile-resolver.js +78 -78
- package/_site/lib/ai-workspace.js +101 -101
- package/_site/lib/automation-config.js +140 -140
- package/_site/lib/bridge-adapter.js +1 -0
- package/_site/lib/bridge-consumer-service.js +402 -402
- package/_site/lib/claude-cli-runner.js +1510 -1510
- package/_site/lib/commit-conversation-binder.js +234 -234
- package/_site/lib/commit-processing-ledger.js +27 -27
- package/_site/lib/commit-prompt.js +202 -202
- package/_site/lib/commit-reconciler.js +406 -406
- package/_site/lib/contracts.js +284 -279
- package/_site/lib/conversation-query-service.js +144 -144
- package/_site/lib/conversation-store.js +385 -385
- package/_site/lib/data-dir.js +74 -74
- package/_site/lib/data-state-classifier.js +40 -40
- package/_site/lib/engine-cutover.js +97 -0
- package/_site/lib/evidence-bundle.js +228 -228
- package/_site/lib/folder-picker-output.js +21 -21
- package/_site/lib/git-runner.js +62 -62
- package/_site/lib/github-team-store.js +1172 -1172
- package/_site/lib/hook-manager.js +203 -202
- package/_site/lib/index-service.js +173 -173
- package/_site/lib/integration-installer.js +907 -907
- package/_site/lib/kb-framework.js +176 -176
- package/_site/lib/kb-validator.js +124 -124
- package/_site/lib/knowledge-promotion.js +435 -435
- package/_site/lib/knowledge-retrieval-service.js +401 -401
- package/_site/lib/knowledge-tool-runtime.js +359 -359
- package/_site/lib/legacy-data-manifest.js +26 -26
- package/_site/lib/llm-client.js +161 -161
- package/_site/lib/markdown-knowledge-indexer.js +230 -230
- package/_site/lib/migration-service.js +442 -442
- package/_site/lib/module-bridge.js +198 -33
- package/_site/lib/post-commit-automation.js +87 -87
- package/_site/lib/project-lifecycle-service.js +519 -497
- package/_site/lib/project-store.js +268 -262
- package/_site/lib/requirement-recorder.js +278 -278
- package/_site/lib/runtime-endpoint.js +155 -155
- package/_site/lib/scanner.js +298 -298
- package/_site/lib/server-app.js +1590 -1520
- package/_site/lib/settings-store.js +133 -113
- package/_site/lib/storage-layout.js +162 -162
- package/_site/lib/structured-logger.js +574 -574
- package/_site/scripts/folder-picker.ps1 +156 -156
- package/_site/scripts/hook-trigger.js +157 -156
- package/_site/scripts/install-module-candidates.js +69 -0
- package/_site/scripts/pack-module-candidates.js +72 -0
- package/_site/scripts/sync-release-version.js +2 -2
- package/_site/scripts/vendor-modules.js +47 -0
- package/_site/scripts/verify-module-runtime.js +45 -0
- package/_site/scripts/verify-product-runtime.js +34 -0
- package/bin/project-knowledge-mcp.js +194 -194
- package/docs/README.zh-CN.md +34 -25
- package/docs/project-registry-schema.md +22 -22
- package/docs/testing-strategy.md +33 -33
- package/module-runtime-manifest.json +220 -0
- package/package.json +13 -9
- package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.mcp.json +1 -1
- package/plugins/project-knowledge/opencode/project-knowledge.md +3 -3
- package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -28
- package/templates/project-readme.md +34 -34
- package/ui/favicon.svg +38 -38
- package/ui/index.html +51 -149
- package/ui/product.css +9 -0
- package/ui/product.js +386 -0
- package/vendor-manifest.json +46 -0
- package/_site/_test/import-ui-flow-test.js +0 -171
- package/_site/_test/project-control-panel-task14-test.js +0 -63
- package/_site/_test/task15-20-ui-flow-test.js +0 -148
- package/_site/_test/ui-i18n-toggle-test.js +0 -114
- package/_site/_test/ui-smoke-test.js +0 -73
- package/ui/app.css +0 -58
- package/ui/app.js +0 -607
- package/ui/i18n.js +0 -146
|
@@ -1,442 +1,442 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const crypto = require('crypto');
|
|
4
|
-
const { SCHEMAS, SCHEMA_VERSIONS, DomainError } = require('./contracts');
|
|
5
|
-
const AtomicFile = require('./atomic-file');
|
|
6
|
-
const { StorageLayout } = require('./storage-layout');
|
|
7
|
-
const { defaultSettings, validateSettings } = require('./settings-store');
|
|
8
|
-
const { emptyRegistry, validateRegistry } = require('./project-registry-store');
|
|
9
|
-
const { defaultProjectConfig, defaultProjectState, validateProjectConfig, validateProjectState } = require('./project-store');
|
|
10
|
-
const { LEGACY_ASSETS, getLegacyAsset, assetPath } = require('./legacy-data-manifest');
|
|
11
|
-
const { STATES, classifyDataState } = require('./data-state-classifier');
|
|
12
|
-
|
|
13
|
-
function hashBuffer(buffer) { return crypto.createHash('sha256').update(buffer).digest('hex'); }
|
|
14
|
-
function hashFile(filePath) { return hashBuffer(fs.readFileSync(filePath)); }
|
|
15
|
-
|
|
16
|
-
function copyRecursive(source, target) {
|
|
17
|
-
const stat = fs.statSync(source);
|
|
18
|
-
if (stat.isDirectory()) {
|
|
19
|
-
fs.mkdirSync(target, { recursive: true });
|
|
20
|
-
for (const entry of fs.readdirSync(source)) copyRecursive(path.join(source, entry), path.join(target, entry));
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
24
|
-
fs.copyFileSync(source, target, fs.constants.COPYFILE_EXCL);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function removeOwnedPath(target) {
|
|
28
|
-
if (!fs.existsSync(target)) return;
|
|
29
|
-
fs.rmSync(target, { recursive: true, force: true });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function pruneEmptyParents(target, boundary) {
|
|
33
|
-
const root = path.resolve(boundary);
|
|
34
|
-
let current = path.dirname(path.resolve(target));
|
|
35
|
-
while (current !== root && current.startsWith(`${root}${path.sep}`)) {
|
|
36
|
-
if (!fs.existsSync(current) || fs.readdirSync(current).length > 0) break;
|
|
37
|
-
fs.rmdirSync(current);
|
|
38
|
-
current = path.dirname(current);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function deterministicProjectId(slug, config) {
|
|
43
|
-
const identity = String(config && (config.projectId || config.gitPath || config.localPath || config.repoPath) || slug);
|
|
44
|
-
const existing = String(config && config.projectId || '');
|
|
45
|
-
if (/^[A-Za-z0-9][A-Za-z0-9._-]{2,127}$/.test(existing)) return existing;
|
|
46
|
-
return `project-${crypto.createHash('sha256').update(`${slug}\0${identity}`).digest('hex').slice(0, 20)}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function safeReadJson(filePath, fallback = null) {
|
|
50
|
-
if (!fs.existsSync(filePath)) return fallback;
|
|
51
|
-
try {
|
|
52
|
-
// Legacy files written via PowerShell/tooling often carry a UTF-8 BOM;
|
|
53
|
-
// JSON.parse rejects it, and a BOM must never fail the whole migration.
|
|
54
|
-
const raw = fs.readFileSync(filePath, 'utf8');
|
|
55
|
-
return JSON.parse(raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw);
|
|
56
|
-
}
|
|
57
|
-
catch (error) { throw new DomainError('DATA_CORRUPT', 'Legacy migration source JSON is corrupt.', { status: 500, cause: error, details: { category: path.basename(filePath) } }); }
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function sourceSnapshot(paths) {
|
|
61
|
-
const entries = [];
|
|
62
|
-
for (const source of paths) {
|
|
63
|
-
if (!fs.existsSync(source)) continue;
|
|
64
|
-
const stat = fs.statSync(source);
|
|
65
|
-
entries.push({
|
|
66
|
-
path: source,
|
|
67
|
-
kind: stat.isDirectory() ? 'directory' : 'file',
|
|
68
|
-
size: stat.size,
|
|
69
|
-
mtimeMs: stat.mtimeMs,
|
|
70
|
-
hash: stat.isFile() ? hashFile(source) : '',
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
return entries;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function verifySourceSnapshot(entries) {
|
|
77
|
-
for (const entry of entries) {
|
|
78
|
-
if (!fs.existsSync(entry.path)) return false;
|
|
79
|
-
const stat = fs.statSync(entry.path);
|
|
80
|
-
if (entry.kind === 'file' && (stat.size !== entry.size || hashFile(entry.path) !== entry.hash)) return false;
|
|
81
|
-
}
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function directoryDigest(root) {
|
|
86
|
-
const hash = crypto.createHash('sha256');
|
|
87
|
-
const walk = current => {
|
|
88
|
-
for (const entry of fs.readdirSync(current, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
89
|
-
const item = path.join(current, entry.name);
|
|
90
|
-
hash.update(`${entry.isDirectory() ? 'd' : 'f'}:${path.relative(root, item).replace(/\\/g, '/')}\0`);
|
|
91
|
-
if (entry.isDirectory()) walk(item); else if (entry.isFile()) hash.update(fs.readFileSync(item));
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
walk(root);
|
|
95
|
-
return hash.digest('hex');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
class MigrationService {
|
|
99
|
-
constructor(options = {}) {
|
|
100
|
-
this.layout = options.layout || new StorageLayout(options);
|
|
101
|
-
this.atomic = options.atomic || AtomicFile;
|
|
102
|
-
this.legacyDataDir = path.resolve(options.legacyDataDir || this.layout.getDataDir());
|
|
103
|
-
this.indexValidator = options.indexValidator || (async indexPath => ({ ok: fs.existsSync(indexPath) }));
|
|
104
|
-
this.logger = options.logger || { info() {}, warn() {}, error() {} };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
completion() {
|
|
108
|
-
const completionPath = this.layout.getMigrationCompletionPath();
|
|
109
|
-
if (!fs.existsSync(completionPath)) return null;
|
|
110
|
-
const marker = this.atomic.readJsonStrict(completionPath, { category: 'layout-migration-completion' });
|
|
111
|
-
return marker && ['layout-migration-completion/v1', 'layout-migration-completion/v2'].includes(marker.schema) && marker.migrationId === SCHEMA_VERSIONS.layoutMigration
|
|
112
|
-
? marker
|
|
113
|
-
: null;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
completionMarker({ runId = '', registry, settings, modelsMigrated = false } = {}) {
|
|
117
|
-
const profiles = settings && settings.ai && Array.isArray(settings.ai.profiles) ? settings.ai.profiles : [];
|
|
118
|
-
return {
|
|
119
|
-
schema: 'layout-migration-completion/v2',
|
|
120
|
-
migrationId: SCHEMA_VERSIONS.layoutMigration,
|
|
121
|
-
runId,
|
|
122
|
-
sourceRoot: this.legacyDataDir,
|
|
123
|
-
sourceManifestHash: hashBuffer(Buffer.from(JSON.stringify(LEGACY_ASSETS.map(asset => [asset.source, asset.target, asset.kind])))),
|
|
124
|
-
projectCount: registry.projectOrder.length,
|
|
125
|
-
aiProfileCount: profiles.length,
|
|
126
|
-
knowledgeRootConfigured: Boolean(settings.knowledge && settings.knowledge.rootPath),
|
|
127
|
-
embeddingConfigured: Boolean(settings.embedding && Object.keys(settings.embedding).length),
|
|
128
|
-
modelsMigrated,
|
|
129
|
-
completedAt: new Date().toISOString(),
|
|
130
|
-
verified: true,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
validateCompletedState(marker) {
|
|
135
|
-
try {
|
|
136
|
-
const registry = this.atomic.readJsonStrict(this.layout.getProjectRegistryPath(), { category: 'project-registry' });
|
|
137
|
-
validateRegistry(registry);
|
|
138
|
-
const settings = this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' });
|
|
139
|
-
validateSettings(settings);
|
|
140
|
-
for (const projectId of registry.projectOrder) {
|
|
141
|
-
validateProjectConfig(this.atomic.readJsonStrict(this.layout.getProjectConfigPath(projectId), { category: 'project-config' }), projectId);
|
|
142
|
-
validateProjectState(this.atomic.readJsonStrict(this.layout.getProjectStatePath(projectId), { category: 'project-state' }));
|
|
143
|
-
}
|
|
144
|
-
// marker.projectCount is a migration-time snapshot only. Imports and
|
|
145
|
-
// deletions legitimately change the live registry count afterwards;
|
|
146
|
-
// comparing them here rejected every restart after the first legal
|
|
147
|
-
// project removal (completion-project-count-mismatch false positive).
|
|
148
|
-
return { ok: true, registry, settings };
|
|
149
|
-
} catch (error) { return { ok: false, reason: 'completion-state-invalid', error }; }
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
discover() {
|
|
153
|
-
const paths = LEGACY_ASSETS.map(asset => assetPath(this.legacyDataDir, asset)).filter(file => fs.existsSync(file));
|
|
154
|
-
const knowledgeStore = safeReadJson(path.join(this.legacyDataDir, 'knowledge-store.json'), {});
|
|
155
|
-
const indexCandidates = [
|
|
156
|
-
path.join(this.legacyDataDir, 'knowledge.lancedb'),
|
|
157
|
-
knowledgeStore && knowledgeStore.rootPath ? path.join(path.resolve(knowledgeStore.rootPath), '.project-knowledge', 'knowledge.lancedb') : '',
|
|
158
|
-
].filter(Boolean);
|
|
159
|
-
const indexPath = indexCandidates.find(candidate => fs.existsSync(candidate)) || '';
|
|
160
|
-
if (indexPath) paths.push(indexPath);
|
|
161
|
-
return {
|
|
162
|
-
legacyProjectsPath: path.join(this.legacyDataDir, 'projects.json'),
|
|
163
|
-
knowledgeStore,
|
|
164
|
-
indexPath,
|
|
165
|
-
modelsPath: assetPath(this.legacyDataDir, getLegacyAsset('models')),
|
|
166
|
-
sources: sourceSnapshot(paths),
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
mergeLegacySettings(discovery) {
|
|
171
|
-
const settings = defaultSettings();
|
|
172
|
-
const read = name => safeReadJson(path.join(this.legacyDataDir, name), null);
|
|
173
|
-
const knowledge = read('knowledge-store.json');
|
|
174
|
-
const ai = read('ai-profiles.json');
|
|
175
|
-
const embedding = read('embedding-config.json');
|
|
176
|
-
const logging = read('logging.json');
|
|
177
|
-
const prompts = read('claude-prompts.json');
|
|
178
|
-
const github = read('github-team.json');
|
|
179
|
-
const providers = read('team-git-providers.json');
|
|
180
|
-
if (knowledge && typeof knowledge.rootPath === 'string') settings.knowledge.rootPath = knowledge.rootPath;
|
|
181
|
-
if (ai && typeof ai === 'object') settings.ai = ai;
|
|
182
|
-
if (embedding && typeof embedding === 'object') settings.embedding = embedding;
|
|
183
|
-
if (logging && typeof logging === 'object') {
|
|
184
|
-
if (Array.isArray(logging.levels)) settings.logging.levels = logging.levels;
|
|
185
|
-
settings.legacyExtensions.logging = { ...logging };
|
|
186
|
-
}
|
|
187
|
-
if (prompts && typeof prompts === 'object') settings.promptOverrides = prompts;
|
|
188
|
-
if (github || providers) settings.integrations = { ...(settings.integrations || {}), ...(github ? { githubTeam: github } : {}), ...(providers ? { teamGitProviders: providers } : {}) };
|
|
189
|
-
settings.updatedAt = new Date().toISOString();
|
|
190
|
-
return validateSettings(settings);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
buildProjectRecords(legacyProjects, settings) {
|
|
194
|
-
const registry = emptyRegistry();
|
|
195
|
-
const records = [];
|
|
196
|
-
for (const [slug, rawConfig] of Object.entries(legacyProjects || {})) {
|
|
197
|
-
const legacy = rawConfig && rawConfig.config && typeof rawConfig.config === 'object' ? rawConfig.config : (rawConfig || {});
|
|
198
|
-
const projectId = deterministicProjectId(slug, legacy);
|
|
199
|
-
const knowledgePath = legacy.knowledgePath || legacy.kbPath || (settings.knowledge.rootPath ? path.join(path.resolve(settings.knowledge.rootPath), slug) : path.join(this.legacyDataDir, 'projects', slug));
|
|
200
|
-
const config = defaultProjectConfig(projectId, {
|
|
201
|
-
displayName: legacy.displayName || slug,
|
|
202
|
-
storageName: legacy.storageName || `${slug}-${projectId.slice(-6)}`,
|
|
203
|
-
repoPath: legacy.repoPath || legacy.gitPath || legacy.localPath || this.legacyDataDir,
|
|
204
|
-
knowledgePath,
|
|
205
|
-
enabled: legacy.enabled !== false,
|
|
206
|
-
createdAt: legacy.createdAt || new Date(0).toISOString(),
|
|
207
|
-
teamBinding: legacy.teamBinding || (legacy.teamKbId ? { teamKbId: legacy.teamKbId } : null),
|
|
208
|
-
aiProfileId: legacy.aiProfileId || null,
|
|
209
|
-
knowledgeLanguage: legacy.knowledgeLanguage || 'zh-CN',
|
|
210
|
-
relatedProjectIds: legacy.relatedProjectIds || [],
|
|
211
|
-
repoIdentity: legacy.repoIdentity || null,
|
|
212
|
-
legacyExtensions: { slug, sourceSchema: legacy.schema || '' },
|
|
213
|
-
});
|
|
214
|
-
const hasTracking = Boolean(legacy.trackingStartCommit || legacy.lastAnalyzedCommit);
|
|
215
|
-
const state = defaultProjectState({
|
|
216
|
-
trackingStartCommit: legacy.trackingStartCommit || null,
|
|
217
|
-
lastAnalyzedCommit: legacy.lastAnalyzedCommit || null,
|
|
218
|
-
trackingMode: hasTracking ? 'normal' : (legacy.repoStatus === 'empty-repo' ? 'empty-repo' : 'normal'),
|
|
219
|
-
hook: { managedVersion: 0, migrationVersion: 0, lastVerifiedAt: '' },
|
|
220
|
-
});
|
|
221
|
-
validateProjectConfig(config, projectId);
|
|
222
|
-
validateProjectState(state);
|
|
223
|
-
registry.projectOrder.push(projectId);
|
|
224
|
-
registry.projects[projectId] = { createdAt: config.createdAt, displayNameSnapshot: config.displayName };
|
|
225
|
-
records.push({ slug, projectId, config, state });
|
|
226
|
-
}
|
|
227
|
-
registry.updatedAt = new Date().toISOString();
|
|
228
|
-
validateRegistry(registry);
|
|
229
|
-
return { registry, records };
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
injectFault(options, stage) {
|
|
233
|
-
if (options && options.faultAt === stage) throw new Error(`injected migration fault: ${stage}`);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
async migrateIfNeeded(options = {}) {
|
|
237
|
-
const existingCompletion = this.completion();
|
|
238
|
-
if (existingCompletion) {
|
|
239
|
-
const verified = this.validateCompletedState(existingCompletion);
|
|
240
|
-
if (!verified.ok) return { ok: false, migrated: false, completed: false, requiresManualRecovery: true, reason: verified.reason, error: verified.error };
|
|
241
|
-
if (existingCompletion.schema === 'layout-migration-completion/v2') return { ok: true, migrated: false, completed: true, marker: existingCompletion };
|
|
242
|
-
const marker = this.completionMarker({ runId: existingCompletion.runId || 'v1-marker-upgrade', registry: verified.registry, settings: verified.settings, modelsMigrated: fs.existsSync(this.layout.getCachePath('models')) });
|
|
243
|
-
this.atomic.writeJsonAtomic(this.layout.getMigrationCompletionPath(), marker);
|
|
244
|
-
return { ok: true, migrated: false, completed: true, marker };
|
|
245
|
-
}
|
|
246
|
-
const state = classifyDataState(this.legacyDataDir);
|
|
247
|
-
if (state.state === STATES.FRESH) return { ok: true, migrated: false, completed: false, reason: 'fresh-install' };
|
|
248
|
-
if (state.state === STATES.CORRUPT || state.state === STATES.MIGRATION_INCOMPLETE) {
|
|
249
|
-
return { ok: false, migrated: false, completed: false, requiresManualRecovery: true, reason: state.state.toLowerCase() };
|
|
250
|
-
}
|
|
251
|
-
const discovery = this.discover();
|
|
252
|
-
this.injectFault(options, 'discovery');
|
|
253
|
-
// A user may have configured only AI/embedding/knowledge settings before
|
|
254
|
-
// ever importing a project. That is still legacy user data and must be
|
|
255
|
-
// migrated into an explicitly empty v2 registry, never treated as Fresh.
|
|
256
|
-
const legacyProjects = safeReadJson(discovery.legacyProjectsPath, {});
|
|
257
|
-
if (legacyProjects && legacyProjects.schema === SCHEMAS.projectRegistry && legacyProjects.schemaVersion === 2 && fs.existsSync(this.layout.getSettingsPath())) {
|
|
258
|
-
validateRegistry(legacyProjects);
|
|
259
|
-
const settings = this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' });
|
|
260
|
-
validateSettings(settings);
|
|
261
|
-
const marker = this.completionMarker({ runId: 'validated-existing-v2', registry: legacyProjects, settings, modelsMigrated: fs.existsSync(this.layout.getCachePath('models')) });
|
|
262
|
-
this.atomic.writeJsonAtomic(this.layout.getMigrationCompletionPath(), marker);
|
|
263
|
-
return { ok: true, migrated: false, completed: true, marker };
|
|
264
|
-
}
|
|
265
|
-
if (state.state !== STATES.LEGACY) return { ok: false, migrated: false, completed: false, requiresManualRecovery: true, reason: 'legacy-state-not-migratable' };
|
|
266
|
-
|
|
267
|
-
const migrationId = options.migrationRunId || `layout-v2-${Date.now()}-${crypto.randomBytes(4).toString('hex')}`;
|
|
268
|
-
const recoveryDir = this.layout.getRecoveryPath(migrationId);
|
|
269
|
-
const backupDir = path.join(recoveryDir, 'backup');
|
|
270
|
-
const stagingDir = path.join(recoveryDir, 'staging');
|
|
271
|
-
fs.mkdirSync(backupDir, { recursive: true });
|
|
272
|
-
fs.mkdirSync(stagingDir, { recursive: true });
|
|
273
|
-
const journalPath = path.join(recoveryDir, 'journal.json');
|
|
274
|
-
const journal = {
|
|
275
|
-
schema: SCHEMAS.migrationJournal,
|
|
276
|
-
migrationId: SCHEMA_VERSIONS.layoutMigration,
|
|
277
|
-
runId: migrationId,
|
|
278
|
-
phase: 'discovered',
|
|
279
|
-
startedAt: new Date().toISOString(),
|
|
280
|
-
sources: discovery.sources,
|
|
281
|
-
activated: [],
|
|
282
|
-
};
|
|
283
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
284
|
-
const legacyRegistryBackup = path.join(backupDir, 'projects.json');
|
|
285
|
-
try {
|
|
286
|
-
for (const entry of discovery.sources) {
|
|
287
|
-
const relative = path.relative(this.legacyDataDir, entry.path);
|
|
288
|
-
const safeRelative = relative.startsWith('..') || path.isAbsolute(relative)
|
|
289
|
-
? path.join('external', hashBuffer(Buffer.from(entry.path)).slice(0, 16), path.basename(entry.path))
|
|
290
|
-
: relative;
|
|
291
|
-
copyRecursive(entry.path, path.join(backupDir, safeRelative));
|
|
292
|
-
}
|
|
293
|
-
journal.phase = 'backed-up';
|
|
294
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
295
|
-
this.injectFault(options, 'backup');
|
|
296
|
-
|
|
297
|
-
const settings = this.mergeLegacySettings(discovery);
|
|
298
|
-
const projectData = this.buildProjectRecords(legacyProjects, settings);
|
|
299
|
-
const staged = [];
|
|
300
|
-
const stageJson = (relative, value) => {
|
|
301
|
-
const stagedPath = path.join(stagingDir, relative);
|
|
302
|
-
this.atomic.writeJsonAtomic(stagedPath, value);
|
|
303
|
-
staged.push({ relative, kind: 'file', hash: hashFile(stagedPath) });
|
|
304
|
-
};
|
|
305
|
-
stageJson('settings.json', settings);
|
|
306
|
-
for (const record of projectData.records) {
|
|
307
|
-
stageJson(path.join('projects', record.projectId, 'config.json'), record.config);
|
|
308
|
-
stageJson(path.join('projects', record.projectId, 'state.json'), record.state);
|
|
309
|
-
}
|
|
310
|
-
stageJson('projects.json', projectData.registry);
|
|
311
|
-
let modelsMigrated = false;
|
|
312
|
-
if (fs.existsSync(discovery.modelsPath)) {
|
|
313
|
-
const modelTarget = this.layout.getCachePath('models');
|
|
314
|
-
if (fs.existsSync(modelTarget)) {
|
|
315
|
-
if (directoryDigest(discovery.modelsPath) !== directoryDigest(modelTarget)) {
|
|
316
|
-
throw new DomainError('MIGRATION_TARGET_CONFLICT', 'Legacy model cache conflicts with the current model cache.', { status: 409, details: { category: 'cache/models' } });
|
|
317
|
-
}
|
|
318
|
-
} else {
|
|
319
|
-
const relative = path.join('cache', 'models');
|
|
320
|
-
copyRecursive(discovery.modelsPath, path.join(stagingDir, relative));
|
|
321
|
-
staged.push({ relative, kind: 'directory', hash: directoryDigest(path.join(stagingDir, relative)) });
|
|
322
|
-
modelsMigrated = true;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
if (discovery.indexPath) {
|
|
326
|
-
const relative = path.join('index', 'knowledge.lancedb');
|
|
327
|
-
copyRecursive(discovery.indexPath, path.join(stagingDir, relative));
|
|
328
|
-
staged.push({ relative, kind: 'directory', hash: '' });
|
|
329
|
-
}
|
|
330
|
-
journal.staged = staged;
|
|
331
|
-
journal.projectMap = Object.fromEntries(projectData.records.map(record => [record.slug, record.projectId]));
|
|
332
|
-
journal.phase = 'staged';
|
|
333
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
334
|
-
this.injectFault(options, 'staging');
|
|
335
|
-
|
|
336
|
-
if (!verifySourceSnapshot(discovery.sources)) throw new DomainError('MIGRATION_SOURCE_CHANGED', 'Migration source changed while staging.', { status: 409 });
|
|
337
|
-
validateSettings(JSON.parse(fs.readFileSync(path.join(stagingDir, 'settings.json'), 'utf8')));
|
|
338
|
-
validateRegistry(JSON.parse(fs.readFileSync(path.join(stagingDir, 'projects.json'), 'utf8')));
|
|
339
|
-
for (const record of projectData.records) {
|
|
340
|
-
validateProjectConfig(JSON.parse(fs.readFileSync(path.join(stagingDir, 'projects', record.projectId, 'config.json'), 'utf8')), record.projectId);
|
|
341
|
-
validateProjectState(JSON.parse(fs.readFileSync(path.join(stagingDir, 'projects', record.projectId, 'state.json'), 'utf8')));
|
|
342
|
-
}
|
|
343
|
-
if (discovery.indexPath) {
|
|
344
|
-
const indexResult = await this.indexValidator(path.join(stagingDir, 'index', 'knowledge.lancedb'));
|
|
345
|
-
if (!indexResult || indexResult.ok !== true) throw new DomainError('MIGRATION_FAILED', 'Staged index validation failed.', { status: 500 });
|
|
346
|
-
}
|
|
347
|
-
journal.phase = 'validated';
|
|
348
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
349
|
-
this.injectFault(options, 'validation');
|
|
350
|
-
|
|
351
|
-
const activationOrder = staged.filter(item => item.relative !== 'projects.json');
|
|
352
|
-
const activationBackupDir = path.join(backupDir, 'activation-targets');
|
|
353
|
-
journal.activationTargets = activationOrder.map(item => {
|
|
354
|
-
const target = path.join(this.layout.getDataDir(), item.relative);
|
|
355
|
-
const existed = fs.existsSync(target);
|
|
356
|
-
if (item.kind === 'directory' && existed) {
|
|
357
|
-
throw new DomainError('MIGRATION_TARGET_CONFLICT', 'Migration target already exists.', { status: 409, details: { category: item.relative } });
|
|
358
|
-
}
|
|
359
|
-
if (existed) copyRecursive(target, path.join(activationBackupDir, item.relative));
|
|
360
|
-
return { relative: item.relative, kind: item.kind, existed };
|
|
361
|
-
});
|
|
362
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
363
|
-
for (const item of activationOrder) {
|
|
364
|
-
const source = path.join(stagingDir, item.relative);
|
|
365
|
-
const target = path.join(this.layout.getDataDir(), item.relative);
|
|
366
|
-
if (item.kind === 'directory') {
|
|
367
|
-
copyRecursive(source, target);
|
|
368
|
-
} else {
|
|
369
|
-
this.atomic.writeFileAtomic(target, fs.readFileSync(source));
|
|
370
|
-
}
|
|
371
|
-
journal.activated.push(item.relative);
|
|
372
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
373
|
-
}
|
|
374
|
-
this.atomic.writeFileAtomic(this.layout.getProjectRegistryPath(), fs.readFileSync(path.join(stagingDir, 'projects.json')));
|
|
375
|
-
journal.activated.push('projects.json');
|
|
376
|
-
journal.phase = 'activated';
|
|
377
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
378
|
-
this.injectFault(options, 'activation');
|
|
379
|
-
|
|
380
|
-
validateSettings(this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' }));
|
|
381
|
-
const openedRegistry = this.atomic.readJsonStrict(this.layout.getProjectRegistryPath(), { category: 'project-registry' });
|
|
382
|
-
validateRegistry(openedRegistry);
|
|
383
|
-
for (const projectId of openedRegistry.projectOrder) {
|
|
384
|
-
validateProjectConfig(this.atomic.readJsonStrict(this.layout.getProjectConfigPath(projectId), { category: 'project-config' }), projectId);
|
|
385
|
-
validateProjectState(this.atomic.readJsonStrict(this.layout.getProjectStatePath(projectId), { category: 'project-state' }));
|
|
386
|
-
}
|
|
387
|
-
journal.phase = 'open-verified';
|
|
388
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
389
|
-
this.injectFault(options, 'open-verification');
|
|
390
|
-
|
|
391
|
-
const marker = this.completionMarker({ runId: migrationId, registry: openedRegistry, settings: this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' }), modelsMigrated });
|
|
392
|
-
this.atomic.writeJsonAtomic(this.layout.getMigrationCompletionPath(), marker);
|
|
393
|
-
journal.phase = 'completed';
|
|
394
|
-
journal.completedAt = marker.completedAt;
|
|
395
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
396
|
-
return { ok: true, migrated: true, completed: true, marker, recoveryDir, projectMap: journal.projectMap };
|
|
397
|
-
} catch (error) {
|
|
398
|
-
const rollbackErrors = [];
|
|
399
|
-
for (const targetInfo of [...(journal.activationTargets || [])].reverse()) {
|
|
400
|
-
const target = path.join(this.layout.getDataDir(), targetInfo.relative);
|
|
401
|
-
try {
|
|
402
|
-
if (targetInfo.existed) {
|
|
403
|
-
const backup = path.join(backupDir, 'activation-targets', targetInfo.relative);
|
|
404
|
-
if (targetInfo.kind === 'directory') {
|
|
405
|
-
removeOwnedPath(target);
|
|
406
|
-
copyRecursive(backup, target);
|
|
407
|
-
} else {
|
|
408
|
-
this.atomic.writeFileAtomic(target, fs.readFileSync(backup));
|
|
409
|
-
}
|
|
410
|
-
} else {
|
|
411
|
-
removeOwnedPath(target);
|
|
412
|
-
pruneEmptyParents(target, this.layout.getDataDir());
|
|
413
|
-
}
|
|
414
|
-
} catch (rollbackError) {
|
|
415
|
-
rollbackErrors.push({ target: targetInfo.relative, message: String(rollbackError.message || rollbackError) });
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
try {
|
|
419
|
-
if (fs.existsSync(legacyRegistryBackup)) {
|
|
420
|
-
this.atomic.writeFileAtomic(this.layout.getProjectRegistryPath(), fs.readFileSync(legacyRegistryBackup));
|
|
421
|
-
}
|
|
422
|
-
} catch (rollbackError) {
|
|
423
|
-
rollbackErrors.push({ target: 'projects.json', message: String(rollbackError.message || rollbackError) });
|
|
424
|
-
}
|
|
425
|
-
journal.phase = 'failed';
|
|
426
|
-
journal.failedAt = new Date().toISOString();
|
|
427
|
-
journal.error = { name: error.name || 'Error', code: error.code || '', message: String(error.message || error) };
|
|
428
|
-
journal.rollback = { ok: rollbackErrors.length === 0, errors: rollbackErrors };
|
|
429
|
-
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
430
|
-
this.logger.error('migration.failed', { error, recoveryDir, rollbackErrors });
|
|
431
|
-
return { ok: false, migrated: false, completed: false, useLegacy: rollbackErrors.length === 0, recoveryDir, error, rollbackErrors };
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
module.exports = {
|
|
437
|
-
MigrationService,
|
|
438
|
-
deterministicProjectId,
|
|
439
|
-
hashFile,
|
|
440
|
-
sourceSnapshot,
|
|
441
|
-
verifySourceSnapshot,
|
|
442
|
-
};
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const { SCHEMAS, SCHEMA_VERSIONS, DomainError } = require('./contracts');
|
|
5
|
+
const AtomicFile = require('./atomic-file');
|
|
6
|
+
const { StorageLayout } = require('./storage-layout');
|
|
7
|
+
const { defaultSettings, validateSettings } = require('./settings-store');
|
|
8
|
+
const { emptyRegistry, validateRegistry } = require('./project-registry-store');
|
|
9
|
+
const { defaultProjectConfig, defaultProjectState, validateProjectConfig, validateProjectState } = require('./project-store');
|
|
10
|
+
const { LEGACY_ASSETS, getLegacyAsset, assetPath } = require('./legacy-data-manifest');
|
|
11
|
+
const { STATES, classifyDataState } = require('./data-state-classifier');
|
|
12
|
+
|
|
13
|
+
function hashBuffer(buffer) { return crypto.createHash('sha256').update(buffer).digest('hex'); }
|
|
14
|
+
function hashFile(filePath) { return hashBuffer(fs.readFileSync(filePath)); }
|
|
15
|
+
|
|
16
|
+
function copyRecursive(source, target) {
|
|
17
|
+
const stat = fs.statSync(source);
|
|
18
|
+
if (stat.isDirectory()) {
|
|
19
|
+
fs.mkdirSync(target, { recursive: true });
|
|
20
|
+
for (const entry of fs.readdirSync(source)) copyRecursive(path.join(source, entry), path.join(target, entry));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
24
|
+
fs.copyFileSync(source, target, fs.constants.COPYFILE_EXCL);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function removeOwnedPath(target) {
|
|
28
|
+
if (!fs.existsSync(target)) return;
|
|
29
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function pruneEmptyParents(target, boundary) {
|
|
33
|
+
const root = path.resolve(boundary);
|
|
34
|
+
let current = path.dirname(path.resolve(target));
|
|
35
|
+
while (current !== root && current.startsWith(`${root}${path.sep}`)) {
|
|
36
|
+
if (!fs.existsSync(current) || fs.readdirSync(current).length > 0) break;
|
|
37
|
+
fs.rmdirSync(current);
|
|
38
|
+
current = path.dirname(current);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function deterministicProjectId(slug, config) {
|
|
43
|
+
const identity = String(config && (config.projectId || config.gitPath || config.localPath || config.repoPath) || slug);
|
|
44
|
+
const existing = String(config && config.projectId || '');
|
|
45
|
+
if (/^[A-Za-z0-9][A-Za-z0-9._-]{2,127}$/.test(existing)) return existing;
|
|
46
|
+
return `project-${crypto.createHash('sha256').update(`${slug}\0${identity}`).digest('hex').slice(0, 20)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function safeReadJson(filePath, fallback = null) {
|
|
50
|
+
if (!fs.existsSync(filePath)) return fallback;
|
|
51
|
+
try {
|
|
52
|
+
// Legacy files written via PowerShell/tooling often carry a UTF-8 BOM;
|
|
53
|
+
// JSON.parse rejects it, and a BOM must never fail the whole migration.
|
|
54
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
55
|
+
return JSON.parse(raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw);
|
|
56
|
+
}
|
|
57
|
+
catch (error) { throw new DomainError('DATA_CORRUPT', 'Legacy migration source JSON is corrupt.', { status: 500, cause: error, details: { category: path.basename(filePath) } }); }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function sourceSnapshot(paths) {
|
|
61
|
+
const entries = [];
|
|
62
|
+
for (const source of paths) {
|
|
63
|
+
if (!fs.existsSync(source)) continue;
|
|
64
|
+
const stat = fs.statSync(source);
|
|
65
|
+
entries.push({
|
|
66
|
+
path: source,
|
|
67
|
+
kind: stat.isDirectory() ? 'directory' : 'file',
|
|
68
|
+
size: stat.size,
|
|
69
|
+
mtimeMs: stat.mtimeMs,
|
|
70
|
+
hash: stat.isFile() ? hashFile(source) : '',
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return entries;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function verifySourceSnapshot(entries) {
|
|
77
|
+
for (const entry of entries) {
|
|
78
|
+
if (!fs.existsSync(entry.path)) return false;
|
|
79
|
+
const stat = fs.statSync(entry.path);
|
|
80
|
+
if (entry.kind === 'file' && (stat.size !== entry.size || hashFile(entry.path) !== entry.hash)) return false;
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function directoryDigest(root) {
|
|
86
|
+
const hash = crypto.createHash('sha256');
|
|
87
|
+
const walk = current => {
|
|
88
|
+
for (const entry of fs.readdirSync(current, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
89
|
+
const item = path.join(current, entry.name);
|
|
90
|
+
hash.update(`${entry.isDirectory() ? 'd' : 'f'}:${path.relative(root, item).replace(/\\/g, '/')}\0`);
|
|
91
|
+
if (entry.isDirectory()) walk(item); else if (entry.isFile()) hash.update(fs.readFileSync(item));
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
walk(root);
|
|
95
|
+
return hash.digest('hex');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
class MigrationService {
|
|
99
|
+
constructor(options = {}) {
|
|
100
|
+
this.layout = options.layout || new StorageLayout(options);
|
|
101
|
+
this.atomic = options.atomic || AtomicFile;
|
|
102
|
+
this.legacyDataDir = path.resolve(options.legacyDataDir || this.layout.getDataDir());
|
|
103
|
+
this.indexValidator = options.indexValidator || (async indexPath => ({ ok: fs.existsSync(indexPath) }));
|
|
104
|
+
this.logger = options.logger || { info() {}, warn() {}, error() {} };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
completion() {
|
|
108
|
+
const completionPath = this.layout.getMigrationCompletionPath();
|
|
109
|
+
if (!fs.existsSync(completionPath)) return null;
|
|
110
|
+
const marker = this.atomic.readJsonStrict(completionPath, { category: 'layout-migration-completion' });
|
|
111
|
+
return marker && ['layout-migration-completion/v1', 'layout-migration-completion/v2'].includes(marker.schema) && marker.migrationId === SCHEMA_VERSIONS.layoutMigration
|
|
112
|
+
? marker
|
|
113
|
+
: null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
completionMarker({ runId = '', registry, settings, modelsMigrated = false } = {}) {
|
|
117
|
+
const profiles = settings && settings.ai && Array.isArray(settings.ai.profiles) ? settings.ai.profiles : [];
|
|
118
|
+
return {
|
|
119
|
+
schema: 'layout-migration-completion/v2',
|
|
120
|
+
migrationId: SCHEMA_VERSIONS.layoutMigration,
|
|
121
|
+
runId,
|
|
122
|
+
sourceRoot: this.legacyDataDir,
|
|
123
|
+
sourceManifestHash: hashBuffer(Buffer.from(JSON.stringify(LEGACY_ASSETS.map(asset => [asset.source, asset.target, asset.kind])))),
|
|
124
|
+
projectCount: registry.projectOrder.length,
|
|
125
|
+
aiProfileCount: profiles.length,
|
|
126
|
+
knowledgeRootConfigured: Boolean(settings.knowledge && settings.knowledge.rootPath),
|
|
127
|
+
embeddingConfigured: Boolean(settings.embedding && Object.keys(settings.embedding).length),
|
|
128
|
+
modelsMigrated,
|
|
129
|
+
completedAt: new Date().toISOString(),
|
|
130
|
+
verified: true,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
validateCompletedState(marker) {
|
|
135
|
+
try {
|
|
136
|
+
const registry = this.atomic.readJsonStrict(this.layout.getProjectRegistryPath(), { category: 'project-registry' });
|
|
137
|
+
validateRegistry(registry);
|
|
138
|
+
const settings = this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' });
|
|
139
|
+
validateSettings(settings);
|
|
140
|
+
for (const projectId of registry.projectOrder) {
|
|
141
|
+
validateProjectConfig(this.atomic.readJsonStrict(this.layout.getProjectConfigPath(projectId), { category: 'project-config' }), projectId);
|
|
142
|
+
validateProjectState(this.atomic.readJsonStrict(this.layout.getProjectStatePath(projectId), { category: 'project-state' }));
|
|
143
|
+
}
|
|
144
|
+
// marker.projectCount is a migration-time snapshot only. Imports and
|
|
145
|
+
// deletions legitimately change the live registry count afterwards;
|
|
146
|
+
// comparing them here rejected every restart after the first legal
|
|
147
|
+
// project removal (completion-project-count-mismatch false positive).
|
|
148
|
+
return { ok: true, registry, settings };
|
|
149
|
+
} catch (error) { return { ok: false, reason: 'completion-state-invalid', error }; }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
discover() {
|
|
153
|
+
const paths = LEGACY_ASSETS.map(asset => assetPath(this.legacyDataDir, asset)).filter(file => fs.existsSync(file));
|
|
154
|
+
const knowledgeStore = safeReadJson(path.join(this.legacyDataDir, 'knowledge-store.json'), {});
|
|
155
|
+
const indexCandidates = [
|
|
156
|
+
path.join(this.legacyDataDir, 'knowledge.lancedb'),
|
|
157
|
+
knowledgeStore && knowledgeStore.rootPath ? path.join(path.resolve(knowledgeStore.rootPath), '.project-knowledge', 'knowledge.lancedb') : '',
|
|
158
|
+
].filter(Boolean);
|
|
159
|
+
const indexPath = indexCandidates.find(candidate => fs.existsSync(candidate)) || '';
|
|
160
|
+
if (indexPath) paths.push(indexPath);
|
|
161
|
+
return {
|
|
162
|
+
legacyProjectsPath: path.join(this.legacyDataDir, 'projects.json'),
|
|
163
|
+
knowledgeStore,
|
|
164
|
+
indexPath,
|
|
165
|
+
modelsPath: assetPath(this.legacyDataDir, getLegacyAsset('models')),
|
|
166
|
+
sources: sourceSnapshot(paths),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
mergeLegacySettings(discovery) {
|
|
171
|
+
const settings = defaultSettings();
|
|
172
|
+
const read = name => safeReadJson(path.join(this.legacyDataDir, name), null);
|
|
173
|
+
const knowledge = read('knowledge-store.json');
|
|
174
|
+
const ai = read('ai-profiles.json');
|
|
175
|
+
const embedding = read('embedding-config.json');
|
|
176
|
+
const logging = read('logging.json');
|
|
177
|
+
const prompts = read('claude-prompts.json');
|
|
178
|
+
const github = read('github-team.json');
|
|
179
|
+
const providers = read('team-git-providers.json');
|
|
180
|
+
if (knowledge && typeof knowledge.rootPath === 'string') settings.knowledge.rootPath = knowledge.rootPath;
|
|
181
|
+
if (ai && typeof ai === 'object') settings.ai = ai;
|
|
182
|
+
if (embedding && typeof embedding === 'object') settings.embedding = embedding;
|
|
183
|
+
if (logging && typeof logging === 'object') {
|
|
184
|
+
if (Array.isArray(logging.levels)) settings.logging.levels = logging.levels;
|
|
185
|
+
settings.legacyExtensions.logging = { ...logging };
|
|
186
|
+
}
|
|
187
|
+
if (prompts && typeof prompts === 'object') settings.promptOverrides = prompts;
|
|
188
|
+
if (github || providers) settings.integrations = { ...(settings.integrations || {}), ...(github ? { githubTeam: github } : {}), ...(providers ? { teamGitProviders: providers } : {}) };
|
|
189
|
+
settings.updatedAt = new Date().toISOString();
|
|
190
|
+
return validateSettings(settings);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
buildProjectRecords(legacyProjects, settings) {
|
|
194
|
+
const registry = emptyRegistry();
|
|
195
|
+
const records = [];
|
|
196
|
+
for (const [slug, rawConfig] of Object.entries(legacyProjects || {})) {
|
|
197
|
+
const legacy = rawConfig && rawConfig.config && typeof rawConfig.config === 'object' ? rawConfig.config : (rawConfig || {});
|
|
198
|
+
const projectId = deterministicProjectId(slug, legacy);
|
|
199
|
+
const knowledgePath = legacy.knowledgePath || legacy.kbPath || (settings.knowledge.rootPath ? path.join(path.resolve(settings.knowledge.rootPath), slug) : path.join(this.legacyDataDir, 'projects', slug));
|
|
200
|
+
const config = defaultProjectConfig(projectId, {
|
|
201
|
+
displayName: legacy.displayName || slug,
|
|
202
|
+
storageName: legacy.storageName || `${slug}-${projectId.slice(-6)}`,
|
|
203
|
+
repoPath: legacy.repoPath || legacy.gitPath || legacy.localPath || this.legacyDataDir,
|
|
204
|
+
knowledgePath,
|
|
205
|
+
enabled: legacy.enabled !== false,
|
|
206
|
+
createdAt: legacy.createdAt || new Date(0).toISOString(),
|
|
207
|
+
teamBinding: legacy.teamBinding || (legacy.teamKbId ? { teamKbId: legacy.teamKbId } : null),
|
|
208
|
+
aiProfileId: legacy.aiProfileId || null,
|
|
209
|
+
knowledgeLanguage: legacy.knowledgeLanguage || 'zh-CN',
|
|
210
|
+
relatedProjectIds: legacy.relatedProjectIds || [],
|
|
211
|
+
repoIdentity: legacy.repoIdentity || null,
|
|
212
|
+
legacyExtensions: { slug, sourceSchema: legacy.schema || '' },
|
|
213
|
+
});
|
|
214
|
+
const hasTracking = Boolean(legacy.trackingStartCommit || legacy.lastAnalyzedCommit);
|
|
215
|
+
const state = defaultProjectState({
|
|
216
|
+
trackingStartCommit: legacy.trackingStartCommit || null,
|
|
217
|
+
lastAnalyzedCommit: legacy.lastAnalyzedCommit || null,
|
|
218
|
+
trackingMode: hasTracking ? 'normal' : (legacy.repoStatus === 'empty-repo' ? 'empty-repo' : 'normal'),
|
|
219
|
+
hook: { managedVersion: 0, migrationVersion: 0, lastVerifiedAt: '' },
|
|
220
|
+
});
|
|
221
|
+
validateProjectConfig(config, projectId);
|
|
222
|
+
validateProjectState(state);
|
|
223
|
+
registry.projectOrder.push(projectId);
|
|
224
|
+
registry.projects[projectId] = { createdAt: config.createdAt, displayNameSnapshot: config.displayName };
|
|
225
|
+
records.push({ slug, projectId, config, state });
|
|
226
|
+
}
|
|
227
|
+
registry.updatedAt = new Date().toISOString();
|
|
228
|
+
validateRegistry(registry);
|
|
229
|
+
return { registry, records };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
injectFault(options, stage) {
|
|
233
|
+
if (options && options.faultAt === stage) throw new Error(`injected migration fault: ${stage}`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async migrateIfNeeded(options = {}) {
|
|
237
|
+
const existingCompletion = this.completion();
|
|
238
|
+
if (existingCompletion) {
|
|
239
|
+
const verified = this.validateCompletedState(existingCompletion);
|
|
240
|
+
if (!verified.ok) return { ok: false, migrated: false, completed: false, requiresManualRecovery: true, reason: verified.reason, error: verified.error };
|
|
241
|
+
if (existingCompletion.schema === 'layout-migration-completion/v2') return { ok: true, migrated: false, completed: true, marker: existingCompletion };
|
|
242
|
+
const marker = this.completionMarker({ runId: existingCompletion.runId || 'v1-marker-upgrade', registry: verified.registry, settings: verified.settings, modelsMigrated: fs.existsSync(this.layout.getCachePath('models')) });
|
|
243
|
+
this.atomic.writeJsonAtomic(this.layout.getMigrationCompletionPath(), marker);
|
|
244
|
+
return { ok: true, migrated: false, completed: true, marker };
|
|
245
|
+
}
|
|
246
|
+
const state = classifyDataState(this.legacyDataDir);
|
|
247
|
+
if (state.state === STATES.FRESH) return { ok: true, migrated: false, completed: false, reason: 'fresh-install' };
|
|
248
|
+
if (state.state === STATES.CORRUPT || state.state === STATES.MIGRATION_INCOMPLETE) {
|
|
249
|
+
return { ok: false, migrated: false, completed: false, requiresManualRecovery: true, reason: state.state.toLowerCase() };
|
|
250
|
+
}
|
|
251
|
+
const discovery = this.discover();
|
|
252
|
+
this.injectFault(options, 'discovery');
|
|
253
|
+
// A user may have configured only AI/embedding/knowledge settings before
|
|
254
|
+
// ever importing a project. That is still legacy user data and must be
|
|
255
|
+
// migrated into an explicitly empty v2 registry, never treated as Fresh.
|
|
256
|
+
const legacyProjects = safeReadJson(discovery.legacyProjectsPath, {});
|
|
257
|
+
if (legacyProjects && legacyProjects.schema === SCHEMAS.projectRegistry && legacyProjects.schemaVersion === 2 && fs.existsSync(this.layout.getSettingsPath())) {
|
|
258
|
+
validateRegistry(legacyProjects);
|
|
259
|
+
const settings = this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' });
|
|
260
|
+
validateSettings(settings);
|
|
261
|
+
const marker = this.completionMarker({ runId: 'validated-existing-v2', registry: legacyProjects, settings, modelsMigrated: fs.existsSync(this.layout.getCachePath('models')) });
|
|
262
|
+
this.atomic.writeJsonAtomic(this.layout.getMigrationCompletionPath(), marker);
|
|
263
|
+
return { ok: true, migrated: false, completed: true, marker };
|
|
264
|
+
}
|
|
265
|
+
if (state.state !== STATES.LEGACY) return { ok: false, migrated: false, completed: false, requiresManualRecovery: true, reason: 'legacy-state-not-migratable' };
|
|
266
|
+
|
|
267
|
+
const migrationId = options.migrationRunId || `layout-v2-${Date.now()}-${crypto.randomBytes(4).toString('hex')}`;
|
|
268
|
+
const recoveryDir = this.layout.getRecoveryPath(migrationId);
|
|
269
|
+
const backupDir = path.join(recoveryDir, 'backup');
|
|
270
|
+
const stagingDir = path.join(recoveryDir, 'staging');
|
|
271
|
+
fs.mkdirSync(backupDir, { recursive: true });
|
|
272
|
+
fs.mkdirSync(stagingDir, { recursive: true });
|
|
273
|
+
const journalPath = path.join(recoveryDir, 'journal.json');
|
|
274
|
+
const journal = {
|
|
275
|
+
schema: SCHEMAS.migrationJournal,
|
|
276
|
+
migrationId: SCHEMA_VERSIONS.layoutMigration,
|
|
277
|
+
runId: migrationId,
|
|
278
|
+
phase: 'discovered',
|
|
279
|
+
startedAt: new Date().toISOString(),
|
|
280
|
+
sources: discovery.sources,
|
|
281
|
+
activated: [],
|
|
282
|
+
};
|
|
283
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
284
|
+
const legacyRegistryBackup = path.join(backupDir, 'projects.json');
|
|
285
|
+
try {
|
|
286
|
+
for (const entry of discovery.sources) {
|
|
287
|
+
const relative = path.relative(this.legacyDataDir, entry.path);
|
|
288
|
+
const safeRelative = relative.startsWith('..') || path.isAbsolute(relative)
|
|
289
|
+
? path.join('external', hashBuffer(Buffer.from(entry.path)).slice(0, 16), path.basename(entry.path))
|
|
290
|
+
: relative;
|
|
291
|
+
copyRecursive(entry.path, path.join(backupDir, safeRelative));
|
|
292
|
+
}
|
|
293
|
+
journal.phase = 'backed-up';
|
|
294
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
295
|
+
this.injectFault(options, 'backup');
|
|
296
|
+
|
|
297
|
+
const settings = this.mergeLegacySettings(discovery);
|
|
298
|
+
const projectData = this.buildProjectRecords(legacyProjects, settings);
|
|
299
|
+
const staged = [];
|
|
300
|
+
const stageJson = (relative, value) => {
|
|
301
|
+
const stagedPath = path.join(stagingDir, relative);
|
|
302
|
+
this.atomic.writeJsonAtomic(stagedPath, value);
|
|
303
|
+
staged.push({ relative, kind: 'file', hash: hashFile(stagedPath) });
|
|
304
|
+
};
|
|
305
|
+
stageJson('settings.json', settings);
|
|
306
|
+
for (const record of projectData.records) {
|
|
307
|
+
stageJson(path.join('projects', record.projectId, 'config.json'), record.config);
|
|
308
|
+
stageJson(path.join('projects', record.projectId, 'state.json'), record.state);
|
|
309
|
+
}
|
|
310
|
+
stageJson('projects.json', projectData.registry);
|
|
311
|
+
let modelsMigrated = false;
|
|
312
|
+
if (fs.existsSync(discovery.modelsPath)) {
|
|
313
|
+
const modelTarget = this.layout.getCachePath('models');
|
|
314
|
+
if (fs.existsSync(modelTarget)) {
|
|
315
|
+
if (directoryDigest(discovery.modelsPath) !== directoryDigest(modelTarget)) {
|
|
316
|
+
throw new DomainError('MIGRATION_TARGET_CONFLICT', 'Legacy model cache conflicts with the current model cache.', { status: 409, details: { category: 'cache/models' } });
|
|
317
|
+
}
|
|
318
|
+
} else {
|
|
319
|
+
const relative = path.join('cache', 'models');
|
|
320
|
+
copyRecursive(discovery.modelsPath, path.join(stagingDir, relative));
|
|
321
|
+
staged.push({ relative, kind: 'directory', hash: directoryDigest(path.join(stagingDir, relative)) });
|
|
322
|
+
modelsMigrated = true;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (discovery.indexPath) {
|
|
326
|
+
const relative = path.join('index', 'knowledge.lancedb');
|
|
327
|
+
copyRecursive(discovery.indexPath, path.join(stagingDir, relative));
|
|
328
|
+
staged.push({ relative, kind: 'directory', hash: '' });
|
|
329
|
+
}
|
|
330
|
+
journal.staged = staged;
|
|
331
|
+
journal.projectMap = Object.fromEntries(projectData.records.map(record => [record.slug, record.projectId]));
|
|
332
|
+
journal.phase = 'staged';
|
|
333
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
334
|
+
this.injectFault(options, 'staging');
|
|
335
|
+
|
|
336
|
+
if (!verifySourceSnapshot(discovery.sources)) throw new DomainError('MIGRATION_SOURCE_CHANGED', 'Migration source changed while staging.', { status: 409 });
|
|
337
|
+
validateSettings(JSON.parse(fs.readFileSync(path.join(stagingDir, 'settings.json'), 'utf8')));
|
|
338
|
+
validateRegistry(JSON.parse(fs.readFileSync(path.join(stagingDir, 'projects.json'), 'utf8')));
|
|
339
|
+
for (const record of projectData.records) {
|
|
340
|
+
validateProjectConfig(JSON.parse(fs.readFileSync(path.join(stagingDir, 'projects', record.projectId, 'config.json'), 'utf8')), record.projectId);
|
|
341
|
+
validateProjectState(JSON.parse(fs.readFileSync(path.join(stagingDir, 'projects', record.projectId, 'state.json'), 'utf8')));
|
|
342
|
+
}
|
|
343
|
+
if (discovery.indexPath) {
|
|
344
|
+
const indexResult = await this.indexValidator(path.join(stagingDir, 'index', 'knowledge.lancedb'));
|
|
345
|
+
if (!indexResult || indexResult.ok !== true) throw new DomainError('MIGRATION_FAILED', 'Staged index validation failed.', { status: 500 });
|
|
346
|
+
}
|
|
347
|
+
journal.phase = 'validated';
|
|
348
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
349
|
+
this.injectFault(options, 'validation');
|
|
350
|
+
|
|
351
|
+
const activationOrder = staged.filter(item => item.relative !== 'projects.json');
|
|
352
|
+
const activationBackupDir = path.join(backupDir, 'activation-targets');
|
|
353
|
+
journal.activationTargets = activationOrder.map(item => {
|
|
354
|
+
const target = path.join(this.layout.getDataDir(), item.relative);
|
|
355
|
+
const existed = fs.existsSync(target);
|
|
356
|
+
if (item.kind === 'directory' && existed) {
|
|
357
|
+
throw new DomainError('MIGRATION_TARGET_CONFLICT', 'Migration target already exists.', { status: 409, details: { category: item.relative } });
|
|
358
|
+
}
|
|
359
|
+
if (existed) copyRecursive(target, path.join(activationBackupDir, item.relative));
|
|
360
|
+
return { relative: item.relative, kind: item.kind, existed };
|
|
361
|
+
});
|
|
362
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
363
|
+
for (const item of activationOrder) {
|
|
364
|
+
const source = path.join(stagingDir, item.relative);
|
|
365
|
+
const target = path.join(this.layout.getDataDir(), item.relative);
|
|
366
|
+
if (item.kind === 'directory') {
|
|
367
|
+
copyRecursive(source, target);
|
|
368
|
+
} else {
|
|
369
|
+
this.atomic.writeFileAtomic(target, fs.readFileSync(source));
|
|
370
|
+
}
|
|
371
|
+
journal.activated.push(item.relative);
|
|
372
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
373
|
+
}
|
|
374
|
+
this.atomic.writeFileAtomic(this.layout.getProjectRegistryPath(), fs.readFileSync(path.join(stagingDir, 'projects.json')));
|
|
375
|
+
journal.activated.push('projects.json');
|
|
376
|
+
journal.phase = 'activated';
|
|
377
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
378
|
+
this.injectFault(options, 'activation');
|
|
379
|
+
|
|
380
|
+
validateSettings(this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' }));
|
|
381
|
+
const openedRegistry = this.atomic.readJsonStrict(this.layout.getProjectRegistryPath(), { category: 'project-registry' });
|
|
382
|
+
validateRegistry(openedRegistry);
|
|
383
|
+
for (const projectId of openedRegistry.projectOrder) {
|
|
384
|
+
validateProjectConfig(this.atomic.readJsonStrict(this.layout.getProjectConfigPath(projectId), { category: 'project-config' }), projectId);
|
|
385
|
+
validateProjectState(this.atomic.readJsonStrict(this.layout.getProjectStatePath(projectId), { category: 'project-state' }));
|
|
386
|
+
}
|
|
387
|
+
journal.phase = 'open-verified';
|
|
388
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
389
|
+
this.injectFault(options, 'open-verification');
|
|
390
|
+
|
|
391
|
+
const marker = this.completionMarker({ runId: migrationId, registry: openedRegistry, settings: this.atomic.readJsonStrict(this.layout.getSettingsPath(), { category: 'settings' }), modelsMigrated });
|
|
392
|
+
this.atomic.writeJsonAtomic(this.layout.getMigrationCompletionPath(), marker);
|
|
393
|
+
journal.phase = 'completed';
|
|
394
|
+
journal.completedAt = marker.completedAt;
|
|
395
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
396
|
+
return { ok: true, migrated: true, completed: true, marker, recoveryDir, projectMap: journal.projectMap };
|
|
397
|
+
} catch (error) {
|
|
398
|
+
const rollbackErrors = [];
|
|
399
|
+
for (const targetInfo of [...(journal.activationTargets || [])].reverse()) {
|
|
400
|
+
const target = path.join(this.layout.getDataDir(), targetInfo.relative);
|
|
401
|
+
try {
|
|
402
|
+
if (targetInfo.existed) {
|
|
403
|
+
const backup = path.join(backupDir, 'activation-targets', targetInfo.relative);
|
|
404
|
+
if (targetInfo.kind === 'directory') {
|
|
405
|
+
removeOwnedPath(target);
|
|
406
|
+
copyRecursive(backup, target);
|
|
407
|
+
} else {
|
|
408
|
+
this.atomic.writeFileAtomic(target, fs.readFileSync(backup));
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
removeOwnedPath(target);
|
|
412
|
+
pruneEmptyParents(target, this.layout.getDataDir());
|
|
413
|
+
}
|
|
414
|
+
} catch (rollbackError) {
|
|
415
|
+
rollbackErrors.push({ target: targetInfo.relative, message: String(rollbackError.message || rollbackError) });
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
try {
|
|
419
|
+
if (fs.existsSync(legacyRegistryBackup)) {
|
|
420
|
+
this.atomic.writeFileAtomic(this.layout.getProjectRegistryPath(), fs.readFileSync(legacyRegistryBackup));
|
|
421
|
+
}
|
|
422
|
+
} catch (rollbackError) {
|
|
423
|
+
rollbackErrors.push({ target: 'projects.json', message: String(rollbackError.message || rollbackError) });
|
|
424
|
+
}
|
|
425
|
+
journal.phase = 'failed';
|
|
426
|
+
journal.failedAt = new Date().toISOString();
|
|
427
|
+
journal.error = { name: error.name || 'Error', code: error.code || '', message: String(error.message || error) };
|
|
428
|
+
journal.rollback = { ok: rollbackErrors.length === 0, errors: rollbackErrors };
|
|
429
|
+
this.atomic.writeJsonAtomic(journalPath, journal);
|
|
430
|
+
this.logger.error('migration.failed', { error, recoveryDir, rollbackErrors });
|
|
431
|
+
return { ok: false, migrated: false, completed: false, useLegacy: rollbackErrors.length === 0, recoveryDir, error, rollbackErrors };
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
module.exports = {
|
|
437
|
+
MigrationService,
|
|
438
|
+
deterministicProjectId,
|
|
439
|
+
hashFile,
|
|
440
|
+
sourceSnapshot,
|
|
441
|
+
verifySourceSnapshot,
|
|
442
|
+
};
|