@twinsearth/timemachine-dsh-plugin 0.0.0-stage → 0.1.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/CHANGELOG.md +55 -0
- package/LICENSE +21 -0
- package/README.md +225 -2
- package/cordis.patch.yml +28 -0
- package/docs/ARCHITECTURE.md +172 -0
- package/docs/REQUIREMENTS.md +81 -0
- package/docs/TESTPLAN.md +155 -0
- package/lib/client.js +818 -0
- package/lib/index.js +5 -0
- package/package.json +48 -4
- package/plugin.json +9 -0
- package/scripts/host-menu-patch.mjs +274 -0
- package/src/archive/tar.mjs +138 -0
- package/src/archive/zstd.mjs +206 -0
- package/src/plugin.mjs +213 -0
- package/src/plugins.mjs +355 -0
- package/src/point.mjs +671 -0
- package/src/registry.mjs +120 -0
- package/src/restore.mjs +777 -0
- package/src/scan.mjs +244 -0
- package/src/service.mjs +448 -0
- package/src/session-log.mjs +123 -0
- package/src/util/fs.mjs +92 -0
- package/src/util/paths.mjs +82 -0
package/src/restore.mjs
ADDED
|
@@ -0,0 +1,777 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restore planning and application.
|
|
3
|
+
*
|
|
4
|
+
* Planning is read-only and returns every destination path, workspace mapping,
|
|
5
|
+
* overwrite and warning the UI needs. Applying writes a pre-restore copy first,
|
|
6
|
+
* then verifies each session header and the merged registry; a failed
|
|
7
|
+
* verification rolls the touched files back.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { randomUUID } from 'node:crypto';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import os from 'node:os';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
|
|
15
|
+
import { readArchive, readRestorePoint } from './point.mjs';
|
|
16
|
+
import { mergeRegistry, registryDocument, registrySummary } from './registry.mjs';
|
|
17
|
+
import { inspectSessionBuffer, isV4Header, remapSessionCwd } from './session-log.mjs';
|
|
18
|
+
import { normalizeRegistry } from './scan.mjs';
|
|
19
|
+
import { reinstallPlugins, SELF_PLUGIN } from './plugins.mjs';
|
|
20
|
+
import { atomicWriteFile, atomicWriteJson, copyFileDurable, ensureDir, isInside, readJson, safeSegment, sha256, stamp } from './util/fs.mjs';
|
|
21
|
+
import { encodeSegment, projectKey, profileDir, resolveDshHome, sessionDir } from './util/paths.mjs';
|
|
22
|
+
|
|
23
|
+
export const RESTORE_CODES = Object.freeze({
|
|
24
|
+
SESSION_INVALID: 'TIMEMACHINE_SESSION_INVALID',
|
|
25
|
+
SESSION_MISSING: 'TIMEMACHINE_SESSION_MISSING',
|
|
26
|
+
PATH_INVALID: 'TIMEMACHINE_PATH_INVALID',
|
|
27
|
+
VERIFY_FAILED: 'TIMEMACHINE_VERIFY_FAILED',
|
|
28
|
+
APPLY_FAILED: 'TIMEMACHINE_APPLY_FAILED',
|
|
29
|
+
PLUGIN_INSTALL_FAILED: 'TIMEMACHINE_PLUGIN_INSTALL_FAILED',
|
|
30
|
+
PLUGIN_PACKAGE_INVALID: 'TIMEMACHINE_PLUGIN_PACKAGE_INVALID',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
function isObject(value) {
|
|
34
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function normalizeSourcePath(value) {
|
|
38
|
+
return String(value ?? '').replaceAll('\\', '/').replace(/\/+$/u, '').toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function winBasename(value) {
|
|
42
|
+
const parts = String(value ?? '').split(/[\\/]/u).filter((part) => part !== '');
|
|
43
|
+
return parts.at(-1) ?? '';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function pathKeyForTarget(value) {
|
|
47
|
+
const resolved = path.resolve(value);
|
|
48
|
+
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isAbsoluteForPlatform(value) {
|
|
52
|
+
if (typeof value !== 'string' || value.trim() === '') return false;
|
|
53
|
+
if (path.isAbsolute(value)) return true;
|
|
54
|
+
return /^[A-Za-z]:[\\/]/u.test(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function mapPath(input, sourcePath, title) {
|
|
58
|
+
const source = typeof sourcePath === 'string' ? sourcePath : '';
|
|
59
|
+
for (const row of input.pathMap ?? []) {
|
|
60
|
+
if (!isObject(row)) continue;
|
|
61
|
+
if (normalizeSourcePath(row.from) === normalizeSourcePath(source) && typeof row.to === 'string' && row.to.trim() !== '') {
|
|
62
|
+
return { path: path.resolve(row.to.trim()), source: 'request' };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (source !== '' && isAbsoluteForPlatform(source)) return { path: path.resolve(source), source: 'same' };
|
|
66
|
+
const fallback = path.join(os.homedir(), 'DSH-Restored', safeSegment(title || winBasename(source) || 'workspace', 'workspace'));
|
|
67
|
+
return { path: fallback, source: 'default' };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function sourceRegistryFrom(archive, point) {
|
|
71
|
+
const raw = archive.map.get('registry/workspace.json');
|
|
72
|
+
if (raw !== undefined) {
|
|
73
|
+
try { return normalizeRegistry(JSON.parse(raw.toString('utf8'))); } catch { /* fall through */ }
|
|
74
|
+
}
|
|
75
|
+
return normalizeRegistry({
|
|
76
|
+
global: { defaultWorkspaceId: null, workspaceIds: [], archivedSessionIds: [], pinnedSessionIds: [] },
|
|
77
|
+
tables: { workspaces: Object.fromEntries((point.workspaces ?? []).map((row) => [row.id, row])) },
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Build a restore plan without writing anything.
|
|
83
|
+
* @param {{
|
|
84
|
+
* file: string, dshHome?: string, profile?: string,
|
|
85
|
+
* pathMap?: Array<{from: string, to: string}>,
|
|
86
|
+
* includeSessions?: boolean, includeConfig?: boolean,
|
|
87
|
+
* includePlugins?: boolean, allowInvalidSessions?: boolean,
|
|
88
|
+
* }} input
|
|
89
|
+
*/
|
|
90
|
+
export function planRestore(input) {
|
|
91
|
+
const dshHome = resolveDshHome(input.dshHome);
|
|
92
|
+
const profile = typeof input.profile === 'string' && input.profile !== '' ? input.profile : 'desktop';
|
|
93
|
+
const archive = readArchive(input.file);
|
|
94
|
+
if (!archive.ok) return archive;
|
|
95
|
+
const point = archive.point;
|
|
96
|
+
const includeSessions = input.includeSessions !== false && point.options?.include_sessions !== false;
|
|
97
|
+
const includeConfig = input.includeConfig !== false && point.options?.include_config !== false;
|
|
98
|
+
const includePlugins = input.includePlugins !== false && point.options?.include_plugins !== false;
|
|
99
|
+
const allowInvalid = input.allowInvalidSessions === true;
|
|
100
|
+
const sourceRegistry = sourceRegistryFrom(archive, point);
|
|
101
|
+
const targetRegistryFile = path.join(dshHome, 'storages', 'workspace.json');
|
|
102
|
+
const targetRegistryRaw = readJson(targetRegistryFile, null);
|
|
103
|
+
const targetRegistry = normalizeRegistry(targetRegistryRaw);
|
|
104
|
+
const targetDocument = registryDocument(targetRegistryRaw);
|
|
105
|
+
|
|
106
|
+
const warnings = [];
|
|
107
|
+
if (point.source_platform !== undefined && point.source_platform !== process.platform) {
|
|
108
|
+
warnings.push(`还原点来自 ${point.source_platform},本机是 ${process.platform};工作区路径必须重新映射。`);
|
|
109
|
+
}
|
|
110
|
+
const sessions = [];
|
|
111
|
+
const invalid = [];
|
|
112
|
+
const workspacesBySource = new Map();
|
|
113
|
+
const seenSessionIds = new Set();
|
|
114
|
+
const sessionRows = includeSessions
|
|
115
|
+
? (archive.manifest.files ?? []).filter((row) => row.kind === 'session')
|
|
116
|
+
: [];
|
|
117
|
+
|
|
118
|
+
for (const row of sessionRows) {
|
|
119
|
+
const buffer = archive.map.get(row.rel);
|
|
120
|
+
if (buffer === undefined) {
|
|
121
|
+
invalid.push({ session_id: row.session_id ?? null, rel: row.rel, error: 'session entry is missing from the archive' });
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const inspected = inspectSessionBuffer(buffer);
|
|
125
|
+
if (!inspected.ok || !isV4Header(inspected.header ?? null)) {
|
|
126
|
+
invalid.push({ session_id: row.session_id ?? null, rel: row.rel, error: inspected.reason ?? 'session header is not v4' });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const header = inspected.header;
|
|
130
|
+
const sessionId = header.id;
|
|
131
|
+
if (seenSessionIds.has(sessionId)) {
|
|
132
|
+
invalid.push({ session_id: sessionId, rel: row.rel, error: 'duplicate session id in the restore point' });
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
seenSessionIds.add(sessionId);
|
|
136
|
+
const sourceCwd = header.cwd;
|
|
137
|
+
const pointSession = (point.sessions ?? []).find((session) => session.session_id === sessionId) ?? null;
|
|
138
|
+
const registryWorkspace = sourceRegistry.workspaces.find((workspace) => workspace.sessionIds.includes(sessionId)) ?? null;
|
|
139
|
+
const sourceWorkspaceId = registryWorkspace?.id ?? pointSession?.workspace_id ?? null;
|
|
140
|
+
const sourcePath = registryWorkspace?.path ?? pointSession?.workspace_path ?? sourceCwd;
|
|
141
|
+
const sourceTitle = registryWorkspace?.title ?? pointSession?.workspace_title ?? winBasename(sourcePath) ?? 'workspace';
|
|
142
|
+
const key = sourceWorkspaceId ?? `path:${normalizeSourcePath(sourcePath)}`;
|
|
143
|
+
let workspace = workspacesBySource.get(key);
|
|
144
|
+
if (workspace === undefined) {
|
|
145
|
+
const mapped = mapPath(input, sourcePath, sourceTitle);
|
|
146
|
+
workspace = {
|
|
147
|
+
key,
|
|
148
|
+
source_id: sourceWorkspaceId,
|
|
149
|
+
source_path: sourcePath,
|
|
150
|
+
source_title: sourceTitle,
|
|
151
|
+
target_path: mapped.path,
|
|
152
|
+
mapped_by: mapped.source,
|
|
153
|
+
session_ids: [],
|
|
154
|
+
};
|
|
155
|
+
workspacesBySource.set(key, workspace);
|
|
156
|
+
}
|
|
157
|
+
workspace.session_ids.push(sessionId);
|
|
158
|
+
sessions.push({
|
|
159
|
+
session_id: sessionId,
|
|
160
|
+
entry: row.rel,
|
|
161
|
+
source_cwd: sourceCwd,
|
|
162
|
+
source_workspace_id: sourceWorkspaceId,
|
|
163
|
+
workspace_key: key,
|
|
164
|
+
target_cwd: workspace.target_path,
|
|
165
|
+
bytes: buffer.length,
|
|
166
|
+
sha256: row.sha256 ?? sha256(buffer),
|
|
167
|
+
archived: pointSession?.archived === true,
|
|
168
|
+
pinned: pointSession?.pinned === true,
|
|
169
|
+
frame_count: inspected.frame_count,
|
|
170
|
+
torn_tail: inspected.torn_tail === true,
|
|
171
|
+
buffer,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (invalid.length > 0 && !allowInvalid) {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
reason_code: RESTORE_CODES.SESSION_INVALID,
|
|
179
|
+
detail: `${invalid.length} session(s) cannot be restored: ${invalid.map((row) => `${row.session_id ?? row.rel}: ${row.error}`).join('; ')}`,
|
|
180
|
+
invalid_sessions: invalid,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
if (includeSessions && sessions.length === 0) {
|
|
184
|
+
return { ok: false, reason_code: RESTORE_CODES.SESSION_MISSING, detail: 'the point contains no valid session selected for restore' };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const workspacePlan = [];
|
|
188
|
+
const usedTargetIds = new Set(Object.keys(targetDocument.tables.workspaces));
|
|
189
|
+
for (const workspace of workspacesBySource.values()) {
|
|
190
|
+
if (!isAbsoluteForPlatform(workspace.target_path)) {
|
|
191
|
+
return { ok: false, reason_code: RESTORE_CODES.PATH_INVALID, detail: `${workspace.source_path}: mapped path ${workspace.target_path} is not absolute` };
|
|
192
|
+
}
|
|
193
|
+
let existingId = null;
|
|
194
|
+
for (const [id, row] of Object.entries(targetDocument.tables.workspaces)) {
|
|
195
|
+
if (isObject(row) && typeof row.path === 'string' && pathKeyForTarget(row.path) === pathKeyForTarget(workspace.target_path)) {
|
|
196
|
+
existingId = id;
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const sourceId = workspace.source_id;
|
|
201
|
+
const canReuseSource = typeof sourceId === 'string' && sourceId !== '' && !usedTargetIds.has(sourceId);
|
|
202
|
+
const targetId = existingId ?? (canReuseSource ? sourceId : randomUUID());
|
|
203
|
+
usedTargetIds.add(targetId);
|
|
204
|
+
const existing = targetDocument.tables.workspaces[targetId];
|
|
205
|
+
const targetPathExists = fs.existsSync(workspace.target_path);
|
|
206
|
+
if (!targetPathExists) warnings.push(`工作区目录不存在,将按映射路径创建:${workspace.target_path}`);
|
|
207
|
+
workspacePlan.push({
|
|
208
|
+
source_id: workspace.source_id,
|
|
209
|
+
source_path: workspace.source_path,
|
|
210
|
+
source_title: workspace.source_title,
|
|
211
|
+
target_id: targetId,
|
|
212
|
+
target_path: workspace.target_path,
|
|
213
|
+
title: existing?.title ?? workspace.source_title,
|
|
214
|
+
session_ids: workspace.session_ids,
|
|
215
|
+
exists: targetPathExists,
|
|
216
|
+
is_new: existing === undefined,
|
|
217
|
+
mapped_by: workspace.mapped_by,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
const workspaceById = new Map(workspacePlan.map((row) => [row.source_id ?? `path:${normalizeSourcePath(row.source_path)}`, row]));
|
|
221
|
+
for (const session of sessions) {
|
|
222
|
+
const workspace = workspacesBySource.get(session.workspace_key);
|
|
223
|
+
const target = workspaceById.get(workspace.source_id ?? `path:${normalizeSourcePath(workspace.source_path)}`);
|
|
224
|
+
session.target_workspace_id = target?.target_id ?? null;
|
|
225
|
+
session.action = fs.existsSync(path.join(sessionDir(dshHome, session.target_cwd, session.session_id), 'session.v4.jsonl.zstd')) ? 'overwrite' : 'create';
|
|
226
|
+
session.target_rel = path.relative(dshHome, path.join(sessionDir(dshHome, session.target_cwd, session.session_id), 'session.v4.jsonl.zstd')).replaceAll('\\', '/');
|
|
227
|
+
if (session.torn_tail) warnings.push(`会话 ${session.session_id} 末尾有不完整帧(崩溃残留),已按完整帧还原。`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const attachments = [];
|
|
231
|
+
const attachmentMap = new Map();
|
|
232
|
+
if (includeSessions) {
|
|
233
|
+
for (const row of archive.manifest.files ?? []) {
|
|
234
|
+
if (row.kind !== 'attachment') continue;
|
|
235
|
+
const data = archive.map.get(row.rel);
|
|
236
|
+
if (data === undefined) continue;
|
|
237
|
+
attachments.push({ rel: row.rel, target_rel: row.rel, size: data.length, sha256: row.sha256 ?? sha256(data) });
|
|
238
|
+
attachmentMap.set(row.rel, data);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const configFiles = [];
|
|
243
|
+
const stateFiles = [];
|
|
244
|
+
if (includeConfig) {
|
|
245
|
+
for (const row of archive.manifest.files ?? []) {
|
|
246
|
+
if (row.kind !== 'config' && row.kind !== 'state') continue;
|
|
247
|
+
const data = archive.map.get(row.rel);
|
|
248
|
+
if (data === undefined) continue;
|
|
249
|
+
let targetRel = row.rel;
|
|
250
|
+
const prefix = `profiles/${point.profile}/`;
|
|
251
|
+
if (targetRel.startsWith(prefix)) targetRel = `profiles/${profile}/${targetRel.slice(prefix.length)}`;
|
|
252
|
+
const item = {
|
|
253
|
+
rel: row.rel,
|
|
254
|
+
target_rel: targetRel,
|
|
255
|
+
kind: row.kind,
|
|
256
|
+
size: data.length,
|
|
257
|
+
sha256: row.sha256 ?? sha256(data),
|
|
258
|
+
buffer: data,
|
|
259
|
+
};
|
|
260
|
+
if (row.kind === 'config') configFiles.push(item);
|
|
261
|
+
else stateFiles.push(item);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const registrySessions = sessions.map((session) => ({
|
|
266
|
+
session_id: session.session_id,
|
|
267
|
+
archived: session.archived,
|
|
268
|
+
pinned: session.pinned,
|
|
269
|
+
}));
|
|
270
|
+
const merged = mergeRegistry({
|
|
271
|
+
target: targetDocument,
|
|
272
|
+
workspaces: workspacePlan.map((workspace) => ({
|
|
273
|
+
target_id: workspace.target_id,
|
|
274
|
+
target_path: workspace.target_path,
|
|
275
|
+
title: workspace.title,
|
|
276
|
+
session_ids: workspace.session_ids,
|
|
277
|
+
created_at: null,
|
|
278
|
+
})),
|
|
279
|
+
sessions: registrySessions,
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const plugins = includePlugins
|
|
283
|
+
? (point.plugins ?? []).filter((plugin) => plugin?.name !== SELF_PLUGIN)
|
|
284
|
+
: [];
|
|
285
|
+
const pluginPackages = input.useSavedPackages === true ? (point.plugin_package_files ?? {}) : {};
|
|
286
|
+
for (const [name, rel] of Object.entries(pluginPackages)) {
|
|
287
|
+
if (!archive.map.has(rel)) return { ok: false, reason_code: RESTORE_CODES.PLUGIN_PACKAGE_INVALID, detail: `${name}: ${rel} is missing` };
|
|
288
|
+
}
|
|
289
|
+
if (includeConfig && !includePlugins && plugins.length === 0 && (point.plugins ?? []).length > 0) {
|
|
290
|
+
warnings.push('本还原点包含第三方插件清单,但本次未勾选“安装第三方插件”;配置里仍会写入插件条目,DSH 可能提示插件缺失。');
|
|
291
|
+
}
|
|
292
|
+
const overwrites = sessions.filter((session) => session.action === 'overwrite').length;
|
|
293
|
+
if (overwrites > 0) warnings.push(`${overwrites} 个同名会话已存在,会被本还原点覆盖;覆盖前会写入 .timemachine-backups。`);
|
|
294
|
+
|
|
295
|
+
const result = {
|
|
296
|
+
ok: true,
|
|
297
|
+
file: archive.file,
|
|
298
|
+
bytes: archive.bytes,
|
|
299
|
+
sha256: archive.sha256,
|
|
300
|
+
point,
|
|
301
|
+
counts: archive.manifest.counts ?? point.counts,
|
|
302
|
+
platform: process.platform,
|
|
303
|
+
source_platform: point.source_platform ?? null,
|
|
304
|
+
cross_platform: point.source_platform !== undefined && point.source_platform !== process.platform,
|
|
305
|
+
include_sessions: includeSessions,
|
|
306
|
+
include_config: includeConfig,
|
|
307
|
+
include_plugins: includePlugins,
|
|
308
|
+
sessions: sessions.map(({ buffer, ...row }) => row),
|
|
309
|
+
invalid_sessions: invalid,
|
|
310
|
+
attachments,
|
|
311
|
+
config_files: configFiles.map(({ buffer, ...row }) => row),
|
|
312
|
+
state_files: stateFiles.map(({ buffer, ...row }) => row),
|
|
313
|
+
registry: {
|
|
314
|
+
target_file: targetRegistryFile,
|
|
315
|
+
source: registrySummary(targetRegistryRaw),
|
|
316
|
+
merged: registrySummary(merged.document),
|
|
317
|
+
document: merged.document,
|
|
318
|
+
added_workspaces: merged.added_workspaces,
|
|
319
|
+
added_sessions: merged.added_sessions,
|
|
320
|
+
},
|
|
321
|
+
workspaces: workspacePlan,
|
|
322
|
+
plugins,
|
|
323
|
+
plugin_packages: pluginPackages,
|
|
324
|
+
will: {
|
|
325
|
+
sessions: sessions.length,
|
|
326
|
+
attachments: attachments.length,
|
|
327
|
+
config_files: configFiles.length,
|
|
328
|
+
state_files: stateFiles.length,
|
|
329
|
+
registry: true,
|
|
330
|
+
plugins: plugins.length,
|
|
331
|
+
restart_required: true,
|
|
332
|
+
},
|
|
333
|
+
warnings,
|
|
334
|
+
};
|
|
335
|
+
// Buffers are needed by applyRestore but must never be serialized into the
|
|
336
|
+
// plan HTTP response.
|
|
337
|
+
Object.defineProperty(result, 'session_buffers', { value: sessions, enumerable: false });
|
|
338
|
+
Object.defineProperty(result, 'attachment_map', { value: attachmentMap, enumerable: false });
|
|
339
|
+
Object.defineProperty(result, 'config_buffers', { value: configFiles, enumerable: false });
|
|
340
|
+
Object.defineProperty(result, 'state_buffers', { value: stateFiles, enumerable: false });
|
|
341
|
+
Object.defineProperty(result, 'archive_map', { value: archive.map, enumerable: false });
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function backupDirFor(dshHome) {
|
|
346
|
+
const dir = path.join(dshHome, '.timemachine-backups', `${stamp()}-${randomUUID().slice(0, 8)}`);
|
|
347
|
+
ensureDir(dir);
|
|
348
|
+
return dir;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function backupExisting(target, rel, backupDir) {
|
|
352
|
+
if (!fs.existsSync(target)) return null;
|
|
353
|
+
const safeRel = rel.split('/').filter((part) => part !== '' && part !== '.' && part !== '..').join(path.sep);
|
|
354
|
+
if (safeRel === '') return null;
|
|
355
|
+
const copy = path.join(backupDir, 'files', safeRel);
|
|
356
|
+
ensureDir(path.dirname(copy));
|
|
357
|
+
if (fs.statSync(target).isDirectory()) {
|
|
358
|
+
fs.cpSync(target, copy, { recursive: true, force: true });
|
|
359
|
+
} else {
|
|
360
|
+
copyFileDurable(target, copy);
|
|
361
|
+
}
|
|
362
|
+
return copy;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Apply a plan. `configOverrides` maps a target_rel to the exact bytes to write
|
|
367
|
+
* (used for package.json after the installer updated dependency versions).
|
|
368
|
+
*
|
|
369
|
+
* `skipRegistry` is used when the caller can register the workspaces through
|
|
370
|
+
* the live DSH `workspaceRegistry` service. Writing `storages/workspace.json`
|
|
371
|
+
* under a running host would race its in-memory domain, so the live path
|
|
372
|
+
* leaves that file to the service.
|
|
373
|
+
*
|
|
374
|
+
* @param {ReturnType<typeof planRestore>} plan
|
|
375
|
+
* @param {{configOverrides?: Record<string, Buffer>, backupDir?: string, skipRegistry?: boolean}} [options]
|
|
376
|
+
*/
|
|
377
|
+
export function applyRestore(plan, options = {}) {
|
|
378
|
+
if (!plan?.ok) return plan;
|
|
379
|
+
// plan carries absolute targets; resolve the live home from the registry file.
|
|
380
|
+
const home = path.dirname(path.dirname(plan.registry.target_file));
|
|
381
|
+
const backupDir = options.backupDir ?? backupDirFor(home);
|
|
382
|
+
const created = [];
|
|
383
|
+
const createdDirs = [];
|
|
384
|
+
const backedUp = [];
|
|
385
|
+
const overrides = options.configOverrides ?? {};
|
|
386
|
+
|
|
387
|
+
const write = (target, data, mode = 0o600) => {
|
|
388
|
+
ensureDir(path.dirname(target));
|
|
389
|
+
const existed = fs.existsSync(target);
|
|
390
|
+
if (existed) {
|
|
391
|
+
const rel = path.relative(home, target).replaceAll('\\', '/');
|
|
392
|
+
const copy = backupExisting(target, rel, backupDir);
|
|
393
|
+
if (copy !== null) backedUp.push({ target, copy });
|
|
394
|
+
} else {
|
|
395
|
+
created.push(target);
|
|
396
|
+
}
|
|
397
|
+
atomicWriteFile(target, data, mode);
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
try {
|
|
401
|
+
if (plan.include_sessions) {
|
|
402
|
+
for (const session of plan.session_buffers ?? []) {
|
|
403
|
+
const target = path.join(dshHomeForPlan(plan), session.target_rel);
|
|
404
|
+
let buffer = session.buffer;
|
|
405
|
+
if (pathKeyForTarget(session.source_cwd) !== pathKeyForTarget(session.target_cwd)) {
|
|
406
|
+
const remapped = remapSessionCwd(buffer, session.target_cwd);
|
|
407
|
+
buffer = remapped.buffer;
|
|
408
|
+
}
|
|
409
|
+
write(target, buffer);
|
|
410
|
+
}
|
|
411
|
+
for (const attachment of plan.attachments ?? []) {
|
|
412
|
+
const data = plan.attachment_map?.get?.(attachment.rel) ?? null;
|
|
413
|
+
if (data === null) continue;
|
|
414
|
+
write(path.join(home, attachment.target_rel), data);
|
|
415
|
+
}
|
|
416
|
+
for (const workspace of plan.workspaces ?? []) {
|
|
417
|
+
if (workspace.exists === true) continue;
|
|
418
|
+
try {
|
|
419
|
+
fs.mkdirSync(workspace.target_path, { recursive: true });
|
|
420
|
+
createdDirs.push(workspace.target_path);
|
|
421
|
+
} catch {
|
|
422
|
+
// A missing workspace folder is not fatal: DSH may create it on first open.
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (plan.include_config) {
|
|
427
|
+
for (const item of [...(plan.config_buffers ?? []), ...(plan.state_buffers ?? [])]) {
|
|
428
|
+
const data = overrides[item.target_rel] ?? item.buffer;
|
|
429
|
+
write(path.join(home, item.target_rel), data);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (options.skipRegistry !== true) {
|
|
433
|
+
const registryTarget = plan.registry.target_file;
|
|
434
|
+
write(registryTarget, Buffer.from(`${JSON.stringify(plan.registry.document, null, 2)}\n`, 'utf8'));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Verification: every restored session must be readable and carry the expected id/cwd.
|
|
438
|
+
const verifyErrors = [];
|
|
439
|
+
if (plan.include_sessions) {
|
|
440
|
+
for (const session of plan.sessions ?? []) {
|
|
441
|
+
const file = path.join(home, session.target_rel);
|
|
442
|
+
try {
|
|
443
|
+
const buffer = fs.readFileSync(file);
|
|
444
|
+
const inspected = inspectSessionBuffer(buffer);
|
|
445
|
+
if (!inspected.ok) verifyErrors.push(`${session.session_id}: ${inspected.reason}`);
|
|
446
|
+
else if (inspected.header?.id !== session.session_id) verifyErrors.push(`${session.session_id}: header id ${inspected.header?.id}`);
|
|
447
|
+
else if (pathKeyForTarget(inspected.header?.cwd ?? '') !== pathKeyForTarget(session.target_cwd)) verifyErrors.push(`${session.session_id}: header cwd ${inspected.header?.cwd}`);
|
|
448
|
+
} catch (error) {
|
|
449
|
+
verifyErrors.push(`${session.session_id}: ${error instanceof Error ? error.message : String(error)}`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (options.skipRegistry !== true) {
|
|
454
|
+
const registryTarget = plan.registry.target_file;
|
|
455
|
+
const registry = readJson(registryTarget, null);
|
|
456
|
+
if (!isObject(registry)) verifyErrors.push('workspace.json is not a JSON object after restore');
|
|
457
|
+
else {
|
|
458
|
+
const rows = registry?.tables?.workspaces ?? {};
|
|
459
|
+
for (const workspace of plan.workspaces ?? []) {
|
|
460
|
+
const row = rows[workspace.target_id];
|
|
461
|
+
if (!isObject(row)) verifyErrors.push(`workspace ${workspace.target_id} is missing`);
|
|
462
|
+
else for (const id of workspace.session_ids) {
|
|
463
|
+
if (!Array.isArray(row.sessionIds) || !row.sessionIds.includes(id)) verifyErrors.push(`workspace ${workspace.target_id} is missing session ${id}`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
for (const session of plan.sessions ?? []) {
|
|
467
|
+
const found = Object.values(rows).some((row) => Array.isArray(row?.sessionIds) && row.sessionIds.includes(session.session_id));
|
|
468
|
+
if (!found) verifyErrors.push(`session ${session.session_id} is not registered in any workspace`);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (verifyErrors.length > 0) {
|
|
473
|
+
const rollback = rollbackRestore(backupDir, created, backedUp, createdDirs);
|
|
474
|
+
return {
|
|
475
|
+
ok: false,
|
|
476
|
+
reason_code: RESTORE_CODES.VERIFY_FAILED,
|
|
477
|
+
detail: verifyErrors.slice(0, 12).join('; '),
|
|
478
|
+
rollback,
|
|
479
|
+
pre_restore_dir: backupDir,
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
return {
|
|
483
|
+
ok: true,
|
|
484
|
+
pre_restore_dir: backupDir,
|
|
485
|
+
restored: {
|
|
486
|
+
sessions: plan.will?.sessions ?? 0,
|
|
487
|
+
attachments: plan.will?.attachments ?? 0,
|
|
488
|
+
config_files: plan.will?.config_files ?? 0,
|
|
489
|
+
state_files: plan.will?.state_files ?? 0,
|
|
490
|
+
registry: options.skipRegistry !== true,
|
|
491
|
+
},
|
|
492
|
+
workspaces: plan.workspaces,
|
|
493
|
+
reload_required: true,
|
|
494
|
+
};
|
|
495
|
+
} catch (error) {
|
|
496
|
+
const rollback = rollbackRestore(backupDir, created, backedUp, createdDirs);
|
|
497
|
+
return {
|
|
498
|
+
ok: false,
|
|
499
|
+
reason_code: RESTORE_CODES.APPLY_FAILED,
|
|
500
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
501
|
+
rollback,
|
|
502
|
+
pre_restore_dir: backupDir,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function dshHomeForPlan(plan) {
|
|
508
|
+
return path.dirname(path.dirname(plan.registry.target_file));
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function rollbackRestore(backupDir, created, backedUp, createdDirs = []) {
|
|
512
|
+
const removed = [];
|
|
513
|
+
const restored = [];
|
|
514
|
+
const failed = [];
|
|
515
|
+
for (const target of [...created].reverse()) {
|
|
516
|
+
try { fs.rmSync(target, { recursive: true, force: true }); removed.push(target); } catch (error) { failed.push({ target, error: String(error) }); }
|
|
517
|
+
}
|
|
518
|
+
for (const dir of [...createdDirs].reverse()) {
|
|
519
|
+
try { fs.rmdirSync(dir); removed.push(dir); } catch { /* not empty or already gone */ }
|
|
520
|
+
}
|
|
521
|
+
for (const row of [...backedUp].reverse()) {
|
|
522
|
+
try {
|
|
523
|
+
ensureDir(path.dirname(row.target));
|
|
524
|
+
fs.cpSync(row.copy, row.target, { recursive: true, force: true });
|
|
525
|
+
restored.push(row.target);
|
|
526
|
+
} catch (error) {
|
|
527
|
+
failed.push({ target: row.target, error: String(error) });
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
return { dir: backupDir, removed, restored, failed };
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** Extract saved plugin tarballs from the point into a temp folder for the installer. */
|
|
534
|
+
export function extractPluginPackages(plan, destination) {
|
|
535
|
+
const packages = {};
|
|
536
|
+
ensureDir(destination);
|
|
537
|
+
for (const [name, rel] of Object.entries(plan.plugin_packages ?? {})) {
|
|
538
|
+
const data = plan.archive_map?.get?.(rel);
|
|
539
|
+
if (data === undefined) throw new Error(`${name}: ${rel} is missing`);
|
|
540
|
+
const target = path.join(destination, path.basename(rel));
|
|
541
|
+
atomicWriteFile(target, data, 0o600);
|
|
542
|
+
packages[name] = target;
|
|
543
|
+
}
|
|
544
|
+
return packages;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Register restored workspaces and sessions through the running DSH
|
|
549
|
+
* `workspaceRegistry` service. This is what makes a one-click restore visible
|
|
550
|
+
* without restarting the client: the service writes through its live storage
|
|
551
|
+
* domain, whose changes the Workspace follow stream publishes to the client.
|
|
552
|
+
*
|
|
553
|
+
* Returns the real workspace ids (the plan's ids are only a file-level
|
|
554
|
+
* preview) and the first restored session for the client to open.
|
|
555
|
+
*
|
|
556
|
+
* @param {object} workspaceRegistry live `workspaceRegistry` service
|
|
557
|
+
* @param {ReturnType<typeof planRestore>} plan
|
|
558
|
+
*/
|
|
559
|
+
export async function registerRestoredWorkspaces(workspaceRegistry, plan) {
|
|
560
|
+
if (workspaceRegistry === undefined || workspaceRegistry === null) {
|
|
561
|
+
return { ok: false, reason_code: 'TIMEMACHINE_REGISTRY_UNAVAILABLE', detail: 'DSH workspaceRegistry service is unavailable' };
|
|
562
|
+
}
|
|
563
|
+
if (typeof workspaceRegistry.resolveByPath !== 'function' || typeof workspaceRegistry.create !== 'function') {
|
|
564
|
+
return { ok: false, reason_code: 'TIMEMACHINE_REGISTRY_UNAVAILABLE', detail: 'DSH workspaceRegistry service lacks resolveByPath/create' };
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const createdWorkspaces = [];
|
|
568
|
+
const attached = [];
|
|
569
|
+
const workspaces = [];
|
|
570
|
+
try {
|
|
571
|
+
for (const workspace of plan.workspaces ?? []) {
|
|
572
|
+
try {
|
|
573
|
+
fs.mkdirSync(workspace.target_path, { recursive: true });
|
|
574
|
+
} catch {
|
|
575
|
+
// create() below reports a precise error when the directory is unusable.
|
|
576
|
+
}
|
|
577
|
+
let live = await workspaceRegistry.resolveByPath(workspace.target_path);
|
|
578
|
+
if (live === undefined || live === null) {
|
|
579
|
+
live = await workspaceRegistry.create(workspace.target_path, workspace.title);
|
|
580
|
+
createdWorkspaces.push(live.id);
|
|
581
|
+
}
|
|
582
|
+
const known = new Set(Array.isArray(live.sessionIds) ? live.sessionIds : []);
|
|
583
|
+
for (const sessionId of workspace.session_ids ?? []) {
|
|
584
|
+
if (known.has(sessionId)) continue;
|
|
585
|
+
await live.attachSession(sessionId);
|
|
586
|
+
attached.push({ workspace: live, sessionId });
|
|
587
|
+
known.add(sessionId);
|
|
588
|
+
}
|
|
589
|
+
workspaces.push({
|
|
590
|
+
...workspace,
|
|
591
|
+
target_id: live.id,
|
|
592
|
+
live: true,
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Archive/pin flags are part of the registry. Apply them after every
|
|
597
|
+
// session is attached so a failure cannot leave a half-accounted session.
|
|
598
|
+
for (const session of plan.sessions ?? []) {
|
|
599
|
+
if (session.archived === true && typeof workspaceRegistry.archiveSession === 'function') {
|
|
600
|
+
await workspaceRegistry.archiveSession(session.session_id, { stopActivity: true });
|
|
601
|
+
}
|
|
602
|
+
if (session.pinned === true && session.archived !== true && typeof workspaceRegistry.pinSession === 'function') {
|
|
603
|
+
await workspaceRegistry.pinSession(session.session_id);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// Verify against the live service, not the plan document.
|
|
608
|
+
const listed = typeof workspaceRegistry.list === 'function' ? workspaceRegistry.list() : [];
|
|
609
|
+
for (const workspace of workspaces) {
|
|
610
|
+
const live = listed.find((row) => row.id === workspace.target_id);
|
|
611
|
+
if (live === undefined) throw new Error(`workspace ${workspace.target_id} is missing from the live registry`);
|
|
612
|
+
for (const sessionId of workspace.session_ids ?? []) {
|
|
613
|
+
if (!Array.isArray(live.sessionIds) || !live.sessionIds.includes(sessionId)) {
|
|
614
|
+
throw new Error(`workspace ${workspace.target_id} is missing session ${sessionId}`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const first = (plan.sessions ?? [])[0] ?? null;
|
|
620
|
+
return {
|
|
621
|
+
ok: true,
|
|
622
|
+
workspaces,
|
|
623
|
+
first_session_id: first?.session_id ?? null,
|
|
624
|
+
first_workspace_id: workspaces[0]?.target_id ?? null,
|
|
625
|
+
display: first === null ? null : {
|
|
626
|
+
action: 'open_session',
|
|
627
|
+
session_id: first.session_id,
|
|
628
|
+
workspace_id: workspaces.find((row) => row.session_ids.includes(first.session_id))?.target_id ?? workspaces[0]?.target_id ?? null,
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
} catch (error) {
|
|
632
|
+
// Best-effort compensation: detach sessions from pre-existing workspaces and
|
|
633
|
+
// delete workspaces this call created. Restored files stay on disk; the
|
|
634
|
+
// caller reports that a restart/retry is still required.
|
|
635
|
+
for (const row of [...attached].reverse()) {
|
|
636
|
+
try { await row.workspace.detachSession(row.sessionId); } catch { /* best effort */ }
|
|
637
|
+
}
|
|
638
|
+
for (const id of [...createdWorkspaces].reverse()) {
|
|
639
|
+
try { await workspaceRegistry.delete(id); } catch { /* best effort */ }
|
|
640
|
+
}
|
|
641
|
+
return {
|
|
642
|
+
ok: false,
|
|
643
|
+
reason_code: 'TIMEMACHINE_REGISTRY_LIVE_FAILED',
|
|
644
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Restore a point end to end: plan -> install plugins (optional) -> apply.
|
|
651
|
+
* @param {{
|
|
652
|
+
* file: string, dshHome?: string, profile?: string, pathMap?: array,
|
|
653
|
+
* includeSessions?: boolean, includeConfig?: boolean, includePlugins?: boolean,
|
|
654
|
+
* installPlugins?: boolean, useSavedPackages?: boolean, allowInvalidSessions?: boolean,
|
|
655
|
+
* runner?: Function, pnpmFile?: string|null, onProgress?: Function,
|
|
656
|
+
* workspaceRegistry?: object,
|
|
657
|
+
* }} input
|
|
658
|
+
*/
|
|
659
|
+
export async function restorePoint(input) {
|
|
660
|
+
const plan = planRestore(input);
|
|
661
|
+
if (!plan.ok) return plan;
|
|
662
|
+
const dshHome = dshHomeForPlan(plan);
|
|
663
|
+
const profile = typeof input.profile === 'string' && input.profile !== '' ? input.profile : plan.point.profile;
|
|
664
|
+
const profilePath = profileDir(dshHome, profile);
|
|
665
|
+
let install = null;
|
|
666
|
+
let backupDir = null;
|
|
667
|
+
if (input.installPlugins !== false && plan.plugins.length > 0) {
|
|
668
|
+
backupDir = backupDirFor(dshHome);
|
|
669
|
+
const packageFile = path.join(profilePath, 'package.json');
|
|
670
|
+
const patchFile = path.join(profilePath, 'cordis.patch.yml');
|
|
671
|
+
const preserved = [
|
|
672
|
+
[packageFile, path.join(backupDir, 'files', 'profiles', profile, 'package.json')],
|
|
673
|
+
[patchFile, path.join(backupDir, 'files', 'profiles', profile, 'cordis.patch.yml')],
|
|
674
|
+
];
|
|
675
|
+
for (const [source, copy] of preserved) {
|
|
676
|
+
if (fs.existsSync(source)) copyFileDurable(source, copy);
|
|
677
|
+
}
|
|
678
|
+
let temp = null;
|
|
679
|
+
try {
|
|
680
|
+
let packages = {};
|
|
681
|
+
if (input.useSavedPackages === true) {
|
|
682
|
+
temp = fs.mkdtempSync(path.join(os.tmpdir(), 'timemachine-packages-'));
|
|
683
|
+
packages = extractPluginPackages(plan, temp);
|
|
684
|
+
}
|
|
685
|
+
install = await reinstallPlugins({
|
|
686
|
+
profileDir: profilePath,
|
|
687
|
+
plugins: plan.plugins,
|
|
688
|
+
packages,
|
|
689
|
+
runner: input.runner,
|
|
690
|
+
pnpmFile: input.pnpmFile,
|
|
691
|
+
onProgress: input.onProgress,
|
|
692
|
+
});
|
|
693
|
+
} catch (error) {
|
|
694
|
+
install = {
|
|
695
|
+
ok: false,
|
|
696
|
+
reason_code: RESTORE_CODES.PLUGIN_PACKAGE_INVALID,
|
|
697
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
698
|
+
installed: [],
|
|
699
|
+
failed: [],
|
|
700
|
+
skipped: [],
|
|
701
|
+
};
|
|
702
|
+
} finally {
|
|
703
|
+
if (temp !== null) fs.rmSync(temp, { recursive: true, force: true });
|
|
704
|
+
}
|
|
705
|
+
if (!install.ok) {
|
|
706
|
+
// The installer may have rewritten package.json before failing. Put the
|
|
707
|
+
// two config files the installer touches back; no session/config file was
|
|
708
|
+
// written yet, so the home stays as it was.
|
|
709
|
+
for (const [source, copy] of preserved) {
|
|
710
|
+
try {
|
|
711
|
+
if (fs.existsSync(copy)) {
|
|
712
|
+
ensureDir(path.dirname(source));
|
|
713
|
+
fs.copyFileSync(copy, source);
|
|
714
|
+
}
|
|
715
|
+
} catch { /* reported as part of the failure */ }
|
|
716
|
+
}
|
|
717
|
+
return {
|
|
718
|
+
ok: false,
|
|
719
|
+
reason_code: install.reason_code ?? RESTORE_CODES.PLUGIN_INSTALL_FAILED,
|
|
720
|
+
detail: install.detail,
|
|
721
|
+
stage: 'plugin-install',
|
|
722
|
+
plugins: install,
|
|
723
|
+
plan_ready: true,
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// pnpm writes the installed versions into package.json. Re-apply the
|
|
728
|
+
// archived package metadata, but keep the versions that were just installed.
|
|
729
|
+
const archived = plan.config_buffers?.find((row) => row.target_rel === `profiles/${profile}/package.json`);
|
|
730
|
+
if (archived !== undefined) {
|
|
731
|
+
try {
|
|
732
|
+
const current = JSON.parse(fs.readFileSync(packageFile, 'utf8'));
|
|
733
|
+
const next = JSON.parse(archived.buffer.toString('utf8'));
|
|
734
|
+
next.dependencies = isObject(next.dependencies) ? next.dependencies : {};
|
|
735
|
+
if (isObject(current.dependencies)) {
|
|
736
|
+
for (const row of install.installed ?? []) {
|
|
737
|
+
if (typeof current.dependencies[row.name] === 'string') next.dependencies[row.name] = current.dependencies[row.name];
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
plan.configOverrides = { [`profiles/${profile}/package.json`]: Buffer.from(`${JSON.stringify(next, null, 2)}\n`, 'utf8') };
|
|
741
|
+
} catch { /* keep the archived package.json */ }
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
const liveRegistry = input.workspaceRegistry ?? null;
|
|
745
|
+
const applied = applyRestore(plan, {
|
|
746
|
+
configOverrides: plan.configOverrides,
|
|
747
|
+
backupDir,
|
|
748
|
+
skipRegistry: liveRegistry !== null,
|
|
749
|
+
});
|
|
750
|
+
if (!applied.ok) return { ...applied, stage: 'file-restore', plugins: install };
|
|
751
|
+
|
|
752
|
+
let live = null;
|
|
753
|
+
if (liveRegistry !== null) {
|
|
754
|
+
live = await registerRestoredWorkspaces(liveRegistry, plan);
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
ok: true,
|
|
758
|
+
file: plan.file,
|
|
759
|
+
point: plan.point,
|
|
760
|
+
stage: 'restored',
|
|
761
|
+
plugins: install,
|
|
762
|
+
restore: applied,
|
|
763
|
+
workspaces: live?.workspaces ?? applied.workspaces,
|
|
764
|
+
display: live?.display ?? null,
|
|
765
|
+
display_hint: live?.ok === true
|
|
766
|
+
? '还原完成,工作区与会话已导入当前 DSH 并自动打开。配置文件与插件列表会在下次启动时生效。'
|
|
767
|
+
: '还原完成,文件已写入;当前 DSH 未能即时注册工作区。请完全退出 DSH 客户端后重新启动。',
|
|
768
|
+
live_registration: live,
|
|
769
|
+
pre_restore_dir: applied.pre_restore_dir,
|
|
770
|
+
restart_required: live?.ok !== true,
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/** Read a point's metadata; exported for the route layer. */
|
|
775
|
+
export function readPoint(file) {
|
|
776
|
+
return readRestorePoint(file);
|
|
777
|
+
}
|