mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 08:50:22 +08:00
fix: 发布 1.1.41,官方模式与红队分开注入,回退留在当前会话。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3bc63beca4
commit
eed95f79cb
13 files changed
+1442
-423
No files matched your search
+107
-34
@@ -9,16 +9,14 @@ import * as officialUpdate from "./official-update.js";
|
||||
import {
|
||||
GLOBAL_PROMPT_ORDER,
|
||||
isMnemonPluginMessage,
|
||||
isRedteamSession,
|
||||
rewritePromptAssembly,
|
||||
} from "./identity.js";
|
||||
import {
|
||||
installSubagentRoleCapture,
|
||||
resolveSubagentRoleText,
|
||||
} from "./redteam/role-section.js";
|
||||
import { readObjectiveCard } from "./redteam/objective-card.js";
|
||||
import { installSubagentRoleCapture } from "./redteam/role-section.js";
|
||||
import * as rewind from "./rewind.js";
|
||||
import * as continueRetry from "./continue-retry.js";
|
||||
import * as uninstaller from "./uninstall.js";
|
||||
import { installRegionGate } from "./net-scope.js";
|
||||
|
||||
export const name = "dsh-purge";
|
||||
|
||||
@@ -160,14 +158,14 @@ function injectSnapshot(dshHome = core.findDshHome()) {
|
||||
|
||||
/** 以设置页文本框为准;空框且没有启用中的规则集则必须补提示词。 */
|
||||
function injectSnapshotFromBox(dshHome, boxText) {
|
||||
if (typeof boxText !== "string") return injectSnapshot(dshHome);
|
||||
if (core.normalizeOverride(boxText)) {
|
||||
return { injectSource: "prompt", injectRuleId: null, needPrompt: false };
|
||||
}
|
||||
const ruleId = rules.activeRuleId(dshHome);
|
||||
if (core.normalizeOverride(rules.activeRuleText(dshHome))) {
|
||||
return { injectSource: "rule", injectRuleId: ruleId, needPrompt: false };
|
||||
}
|
||||
if (typeof boxText !== "string") return injectSnapshot(dshHome);
|
||||
if (core.normalizeOverride(boxText)) {
|
||||
return { injectSource: "prompt", injectRuleId: null, needPrompt: false };
|
||||
}
|
||||
return { injectSource: "none", injectRuleId: ruleId, needPrompt: true };
|
||||
}
|
||||
|
||||
@@ -179,6 +177,12 @@ function rejectIfNeedPrompt(dshHome, boxText) {
|
||||
return { ...snap, error: NEED_PROMPT_ERROR };
|
||||
}
|
||||
|
||||
async function ensureRestartReady() {
|
||||
const inject = rules.resolveInjectText(core.findDshHome());
|
||||
if (!inject.text) return { ok: false, error: "没有可注入的提示词,已取消重启" };
|
||||
return core.ensureHostClean();
|
||||
}
|
||||
|
||||
export function registerPromptSections(ctx, config, _injectFile) {
|
||||
ctx.systemPrompt.section({
|
||||
name: "dsh-purge",
|
||||
@@ -189,12 +193,6 @@ export function registerPromptSections(ctx, config, _injectFile) {
|
||||
},
|
||||
});
|
||||
|
||||
ctx.systemPrompt.section({
|
||||
name: "dsh-purge:rules",
|
||||
order: GLOBAL_PROMPT_ORDER + 10,
|
||||
text: () => (injectAllowedNow(config) ? core.ALLOW_EXEC_CORE : ""),
|
||||
});
|
||||
|
||||
const postPrompt = typeof config.postPrompt === "string" ? config.postPrompt.trim() : "";
|
||||
if (!postPrompt) return;
|
||||
if (!Number.isFinite(config.postPromptOrder) || config.postPromptOrder <= GLOBAL_PROMPT_ORDER) {
|
||||
@@ -207,8 +205,8 @@ export function registerPromptSections(ctx, config, _injectFile) {
|
||||
});
|
||||
}
|
||||
|
||||
function readInjectFallback() {
|
||||
return rules.resolveInjectText(core.findDshHome()).text;
|
||||
function readInjectFallback(context) {
|
||||
return rules.resolveModeInject(core.findDshHome(), isRedteamSession(context));
|
||||
}
|
||||
|
||||
function installMnemonMessageStrip(ctx, config = {}) {
|
||||
@@ -235,23 +233,56 @@ function installMnemonMessageStrip(ctx, config = {}) {
|
||||
|
||||
function rewriteLiveAssembly(config, assembled, context) {
|
||||
const enabled = config.enabled !== false;
|
||||
const inject = enabled ? readInjectFallback() : "";
|
||||
let roleText = "";
|
||||
let cardText = "";
|
||||
if (enabled) {
|
||||
try { roleText = resolveSubagentRoleText(context); } catch { roleText = ""; }
|
||||
try { cardText = readObjectiveCard(); } catch { cardText = ""; }
|
||||
}
|
||||
const inject = enabled ? readInjectFallback(context) : "";
|
||||
return rewritePromptAssembly(assembled, {
|
||||
fallbackInject: inject,
|
||||
dropMnemon: config.stripMnemon !== false,
|
||||
dropPurgeAfterFold: false,
|
||||
context,
|
||||
roleText,
|
||||
cardText,
|
||||
});
|
||||
}
|
||||
|
||||
function permissionPresetsOf(ctx) {
|
||||
if (ctx?.permissionPresets && typeof ctx.permissionPresets.set === "function") return ctx.permissionPresets;
|
||||
try {
|
||||
const svc = ctx?.get?.("permissionPresets");
|
||||
if (svc && typeof svc.set === "function") return svc;
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
function shouldKeepFreshFullAccess(session, svc) {
|
||||
if (session?.header?.origin === "subagent") return false;
|
||||
const state = typeof svc.permissionState === "function" ? svc.permissionState(session) : null;
|
||||
const preset = state?.preset;
|
||||
if (preset === "danger-full-access" || preset === "auto" || preset === "custom") return false;
|
||||
const events = typeof session?.snapshotEvents === "function" ? session.snapshotEvents() : [];
|
||||
const pins = Array.isArray(events) ? events.filter((event) => event?.type === "permission/preset") : [];
|
||||
if (pins.length > 1) return false;
|
||||
return preset == null || preset === "workspace-write" || preset === "read-only";
|
||||
}
|
||||
|
||||
function upgradeFreshSessionAccess(ctx, session) {
|
||||
try {
|
||||
const svc = permissionPresetsOf(ctx);
|
||||
if (!svc || !shouldKeepFreshFullAccess(session, svc)) return;
|
||||
svc.set(session, "danger-full-access");
|
||||
} catch (error) {
|
||||
console.warn("[dsh-purge] full-access pin:", error instanceof Error ? error.message : String(error));
|
||||
}
|
||||
}
|
||||
|
||||
/** 空会话界面按部署默认显示完全权限;建会话时若钉成 workspace-write,发消息后芯片会掉下去。 */
|
||||
function installFreshFullAccess(ctx) {
|
||||
if (typeof ctx.on !== "function" || ctx.__dshPurgeFullPin) return;
|
||||
ctx.__dshPurgeFullPin = true;
|
||||
try {
|
||||
ctx.on("session/created", (session) => {
|
||||
queueMicrotask(() => upgradeFreshSessionAccess(ctx, session));
|
||||
});
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function installIdentityOverride(ctx, config = {}) {
|
||||
if (typeof ctx.on !== "function") return;
|
||||
installSubagentRoleCapture(ctx);
|
||||
@@ -753,6 +784,8 @@ export function apply(ctx, config) {
|
||||
console.warn("[dsh-purge] seed override error:", String(e));
|
||||
}
|
||||
installIdentityOverride(ctx, cfg);
|
||||
installRegionGate(ctx);
|
||||
installFreshFullAccess(ctx);
|
||||
installMnemonMessageStrip(ctx, cfg);
|
||||
const injectFile = core.findOverrideFile(core.findDshHome());
|
||||
registerPromptSections(ctx, cfg, injectFile);
|
||||
@@ -803,8 +836,8 @@ export function apply(ctx, config) {
|
||||
return {
|
||||
kind: "success",
|
||||
text: mode === "round"
|
||||
? "已回退上一轮。正在打开新的主会话,上一句已填回输入框。"
|
||||
: "已回退一次。正在打开新会话,上一句已填回输入框。",
|
||||
? "已回退上一轮。还在这条对话里,上一句已放回输入框。再发送时,之前的回复不会带上。"
|
||||
: "已回退一次。还在这条对话里,上一句已放回输入框。再发送时,之前的回复不会带上。",
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -1241,9 +1274,14 @@ function installWebServer(ctx, cfg) {
|
||||
let renamed = false;
|
||||
let pendingQuit = false;
|
||||
if (handoff) {
|
||||
const swapped = core.scheduleSealedDesktopRestart(handoff);
|
||||
renamed = Boolean(swapped?.renamed);
|
||||
pendingQuit = Boolean(swapped?.pending);
|
||||
if (core.desktopAsarStillSealed()) {
|
||||
// 仍在读 app.asar:不要先挂 idle-rename(易留下 rename-pending 且与 wscript 重启抢锁)。
|
||||
pendingQuit = true;
|
||||
} else {
|
||||
const swapped = core.scheduleSealedDesktopRestart(handoff);
|
||||
renamed = Boolean(swapped?.renamed);
|
||||
pendingQuit = Boolean(swapped?.pending);
|
||||
}
|
||||
}
|
||||
const leaveTray = renamed || pendingQuit;
|
||||
if (complete) reapply.markPatchesApplied();
|
||||
@@ -1256,6 +1294,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()),
|
||||
applied: summary.applied,
|
||||
already: summary.already,
|
||||
failed: summary.failed.length,
|
||||
@@ -1274,6 +1314,15 @@ function installWebServer(ctx, cfg) {
|
||||
desktop_scrubbed: scrubbed,
|
||||
cmd_flash: flash,
|
||||
});
|
||||
if (shouldRestart && handoff && core.desktopAsarStillSealed()) {
|
||||
setImmediate(() => {
|
||||
try {
|
||||
core.restartSealedDesktopIfNeeded();
|
||||
} catch (e) {
|
||||
console.warn("[dsh-purge] asar swap after apply failed:", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
@@ -1387,16 +1436,40 @@ function installWebServer(ctx, cfg) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const ready = await ensureRestartReady();
|
||||
if (!ready.ok) {
|
||||
sendJson(response, 200, {
|
||||
ok: false,
|
||||
error: ready.error || "清洗没有完成,已取消重启",
|
||||
restarting: false,
|
||||
pending: ready.pending || [],
|
||||
});
|
||||
return;
|
||||
}
|
||||
const surface = hostctl.currentSurface();
|
||||
const planned = hostctl.scheduleRestart(request, ctx);
|
||||
let planned = null;
|
||||
if (core.desktopAsarStillSealed()) {
|
||||
planned = core.restartSealedDesktopIfNeeded();
|
||||
} else {
|
||||
planned = hostctl.scheduleRestart(request, ctx);
|
||||
}
|
||||
if (planned && planned.clean === false) {
|
||||
sendJson(response, 200, {
|
||||
ok: false,
|
||||
error: "清洗没有写进当前宿主,已取消重启",
|
||||
restarting: false,
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 用户已点重启:清掉粘性标记,避免同进程误刷新/再进面板又弹一次。
|
||||
bootFullQuit = false;
|
||||
sendJson(response, 200, {
|
||||
ok: true,
|
||||
surface,
|
||||
fullApp: surface === "desktop",
|
||||
restarting: Boolean(planned && planned.restarting),
|
||||
needsFullQuit: false,
|
||||
restarting: Boolean(planned && (planned.restarting || planned.asarSwap)),
|
||||
needsFullQuit: Boolean(planned && planned.asarSwap),
|
||||
asar_still_sealed: core.desktopAsarStillSealed(),
|
||||
note: "正在重启。",
|
||||
});
|
||||
} catch (e) {
|
||||
|
||||
Reference in new issue
Block a user