@xsok/dsh-enhanced 0.0.0-stage → 0.2.2-11
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 +103 -0
- package/LICENSE +21 -0
- package/README.md +149 -2
- package/client.js +3791 -0
- package/cordis.patch.yml +4 -0
- package/host/engine/archive.js +223 -0
- package/host/engine/atomic.js +27 -0
- package/host/engine/attachments.js +102 -0
- package/host/engine/config.js +218 -0
- package/host/engine/index.js +356 -0
- package/host/engine/ledger.js +108 -0
- package/host/engine/paths.js +81 -0
- package/host/engine/pluginlist.js +81 -0
- package/host/engine/projcache.js +202 -0
- package/host/engine/prune.js +187 -0
- package/host/engine/scan.js +177 -0
- package/host/engine/session.js +138 -0
- package/host/engine/sessions.js +395 -0
- package/host/engine/workspace.js +255 -0
- package/host/engine/zstd.js +91 -0
- package/host/envcheck.js +289 -0
- package/host/mirror.js +502 -0
- package/host/move.js +196 -0
- package/host/overwrite.js +305 -0
- package/host/restart.js +158 -0
- package/host/schedule.js +319 -0
- package/host/search.js +250 -0
- package/host/sync.js +608 -0
- package/host/trash.js +738 -0
- package/host/trashsync.js +204 -0
- package/host/webdav.js +429 -0
- package/icon.svg +1 -0
- package/index.js +412 -0
- package/locale/en.json +68 -0
- package/locale/zh.json +68 -0
- package/package.json +79 -4
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-way overrides: replace the cloud with this machine's data, or this machine
|
|
3
|
+
* with the cloud's data. No merge runs in either direction.
|
|
4
|
+
*
|
|
5
|
+
* The two commands still keep the coordination files working: after the copy the
|
|
6
|
+
* per-machine snapshot describes what this machine now holds, and both deletion
|
|
7
|
+
* ledgers are reset, so the next ordinary sync does not replay stale deletions.
|
|
8
|
+
*/
|
|
9
|
+
import { promises as fsp } from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { mergeWorkspaceDocuments } from './engine/workspace.js';
|
|
12
|
+
import { readLedger, writeJsonAtomic, writeLedger, SESSION_LEDGER, WORKSPACE_LEDGER } from './engine/ledger.js';
|
|
13
|
+
import { projectKey } from './engine/scan.js';
|
|
14
|
+
import { readSessionFile, rewriteCwd, writeSessionFile } from './engine/session.js';
|
|
15
|
+
import { pullMirror, pushMirror } from './mirror.js';
|
|
16
|
+
import { listRemoteTree, deleteRemoteFile } from './webdav.js';
|
|
17
|
+
|
|
18
|
+
/** Areas that always mirror between this machine and the sync folder. */
|
|
19
|
+
function areas(paths, profile) {
|
|
20
|
+
return [
|
|
21
|
+
{ from: path.join(paths.home, 'sessions'), to: 'sessions' },
|
|
22
|
+
{ from: path.join(paths.home, 'storages', 'session_projcache'), to: 'config/storages/session_projcache' },
|
|
23
|
+
{ from: path.join(paths.home, 'storages', 'workspace.json'), to: 'config/storages/workspace.json', file: true },
|
|
24
|
+
{ from: path.join(paths.home, 'profiles', profile, 'cordis.patch.yml'), to: `config/profiles/${profile}/cordis.patch.yml`, file: true },
|
|
25
|
+
{ from: path.join(paths.home, 'skills'), to: 'config/skills' },
|
|
26
|
+
{ from: path.join(paths.home, 'AGENTS.md'), to: 'config/AGENTS.md', file: true },
|
|
27
|
+
{ from: path.join(paths.home, '.credentials.yaml'), to: 'config/.credentials.yaml', file: true },
|
|
28
|
+
{ from: path.join(paths.home, 'attachments', 'v1', 'objects'), to: 'attachments/v1/objects' },
|
|
29
|
+
{ from: path.join(paths.home, 'attachments', 'v1', 'file-objects'), to: 'attachments/v1/file-objects' },
|
|
30
|
+
{ from: path.join(paths.home, 'attachments', 'v1', 'files'), to: 'attachments/v1/files' },
|
|
31
|
+
{ from: paths.trashDir, to: 'trash' },
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Copy one file or directory over the other side, replacing what was there. */
|
|
36
|
+
async function replaceFrom(source, target) {
|
|
37
|
+
await fsp.rm(target, { recursive: true, force: true });
|
|
38
|
+
await fsp.mkdir(path.dirname(target), { recursive: true });
|
|
39
|
+
let stat;
|
|
40
|
+
try {
|
|
41
|
+
stat = await fsp.stat(source);
|
|
42
|
+
} catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (stat.isDirectory()) {
|
|
46
|
+
await fsp.cp(source, target, { recursive: true, force: true });
|
|
47
|
+
} else {
|
|
48
|
+
await fsp.copyFile(source, target);
|
|
49
|
+
await fsp.utimes(target, stat.atimeMs / 1000, stat.mtimeMs / 1000).catch(() => {});
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Every file the mirror holds, as relative paths. */
|
|
55
|
+
async function mirrorFiles(root) {
|
|
56
|
+
const out = new Set();
|
|
57
|
+
const walk = async (dir, prefix) => {
|
|
58
|
+
let entries = [];
|
|
59
|
+
try {
|
|
60
|
+
entries = await fsp.readdir(dir, { withFileTypes: true });
|
|
61
|
+
} catch {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
for (const entry of entries) {
|
|
65
|
+
const rel = prefix === '' ? entry.name : `${prefix}/${entry.name}`;
|
|
66
|
+
if (entry.isDirectory()) await walk(path.join(dir, entry.name), rel);
|
|
67
|
+
else out.add(rel);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
await walk(root, '');
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Delete remote files the mirror no longer holds, in paced batches.
|
|
76
|
+
*
|
|
77
|
+
* Pacing keeps a large removal under the provider's request-rate limit; a 503 is
|
|
78
|
+
* retried by the transport itself, and anything still failing is reported so the
|
|
79
|
+
* next run can finish the job.
|
|
80
|
+
*/
|
|
81
|
+
async function deleteRemoteExtras(settings, keep) {
|
|
82
|
+
const tree = await listRemoteTree(settings);
|
|
83
|
+
const files = tree.files instanceof Map ? [...tree.files.keys()] : [];
|
|
84
|
+
const stale = files.filter((rel) => !keep.has(rel)).sort((a, b) => b.length - a.length);
|
|
85
|
+
let deleted = 0;
|
|
86
|
+
let failed = 0;
|
|
87
|
+
for (let index = 0; index < stale.length; index += 1) {
|
|
88
|
+
const done = await deleteRemoteFile(settings, stale[index]).catch(() => false);
|
|
89
|
+
if (done) deleted += 1;
|
|
90
|
+
else failed += 1;
|
|
91
|
+
if ((index + 1) % 20 === 0) await new Promise((resolve) => setTimeout(resolve, 400));
|
|
92
|
+
}
|
|
93
|
+
return { deleted, failed, total: stale.length };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Write the per-machine snapshot and reset the ledgers for one side. */
|
|
97
|
+
async function resetState(paths, machineId, sessionsRoot, sharedStateDir) {
|
|
98
|
+
const sessions = {};
|
|
99
|
+
let dirs = [];
|
|
100
|
+
try {
|
|
101
|
+
dirs = await fsp.readdir(sessionsRoot, { withFileTypes: true });
|
|
102
|
+
} catch {
|
|
103
|
+
dirs = [];
|
|
104
|
+
}
|
|
105
|
+
for (const project of dirs) {
|
|
106
|
+
if (!project.isDirectory()) continue;
|
|
107
|
+
let ids = [];
|
|
108
|
+
try {
|
|
109
|
+
ids = await fsp.readdir(path.join(sessionsRoot, project.name), { withFileTypes: true });
|
|
110
|
+
} catch {
|
|
111
|
+
ids = [];
|
|
112
|
+
}
|
|
113
|
+
for (const session of ids) {
|
|
114
|
+
if (!session.isDirectory()) continue;
|
|
115
|
+
sessions[session.name] = project.name;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const keys = {};
|
|
119
|
+
for (const [id, project] of Object.entries(sessions)) {
|
|
120
|
+
try {
|
|
121
|
+
const dir = path.join(sessionsRoot, project, id);
|
|
122
|
+
const file = (await fsp.readdir(dir)).find((name) => name.startsWith('session.v'));
|
|
123
|
+
if (file === undefined) continue;
|
|
124
|
+
const stat = await fsp.stat(path.join(dir, file));
|
|
125
|
+
keys[id] = new Date(stat.mtimeMs).toISOString();
|
|
126
|
+
} catch {
|
|
127
|
+
// A session without a readable log simply stays out of the snapshot.
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
await writeJsonAtomic(path.join(sharedStateDir, `machine-${machineId}.json`), {
|
|
131
|
+
version: 1,
|
|
132
|
+
machine: machineId,
|
|
133
|
+
updatedAt: new Date().toISOString(),
|
|
134
|
+
keys,
|
|
135
|
+
});
|
|
136
|
+
await writeLedger(sharedStateDir, SESSION_LEDGER, { version: 1, deleted: {} });
|
|
137
|
+
await writeLedger(sharedStateDir, WORKSPACE_LEDGER, { version: 1, deleted: {} });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Local session paths to this machine when the target directory exists. */
|
|
141
|
+
async function localizeSessions(paths) {
|
|
142
|
+
let rewritten = 0;
|
|
143
|
+
let dirs = [];
|
|
144
|
+
try {
|
|
145
|
+
dirs = await fsp.readdir(path.join(paths.home, 'sessions'), { withFileTypes: true });
|
|
146
|
+
} catch {
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
for (const project of dirs) {
|
|
150
|
+
if (!project.isDirectory()) continue;
|
|
151
|
+
const projectDir = path.join(paths.home, 'sessions', project.name);
|
|
152
|
+
let ids = [];
|
|
153
|
+
try {
|
|
154
|
+
ids = await fsp.readdir(projectDir, { withFileTypes: true });
|
|
155
|
+
} catch {
|
|
156
|
+
ids = [];
|
|
157
|
+
}
|
|
158
|
+
for (const session of ids) {
|
|
159
|
+
if (!session.isDirectory()) continue;
|
|
160
|
+
const dir = path.join(projectDir, session.name);
|
|
161
|
+
const file = (await fsp.readdir(dir).catch(() => [])).find((name) => name.startsWith('session.v'));
|
|
162
|
+
if (file === undefined) continue;
|
|
163
|
+
const source = path.join(dir, file);
|
|
164
|
+
let parsed;
|
|
165
|
+
try {
|
|
166
|
+
parsed = await readSessionFile(source);
|
|
167
|
+
} catch {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const cwd = parsed.header.cwd;
|
|
171
|
+
if (typeof cwd !== 'string' || cwd === '') continue;
|
|
172
|
+
let exists = false;
|
|
173
|
+
try {
|
|
174
|
+
exists = (await fsp.stat(cwd)).isDirectory();
|
|
175
|
+
} catch {
|
|
176
|
+
exists = false;
|
|
177
|
+
}
|
|
178
|
+
if (!exists) continue;
|
|
179
|
+
const key = projectKey(cwd);
|
|
180
|
+
if (key === project.name) continue;
|
|
181
|
+
const targetDir = path.join(paths.home, 'sessions', key, session.name);
|
|
182
|
+
await fsp.mkdir(targetDir, { recursive: true });
|
|
183
|
+
const stat = await fsp.stat(source);
|
|
184
|
+
await writeSessionFile(path.join(targetDir, file), rewriteCwd(parsed, cwd).texts);
|
|
185
|
+
await fsp.utimes(path.join(targetDir, file), stat.mtimeMs / 1000, stat.mtimeMs / 1000).catch(() => {});
|
|
186
|
+
await fsp.rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
187
|
+
rewritten += 1;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return rewritten;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Make the cloud identical to this machine.
|
|
195
|
+
* @returns counters for the undo panel.
|
|
196
|
+
*/
|
|
197
|
+
export async function overwriteCloud(deps) {
|
|
198
|
+
const { paths, settings, machineId, mirrorDir, apply = true, transport = 'webdav' } = deps;
|
|
199
|
+
const profile = deps.profile ?? 'desktop';
|
|
200
|
+
const sharedStateDir = path.join(mirrorDir, 'config', 'sync-state');
|
|
201
|
+
const copied = [];
|
|
202
|
+
|
|
203
|
+
for (const area of areas(paths, profile)) {
|
|
204
|
+
const target = path.join(mirrorDir, ...area.to.split('/'));
|
|
205
|
+
if (apply) {
|
|
206
|
+
const done = await replaceFrom(area.from, target);
|
|
207
|
+
if (done) copied.push(area.to);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (apply) await resetState(paths, machineId, path.join(paths.home, 'sessions'), sharedStateDir);
|
|
211
|
+
|
|
212
|
+
let uploaded = 0;
|
|
213
|
+
let deleted = 0;
|
|
214
|
+
let failed = 0;
|
|
215
|
+
if (apply && transport === 'webdav') {
|
|
216
|
+
// Upload first: a failure here leaves the previous cloud content in place
|
|
217
|
+
// instead of an empty folder. The prune below only removes what the mirror
|
|
218
|
+
// does not hold.
|
|
219
|
+
const result = await pushMirror({ settings });
|
|
220
|
+
uploaded = result?.uploaded ?? 0;
|
|
221
|
+
const keep = await mirrorFiles(mirrorDir);
|
|
222
|
+
const pruned = await deleteRemoteExtras(settings, keep);
|
|
223
|
+
deleted = pruned.deleted;
|
|
224
|
+
failed = pruned.failed;
|
|
225
|
+
if (failed > 0) {
|
|
226
|
+
return {
|
|
227
|
+
ok: false,
|
|
228
|
+
direction: 'cloud',
|
|
229
|
+
copied: copied.length,
|
|
230
|
+
uploaded,
|
|
231
|
+
deleted,
|
|
232
|
+
failed,
|
|
233
|
+
warning: `有 ${String(failed)} 个多余文件未能删除,可能被云端限流;稍后再次执行“覆盖云端”即可补齐。`,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { ok: true, direction: 'cloud', copied: copied.length, uploaded, deleted };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Make this machine identical to the cloud.
|
|
242
|
+
* @returns counters for the undo panel.
|
|
243
|
+
*/
|
|
244
|
+
export async function overwriteLocal(deps) {
|
|
245
|
+
const { paths, settings, machineId, mirrorDir, apply = true, transport = 'webdav' } = deps;
|
|
246
|
+
const profile = deps.profile ?? 'desktop';
|
|
247
|
+
const sharedStateDir = path.join(mirrorDir, 'config', 'sync-state');
|
|
248
|
+
|
|
249
|
+
if (apply && transport === 'webdav') {
|
|
250
|
+
// The mirror is only a cache: rebuild it from the cloud before copying back.
|
|
251
|
+
await fsp.rm(mirrorDir, { recursive: true, force: true });
|
|
252
|
+
await fsp.mkdir(mirrorDir, { recursive: true });
|
|
253
|
+
await pullMirror({ settings });
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const copied = [];
|
|
257
|
+
for (const area of areas(paths, profile)) {
|
|
258
|
+
const source = path.join(mirrorDir, ...area.to.split('/'));
|
|
259
|
+
if (apply) {
|
|
260
|
+
const done = await replaceFrom(source, area.from);
|
|
261
|
+
if (done) copied.push(area.to);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const localized = apply ? await localizeSessions(paths) : 0;
|
|
265
|
+
if (apply) await resetState(paths, machineId, path.join(paths.home, 'sessions'), sharedStateDir);
|
|
266
|
+
|
|
267
|
+
return { ok: true, direction: 'local', copied: copied.length, localized };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Counts for a preview: how many files each area holds right now. */
|
|
271
|
+
export async function inspectOverride(deps) {
|
|
272
|
+
const { paths, mirrorDir } = deps;
|
|
273
|
+
const profile = deps.profile ?? 'desktop';
|
|
274
|
+
const rows = [];
|
|
275
|
+
for (const area of areas(paths, profile)) {
|
|
276
|
+
const localCount = await countFiles(area.from);
|
|
277
|
+
const sharedCount = await countFiles(path.join(mirrorDir, ...area.to.split('/')));
|
|
278
|
+
rows.push({ area: area.to, local: localCount, mirror: sharedCount });
|
|
279
|
+
}
|
|
280
|
+
return { areas: rows };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function countFiles(target) {
|
|
284
|
+
try {
|
|
285
|
+
const stat = await fsp.stat(target);
|
|
286
|
+
if (!stat.isDirectory()) return 1;
|
|
287
|
+
let count = 0;
|
|
288
|
+
const walk = async (dir) => {
|
|
289
|
+
let entries = [];
|
|
290
|
+
try {
|
|
291
|
+
entries = await fsp.readdir(dir, { withFileTypes: true });
|
|
292
|
+
} catch {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
for (const entry of entries) {
|
|
296
|
+
if (entry.isDirectory()) await walk(path.join(dir, entry.name));
|
|
297
|
+
else count += 1;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
await walk(target);
|
|
301
|
+
return count;
|
|
302
|
+
} catch {
|
|
303
|
+
return 0;
|
|
304
|
+
}
|
|
305
|
+
}
|
package/host/restart.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restart the desktop application from outside itself, pruning stale auth cookies.
|
|
3
|
+
*
|
|
4
|
+
* The Host is a child of the desktop shell, so a launcher spawned from here belongs
|
|
5
|
+
* to that process tree and would die with the app. The helpers are written next to
|
|
6
|
+
* the plugin data and started by Explorer instead.
|
|
7
|
+
*
|
|
8
|
+
* Each start opens a random local port and stores a `dsh-auth-<hash>` cookie for
|
|
9
|
+
* 127.0.0.1; cookies are not port-scoped, so they pile up until the request header
|
|
10
|
+
* exceeds the server limit (HTTP 431) and the app cannot finish its handshake. The
|
|
11
|
+
* helper removes those rows while the cookie database is unlocked.
|
|
12
|
+
*/
|
|
13
|
+
import { promises as fsp } from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { spawn } from 'node:child_process';
|
|
16
|
+
|
|
17
|
+
const PS1 = 'restart-dsh.ps1';
|
|
18
|
+
const VBS = 'restart-dsh.vbs';
|
|
19
|
+
const CLEAN = 'clean-dsh-cookies.mjs';
|
|
20
|
+
|
|
21
|
+
/** Removes only the auth rows; prints how many were deleted. */
|
|
22
|
+
export function cleanScript() {
|
|
23
|
+
return `import { DatabaseSync } from 'node:sqlite';
|
|
24
|
+
import { copyFileSync, existsSync } from 'node:fs';
|
|
25
|
+
|
|
26
|
+
const file = process.argv[2];
|
|
27
|
+
if (file === undefined || !existsSync(file)) {
|
|
28
|
+
console.log('cookies: missing');
|
|
29
|
+
process.exit(0);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
copyFileSync(file, \`\${file}.bak\`);
|
|
33
|
+
} catch {
|
|
34
|
+
// A failed backup must not block the cleanup.
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const db = new DatabaseSync(file);
|
|
38
|
+
const info = db.prepare("delete from cookies where name like 'dsh-auth-%'").run();
|
|
39
|
+
db.close();
|
|
40
|
+
console.log(\`cookies: removed \${String(info.changes)}\`);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.log(\`cookies: failed \${String(error?.message ?? error)}\`);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The restart helper; falls back to deleting the whole store when Node is absent. */
|
|
49
|
+
export function ps1Body(exe, cleanPath) {
|
|
50
|
+
const quotedExe = exe.replace(/'/gu, "''");
|
|
51
|
+
const quotedClean = cleanPath.replace(/'/gu, "''");
|
|
52
|
+
return `param([string]$ExePath = '${quotedExe}')
|
|
53
|
+
Start-Sleep -Milliseconds 300
|
|
54
|
+
& taskkill.exe /IM 'DeepSeek Harness.exe' /T /F | Out-Null
|
|
55
|
+
Start-Sleep -Milliseconds 500
|
|
56
|
+
|
|
57
|
+
$cookies = Join-Path $env:APPDATA '@deepseek-ai\\dsh-desktop\\Network\\Cookies'
|
|
58
|
+
$stamp = Join-Path (Join-Path $env:LOCALAPPDATA 'dsh-enhanced') 'cookie-clean.week'
|
|
59
|
+
$now = Get-Date
|
|
60
|
+
$week = '{0}-W{1:D2}' -f $now.Year, ([math]::Floor(($now.DayOfYear - 1) / 7) + 1)
|
|
61
|
+
$lastClean = ''
|
|
62
|
+
if (Test-Path -LiteralPath $stamp) {
|
|
63
|
+
try { $lastClean = (Get-Content -LiteralPath $stamp -Raw).Trim() } catch { $lastClean = '' }
|
|
64
|
+
}
|
|
65
|
+
$node = Get-ChildItem (Join-Path $env:USERPROFILE '.dsh\\dsh-runtimes') -Recurse -Filter node.exe -ErrorAction SilentlyContinue | Sort-Object FullName -Descending | Select-Object -First 1
|
|
66
|
+
$cleaned = $false
|
|
67
|
+
if ($lastClean -eq $week) {
|
|
68
|
+
$cleaned = $true
|
|
69
|
+
} elseif ($null -ne $node -and (Test-Path -LiteralPath $cookies)) {
|
|
70
|
+
try {
|
|
71
|
+
& $node.FullName '${quotedClean}' $cookies | Out-Null
|
|
72
|
+
$cleaned = $LASTEXITCODE -eq 0
|
|
73
|
+
if ($cleaned) {
|
|
74
|
+
try { Set-Content -LiteralPath $stamp -Value $week -Encoding ascii } catch { }
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
$cleaned = $false
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (-not $cleaned -and (Test-Path -LiteralPath $cookies)) {
|
|
81
|
+
try {
|
|
82
|
+
Copy-Item -LiteralPath $cookies -Destination ($cookies + '.bak') -Force
|
|
83
|
+
Remove-Item -LiteralPath $cookies, ($cookies + '-journal') -Force -ErrorAction SilentlyContinue
|
|
84
|
+
try { Set-Content -LiteralPath $stamp -Value $week -Encoding ascii } catch { }
|
|
85
|
+
} catch {
|
|
86
|
+
# A locked store simply keeps its old cookies.
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
$pendingRoot = Join-Path (Join-Path $env:USERPROFILE '.dsh\\dsh-enhanced') 'pending\\profiles'
|
|
91
|
+
if (Test-Path -LiteralPath $pendingRoot) {
|
|
92
|
+
Get-ChildItem -LiteralPath $pendingRoot -Directory -ErrorAction SilentlyContinue | ForEach-Object {
|
|
93
|
+
$src = Join-Path $_.FullName 'cordis.patch.yml'
|
|
94
|
+
if (Test-Path -LiteralPath $src) {
|
|
95
|
+
$dst = Join-Path (Join-Path (Join-Path $env:USERPROFILE '.dsh\\profiles') $_.Name) 'cordis.patch.yml'
|
|
96
|
+
try {
|
|
97
|
+
if (Test-Path -LiteralPath $dst) { Copy-Item -LiteralPath $dst -Destination ($dst + '.bak') -Force }
|
|
98
|
+
Copy-Item -LiteralPath $src -Destination $dst -Force
|
|
99
|
+
Remove-Item -LiteralPath $src -Force
|
|
100
|
+
} catch {
|
|
101
|
+
# A failed patch swap leaves the old file in place.
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
Start-Process -FilePath explorer.exe -ArgumentList $ExePath
|
|
108
|
+
`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A silent launcher; Explorer runs this one, so it is not in the app tree. */
|
|
112
|
+
function vbsBody(ps1Path) {
|
|
113
|
+
const target = ps1Path.replace(/"/gu, '""');
|
|
114
|
+
return `Set shell = CreateObject("WScript.Shell")
|
|
115
|
+
shell.Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File ""${target}""", 0, False
|
|
116
|
+
`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Best-effort diagnostic line. */
|
|
120
|
+
async function trace(paths, line) {
|
|
121
|
+
try {
|
|
122
|
+
await fsp.appendFile(path.join(paths.base, 'restart.log'), `${new Date().toISOString()} ${line}\n`, 'utf8');
|
|
123
|
+
} catch {
|
|
124
|
+
// Diagnostics never block a restart.
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Schedule one restart and return immediately.
|
|
130
|
+
* @param paths - plugin data paths (`base` receives the helpers and the log).
|
|
131
|
+
* @returns `{ ok: true }` once Explorer accepted the launch.
|
|
132
|
+
*/
|
|
133
|
+
export async function restartApplication(paths) {
|
|
134
|
+
const exe = process.execPath;
|
|
135
|
+
if (!/(^|[\\/])DeepSeek Harness\.exe$/iu.test(exe)) {
|
|
136
|
+
await trace(paths, `refused: execPath is not the desktop app (${exe})`);
|
|
137
|
+
throw Object.assign(new Error(`重启已拒绝:当前进程不是桌面应用(${exe})`), { code: 'restart-path' });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const scriptPath = path.join(paths.base, PS1);
|
|
141
|
+
const cleanPath = path.join(paths.base, CLEAN);
|
|
142
|
+
const launcherPath = path.join(paths.base, VBS);
|
|
143
|
+
|
|
144
|
+
await fsp.mkdir(paths.base, { recursive: true }).catch(() => {});
|
|
145
|
+
await fsp.writeFile(cleanPath, cleanScript(), 'utf8');
|
|
146
|
+
await fsp.writeFile(scriptPath, `\ufeff${ps1Body(exe, cleanPath)}`, 'utf8');
|
|
147
|
+
await fsp.writeFile(launcherPath, Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(vbsBody(scriptPath), 'utf16le')]));
|
|
148
|
+
await trace(paths, `scheduling restart; exe=${exe}; launcher=${launcherPath}`);
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
spawn('explorer.exe', [launcherPath], { detached: true, stdio: 'ignore', windowsHide: true }).unref();
|
|
152
|
+
await trace(paths, 'launcher handed to explorer');
|
|
153
|
+
return { ok: true };
|
|
154
|
+
} catch (error) {
|
|
155
|
+
await trace(paths, `launch failed: ${String(error?.message ?? error)}`);
|
|
156
|
+
throw Object.assign(new Error(`无法启动重启脚本:${String(error?.message ?? error)}`), { code: 'restart-spawn' });
|
|
157
|
+
}
|
|
158
|
+
}
|