mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 09:40:22 +08:00
fix(desktop): DSH_PROFILE 兜底 + console.warn 污染治理 + --updated 抢前台
- patch #82 main.js 启动顶部补 DSH_PROFILE=desktop 兜底 (双击 exe 从 explorer 继承 env 常缺 → host "cannot determine the boot profile" crash → dsh-purge 没 activate → inject 全断) - lib/desktop.js 全部 console.warn("[dsh-purge] ...") 换成 logInternal() 写内部 log (不污染 host stderr → main.js:3729 fail 回调不再误报假 crash) - lib/restart-desktop.js spawnEnv() 补 DSH_PROFILE + spawn argv 加 --updated (复用 Electron main.js:11256 raiseAfterUpdate 内部 moveTop+focus 强制前台,比外部 PS SetForegroundWindow 可靠) + PS 兜底保留做双保险
This commit is contained in:
1 parent
288c9dbefd
commit
f184ae222d
3 files changed
+138
-17
No files matched your search
+26
@@ -5328,6 +5328,32 @@ const NEW_TOOL_PATCHES = [
|
||||
],
|
||||
markers: ["[dsh-purge] restore missing overlay from bak"],
|
||||
},
|
||||
{
|
||||
id: 82,
|
||||
name: "DESKTOP_SHELL_PROFILE_FALLBACK",
|
||||
layer: "代码",
|
||||
layer_en: "Code",
|
||||
desc: "主进程兜底 DSH_PROFILE=desktop:双击 exe 从 explorer 继承 env 常缺 DSH_PROFILE → host 'cannot determine the boot profile' → dsh-purge 插件没 activate → inject 全断",
|
||||
file: "desktop-shell",
|
||||
surfaces: ["desktop"],
|
||||
optional: true,
|
||||
skipIfMarked: true,
|
||||
replacements: [
|
||||
{
|
||||
pattern:
|
||||
"import { REMOTE_STREAM_MUX_PATH, parseRemoteStreamServerMessage } from \"@deepseek-ai/dsh-api-gateway/stream-protocol\";\n" +
|
||||
"//#region ../../packages/util/home-paths/lib/index.js",
|
||||
replace:
|
||||
"import { REMOTE_STREAM_MUX_PATH, parseRemoteStreamServerMessage } from \"@deepseek-ai/dsh-api-gateway/stream-protocol\";\n" +
|
||||
"// [dsh-purge-82] 双击 exe 从 explorer 继承 env 时常缺 DSH_PROFILE\n" +
|
||||
"// → host 子进程 \"cannot determine the boot profile\" → cordis 不加载\n" +
|
||||
"// → dsh-purge 插件没 activate → __dshPurgeHungPrompt 空 → inject 全断。兜底默认 desktop。\n" +
|
||||
"if (typeof process !== \"undefined\" && process.env && !process.env.DSH_PROFILE) process.env.DSH_PROFILE = \"desktop\";\n" +
|
||||
"//#region ../../packages/util/home-paths/lib/index.js",
|
||||
},
|
||||
],
|
||||
markers: ["[dsh-purge-82] 双击 exe 从 explorer 继承 env 时常缺 DSH_PROFILE"],
|
||||
},
|
||||
];
|
||||
|
||||
export const ALL_PATCHES = [...PATCHES, ...CODE_PATCHES, ...ENGINE_PATCHES, ...NEW_TOOL_PATCHES];
|
||||
|
||||
+44
-15
@@ -15,6 +15,19 @@ function restartUniqSuffix() {
|
||||
// 无归档时用 node:fs。app.asar 还在时不能把映射出来的 resources/app 当成已经解开。
|
||||
const fs = hostFs;
|
||||
|
||||
// [dsh-purge-fix 2026-10-09] console.warn 写 host stderr → main.js:3729 host.exitPromise
|
||||
// fail 回调把最后一行 stderr 当 Error reason。helper restart 期间任何 warn 都可能
|
||||
// 被误判成 crash log。改走内部文件,不污染 stderr。
|
||||
const INTERNAL_LOG_PATH = path.join(os.tmpdir(), "dsh-purge-desktop.log");
|
||||
function logInternal(msg) {
|
||||
try {
|
||||
const line = `${new Date().toISOString()} [dsh-purge] ${String(msg)}\n`;
|
||||
fs.appendFileSync(INTERNAL_LOG_PATH, line, "utf8");
|
||||
} catch {
|
||||
/* 日志写不进去就吞,绝不走 stderr */
|
||||
}
|
||||
}
|
||||
|
||||
/** 启动一次:清掉旧版本留下的无 pid 后缀的 restart 脚本/日志(超过 24h)。 */
|
||||
export function cleanupLegacyRestartScripts() {
|
||||
const tmp = os.tmpdir();
|
||||
@@ -1064,18 +1077,18 @@ export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit
|
||||
// 没 runtime —— 短 timeout 给 PowerShell 启动完成的时间(实测 ~200-500ms),
|
||||
// 然后主动退出。PowerShell 会在这期间杀掉 Electron,即便 host 自己退出
|
||||
// Electron 也已经不在了,不会弹 dialog。
|
||||
try { console.warn("[dsh-purge] asar swap: no runtime handoff, relying on PowerShell to kill Electron first"); } catch {}
|
||||
logInternal("asar swap: no runtime handoff, relying on PowerShell to kill Electron first");
|
||||
setTimeout(() => { try { process.exit(0); } catch {} }, 1500);
|
||||
return;
|
||||
}
|
||||
try { console.warn("[dsh-purge] asar swap: handing off restart to desktopRuntime"); } catch {}
|
||||
logInternal("asar swap: handing off restart to desktopRuntime");
|
||||
try {
|
||||
// #64:requestRestart 可能"空返回"但副作用也许仍标记预期退出,双保险之一。
|
||||
Promise.resolve(runtime.requestRestart()).catch((err) => {
|
||||
try { console.warn("[dsh-purge] desktopRuntime.requestRestart rejected:", err?.message || err); } catch {}
|
||||
logInternal("desktopRuntime.requestRestart rejected: " + (err?.message || err));
|
||||
});
|
||||
} catch (err) {
|
||||
try { console.warn("[dsh-purge] desktopRuntime.requestRestart threw:", err?.message || err); } catch {}
|
||||
logInternal("desktopRuntime.requestRestart threw: " + (err?.message || err));
|
||||
}
|
||||
// 2000ms 兜底:给宿主 requestRestart 做完 + PowerShell 启动完成。
|
||||
// 即便两条路径都挂,PowerShell 也会在 ~500ms 内 kill Electron,不会弹 crash。
|
||||
@@ -1119,7 +1132,7 @@ export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit
|
||||
// 没挂 'error' listener,PowerShell 不在 PATH / 被 AV 拦就是 unhandled 'error' event,
|
||||
// 会被 Node 当 uncaughtException 直接把宿主崩掉。
|
||||
child.on("error", (err) => {
|
||||
try { console.warn("[dsh-purge] asar swap PowerShell spawn failed:", err?.message || err); } catch {}
|
||||
logInternal("asar swap PowerShell spawn failed: " + (err?.message || err));
|
||||
});
|
||||
child.unref();
|
||||
if (exit !== false) gracefulExit();
|
||||
@@ -1216,7 +1229,7 @@ export function scheduleRestart(request, ctx, options = {}) {
|
||||
if (runtime) {
|
||||
// 只走桌面整应用 relaunch,不要 exit Host(那会先把内嵌 web 单独拉起来)。
|
||||
Promise.resolve(runtime.requestRestart()).catch((err) => {
|
||||
console.warn("[dsh-purge] desktop requestRestart failed:", err);
|
||||
logInternal("desktop requestRestart failed: " + (err?.message || err));
|
||||
});
|
||||
return { restarting: true };
|
||||
}
|
||||
@@ -1444,15 +1457,31 @@ export function restartOfficialHarness(options = {}) {
|
||||
try { fs.unlinkSync(inflight); } catch { /* 没有残留标记 */ }
|
||||
}
|
||||
if (plan.kind === "powershell") {
|
||||
// [dsh-purge-fix 2026-10-09] asar 已被挪走(unpacked 模式常态)时,再 schedule
|
||||
// 一次 PowerShell kill+swap 没意义:fs.rename ENOENT,但 PowerShell 已经 kill
|
||||
// Electron 一次 → 用户看到启动闪屏 + 启动慢 (多拉一次 Electron)。此处 early-
|
||||
// return,让宿主自然启动走 unpacked /app/,不再多 kill 一次。
|
||||
// [dsh-purge-fix 2026-10-09] asar 已被挪走(unpacked 模式常态):
|
||||
// ASAR_SWAP_PS1 fs.rename asar 会 ENOENT, 返 restarting:false 时 UI 显示
|
||||
// "失败: not started"。改走 helper restart-desktop.js:它的 moveAsarAsideAsync
|
||||
// 本身就处理 asar=null(第 283 行 fs.existsSync 不存在直接 return true),
|
||||
// relaunchDesktop 走 execPath spawn,干净 kill+relaunch,无 asar swap。
|
||||
if (!asar || !asarArchiveIsFile(asar)) {
|
||||
console.warn("[dsh-purge] asar 已不存在(unpacked 模式),跳过 PowerShell swap restart");
|
||||
return { restarting: false, asarMissing: true };
|
||||
logInternal("asar 已不存在(unpacked 模式),改走 helper kill+relaunch");
|
||||
try {
|
||||
const helper = fileURLToPath(new URL("./restart-desktop.js", import.meta.url));
|
||||
spawnHelper(helper, {
|
||||
respawn: true,
|
||||
killApp: true,
|
||||
asar: null,
|
||||
execPath: exe,
|
||||
cwd: path.dirname(exe),
|
||||
waitPid: process.pid,
|
||||
}, { exit: options.exit !== false });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
logInternal("unpacked helper restart failed: " + message);
|
||||
return { restarting: false, error: message };
|
||||
}
|
||||
return { restarting: true, mode: "unpacked-helper", engine: "helper" };
|
||||
}
|
||||
console.warn("[dsh-purge] wscript 不能执行 .js,改用 PowerShell 挪开 app.asar");
|
||||
logInternal("wscript 不能执行 .js,改用 PowerShell 挪开 app.asar");
|
||||
try {
|
||||
scheduleAsarSwapRestart({
|
||||
exe,
|
||||
@@ -1463,7 +1492,7 @@ export function restartOfficialHarness(options = {}) {
|
||||
});
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.warn("[dsh-purge] PowerShell asar swap failed:", message);
|
||||
logInternal("PowerShell asar swap failed: " + message);
|
||||
return { restarting: false, error: message };
|
||||
}
|
||||
return { restarting: true, asarSwap: true, engine: "powershell" };
|
||||
@@ -1509,7 +1538,7 @@ export function scheduleCleanupRestart({ cleanup = [], ctx } = {}) {
|
||||
if (runtime) {
|
||||
spawnHelper(dest, { cleanup, helper: dest, respawn: false }, { exit: false });
|
||||
Promise.resolve(runtime.requestRestart()).catch((err) => {
|
||||
console.warn("[dsh-purge] desktop uninstall restart failed:", err);
|
||||
logInternal("desktop uninstall restart failed: " + (err?.message || err));
|
||||
});
|
||||
return { restarting: true };
|
||||
}
|
||||
|
||||
+68
-2
@@ -33,6 +33,11 @@ function spawnEnv() {
|
||||
for (const key of Object.keys(env)) {
|
||||
if (key.toUpperCase() === "ELECTRON_RUN_AS_NODE") delete env[key];
|
||||
}
|
||||
// [dsh-purge-fix 2026-10-09] 从 explorer 继承的 env 常缺 DSH_PROFILE
|
||||
// (WM_SETTINGCHANGE broadcast 不刷新存活很久的 explorer)。helper relaunch
|
||||
// 时如果不兜底,新 Electron → host 子进程继续缺,报 "cannot determine the
|
||||
// boot profile" host crash → dsh-purge 插件没 activate → inject 全断。
|
||||
if (!env.DSH_PROFILE) env.DSH_PROFILE = "desktop";
|
||||
return env;
|
||||
}
|
||||
|
||||
@@ -336,7 +341,15 @@ function relaunchDesktop() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
const child = spawn(info.execPath, Array.isArray(info.argv) ? info.argv : [], {
|
||||
// [dsh-purge-fix 2026-10-09] 复用 Electron 自带的 --updated argv 分支:
|
||||
// main.js:11256 process.argv.includes("--updated") → raiseAfterUpdate=true
|
||||
// → enterWorkspace 时 window.moveTop() + window.focus() 内部强制前台。
|
||||
// 比外部 PS SetForegroundWindow 可靠得多 (Win11 前台保护只挡外部进程)。
|
||||
const baseArgv = Array.isArray(info.argv) ? info.argv.slice() : [];
|
||||
if (process.platform === "win32" && !baseArgv.includes("--updated")) {
|
||||
baseArgv.push("--updated");
|
||||
}
|
||||
const child = spawn(info.execPath, baseArgv, {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
cwd: info.cwd || process.cwd(),
|
||||
@@ -346,7 +359,60 @@ function relaunchDesktop() {
|
||||
});
|
||||
child.on("error", () => process.exit(1));
|
||||
child.unref();
|
||||
log("relaunched via execPath");
|
||||
log("relaunched via execPath (argv=" + JSON.stringify(baseArgv) + ")");
|
||||
|
||||
// [dsh-purge-fix 2026-10-09] Windows: 新 Electron 从 detached helper spawn,
|
||||
// Windows 焦点规则让它跑在后台。helper 已经不是前台(老 Electron 被 kill 了),
|
||||
// 直接 SetForegroundWindow 会被 Windows 静默 reject。
|
||||
// 三重 trick 强推前台:
|
||||
// 1) keybd_event 发送虚拟 Alt key — 模拟用户输入,解除 foreground lock
|
||||
// 2) AttachThreadInput attach 当前前台线程的 input queue — 继承前台权限
|
||||
// 3) ShowWindow(SW_RESTORE) + BringWindowToTop + SetForegroundWindow
|
||||
if (process.platform === "win32") {
|
||||
const exeLiteral = info.execPath.replace(/'/g, "''");
|
||||
const psScript =
|
||||
"$ErrorActionPreference='SilentlyContinue';" +
|
||||
"$sig=@'\n" +
|
||||
"[DllImport(\"user32.dll\")] public static extern bool SetForegroundWindow(IntPtr h);\n" +
|
||||
"[DllImport(\"user32.dll\")] public static extern bool ShowWindow(IntPtr h,int c);\n" +
|
||||
"[DllImport(\"user32.dll\")] public static extern bool BringWindowToTop(IntPtr h);\n" +
|
||||
"[DllImport(\"user32.dll\")] public static extern IntPtr GetForegroundWindow();\n" +
|
||||
"[DllImport(\"user32.dll\")] public static extern uint GetWindowThreadProcessId(IntPtr h,out uint pid);\n" +
|
||||
"[DllImport(\"kernel32.dll\")] public static extern uint GetCurrentThreadId();\n" +
|
||||
"[DllImport(\"user32.dll\")] public static extern bool AttachThreadInput(uint a,uint b,bool f);\n" +
|
||||
"[DllImport(\"user32.dll\")] public static extern void keybd_event(byte k,byte s,uint f,int e);\n" +
|
||||
"'@;" +
|
||||
"$t=Add-Type -MemberDefinition $sig -Name W -Namespace U -PassThru;" +
|
||||
"[void]$t::keybd_event(0x12,0,0,0);" +
|
||||
"[void]$t::keybd_event(0x12,0,2,0);" +
|
||||
"for($i=0;$i -lt 25;$i++){" +
|
||||
" Start-Sleep -Milliseconds 300;" +
|
||||
" $p=Get-Process | Where-Object {$_.Path -eq '" + exeLiteral + "' -and $_.MainWindowHandle -ne 0} | Select-Object -First 1;" +
|
||||
" if($p){" +
|
||||
" $h=$p.MainWindowHandle;" +
|
||||
" $fg=$t::GetForegroundWindow();" +
|
||||
" $pid2=0;" +
|
||||
" $fgTid=$t::GetWindowThreadProcessId($fg,[ref]$pid2);" +
|
||||
" $curTid=$t::GetCurrentThreadId();" +
|
||||
" [void]$t::AttachThreadInput($curTid,$fgTid,$true);" +
|
||||
" [void]$t::ShowWindow($h,9);" +
|
||||
" [void]$t::BringWindowToTop($h);" +
|
||||
" [void]$t::SetForegroundWindow($h);" +
|
||||
" [void]$t::AttachThreadInput($curTid,$fgTid,$false);" +
|
||||
" break;" +
|
||||
" }" +
|
||||
"}";
|
||||
try {
|
||||
const ps = spawn("powershell.exe", [
|
||||
"-NoProfile", "-WindowStyle", "Hidden", "-ExecutionPolicy", "Bypass",
|
||||
"-Command", psScript,
|
||||
], { detached: true, stdio: "ignore", windowsHide: true, shell: false });
|
||||
ps.unref();
|
||||
log("spawned ps bring-to-front helper (keybd+attach+setfg)");
|
||||
} catch (err) {
|
||||
log("ps bring-to-front spawn failed: " + String(err));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let asarReleased = true;
|
||||
|
||||
Reference in new issue
Block a user