mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 10:10:22 +08:00
fix: 发布 1.1.60,极简/PTC 清洗、回退轮次与桌面补丁
- 反转 run_code/沙箱/plan 拦截(#60/#61/#63);minimal 补 agent-instructions(#26) - 回退按当前轮定位(rewind + #74/#75);Apply 写入 client 包避免取消重启 - 默认提示词重新加密;#66/#67/#69/#71/#70 与 README #68 安装说明 Closes #66, #67, #69, #70, #71. Docs for #68. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7ad39ad832
commit
f50ebbc032
25 files changed
+1461
-792
No files matched your search
+217
-37
@@ -876,7 +876,12 @@ const ASAR_RENAME_WHEN_IDLE_PS1 = [
|
||||
"$ErrorActionPreference = 'Continue'",
|
||||
"$deadline = (Get-Date).AddMinutes(30)",
|
||||
"while ((Get-Date) -lt $deadline) {",
|
||||
" if (Test-Path -LiteralPath (Join-Path $env:TEMP 'dsh-purge-official-restarting')) { exit 0 }",
|
||||
" $restartFlag = Join-Path $env:TEMP 'dsh-purge-official-restarting'",
|
||||
" if (Test-Path -LiteralPath $restartFlag) {",
|
||||
" $flagAge = (Get-Date) - (Get-Item -LiteralPath $restartFlag).LastWriteTime",
|
||||
" if ($flagAge.TotalSeconds -ge 0 -and $flagAge.TotalSeconds -lt 180) { exit 0 }",
|
||||
" Remove-Item -LiteralPath $restartFlag -Force -ErrorAction SilentlyContinue",
|
||||
" }",
|
||||
" $holding = @(Get-Process -ErrorAction SilentlyContinue | Where-Object { try { $_.Path -eq $ExePath } catch { $false } })",
|
||||
" if ($holding.Count -gt 0) { Start-Sleep -Milliseconds 500; continue }",
|
||||
" if (-not (Test-Path -LiteralPath $AsarPath)) {",
|
||||
@@ -927,11 +932,18 @@ const ASAR_RENAME_WHEN_IDLE_SH = [
|
||||
" esac",
|
||||
" nohup \"$exe\" >/dev/null 2>&1 &",
|
||||
"}",
|
||||
"app_running() {",
|
||||
" ps -Ao command= 2>/dev/null | grep -F \"$exe\" | grep -v \"dsh-purge-asar-rename-when-idle\" | grep -v grep >/dev/null",
|
||||
"}",
|
||||
"while [ \"$(date +%s)\" -lt \"$deadline\" ]; do",
|
||||
" if [ ! -e \"$asar\" ]; then",
|
||||
" rm -f \"$lock\"",
|
||||
" exit 0",
|
||||
" fi",
|
||||
" if app_running; then",
|
||||
" sleep 1",
|
||||
" continue",
|
||||
" fi",
|
||||
" bak=\"$asar.bak\"",
|
||||
" rm -f \"$bak\"",
|
||||
" if mv \"$asar\" \"$bak\" 2>/dev/null; then",
|
||||
@@ -1054,13 +1066,20 @@ export function sealedSwapKind(platform = process.platform) {
|
||||
}
|
||||
|
||||
function officialExeFromProcess() {
|
||||
// Windows 官方包用 wscript。macOS 解开后的重启走 restart-desktop.js,不靠 requestRestart。
|
||||
if (process.platform !== "win32") return "";
|
||||
if (isOfficialHarnessExecutable(process.execPath)) return path.normalize(process.execPath);
|
||||
if (isOfficialHarnessExecutable(process.execPath) && isFile(process.execPath)) {
|
||||
return path.normalize(process.execPath);
|
||||
}
|
||||
const resources = process.resourcesPath;
|
||||
if (!resources) return "";
|
||||
const beside = path.join(path.dirname(resources), "DeepSeek Harness.exe");
|
||||
return fs.existsSync(beside) ? beside : "";
|
||||
const hit = firstExistingExe([path.dirname(path.normalize(resources))]);
|
||||
if (hit && isOfficialHarnessExecutable(hit)) return hit;
|
||||
return "";
|
||||
}
|
||||
|
||||
/** 密封包换走 app.asar 用哪条。Windows 有 JScript 才用 wscript,否则 PowerShell。Linux 和 macOS 用重启脚本。 */
|
||||
export function sealedSwapEngine(platform = process.platform, wshJs = false) {
|
||||
if (platform === "win32") return wshJs ? "wscript" : "powershell";
|
||||
return "helper";
|
||||
}
|
||||
|
||||
function dshHomeForRelaunch() {
|
||||
@@ -1155,7 +1174,7 @@ function officialRestartScript(exe, asar, dshHome) {
|
||||
"var primary = hostClean(readBody(promptFile));",
|
||||
"var alt = hostClean(readBody(altFile));",
|
||||
"clean = (primary == true || alt == true) && primary != false && alt != false;",
|
||||
"if (!clean) log('clean markers missing; not starting');",
|
||||
"if (!clean) { log('clean markers missing; not starting'); try { if (fso.FileExists(" + jsString(path.join(os.tmpdir(), "dsh-purge-official-restarting")) + ")) fso.DeleteFile(" + jsString(path.join(os.tmpdir(), "dsh-purge-official-restarting")) + ", true); } catch (ignoreStop) {} WScript.Quit(1); }",
|
||||
"if (clean) {",
|
||||
"log('clean markers ok');",
|
||||
"WScript.Sleep(800);",
|
||||
@@ -1175,7 +1194,7 @@ function officialRestartScript(exe, asar, dshHome) {
|
||||
"var renamed = false;",
|
||||
"if (!fso.FileExists(asar)) { renamed = true; log('asar already absent'); }",
|
||||
"if (fso.FileExists(asar)) { var bak = asar + '.bak'; if (fso.FileExists(bak)) fso.DeleteFile(bak, true); for (var n = 0; n < 80; n++) { try { fso.MoveFile(asar, bak); renamed = true; log('renamed'); break; } catch (err) { log(err && err.message ? err.message : String(err)); if (n === 20 || n === 50) { sh.Run('taskkill.exe /F /T /IM DeepSeek Harness.exe', 0, true); WScript.Sleep(800); } WScript.Sleep(300); } } }",
|
||||
"if (!renamed) { log('rename failed; not starting'); }",
|
||||
"if (!renamed) { log('rename failed; not starting'); try { if (fso.FileExists(" + jsString(path.join(os.tmpdir(), "dsh-purge-official-restarting")) + ")) fso.DeleteFile(" + jsString(path.join(os.tmpdir(), "dsh-purge-official-restarting")) + ", true); } catch (ignoreStop2) {} WScript.Quit(1); }",
|
||||
"if (renamed) {",
|
||||
"try { sh.Environment('PROCESS').Remove('ELECTRON_RUN_AS_NODE'); } catch (ignore) {}",
|
||||
"try { sh.Environment('PROCESS').Remove('DSH_BASE'); } catch (ignoreBase) {}",
|
||||
@@ -1213,6 +1232,83 @@ function unpackedHostIsClean(exe) {
|
||||
});
|
||||
}
|
||||
|
||||
const OFFICIAL_RESTART_FLAG = "dsh-purge-official-restarting";
|
||||
const OFFICIAL_RESTART_WINDOW_MS = 180000;
|
||||
|
||||
/**
|
||||
* Windows 官方换包走哪条。
|
||||
* 本机 wscript 能跑 .js 时仍用原来的脚本。跑不了(云端很多机器没有 JScript 引擎)
|
||||
* 才改用已经在用的 PowerShell。没确认脚本已经开始,不把进行中的标记当成成功。
|
||||
*/
|
||||
export function planOfficialRestart({ wshJs, scriptAscii, inflightAgeMs, logStarted, logState } = {}) {
|
||||
const age = Number(inflightAgeMs);
|
||||
const fresh = Number.isFinite(age) && age >= 0 && age < OFFICIAL_RESTART_WINDOW_MS;
|
||||
const state = logState || (logStarted === true ? "running" : "none");
|
||||
// 只有脚本正在结束当前进程时才算进行中。写过 begin 又停掉的,这次点击要重新开始。
|
||||
if (fresh && state === "running") return { action: "already" };
|
||||
const kind = wshJs === true && scriptAscii !== false ? "wscript" : "powershell";
|
||||
return {
|
||||
action: "start",
|
||||
kind,
|
||||
writeInflight: kind === "wscript",
|
||||
clearInflight: kind !== "wscript",
|
||||
};
|
||||
}
|
||||
|
||||
function officialRestartFlagPath() {
|
||||
return path.join(os.tmpdir(), OFFICIAL_RESTART_FLAG);
|
||||
}
|
||||
|
||||
function officialRestartLogState(sinceMs) {
|
||||
try {
|
||||
const log = path.join(os.tmpdir(), "dsh-purge-official-restart.log");
|
||||
const st = fs.statSync(log);
|
||||
if (sinceMs && st.mtimeMs + 1000 < sinceMs) return "none";
|
||||
const lines = fs.readFileSync(log, "utf8").split(/\r?\n/).filter(Boolean);
|
||||
let begin = -1;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
if (/(^|\s)begin$/.test(lines[i])) begin = i;
|
||||
}
|
||||
if (begin < 0) return "none";
|
||||
const tail = lines.slice(begin);
|
||||
if (tail.some((line) => /not starting|rename failed/.test(line))) return "aborted";
|
||||
if (tail.some((line) => /(^|\s)started$/.test(line))) return "done";
|
||||
return "running";
|
||||
} catch {
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
||||
let wshJsEngineCached;
|
||||
function wshJsEngineWorks() {
|
||||
if (wshJsEngineCached !== undefined) return wshJsEngineCached;
|
||||
wshJsEngineCached = probeWshJsEngine();
|
||||
return wshJsEngineCached;
|
||||
}
|
||||
|
||||
function probeWshJsEngine() {
|
||||
const id = `${process.pid}-${Date.now()}`;
|
||||
const script = path.join(os.tmpdir(), `dsh-purge-wsh-probe-${id}.js`);
|
||||
const out = path.join(os.tmpdir(), `dsh-purge-wsh-probe-${id}.txt`);
|
||||
try {
|
||||
const body = "var fso=new ActiveXObject('Scripting.FileSystemObject');var f=fso.CreateTextFile("
|
||||
+ wshLiteral(out)
|
||||
+ ", true);f.WriteLine('ok');f.Close();\r\n";
|
||||
fs.writeFileSync(script, body, "utf8");
|
||||
execFileSync("cscript.exe", ["//nologo", script], {
|
||||
timeout: 4000,
|
||||
windowsHide: true,
|
||||
stdio: "ignore",
|
||||
});
|
||||
return fs.existsSync(out);
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
try { fs.unlinkSync(script); } catch { /* 探针用完即删 */ }
|
||||
try { fs.unlinkSync(out); } catch { /* 探针用完即删 */ }
|
||||
}
|
||||
}
|
||||
|
||||
/** 点「重启」后结束当前官方客户端,挪开被占用的 app.asar,再自己打开。 */
|
||||
export function restartOfficialHarness(options = {}) {
|
||||
const hinted = options.exe ? path.normalize(String(options.exe)) : "";
|
||||
@@ -1240,22 +1336,58 @@ export function restartOfficialHarness(options = {}) {
|
||||
if (options.requireClean !== false && !unpackedHostIsClean(exe)) {
|
||||
return { restarting: false, clean: false };
|
||||
}
|
||||
const inflight = path.join(os.tmpdir(), "dsh-purge-official-restarting");
|
||||
const inflight = officialRestartFlagPath();
|
||||
let inflightAgeMs = -1;
|
||||
let inflightMtime = 0;
|
||||
try {
|
||||
const age = Date.now() - fs.statSync(inflight).mtimeMs;
|
||||
// 同一次换进程里不再开第二轮,避免杀掉刚拉起来的客户端。
|
||||
if (age >= 0 && age < 180000) return { restarting: true, already: true };
|
||||
const st = fs.statSync(inflight);
|
||||
inflightAgeMs = Date.now() - st.mtimeMs;
|
||||
inflightMtime = st.mtimeMs;
|
||||
} catch {
|
||||
// 没有进行中的重启。
|
||||
}
|
||||
try { fs.writeFileSync(inflight, `${Date.now()}\n`, "utf8"); } catch { /* 标记写失败仍继续这一次 */ }
|
||||
const asar = options.asar || appAsarPath(exe);
|
||||
const restartBody = officialRestartScript(exe, asar, dshHomeForRelaunch());
|
||||
const scriptAscii = isWshAscii(restartBody);
|
||||
const plan = planOfficialRestart({
|
||||
wshJs: scriptAscii && wshJsEngineWorks(),
|
||||
scriptAscii,
|
||||
inflightAgeMs,
|
||||
logState: officialRestartLogState(inflightMtime),
|
||||
});
|
||||
if (plan.action === "already") return { restarting: true, already: true };
|
||||
if (plan.clearInflight) {
|
||||
try { fs.unlinkSync(inflight); } catch { /* 没有残留标记 */ }
|
||||
}
|
||||
if (plan.kind === "powershell") {
|
||||
console.warn("[dsh-purge] wscript 不能执行 .js,改用 PowerShell 挪开 app.asar");
|
||||
try {
|
||||
scheduleAsarSwapRestart({
|
||||
exe,
|
||||
asar,
|
||||
waitPid: process.pid,
|
||||
exit: options.exit !== false,
|
||||
});
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.warn("[dsh-purge] PowerShell asar swap failed:", message);
|
||||
return { restarting: false, error: message };
|
||||
}
|
||||
return { restarting: true, asarSwap: true, engine: "powershell" };
|
||||
}
|
||||
if (plan.writeInflight) {
|
||||
try { fs.writeFileSync(inflight, `${Date.now()}\n`, "utf8"); } catch { /* 标记写失败仍继续这一次 */ }
|
||||
}
|
||||
const asarPresent = Boolean(asar && asarArchiveIsFile(asar));
|
||||
const swapLock = asar ? `${asar}.swap-lock` : "";
|
||||
if (asarPresent && swapLock) {
|
||||
try {
|
||||
const age = Date.now() - fs.statSync(swapLock).mtimeMs;
|
||||
if (age >= 0 && age < 90 * 1000) return { restarting: true, already: true, asarSwap: true };
|
||||
const lockStat = fs.statSync(swapLock);
|
||||
const age = Date.now() - lockStat.mtimeMs;
|
||||
// 锁还在、但重启日志没有 begin:上一轮 wscript 没跑起来,不能把它当成正在换包。
|
||||
if (age >= 0 && age < 90 * 1000 && officialRestartLogState(lockStat.mtimeMs) === "running") {
|
||||
return { restarting: true, already: true, asarSwap: true };
|
||||
}
|
||||
} catch {
|
||||
// 没有进行中的切换。
|
||||
}
|
||||
@@ -1263,8 +1395,6 @@ export function restartOfficialHarness(options = {}) {
|
||||
}
|
||||
const script = path.join(os.tmpdir(), "dsh-purge-official-restart.js");
|
||||
const launcher = path.join(os.tmpdir(), "dsh-purge-official-restart.cmd");
|
||||
const restartBody = officialRestartScript(exe, asar, dshHomeForRelaunch());
|
||||
if (!isWshAscii(restartBody)) return { restarting: false };
|
||||
fs.writeFileSync(script, restartBody, "utf8");
|
||||
fs.writeFileSync(launcher, `@echo off\r\nstart "" wscript.exe //nologo //B "${script}"\r\n`, "utf8");
|
||||
const child = spawn("cmd.exe", ["/d", "/c", launcher], {
|
||||
@@ -1274,7 +1404,7 @@ export function restartOfficialHarness(options = {}) {
|
||||
shell: false,
|
||||
});
|
||||
child.unref();
|
||||
return { restarting: true };
|
||||
return { restarting: true, engine: "wscript" };
|
||||
}
|
||||
|
||||
export function scheduleCleanupRestart({ cleanup = [], ctx } = {}) {
|
||||
@@ -1359,28 +1489,78 @@ function patchOfficialCliShimText(text) {
|
||||
return { changed: next !== text, text: next };
|
||||
}
|
||||
|
||||
function patchOfficialCliCmdText(text) {
|
||||
const CMD_ENTRY_BEGIN = "REM dsh-purge cli entry begin";
|
||||
const CMD_ENTRY_END = "REM dsh-purge cli entry end";
|
||||
const CMD_CLI_TAIL = "dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli.js";
|
||||
|
||||
function cmdEntryAlreadyGood(text) {
|
||||
if (!text.includes(CMD_ENTRY_BEGIN) || !text.includes(CMD_ENTRY_END)) return false;
|
||||
if (/set\s+"entry=%entry%"/i.test(text)) return false;
|
||||
if (!/set\s+"entry=%~dp0[^"\r\n]*app\.asar\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli\.js"/i.test(text)) return false;
|
||||
if (!/if not exist "%entry%" set "entry=%~dp0[^"\r\n]*app\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli\.js"/i.test(text)) return false;
|
||||
const outside = text.replace(/set\s+"entry=[^"]*"/gi, "");
|
||||
if (/app\.asar\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli\.js/i.test(outside)) return false;
|
||||
return /"%entry%"/.test(text);
|
||||
}
|
||||
|
||||
function stripCmdEntryBlock(text) {
|
||||
return text.replace(/\r?\n?REM dsh-purge cli entry begin[\s\S]*?REM dsh-purge cli entry end\r?\n?/gi, "\r\n");
|
||||
}
|
||||
|
||||
function replaceQuotedInsensitive(text, pathValue, replacement) {
|
||||
const needle = `"${pathValue}"`;
|
||||
const lower = text.toLowerCase();
|
||||
const want = needle.toLowerCase();
|
||||
let out = "";
|
||||
let i = 0;
|
||||
while (i < text.length) {
|
||||
const at = lower.indexOf(want, i);
|
||||
if (at < 0) {
|
||||
out += text.slice(i);
|
||||
break;
|
||||
}
|
||||
out += text.slice(i, at) + replacement;
|
||||
i = at + needle.length;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Windows 官方 dsh.cmd 用 %~dp0,没有 %resources%。先改原文,再插入回退,避免正则改到刚插入的行。 */
|
||||
export function patchOfficialCliCmdText(text) {
|
||||
if (!text) return { changed: false, text };
|
||||
const hasAsar =
|
||||
text.toLowerCase().includes("app.asar\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli.js") ||
|
||||
text.includes(CLI_ASAR_ENTRY);
|
||||
if (!hasAsar) return { changed: false, text };
|
||||
if (text.includes("dsh-purge cli entry begin")) return { changed: false, text, already: true };
|
||||
const asarWin = "%resources%\\app.asar\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli.js";
|
||||
const dirWin = "%resources%\\app\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli.js";
|
||||
let next = text;
|
||||
const hasAsar = text.toLowerCase().includes("app.asar\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli.js")
|
||||
|| text.includes(CLI_ASAR_ENTRY);
|
||||
const hasMarker = text.includes(CMD_ENTRY_BEGIN);
|
||||
if (!hasAsar && !hasMarker) return { changed: false, text };
|
||||
if (cmdEntryAlreadyGood(text)) return { changed: false, text, already: true };
|
||||
|
||||
const nl = text.includes("\r\n") ? "\r\n" : "\n";
|
||||
let next = stripCmdEntryBlock(text);
|
||||
const found = next.match(/%~dp0[^"\r\n]*?app\.asar\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli\.js/i);
|
||||
if (!found) return { changed: false, text };
|
||||
const cut = found[0].toLowerCase().indexOf("app.asar\\");
|
||||
if (cut < 0) return { changed: false, text };
|
||||
const prefix = found[0].slice(0, cut);
|
||||
next = replaceQuotedInsensitive(next, `${prefix}app.asar\\${CMD_CLI_TAIL}`, '"%entry%"');
|
||||
next = replaceQuotedInsensitive(next, `${prefix}app\\${CMD_CLI_TAIL}`, '"%entry%"');
|
||||
const block = [
|
||||
"REM dsh-purge cli entry begin",
|
||||
`set "entry=${asarWin}"`,
|
||||
`if not exist "%entry%" set "entry=${dirWin}"`,
|
||||
"REM dsh-purge cli entry end",
|
||||
CMD_ENTRY_BEGIN,
|
||||
`set "entry=${prefix}app.asar\\${CMD_CLI_TAIL}"`,
|
||||
`if not exist "%entry%" set "entry=${prefix}app\\${CMD_CLI_TAIL}"`,
|
||||
CMD_ENTRY_END,
|
||||
"",
|
||||
].join("\r\n");
|
||||
if (!next.includes("dsh-purge cli entry begin")) next = block + next;
|
||||
next = next.replace(
|
||||
/%resources%\\app\.asar\\dsh\\node_modules\\@deepseek-ai\\dsh-desktop-host\\lib\\cli\.js/gi,
|
||||
"%entry%",
|
||||
);
|
||||
].join(nl);
|
||||
const insertAt = next.search(/ELECTRON_RUN_AS_NODE=1/);
|
||||
if (insertAt >= 0) {
|
||||
const lineEnd = next.indexOf("\n", insertAt);
|
||||
const at = lineEnd >= 0 ? lineEnd + 1 : next.length;
|
||||
next = `${next.slice(0, at)}${block}${next.slice(at)}`;
|
||||
} else {
|
||||
next = block + next;
|
||||
}
|
||||
if (/set\s+"entry=%entry%"/i.test(next)) return { changed: false, text };
|
||||
if (!/"%entry%"/.test(next)) return { changed: false, text };
|
||||
if (!cmdEntryAlreadyGood(next)) return { changed: false, text };
|
||||
return { changed: next !== text, text: next };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user