mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 07:10:22 +08:00
fix: 发布 1.1.55,重启后提示词稳定注入且不再误取消
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4da6d2eaf4
commit
6f37daa01d
10 files changed
+124
-178
No files matched your search
+63
-32
@@ -20,6 +20,7 @@ import * as uninstaller from "./uninstall.js";
|
||||
import { installRegionGate, readAllowText, writeAllowText } from "./net-scope.js";
|
||||
import * as refusalRecover from "./refusal-recover.js";
|
||||
import {
|
||||
applyShouldRestart,
|
||||
isInactiveContextError,
|
||||
settleDelayMs,
|
||||
settleMayReapply,
|
||||
@@ -203,9 +204,17 @@ function rejectIfNeedPrompt(dshHome, boxText) {
|
||||
}
|
||||
|
||||
async function ensureRestartReady() {
|
||||
const inject = rules.resolveInjectText(core.findDshHome());
|
||||
const home = core.findDshHome();
|
||||
try { core.seedOverrideSync(home); } catch { /* 仍可读内置默认 */ }
|
||||
const inject = rules.resolveInjectText(home);
|
||||
if (!inject.text) return { ok: false, error: "没有可注入的提示词,已取消重启" };
|
||||
return core.ensureHostClean();
|
||||
const clean = await core.ensureHostClean();
|
||||
if (clean.ok) return clean;
|
||||
// 密封 asar 读的是旧包,清洗写在解包目录;允许换包重启,注入靠 assemble 包装。
|
||||
try {
|
||||
if (core.desktopAsarStillSealed()) return { ok: true, pending: clean.pending || [] };
|
||||
} catch { /* 非桌面 */ }
|
||||
return clean;
|
||||
}
|
||||
|
||||
export function registerPromptSections(ctx, config, _injectFile) {
|
||||
@@ -215,7 +224,7 @@ export function registerPromptSections(ctx, config, _injectFile) {
|
||||
interpolate: false,
|
||||
text: (context) => {
|
||||
if (!injectAllowedNow(config)) return "";
|
||||
return rules.resolveModeInject(core.findDshHome(), isRedteamSession(context));
|
||||
return readInjectFallback(context);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -249,7 +258,9 @@ export function registerPromptSections(ctx, config, _injectFile) {
|
||||
}
|
||||
|
||||
function readInjectFallback(context) {
|
||||
return rules.resolveModeInject(core.findDshHome(), isRedteamSession(context));
|
||||
const home = core.findDshHome();
|
||||
try { core.seedOverrideSync(home); } catch { /* 落盘失败仍试读默认 */ }
|
||||
return rules.resolveModeInject(home, isRedteamSession(context));
|
||||
}
|
||||
|
||||
function installMnemonMessageStrip(ctx, config = {}) {
|
||||
@@ -360,33 +371,46 @@ function installRewindDeriveFilter(ctx) {
|
||||
function installIdentityOverride(ctx, config = {}) {
|
||||
if (typeof ctx.on !== "function") return;
|
||||
installSubagentRoleCapture(ctx);
|
||||
if (ctx.__dshPurgeAssembleHook) return;
|
||||
ctx.__dshPurgeAssembleHook = true;
|
||||
const hook = async (_assembly, context, next) => {
|
||||
const assembled = typeof next === "function" ? await next() : _assembly;
|
||||
return rewriteLiveAssembly(config, assembled, context);
|
||||
};
|
||||
// 只挂一次。不改 contexts,不改 pre-step 白名单,梁神 phase-1 和其它插件的消息原样保留。
|
||||
ctx.on("system-prompt/assemble", hook, { global: true, prepend: true });
|
||||
// 0.1.5 起 complete:true 在 waterfall 之后用调用前的快照盖掉 sections。
|
||||
// 换宿主版本时文件补丁还没写上,或换插件版本时旧钩子把 dsh-purge 段删掉,注入都会丢。
|
||||
// 包住 assemble 的返回值,按 prompt-inject.md 再补回。
|
||||
const wrapAssemble = () => {
|
||||
const prompt = ctx.systemPrompt;
|
||||
if (!prompt || typeof prompt.assemble !== "function" || prompt.__dshPurgeAssembleWrapped) return false;
|
||||
const orig = prompt.assemble;
|
||||
prompt.assemble = async function (...args) {
|
||||
const assembled = await orig.apply(this, args);
|
||||
return rewriteLiveAssembly(config, assembled, args[0]);
|
||||
};
|
||||
prompt.__dshPurgeAssembleWrapped = true;
|
||||
return true;
|
||||
try {
|
||||
const prompt = ctx.systemPrompt;
|
||||
if (!prompt || typeof prompt.assemble !== "function") return false;
|
||||
if (prompt.assemble._dshPurgeWrapped) return true;
|
||||
const orig = prompt.assemble.bind(prompt);
|
||||
const wrapped = async function assembleWithPurge(...args) {
|
||||
const assembled = await orig(...args);
|
||||
return rewriteLiveAssembly(config, assembled, args[0]);
|
||||
};
|
||||
wrapped._dshPurgeWrapped = true;
|
||||
prompt.assemble = wrapped;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
if (!wrapAssemble()) {
|
||||
queueMicrotask(wrapAssemble);
|
||||
setTimeout(wrapAssemble, 500);
|
||||
setTimeout(wrapAssemble, 2000);
|
||||
if (!ctx.__dshPurgeAssembleHook) {
|
||||
ctx.__dshPurgeAssembleHook = true;
|
||||
ctx.on("system-prompt/assemble", async (_assembly, context, next) => {
|
||||
const assembled = typeof next === "function" ? await next() : _assembly;
|
||||
return rewriteLiveAssembly(config, assembled, context);
|
||||
}, { global: true, prepend: true });
|
||||
}
|
||||
// complete:true 会在 waterfall 之后盖掉 sections;包住 assemble 返回值,每轮都补回注入。
|
||||
wrapAssemble();
|
||||
const started = Date.now();
|
||||
const timer = setInterval(() => {
|
||||
wrapAssemble();
|
||||
if (Date.now() - started > 20000) clearInterval(timer);
|
||||
}, 400);
|
||||
if (typeof timer.unref === "function") timer.unref();
|
||||
try { ctx.effect(() => () => clearInterval(timer)); } catch { /* 无 effect 时靠 unref */ }
|
||||
try { ctx.on("session/created", wrapAssemble); } catch { /* ignore */ }
|
||||
try {
|
||||
ctx.on("agent/pre-step", (payload, next) => {
|
||||
wrapAssemble();
|
||||
return typeof next === "function" ? next() : payload;
|
||||
}, { global: true, prepend: true });
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
function shimNeedsPatch(state) {
|
||||
@@ -1503,7 +1527,13 @@ function installWebServer(ctx, cfg) {
|
||||
const leaveTray = renamed || pendingQuit;
|
||||
if (complete) reapply.markPatchesApplied();
|
||||
if (leaveTray) bootFullQuit = true;
|
||||
const shouldRestart = complete;
|
||||
const sealedNow = core.desktopAsarStillSealed();
|
||||
const shouldRestart = applyShouldRestart({
|
||||
complete,
|
||||
sealed: sealedNow,
|
||||
appliedCount: summary.applied,
|
||||
});
|
||||
if (shouldRestart) reapply.markSettleRestart();
|
||||
sendJson(response, 200, {
|
||||
ok: true,
|
||||
complete,
|
||||
@@ -1511,8 +1541,8 @@ function installWebServer(ctx, cfg) {
|
||||
restart_after_apply: shouldRestart,
|
||||
unpacked_asar: leaveTray,
|
||||
needs_full_quit: leaveTray,
|
||||
asar_still_sealed: core.desktopAsarStillSealed(),
|
||||
asar_swap_started: Boolean(shouldRestart && handoff && core.desktopAsarStillSealed()),
|
||||
asar_still_sealed: sealedNow,
|
||||
asar_swap_started: Boolean(shouldRestart && handoff && sealedNow),
|
||||
applied: summary.applied,
|
||||
already: summary.already,
|
||||
failed: summary.failed.length,
|
||||
@@ -1532,7 +1562,7 @@ function installWebServer(ctx, cfg) {
|
||||
cli_repair: cliRepair,
|
||||
cmd_flash: flash,
|
||||
});
|
||||
if (shouldRestart && handoff && core.desktopAsarStillSealed()) {
|
||||
if (shouldRestart && handoff && sealedNow) {
|
||||
setImmediate(() => {
|
||||
try {
|
||||
core.restartSealedDesktopIfNeeded();
|
||||
@@ -1606,6 +1636,7 @@ function installWebServer(ctx, cfg) {
|
||||
if (request.method === "GET") {
|
||||
try {
|
||||
const home = core.findDshHome();
|
||||
try { core.seedOverrideSync(home); } catch { /* 仍可读内置默认 */ }
|
||||
const ui = await core.readOverrideForUi(home);
|
||||
sendJson(response, 200, { ok: true, ...ui, ...injectSnapshot(home) });
|
||||
} catch (e) {
|
||||
|
||||
@@ -41,3 +41,17 @@ export function settleMayReapply({ surface, sealed = false } = {}) {
|
||||
if (surface === "desktop" && sealed) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 点「应用」后是否重启:密封 asar 还要换包就重启;否则只在这次真的写了宿主补丁时重启。
|
||||
* 冷却不放在这里——否则第一次重启没换掉 asar,第二次应用会被当成取消。
|
||||
*/
|
||||
export function applyShouldRestart({
|
||||
complete = false,
|
||||
sealed = false,
|
||||
appliedCount = 0,
|
||||
} = {}) {
|
||||
if (!complete) return false;
|
||||
if (sealed) return true;
|
||||
return Number(appliedCount) > 0;
|
||||
}
|
||||
Reference in new issue
Block a user