fix: 发布 1.1.67,#85/#84 + 启动种子 + 85 项清单一次性修复

- #85 continue 续跑加 failStreak 阈值;refundContinue 不清 lastContinueSeq,连错 3 次放弃
- #84 region gate denialStream 单批 text-chunks 带 .length,不再卡宿主 UI
- 启动即种子:$DSH_HOME/prompt-inject.md 为空/缺失自动写加密默认稿,用户改过的不动
- Windows 重启脚本:pid+random 命名 + 直接 wscript.exe //B,不再 /IM 杀全名
- 官方 CLI shim 原子写 + .dsh-purge.bak 可回退
- WScript clean 判定两 null 放行,首装不卡;探测缓存不缓存空结果
- scheduleAsarRenameWhenIdle 的 .rename-pending 锁时钟回拨/30min 失效自动清
- 三处 execFileSync 加 maxBuffer: 8MB,大结果不再 ENOBUFS 静默
- net-scope / identity / hooks-deny / extract-asar / client.js / core.js / update.js / skills.js / refusal-recover / rewind / rules / reapply 等共 85 项修复
This commit is contained in:
叫我小杨同学 committed 2026-10-09 11:52:39 +08:00
1 parent 3df4f489b1
commit d26e69a4fa
18 files changed
+1153 -403

No files matched your search

+57 -20
View File
@@ -2,13 +2,37 @@ import os from "node:os";
import path from "node:path";
import { execFileSync, spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { randomBytes } from "node:crypto";
import { isWshAscii, wshLiteral } from "./official-update.js";
import { asarArchiveIsFile, hostFs } from "./extract-asar.js";
import { hostPromptKeepsInject } from "./host-clean.js";
import { atomicCopyFile, atomicWriteSync } from "./core.js";
function restartUniqSuffix() {
return `${process.pid}-${randomBytes(4).toString("hex")}`;
}
// 无归档时用 node:fs。app.asar 还在时不能把映射出来的 resources/app 当成已经解开。
const fs = hostFs;
/** 启动一次:清掉旧版本留下的无 pid 后缀的 restart 脚本/日志(超过 24h)。 */
export function cleanupLegacyRestartScripts() {
const tmp = os.tmpdir();
let entries = [];
try { entries = fs.readdirSync(tmp); } catch { return; }
const pat = /^dsh-purge-official-restart(?:-[0-9a-f-]+)?\.(?:js|cmd|log)$/i;
for (const name of entries) {
if (!pat.test(name)) continue;
const fp = path.join(tmp, name);
try {
const st = fs.statSync(fp);
if (Date.now() - st.mtimeMs > 24 * 60 * 60 * 1000) {
fs.unlinkSync(fp);
}
} catch { /* ignore */ }
}
}
/** 路径落在 `app.asar/...` 里面。`app.asar` 文件本身和 `app.asar.unpacked` 不算。 */
export function isAsarSealedPath(p) {
const parts = String(p || "").replace(/\\/g, "/").split("/").filter(Boolean);
@@ -317,6 +341,7 @@ export function registryInstallDirs() {
encoding: "utf8",
windowsHide: true,
timeout: 15000,
maxBuffer: 8 << 20,
});
} catch {
return [];
@@ -335,8 +360,9 @@ export function registryInstallDirs() {
found.push(exeDirFromRegistryValue(row.DisplayIcon));
found.push(exeDirFromRegistryValue(row.UninstallString));
}
cachedRegistryDirs = uniquePaths(found).filter((dir) => firstExistingExe([dir]));
return cachedRegistryDirs;
const result = uniquePaths(found).filter((dir) => firstExistingExe([dir]));
if (result.length > 0) cachedRegistryDirs = result;
return result;
}
const PORTABLE_INSTALL_NAMES = [
@@ -420,6 +446,7 @@ function runningOfficialExecutables() {
encoding: "utf8",
windowsHide: true,
timeout: 15000,
maxBuffer: 8 << 20,
});
} catch {
// 探测失败不缓存空结果,否则整次进程都找不到正在跑的客户端。
@@ -432,8 +459,10 @@ function runningOfficialExecutables() {
} catch {
return [];
}
cachedRunningExes = uniquePaths(rows.map((item) => String(item || "").trim()).filter((fp) => isDesktopExecutable(fp) && isFile(fp)));
return cachedRunningExes;
// 结果为空也不缓存:客户端可能稍后才启动,否则本进程后续永远找不到它。
const result = uniquePaths(rows.map((item) => String(item || "").trim()).filter((fp) => isDesktopExecutable(fp) && isFile(fp)));
if (result.length > 0) cachedRunningExes = result;
return result;
}
const SHORTCUT_EXE_SCRIPT = `
@@ -475,6 +504,7 @@ function shortcutOfficialExecutables() {
encoding: "utf8",
windowsHide: true,
timeout: 15000,
maxBuffer: 8 << 20,
});
} catch {
return [];
@@ -486,8 +516,9 @@ function shortcutOfficialExecutables() {
} catch {
return [];
}
cachedShortcutExes = uniquePaths(rows.map((item) => String(item || "").trim()).filter((fp) => isDesktopExecutable(fp) && isFile(fp)));
return cachedShortcutExes;
const result = uniquePaths(rows.map((item) => String(item || "").trim()).filter((fp) => isDesktopExecutable(fp) && isFile(fp)));
if (result.length > 0) cachedShortcutExes = result;
return result;
}
/** 点「应用」前清掉失败的空缓存,重新找本机 exe。 */
@@ -811,9 +842,7 @@ const ASAR_SWAP_PS1 = [
"$installRoot = (Split-Path -Parent $ExePath).TrimEnd('\\')",
"$exeNorm = $ExePath.ToLower()",
"function Stop-HarnessInstall {",
" foreach ($n in $names) {",
" & taskkill.exe /F /T /IM ($n + '.exe') 2>$null | Out-Null",
" }",
" # 仅按本次安装根路径杀本安装的进程;不再用 /IM 全名杀,避免误伤同名第三方。",
" Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {",
" $ep = $_.ExecutablePath",
" if (-not $ep) { return $false }",
@@ -966,12 +995,14 @@ export function scheduleAsarRenameWhenIdle({ exe, asar } = {}) {
try {
const age = Date.now() - fs.statSync(lock).mtimeMs;
if (age >= 0 && age < 30 * 60 * 1000) return { pending: true, already: true };
// age<0 时钟回拨或 age>=30min stale,视为失效,清掉重做。
try { fs.unlinkSync(lock); } catch { /* ignore */ }
} catch {
// 没有待处理标记,下面新建。
}
fs.writeFileSync(lock, `${Date.now()}\n`, "utf8");
if (process.platform === "win32") {
const script = path.join(os.tmpdir(), "dsh-purge-asar-rename-when-idle.ps1");
const script = path.join(os.tmpdir(), `dsh-purge-asar-rename-when-idle-${restartUniqSuffix()}.ps1`);
fs.writeFileSync(script, ASAR_RENAME_WHEN_IDLE_PS1, "utf8");
const child = spawn(
"powershell.exe",
@@ -993,7 +1024,7 @@ export function scheduleAsarRenameWhenIdle({ exe, asar } = {}) {
child.unref();
return { pending: true };
}
const script = path.join(os.tmpdir(), "dsh-purge-asar-rename-when-idle.sh");
const script = path.join(os.tmpdir(), `dsh-purge-asar-rename-when-idle-${restartUniqSuffix()}.sh`);
fs.writeFileSync(script, ASAR_RENAME_WHEN_IDLE_SH, { encoding: "utf8", mode: 0o755 });
const child = spawn("/bin/sh", [script, asar, exe, lock], {
detached: true,
@@ -1168,7 +1199,8 @@ function officialRestartScript(exe, asar, dshHome) {
"var clean = false;",
"var primary = hostClean(readBody(promptFile));",
"var alt = hostClean(readBody(altFile));",
"clean = (primary == true || alt == true) && primary != false && alt != false;",
"// null = 文件不存在或暂无法判断(首装、读失败)。此时不阻塞启动;只有任一明确 false(真的脏)才拦。",
"clean = primary != false && alt != false;",
"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');",
@@ -1176,8 +1208,7 @@ function officialRestartScript(exe, asar, dshHome) {
"var list = pids();",
"log('pids ' + list.join(','));",
"for (var i = 0; i < list.length; i++) sh.Run('taskkill.exe /F /PID ' + list[i], 0, true);",
"sh.Run('taskkill.exe /F /T /IM DeepSeek Harness.exe', 0, true);",
"sh.Run('taskkill.exe /F /T /IM DSH Desktop.exe', 0, true);",
"// 不再按 /IM 全名杀,避免误伤同名第三方。超时后再按 pid 列表重跑。",
"var deadline = new Date().getTime() + 20000;",
"var left = list;",
"while (new Date().getTime() < deadline) { left = pids(); if (left.length === 0) break; WScript.Sleep(200); }",
@@ -1188,7 +1219,7 @@ function officialRestartScript(exe, asar, dshHome) {
"if (fso.FolderExists(unpacked) && !fso.FileExists(marker)) { sh.Run('robocopy.exe \"' + unpacked + '\" \"' + appDir + '\" /E /XC /XN /XO /NFL /NDL /NJH /NJS /nc /ns /np', 0, true); log('overlay'); }",
"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 (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) { var hold = pids(); for (var k = 0; k < hold.length; k++) sh.Run('taskkill.exe /F /PID ' + hold[k], 0, true); WScript.Sleep(800); } WScript.Sleep(300); } } }",
"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) {}",
@@ -1388,11 +1419,12 @@ export function restartOfficialHarness(options = {}) {
}
try { fs.writeFileSync(swapLock, `${Date.now()}\n`, "utf8"); } catch {}
}
const script = path.join(os.tmpdir(), "dsh-purge-official-restart.js");
const launcher = path.join(os.tmpdir(), "dsh-purge-official-restart.cmd");
// 启动时清一次残留的旧 restart 脚本/日志。多实例时,脚本名用 pid+random 避免打架。
cleanupLegacyRestartScripts();
const script = path.join(os.tmpdir(), `dsh-purge-official-restart-${restartUniqSuffix()}.js`);
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], {
// 直接起 wscript.exe;不写 .cmd 中介文件,避免路径有空格/引号时解析异常。
const child = spawn("wscript.exe", ["//nologo", "//B", script], {
detached: true,
stdio: "ignore",
windowsHide: true,
@@ -1590,7 +1622,12 @@ export function repairOfficialCliAfterUnpack(exe = findDesktopAppExecutable()) {
continue;
}
try {
fs.writeFileSync(fp, patched.text, "utf8");
// 原子写之前先存一份 .dsh-purge.bak,断电/磁盘满也能回退到官方原文。
const bak = `${fp}.dsh-purge.bak`;
if (!isFile(bak)) {
try { atomicCopyFile(fp, bak); } catch { /* 写不了 bak 不致命 */ }
}
atomicWriteSync(fp, patched.text, "utf8");
report.shims.push({ path: fp, status: "patched" });
} catch (e) {
report.shims.push({ path: fp, status: "write_error", error: String(e && e.message ? e.message : e) });