fix(runtime)!: preserve unknown debugger attachment state

Keep omitted discovery attachment state as null through Node, browser, Electron, and reconciliation evidence. Report the uncertainty while retaining Node observation unless discovery explicitly marks the target attached. Reject worker TargetInfo records missing the required boolean with partial coverage.

Validate omitted and explicit states through provider, CLI, and MCP boundaries and real Node 24 Inspector workflows. Consolidate Electron regression coverage into the existing list-and-inspect workflow.

BREAKING CHANGE: attached fields in JavaScript runtime, browser, and Electron target discovery and inspection results may be null when discovery does not report attachment state.
This commit is contained in:
morluto
2026-10-12 12:37:00 +08:00
parent 48c352b6bf
commit e55ef72135
23 changed files with 249 additions and 40 deletions
@@ -14,7 +14,7 @@ describe("CDP target inventory", () => {
type: "page",
title: `Page ${String(index)}`,
url: `https://allowed.example/page/${String(index)}`,
attached: false,
...(index === 0 ? {} : { attached: index === 1 ? false : true }),
webSocketDebuggerUrl: `ws://127.0.0.1:${String(port)}/devtools/page/${id}`,
};
});
@@ -60,6 +60,14 @@ describe("CDP target inventory", () => {
expect(result.value.targets).toHaveLength(targets.length);
expect(result.value.targets[0]?.target_id).toBe("target-0000");
expect(result.value.targets.at(-1)?.target_id).toBe("target-1100");
expect(
result.value.targets.slice(0, 3).map(({ attached }) => attached),
).toEqual([null, false, true]);
expect(
result.value.limitations.some((item) =>
item.includes("attachment state"),
),
).toBe(true);
} finally {
if (server.listening)
await new Promise<void>((resolve, reject) => {
@@ -83,6 +83,7 @@ describe("CdpElectronProvider target selection", () => {
const browser = await startFakeCdpBrowser({
electronFileUrl: pathToFileURL(index).href,
duplicateElectronInventory: true,
omitElectronTargetAttachmentState: true,
});
browsers.push(browser);
const provider = new CdpElectronProvider();
@@ -96,8 +97,14 @@ describe("CdpElectronProvider target selection", () => {
expect.objectContaining({
target_id: "electron-page",
file_path: index,
attached: null,
}),
]);
expect(
listed.value.limitations.some((item) =>
item.includes("attachment state"),
),
).toBe(true);
expect(listed.value.excluded).toEqual({
unsupported_url: 3,
non_page: 2,
@@ -112,6 +119,12 @@ describe("CdpElectronProvider target selection", () => {
);
if (!inspected.ok) throw inspected.error;
expect(inspected.value.target.file_path).toBe(index);
expect(inspected.value.target.attached).toBeNull();
expect(
inspected.value.limitations.some((item) =>
item.includes("attachment state"),
),
).toBe(true);
expect(inspected.value.frames).toEqual([
expect.objectContaining({ file_path: index }),
]);
@@ -0,0 +1,46 @@
import { expect, it } from "vitest";
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js";
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
it.each(["omitted", "null"] as const)(
"excludes TargetInfo workers with %s required attached state",
async (attachmentState) => {
const browser = await startFakeCdpBrowser({
commandResult: (command, origin) => {
if (command.method !== "Target.getTargets") return undefined;
return {
targetInfos: [
{
targetId: "worker-1",
type: "service_worker",
url: `${origin}/worker.js`,
openerId: "allowed-page",
...(attachmentState === "null" ? { attached: null } : {}),
},
],
};
},
});
try {
const result = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 0,
}),
);
if (!result.ok) throw result.error;
expect(result.value.workers).toEqual([]);
expect(result.value.completeness.excluded).toContainEqual({
section: "workers",
reason: "invalid_protocol_value",
count: 1,
});
} finally {
await browser.close();
}
},
);
@@ -235,6 +235,68 @@ test("returns every target exposed by the selected Inspector endpoint inline", a
}
});
test.each([
{ label: "omitted", attached: undefined, expected: null },
{ label: "explicitly detached", attached: false, expected: false },
] as const)(
"preserves $label V8 attachment state through listing and observation",
async ({ attached, expected }) => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
...(attached === undefined ? {} : { attached }),
});
try {
const provider = new V8InspectorProvider();
const listed = await provider.listTargets({
inspector_endpoint: fake.endpoint,
});
if (!listed.ok) throw listed.error;
expect(listed.value.targets[0]?.attached).toBe(expected);
if (expected === null)
expect(
listed.value.limitations.some((item) =>
item.includes("attachment state"),
),
).toBe(true);
const observed = await provider.observe(
observeInput(fake.endpoint, fake.targetId, "node"),
);
if (!observed.ok) throw observed.error;
expect(observed.value.target.attached).toBe(expected);
if (expected === null)
expect(
observed.value.limitations.some((item) =>
item.includes("attachment state"),
),
).toBe(true);
} finally {
await fake.close();
}
},
);
test("rejects a V8 target explicitly marked attached", async () => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
attached: true,
});
try {
const result = await new V8InspectorProvider().observe(
observeInput(fake.endpoint, fake.targetId, "node"),
);
expect(result).toMatchObject({
ok: false,
error: { reason: "target_not_allowed" },
});
expect(fake.commands).toEqual([]);
} finally {
await fake.close();
}
});
test("includes targets outside the old caller root filter", async () => {
const fixture = await runtimeFixture();
const outside = await temporaryFile("outside.js");
+3 -1
View File
@@ -101,7 +101,9 @@ export const targets = (
type: "page",
title: "Electron application",
url: options.electronFileUrl,
attached: false,
...(options.omitElectronTargetAttachmentState === true
? {}
: { attached: false }),
...(options.omitTargetWebSocket === true
? {}
: {
+2
View File
@@ -114,6 +114,8 @@ export interface FakeOptions {
readonly webMcpChildRecoversAfterTransient?: boolean;
readonly webMcpChildNavigatesAllowed?: boolean;
readonly electronFileUrl?: string;
/** Omit attached from the Electron page's HTTP discovery target. */
readonly omitElectronTargetAttachmentState?: boolean;
readonly duplicateElectronInventory?: boolean;
readonly electronInventoryCount?: number;
readonly urlShapedAllowedTitle?:
+22 -17
View File
@@ -10,6 +10,7 @@ interface InspectorCommand {
interface FakeV8InspectorOptions {
readonly targetUrl: string;
readonly attached?: boolean | null;
readonly targetType?: "node" | "page";
readonly runtimeProduct?: string;
readonly scriptUrls?: readonly string[];
@@ -60,21 +61,19 @@ export const startFakeV8Inspector = async (
if (request.url === "/json/list") {
response.end(
JSON.stringify([
target(
targetId,
options.targetUrl,
options.targetType ?? "node",
target(targetId, options.targetUrl, {
type: options.targetType ?? "node",
port,
),
attached: options.attached,
}),
...(options.additionalTargetUrl === undefined
? []
: [
target(
otherTargetId,
options.additionalTargetUrl,
options.targetType ?? "node",
target(otherTargetId, options.additionalTargetUrl, {
type: options.targetType ?? "node",
port,
),
attached: options.attached,
}),
]),
...Array.from(
{ length: options.additionalTargetCount ?? 0 },
@@ -82,8 +81,11 @@ export const startFakeV8Inspector = async (
target(
`00000000-0000-4000-8000-${String(index + 3).padStart(12, "0")}`,
options.targetUrl,
options.targetType ?? "node",
port,
{
type: options.targetType ?? "node",
port,
attached: options.attached,
},
),
),
]),
@@ -220,15 +222,18 @@ const sendScriptEvents = (
const target = (
id: string,
url: string,
type: "node" | "page",
port: number,
options: {
readonly type: "node" | "page";
readonly port: number;
readonly attached: boolean | null | undefined;
},
) => ({
id,
type,
type: options.type,
title: "redacted-by-provider",
url,
attached: false,
webSocketDebuggerUrl: `ws://localhost:${String(port)}/${id}`,
...(options.attached === undefined ? {} : { attached: options.attached }),
webSocketDebuggerUrl: `ws://localhost:${String(options.port)}/${id}`,
});
const parseCommand = (raw: string): InspectorCommand => {