From 3e4f66f813ee76d8d9eec6d119fa37666825ea36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AB=E6=88=91=E5=B0=8F=E6=9D=A8=E5=90=8C=E5=AD=A6?= <188421097+HunYiSage@users.noreply.github.com> Date: Thu, 24 Sep 2026 12:22:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20bash=20=E8=B6=85=E6=97=B6=E6=B3=A8?= =?UTF-8?q?=E8=AE=B0=E6=94=B9=E6=88=90=20YAML=20=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E5=90=AF=E5=8A=A8=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=20(#36)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- lib/core.js | 45 ++++++++++++++++--- package.json | 1 + release-notes.md | 2 + scripts/test-yaml-comment.mjs | 84 +++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 scripts/test-yaml-comment.mjs diff --git a/lib/core.js b/lib/core.js index 6c6c926..1df9b3b 100644 --- a/lib/core.js +++ b/lib/core.js @@ -2230,11 +2230,13 @@ const NEW_TOOL_PATCHES = [ // 现在的做法:锚在 bash-sandbox 行内,`[^\n]*` 必须吃到行尾 —— 这样上一次 // 追加的注记会被整体重写而不是再叠一层,数值也不再有可再匹配的"前缀"。 // 这条是带 g 的模块级正则,匹配和写入都要克隆后再用,否则 lastIndex 残留会导致永远 pending。 + // YAML 只认 #。写成 // 时从数字到行尾都是字符串,timeoutMs 校验失败,宿主起不来。 + // [^\n]* 会把已经写成 // 的那一行整段重写成下面的 # 版本。 pattern: /(name: '@deepseek-ai\/dsh-bash-sandbox'[\s\S]*?\n {8}timeoutMs: )6[0-9]+[^\n]*/g, - replace: "$1600000 // [dsh-purge] bash timeout raised", + replace: "$1600000 # [dsh-purge] bash timeout raised", }, ], - markers: ["timeoutMs: 600000 // [dsh-purge] bash timeout raised"], + markers: ["timeoutMs: 600000 # [dsh-purge] bash timeout raised"], }, { id: 22, name: "READ_CAPS_RAISED", layer: "代码", layer_en: "Code", @@ -3646,7 +3648,39 @@ function isBareIdentityNeedle(pattern) { return /^You are a coding agent powered by\b/.test(needle); } -function applyReplacementsToText(text, patch, fp = "") { +// 行尾的 JS 注释写进 YAML 会变成字符串。!!js 行本身是表达式,引号里的 // 也不动。 +export function yamlJsCommentsToHash(text) { + return asLf(text).split("\n").map((line) => { + if (line.includes("!!js")) return line; + let quote = ""; + for (let i = 0; i < line.length - 1; i += 1) { + const ch = line[i]; + if (quote) { + if (quote === "'" && ch === "'" && line[i + 1] === "'") { + i += 1; + continue; + } + if (quote === '"' && ch === "\\") { + i += 1; + continue; + } + if (ch === quote) quote = ""; + continue; + } + if (ch === '"' || ch === "'") { + quote = ch; + continue; + } + if (ch === "#") return line; + if (ch === "/" && line[i + 1] === "/" && (i === 0 || /\s/.test(line[i - 1]))) { + return `${line.slice(0, i)}#${line.slice(i + 2)}`; + } + } + return line; + }).join("\n"); +} + +export function applyReplacementsToText(text, patch, fp = "") { // 插入型补丁锚点仍在,已有标记则不再 replaceAll。 if (patch.skipIfMarked && patchApplied(text, patch)) { return { text, changed: false }; @@ -3659,6 +3693,7 @@ function applyReplacementsToText(text, patch, fp = "") { if (!pattern || typeof replace !== "string") continue; // YAML 块标量行首有缩进;无缩进 identity+\n\n 会留下 6 空格,再和下一段拼成 12 空格。 if (yaml && isBareIdentityNeedle(pattern)) continue; + const written = yaml ? yamlJsCommentsToHash(asLf(replace)) : asLf(replace); // 正则 pattern:用于自匹配型替换(产物包含原 pattern,如 timeoutMs 追加 0)。 if (pattern instanceof RegExp) { const re = cloneRegExp(pattern, true); @@ -3666,7 +3701,7 @@ function applyReplacementsToText(text, patch, fp = "") { if (!re.test(out)) continue; re.lastIndex = 0; // 用字符串形态调用:保留 $1..$9 展开语义(回调形态会把 "$1" 当字面量写进文件)。 - const next = out.replace(re, asLf(replace)); + const next = out.replace(re, written); if (next === out) continue; out = next; changed = true; @@ -3674,7 +3709,7 @@ function applyReplacementsToText(text, patch, fp = "") { } const needle = asLf(pattern); if (!out.includes(needle)) continue; - out = out.split(needle).join(asLf(replace)); + out = out.split(needle).join(written); changed = true; } if (crlf) out = out.replace(/\n/g, "\r\n"); diff --git a/package.json b/package.json index c28da18..6228a16 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "scripts": { "embed-prompt": "node scripts/embed-default-prompt.mjs", "test:identity": "node scripts/test-identity-assembly.mjs", + "test:yaml": "node scripts/test-yaml-comment.mjs", "test:update": "node scripts/test-update-guards.mjs", "test:rewind": "node scripts/test-rewind-plan.mjs" }, diff --git a/release-notes.md b/release-notes.md index c3b1883..0cd7622 100644 --- a/release-notes.md +++ b/release-notes.md @@ -5,6 +5,7 @@ - 设置页点「应用」会把输入框里的提示词写入 `prompt-inject.md`。以前只检查有没有字,重启后读的还是旧文件,所以注入像没生效。 - 带 `complete: true` 的预设在重启后不再丢掉 `dsh-purge` 注入段。点「应用」之后必须再点「重启」,宿主重新打开后新开一轮对话,注入才进入当前会话。 - bash 超时补丁不再和状态检查共用同一条正则。点「应用」会把 60 秒写成 10 分钟,不会一直停在待应用(#35)。 +- bash 超时写进 `cordis.patch.yml` 时用 `#` 注释。`//` 会被 YAML 当成字符串,`timeoutMs` 校验失败,`dsh` 一启动就崩(#36)。已经起不来的,先把 `@deepseek-ai/dsh-base` 里那一行的 `//` 改成 `#`,或改回 `timeoutMs: 600000`,再更新插件。 - 去掉点了也不变的「启用 fetch」补丁,后面的编号依次前移。没装 `dsh-web-fetch-http` 时不再占着一条待应用。 - README 开头写明必须应用并重启。工作原理改成流程图,去掉 0.1.5 适配表和生效验证。 @@ -13,5 +14,6 @@ - Apply on the settings page writes the prompt box into `prompt-inject.md`. It used to only check that the box was not empty, so after a restart the host still read the old file and the inject looked like it never happened. - Presets with `complete: true` no longer drop the `dsh-purge` inject section after a restart. After Apply you must click Restart. When the host is back, start a new chat; that is when the inject enters the session. - The bash timeout patch no longer shares one regular expression with the status check. Apply now writes 60 seconds as 10 minutes instead of staying on pending (#35). +- The bash timeout note in `cordis.patch.yml` is a `#` comment. A `//` comment is a YAML string, so `timeoutMs` fails validation and `dsh` dies on startup (#36). If it already will not start, change that `//` to `#` in `@deepseek-ai/dsh-base`, or put the line back to `timeoutMs: 600000`, then update the plugin. - The fetch-enable patch that stayed pending is gone, and later patch numbers move up by one. A missing `dsh-web-fetch-http` package no longer occupies a pending row. - The README now says at the top that Apply must be followed by Restart. How it works is a flowchart. The 0.1.5 notes table and the verify list are removed. diff --git a/scripts/test-yaml-comment.mjs b/scripts/test-yaml-comment.mjs new file mode 100644 index 0000000..6259b2f --- /dev/null +++ b/scripts/test-yaml-comment.mjs @@ -0,0 +1,84 @@ +import assert from "node:assert/strict"; +import { ALL_PATCHES, applyReplacementsToText, yamlJsCommentsToHash } from "../lib/core.js"; + +const bash = ALL_PATCHES.find((patch) => patch.id === 21); +assert.equal(bash.name, "BASH_TIMEOUT_RAISED"); +assert.equal(bash.replacements[0].replace.includes("//"), false); +assert.match(bash.markers[0], /timeoutMs: 600000 # \[dsh-purge\]/); + +const head = + "- id: bash-sandbox\n" + + " name: '@deepseek-ai/dsh-bash-sandbox'\n" + + " config:\n"; +const indent = " ".repeat(8); + +const fresh = applyReplacementsToText( + `${head}${indent}timeoutMs: 60000\n`, + bash, + "cordis.patch.yml", +); +assert.equal(fresh.changed, true); +assert.match(fresh.text, /timeoutMs: 600000 # \[dsh-purge\] bash timeout raised/); +assert.equal(fresh.text.includes("//"), false); + +const broken = applyReplacementsToText( + `${head}${indent}timeoutMs: 600000 // [dsh-purge] bash timeout raised\n`, + bash, + "cordis.patch.yml", +); +assert.equal(broken.changed, true); +assert.equal(broken.text, fresh.text); + +const healed = applyReplacementsToText(fresh.text, bash, "cordis.patch.yml"); +assert.equal(healed.changed, false); +assert.equal(healed.text, fresh.text); + +const jsKept = applyReplacementsToText( + 'text: "You are an AI agent powered by DeepSeek Harness."\n', + { + replacements: [{ + pattern: 'text: "You are an AI agent powered by DeepSeek Harness."', + replace: 'text: "" // [dsh-purge] identity stripped', + }], + }, + "lib/index.js", +); +assert.match(jsKept.text, /text: "" \/\/ \[dsh-purge\] identity stripped/); + +const yamlFixed = applyReplacementsToText( + 'text: "You are an AI agent powered by DeepSeek Harness."\n', + { + replacements: [{ + pattern: 'text: "You are an AI agent powered by DeepSeek Harness."', + replace: 'text: "" // [dsh-purge] identity stripped', + }], + }, + "agent.cordis.yml", +); +assert.match(yamlFixed.text, /text: "" # \[dsh-purge\] identity stripped/); +assert.equal(yamlFixed.text.includes("//"), false); + +assert.equal( + yamlJsCommentsToHash("mode: !!js process.env.X ?? 'https://example.com' // keep"), + "mode: !!js process.env.X ?? 'https://example.com' // keep", +); +assert.equal(yamlJsCommentsToHash("url: https://example.com"), "url: https://example.com"); +assert.equal( + yamlJsCommentsToHash('note: "keep // inside"'), + 'note: "keep // inside"', +); + +const yamlPatches = ALL_PATCHES.filter((patch) => { + const keys = Array.isArray(patch.file) ? patch.file : [patch.file]; + return keys.some((key) => /preset|web-app|headless|base$|agent-preset/.test(key)); +}); +for (const patch of yamlPatches) { + const reps = patch.replacements || (patch.patterns || []).map((pattern) => ({ pattern, replace: patch.replace })); + for (const rep of reps) { + if (typeof rep.replace !== "string" || !rep.replace.includes("//")) continue; + const safe = yamlJsCommentsToHash(rep.replace); + assert.equal(safe.includes(" //"), false, `${patch.name} still writes a JS comment into YAML`); + } +} + +console.log("ok: yaml comments");