dsh-snowbreak-heimdall 3.5.2 → 3.7.2

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/lib/index.js CHANGED
@@ -1,462 +1,474 @@
1
- /**
2
- * Snowbreak: Heimdall Terminal — source-layer companion.
3
- *
4
- * The plugin does NOT paint the loading screen and does NOT own the palette.
5
- * It stages a self-contained copy of the source patcher inside the DSH
6
- * installation and leaves one disposable helper that reconciles the asar and
7
- * icons after every DSH process has exited. No Startup entry, scheduled task,
8
- * registry value, service, HTTP listener, or auto-relaunch is used.
9
- */
10
- import fs from 'node:fs';
11
- import os from 'node:os';
12
- import path from 'node:path';
13
- import { createRequire } from 'node:module';
14
- import { spawn } from 'node:child_process';
15
- import { fileURLToPath } from 'node:url';
16
-
17
- const requireFromHere = createRequire(import.meta.url);
18
-
19
- const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
20
- const VERSION = '3.5.2';
21
- let restartPrompted = false;
22
- const INSTALL = path.join(process.env.LOCALAPPDATA || '', 'Programs', 'DeepSeek Harness');
23
- const DEFAULT_RESOURCES = process.env.HEIMDALL_RESOURCES
24
- || (typeof process.resourcesPath === 'string' && /[\\/]resources$/i.test(process.resourcesPath)
25
- ? process.resourcesPath
26
- : path.join(INSTALL, 'resources'));
27
- const TARGET = process.env.HEIMDALL_TARGET || path.join(DEFAULT_RESOURCES, 'app.asar');
28
- const RESOURCES = path.dirname(TARGET);
29
- const STATE_DIR = path.join(RESOURCES, 'HeimdallTerminal');
30
- const RUNTIME = path.join(STATE_DIR, 'runtime');
31
- const LOG = path.join(STATE_DIR, 'host.log');
32
- const PROFILE_DIR = process.env.DSH_PROFILE_DIR
33
- || path.join(process.env.DSH_HOME || path.join(os.homedir(), '.dsh'), 'profiles', 'desktop');
34
- const WSCRIPT = path.join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'wscript.exe');
35
-
36
- const EXCLUDED_PY = new Set([
37
- 'site-packages', '__pycache__', 'test', 'tests', 'idlelib', 'tkinter', 'turtledemo',
38
- 'ensurepip', 'lib2to3', 'pydoc_data', 'venv', 'distutils', 'tcl', 'Scripts', 'include', 'libs',
39
- ]);
40
-
41
- function log(message) {
42
- try {
43
- fs.mkdirSync(STATE_DIR, { recursive: true });
44
- fs.appendFileSync(LOG, '[' + new Date().toISOString() + '] ' + message + '\n', 'utf8');
45
- } catch (_) { /* diagnostics only */ }
46
- }
47
-
48
- function readState() {
49
- try { return JSON.parse(fs.readFileSync(path.join(STATE_DIR, 'state.json'), 'utf8')); }
50
- catch (_) { return {}; }
51
- }
52
-
53
- function writeState(patch) {
54
- try {
55
- fs.mkdirSync(STATE_DIR, { recursive: true });
56
- const file = path.join(STATE_DIR, 'state.json');
57
- const next = { ...readState(), ...patch, hostUpdatedAt: new Date().toISOString() };
58
- const tmp = file + '.tmp';
59
- fs.writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n', 'utf8');
60
- fs.renameSync(tmp, file);
61
- } catch (error) {
62
- log('state write failed: ' + error.message);
63
- }
64
- }
65
-
66
- function sleep(ms) {
67
- try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); }
68
- catch (_) { /* best effort */ }
69
- }
70
-
71
- function copyFileRetry(source, destination) {
72
- for (let attempt = 0; attempt < 6; attempt += 1) {
73
- try {
74
- fs.copyFileSync(source, destination);
75
- return;
76
- } catch (error) {
77
- if (attempt === 5) throw error;
78
- sleep(120);
79
- }
80
- }
81
- }
82
-
83
- function copyTree(source, destination, filter) {
84
- if (!fs.existsSync(source)) return;
85
- fs.mkdirSync(destination, { recursive: true });
86
- for (const entry of fs.readdirSync(source, { withFileTypes: true })) {
87
- const src = path.join(source, entry.name);
88
- if (filter && !filter(src)) continue;
89
- const dst = path.join(destination, entry.name);
90
- if (entry.isDirectory()) copyTree(src, dst, filter);
91
- else copyFileRetry(src, dst);
92
- }
93
- }
94
-
95
- function findPythonSource() {
96
- const home = process.env.DSH_HOME || path.join(os.homedir(), '.dsh');
97
- return path.join(home, 'dsh-runtimes', 'dsh-primary-runtime', 'dependencies', 'python');
98
- }
99
-
100
- function ensurePython() {
101
- const dest = path.join(RUNTIME, 'python');
102
- const pythonw = path.join(dest, 'pythonw.exe');
103
- if (fs.existsSync(pythonw)) return pythonw;
104
- const source = findPythonSource();
105
- if (!fs.existsSync(path.join(source, 'pythonw.exe'))) {
106
- log('bundled python runtime unavailable');
107
- return null;
108
- }
109
- fs.mkdirSync(dest, { recursive: true });
110
- for (const name of fs.readdirSync(source)) {
111
- if (!/^(python|pythonw|python3|python312|vcruntime.*)\.(exe|dll)$/i.test(name)) continue;
112
- fs.copyFileSync(path.join(source, name), path.join(dest, name));
113
- }
114
- copyTree(path.join(source, 'Lib'), path.join(dest, 'Lib'), (entry) => {
115
- const base = path.basename(entry);
116
- if (EXCLUDED_PY.has(base)) return false;
117
- if (base.endsWith('.pyc')) return false;
118
- return true;
119
- });
120
- copyTree(path.join(source, 'DLLs'), path.join(dest, 'DLLs'), (entry) => {
121
- return !entry.endsWith('.pdb');
122
- });
123
- fs.copyFileSync(path.join(source, 'LICENSE.txt'), path.join(dest, 'PYTHON-LICENSE.txt'));
124
- log('staged bundled python runtime');
125
- return pythonw;
126
- }
127
-
128
- function stageRuntime() {
129
- fs.mkdirSync(path.join(RUNTIME, 'patch'), { recursive: true });
130
- fs.mkdirSync(path.join(RUNTIME, 'assets'), { recursive: true });
131
- copyTree(path.join(ROOT, 'patch'), path.join(RUNTIME, 'patch'), (entry) => {
132
- return !entry.endsWith('.pyc') && !entry.includes('__pycache__');
133
- });
134
- const skip = new Set(['logo.webp', 'mark-color.png', 'client-template.js', 'hm-mark-ink.png']);
135
- copyTree(path.join(ROOT, 'assets', 'source'), path.join(RUNTIME, 'assets', 'source'), (entry) => {
136
- return !skip.has(path.basename(entry));
137
- });
138
- copyTree(path.join(ROOT, 'assets', 'icons'), path.join(RUNTIME, 'assets', 'icons'));
139
- copyTree(path.join(ROOT, 'vendor', 'rcedit'), path.join(RUNTIME, 'vendor', 'rcedit'));
140
- return ensurePython();
141
- }
142
-
143
- function vbsQuote(value) {
144
- return '"' + String(value).replace(/"/g, '""') + '"';
145
- }
146
-
147
- function writeRecovery(pythonw) {
148
- try {
149
- const patcher = path.join(RUNTIME, 'patch', 'patcher.py');
150
- for (const [mode, name] of [['apply', 'apply.vbs'], ['restore', 'restore.vbs']]) {
151
- const command = [
152
- vbsQuote(pythonw), vbsQuote(patcher),
153
- '--mode', mode,
154
- '--target', vbsQuote(TARGET),
155
- '--state-dir', vbsQuote(STATE_DIR),
156
- '--profile-dir', vbsQuote(PROFILE_DIR),
157
- ].join(' ');
158
- fs.writeFileSync(path.join(STATE_DIR, name), [
159
- 'Option Explicit',
160
- 'Dim sh',
161
- 'Set sh = CreateObject("WScript.Shell")',
162
- 'sh.Run ' + vbsQuote(command) + ', 0, False',
163
- '',
164
- ].join('\r\n'), 'utf8');
165
- }
166
- fs.writeFileSync(path.join(STATE_DIR, 'README.txt'), [
167
- '海姆达尔终端 · 源码层维护入口',
168
- '必须先完全退出 DeepSeek Harness。',
169
- ' apply.vbs 写入/修复皮肤',
170
- ' restore.vbs 还原官方 asar、图标与 EXE 图标',
171
- '备份与哈希在 backup\\,状态在 state.json,日志在 heimdall.log。',
172
- '全部位于 DSH 安装目录;没有启动项、计划任务、注册表写入或后台服务。',
173
- '',
174
- ].join('\r\n'), 'utf8');
175
- } catch (error) {
176
- log('recovery scripts failed: ' + error.message);
177
- }
178
- }
179
-
180
- function arm(autoRestart = false) {
181
- try {
182
- const pythonw = stageRuntime();
183
- if (!pythonw) throw new Error('python runtime not found');
184
- writeRecovery(pythonw);
185
- const patcher = path.join(RUNTIME, 'patch', 'patcher.py');
186
- const command = [
187
- vbsQuote(pythonw),
188
- vbsQuote(patcher),
189
- '--mode', 'sync',
190
- '--target', vbsQuote(TARGET),
191
- '--state-dir', vbsQuote(STATE_DIR),
192
- '--profile-dir', vbsQuote(PROFILE_DIR),
193
- '--wait-pid', String(process.pid),
194
- ];
195
- if (autoRestart) command.push('--auto-restart');
196
- const joined = command.join(' ');
197
- const syncVbs = path.join(STATE_DIR, 'sync.vbs');
198
- fs.writeFileSync(syncVbs, [
199
- 'Option Explicit',
200
- 'Dim sh',
201
- 'Set sh = CreateObject("WScript.Shell")',
202
- 'sh.Run ' + vbsQuote(joined) + ', 0, False',
203
- '',
204
- ].join('\r\n'), 'utf8');
205
-
206
- // Win32_Process.Create starts the helper outside Electron's job object, so
207
- // the helper can still reconcile the installation after DSH exits.
208
- const armVbs = path.join(STATE_DIR, 'wmi-arm.vbs');
209
- const wmiCommand = 'Chr(34) & ' + vbsQuote(WSCRIPT) + ' & Chr(34) & " //B "'
210
- + ' & Chr(34) & ' + vbsQuote(syncVbs) + ' & Chr(34)';
211
- fs.writeFileSync(armVbs, [
212
- 'Option Explicit',
213
- 'Dim wmi, proc, rc',
214
- 'Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2")',
215
- 'Set proc = wmi.Get("Win32_Process")',
216
- 'rc = proc.Create(' + wmiCommand + ', Null, Null)',
217
- 'If rc <> 0 Then WScript.Quit 1',
218
- '',
219
- ].join('\r\n'), 'utf8');
220
-
221
- const child = spawn(WSCRIPT, ['//B', armVbs], {
222
- detached: true, stdio: 'ignore', windowsHide: true,
223
- });
224
- child.unref();
225
- log('one-shot source helper armed');
226
- } catch (error) {
227
- log('arm failed: ' + error.message);
228
- }
229
- }
230
-
231
- function psQuote(value) {
232
- return "'" + String(value).replace(/'/g, "''") + "'";
233
- }
234
-
235
- function showRestartPrompt() {
236
- try {
237
- const state = readState();
238
- if (state.status === 'patched') return;
239
- const marker = path.join(STATE_DIR, 'restart-prompt.pid');
240
- if (fs.existsSync(marker) && fs.readFileSync(marker, 'utf8').trim() === String(process.pid)) return;
241
- fs.mkdirSync(STATE_DIR, { recursive: true });
242
- fs.writeFileSync(marker, String(process.pid), 'utf8');
243
- const text = [
244
- '安装完成。源码层尚未接入。',
245
- '',
246
- '平台:' + process.platform + ' ' + process.arch,
247
- 'DSH:' + (process.versions.electron || 'Electron'),
248
- '',
249
- '请完全退出 DSH(托盘退出,不是关闭窗口),再重新打开。',
250
- '退出后 helper 会写入加载画面、主题、菜单与图标。',
251
- ].join('\n');
252
- const script = "Add-Type -AssemblyName PresentationFramework; "
253
- + "[System.Windows.MessageBox]::Show(" + psQuote(text)
254
- + ", '海姆达尔终端 // 需要重启', 'OK', 'Warning') | Out-Null";
255
- const encoded = Buffer.from(script, 'utf16le').toString('base64');
256
- const powershell = path.join(
257
- process.env.SystemRoot || 'C:\\Windows',
258
- 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe',
259
- );
260
- const child = spawn(powershell, [
261
- '-NoProfile', '-STA', '-WindowStyle', 'Hidden', '-EncodedCommand', encoded,
262
- ], { detached: true, stdio: 'ignore', windowsHide: true });
263
- child.unref();
264
- log('restart prompt shown');
265
- } catch (error) {
266
- log('restart prompt failed: ' + error.message);
267
- }
268
- }
269
-
270
- function injectedRestartNoticeScript() {
271
- const platform = process.platform + ' ' + process.arch;
272
- return `(() => {
273
- if (document.getElementById('hm-restart-notice')) return;
274
- const css = [
275
- '#hm-restart-notice{position:fixed;right:20px;bottom:20px;z-index:2147483003;width:min(440px,calc(100vw - 40px));',
276
- 'border:1px solid #2C3A46;border-radius:2px;background:#0B1118;color:#E6EEF5;',
277
- 'box-shadow:0 0 0 1px rgba(0,0,0,.24),0 18px 50px rgba(0,0,0,.45);',
278
- 'font-family:"Noto Sans SC","Microsoft YaHei UI","Microsoft YaHei","Segoe UI",sans-serif;}',
279
- '#hm-restart-notice .hm-r-head{display:flex;align-items:center;justify-content:space-between;height:30px;padding:0 12px;',
280
- 'border-bottom:1px solid #263440;font:8px "Bahnschrift","Segoe UI",sans-serif;letter-spacing:.2em;color:#7E8E9E;}',
281
- '#hm-restart-notice .hm-r-body{padding:14px 16px 16px;}',
282
- '#hm-restart-notice .hm-r-kicker{font:8px "Bahnschrift","Segoe UI",sans-serif;letter-spacing:.22em;color:#7FB2D0;margin-bottom:7px;}',
283
- '#hm-restart-notice .hm-r-title{margin:0;font-size:17px;font-weight:600;line-height:1.45;color:#E6EEF5;}',
284
- '#hm-restart-notice .hm-r-text{margin:8px 0 0;font-size:12px;line-height:1.7;color:#AAB8C5;}',
285
- '#hm-restart-notice .hm-r-env{display:flex;flex-wrap:wrap;gap:4px 7px;margin-top:11px;padding:8px 10px;',
286
- 'border:1px solid #263440;background:#0F1720;font-size:10px;color:#7E8E9E;}',
287
- '#hm-restart-notice .hm-r-env b{color:#D97745;font-weight:500;}',
288
- '#hm-restart-notice .hm-r-actions{display:flex;justify-content:flex-end;margin-top:12px;}',
289
- '#hm-restart-notice button{height:30px;padding:0 14px;border:1px solid #D97745;background:#D97745;color:#0A0F14;',
290
- 'cursor:pointer;border-radius:1px;font:600 11px "Microsoft YaHei UI","Segoe UI",sans-serif;}'
291
- ].join('');
292
- const style = document.createElement('style');
293
- style.textContent = css;
294
- document.head.appendChild(style);
295
- const el = document.createElement('section');
296
- el.id = 'hm-restart-notice';
297
- el.setAttribute('role', 'status');
298
- el.innerHTML = '<div class="hm-r-head"><span>HEIMDALL // RESTART REQUIRED</span><span>SOURCE LAYER</span></div>'
299
- + '<div class="hm-r-body">'
300
- + '<div class="hm-r-kicker">环境检测:源码层尚未接入</div>'
301
- + '<h2 class="hm-r-title">安装完成,需要完全退出 DSH</h2>'
302
- + '<p class="hm-r-text">当前 DSH 仍在运行,皮肤还没有写入。请完全退出 DSH(托盘退出或退出程序,不是关闭窗口),下一次启动才会应用加载画面、主题、菜单和图标。</p>'
303
- + '<div class="hm-r-env"><span>平台:<b>${platform}</b></span><span>源码层:<b>未接入</b></span><span>操作:<b>完全退出 DSH</b></span></div>'
304
- + '<div class="hm-r-actions"><button type="button">我知道了</button></div>'
305
- + '</div>';
306
- document.body.appendChild(el);
307
- el.querySelector('button').addEventListener('click', () => el.remove());
308
- })()`;
309
- }
310
-
311
- function showInjectedRestartNotice() {
312
- try {
313
- const state = readState();
314
- if (state.status === 'patched') return;
315
- const electron = requireFromHere('electron');
316
- const windows = electron && electron.BrowserWindow ? electron.BrowserWindow.getAllWindows() : [];
317
- if (!windows.length) {
318
- log('no BrowserWindow for restart notice');
319
- return;
320
- }
321
- const script = injectedRestartNoticeScript();
322
- for (const win of windows) {
323
- win.webContents.executeJavaScript(script, true).catch((error) => {
324
- log('restart notice inject failed: ' + error.message);
325
- });
326
- }
327
- log('restart notice injected');
328
- } catch (error) {
329
- log('restart notice unavailable: ' + error.message);
330
- }
331
- }
332
-
333
- function writeRestartScript() {
334
- try {
335
- const statePath = path.join(STATE_DIR, 'state.json');
336
- const appExe = path.join(path.dirname(RESOURCES), 'DeepSeek Harness.exe');
337
- const script = [
338
- "$ErrorActionPreference = 'SilentlyContinue'",
339
- 'Start-Sleep -Seconds 3',
340
- "$name = 'DeepSeek Harness'",
341
- '$procs = Get-Process -Name $name',
342
- 'foreach ($p in $procs) { if ($p.MainWindowHandle -ne 0) { [void]$p.CloseMainWindow() } }',
343
- 'Start-Sleep -Seconds 6',
344
- '$procs = Get-Process -Name $name',
345
- 'if ($procs) { $procs | Stop-Process -Force }',
346
- '$deadline = (Get-Date).AddSeconds(180)',
347
- 'while ((Get-Date) -lt $deadline) {',
348
- ' Start-Sleep -Seconds 2',
349
- ' if (Test-Path -LiteralPath ' + psQuote(statePath) + ') {',
350
- ' try {',
351
- ' $s = Get-Content -LiteralPath ' + psQuote(statePath) + ' -Raw | ConvertFrom-Json',
352
- " if ($s.status -eq 'patched') { break }",
353
- ' } catch {}',
354
- ' }',
355
- '}',
356
- 'Start-Sleep -Seconds 2',
357
- 'if (-not (Get-Process -Name $name)) { Start-Process -FilePath ' + psQuote(appExe) + ' }',
358
- '',
359
- ].join('\r\n');
360
- const file = path.join(STATE_DIR, 'restart-dsh.ps1');
361
- fs.writeFileSync(file, '\ufeff' + script, 'utf8');
362
- return file;
363
- } catch (error) {
364
- log('auto restart script failed: ' + error.message);
365
- return null;
366
- }
367
- }
368
-
369
- function askRestartConfirmation(restartScript, mode = 'apply') {
370
- if (!restartScript) return;
371
- try {
372
- const templatePath = path.join(ROOT, 'assets', 'source', 'restart-confirm.ps1');
373
- let script = fs.readFileSync(templatePath, 'utf8').replace(/^\uFEFF/, '');
374
- const background = path.join(ROOT, 'assets', 'previews', 'loading-dark.png').replace(/\\/g, '/');
375
- script = script.replace('__BACKGROUND__', background);
376
- const mark = path.join(ROOT, 'assets', 'source', 'confirm-mark.png').replace(/\\/g, '/');
377
- script = script.replace('__MARK__', mark);
378
- const logo = path.join(ROOT, 'assets', 'source', 'hm-mark-light.png').replace(/\\/g, '/');
379
- script = script.replace('__LOGO__', logo);
380
- if (mode === 'disable') {
381
- script = script
382
- .replace('RESTART REQUIRED // 需要重启', 'DISABLE PENDING // 等待重启')
383
- .replace('安装完成,需要重启', '皮肤已关闭,重启后还原')
384
- .replace('源码层尚未接入 · 完全退出 DSH 后写入', '原始档案将在 DSH 完全退出后写回')
385
- .replace('点击「现在重启」后关闭 DSH,应用源码层并自动重新启动',
386
- '点击「立即重启」退出并还原;也可以稍后自己重启')
387
- .replace('现在重启', '立即重启');
388
- }
389
- const confirmPath = path.join(STATE_DIR, 'restart-confirm.ps1');
390
- fs.writeFileSync(confirmPath, '\ufeff' + script, 'utf8');
391
-
392
- const powershell = path.join(
393
- process.env.SystemRoot || 'C:\\Windows',
394
- 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe',
395
- );
396
- const pythonw = stageRuntime();
397
- if (!pythonw) throw new Error('python runtime not found');
398
- const launcher = path.join(STATE_DIR, 'launch-confirm.py');
399
- const lines = [
400
- 'import subprocess',
401
- 'subprocess.Popen([',
402
- ' r"' + powershell + '",',
403
- ' "-NoProfile", "-NonInteractive", "-STA",',
404
- ' "-ExecutionPolicy", "Bypass",',
405
- ' "-File", r"' + confirmPath + '",',
406
- ' "-RestartScript", r"' + restartScript + '",',
407
- '], creationflags=0x08000000)',
408
- '',
409
- ];
410
- fs.writeFileSync(launcher, lines.join('\n'), 'utf8');
411
- const child = spawn(pythonw, [launcher], {
412
- detached: true, stdio: 'ignore',
413
- });
414
- child.unref();
415
- log('restart confirmation shown');
416
- } catch (error) {
417
- log('restart confirmation failed: ' + error.message);
418
- }
419
- }
420
-
421
- export function apply(ctx) {
422
- try {
423
- fs.mkdirSync(STATE_DIR, { recursive: true });
424
- writeState({ desired: 'on', pluginVersion: VERSION });
425
- const state = readState();
426
- const isPatched = state.status === 'patched';
427
- if (!isPatched) {
428
- arm(true);
429
- askRestartConfirmation(writeRestartScript());
430
- } else {
431
- arm(false);
432
- }
433
- if (ctx && typeof ctx.effect === 'function') {
434
- const loadedAt = Date.now();
435
- ctx.effect(() => () => {
436
- try {
437
- const live = (() => {
438
- try {
439
- const gw = ctx.get('typertGateway');
440
- return gw && typeof gw.hasLiveClient === 'function' ? gw.hasLiveClient() : false;
441
- } catch (_) { return false; }
442
- })();
443
- const st = readState();
444
- const applied = st.status === 'patched';
445
- writeState({ desired: 'off', pluginVersion: VERSION });
446
- log('plugin disposing; applied=' + applied + ' live=' + live
447
- + ' after=' + Math.round((Date.now() - loadedAt) / 1000) + 's');
448
- arm(false);
449
- // Only surface the prompt when the app keeps running (a disable),
450
- // not when this disposer runs during shutdown.
451
- if (applied && live && Date.now() - loadedAt > 3000) {
452
- askRestartConfirmation(writeRestartScript(), 'disable');
453
- }
454
- } catch (error) {
455
- log('dispose failed: ' + error.message);
456
- }
457
- }, 'snowbreak-heimdall: source reconciliation');
458
- }
459
- } catch (error) {
460
- log('apply failed: ' + error.message);
461
- }
462
- }
1
+ /**
2
+ * Snowbreak: Heimdall Terminal — source-layer companion.
3
+ *
4
+ * The plugin does NOT paint the loading screen and does NOT own the palette.
5
+ * It stages a self-contained copy of the source patcher inside the DSH
6
+ * installation and leaves one disposable helper that reconciles the asar and
7
+ * icons after every DSH process has exited. No Startup entry, scheduled task,
8
+ * registry value, service, HTTP listener, or auto-relaunch is used.
9
+ */
10
+ import fs from 'node:fs';
11
+ import os from 'node:os';
12
+ import path from 'node:path';
13
+ import { createRequire } from 'node:module';
14
+ import { spawn } from 'node:child_process';
15
+ import { fileURLToPath } from 'node:url';
16
+
17
+ const requireFromHere = createRequire(import.meta.url);
18
+
19
+ const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
20
+ const VERSION = '3.7.2';
21
+ let restartPrompted = false;
22
+ const INSTALL = path.join(process.env.LOCALAPPDATA || '', 'Programs', 'DeepSeek Harness');
23
+ const DEFAULT_RESOURCES = process.env.HEIMDALL_RESOURCES
24
+ || (typeof process.resourcesPath === 'string' && /[\\/]resources$/i.test(process.resourcesPath)
25
+ ? process.resourcesPath
26
+ : path.join(INSTALL, 'resources'));
27
+ const TARGET = process.env.HEIMDALL_TARGET || path.join(DEFAULT_RESOURCES, 'app.asar');
28
+ const RESOURCES = path.dirname(TARGET);
29
+ const STATE_DIR = path.join(RESOURCES, 'HeimdallTerminal');
30
+ const RUNTIME = path.join(STATE_DIR, 'runtime');
31
+ const LOG = path.join(STATE_DIR, 'host.log');
32
+ const PROFILE_DIR = process.env.DSH_PROFILE_DIR
33
+ || path.join(process.env.DSH_HOME || path.join(os.homedir(), '.dsh'), 'profiles', 'desktop');
34
+ const WSCRIPT = path.join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'wscript.exe');
35
+
36
+ const EXCLUDED_PY = new Set([
37
+ 'site-packages', '__pycache__', 'test', 'tests', 'idlelib', 'tkinter', 'turtledemo',
38
+ 'ensurepip', 'lib2to3', 'pydoc_data', 'venv', 'distutils', 'tcl', 'Scripts', 'include', 'libs',
39
+ ]);
40
+
41
+ function log(message) {
42
+ try {
43
+ fs.mkdirSync(STATE_DIR, { recursive: true });
44
+ fs.appendFileSync(LOG, '[' + new Date().toISOString() + '] ' + message + '\n', 'utf8');
45
+ } catch (_) { /* diagnostics only */ }
46
+ }
47
+
48
+ function readState() {
49
+ try { return JSON.parse(fs.readFileSync(path.join(STATE_DIR, 'state.json'), 'utf8')); }
50
+ catch (_) { return {}; }
51
+ }
52
+
53
+ function writeState(patch) {
54
+ try {
55
+ fs.mkdirSync(STATE_DIR, { recursive: true });
56
+ const file = path.join(STATE_DIR, 'state.json');
57
+ const next = { ...readState(), ...patch, hostUpdatedAt: new Date().toISOString() };
58
+ const tmp = file + '.tmp';
59
+ fs.writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n', 'utf8');
60
+ fs.renameSync(tmp, file);
61
+ } catch (error) {
62
+ log('state write failed: ' + error.message);
63
+ }
64
+ }
65
+
66
+ function sleep(ms) {
67
+ try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); }
68
+ catch (_) { /* best effort */ }
69
+ }
70
+
71
+ function copyFileRetry(source, destination) {
72
+ for (let attempt = 0; attempt < 6; attempt += 1) {
73
+ try {
74
+ fs.copyFileSync(source, destination);
75
+ return;
76
+ } catch (error) {
77
+ if (attempt === 5) throw error;
78
+ sleep(120);
79
+ }
80
+ }
81
+ }
82
+
83
+ function copyTree(source, destination, filter) {
84
+ if (!fs.existsSync(source)) return;
85
+ fs.mkdirSync(destination, { recursive: true });
86
+ for (const entry of fs.readdirSync(source, { withFileTypes: true })) {
87
+ const src = path.join(source, entry.name);
88
+ if (filter && !filter(src)) continue;
89
+ const dst = path.join(destination, entry.name);
90
+ if (entry.isDirectory()) copyTree(src, dst, filter);
91
+ else copyFileRetry(src, dst);
92
+ }
93
+ }
94
+
95
+ function findPythonSource() {
96
+ const home = process.env.DSH_HOME || path.join(os.homedir(), '.dsh');
97
+ return path.join(home, 'dsh-runtimes', 'dsh-primary-runtime', 'dependencies', 'python');
98
+ }
99
+
100
+ function ensurePython() {
101
+ const dest = path.join(RUNTIME, 'python');
102
+ const pythonw = path.join(dest, 'pythonw.exe');
103
+ if (fs.existsSync(pythonw)) return pythonw;
104
+ const source = findPythonSource();
105
+ if (!fs.existsSync(path.join(source, 'pythonw.exe'))) {
106
+ log('bundled python runtime unavailable');
107
+ return null;
108
+ }
109
+ fs.mkdirSync(dest, { recursive: true });
110
+ for (const name of fs.readdirSync(source)) {
111
+ if (!/^(python|pythonw|python3|python312|vcruntime.*)\.(exe|dll)$/i.test(name)) continue;
112
+ fs.copyFileSync(path.join(source, name), path.join(dest, name));
113
+ }
114
+ copyTree(path.join(source, 'Lib'), path.join(dest, 'Lib'), (entry) => {
115
+ const base = path.basename(entry);
116
+ if (EXCLUDED_PY.has(base)) return false;
117
+ if (base.endsWith('.pyc')) return false;
118
+ return true;
119
+ });
120
+ copyTree(path.join(source, 'DLLs'), path.join(dest, 'DLLs'), (entry) => {
121
+ return !entry.endsWith('.pdb');
122
+ });
123
+ fs.copyFileSync(path.join(source, 'LICENSE.txt'), path.join(dest, 'PYTHON-LICENSE.txt'));
124
+ log('staged bundled python runtime');
125
+ return pythonw;
126
+ }
127
+
128
+ function stageRuntime() {
129
+ fs.mkdirSync(path.join(RUNTIME, 'patch'), { recursive: true });
130
+ fs.mkdirSync(path.join(RUNTIME, 'assets'), { recursive: true });
131
+ try { copyFileRetry(path.join(ROOT, 'package.json'), path.join(RUNTIME, 'package.json')); }
132
+ catch (_) { /* the layer version falls back to the CLI argument */ }
133
+ copyTree(path.join(ROOT, 'patch'), path.join(RUNTIME, 'patch'), (entry) => {
134
+ return !entry.endsWith('.pyc') && !entry.includes('__pycache__');
135
+ });
136
+ const skip = new Set(['logo.webp', 'mark-color.png', 'client-template.js', 'hm-mark-ink.png']);
137
+ copyTree(path.join(ROOT, 'assets', 'source'), path.join(RUNTIME, 'assets', 'source'), (entry) => {
138
+ return !skip.has(path.basename(entry));
139
+ });
140
+ copyTree(path.join(ROOT, 'assets', 'icons'), path.join(RUNTIME, 'assets', 'icons'));
141
+ copyTree(path.join(ROOT, 'vendor', 'rcedit'), path.join(RUNTIME, 'vendor', 'rcedit'));
142
+ return ensurePython();
143
+ }
144
+
145
+ function vbsQuote(value) {
146
+ return '"' + String(value).replace(/"/g, '""') + '"';
147
+ }
148
+
149
+ function writeRecovery(pythonw) {
150
+ try {
151
+ const patcher = path.join(RUNTIME, 'patch', 'patcher.py');
152
+ for (const [mode, name] of [['apply', 'apply.vbs'], ['restore', 'restore.vbs']]) {
153
+ const command = [
154
+ vbsQuote(pythonw), vbsQuote(patcher),
155
+ '--mode', mode,
156
+ '--target', vbsQuote(TARGET),
157
+ '--state-dir', vbsQuote(STATE_DIR),
158
+ '--profile-dir', vbsQuote(PROFILE_DIR),
159
+ ].join(' ');
160
+ fs.writeFileSync(path.join(STATE_DIR, name), [
161
+ 'Option Explicit',
162
+ 'Dim sh',
163
+ 'Set sh = CreateObject("WScript.Shell")',
164
+ 'sh.Run ' + vbsQuote(command) + ', 0, False',
165
+ '',
166
+ ].join('\r\n'), 'utf8');
167
+ }
168
+ fs.writeFileSync(path.join(STATE_DIR, 'README.txt'), [
169
+ '海姆达尔终端 · 源码层维护入口',
170
+ '必须先完全退出 DeepSeek Harness。',
171
+ ' apply.vbs 写入/修复皮肤',
172
+ ' restore.vbs 还原官方 asar、图标与 EXE 图标',
173
+ '备份与哈希在 backup\\,状态在 state.json,日志在 heimdall.log。',
174
+ '全部位于 DSH 安装目录;没有启动项、计划任务、注册表写入或后台服务。',
175
+ '',
176
+ ].join('\r\n'), 'utf8');
177
+ } catch (error) {
178
+ log('recovery scripts failed: ' + error.message);
179
+ }
180
+ }
181
+
182
+ function arm(autoRestart = false) {
183
+ try {
184
+ const pythonw = stageRuntime();
185
+ if (!pythonw) throw new Error('python runtime not found');
186
+ writeRecovery(pythonw);
187
+ const patcher = path.join(RUNTIME, 'patch', 'patcher.py');
188
+ const command = [
189
+ vbsQuote(pythonw),
190
+ vbsQuote(patcher),
191
+ '--mode', 'sync',
192
+ '--target', vbsQuote(TARGET),
193
+ '--state-dir', vbsQuote(STATE_DIR),
194
+ '--profile-dir', vbsQuote(PROFILE_DIR),
195
+ '--wait-pid', String(process.pid),
196
+ '--layer-version', VERSION,
197
+ ];
198
+ if (autoRestart) command.push('--auto-restart');
199
+ const joined = command.join(' ');
200
+ const syncVbs = path.join(STATE_DIR, 'sync.vbs');
201
+ fs.writeFileSync(syncVbs, [
202
+ 'Option Explicit',
203
+ 'Dim sh',
204
+ 'Set sh = CreateObject("WScript.Shell")',
205
+ 'sh.Run ' + vbsQuote(joined) + ', 0, False',
206
+ '',
207
+ ].join('\r\n'), 'utf8');
208
+
209
+ // Win32_Process.Create starts the helper outside Electron's job object, so
210
+ // the helper can still reconcile the installation after DSH exits.
211
+ const armVbs = path.join(STATE_DIR, 'wmi-arm.vbs');
212
+ const wmiCommand = 'Chr(34) & ' + vbsQuote(WSCRIPT) + ' & Chr(34) & " //B "'
213
+ + ' & Chr(34) & ' + vbsQuote(syncVbs) + ' & Chr(34)';
214
+ fs.writeFileSync(armVbs, [
215
+ 'Option Explicit',
216
+ 'Dim wmi, proc, rc',
217
+ 'Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2")',
218
+ 'Set proc = wmi.Get("Win32_Process")',
219
+ 'rc = proc.Create(' + wmiCommand + ', Null, Null)',
220
+ 'If rc <> 0 Then WScript.Quit 1',
221
+ '',
222
+ ].join('\r\n'), 'utf8');
223
+
224
+ const child = spawn(WSCRIPT, ['//B', armVbs], {
225
+ detached: true, stdio: 'ignore', windowsHide: true,
226
+ });
227
+ child.unref();
228
+ log('one-shot source helper armed');
229
+ } catch (error) {
230
+ log('arm failed: ' + error.message);
231
+ }
232
+ }
233
+
234
+ function psQuote(value) {
235
+ return "'" + String(value).replace(/'/g, "''") + "'";
236
+ }
237
+
238
+ function showRestartPrompt() {
239
+ try {
240
+ const state = readState();
241
+ if (state.status === 'patched') return;
242
+ const marker = path.join(STATE_DIR, 'restart-prompt.pid');
243
+ if (fs.existsSync(marker) && fs.readFileSync(marker, 'utf8').trim() === String(process.pid)) return;
244
+ fs.mkdirSync(STATE_DIR, { recursive: true });
245
+ fs.writeFileSync(marker, String(process.pid), 'utf8');
246
+ const text = [
247
+ '安装完成。源码层尚未接入。',
248
+ '',
249
+ '平台:' + process.platform + ' ' + process.arch,
250
+ 'DSH:' + (process.versions.electron || 'Electron'),
251
+ '',
252
+ '请完全退出 DSH(托盘退出,不是关闭窗口),再重新打开。',
253
+ '退出后 helper 会写入加载画面、主题、菜单与图标。',
254
+ ].join('\n');
255
+ const script = "Add-Type -AssemblyName PresentationFramework; "
256
+ + "[System.Windows.MessageBox]::Show(" + psQuote(text)
257
+ + ", '海姆达尔终端 // 需要重启', 'OK', 'Warning') | Out-Null";
258
+ const encoded = Buffer.from(script, 'utf16le').toString('base64');
259
+ const powershell = path.join(
260
+ process.env.SystemRoot || 'C:\\Windows',
261
+ 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe',
262
+ );
263
+ const child = spawn(powershell, [
264
+ '-NoProfile', '-STA', '-WindowStyle', 'Hidden', '-EncodedCommand', encoded,
265
+ ], { detached: true, stdio: 'ignore', windowsHide: true });
266
+ child.unref();
267
+ log('restart prompt shown');
268
+ } catch (error) {
269
+ log('restart prompt failed: ' + error.message);
270
+ }
271
+ }
272
+
273
+ function injectedRestartNoticeScript() {
274
+ const platform = process.platform + ' ' + process.arch;
275
+ return `(() => {
276
+ if (document.getElementById('hm-restart-notice')) return;
277
+ const css = [
278
+ '#hm-restart-notice{position:fixed;right:20px;bottom:20px;z-index:2147483003;width:min(440px,calc(100vw - 40px));',
279
+ 'border:1px solid #2C3A46;border-radius:2px;background:#0B1118;color:#E6EEF5;',
280
+ 'box-shadow:0 0 0 1px rgba(0,0,0,.24),0 18px 50px rgba(0,0,0,.45);',
281
+ 'font-family:"Noto Sans SC","Microsoft YaHei UI","Microsoft YaHei","Segoe UI",sans-serif;}',
282
+ '#hm-restart-notice .hm-r-head{display:flex;align-items:center;justify-content:space-between;height:30px;padding:0 12px;',
283
+ 'border-bottom:1px solid #263440;font:8px "Bahnschrift","Segoe UI",sans-serif;letter-spacing:.2em;color:#7E8E9E;}',
284
+ '#hm-restart-notice .hm-r-body{padding:14px 16px 16px;}',
285
+ '#hm-restart-notice .hm-r-kicker{font:8px "Bahnschrift","Segoe UI",sans-serif;letter-spacing:.22em;color:#7FB2D0;margin-bottom:7px;}',
286
+ '#hm-restart-notice .hm-r-title{margin:0;font-size:17px;font-weight:600;line-height:1.45;color:#E6EEF5;}',
287
+ '#hm-restart-notice .hm-r-text{margin:8px 0 0;font-size:12px;line-height:1.7;color:#AAB8C5;}',
288
+ '#hm-restart-notice .hm-r-env{display:flex;flex-wrap:wrap;gap:4px 7px;margin-top:11px;padding:8px 10px;',
289
+ 'border:1px solid #263440;background:#0F1720;font-size:10px;color:#7E8E9E;}',
290
+ '#hm-restart-notice .hm-r-env b{color:#D97745;font-weight:500;}',
291
+ '#hm-restart-notice .hm-r-actions{display:flex;justify-content:flex-end;margin-top:12px;}',
292
+ '#hm-restart-notice button{height:30px;padding:0 14px;border:1px solid #D97745;background:#D97745;color:#0A0F14;',
293
+ 'cursor:pointer;border-radius:1px;font:600 11px "Microsoft YaHei UI","Segoe UI",sans-serif;}'
294
+ ].join('');
295
+ const style = document.createElement('style');
296
+ style.textContent = css;
297
+ document.head.appendChild(style);
298
+ const el = document.createElement('section');
299
+ el.id = 'hm-restart-notice';
300
+ el.setAttribute('role', 'status');
301
+ el.innerHTML = '<div class="hm-r-head"><span>HEIMDALL // RESTART REQUIRED</span><span>SOURCE LAYER</span></div>'
302
+ + '<div class="hm-r-body">'
303
+ + '<div class="hm-r-kicker">环境检测:源码层尚未接入</div>'
304
+ + '<h2 class="hm-r-title">安装完成,需要完全退出 DSH</h2>'
305
+ + '<p class="hm-r-text">当前 DSH 仍在运行,皮肤还没有写入。请完全退出 DSH(托盘退出或退出程序,不是关闭窗口),下一次启动才会应用加载画面、主题、菜单和图标。</p>'
306
+ + '<div class="hm-r-env"><span>平台:<b>${platform}</b></span><span>源码层:<b>未接入</b></span><span>操作:<b>完全退出 DSH</b></span></div>'
307
+ + '<div class="hm-r-actions"><button type="button">我知道了</button></div>'
308
+ + '</div>';
309
+ document.body.appendChild(el);
310
+ el.querySelector('button').addEventListener('click', () => el.remove());
311
+ })()`;
312
+ }
313
+
314
+ function showInjectedRestartNotice() {
315
+ try {
316
+ const state = readState();
317
+ if (state.status === 'patched') return;
318
+ const electron = requireFromHere('electron');
319
+ const windows = electron && electron.BrowserWindow ? electron.BrowserWindow.getAllWindows() : [];
320
+ if (!windows.length) {
321
+ log('no BrowserWindow for restart notice');
322
+ return;
323
+ }
324
+ const script = injectedRestartNoticeScript();
325
+ for (const win of windows) {
326
+ win.webContents.executeJavaScript(script, true).catch((error) => {
327
+ log('restart notice inject failed: ' + error.message);
328
+ });
329
+ }
330
+ log('restart notice injected');
331
+ } catch (error) {
332
+ log('restart notice unavailable: ' + error.message);
333
+ }
334
+ }
335
+
336
+ function writeRestartScript() {
337
+ try {
338
+ const statePath = path.join(STATE_DIR, 'state.json');
339
+ const appExe = path.join(path.dirname(RESOURCES), 'DeepSeek Harness.exe');
340
+ const script = [
341
+ "$ErrorActionPreference = 'SilentlyContinue'",
342
+ 'Start-Sleep -Seconds 3',
343
+ "$name = 'DeepSeek Harness'",
344
+ '$procs = Get-Process -Name $name',
345
+ 'foreach ($p in $procs) { if ($p.MainWindowHandle -ne 0) { [void]$p.CloseMainWindow() } }',
346
+ 'Start-Sleep -Seconds 6',
347
+ '$procs = Get-Process -Name $name',
348
+ 'if ($procs) { $procs | Stop-Process -Force }',
349
+ '$deadline = (Get-Date).AddSeconds(180)',
350
+ 'while ((Get-Date) -lt $deadline) {',
351
+ ' Start-Sleep -Seconds 2',
352
+ ' if (Test-Path -LiteralPath ' + psQuote(statePath) + ') {',
353
+ ' try {',
354
+ ' $s = Get-Content -LiteralPath ' + psQuote(statePath) + ' -Raw | ConvertFrom-Json',
355
+ " if ($s.status -eq 'patched') { break }",
356
+ ' } catch {}',
357
+ ' }',
358
+ '}',
359
+ 'Start-Sleep -Seconds 2',
360
+ 'if (-not (Get-Process -Name $name)) { Start-Process -FilePath ' + psQuote(appExe) + ' }',
361
+ '',
362
+ ].join('\r\n');
363
+ const file = path.join(STATE_DIR, 'restart-dsh.ps1');
364
+ fs.writeFileSync(file, '\ufeff' + script, 'utf8');
365
+ return file;
366
+ } catch (error) {
367
+ log('auto restart script failed: ' + error.message);
368
+ return null;
369
+ }
370
+ }
371
+
372
+ function askRestartConfirmation(restartScript, mode = 'apply') {
373
+ if (!restartScript) return;
374
+ try {
375
+ const templatePath = path.join(ROOT, 'assets', 'source', 'restart-confirm.ps1');
376
+ let script = fs.readFileSync(templatePath, 'utf8').replace(/^\uFEFF/, '');
377
+ const background = path.join(ROOT, 'assets', 'previews', 'loading-dark.png').replace(/\\/g, '/');
378
+ script = script.replace('__BACKGROUND__', background);
379
+ const mark = path.join(ROOT, 'assets', 'source', 'confirm-mark.png').replace(/\\/g, '/');
380
+ script = script.replace('__MARK__', mark);
381
+ const logo = path.join(ROOT, 'assets', 'source', 'hm-mark-light.png').replace(/\\/g, '/');
382
+ script = script.replace('__LOGO__', logo);
383
+ if (mode === 'disable') {
384
+ script = script
385
+ .replace('RESTART REQUIRED // 需要重启', 'DISABLE PENDING // 等待重启')
386
+ .replace('安装完成,需要重启', '皮肤已关闭,重启后还原')
387
+ .replace('源码层尚未接入 · 完全退出 DSH 后写入', '原始档案将在 DSH 完全退出后写回')
388
+ .replace('点击「现在重启」后关闭 DSH,应用源码层并自动重新启动',
389
+ '点击「立即重启」退出并还原;也可以稍后自己重启')
390
+ .replace('现在重启', '立即重启');
391
+ }
392
+ const confirmPath = path.join(STATE_DIR, 'restart-confirm.ps1');
393
+ fs.writeFileSync(confirmPath, '\ufeff' + script, 'utf8');
394
+
395
+ const powershell = path.join(
396
+ process.env.SystemRoot || 'C:\\Windows',
397
+ 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe',
398
+ );
399
+ const pythonw = stageRuntime();
400
+ if (!pythonw) throw new Error('python runtime not found');
401
+ const launcher = path.join(STATE_DIR, 'launch-confirm.py');
402
+ const lines = [
403
+ 'import subprocess',
404
+ 'subprocess.Popen([',
405
+ ' r"' + powershell + '",',
406
+ ' "-NoProfile", "-NonInteractive", "-STA",',
407
+ ' "-ExecutionPolicy", "Bypass",',
408
+ ' "-File", r"' + confirmPath + '",',
409
+ ' "-RestartScript", r"' + restartScript + '",',
410
+ '], creationflags=0x08000000)',
411
+ '',
412
+ ];
413
+ fs.writeFileSync(launcher, lines.join('\n'), 'utf8');
414
+ const child = spawn(pythonw, [launcher], {
415
+ detached: true, stdio: 'ignore',
416
+ });
417
+ child.unref();
418
+ log('restart confirmation shown');
419
+ } catch (error) {
420
+ log('restart confirmation failed: ' + error.message);
421
+ }
422
+ }
423
+
424
+ export function apply(ctx) {
425
+ try {
426
+ fs.mkdirSync(STATE_DIR, { recursive: true });
427
+ // Read before stamping this version, otherwise a stale skin looks current.
428
+ const state = readState();
429
+ const isPatched = state.status === 'patched';
430
+ // A skin upgrade keeps the archive patched but with stale inlined sources.
431
+ // Queue a restore-then-apply so one restart is enough after an update.
432
+ const stale = isPatched && String(state.pluginVersion || '') !== VERSION;
433
+ writeState({ desired: 'on', pluginVersion: VERSION });
434
+ if (!isPatched || stale) {
435
+ if (stale) {
436
+ writeState({ reapply: true });
437
+ log('skin source changed ' + String(state.pluginVersion || '?') + ' -> ' + VERSION
438
+ + '; reapply queued');
439
+ }
440
+ arm(true);
441
+ askRestartConfirmation(writeRestartScript());
442
+ } else {
443
+ arm(false);
444
+ }
445
+ if (ctx && typeof ctx.effect === 'function') {
446
+ const loadedAt = Date.now();
447
+ ctx.effect(() => () => {
448
+ try {
449
+ const live = (() => {
450
+ try {
451
+ const gw = ctx.get('typertGateway');
452
+ return gw && typeof gw.hasLiveClient === 'function' ? gw.hasLiveClient() : false;
453
+ } catch (_) { return false; }
454
+ })();
455
+ const st = readState();
456
+ const applied = st.status === 'patched';
457
+ writeState({ desired: 'off', pluginVersion: VERSION });
458
+ log('plugin disposing; applied=' + applied + ' live=' + live
459
+ + ' after=' + Math.round((Date.now() - loadedAt) / 1000) + 's');
460
+ arm(false);
461
+ // Only surface the prompt when the app keeps running (a disable),
462
+ // not when this disposer runs during shutdown.
463
+ if (applied && live && Date.now() - loadedAt > 3000) {
464
+ askRestartConfirmation(writeRestartScript(), 'disable');
465
+ }
466
+ } catch (error) {
467
+ log('dispose failed: ' + error.message);
468
+ }
469
+ }, 'snowbreak-heimdall: source reconciliation');
470
+ }
471
+ } catch (error) {
472
+ log('apply failed: ' + error.message);
473
+ }
474
+ }