mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 06:50:22 +08:00
- 反转 run_code/沙箱/plan 拦截(#60/#61/#63);minimal 补 agent-instructions(#26) - 回退按当前轮定位(rewind + #74/#75);Apply 写入 client 包避免取消重启 - 默认提示词重新加密;#66/#67/#69/#71/#70 与 README #68 安装说明 Closes #66, #67, #69, #70, #71. Docs for #68. Co-authored-by: Cursor <cursoragent@cursor.com>
289 lines
8.4 KiB
JavaScript
289 lines
8.4 KiB
JavaScript
import fs from "node:fs";
|
|
import { promises as fsp } from "node:fs";
|
|
import path from "node:path";
|
|
import {
|
|
findDshHome,
|
|
defaultOverrideText,
|
|
hangOperatorPrompt,
|
|
hungOperatorPrompt,
|
|
normalizeOverride,
|
|
peekCommittedOverride,
|
|
} from "./core.js";
|
|
import { modePromptText } from "./identity.js";
|
|
|
|
const RULE_ID_RE = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/;
|
|
const MAX_RULE_BYTES = 256 * 1024;
|
|
const MAX_NAME_LENGTH = 64;
|
|
export const RULE_TARGETS = ["AGENTS.md", "CLAUDE.md"];
|
|
|
|
export { findDshHome };
|
|
|
|
export function rulesDir(dshHome) {
|
|
return path.join(dshHome, "rules");
|
|
}
|
|
|
|
export function stateFile(dshHome) {
|
|
return path.join(rulesDir(dshHome), "state.json");
|
|
}
|
|
|
|
export function rulePath(dshHome, id) {
|
|
return path.join(rulesDir(dshHome), id + ".md");
|
|
}
|
|
|
|
export function ruleMetaPath(dshHome, id) {
|
|
return path.join(rulesDir(dshHome), id + ".json");
|
|
}
|
|
|
|
export function validRuleId(id) {
|
|
return typeof id === "string" && RULE_ID_RE.test(id);
|
|
}
|
|
|
|
export function validTarget(target) {
|
|
return typeof target === "string" && RULE_TARGETS.includes(target);
|
|
}
|
|
|
|
export function formatSize(bytes) {
|
|
if (!Number.isFinite(bytes) || bytes < 0) return "0 B";
|
|
if (bytes < 1024) return `${bytes} B`;
|
|
const kb = bytes / 1024;
|
|
return `${kb >= 100 ? Math.round(kb) : kb.toFixed(1)} KB`;
|
|
}
|
|
|
|
export function targetPath(dshHome, target) {
|
|
if (!validTarget(target)) throw new Error(`invalid target: ${target}`);
|
|
return path.join(dshHome, target);
|
|
}
|
|
|
|
export function normalizeMeta(id, meta) {
|
|
const name =
|
|
typeof meta?.name === "string" && meta.name.trim()
|
|
? meta.name.trim().slice(0, MAX_NAME_LENGTH)
|
|
: id;
|
|
const target = validTarget(meta?.target) ? meta.target : "AGENTS.md";
|
|
return { name, target };
|
|
}
|
|
|
|
export async function readRuleMeta(dshHome, id) {
|
|
try {
|
|
const parsed = JSON.parse(await fsp.readFile(ruleMetaPath(dshHome, id), "utf8"));
|
|
return normalizeMeta(id, parsed);
|
|
} catch {
|
|
return normalizeMeta(id, null);
|
|
}
|
|
}
|
|
|
|
export async function writeRuleMeta(dshHome, id, meta) {
|
|
if (!validRuleId(id)) throw new Error(`invalid rule id: ${id}`);
|
|
await fsp.mkdir(rulesDir(dshHome), { recursive: true });
|
|
await fsp.writeFile(ruleMetaPath(dshHome, id), JSON.stringify(normalizeMeta(id, meta), null, 2), "utf8");
|
|
}
|
|
|
|
export async function listRules(dshHome) {
|
|
const dir = rulesDir(dshHome);
|
|
const out = [];
|
|
try {
|
|
const entries = await fsp.readdir(dir, { withFileTypes: true });
|
|
for (const e of entries) {
|
|
if (!e.isFile() || !e.name.endsWith(".md")) continue;
|
|
const id = e.name.slice(0, -3);
|
|
if (!validRuleId(id)) continue;
|
|
let size = 0;
|
|
try {
|
|
size = (await fsp.stat(path.join(dir, e.name))).size;
|
|
} catch {
|
|
}
|
|
const meta = await readRuleMeta(dshHome, id);
|
|
out.push({ id, name: meta.name, target: meta.target, size });
|
|
}
|
|
} catch {
|
|
// rules 目录不存在时按空列表返回
|
|
}
|
|
out.sort((a, b) => a.id.localeCompare(b.id));
|
|
return out;
|
|
}
|
|
|
|
export async function readRule(dshHome, id) {
|
|
if (!validRuleId(id)) throw new Error(`invalid rule id: ${id}`);
|
|
try {
|
|
return await fsp.readFile(rulePath(dshHome, id), "utf8");
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function saveRule(dshHome, id, content, meta) {
|
|
if (!validRuleId(id)) throw new Error(`invalid rule id: ${id}`);
|
|
if (Buffer.byteLength(content, "utf8") > MAX_RULE_BYTES) {
|
|
throw new Error("rule too large (max 256KB)");
|
|
}
|
|
await fsp.mkdir(rulesDir(dshHome), { recursive: true });
|
|
await fsp.writeFile(rulePath(dshHome, id), content, "utf8");
|
|
await writeRuleMeta(dshHome, id, meta);
|
|
if ((await readActive(dshHome)) === id) {
|
|
await activateRule(dshHome, id);
|
|
}
|
|
}
|
|
|
|
export async function deleteRule(dshHome, id) {
|
|
if (!validRuleId(id)) throw new Error(`invalid rule id: ${id}`);
|
|
try {
|
|
await fsp.unlink(rulePath(dshHome, id));
|
|
} catch (e) {
|
|
if (e.code !== "ENOENT") throw e;
|
|
}
|
|
try {
|
|
await fsp.unlink(ruleMetaPath(dshHome, id));
|
|
} catch (e) {
|
|
if (e.code !== "ENOENT") throw e;
|
|
}
|
|
if ((await readActive(dshHome)) === id) {
|
|
try {
|
|
await fsp.unlink(stateFile(dshHome));
|
|
} catch {
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function readActive(dshHome) {
|
|
try {
|
|
const parsed = JSON.parse(await fsp.readFile(stateFile(dshHome), "utf8"));
|
|
return typeof parsed?.active === "string" && validRuleId(parsed.active) ? parsed.active : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function setActive(dshHome, id) {
|
|
if (!validRuleId(id)) throw new Error(`invalid rule id: ${id}`);
|
|
await fsp.mkdir(rulesDir(dshHome), { recursive: true });
|
|
await fsp.writeFile(stateFile(dshHome), JSON.stringify({ active: id }, null, 2), "utf8");
|
|
}
|
|
|
|
export function activeRuleId(dshHome) {
|
|
try {
|
|
const parsed = JSON.parse(fs.readFileSync(stateFile(dshHome), "utf8"));
|
|
const id = parsed?.active;
|
|
return validRuleId(id) ? id : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function activeRuleText(dshHome) {
|
|
const id = activeRuleId(dshHome);
|
|
if (!id) return "";
|
|
try {
|
|
return fs.readFileSync(rulePath(dshHome, id), "utf8");
|
|
} catch {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/** 本次进程里刚从提示词框提交的正文优先;否则用插件加密默认。不读宿主磁盘。 */
|
|
export function resolveInjectText(dshHome) {
|
|
const pinned = peekCommittedOverride(dshHome);
|
|
if (typeof pinned === "string" && normalizeOverride(pinned)) {
|
|
return { text: normalizeOverride(pinned), source: "prompt", ruleId: null };
|
|
}
|
|
const bundled = normalizeOverride(defaultOverrideText());
|
|
if (bundled) return { text: bundled, source: "default", ruleId: null };
|
|
return { text: "", source: "none", ruleId: null };
|
|
}
|
|
|
|
/**
|
|
* 每次组装都挂插件加密默认。重启后不用点保存,也不看宿主磁盘。
|
|
*/
|
|
export function ensureOperatorBody(dshHome) {
|
|
const resolved = resolveInjectText(dshHome);
|
|
if (resolved.text) hangOperatorPrompt(resolved.text);
|
|
return resolved.text || normalizeOverride(hungOperatorPrompt()) || "";
|
|
}
|
|
|
|
/** 官方和红队同一顺序。红队再去掉 CTF。没有正文时不注入操作者段。 */
|
|
export function resolveModeInject(dshHome, redteam) {
|
|
const resolved = resolveInjectText(dshHome);
|
|
if (!resolved.text) return "";
|
|
return modePromptText(resolved.text, Boolean(redteam));
|
|
}
|
|
|
|
export async function activateRule(dshHome, id) {
|
|
const content = await readRule(dshHome, id);
|
|
if (content === null) throw new Error(`rule not found: ${id}`);
|
|
const meta = await readRuleMeta(dshHome, id);
|
|
await fsp.writeFile(targetPath(dshHome, meta.target), content, "utf8");
|
|
await setActive(dshHome, id);
|
|
return meta;
|
|
}
|
|
|
|
export async function ensureInitialState(dshHome) {
|
|
const listed = await listRules(dshHome);
|
|
let active = await readActive(dshHome);
|
|
if (active !== null && !listed.some((r) => r.id === active)) {
|
|
try {
|
|
await fsp.unlink(stateFile(dshHome));
|
|
} catch {
|
|
}
|
|
active = null;
|
|
}
|
|
return { active };
|
|
}
|
|
|
|
export async function resetToOriginal(dshHome) {
|
|
const active = await readActive(dshHome);
|
|
const removed = [];
|
|
const skipped = [];
|
|
if (active !== null) {
|
|
const meta = await readRuleMeta(dshHome, active);
|
|
const ruleContent = await readRule(dshHome, active);
|
|
const fp = targetPath(dshHome, meta.target);
|
|
try {
|
|
const fileContent = await fsp.readFile(fp, "utf8");
|
|
if (ruleContent !== null && fileContent === ruleContent) {
|
|
await fsp.unlink(fp);
|
|
removed.push(fp);
|
|
} else {
|
|
skipped.push(fp);
|
|
}
|
|
} catch {
|
|
}
|
|
}
|
|
try {
|
|
await fsp.unlink(stateFile(dshHome));
|
|
} catch {
|
|
}
|
|
return { removed, skipped };
|
|
}
|
|
|
|
export async function rulesStatus(dshHome) {
|
|
const listed = await listRules(dshHome);
|
|
const active = await readActive(dshHome);
|
|
let active_target = null;
|
|
let agents_exists = false;
|
|
let agents_synced = false;
|
|
let agents_path = targetPath(dshHome, "AGENTS.md");
|
|
if (active !== null) {
|
|
const meta = await readRuleMeta(dshHome, active);
|
|
active_target = meta.target;
|
|
agents_path = targetPath(dshHome, meta.target);
|
|
try {
|
|
const content = await fsp.readFile(agents_path, "utf8");
|
|
agents_exists = true;
|
|
const ruleContent = await readRule(dshHome, active);
|
|
agents_synced = ruleContent !== null && ruleContent === content;
|
|
} catch {
|
|
}
|
|
}
|
|
const inject = resolveInjectText(dshHome);
|
|
return {
|
|
rules: listed,
|
|
active,
|
|
injectSource: inject.source,
|
|
injectRuleId: inject.ruleId,
|
|
active_target,
|
|
agents_exists,
|
|
agents_synced,
|
|
agents_path,
|
|
rules_dir: rulesDir(dshHome),
|
|
};
|
|
}
|