diff --git a/src/agent/dsh-compaction-disable.ts b/src/agent/dsh-compaction-disable.ts index 4970e3d159..30ead44212 100644 --- a/src/agent/dsh-compaction-disable.ts +++ b/src/agent/dsh-compaction-disable.ts @@ -172,6 +172,17 @@ export type CompactionDisableHost = { }; type MiddlewareSelf = { entry?: { options?: { name?: unknown } } | undefined }; +/** Entry option name carried by the fiber that owns a config resolution. + * The host dispatches `internal/config` as `waterfall('internal/config', + * fiber, next)` — the first argument is the event NAME, so cordis binds no + * `this` (vendor/cordis/src/events.ts:163 → thisArg null) and the listener + * receives `(fiber, next)`. Reading `this` therefore always yields + * undefined; the name lives on the first argument. */ +export function entryNameOf(fiber: unknown): string | undefined { + const name = (fiber as MiddlewareSelf | undefined)?.entry?.options?.name; + return typeof name === "string" && name.length > 0 ? name : undefined; +} + /** Register the load-time overlay + publish the gate. Runs from the always-on * gate entry's apply() (dsh-compaction-gate.ts, #3662) — NOT from bili-native, * whose disable/kill-switch must not take the service with it. The provide @@ -183,7 +194,7 @@ type MiddlewareSelf = { entry?: { options?: { name?: unknown } } | undefined }; export function registerCompactionDisable(ctx: CompactionDisableHost): void { if (typeof ctx.on === "function") { // Preset entries must carry the gate: sibling imports may finish out of order. - ctx.on("internal/config", function (this: MiddlewareSelf, _raw: unknown, next: () => unknown): unknown { + ctx.on("internal/config", function (fiber: unknown, next: () => unknown): unknown { const resolved = next(); // #3662: honor the process verdict — without an active declaration // from bili-native every row passes through stock. @@ -197,7 +208,7 @@ export function registerCompactionDisable(ctx: CompactionDisableHost): void { if (!Array.isArray(config.plugins)) return resolved; const compactionRows = countCompactionRows(config.plugins); if (compactionRows === 0) return resolved; - const rawName = this?.entry?.options?.name; + const rawName = entryNameOf(fiber); const entryName = typeof rawName === "string" ? rawName : "unnamed-entry"; noteGateCoverage(entryName.slice(0, 120), compactionRows, entryName === DSH_AGENT_PRESET_PACKAGE); return { ...config, plugins: disableBasicCompaction(config.plugins) }; diff --git a/tests/issue3354-dsh-compaction-disable.test.ts b/tests/issue3354-dsh-compaction-disable.test.ts index 0474d9ef2a..c21b5b13b3 100644 --- a/tests/issue3354-dsh-compaction-disable.test.ts +++ b/tests/issue3354-dsh-compaction-disable.test.ts @@ -100,7 +100,9 @@ test("#3806 detectCompactionOverlayGap: missing service provide -> no-service-pr }); test("#3354 registerCompactionDisable: transform honors the entry predicate and preserves siblings", () => { - type Mw = (raw: unknown, next: () => unknown) => unknown; + // #7112: cordis' real binding — `this` is null, the fiber is the FIRST + // argument; the mock calls below pass it there. + type Mw = (fiber: unknown, next: () => unknown) => unknown; let mw: Mw | undefined; let options: unknown; // #3662: the transform acts only while bili-native declared the lane owned. @@ -119,8 +121,8 @@ test("#3354 registerCompactionDisable: transform honors the entry predicate and { name: "@deepseek-ai/dsh-tool-result-pruner" }, ]; const cfg: Record = { plugins, isolate: { compaction: true } }; - const self = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; - const out = mw?.call(self, null, () => cfg) as { plugins?: Array>; isolate?: Record }; + const presetFiber = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; + const out = mw?.call(null, presetFiber, () => cfg) as { plugins?: Array>; isolate?: Record }; assert.deepEqual(out.isolate, { compaction: true }); assert.equal(out.plugins?.[0]?.disabled, true); assert.equal(out.plugins?.[1]?.disabled, true); @@ -129,13 +131,13 @@ test("#3354 registerCompactionDisable: transform honors the entry predicate and // compaction rows is rewritten too (custom presets), while an entry // whose plugins carry none keeps its resolved config object identity. const custom: Record = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - const customOut = mw?.call({ entry: { options: { name: "preset-general" } } }, null, () => custom) as { plugins?: Array> }; + const customOut = mw?.call(null, { entry: { options: { name: "preset-general" } } }, () => custom) as { plugins?: Array> }; assert.equal(customOut.plugins?.[0]?.disabled, true); const untouched: Record = { plugins: [{ name: "@deepseek-ai/dsh-other" }] }; - assert.equal(mw?.call({ entry: { options: { name: "other-entry" } } }, null, () => untouched), untouched); + assert.equal(mw?.call(null, { entry: { options: { name: "other-entry" } } }, () => untouched), untouched); assert.equal(mw?.call(undefined, null, () => untouched), untouched); // next() returning nothing → verbatim passthrough - assert.equal(mw?.call(self, null, () => undefined), undefined); + assert.equal(mw?.call(null, presetFiber, () => undefined), undefined); } finally { _resetDshCompactionVerdictForTest(); } diff --git a/tests/issue3662-dsh-gate-fallback.test.ts b/tests/issue3662-dsh-gate-fallback.test.ts index dfd5869d12..d4bae5ff37 100644 --- a/tests/issue3662-dsh-gate-fallback.test.ts +++ b/tests/issue3662-dsh-gate-fallback.test.ts @@ -19,14 +19,14 @@ const TESTS_DIR = path.dirname(fileURLToPath(import.meta.url)); const GATE_INSERT = `- insert:\n - id: bili-native\n name: billion-context\n - id: bili-compaction-gate\n name: billion-context/dsh-compaction-gate\n`; type GateHost = { - on?: (event: string, listener: (raw: unknown, next: () => unknown) => unknown, options?: { global?: boolean }) => void; + on?: (event: string, listener: (fiber: unknown, next: () => unknown) => unknown, options?: { global?: boolean }) => void; reflect?: { provide?: (name: string, value: unknown) => void } | undefined; }; function mockGateHost() { const order: string[] = []; const provided: Array<{ name: string; value: unknown }> = []; let onOptions: { global?: boolean } | undefined; - let mw: ((raw: unknown, next: () => unknown) => unknown) | undefined; + let mw: ((fiber: unknown, next: () => unknown) => unknown) | undefined; const host: GateHost = { on: (event, listener, options) => { order.push(`on:${event}`); @@ -45,7 +45,9 @@ function mockGateHost() { globalFlag: () => onOptions?.global === true, }; } -const PRESET_SELF = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; +// #7112: the fiber is the FIRST listener argument (cordis binds no `this`); +// the mock calls below pass it there, not as `this`. +const PRESET_FIBER = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; function presetPlugins(): Array> { return [ { name: "@deepseek-ai/dsh-compaction-basic", config: { auto: true } }, @@ -91,10 +93,10 @@ test("#3662: bili-native absent (disabled / kill-switched) — service still pro // the four presets resolvable instead of pending forever const raw = m.middleware(); assert.equal(typeof raw, "function"); - const mw = raw as (raw: unknown, next: () => unknown) => unknown; + const mw = raw as (fiber: unknown, next: () => unknown) => unknown; const plugins = presetPlugins(); const cfg: Record = { plugins, x: 1 }; - const out = mw.call(PRESET_SELF, null, () => cfg) as Record; + const out = mw.call(null, PRESET_FIBER, () => cfg) as Record; assert.equal(out, cfg, "absent verdict must pass the resolved config through verbatim"); for (const row of plugins) assert.equal(row.disabled, undefined); } finally { @@ -111,10 +113,10 @@ test("#3662: normal boot (verdict active) — the gate entry disables the compac assert.deepEqual(m.provided.map((p) => p.name), [BILI_COMPACTION_DISABLE_SERVICE]); const raw = m.middleware(); assert.equal(typeof raw, "function"); - const mw = raw as (raw: unknown, next: () => unknown) => unknown; + const mw = raw as (fiber: unknown, next: () => unknown) => unknown; const plugins = presetPlugins(); const cfg: Record = { plugins, x: 1 }; - const out = mw.call(PRESET_SELF, null, () => cfg) as { plugins?: Array>; x?: number }; + const out = mw.call(null, PRESET_FIBER, () => cfg) as { plugins?: Array>; x?: number }; assert.equal(out.x, 1); assert.deepEqual(out.plugins?.[0]?.config, { auto: true }); assert.equal(out.plugins?.[0]?.disabled, true); @@ -124,9 +126,9 @@ test("#3662: normal boot (verdict active) — the gate entry disables the compac // they nest no compaction rows — a custom preset nesting them is // rewritten (structural match), unrelated config keeps identity. const otherCfg: Record = { plugins: [{ name: "@deepseek-ai/dsh-other" }] }; - assert.equal(mw.call({ entry: { options: { name: "something-else" } } }, null, () => otherCfg), otherCfg); + assert.equal(mw.call(null, { entry: { options: { name: "something-else" } } }, () => otherCfg), otherCfg); const customCfg: Record = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - const customOut = mw.call({ entry: { options: { name: "preset-general" } } }, null, () => customCfg) as { plugins?: Array> }; + const customOut = mw.call(null, { entry: { options: { name: "preset-general" } } }, () => customCfg) as { plugins?: Array> }; assert.equal(customOut.plugins?.[0]?.disabled, true); } finally { _resetDshCompactionVerdictForTest(); diff --git a/tests/issue4147-dsh-overlay-structural.test.ts b/tests/issue4147-dsh-overlay-structural.test.ts index 6cadef1222..66b9d1cc15 100644 --- a/tests/issue4147-dsh-overlay-structural.test.ts +++ b/tests/issue4147-dsh-overlay-structural.test.ts @@ -16,7 +16,9 @@ import { // globalThis telemetry channel so the durable boot note can state actual // coverage (patch-row misses are otherwise loader-log-only). -type Mw = (raw: unknown, next: () => unknown) => unknown; +// #7112: cordis' real binding — `this` is null, the fiber (the name carrier) +// is the FIRST argument; the mock calls below pass it there. +type Mw = (fiber: unknown, next: () => unknown) => unknown; function middleware(): Mw { let mw: Mw | undefined; @@ -43,7 +45,7 @@ test("#4147: custom preset entries nesting compaction rows are rewritten (group { name: "@deepseek-ai/dsh-command-compact" }, ], }; - const out = mw.call({ entry: { options: { name: "preset-general" } } }, null, () => cfg) as { plugins: Array> }; + const out = mw.call(null, { entry: { options: { name: "preset-general" } } }, () => cfg) as { plugins: Array> }; assert.equal(out.plugins[0].disabled, undefined, "unrelated sibling row untouched"); const group = out.plugins[1] as { config: Array> }; assert.equal(group.config[0].disabled, true, "nested group row disabled"); @@ -66,9 +68,9 @@ test("#4147 negative: no compaction rows → resolved config object identity kep declareDshCompactionActive(); const mw = middleware(); const unrelated = { plugins: [{ name: "@deepseek-ai/dsh-tool-result-pruner" }] }; - assert.equal(mw.call({ entry: { options: { name: "preset-shipin" } } }, null, () => unrelated), unrelated); + assert.equal(mw.call(null, { entry: { options: { name: "preset-shipin" } } }, () => unrelated), unrelated); const noPlugins = { other: 1 }; - assert.equal(mw.call({ entry: { options: { name: "preset-shipin" } } }, null, () => noPlugins), noPlugins); + assert.equal(mw.call(null, { entry: { options: { name: "preset-shipin" } } }, () => noPlugins), noPlugins); assert.equal(dshCompactionGateTelemetry(), null, "no rewrite, no coverage record"); } finally { _resetDshCompactionVerdictForTest(); @@ -82,7 +84,7 @@ test("#4147/#3662 negative: absent verdict → stock passthrough records nothing _resetDshCompactionGateTelemetryForTest(); const mw = middleware(); const cfg = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - const out = mw.call({ entry: { options: { name: "preset-general" } } }, null, () => cfg) as { plugins: Array> }; + const out = mw.call(null, { entry: { options: { name: "preset-general" } } }, () => cfg) as { plugins: Array> }; assert.equal(out, cfg, "verbatim passthrough"); assert.equal((cfg.plugins as Array>)[0].disabled, undefined); assert.equal(dshCompactionGateTelemetry(), null, "stock passthrough leaves no coverage record"); @@ -99,11 +101,11 @@ test("#3962: telemetry dedups per entry, keeps the max row count, flags the fact declareDshCompactionActive(); const mw = middleware(); const one = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - mw.call({ entry: { options: { name: "preset-general" } } }, null, () => one); + mw.call(null, { entry: { options: { name: "preset-general" } } }, () => one); const two = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }, { name: "@deepseek-ai/dsh-command-compact" }] }; - mw.call({ entry: { options: { name: "preset-general" } } }, null, () => two); + mw.call(null, { entry: { options: { name: "preset-general" } } }, () => two); const factory = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - mw.call({ entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }, null, () => factory); + mw.call(null, { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }, () => factory); const unnamed = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; mw.call(undefined, null, () => unnamed); const t = dshCompactionGateTelemetry()!; @@ -126,7 +128,7 @@ test("#3962: long entry names (URLs) are truncated for the durable note", () => const mw = middleware(); const longName = "file:///".padEnd(200, "x"); const cfg = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - mw.call({ entry: { options: { name: longName } } }, null, () => cfg); + mw.call(null, { entry: { options: { name: longName } } }, () => cfg); const t = dshCompactionGateTelemetry()!; assert.equal(t.covered[0].entry.length, 120); } finally { diff --git a/tests/issue7112-config-hook-fiber.test.ts b/tests/issue7112-config-hook-fiber.test.ts new file mode 100644 index 0000000000..80e38bb952 --- /dev/null +++ b/tests/issue7112-config-hook-fiber.test.ts @@ -0,0 +1,120 @@ +// #7112: the internal/config middleware read the entry name from `this`, but +// cordis dispatches that waterfall as `('internal/config', fiber, next)` — the +// first argument is the event name, so no `this` is bound (thisArg null) and +// the listener receives (fiber, next). Every name-keyed branch was therefore +// dead: coverage telemetry degraded to "unnamed-entry", sawFactoryPreset never +// latched, and PR #5994's #5248 restore could never fire. These tests dispatch +// through a REAL cordis Context — the mock hosts elsewhere call the captured +// listener directly, which is exactly why the defect stayed green. +import test from "node:test"; +import assert from "node:assert/strict"; +import { Context } from "@deepseek-ai/cordis"; +import { + BILI_COMPACTION_DISABLE_SERVICE, + _resetDshCompactionGateTelemetryForTest, + _resetDshCompactionVerdictForTest, + declareDshCompactionActive, + dshCompactionGateTelemetry, + entryNameOf, + registerCompactionDisable, +} from "../src/agent/dsh-compaction-disable.ts"; + +const FACTORY_PRESET = "@deepseek-ai/dsh-agent-preset"; + +function presetConfig() { + return { + id: "standard", + plugins: [ + { + id: "compaction", + group: true, + config: [{ id: "compaction-basic", name: "@deepseek-ai/dsh-compaction-basic" }], + }, + ], + }; +} + +function fiberNamed(name: string) { + return { entry: { options: { id: "preset-standard", name } } }; +} + +function dispatch(name: string): { config: unknown; telemetry: ReturnType } { + const ctx = new Context(); + registerCompactionDisable(ctx as unknown as Parameters[0]); + const dispatchConfig = ctx.waterfall as unknown as + (event: string, fiber: unknown, next: () => unknown) => unknown; + const config = dispatchConfig("internal/config", fiberNamed(name), () => presetConfig()); + return { config, telemetry: dshCompactionGateTelemetry() }; +} + +test("#7112: the hook binds no `this`, so the name comes from the fiber argument", () => { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + declareDshCompactionActive(); + try { + const ctx = new Context(); + let seenThis: unknown = "unset"; + let seenFiberName: string | undefined; + const on = ctx.on as unknown as + (event: string, listener: (fiber: unknown, next: () => unknown) => unknown, options?: { global?: boolean }) => void; + const listener: (this: unknown, fiber: unknown, next: () => unknown) => unknown = + function (this: unknown, fiber: unknown, next: () => unknown) { + seenThis = this; + seenFiberName = entryNameOf(fiber); + return next(); + }; + on("internal/config", listener, { global: true }); + const dispatchConfig = ctx.waterfall as unknown as + (event: string, fiber: unknown, next: () => unknown) => unknown; + dispatchConfig("internal/config", fiberNamed(FACTORY_PRESET), () => presetConfig()); + assert.equal(seenThis, null, "cordis must bind null, not the fiber"); + assert.equal(seenFiberName, FACTORY_PRESET, "the fiber argument carries the entry name"); + } finally { + _resetDshCompactionVerdictForTest(); + } +}); + +test("#7112: coverage telemetry records the real entry name, not unnamed-entry", () => { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + declareDshCompactionActive(); + try { + const { config, telemetry } = dispatch(FACTORY_PRESET); + assert.ok(telemetry, "the transform must record coverage"); + assert.deepEqual(telemetry.covered, [{ entry: FACTORY_PRESET, rows: 1 }]); + assert.equal(telemetry.sawFactoryPreset, true, "the factory preset must latch"); + const rows = (config as { plugins: Array<{ config: Array<{ disabled?: boolean }> }> }).plugins; + assert.equal(rows[0]?.config[0]?.disabled, true, "the rewrite still disables the row"); + } finally { + _resetDshCompactionVerdictForTest(); + } +}); + +test("#7112: a custom preset keeps its own name and does not latch sawFactoryPreset", () => { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + declareDshCompactionActive(); + try { + const { telemetry } = dispatch("my-custom-preset"); + assert.deepEqual(telemetry?.covered, [{ entry: "my-custom-preset", rows: 1 }]); + assert.equal(telemetry?.sawFactoryPreset, false); + } finally { + _resetDshCompactionVerdictForTest(); + } +}); + +test("#7112: absent or empty names fall back to undefined (caller renders unnamed-entry)", () => { + assert.equal(entryNameOf(undefined), undefined); + assert.equal(entryNameOf({}), undefined); + assert.equal(entryNameOf({ entry: { options: { name: "" } } }), undefined); + assert.equal(entryNameOf({ entry: { options: { name: FACTORY_PRESET } } }), FACTORY_PRESET); +}); + +test("#7112: the gate service is published even when the transform cannot bind a name", () => { + const provided: string[] = []; + registerCompactionDisable({ + on: () => {}, + reflect: { provide: (name: string) => { provided.push(name) } }, + } as unknown as Parameters[0]); + assert.deepEqual(provided, [BILI_COMPACTION_DISABLE_SERVICE]); +});