diff --git a/lib/profile-insert.js b/lib/profile-insert.js index 4c476b1..011b5f0 100644 --- a/lib/profile-insert.js +++ b/lib/profile-insert.js @@ -1,8 +1,11 @@ /** - * 用户层 profile/cordis.patch.yml 的本体 insert(#154 / #159)。 + * 用户层 profile/cordis.patch.yml 的本体 insert(#154 / #159 / #191)。 * * 包内 overlay 只改已入队行的 inject/config,不能再 insert 一次(Hub 已入队会 * already queued)。postinstall 和演练台子行都走这里:行不存在就写一条。 + * + * #191:文件本身是一个完整文档时(宿主生成的空 flow 序列 `[]`),不能再追加, + * 追加会变成第二个文档,宿主启动解析失败。 */ import fs from "node:fs"; import path from "node:path"; @@ -47,6 +50,52 @@ export function profileHasPurgeBodyInsert(text) { return false; } +/** + * 判断 cordis.patch.yml 的数据部分形态(#191)。 + * + * 宿主生成的 profile 层 patch 文件可能整体只有一个空 flow 序列 `[]`, + * 那是**一个完整 YAML 文档**。往文件尾追加 `- insert:` 会变成第二个文档, + * 宿主启动解析报 "end of the stream or a document separator is expected"。 + * + * 只看最后一个文档的根节点类型(追加内容落在最后一个文档里): + * - "empty" 没有数据行(空文件 / 只有注释和空行) + * - "empty-flow" 数据部分恰好是空 flow 序列 `[]`(可带 `---` 前缀) + * - "block-seq" 正常块序列(顶层 `- ...`),沿用旧的追加行为 + * - "flow-seq" 非空 flow 序列(宿主/用户内容,动不得) + * - "unknown" 其它形态(顶层映射等),认不准就不写 + */ +export function classifyPatchBody(text) { + const lines = String(text || "").replace(/\r\n/g, "\n").split("\n"); + const dataLines = []; + for (let i = 0; i < lines.length; i += 1) { + const trimmed = lines[i].trim(); + if (!trimmed) continue; // 空行 + if (trimmed.startsWith("#")) continue; // 注释行 + dataLines.push({ index: i, trimmed }); + } + // 按 `---` 切成多个文档,只看最后一个(追加内容会落在最后一个文档里) + const lastDoc = []; + for (const item of dataLines) { + if (item.trimmed === "---") { + lastDoc.length = 0; + continue; + } + lastDoc.push(item); + } + if (lastDoc.length === 0) return { kind: "empty", lines }; + // 空 flow 序列:`[]` / `[ ]`,允许行尾注释 + if (lastDoc.length === 1 && /^\[\s*\]\s*(#.*)?$/.test(lastDoc[0].trimmed)) { + return { kind: "empty-flow", lines, emptyFlowIndex: lastDoc[0].index }; + } + if (lastDoc[0].trimmed.startsWith("[")) return { kind: "flow-seq", lines }; + if (/^-(\s|$)/.test(lastDoc[0].trimmed)) { + // 块序列里混进顶层 flow 序列 → 本身就是坏 YAML,认不准就不动 + if (lastDoc.slice(1).some((item) => item.trimmed.startsWith("["))) return { kind: "unknown", lines }; + return { kind: "block-seq", lines }; + } + return { kind: "unknown", lines }; +} + export function ensureProfileBodyInsert(profileDir) { const fp = path.join(profileDir, "cordis.patch.yml"); let text = ""; @@ -54,6 +103,23 @@ export function ensureProfileBodyInsert(profileDir) { if (profileHasPurgeBodyInsert(text)) { return { ok: true, wrote: false, path: fp }; } + const shape = classifyPatchBody(text); + if (shape.kind === "empty-flow") { + // `[]` 本身是一个完整文档:把整行替换成 insert 块,绝不追加成第二个文档(#191) + const blockLines = INSERT_BLOCK.replace(/\n$/, "").split("\n"); + const out = [ + ...shape.lines.slice(0, shape.emptyFlowIndex), + ...blockLines, + ...shape.lines.slice(shape.emptyFlowIndex + 1), + ].join("\n"); + fs.writeFileSync(fp, `${out.endsWith("\n") ? out : `${out}\n`}`, "utf8"); + return { ok: true, wrote: true, path: fp }; + } + if (shape.kind === "flow-seq" || shape.kind === "unknown") { + // 宿主 / 用户自己的内容,宁可不动也别写坏(#191) + return { ok: false, wrote: false, path: fp }; + } + // 空文件 / 只有注释 → 直接写 insert 块;正常块序列 → 沿用旧的追加行为 const prefix = text && !text.endsWith("\n") ? "\n" : ""; fs.writeFileSync(fp, `${text}${prefix}${text ? "\n" : ""}${INSERT_BLOCK}`, "utf8"); return { ok: true, wrote: true, path: fp }; diff --git a/package.json b/package.json index a2e6b42..0ba68d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-purge", - "version": "1.1.82", + "version": "1.1.83", "description": "DeepSeek Harness 破甲:让所有模型都能破甲,不同模型可换不同提示词;默认提示词面向国模「小码酱」。Jailbreak for every model — swap prompts per model. 求 Star 收藏 ⭐ 如要二开,请查看署名要求部分", "type": "module", "main": "lib/index.js", diff --git a/release-notes.md b/release-notes.md index 53e26cb..b6d6556 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,3 +1,29 @@ +# 1.1.83 + +## 中文 + +- 版本 **1.1.83**。修复 bug,修复稳定性。 +- **#185**:修复 pnpm 11(DSH 内置 11.2.2)下 `pnpm install` 因 `allowBuilds` 崩溃的问题。 + - 之前 profile 的 `pnpm-workspace.yaml` 会写入 `dsh-purge@https://...` 这类 URL 键,pnpm 把它当版本联合解析,直接报 `[ERR_PNPM_INVALID_VERSION_UNION] Invalid versions union`。 + - 现在只写裸包名 `dsh-purge`:pnpm 的 `allowBuilds` 本来就按包名匹配,tarball / git / registry 三种安装形态都放行。 + - 自愈加强:所有 `dsh-purge@*` 历史键(URL 键、旧截断键 `dsh-purge@https`、`@1.1.81` 之类版本键)一律清掉,只保留裸包名;非 dsh-purge 的键原样保留。 +- **#191**:修复 macOS DSH Desktop(0.2.1-rc.4)启动崩溃 `failed to parse overlay .../cordis.patch.yml`。 + - 宿主生成的 profile 层 `cordis.patch.yml` 可能整体只有一个空 flow 序列 `[]`,那本身就是一个完整 YAML 文档;再往文件尾追加 `- insert:` 会变成第二个文档,宿主解析直接失败。 + - 现在写之前先判断数据部分形态:空 flow 序列(`[]`,可带 `---` 前缀)→ **替换** `[]` 整行而不是追加;空文件 / 只有注释 → 直接写 insert 块;正常块序列 → 沿用原来的追加行为。 + - 非空 flow 序列(宿主或用户自己的内容)认不准就不动,返回 `{ ok: false, wrote: false }`,宁可留着也不写坏。 + +## English + +- **1.1.83**. Bug fixes and stability. +- **#185**: fix `pnpm install` crashing under pnpm 11 (11.2.2, bundled with DSH) because of `allowBuilds`. + - The profile `pnpm-workspace.yaml` used to get a `dsh-purge@https://...` URL key, which pnpm parses as a version union and rejects with `[ERR_PNPM_INVALID_VERSION_UNION] Invalid versions union`. + - Now only the bare package name `dsh-purge` is written: pnpm matches `allowBuilds` by package name, so tarball / git / registry installs are all allowed. + - Stronger self-healing: every `dsh-purge@*` legacy key (URL keys, truncated `dsh-purge@https`, `@1.1.81`-style version keys) is dropped and only the bare name is kept; non-dsh-purge keys are preserved as-is. +- **#191**: fix the macOS DSH Desktop (0.2.1-rc.4) startup crash `failed to parse overlay .../cordis.patch.yml`. + - The host-generated profile `cordis.patch.yml` may consist of a single empty flow sequence `[]`, which is already a complete YAML document; appending `- insert:` after it creates a second document and the host fails to parse it. + - The data shape is now detected before writing: an empty flow sequence (`[]`, optionally `---`-prefixed) → **replace** the `[]` line instead of appending; empty file / comments only → write the insert block; a normal block sequence → the previous append behaviour is kept. + - A non-empty flow sequence (host or user content) is left alone and returns `{ ok: false, wrote: false }` — better to skip than to write something broken. + # 1.1.82 ## 中文 diff --git a/scripts/ensure-profile-bundle.mjs b/scripts/ensure-profile-bundle.mjs index c66cdc8..8d2649b 100644 --- a/scripts/ensure-profile-bundle.mjs +++ b/scripts/ensure-profile-bundle.mjs @@ -51,6 +51,9 @@ function ensureProfileInsert(profileDir) { const result = ensureProfileBodyInsert(profileDir); if (result.wrote) { process.stdout.write(`dsh-purge: wrote body insert (${result.path})\n`); + } else if (result.ok === false) { + // 宿主 / 用户自己的 cordis.patch.yml 内容,宁可不动也别写坏(#191) + process.stdout.write(`dsh-purge: skipped body insert, unrecognized patch body (${result.path})\n`); } else { process.stdout.write(`dsh-purge: insert already in ${result.path}\n`); } diff --git a/test/profile-insert.test.js b/test/profile-insert.test.js index f338581..639cc12 100644 --- a/test/profile-insert.test.js +++ b/test/profile-insert.test.js @@ -10,6 +10,9 @@ import { purgePnpmBuildSpecs, rewritePnpmAllowBuilds, ensurePnpmAllowBuild, + ensureProfileBodyInsert, + classifyPatchBody, + INSERT_BLOCK, } from "../lib/profile-insert.js"; const MASTER_URL = "https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/refs/heads/master"; @@ -23,6 +26,26 @@ function withTempProfile(lockText) { return dir; } +function withTempPatch(content) { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-patch-")); + if (content !== undefined) { + fs.writeFileSync(path.join(dir, "cordis.patch.yml"), content, "utf8"); + } + return dir; +} + +/** 去掉注释和空行后,统计顶层 `- insert:` 组数(判定是不是第二个文档)。 */ +function topLevelInsertGroups(text) { + const lines = String(text || "").split(/\r?\n/); + let count = 0; + for (const line of lines) { + const trimmed = line.trim(); + if (!trimmed || trimmed.startsWith("#")) continue; + if (/^-\s*insert:\s*$/.test(line)) count += 1; + } + return count; +} + test("URL 键解析不被冒号截断", () => { const parsed = parseAllowBuildLine(` dsh-purge@${MASTER_URL}: true`); assert.equal(parsed.key, `dsh-purge@${MASTER_URL}`); @@ -233,3 +256,105 @@ test("幂等:第二次 ensure 不再写盘(自愈后收敛)", () => { assert.equal(fs.readFileSync(fp, "utf8"), healed); }); +// ---- #191:cordis.patch.yml 是 `[]` 时追加会导致宿主启动解析失败 ---- + +test("patch 形态分类:空 / 空 flow / 块序列 / 非空 flow / 未知", () => { + assert.equal(classifyPatchBody("").kind, "empty"); + assert.equal(classifyPatchBody("# 只有注释\n\n").kind, "empty"); + assert.equal(classifyPatchBody("[]").kind, "empty-flow"); + assert.equal(classifyPatchBody("# c\n[]\n").kind, "empty-flow"); + assert.equal(classifyPatchBody("---\n[]\n").kind, "empty-flow"); + assert.equal(classifyPatchBody("- id: foo\n name: foo\n").kind, "block-seq"); + assert.equal(classifyPatchBody("[a, b]\n").kind, "flow-seq"); + assert.equal(classifyPatchBody("foo: bar\n").kind, "unknown"); +}); + +test("空文件:直接写 insert 块", () => { + const dir = withTempPatch(undefined); + const fp = path.join(dir, "cordis.patch.yml"); + const result = ensureProfileBodyInsert(dir); + assert.equal(result.ok, true); + assert.equal(result.wrote, true); + assert.equal(fs.readFileSync(fp, "utf8"), INSERT_BLOCK); +}); + +test("只有注释和空行:写 insert 块,注释保留", () => { + const dir = withTempPatch("# overrides, disables, and insertions\n\n"); + const fp = path.join(dir, "cordis.patch.yml"); + const result = ensureProfileBodyInsert(dir); + assert.equal(result.ok, true); + assert.equal(result.wrote, true); + const text = fs.readFileSync(fp, "utf8"); + assert.equal(text.includes("# overrides, disables, and insertions"), true); + assert.equal(text.includes("- insert:"), true); + assert.equal(topLevelInsertGroups(text), 1); +}); + +test("注释 + `[]`:`[]` 被替换成 insert 块,注释保留,无第二个文档", () => { + const dir = withTempPatch("# overrides, disables, and insertions\n[]\n"); + const fp = path.join(dir, "cordis.patch.yml"); + const result = ensureProfileBodyInsert(dir); + assert.equal(result.ok, true); + assert.equal(result.wrote, true); + const text = fs.readFileSync(fp, "utf8"); + assert.equal(text.includes("[]"), false); + assert.equal(text.includes("# overrides, disables, and insertions"), true); + assert.equal(text.includes("- insert:"), true); + // 只有一个顶层 `- insert:` 组 → 没有变成第二个文档 + assert.equal(topLevelInsertGroups(text), 1); +}); + +test("`---` 前缀 + `[]`:同样替换,无第二个文档", () => { + const dir = withTempPatch("---\n[]\n"); + const fp = path.join(dir, "cordis.patch.yml"); + const result = ensureProfileBodyInsert(dir); + assert.equal(result.ok, true); + assert.equal(result.wrote, true); + const text = fs.readFileSync(fp, "utf8"); + assert.equal(text.includes("[]"), false); + assert.equal(topLevelInsertGroups(text), 1); +}); + +test("正常块序列:沿用旧的追加行为", () => { + const original = "- id: foo\n name: foo\n"; + const dir = withTempPatch(original); + const fp = path.join(dir, "cordis.patch.yml"); + const result = ensureProfileBodyInsert(dir); + assert.equal(result.ok, true); + assert.equal(result.wrote, true); + const text = fs.readFileSync(fp, "utf8"); + assert.equal(text.startsWith(original), true); + assert.equal(text.includes("- insert:"), true); + assert.equal(topLevelInsertGroups(text), 1); +}); + +test("非空 flow 序列:宿主内容不动,返回 ok:false", () => { + const original = "[a, b]\n"; + const dir = withTempPatch(original); + const fp = path.join(dir, "cordis.patch.yml"); + const result = ensureProfileBodyInsert(dir); + assert.equal(result.ok, false); + assert.equal(result.wrote, false); + assert.equal(fs.readFileSync(fp, "utf8"), original); +}); + +test("已有 dsh-purge 本体行:零写入", () => { + const original = "- insert:\n - id: dsh-purge\n name: dsh-purge\n"; + const dir = withTempPatch(original); + const fp = path.join(dir, "cordis.patch.yml"); + const result = ensureProfileBodyInsert(dir); + assert.equal(result.ok, true); + assert.equal(result.wrote, false); + assert.equal(fs.readFileSync(fp, "utf8"), original); +}); + +test("`[]` 替换后幂等:再 ensure 一次零写入", () => { + const dir = withTempPatch("# c\n[]\n"); + const fp = path.join(dir, "cordis.patch.yml"); + const first = ensureProfileBodyInsert(dir); + assert.equal(first.wrote, true); + const healed = fs.readFileSync(fp, "utf8"); + const second = ensureProfileBodyInsert(dir); + assert.equal(second.wrote, false); + assert.equal(fs.readFileSync(fp, "utf8"), healed); +}); \ No newline at end of file