Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/dsh-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ————————————————————————————————

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
Expand Down
28 changes: 5 additions & 23 deletions src/host-plugin-notice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -94,35 +94,17 @@ function readProfileManifest(dir: string): Record<string, unknown> | undefined {
}

// True when the profile patch layer carries `- id: <loaderId>` 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 {
text = fs.readFileSync(patchFile, "utf8");
} 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[] {
Expand Down
21 changes: 8 additions & 13 deletions src/plugin-advisory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: <value>" 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<string> {
const ids = new Set<string>();
let dirs: string[];
Expand All @@ -52,21 +53,15 @@ function dshEnabledPatchIds(env: NodeJS.ProcessEnv): Set<string> {
} catch {
return ids;
}
const itemRe = /^\s*-\s*id\s*:\s*(\S+)/;
for (const dir of dirs) {
let text: string;
try {
text = fs.readFileSync(path.join(dir, "cordis.patch.yml"), "utf8");
} 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;
Expand Down
58 changes: 58 additions & 0 deletions tests/dsh-patch-entries.test.ts
Original file line number Diff line number Diff line change
@@ -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 },
]);
});
39 changes: 36 additions & 3 deletions tests/issue2462-plugin-advisory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, unknown>): void {
const file = path.join(root, "dsh", "profiles", "main", "package.json");
Expand Down Expand Up @@ -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-");
Expand Down
Loading