From 5bacb41751643f69f2339cd6cbea8ef774167a42 Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 21:14:57 +0800 Subject: [PATCH 1/2] fix(#3838): stop the cost advisory firing for disabled cordis.patch.yml entries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The high-cost plugin banner (#2462) fired whenever the loader id appeared as a top-level entry in any profile's cordis.patch.yml — an explicitly disabled plugin (entry + 'disabled: true') kept warning forever, because dshEnabledPatchIds() only collected '- id:' lines and never read the disable flag. The sibling #2861 host-plugin notice already implemented the correct block-scoped state machine; the two parsers had drifted apart. Fix: extract that state machine as parseDshPatchEntries() in dsh-channel.ts (single source of truth for patch-layer enablement semantics) and route BOTH scans through it — the advisory now skips entries whose own block carries an indented 'disabled: true', and patchDisabled() keeps its exact behavior (pinned by the #2861 suite). Tests: 6 parser unit tests pinning the attribution contract (block scoping, quote stripping, CRLF, duplicate ids, non-indented/other-value flags); 4 advisory gate pairs — disabled:true yields no advisory (the regression), disabled:false still reports, unrelated-entry disable does not silence, cross-profile union still reports once. --- src/dsh-channel.ts | 46 ++++++++++++++++++++ src/host-plugin-notice.ts | 28 +++--------- src/plugin-advisory.ts | 21 ++++----- tests/dsh-patch-entries.test.ts | 58 +++++++++++++++++++++++++ tests/issue2462-plugin-advisory.test.ts | 39 +++++++++++++++-- 5 files changed, 153 insertions(+), 39 deletions(-) create mode 100644 tests/dsh-patch-entries.test.ts diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index 7aaf318cd8..55f44a94a6 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -42,6 +42,52 @@ export function dshProfileDirs(env: NodeJS.ProcessEnv = process.env): string[] { return entries.filter((e) => e.isDirectory() && e.name !== "node_modules").map((e) => path.join(profiles, e.name)); } +// — cordis.patch.yml entry parsing (#3838) ———————————————————————————————— + +export interface DshPatchEntry { + /** Loader id as written in the `- id:` line (surrounding quotes stripped). */ + id: string; + /** True when the entry's own block carries an indented `disabled: true` line. */ + disabled: boolean; +} + +/** Parse top-level `- id:` entries out of cordis.patch.yml content. Line-based + * and forgiving — a malformed line simply yields no entry. A new `- id:` line + * or ANY other top-level list item closes the previous entry, so a stray + * `disabled:` line cannot leak into the wrong block; only an INDENTED + * `disabled: true` inside the entry's own block counts as disabling it + * (#3838: the #2462 cost-advisory scan ignored the flag entirely and fired + * for explicitly disabled plugins). Duplicate ids are kept per occurrence — + * callers decide how duplicates combine. The single source of truth for + * patch-layer enablement semantics: both the #2462 cost advisory and the + * #2861 host-plugin notice scan through this so they can never drift apart. */ +export function parseDshPatchEntries(text: string): DshPatchEntry[] { + const itemRe = /^\s*-\s*id\s*:\s*(\S+)/; + const listItemRe = /^\s*-\s/; + const disabledRe = /^\s+disabled\s*:\s*true\b/; + const out: DshPatchEntry[] = []; + let current: DshPatchEntry | undefined; + for (const line of text.split(/\r?\n/)) { + const m = line.match(itemRe); + if (m) { + if (current) out.push(current); + let v = m[1]; + if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1); + v = v.trim(); + current = v.length > 0 ? { id: v, disabled: false } : undefined; + continue; + } + if (listItemRe.test(line)) { + if (current) out.push(current); + current = undefined; + continue; + } + if (current && disabledRe.test(line)) current.disabled = true; + } + if (current) out.push(current); + return out; +} + // — legacy managed-block migration (#966) ———————————————————————————————————— // Pre-unification installs wrote this block into every profile's diff --git a/src/host-plugin-notice.ts b/src/host-plugin-notice.ts index 4ecdada57a..1f13bb1ea9 100644 --- a/src/host-plugin-notice.ts +++ b/src/host-plugin-notice.ts @@ -24,7 +24,7 @@ import fs from "node:fs"; import path from "node:path"; -import { dshProfileDirs } from "./dsh-channel.js"; +import { dshProfileDirs, parseDshPatchEntries } from "./dsh-channel.js"; import { stateDir } from "./paths.js"; interface HostPluginNotice { @@ -94,9 +94,9 @@ function readProfileManifest(dir: string): Record | undefined { } // True when the profile patch layer carries `- id: ` with a -// `disabled: true` line inside its block. Line-forgiving like #2462's scan: -// any top-level list item that is not an `- id:` entry resets attribution, so -// a stray `disabled:` line cannot leak into the wrong entry. +// `disabled: true` line inside its block. Delegates to the shared dsh-channel +// entry scanner (#3838) so this gate and the #2462 cost-advisory scan share +// one parsing discipline instead of drifting apart. function patchDisabled(patchFile: string, loaderId: string): boolean { let text: string; try { @@ -104,25 +104,7 @@ function patchDisabled(patchFile: string, loaderId: string): boolean { } catch { return false; } - const itemRe = /^\s*-\s*id\s*:\s*(\S+)/; - const listItemRe = /^\s*-\s/; - const disabledRe = /^\s+disabled\s*:\s*true\b/; - let current: string | undefined; - for (const line of text.split(/\r?\n/)) { - const m = line.match(itemRe); - if (m) { - let v = m[1]; - if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1); - current = v.trim(); - continue; - } - if (listItemRe.test(line)) { - current = undefined; - continue; - } - if (current === loaderId && disabledRe.test(line)) return true; - } - return false; + return parseDshPatchEntries(text).some((e) => e.id === loaderId && e.disabled); } export function detectHostPluginNotices(env: NodeJS.ProcessEnv = process.env): HostPluginNotice[] { diff --git a/src/plugin-advisory.ts b/src/plugin-advisory.ts index c5c324372d..ee3233938c 100644 --- a/src/plugin-advisory.ts +++ b/src/plugin-advisory.ts @@ -18,7 +18,7 @@ import fs from "node:fs"; import path from "node:path"; -import { dshProfileDirs } from "./dsh-channel.js"; +import { dshProfileDirs, parseDshPatchEntries } from "./dsh-channel.js"; interface CostAdvisory { id: string; @@ -40,10 +40,11 @@ const KNOWN_COST_PLUGINS: ReadonlyArray<{ client: string; id: string; name: stri const SCAN_TTL_MS = 5 * 60 * 1000; let cache: { key: string; at: number; result: CostAdvisory[] } | undefined; -// Top-level "- id: " patch entries across every dsh profile's -// cordis.patch.yml. `\S+` stops at whitespace so trailing config/comments are -// naturally excluded; surrounding quotes are stripped. Per-line and forgiving — -// a malformed line simply yields no id. +// Enabled top-level patch-entry loader ids across every dsh profile's +// cordis.patch.yml — entries carrying `disabled: true` in their own block do +// NOT count (#3838: presence of the id line alone used to fire the advisory +// for explicitly disabled plugins). Parsed by the shared dsh-channel entry +// scanner so this gate and the #2861 disable check share one discipline. function dshEnabledPatchIds(env: NodeJS.ProcessEnv): Set { const ids = new Set(); let dirs: string[]; @@ -52,7 +53,6 @@ function dshEnabledPatchIds(env: NodeJS.ProcessEnv): Set { } catch { return ids; } - const itemRe = /^\s*-\s*id\s*:\s*(\S+)/; for (const dir of dirs) { let text: string; try { @@ -60,13 +60,8 @@ function dshEnabledPatchIds(env: NodeJS.ProcessEnv): Set { } catch { continue; } - for (const line of text.split(/\r?\n/)) { - const m = line.match(itemRe); - if (!m) continue; - let v = m[1]; - if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1); - v = v.trim(); - if (v.length > 0) ids.add(v); + for (const e of parseDshPatchEntries(text)) { + if (!e.disabled) ids.add(e.id); } } return ids; diff --git a/tests/dsh-patch-entries.test.ts b/tests/dsh-patch-entries.test.ts new file mode 100644 index 0000000000..4afa35172e --- /dev/null +++ b/tests/dsh-patch-entries.test.ts @@ -0,0 +1,58 @@ +// #3838: parseDshPatchEntries — the single source of truth for cordis.patch.yml +// entry enablement semantics, shared by the #2462 cost advisory and the #2861 +// host-plugin notice. These tests pin the exact block-attribution contract both +// consumers rely on: a disabled flag belongs to its own entry's block only. +import test from "node:test"; +import assert from "node:assert/strict"; + +import { parseDshPatchEntries } from "../src/dsh-channel.js"; + +test("parseDshPatchEntries: enabled and disabled entries parsed with block-scoped flags", () => { + const text = [ + "# a comment", + "- id: llm-pi-ai", + "- id: auto-review", + " disabled: true", + "- id: other", + " config:", + " x: 1", + "", + ].join("\n"); + assert.deepEqual(parseDshPatchEntries(text), [ + { id: "llm-pi-ai", disabled: false }, + { id: "auto-review", disabled: true }, + { id: "other", disabled: false }, + ]); +}); + +test("parseDshPatchEntries: surrounding quotes stripped, empty id dropped, trailing entry flushed", () => { + const text = '- id: "auto-review"\n- id: \'\' \n'; + assert.deepEqual(parseDshPatchEntries(text), [{ id: "auto-review", disabled: false }]); +}); + +test("parseDshPatchEntries: only an INDENTED `disabled: true` inside the own block counts", () => { + // column-0 flag belongs to no entry; false / non-word values do not match + const text = ["disabled: true", "- id: a", " disabled: false", "- id: b", " disabled: truex"].join("\n"); + assert.deepEqual(parseDshPatchEntries(text), [ + { id: "a", disabled: false }, + { id: "b", disabled: false }, + ]); +}); + +test("parseDshPatchEntries: a non-id list item resets attribution — no leak across blocks", () => { + const text = ["- insert:", " - bare-entry", " disabled: true", "- id: c"].join("\n"); + assert.deepEqual(parseDshPatchEntries(text), [{ id: "c", disabled: false }]); +}); + +test("parseDshPatchEntries: CRLF line endings", () => { + const text = "- id: d\r\n disabled: true\r\n"; + assert.deepEqual(parseDshPatchEntries(text), [{ id: "d", disabled: true }]); +}); + +test("parseDshPatchEntries: duplicate ids kept per occurrence", () => { + const text = ["- id: e", "- id: e", " disabled: true"].join("\n"); + assert.deepEqual(parseDshPatchEntries(text), [ + { id: "e", disabled: false }, + { id: "e", disabled: true }, + ]); +}); diff --git a/tests/issue2462-plugin-advisory.test.ts b/tests/issue2462-plugin-advisory.test.ts index 1a94ea87fe..2254088e7e 100644 --- a/tests/issue2462-plugin-advisory.test.ts +++ b/tests/issue2462-plugin-advisory.test.ts @@ -3,8 +3,9 @@ // these plugins are not compressors; they burn prefix-cache hits host-side and // bili already isolates them into a sub-session. // Detection keys off ENABLEMENT, not installation: the advisory fires only when -// the dsh loader id (`auto-review`) appears as a top-level entry in cordis.patch.yml — -// mere presence in package.json deps must NOT trigger it (installed-but-off / bundled). +// the dsh loader id (`auto-review`) appears as a top-level, NOT-disabled entry in +// cordis.patch.yml (#3838) — mere presence in package.json deps must NOT trigger +// it (installed-but-off / bundled), and neither must an explicitly disabled entry. import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; @@ -34,10 +35,13 @@ function assertTestOwned(file: string, root: string): void { `refusing to write outside test-owned root ${root}: ${file}`); } function writeProfilePatch(root: string, profile: string, ids: string[]): void { + writeProfilePatchLines(root, profile, ids.map((id) => `- id: ${id}`)); +} +function writeProfilePatchLines(root: string, profile: string, lines: string[]): void { const file = path.join(root, "dsh", "profiles", profile, "cordis.patch.yml"); assertTestOwned(file, root); fs.mkdirSync(path.dirname(file), { recursive: true }); - fs.writeFileSync(file, [ ...ids.map((id) => `- id: ${id}`), "" ].join("\n")); + fs.writeFileSync(file, [...lines, ""].join("\n")); } function writeProfilePkg(root: string, pkg: Record): void { const file = path.join(root, "dsh", "profiles", "main", "package.json"); @@ -111,6 +115,35 @@ test("#2462: multiple profiles enabling the same plugin report once", () => { assert.equal(detectCostAdvisories(hermeticEnv(root)).length, 1); }); +test("#3838: entry present but disabled:true yields NO advisory (explicitly off plugins must not warn)", () => { + _clearCostAdvisoryCache(); + const root = tmp("bili-3838-disabled-"); + writeProfilePatchLines(root, "main", ["- id: llm-pi-ai", `- id: ${PATCH_ID}`, " disabled: true"]); + assert.deepEqual(detectCostAdvisories(hermeticEnv(root)), []); +}); + +test("#3838: disabled:false is NOT disabling — explicitly enabled still reports", () => { + _clearCostAdvisoryCache(); + const root = tmp("bili-3838-enabled-flag-"); + writeProfilePatchLines(root, "main", [`- id: ${PATCH_ID}`, " disabled: false"]); + assert.equal(detectCostAdvisories(hermeticEnv(root)).length, 1); +}); + +test("#3838: disabled:true on an UNRELATED entry does not silence the advisory", () => { + _clearCostAdvisoryCache(); + const root = tmp("bili-3838-other-disabled-"); + writeProfilePatchLines(root, "main", ["- id: other-plugin", " disabled: true", `- id: ${PATCH_ID}`]); + assert.equal(detectCostAdvisories(hermeticEnv(root)).length, 1); +}); + +test("#3838: disabled in one profile but enabled in another still reports once", () => { + _clearCostAdvisoryCache(); + const root = tmp("bili-3838-cross-profile-"); + writeProfilePatchLines(root, "a", [`- id: ${PATCH_ID}`, " disabled: true"]); + writeProfilePatch(root, "b", [PATCH_ID]); + assert.equal(detectCostAdvisories(hermeticEnv(root)).length, 1); +}); + test("#2462: result cached within TTL, invalidated by _clearCostAdvisoryCache", () => { _clearCostAdvisoryCache(); const root = tmp("bili-2462-cache-"); From 99468a9976ad18b8c7c068f1dfa74aef9adfc638 Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 21:48:10 +0800 Subject: [PATCH 2/2] fix: keep DshPatchEntry module-local to satisfy the unused-exports gate CI job unused-exports (scripts/check-unused-exports.mjs, #1440 P6) flagged the new DshPatchEntry interface as INTERNAL_ONLY: both consumers (host-plugin-notice.ts, plugin-advisory.ts) use the shape structurally through parseDshPatchEntries() without importing the type name, and the unit tests pin it with plain object literals. Dropping the export matches the #3806 treatment (62d47dc63 kept its interface module-local for the same gate). No behavior change. --- src/dsh-channel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index 55f44a94a6..308c385f54 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -44,7 +44,7 @@ export function dshProfileDirs(env: NodeJS.ProcessEnv = process.env): string[] { // — cordis.patch.yml entry parsing (#3838) ———————————————————————————————— -export interface DshPatchEntry { +interface DshPatchEntry { /** Loader id as written in the `- id:` line (surrounding quotes stripped). */ id: string; /** True when the entry's own block carries an indented `disabled: true` line. */