mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 11:00:22 +08:00
release: 1.1.84,尾部锚点与子代理审批接管,红队模式中文化,#194/#188
- 接线 lib/anchor.js:锚点物理追加到 system prompt sections 末尾, identity.js 支持 anchorText(对象/字符串两种入参),已有同名段先滤再追加; 锚点改为跟随操作者语言,删除写死的英文开头句(红队回答变英文的根因) - 接线 lib/subagent-control.js:子会话 approval/policy=never 改回 ask, 并在 approval/request 瀑布放行;anchorMode/subagentControl 提供回滚开关 - index.js:真实 anchorApplied / injectTail 判定、审批策略探针、iso 分支 注册锚点、applyHostHooks 注册锚点与子代理接管并统一释放 - office 技能自愈:core/desktop 增加 healOfficeSkillsRuntimeOnDisk,桌面端触发 - 红队模式中文化:preset 文案、角色名、五阶段、25 项计分规则与 POC 分类、 技能修复说明、环境适配提示、面板报错、成果报告与复现报告 - #194:hung-operator 改按内容变更缓存,加风暴/慢调用熔断;WeakMap 去重 同次 assemble 的重复改写;新增 #91 记录宿主信号退出的持久化日志 - #188(rc.4 专属部分):新增 #89 沙箱策略去品牌、#90 skill-badge 中文描述, 扩展 #32/#53 的 rc.3 变体与 marker,均 optional - 新增锚点接线与红队文案测试,81/81 通过
This commit is contained in:
1 parent
97c5de75fa
commit
624976ed1d
19 files changed
+1521
-847
No files matched your search
+134
-27
@@ -21,6 +21,8 @@ import {
|
||||
rewritePromptAssembly,
|
||||
} from "./identity.js";
|
||||
import { capabilityProtocolText, CAPABILITY_SECTION } from "./capability-protocol.js";
|
||||
import { registerAnchor, anchorTextFor, ANCHOR_SECTION } from "./anchor.js";
|
||||
import { installSubagentControl } from "./subagent-control.js";
|
||||
import { installSubagentRoleCapture, resolveSubagentRoleText } from "./redteam/role-section.js";
|
||||
import * as rewind from "./rewind.js";
|
||||
import * as continueRetry from "./continue-retry.js";
|
||||
@@ -77,6 +79,8 @@ const DEFAULTS = {
|
||||
// Codex 风格:教模型主动发现 skill / MCP;不改官方注册与工具面。
|
||||
capabilityProtocol: true,
|
||||
hooksDenyBypass: true,
|
||||
anchorMode: "auto", // "auto" | "off" —— off 立即关锚点,不用改代码
|
||||
subagentControl: true, // false 立即回滚子代理审批接管
|
||||
};
|
||||
|
||||
const log = (config, ...args) => {
|
||||
@@ -191,11 +195,15 @@ function injectAllowedNow(config) {
|
||||
}
|
||||
|
||||
/** 提示词框自改 > prompt-inject.md > 加密默认(见 rules.resolveInjectText)。 */
|
||||
// mtime 节流:400ms setInterval + 每 pre-step + 每 preset-selected + 每 readInjectFallback
|
||||
// 的高频触发下,disk sync + 3 次 hashOverride + 2 次模块级赋值一轮能点 20 次。
|
||||
// 短窗复用上次结果:home 没换 + 文件 mtime 没变 + 距上次 < 200ms,直接返回缓存。
|
||||
let hungCache = { home: "", mtime: 0, at: 0, resolved: null };
|
||||
const HUNG_CACHE_MS = 200;
|
||||
// 缓存按 (home, mtime, 提交版本) 失效,不再靠 200ms 时间窗:
|
||||
// host 每轮 refresh 都会调 __dshPurgeHungPrompt,时间窗一过就全量 sync + 解密 23KB,
|
||||
// 桌面宿主在这段时间里被拖住(#194)。只有文件或内存正文真的变了才重算。
|
||||
let hungCache = { home: "", mtime: 0, rev: -1, resolved: null };
|
||||
// 风暴熔断:正常一轮请求只有个位数调用;10s 内上百次说明有循环在踩,
|
||||
// 直接返回上一次结果,避免继续拖住宿主主线程。
|
||||
const HUNG_STORM_WINDOW_MS = 10000;
|
||||
const HUNG_STORM_LIMIT = 120;
|
||||
let hungStorm = { at: 0, calls: 0, tripped: false };
|
||||
function statInjectMtimeMs(home) {
|
||||
if (!home) return 0;
|
||||
try {
|
||||
@@ -206,14 +214,28 @@ function refreshHungOperator(dshHome = core.findDshHome()) {
|
||||
const home = core.pinDshHome() || dshHome || core.findDshHome();
|
||||
const now = Date.now();
|
||||
const mtimeBefore = statInjectMtimeMs(home);
|
||||
const rev = core.committedOverrideRevision();
|
||||
if (
|
||||
hungCache.resolved
|
||||
&& hungCache.home === home
|
||||
&& hungCache.mtime === mtimeBefore
|
||||
&& now - hungCache.at < HUNG_CACHE_MS
|
||||
&& hungCache.rev === rev
|
||||
) {
|
||||
return hungCache.resolved;
|
||||
}
|
||||
if (now - hungStorm.at > HUNG_STORM_WINDOW_MS) {
|
||||
hungStorm = { at: now, calls: 0, tripped: false };
|
||||
}
|
||||
hungStorm.calls += 1;
|
||||
if (hungStorm.calls > HUNG_STORM_LIMIT) {
|
||||
if (!hungStorm.tripped) {
|
||||
hungStorm.tripped = true;
|
||||
purgeLog("host.hungStorm", { calls: hungStorm.calls, windowMs: HUNG_STORM_WINDOW_MS });
|
||||
}
|
||||
if (hungCache.resolved) return hungCache.resolved;
|
||||
const pinned = core.hungOperatorPrompt() || "";
|
||||
if (pinned) return { text: pinned, source: "storm", ruleId: null };
|
||||
}
|
||||
try { core.syncPromptInjectOnDiskSync(home); } catch { /* 同步失败仍按 resolve 走 */ }
|
||||
const resolved = rules.resolveInjectText(home);
|
||||
// 兜底顺序:resolved.text > 加密默认 > 永不空串(避免二轮 fallbackInject 为空走 purge[] 残留)。
|
||||
@@ -226,8 +248,13 @@ function refreshHungOperator(dshHome = core.findDshHome()) {
|
||||
core.rememberCommittedOverride(home, text);
|
||||
}
|
||||
const out = { ...resolved, text: text || resolved.text };
|
||||
// 用 sync 之后的 mtime 存 cache,下次高频轮询(文件没被外部动)直接命中。
|
||||
hungCache = { home, mtime: statInjectMtimeMs(home), at: now, resolved: out };
|
||||
// 用 sync 之后的 mtime + 最新提交版本存 cache,下次高频轮询直接命中。
|
||||
hungCache = {
|
||||
home,
|
||||
mtime: statInjectMtimeMs(home),
|
||||
rev: core.committedOverrideRevision(),
|
||||
resolved: out,
|
||||
};
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -241,28 +268,54 @@ function installHungPromptGlobal() {
|
||||
// 2) hungOperatorPrompt — 上次挂上的内存钉子(进程生命周期)
|
||||
// 3) readPromptInjectFileSync — 直读磁盘 prompt-inject.md,绕过 sync/resolve
|
||||
// 4) defaultOverrideText — 加密槽解包,除非插件安装残缺否则非空
|
||||
// 超时熔断(#194):这是 host 主线程同步调用的函数,一次全量 resolve 若被磁盘/解密
|
||||
// 拖到秒级,宿主就整段挂住。连续慢调用后进入冷却期,只交内存里已挂上的正文。
|
||||
const HUNG_SLOW_MS = 1500;
|
||||
const HUNG_SLOW_COOLDOWN_MS = 30000;
|
||||
let hungSlowAt = 0;
|
||||
let hungDepth = 0;
|
||||
const fn = () => {
|
||||
const norm = (s) => peelChatmlEnvelope(String(s || "").replace(/\r\n/g, "\n"));
|
||||
// 重入保护:同一轮里再进来只回内存钉子,绝不再走磁盘。
|
||||
if (hungDepth > 0) return norm(core.hungOperatorPrompt());
|
||||
if (hungSlowAt && Date.now() - hungSlowAt < HUNG_SLOW_COOLDOWN_MS) {
|
||||
const pinned = norm(core.hungOperatorPrompt());
|
||||
if (pinned) {
|
||||
purgeLog("host.hungFn", { tier: "breaker", bytes: pinned.length });
|
||||
return pinned;
|
||||
}
|
||||
}
|
||||
let home = "";
|
||||
try { home = core.findDshHome(); } catch { home = ""; }
|
||||
hungDepth += 1;
|
||||
const started = Date.now();
|
||||
try {
|
||||
const text = norm(refreshHungOperator(home).text);
|
||||
if (text) { purgeLog("host.hungFn", { tier: "refresh", bytes: text.length }); return text; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "refresh", err: String(e?.message || e) }); }
|
||||
try {
|
||||
const hung = norm(core.hungOperatorPrompt());
|
||||
if (hung) { purgeLog("host.hungFn", { tier: "hung", bytes: hung.length }); return hung; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "hung", err: String(e?.message || e) }); }
|
||||
try {
|
||||
const disk = norm(core.readPromptInjectFileSync(home));
|
||||
if (disk) { purgeLog("host.hungFn", { tier: "disk", bytes: disk.length }); return disk; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "disk", err: String(e?.message || e) }); }
|
||||
try {
|
||||
const sealed = norm(core.defaultOverrideText());
|
||||
if (sealed) { purgeLog("host.hungFn", { tier: "sealed", bytes: sealed.length }); return sealed; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "sealed", err: String(e?.message || e) }); }
|
||||
purgeLog("host.hungFn", { tier: "empty", bytes: 0 });
|
||||
return "";
|
||||
try {
|
||||
const text = norm(refreshHungOperator(home).text);
|
||||
if (text) { purgeLog("host.hungFn", { tier: "refresh", bytes: text.length }); return text; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "refresh", err: String(e?.message || e) }); }
|
||||
try {
|
||||
const hung = norm(core.hungOperatorPrompt());
|
||||
if (hung) { purgeLog("host.hungFn", { tier: "hung", bytes: hung.length }); return hung; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "hung", err: String(e?.message || e) }); }
|
||||
try {
|
||||
const disk = norm(core.readPromptInjectFileSync(home));
|
||||
if (disk) { purgeLog("host.hungFn", { tier: "disk", bytes: disk.length }); return disk; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "disk", err: String(e?.message || e) }); }
|
||||
try {
|
||||
const sealed = norm(core.defaultOverrideText());
|
||||
if (sealed) { purgeLog("host.hungFn", { tier: "sealed", bytes: sealed.length }); return sealed; }
|
||||
} catch (e) { purgeLog("host.hungFn", { tier: "sealed", err: String(e?.message || e) }); }
|
||||
purgeLog("host.hungFn", { tier: "empty", bytes: 0 });
|
||||
return "";
|
||||
} finally {
|
||||
hungDepth -= 1;
|
||||
const cost = Date.now() - started;
|
||||
if (cost >= HUNG_SLOW_MS) {
|
||||
hungSlowAt = Date.now();
|
||||
purgeLog("host.hungFn", { tier: "slow", ms: cost });
|
||||
}
|
||||
}
|
||||
};
|
||||
globalThis.__dshPurgeHungPrompt = fn;
|
||||
return fn;
|
||||
@@ -519,7 +572,16 @@ function installMnemonMessageStrip(ctx, config = {}) {
|
||||
} catch { /* 死 ctx 时无 effect,靠下一次 apply 覆盖 */ }
|
||||
}
|
||||
|
||||
// assemble 可能被 guard 和 waterfall 各处理一次;同一对象只改写一次。
|
||||
const liveRewriteCache = new WeakMap();
|
||||
|
||||
function rewriteLiveAssembly(config, assembled, context, decision = { mode: "full", taskKind: "general" }) {
|
||||
// 同一份 assembly 可能同时经过 assemble guard 和 system-prompt/assemble 瀑布:
|
||||
// 第二次进来内容没变,没必要再走一遍 22k 正文处理。WeakMap 随对象回收,不累积。
|
||||
if (assembled && typeof assembled === "object") {
|
||||
const cached = liveRewriteCache.get(assembled);
|
||||
if (cached) return cached;
|
||||
}
|
||||
const enabled = config.enabled !== false;
|
||||
const redteam = enabled && isRedteamSession(context);
|
||||
const full = enabled ? readInjectFallback(context) : "";
|
||||
@@ -537,7 +599,6 @@ function rewriteLiveAssembly(config, assembled, context, decision = { mode: "ful
|
||||
}
|
||||
const effectiveFull = String(full || "").trim() ? full : localInject;
|
||||
const inject = effectiveFull;
|
||||
const anchorApplied = false;
|
||||
const hasInject = Boolean(String(inject || "").trim());
|
||||
let dropWorkspaceBaseline = false;
|
||||
if (enabled) {
|
||||
@@ -554,8 +615,20 @@ function rewriteLiveAssembly(config, assembled, context, decision = { mode: "ful
|
||||
context,
|
||||
roleText,
|
||||
dropWorkspaceBaseline,
|
||||
anchorText: enabled ? anchorTextFor(config) : "",
|
||||
});
|
||||
const anchorApplied = Array.isArray(rewritten?.sections)
|
||||
&& rewritten.sections.some((section) => String(section?.name || "") === ANCHOR_SECTION);
|
||||
const closerFollow = closerFollowBytes(rewritten?.sections);
|
||||
// 主会话审批策略探针:never 会让 decide() 在 approval/request 瀑布之前直接 rejected,
|
||||
// 这是「一直拒绝」最直接的证据,必须随每轮装配打出来。
|
||||
const approvalPolicy = (() => {
|
||||
try {
|
||||
const session = context?.agent?.session;
|
||||
const svc = context?.agent?.ctx?.get?.("approval");
|
||||
return typeof svc?.effectivePolicy === "function" ? svc.effectivePolicy(session) : "n/a";
|
||||
} catch { return "n/a"; }
|
||||
})();
|
||||
purgeLog("assemble.rewrite", {
|
||||
enabled,
|
||||
redteam,
|
||||
@@ -570,12 +643,19 @@ function rewriteLiveAssembly(config, assembled, context, decision = { mode: "ful
|
||||
cap: config.capabilityProtocol !== false,
|
||||
strip: config.stripMnemon !== false,
|
||||
policy,
|
||||
approval: approvalPolicy,
|
||||
dropWorkspaceBaseline,
|
||||
injectHead: Array.isArray(rewritten?.sections) && rewritten.sections[0]?.name === "dsh-purge",
|
||||
// 接上锚点之后最后一段的 name 是 dsh-purge:anchor,不是 dsh-purge。
|
||||
injectTail: Array.isArray(rewritten?.sections) && rewritten.sections.length > 0
|
||||
&& /^dsh-purge(:|$)/.test(String(rewritten.sections[rewritten.sections.length - 1]?.name || "")),
|
||||
chatml: Array.isArray(rewritten?.sections) && /<project_instructions>|<\|im_start\|>/i.test(String(rewritten.sections[0]?.text || "")),
|
||||
afterCloser: closerFollow,
|
||||
cwd,
|
||||
});
|
||||
if (assembled && typeof assembled === "object" && rewritten && typeof rewritten === "object") {
|
||||
liveRewriteCache.set(assembled, rewritten);
|
||||
}
|
||||
return rewritten;
|
||||
}
|
||||
|
||||
@@ -859,7 +939,11 @@ function installIdentityOverride(ctx, config = {}) {
|
||||
}
|
||||
try {
|
||||
const iso = (loopCtx && loopCtx.ctx) || loopCtx;
|
||||
if (iso && iso !== ctx) registerPromptSections(iso, config);
|
||||
if (iso && iso !== ctx) {
|
||||
registerPromptSections(iso, config);
|
||||
// 不补这一行,切换隔离上下文后锚点会消失。
|
||||
registerAnchor(iso, config);
|
||||
}
|
||||
} catch { /* 隔离层还没有 systemPrompt */ }
|
||||
wrapAssemble("prepareAssemble");
|
||||
return prompt || null;
|
||||
@@ -1647,6 +1731,15 @@ function applyHostHooks(ctx, cfg) {
|
||||
const home = core.pinDshHome() || core.findDshHome();
|
||||
refreshHungOperator(home);
|
||||
} catch { /* 主目录尚未就绪 */ }
|
||||
// 桌面端 office 技能缺失会让启动直接退出;异步补回,别挡启动热路径。
|
||||
if (hostctl.currentSurface() === "desktop") {
|
||||
setImmediate(() => {
|
||||
try {
|
||||
const office = core.healOfficeSkillsRuntimeOnDisk();
|
||||
if (office && office.healed) purgeLog("heal-office-skills", office);
|
||||
} catch { /* 没装桌面或目录不可写 */ }
|
||||
});
|
||||
}
|
||||
installIdentityOverride(ctx, cfg);
|
||||
installRegionGate(ctx);
|
||||
installFreshFullAccess(ctx);
|
||||
@@ -1654,6 +1747,20 @@ function applyHostHooks(ctx, cfg) {
|
||||
installCwdCapture(ctx);
|
||||
installMnemonMessageStrip(ctx, cfg);
|
||||
registerPromptSections(ctx, cfg);
|
||||
// 尾部锚点 + 子代理审批接管。两者都返回 disposer,统一挂到 ctx.effect 回收;
|
||||
// 老宿主没有 effect 时靠 cordis scope 自己回收。
|
||||
try {
|
||||
const disposeAnchor = registerAnchor(ctx, cfg);
|
||||
const disposeSubagent = installSubagentControl(ctx, cfg);
|
||||
try {
|
||||
ctx.effect(() => () => {
|
||||
try { disposeAnchor?.(); } catch { /* 释放过就忽略 */ }
|
||||
try { disposeSubagent?.(); } catch { /* 释放过就忽略 */ }
|
||||
});
|
||||
} catch { /* 已释放 */ }
|
||||
} catch (e) {
|
||||
purgeLog("apply-hooks", { name: "anchor/subagent", err: String(e?.message || e) });
|
||||
}
|
||||
|
||||
// 本次 apply 作用域内的 continue-retry 状态;旧 ctx dispose 会把这些闭包一并带走。
|
||||
const continuePluginCfg = cfg;
|
||||
|
||||
Reference in new issue
Block a user