mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 05:10:22 +08:00
fix: 回退只撤最后一轮,提示词只保留一份。
解包改到旁边的目录,失败时放回上一份。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2929bb783b
commit
3ca5b1ec63
8 files changed
+354
-45
No files matched your search
@@ -130,7 +130,7 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
const TARGETS = ["AGENTS.md", "CLAUDE.md"];
|
||||
const PATCH_GROUPS = [
|
||||
{ key: "prompt", ids: [1, 2, 3, 4, 5, 25, 26, 27, 28, 32, 43, 50, 53, 54] },
|
||||
{ key: "code", ids: [6, 7, 8, 42, 44, 45, 46, 51, 52] },
|
||||
{ key: "code", ids: [6, 7, 8, 42, 44, 45, 46, 51, 52, 55, 56] },
|
||||
{ key: "engine", ids: [9, 10, 11, 12, 13, 14, 15, 16, 35] },
|
||||
{ key: "tools", ids: [17, 18, 19, 21, 22, 23, 24, 29, 30, 31, 33, 34, 36, 37, 48] },
|
||||
{ key: "compat", ids: [38, 39, 40] },
|
||||
@@ -378,6 +378,8 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
"patch.52": "会话日志默认不上传",
|
||||
"patch.53": "网页提示去掉禁止另开服务器",
|
||||
"patch.54": "交付物提示去掉多余禁止",
|
||||
"patch.55": "每一步替换第一条系统提示",
|
||||
"patch.56": "回退只藏被撤掉的那一轮",
|
||||
"status.unlocated": "未定位",
|
||||
"metric.unlocated": "还没定位到本机 Harness",
|
||||
"rewind.label": "回退",
|
||||
@@ -646,6 +648,8 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
"patch.52": "Session log upload off by default",
|
||||
"patch.53": "Web prompt drops do-not-start",
|
||||
"patch.54": "Deliverable prompt drops extra prohibition",
|
||||
"patch.55": "Replace the first system prompt every step",
|
||||
"patch.56": "Rewind hides only the cut range",
|
||||
"status.unlocated": "Not located",
|
||||
"metric.unlocated": "This install was not found yet",
|
||||
"rewind.label": "Undo",
|
||||
|
||||
+154
-6
@@ -11,7 +11,16 @@ import {
|
||||
revertHideConsoleFromBin,
|
||||
} from "./hide-console.js";
|
||||
import * as desktop from "./desktop.js";
|
||||
import { asarArchiveIsFile, extractAsar, hostFs, overlayAsarUnpacked, renameAsarAside } from "./extract-asar.js";
|
||||
import {
|
||||
asarArchiveIsFile,
|
||||
commitExtractedDir,
|
||||
extractAsarToSide,
|
||||
hostFs,
|
||||
newestPreservedDir,
|
||||
overlayAsarUnpacked,
|
||||
renameAsarAside,
|
||||
restoreExtractedDir,
|
||||
} from "./extract-asar.js";
|
||||
import { adapterFor, detectSurface } from "./surface.js";
|
||||
import { openSlot } from "./table-read.js";
|
||||
|
||||
@@ -1626,22 +1635,39 @@ export function sealedAsarHandoff(input = {}) {
|
||||
return { exe, asar, swapAsar: true };
|
||||
}
|
||||
|
||||
/** 官方 app.asar 解开到 resources/app。点「应用」时调用,不要求用户先跑脚本。 */
|
||||
/** 官方 app.asar 解开到 resources/app。已有解开目录时直接用,不重新解包。 */
|
||||
export async function openSealedDesktopHost(options = {}) {
|
||||
const handoff = sealedAsarHandoff();
|
||||
if (!handoff) return { aiBase: null, swapAsar: false };
|
||||
if (!handoff) return { aiBase: null, swapAsar: false, extracted: false, prev: "", appDir: "" };
|
||||
const appDir = desktop.appDirForExecutable(handoff.exe);
|
||||
let aiBase = options.forceExtract ? null : aiBaseUnderAppDir(appDir);
|
||||
let extracted = false;
|
||||
let prev = "";
|
||||
if (!aiBase) {
|
||||
await extractAsar(handoff.asar, appDir);
|
||||
const side = extractAsarToSide(handoff.asar, appDir);
|
||||
overlayAsarUnpacked(handoff.asar, side.next);
|
||||
if (!aiBaseUnderAppDir(side.next)) {
|
||||
try { hostFs.rmSync(side.next, { recursive: true, force: true }); } catch { /* 半成品留在旁边,不覆盖正在用的目录 */ }
|
||||
throw new Error(`已解开 ${handoff.asar},但里面没有 @deepseek-ai`);
|
||||
}
|
||||
const committed = commitExtractedDir(appDir, side.next);
|
||||
prev = committed.prev || "";
|
||||
extracted = true;
|
||||
aiBase = aiBaseUnderAppDir(appDir);
|
||||
} else {
|
||||
overlayAsarUnpacked(handoff.asar, appDir);
|
||||
}
|
||||
overlayAsarUnpacked(handoff.asar, appDir);
|
||||
if (!aiBase) {
|
||||
if (prev) restoreExtractedDir(appDir, prev);
|
||||
throw new Error(`已解开 ${handoff.asar},但 ${appDir} 下没有 @deepseek-ai`);
|
||||
}
|
||||
resetPathMemo();
|
||||
return { aiBase, swapAsar: true, exe: handoff.exe, asar: handoff.asar };
|
||||
return { aiBase, swapAsar: true, exe: handoff.exe, asar: handoff.asar, extracted, prev, appDir };
|
||||
}
|
||||
|
||||
export function restoreSealedExtract(appDir, prev) {
|
||||
const saved = prev || newestPreservedDir(appDir);
|
||||
return restoreExtractedDir(appDir, saved);
|
||||
}
|
||||
|
||||
export function scheduleSealedDesktopRestart(handoff) {
|
||||
@@ -3592,6 +3618,122 @@ const NEW_TOOL_PATCHES = [
|
||||
],
|
||||
markers: ["[dsh-purge] deliverable prohibition removed"],
|
||||
},
|
||||
{
|
||||
id: 55,
|
||||
name: "SYSTEM_PROMPT_REPLACE_HEAD",
|
||||
layer: "代码",
|
||||
layer_en: "Code",
|
||||
desc: "每一步用当前整段系统提示替换第一条,避免旧提示被钉住 / replace the first system prompt every step",
|
||||
file: ["agent-loop"],
|
||||
replacements: [
|
||||
{
|
||||
pattern: 'inHistory: preparedCall?.systemPromptUpdate === "in-history",',
|
||||
replace: "inHistory: false, // [dsh-purge] system prompt replaces the first node",
|
||||
},
|
||||
],
|
||||
markers: ["[dsh-purge] system prompt replaces the first node"],
|
||||
},
|
||||
{
|
||||
id: 56,
|
||||
name: "REWIND_HIDE_CUT_ONLY",
|
||||
layer: "代码",
|
||||
layer_en: "Code",
|
||||
desc: "回退只藏被撤掉的那一轮 / rewind hides only the cut range",
|
||||
file: ["conversation-client"],
|
||||
optional: true,
|
||||
replacements: [
|
||||
{
|
||||
pattern:
|
||||
"\t\tfunction rewindVisibleEntries(entries) {\n" +
|
||||
"\t\t\tconst list = Array.isArray(entries) ? entries : [...entries];\n" +
|
||||
"\t\t\tlet latest = null;\n" +
|
||||
"\t\t\tfor (const entry of list) {\n" +
|
||||
"\t\t\t\tconst event = entry && entry.event;\n" +
|
||||
"\t\t\t\tif (!isRewindMarker(event)) continue;\n" +
|
||||
"\t\t\t\tif (latest === null || event.seq > latest.seq) latest = event;\n" +
|
||||
"\t\t\t}\n" +
|
||||
"\t\t\tif (latest === null) return list;\n" +
|
||||
"\t\t\tconst hidden = /* @__PURE__ */ new Set();\n" +
|
||||
"\t\t\tif (Array.isArray(latest.sourceEventSeqs)) {\n" +
|
||||
"\t\t\t\tfor (const seq of latest.sourceEventSeqs) hidden.add(seq);\n" +
|
||||
"\t\t\t}\n" +
|
||||
"\t\t\thidden.add(latest.seq);\n" +
|
||||
"\t\t\treturn list.filter((entry) => {\n" +
|
||||
"\t\t\t\tconst event = entry && entry.event;\n" +
|
||||
"\t\t\t\tif (!event || hidden.has(event.seq)) return false;\n" +
|
||||
"\t\t\t\tif (event.seq > latest.seq) return true;\n" +
|
||||
"\t\t\t\treturn event.type === \"system/message\";\n" +
|
||||
"\t\t\t});\n" +
|
||||
"\t\t}",
|
||||
replace:
|
||||
"\t\tfunction rewindVisibleEntries(entries) {\n" +
|
||||
"\t\t\tconst list = Array.isArray(entries) ? entries : [...entries];\n" +
|
||||
"\t\t\tconst hidden = /* @__PURE__ */ new Set();\n" +
|
||||
"\t\t\tfor (const entry of list) {\n" +
|
||||
"\t\t\t\tconst event = entry && entry.event;\n" +
|
||||
"\t\t\t\tif (!isRewindMarker(event)) continue;\n" +
|
||||
"\t\t\t\thidden.add(event.seq);\n" +
|
||||
"\t\t\t\tif (Array.isArray(event.sourceEventSeqs)) {\n" +
|
||||
"\t\t\t\t\tfor (const seq of event.sourceEventSeqs) hidden.add(seq);\n" +
|
||||
"\t\t\t\t}\n" +
|
||||
"\t\t\t}\n" +
|
||||
"\t\t\tif (hidden.size === 0) return list;\n" +
|
||||
"\t\t\treturn list.filter((entry) => {\n" +
|
||||
"\t\t\t\tconst event = entry && entry.event;\n" +
|
||||
"\t\t\t\treturn !!event && !hidden.has(event.seq);\n" +
|
||||
"\t\t\t});\n" +
|
||||
"\t\t} // [dsh-purge] rewind hides only the cut range",
|
||||
},
|
||||
{
|
||||
pattern:
|
||||
"\t\t/**\n" +
|
||||
"\t\t* Session-owned incremental engine that assembles business Contexts from a\n" +
|
||||
"\t\t* contiguous Event window and materializes registered view snapshots.\n" +
|
||||
"\t\t*/\n" +
|
||||
"\t\tvar ConversationNodeAssembler = class {",
|
||||
replace:
|
||||
"\t\t/**\n" +
|
||||
"\t\t* Session-owned incremental engine that assembles business Contexts from a\n" +
|
||||
"\t\t* contiguous Event window and materializes registered view snapshots.\n" +
|
||||
"\t\t*/\n" +
|
||||
"\t\tfunction isRewindMarker(event) {\n" +
|
||||
"\t\t\tif (!event || event.type !== \"user/message\") return false;\n" +
|
||||
"\t\t\tconst id = event.data && event.data.id;\n" +
|
||||
"\t\t\tif (typeof id !== \"string\" || id.indexOf(\"rewind-\") !== 0) return false;\n" +
|
||||
"\t\t\tconst op = event.surfaceOp;\n" +
|
||||
"\t\t\treturn !!op && op !== \"append\" && op.op === \"replace\";\n" +
|
||||
"\t\t}\n" +
|
||||
"\t\tfunction rewindVisibleEntries(entries) {\n" +
|
||||
"\t\t\tconst list = Array.isArray(entries) ? entries : [...entries];\n" +
|
||||
"\t\t\tconst hidden = /* @__PURE__ */ new Set();\n" +
|
||||
"\t\t\tfor (const entry of list) {\n" +
|
||||
"\t\t\t\tconst event = entry && entry.event;\n" +
|
||||
"\t\t\t\tif (!isRewindMarker(event)) continue;\n" +
|
||||
"\t\t\t\thidden.add(event.seq);\n" +
|
||||
"\t\t\t\tif (Array.isArray(event.sourceEventSeqs)) {\n" +
|
||||
"\t\t\t\t\tfor (const seq of event.sourceEventSeqs) hidden.add(seq);\n" +
|
||||
"\t\t\t\t}\n" +
|
||||
"\t\t\t}\n" +
|
||||
"\t\t\tif (hidden.size === 0) return list;\n" +
|
||||
"\t\t\treturn list.filter((entry) => {\n" +
|
||||
"\t\t\t\tconst event = entry && entry.event;\n" +
|
||||
"\t\t\t\treturn !!event && !hidden.has(event.seq);\n" +
|
||||
"\t\t\t});\n" +
|
||||
"\t\t} // [dsh-purge] rewind hides only the cut range\n" +
|
||||
"\t\tvar ConversationNodeAssembler = class {",
|
||||
},
|
||||
{
|
||||
pattern:
|
||||
"\t\t\treplaceWindow(entries, hasMore) {\n" +
|
||||
"\t\t\t\tthis.contexts.clear();",
|
||||
replace:
|
||||
"\t\t\treplaceWindow(entries, hasMore) {\n" +
|
||||
"\t\t\t\tentries = rewindVisibleEntries(entries);\n" +
|
||||
"\t\t\t\tthis.contexts.clear();",
|
||||
},
|
||||
],
|
||||
markers: ["[dsh-purge] rewind hides only the cut range"],
|
||||
},
|
||||
];
|
||||
|
||||
export const ALL_PATCHES = [...PATCHES, ...CODE_PATCHES, ...ENGINE_PATCHES, ...NEW_TOOL_PATCHES];
|
||||
@@ -4000,6 +4142,12 @@ export function targetFiles(aiBase, dshHome = findDshHome()) {
|
||||
"deliverables-prompt": allExisting([
|
||||
...nestedPkg(aiBase, "dsh-client-ui-deliverables", "lib", "index.js"),
|
||||
]),
|
||||
"agent-loop": allExisting([
|
||||
...nestedPkg(aiBase, "dsh-agent-loop", "lib", "index.js"),
|
||||
]),
|
||||
"conversation-client": allExisting([
|
||||
...nestedPkg(aiBase, "dsh-client-ui-conversation", "lib", "client.js"),
|
||||
]),
|
||||
};
|
||||
return filterTargetFiles(files);
|
||||
}
|
||||
|
||||
@@ -224,6 +224,91 @@ export function overlayAsarUnpacked(asarPath, destDir) {
|
||||
return { copied };
|
||||
}
|
||||
|
||||
function discardDir(dir) {
|
||||
if (!dir) return;
|
||||
try { diskFs().rmSync(dir, { recursive: true, force: true }); } catch { /* 残留目录下次再清 */ }
|
||||
}
|
||||
|
||||
/** 解到目标旁边的目录。失败时删掉半成品,不碰已经在用的那一份。 */
|
||||
export function extractAsarToSide(asarPath, destDir) {
|
||||
const next = `${destDir}.dshpurge-next`;
|
||||
discardDir(next);
|
||||
try {
|
||||
const count = extractAsar(asarPath, next);
|
||||
return { next, count };
|
||||
} catch (error) {
|
||||
discardDir(next);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/** 解开完成之后才把旧目录挪走、新目录换上。换不上就把旧目录放回去。 */
|
||||
export function commitExtractedDir(destDir, nextDir) {
|
||||
const fs = diskFs();
|
||||
if (!nextDir || !fs.existsSync(nextDir)) throw new Error("解包目录不存在");
|
||||
let prev = "";
|
||||
if (fs.existsSync(destDir)) {
|
||||
prev = `${destDir}.dshpurge-prev`;
|
||||
if (fs.existsSync(prev)) prev = `${destDir}.dshpurge-prev-${Date.now()}`;
|
||||
fs.renameSync(destDir, prev);
|
||||
}
|
||||
try {
|
||||
fs.renameSync(nextDir, destDir);
|
||||
} catch (error) {
|
||||
if (prev && !fs.existsSync(destDir)) {
|
||||
try { fs.renameSync(prev, destDir); } catch { /* 调用方再试一次放回 */ }
|
||||
}
|
||||
discardDir(nextDir);
|
||||
throw error;
|
||||
}
|
||||
return { prev };
|
||||
}
|
||||
|
||||
/** 升级脚本事先挪走的旧目录。补丁没写完时用它放回去。 */
|
||||
export function newestPreservedDir(destDir) {
|
||||
const fs = diskFs();
|
||||
const parent = path.dirname(destDir);
|
||||
const prefix = `${path.basename(destDir)}.dshpurge-prev`;
|
||||
let names = [];
|
||||
try { names = fs.readdirSync(parent); } catch { return ""; }
|
||||
let best = "";
|
||||
let bestTime = -1;
|
||||
for (const name of names) {
|
||||
if (name !== prefix && !name.startsWith(`${prefix}-`)) continue;
|
||||
const full = path.join(parent, name);
|
||||
let time = 0;
|
||||
try { time = fs.statSync(full).mtimeMs; } catch { continue; }
|
||||
if (time >= bestTime) {
|
||||
best = full;
|
||||
bestTime = time;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/** 补丁没写完时,把刚才挪走的旧目录放回原处。 */
|
||||
export function restoreExtractedDir(destDir, prev) {
|
||||
const fs = diskFs();
|
||||
if (!prev || !fs.existsSync(prev)) return false;
|
||||
let failed = "";
|
||||
if (fs.existsSync(destDir)) {
|
||||
failed = `${destDir}.dshpurge-failed-${Date.now()}`;
|
||||
fs.renameSync(destDir, failed);
|
||||
}
|
||||
try {
|
||||
fs.renameSync(prev, destDir);
|
||||
} catch (error) {
|
||||
if (failed && !fs.existsSync(destDir)) {
|
||||
try { fs.renameSync(failed, destDir); } catch { /* 旧目录仍在 prev */ }
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (failed) {
|
||||
try { fs.rmSync(failed, { recursive: true, force: true }); } catch { /* 坏目录留着不影响已放回的那一份 */ }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function extractAsar(asarPath, destDir) {
|
||||
const afs = archiveFs();
|
||||
const fd = afs.openSync(asarPath, "r");
|
||||
|
||||
+22
-5
@@ -98,6 +98,10 @@ function stripHarnessPersona(text) {
|
||||
/You are a coding agent powered by the \{\{model\}\} model\. Your working directory is \{\{cwd\}\}\.?\s*/g,
|
||||
"Working directory: {{cwd}}. ",
|
||||
)
|
||||
.replace(
|
||||
/The DeepSeek Harness implementation checkout is at [\s\S]*?Use this checkout only to inspect or extend DSH itself\.?\s*/g,
|
||||
"",
|
||||
)
|
||||
.replace(
|
||||
/You are a coding agent powered by the [^\n.]+ model, running on the DeepSeek Harness\.?\s*/g,
|
||||
"",
|
||||
@@ -140,19 +144,34 @@ function withoutRedteamPreset(sections) {
|
||||
});
|
||||
}
|
||||
|
||||
/** 每次组装都把提示词放到最前。已经嵌在后面的同一份正文拆出来,避免宿主段落挡在前面。 */
|
||||
const OWN_SERVER_MARK = "OWN SERVERS — EXACT LIST ONLY";
|
||||
|
||||
function injectNeedles(inject) {
|
||||
const text = String(inject || "").trim();
|
||||
if (!text) return [];
|
||||
const at = text.indexOf(OWN_SERVER_MARK);
|
||||
const base = at >= 0 ? text.slice(0, at).trim() : "";
|
||||
const needles = [text];
|
||||
if (base && base !== text) needles.push(base);
|
||||
return needles;
|
||||
}
|
||||
|
||||
/** 每次组装都把提示词放到最前。后面段落里的同一份正文拆掉,避免再拼一份。 */
|
||||
function leadWithInject(sections, inject) {
|
||||
const text = String(inject || "").trim();
|
||||
if (!text) return sections;
|
||||
const needles = injectNeedles(text);
|
||||
const rest = [];
|
||||
for (const section of sections) {
|
||||
if (!section || section.name === "dsh-purge") continue;
|
||||
const body = typeof section.text === "string" ? section.text : "";
|
||||
if (!body.includes(text)) {
|
||||
if (!needles.some((needle) => body.includes(needle))) {
|
||||
rest.push(section);
|
||||
continue;
|
||||
}
|
||||
const next = body.split(text).join("").replace(/\n{3,}/g, "\n\n").trim();
|
||||
let next = body;
|
||||
for (const needle of needles) next = next.split(needle).join("");
|
||||
next = next.replace(/\n{3,}/g, "\n\n").trim();
|
||||
if (!next) continue;
|
||||
rest.push({ ...section, text: next });
|
||||
}
|
||||
@@ -418,8 +437,6 @@ function foldInjectIntoPersona(sections, injectText) {
|
||||
return [{ name: "deployment:persona-prefix", text: inject, order }, ...out];
|
||||
}
|
||||
|
||||
const OWN_SERVER_MARK = "OWN SERVERS — EXACT LIST ONLY";
|
||||
|
||||
function withOwnServers(injectText) {
|
||||
const text = String(injectText || "").trim();
|
||||
if (!text) return text;
|
||||
|
||||
+20
-2
@@ -77,6 +77,7 @@ export async function reapplyInstalled(options = {}) {
|
||||
core.applyRuntimeEnv();
|
||||
let aiBase = null;
|
||||
let handoff = null;
|
||||
let freshExtract = null;
|
||||
try {
|
||||
const state = await core.gatherState();
|
||||
aiBase = state.ai_base;
|
||||
@@ -88,6 +89,7 @@ export async function reapplyInstalled(options = {}) {
|
||||
const opened = await core.openSealedDesktopHost({ forceExtract: options.forceExtract === true });
|
||||
if (opened.aiBase) aiBase = opened.aiBase;
|
||||
if (opened.swapAsar) handoff = opened;
|
||||
if (opened.extracted) freshExtract = opened;
|
||||
} catch (e) {
|
||||
return { ok: false, error: String(e && e.message ? e.message : e) };
|
||||
}
|
||||
@@ -95,14 +97,30 @@ export async function reapplyInstalled(options = {}) {
|
||||
handoff = core.sealedAsarHandoff();
|
||||
}
|
||||
if (!aiBase) return { ok: false, error: core.missingAiBaseMessage() };
|
||||
const reverted = await core.revertAll(aiBase);
|
||||
await core.revertAllShims();
|
||||
// 刚解开的是官方原件。已经在用的目录仍先还原再按这一版重打。
|
||||
const reverted = freshExtract ? { reverted: [] } : await core.revertAll(aiBase);
|
||||
if (!freshExtract) await core.revertAllShims();
|
||||
await core.backupAll(aiBase);
|
||||
const report = await core.applyPatches(aiBase);
|
||||
await core.patchAllShims();
|
||||
const flash = core.silenceCmdFlash(aiBase);
|
||||
const summary = core.summarizeApplyReport(report);
|
||||
const clean = summary.failed.length === 0 && flash.ok === true && core.hostCleanMarkersPresent(aiBase);
|
||||
if (!clean && freshExtract) {
|
||||
const restored = core.restoreSealedExtract(freshExtract.appDir, freshExtract.prev);
|
||||
return {
|
||||
ok: false,
|
||||
error: restored ? "清洗没有完成,已放回上一份解开目录" : "清洗没有完成",
|
||||
restored,
|
||||
version: pluginVersion(),
|
||||
reverted: 0,
|
||||
applied: summary.applied || 0,
|
||||
failed: summary.failed.length,
|
||||
renamed: false,
|
||||
needsFullQuit: false,
|
||||
cmdFlashOk: flash.ok === true,
|
||||
};
|
||||
}
|
||||
if (clean) writeAppliedStamp(installedRev());
|
||||
let renamed = false;
|
||||
if (clean && handoff?.asar && options.scheduleSwap !== false) {
|
||||
|
||||
+49
-30
@@ -116,20 +116,23 @@ export function pendingInboxInserts(events) {
|
||||
return ids;
|
||||
}
|
||||
|
||||
export function lastTurnSubagentIds(events) {
|
||||
export function lastTurnSubagentIds(events, fromSeq = -1) {
|
||||
const list = Array.isArray(events) ? events : [];
|
||||
let lastStart = -1;
|
||||
for (let i = list.length - 1; i >= 0; i -= 1) {
|
||||
if (list[i]?.type === "turn/start") {
|
||||
lastStart = list[i].seq;
|
||||
break;
|
||||
let floor = Number(fromSeq);
|
||||
if (!Number.isFinite(floor) || floor < 0) {
|
||||
floor = -1;
|
||||
for (let i = list.length - 1; i >= 0; i -= 1) {
|
||||
if (list[i]?.type === "turn/start") {
|
||||
floor = list[i].seq;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lastStart < 0) return [];
|
||||
if (floor < 0) return [];
|
||||
const ids = [];
|
||||
const seen = new Set();
|
||||
for (const event of list) {
|
||||
if (!event || event.seq < lastStart) continue;
|
||||
if (!event || event.seq < floor) continue;
|
||||
if (event.type !== "subagent/catalog" && event.type !== "subagent/descriptor") continue;
|
||||
const id = event.data?.childId || event.data?.sessionId || event.data?.id;
|
||||
if (!id || seen.has(id)) continue;
|
||||
@@ -141,18 +144,28 @@ export function lastTurnSubagentIds(events) {
|
||||
|
||||
export function planRewind(events) {
|
||||
const list = Array.isArray(events) ? events : [];
|
||||
const turnStarts = list.filter((event) => event?.type === "turn/start");
|
||||
const lastStart = turnStarts[turnStarts.length - 1];
|
||||
const usersInLastTurn = lastStart
|
||||
? list.filter((event) => isHumanUserEvent(event) && event.seq >= lastStart.seq)
|
||||
: [];
|
||||
const lastUser = usersInLastTurn[0]
|
||||
|| list.filter(isHumanUserEvent).at(-1);
|
||||
const lastUser = list.filter(isHumanUserEvent).at(-1);
|
||||
if (!lastUser) {
|
||||
return { ok: false, error: "没有可回退的上一句" };
|
||||
}
|
||||
const text = userTextFromEvent(lastUser);
|
||||
return { ok: true, mode: "input", text };
|
||||
return {
|
||||
ok: true,
|
||||
mode: "input",
|
||||
text: userTextFromEvent(lastUser),
|
||||
fromSeq: lastUser.seq,
|
||||
};
|
||||
}
|
||||
|
||||
/** 只拿掉 fromSeq 及之后的表面节点。系统提示停在节点 0 时不动它。 */
|
||||
export function cutSurfaceNodes(nodes, fromSeq, eventAt) {
|
||||
const list = (Array.isArray(nodes) ? nodes : []).map((seq) => Number(seq));
|
||||
const floor = Number(fromSeq);
|
||||
let startIdx = list.findIndex((seq) => Number.isFinite(floor) ? seq >= floor : true);
|
||||
if (startIdx < 0) return [];
|
||||
const lookup = typeof eventAt === "function" ? eventAt : () => null;
|
||||
if (startIdx === 0 && lookup(list[0])?.type === "system/message") startIdx = 1;
|
||||
if (startIdx >= list.length) return [];
|
||||
return list.slice(startIdx);
|
||||
}
|
||||
|
||||
function rememberDraft(sessionId, text, parentId) {
|
||||
@@ -393,8 +406,8 @@ function derivedStillContains(session, text) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 留在当前会话。把系统提示后面的可见记录从模型请求里拿掉,不另开会话。 */
|
||||
function clearModelHistory(session, text) {
|
||||
/** 留在当前会话。只把 fromSeq 起的这一轮从模型请求里拿掉,更早的轮次留着。 */
|
||||
function clearModelHistory(session, text, fromSeq = 0) {
|
||||
if (!session || typeof session.append !== "function" || !session.surface) {
|
||||
throw new Error("没有清掉之前发送的内容");
|
||||
}
|
||||
@@ -407,15 +420,13 @@ function clearModelHistory(session, text) {
|
||||
}
|
||||
const events = typeof session.snapshotEvents === "function" ? session.snapshotEvents() : [];
|
||||
const eventAt = (seq) => events.find((event) => event?.seq === seq) || session.eventAt?.(seq);
|
||||
let startIdx = 0;
|
||||
if (eventAt(nodes[0])?.type === "system/message") startIdx = 1;
|
||||
if (startIdx >= nodes.length) {
|
||||
const shadowed = cutSurfaceNodes(nodes, fromSeq, eventAt);
|
||||
if (shadowed.length === 0) {
|
||||
if (derivedStillContains(session, text)) {
|
||||
throw new Error("没有清掉之前发送的内容");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
const shadowed = nodes.slice(startIdx).map((seq) => Number(seq));
|
||||
const startSeq = shadowed[0];
|
||||
const endSeq = shadowed[shadowed.length - 1];
|
||||
if (!Number.isSafeInteger(startSeq) || !Number.isSafeInteger(endSeq)) {
|
||||
@@ -430,32 +441,32 @@ function clearModelHistory(session, text) {
|
||||
surfaceOp: { op: "replace", startSeq, endSeq },
|
||||
sourceEventSeqs: shadowed,
|
||||
});
|
||||
if (derivedStillContains(session, text)) {
|
||||
const left = new Set((session.surface.nodes || []).map((seq) => Number(seq)));
|
||||
if (shadowed.some((seq) => left.has(seq))) {
|
||||
throw new Error("没有清掉之前发送的内容");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async function rewindOneSession(ctx, controller, sessionId) {
|
||||
async function rewindOneSession(ctx, controller, sessionId, options = {}) {
|
||||
let log = await readSessionLog(ctx, sessionId);
|
||||
if (lastOpenTurn(log.events)) {
|
||||
try { ignoreSettled(controller.cancel?.({ sessionId })); } catch { /* ignore */ }
|
||||
log = await waitTurnClosed(ctx, sessionId);
|
||||
}
|
||||
const plan = planRewind(log.events);
|
||||
const plan = options.full
|
||||
? { ok: true, mode: "input", text: lastHumanUserText(log.events), fromSeq: 0 }
|
||||
: planRewind(log.events);
|
||||
if (!plan.ok) return plan;
|
||||
|
||||
await disarmRewoundSession(ctx, sessionId);
|
||||
const session = liveSession(ctx, sessionId);
|
||||
let cleared = false;
|
||||
try {
|
||||
cleared = clearModelHistory(session, plan.text);
|
||||
cleared = clearModelHistory(session, plan.text, plan.fromSeq);
|
||||
} catch (err) {
|
||||
return { ok: false, error: errorText(err) };
|
||||
}
|
||||
if (derivedStillContains(session, plan.text)) {
|
||||
return { ok: false, error: "没有清掉之前发送的内容" };
|
||||
}
|
||||
|
||||
const rec = rememberDraft(sessionId, plan.text, sessionId);
|
||||
return {
|
||||
@@ -499,8 +510,16 @@ export async function applyRewind(ctx, sessionId, mode) {
|
||||
if (sessionId !== target.sessionId) {
|
||||
try { ignoreSettled(controller.cancel?.({ sessionId })); } catch { /* ignore */ }
|
||||
}
|
||||
const preview = planRewind(target.log?.events || []);
|
||||
const children = preview.ok ? lastTurnSubagentIds(target.log.events, preview.fromSeq) : [];
|
||||
const result = await rewindOneSession(ctx, controller, target.sessionId);
|
||||
if (!result.ok) return result;
|
||||
for (const childId of children) {
|
||||
if (!childId || childId === target.sessionId) continue;
|
||||
if (!liveSession(ctx, childId)) continue;
|
||||
const child = await rewindOneSession(ctx, controller, childId, { full: true });
|
||||
if (!child.ok) return child;
|
||||
}
|
||||
return { ...result, sourceId: sessionId, mode: "round" };
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dsh-purge",
|
||||
"version": "1.1.43",
|
||||
"version": "1.1.44",
|
||||
"description": "DeepSeek Harness 网络安全红队工具:本机官方 Harness 受控评测与清洗,内嵌演练台;不同模型可换不同提示词;默认提示词面向国模「小码酱」。求 Star ⭐",
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
# 1.1.44
|
||||
|
||||
## 中文
|
||||
|
||||
- 版本升级到 1.1.44。
|
||||
- 官方桌面解开 `app.asar` 时先写到旁边的目录,解开完成才换上 `resources/app`。解包卡住或这一轮补丁没写完,原来的目录还在。
|
||||
- 平常重启不再重新解包。只有官方升级换了新归档时才解一次,然后像 Web 一样把补丁补回去。
|
||||
- 回退上一轮只撤掉最后一条已经发出的消息和它的回复,更早的轮次留在对话里,也从模型请求里拿掉。
|
||||
- 可编辑提示词整份放在系统提示最前,同一份正文不再拼第二遍。每一步用当前这一份替换第一条系统提示。
|
||||
|
||||
## English
|
||||
|
||||
- Version 1.1.44.
|
||||
- Unpacking `app.asar` writes beside `resources/app` and swaps in only after the unpack finishes. If unpacking sticks or that round of patches does not finish, the previous directory stays.
|
||||
- A normal restart does not unpack again. A new archive from an official upgrade is unpacked once, then patches are applied the same way as on Web.
|
||||
- Rewinding the previous round removes only that sent message and its reply. Earlier rounds stay on screen and leave the next model request.
|
||||
- The editable prompt is placed once at the front of the system prompt. Each step replaces the first system prompt with that current text.
|
||||
|
||||
# 1.1.43
|
||||
|
||||
## 中文
|
||||
|
||||
Reference in new issue
Block a user