mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 13:30:22 +08:00
fix: 面板不再盖住官方对话,重打补丁不再回写前端包
浮层收成右侧窄条,底边停在输入框上方;演练台嵌在面板里时不再给官方窗口加右侧内边距。重打补丁时若前端 client.js 内容没变就不写,避免页面按旧 rev 404。桌面启动自检不再杀掉自己,清洗标记同时认新旧两句。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
354f9a1960
commit
813fbe4d54
8 files changed
+458
-188
No files matched your search
+144
-29
@@ -1604,12 +1604,31 @@ export function listShimDirsForPatch() {
|
||||
.filter((dir) => !isDesktopCommandRuntimeDir(dir));
|
||||
}
|
||||
|
||||
function pathIsUnder(child, root) {
|
||||
const c = path.normalize(String(child || "")).replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase();
|
||||
const r = path.normalize(String(root || "")).replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase();
|
||||
if (!c || !r || /^[a-z]:$/.test(r)) return false;
|
||||
return c === r || c.startsWith(`${r}/`);
|
||||
}
|
||||
|
||||
function findAiBaseDesktop() {
|
||||
const env = process.env.DSH_BASE;
|
||||
if (env && env.trim()) {
|
||||
const p = path.normalize(env.trim());
|
||||
if (isAiBase(p) && desktop.isInsideDesktopInstall(p)) return p;
|
||||
const exe = desktop.findDesktopAppExecutable();
|
||||
const owned = exe
|
||||
? desktop.resolveDesktopAiBase({
|
||||
execPath: exe,
|
||||
resourcesPath: path.join(path.dirname(exe), "resources"),
|
||||
argv: [],
|
||||
env: {},
|
||||
})
|
||||
: "";
|
||||
const envBase = String(process.env.DSH_BASE || "").trim();
|
||||
if (envBase) {
|
||||
const p = path.normalize(envBase);
|
||||
const underOwned = Boolean(owned && exe && pathIsUnder(p, path.dirname(exe)));
|
||||
if (isAiBase(p) && underOwned) return p;
|
||||
if (isAiBase(p) && !owned && desktop.isInsideDesktopInstall(p)) return p;
|
||||
}
|
||||
if (owned) return owned;
|
||||
const resolved = desktop.resolveDesktopAiBase();
|
||||
if (resolved) return resolved;
|
||||
const starts = [];
|
||||
@@ -1842,18 +1861,22 @@ export function scheduleSealedDesktopRestart(handoff) {
|
||||
return { renamed: false, pending: true, ...pending };
|
||||
}
|
||||
|
||||
const HOST_CLEAN_MARKERS = ["[dsh-purge] identity stripped", "[dsh-purge] complete prompt keeps inject"];
|
||||
/** waterfall 那句中间多了 waterfall,不包含旧句,两句都要认。 */
|
||||
export function hostPromptKeepsInject(text) {
|
||||
const body = String(text || "");
|
||||
if (!body.includes("[dsh-purge] identity stripped")) return false;
|
||||
return body.includes("[dsh-purge] complete prompt keeps waterfall inject")
|
||||
|| body.includes("[dsh-purge] complete prompt keeps inject");
|
||||
}
|
||||
|
||||
export function hostCleanMarkersPresent(aiBase) {
|
||||
if (!aiBase) return false;
|
||||
const fp = path.join(aiBase, "dsh-system-prompt", "lib", "index.js");
|
||||
let text = "";
|
||||
try {
|
||||
text = fs.readFileSync(fp, "utf8");
|
||||
return hostPromptKeepsInject(fs.readFileSync(fp, "utf8"));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
return HOST_CLEAN_MARKERS.every((mark) => text.includes(mark));
|
||||
}
|
||||
|
||||
/** 重启前把清洗写进当前宿主。没写完就返回失败,调用方不得打开进程。 */
|
||||
@@ -1879,17 +1902,18 @@ export async function ensureHostClean() {
|
||||
let flash = silenceCmdFlash(aiBase);
|
||||
if (pending.length || flash.ok !== true) {
|
||||
await backupAll(aiBase);
|
||||
const report = await applyPatches(aiBase);
|
||||
const report = await applyPatches(aiBase, ALL_PATCHES, { preserveClientBundles: true });
|
||||
await patchAllShims();
|
||||
flash = silenceCmdFlash(aiBase);
|
||||
const summary = summarizeApplyReport(report);
|
||||
pending = await pendingIds();
|
||||
if (pending.length || summary.failed.length || flash.ok !== true) {
|
||||
const blocking = summary.failed.filter((row) => row.status !== "pattern_not_found");
|
||||
if (pending.length || blocking.length || flash.ok !== true) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "清洗没有完成,已取消重启",
|
||||
pending,
|
||||
failed: summary.failed.length,
|
||||
failed: blocking.length,
|
||||
aiBase,
|
||||
};
|
||||
}
|
||||
@@ -2060,16 +2084,14 @@ function quotedConst(name, body) {
|
||||
|
||||
const COMPLETE_PROMPT_WATERFALL_INJECT =
|
||||
"\t\tconst transformed = await this.ctx.waterfall(scopeTarget(this, scope), \"system-prompt/assemble\", assembly, context, () => Promise.resolve(assembly));\n" +
|
||||
"\t\tconst dshPurgeInject = (transformed.sections || []).filter((s) => String(s && s.name || \"\").startsWith(\"dsh-purge\")).map((s) => String(s.text || \"\")).filter((t) => t.length > 0).join(\"\\n\\n\");\n" +
|
||||
"\t\tconst dshPurgeSections = (transformed.sections || []).filter((s) => String(s && s.name || \"\").startsWith(\"dsh-purge\") && String(s.text || \"\").length > 0);\n" +
|
||||
"\t\tif (completeSection === void 0 && !runtimeContextSuppressed) return transformed;\n" +
|
||||
"\t\treturn {\n" +
|
||||
"\t\t\t...transformed,\n" +
|
||||
"\t\t\tsections: completeSection === void 0 ? transformed.sections : (function (complete, inject) {\n" +
|
||||
"\t\t\tsections: completeSection === void 0 ? transformed.sections : (function (complete, purge) {\n" +
|
||||
"\t\t\t\tconst base = { ...complete };\n" +
|
||||
"\t\t\t\tif (!inject) return [base];\n" +
|
||||
"\t\t\t\tif (String(base.text || \"\").includes(inject)) return [base];\n" +
|
||||
"\t\t\t\treturn [{ ...base, text: inject + \"\\n\\n\" + base.text }]; // [dsh-purge] complete prompt keeps waterfall inject\n" +
|
||||
"\t\t\t})(completeSection, dshPurgeInject),\n" +
|
||||
"\t\t\t\treturn [...purge, base]; // [dsh-purge] complete prompt keeps waterfall inject\n" +
|
||||
"\t\t\t})(completeSection, dshPurgeSections),\n" +
|
||||
"\t\t\tcontexts: runtimeContextSuppressed ? [] : transformed.contexts\n" +
|
||||
"\t\t};";
|
||||
|
||||
@@ -2859,6 +2881,14 @@ const NEW_TOOL_PATCHES = [
|
||||
replace:
|
||||
"personaPrefix: !!js process.env.DSH_SYSTEM_PROMPT ?? ''",
|
||||
},
|
||||
{
|
||||
pattern:
|
||||
" prefix: \"\"\n" +
|
||||
" complete: true",
|
||||
replace:
|
||||
" prefix: \"\"\n" +
|
||||
" complete: false # [dsh-purge] minimal keeps operator once",
|
||||
},
|
||||
],
|
||||
markers: [
|
||||
"personaPrefix: !!js process.env.DSH_SYSTEM_PROMPT ?? ''",
|
||||
@@ -4118,6 +4148,8 @@ const NEW_TOOL_PATCHES = [
|
||||
desc: "回退只藏被撤掉的那一轮 / rewind hides only the cut range",
|
||||
file: ["conversation-client"],
|
||||
optional: true,
|
||||
// 锚点还在时也别再 replaceAll。这个文件是前端模块,重复写会改 mtime。
|
||||
skipIfMarked: true,
|
||||
replacements: [
|
||||
{
|
||||
pattern:
|
||||
@@ -4614,6 +4646,21 @@ function flattenTargetPaths(files) {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* client-hmr 按 mtime/ctime/size 轮询 exports["./client"](官方 UI 包是 lib/client.js)。
|
||||
* 页面已经按旧 rev 要脚本时,再写这个文件会让缓存里的地址 404,输入框整串掉光(#63)。
|
||||
*/
|
||||
export function isWatchedClientBundle(fp) {
|
||||
const norm = String(fp || "").replace(/\\/g, "/");
|
||||
return /\/node_modules\/@[^/]+\/[^/]+\/lib\/client\.js$/.test(norm);
|
||||
}
|
||||
|
||||
export function clientBundlePackageName(fp) {
|
||||
const norm = String(fp || "").replace(/\\/g, "/");
|
||||
const matched = norm.match(/\/node_modules\/(@[^/]+\/[^/]+)\/lib\/client\.js$/);
|
||||
return matched ? matched[1] : "";
|
||||
}
|
||||
|
||||
/** 用户配置层:官方允许在 profiles/<name>/cordis.patch.yml 手写 insert,绝不能进 bak/回滚(#43)。 */
|
||||
export function isUserProfilePatchFile(fp) {
|
||||
const n = String(fp || "").replace(/\\/g, "/").toLowerCase();
|
||||
@@ -4671,14 +4718,29 @@ export async function backupAll(aiBase) {
|
||||
return { made, errors, skippedUser, dropped };
|
||||
}
|
||||
|
||||
export async function revertAll(aiBase) {
|
||||
async function sameFileBytes(left, right) {
|
||||
try {
|
||||
const [a, b] = await Promise.all([fsp.readFile(left), fsp.readFile(right)]);
|
||||
return a.equals(b);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function revertAll(aiBase, options = {}) {
|
||||
const files = targetFiles(aiBase);
|
||||
const reverted = [];
|
||||
const skipped = [];
|
||||
const preservedUser = [];
|
||||
const errors = [];
|
||||
const preserveClientBundles = options.preserveClientBundles === true;
|
||||
for (const fp of flattenTargetPaths(files)) {
|
||||
const bak = fp + ".dshpurge.bak";
|
||||
// 启动自愈不能把前端 client.js 盖回去:copyFile 即使字节相同也会改 mtime(#63)。
|
||||
if (preserveClientBundles && isWatchedClientBundle(fp)) {
|
||||
skipped.push(fp);
|
||||
continue;
|
||||
}
|
||||
// 用户 patch 层:有 bak 也只丢掉 bak,绝不 copy 回去(#43)。
|
||||
if (isUserProfilePatchFile(fp)) {
|
||||
if (fs.existsSync(bak)) {
|
||||
@@ -4693,6 +4755,10 @@ export async function revertAll(aiBase) {
|
||||
}
|
||||
if (fs.existsSync(bak)) {
|
||||
try {
|
||||
if (fs.existsSync(fp) && await sameFileBytes(fp, bak)) {
|
||||
skipped.push(fp);
|
||||
continue;
|
||||
}
|
||||
await fsp.copyFile(bak, fp);
|
||||
await fsp.unlink(bak);
|
||||
reverted.push(fp);
|
||||
@@ -5387,7 +5453,15 @@ async function loadText(p) {
|
||||
}
|
||||
|
||||
async function saveText(p, text) {
|
||||
await fsp.writeFile(p, text, "utf8");
|
||||
const next = Buffer.from(text, "utf8");
|
||||
try {
|
||||
const current = await fsp.readFile(p);
|
||||
if (current.equals(next)) return false;
|
||||
} catch (e) {
|
||||
if (!e || e.code !== "ENOENT") throw e;
|
||||
}
|
||||
await fsp.writeFile(p, next);
|
||||
return true;
|
||||
}
|
||||
|
||||
async function repairPluginCordisYaml() {
|
||||
@@ -5398,9 +5472,10 @@ async function repairPluginCordisYaml() {
|
||||
if (cleaned !== text) await saveText(fp, cleaned);
|
||||
}
|
||||
|
||||
export async function applyPatches(aiBase, patches = ALL_PATCHES) {
|
||||
export async function applyPatches(aiBase, patches = ALL_PATCHES, options = {}) {
|
||||
const files = targetFiles(aiBase);
|
||||
const report = [];
|
||||
const preserveClientBundles = options.preserveClientBundles === true;
|
||||
await repairPluginCordisYaml();
|
||||
for (const p of patches) {
|
||||
if (!patchAppliesHere(p)) {
|
||||
@@ -5437,6 +5512,11 @@ export async function applyPatches(aiBase, patches = ALL_PATCHES) {
|
||||
lastError = "read_error";
|
||||
continue;
|
||||
}
|
||||
if (preserveClientBundles && isWatchedClientBundle(fp)) {
|
||||
if (filePatchSettled(text, p, fp)) already += 1;
|
||||
else if (patchHasRemainingWork(text, p, fp) || p.strictFiles) notFound += 1;
|
||||
continue;
|
||||
}
|
||||
const appliedText = applyReplacementsToText(text, p, fp);
|
||||
let textOut = appliedText.text;
|
||||
let changed = appliedText.changed;
|
||||
@@ -5449,8 +5529,9 @@ export async function applyPatches(aiBase, patches = ALL_PATCHES) {
|
||||
}
|
||||
if (changed) {
|
||||
try {
|
||||
await saveText(fp, textOut);
|
||||
applied += 1;
|
||||
const wrote = await saveText(fp, textOut);
|
||||
if (wrote) applied += 1;
|
||||
else already += 1;
|
||||
} catch (e) {
|
||||
lastError = `write_error:${e}`;
|
||||
}
|
||||
@@ -5481,6 +5562,37 @@ export async function applyPatches(aiBase, patches = ALL_PATCHES) {
|
||||
return report;
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面加载前同步写入前端 client.js。只在字节真的变化时写盘。
|
||||
* 启动 4 秒后的 settle 不得再写这些文件,否则 client-hmr 会把旧 rev 从缓存里拿掉(#63)。
|
||||
* @returns 实际写过的包名
|
||||
*/
|
||||
export function patchWatchedClientBundlesSync(aiBase = findAiBase()) {
|
||||
if (!aiBase) return [];
|
||||
const files = targetFiles(aiBase);
|
||||
const written = [];
|
||||
for (const p of ALL_PATCHES) {
|
||||
if (!patchAppliesHere(p)) continue;
|
||||
for (const key of fileKeysOf(p)) {
|
||||
for (const fp of pathsOf(files, key)) {
|
||||
if (!isWatchedClientBundle(fp)) continue;
|
||||
const text = fs.readFileSync(fp, "utf8");
|
||||
const appliedText = applyReplacementsToText(text, p, fp);
|
||||
if (!appliedText.changed) continue;
|
||||
const next = Buffer.from(appliedText.text, "utf8");
|
||||
const current = fs.readFileSync(fp);
|
||||
if (current.equals(next)) continue;
|
||||
const bak = `${fp}.dshpurge.bak`;
|
||||
if (!fs.existsSync(bak)) fs.copyFileSync(fp, bak);
|
||||
fs.writeFileSync(fp, next);
|
||||
const packageName = clientBundlePackageName(fp);
|
||||
if (packageName && !written.includes(packageName)) written.push(packageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
export async function patchStatus(aiBase, patches = ALL_PATCHES) {
|
||||
const files = targetFiles(aiBase);
|
||||
const status = {};
|
||||
@@ -5493,6 +5605,7 @@ export async function patchStatus(aiBase, patches = ALL_PATCHES) {
|
||||
let applied = 0;
|
||||
let pending = 0;
|
||||
let skipped = 0;
|
||||
let unmatched = 0;
|
||||
let missing = 0;
|
||||
for (const key of keys) {
|
||||
const fps = pathsOf(files, key);
|
||||
@@ -5510,12 +5623,14 @@ export async function patchStatus(aiBase, patches = ALL_PATCHES) {
|
||||
}
|
||||
const outcome = filePatchOutcome(text, p, fp);
|
||||
if (outcome === "applied") applied += 1;
|
||||
else if (outcome === "pending" || (p.strictFiles && outcome === "skipped")) pending += 1;
|
||||
else if (outcome === "pending") pending += 1;
|
||||
else if (p.strictFiles) unmatched += 1;
|
||||
else skipped += 1;
|
||||
}
|
||||
}
|
||||
if (pending > 0) status[p.id] = "pending";
|
||||
else if (applied > 0) status[p.id] = "applied";
|
||||
else if (unmatched > 0) status[p.id] = "unmatched";
|
||||
else if (skipped > 0) status[p.id] = "skipped";
|
||||
else status[p.id] = p.optional ? "na" : "missing_file";
|
||||
}
|
||||
@@ -6190,15 +6305,15 @@ export async function gatherState() {
|
||||
let shim_cmd = "missing";
|
||||
let shim_ps1 = "missing";
|
||||
let shim_bin = "missing";
|
||||
if (shimDir) {
|
||||
shim_cmd = shimFileStatus(path.join(shimDir, "dsh.cmd"));
|
||||
shim_ps1 = shimFileStatus(path.join(shimDir, "dsh.ps1"));
|
||||
shim_bin = shimFileStatus(path.join(shimDir, "dsh"));
|
||||
} else if (adapterFor(detectSurface()) === "desktop") {
|
||||
// 桌面宿主不 patch CLI shim;缺目录不是失败。
|
||||
if (adapterFor(detectSurface()) === "desktop") {
|
||||
// 桌面宿主不改 CLI shim。PATH 上就算有网页端的 dsh,也不算进这一页。
|
||||
shim_cmd = "n/a";
|
||||
shim_ps1 = "n/a";
|
||||
shim_bin = "n/a";
|
||||
} else if (shimDir) {
|
||||
shim_cmd = shimFileStatus(path.join(shimDir, "dsh.cmd"));
|
||||
shim_ps1 = shimFileStatus(path.join(shimDir, "dsh.ps1"));
|
||||
shim_bin = shimFileStatus(path.join(shimDir, "dsh"));
|
||||
}
|
||||
return {
|
||||
surface: detectSurface(),
|
||||
|
||||
Reference in new issue
Block a user