fix(browser): advertise zero-based units for runtime source coordinates (#1085)

* fix(browser): advertise zero-based units for runtime source coordinates

inspect_web_event_listeners and observe_web_execution return CDP
callsite, listener and resource_start line_number/column_number values
unchanged: zero-based lines and UTF-16 columns. Neither the advertised
output schema nor docs/web-runtime.md said so. trace_web_source_location
takes one-based lines, so a caller chaining a listener location into it
lands one line early.

Describe the units on the shared schema fields, which reach both tools'
advertised output schemas, and document the conversion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(browser): describe runtime positions relative to the enclosing resource

For an inline <script>, CDP reports listener and callsite positions
relative to the HTML document, and resource_start is where the script
text starts in that document. A real headless Chromium 155 page showed
this: a listener on HTML line 6 came back as line_number 5, with
resource_start {3, 8}. Describe all positions as relative to the
enclosing resource. Document that inline-script positions must have
resource_start subtracted, plus one added to the line, before
trace_web_source_location.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(browser): clarify runtime-to-source coordinate conversion

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: N0zoM1z0 <1705004398@qq.com>
This commit is contained in:
Kaoru0822
2026-10-09 00:22:18 +08:00
committed by GitHub
co-authored by Claude Opus 5.5 N0zoM1z0
parent 23862af1b5
commit 6218d289e2
3 changed files with 55 additions and 5 deletions
+34
View File
@@ -0,0 +1,34 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import {
webRuntimeLocationSchema,
webRuntimeSourceSchema,
} from "./webRuntime.js";
const properties = (schema: z.ZodType) =>
z
.object({ properties: z.record(z.string(), z.unknown()) })
.parse(z.toJSONSchema(schema, { io: "output" })).properties;
describe("web runtime coordinate contract", () => {
it("advertises zero-based resource-relative CDP lines and UTF-16 columns", () => {
const location = properties(webRuntimeLocationSchema);
expect(location.line_number).toMatchObject({
description: expect.stringContaining(
"Zero-based line in the script's enclosing resource",
),
});
expect(location.column_number).toMatchObject({
description: expect.stringContaining("Zero-based UTF-16 column"),
});
const start = properties(
webRuntimeSourceSchema.shape.resource_start.unwrap(),
);
expect(start.line_number).toMatchObject({
description: expect.stringContaining(
"Zero-based line where the script text starts",
),
});
});
});
+18 -4
View File
@@ -6,6 +6,20 @@ import {
import { browserVersionSchema } from "./browserObservationSchemas.js";
import { jsonObjectSchema } from "./jsonValue.js";
// CDP positions are zero-based and relative to the script's enclosing
// resource: the script file for an external script, or the HTML document for
// an inline script. trace_web_source_location takes one-based,
// script-relative lines, so the advertised units keep callers from passing a
// position off by one or by the inline script's offset.
const RESOURCE_LINE =
"Zero-based line in the script's enclosing resource (the script file for an external script, or the HTML document for an inline script), as reported by CDP.";
const RESOURCE_COLUMN =
"Zero-based UTF-16 column on that line of the enclosing resource, as reported by CDP.";
const START_LINE =
"Zero-based line where the script text starts in its enclosing resource (the HTML document for an inline script).";
const START_COLUMN =
"Zero-based UTF-16 column where the script text starts on that line of its enclosing resource.";
/** Scope for one externally owned page; omitted origins select its current origin. */
export const webRuntimeScopeSchema = z.strictObject({
cdp_endpoint: browserEndpointSchema,
@@ -27,8 +41,8 @@ export const webRuntimeSourceSchema = z.object({
language: z.string().nullable(),
resource_start: z
.object({
line_number: z.number().int().min(0),
column_number: z.number().int().min(0),
line_number: z.number().int().min(0).describe(START_LINE),
column_number: z.number().int().min(0).describe(START_COLUMN),
})
.nullable(),
source: z.discriminatedUnion("state", [
@@ -51,8 +65,8 @@ export type WebRuntimeSource = z.infer<typeof webRuntimeSourceSchema>;
export const webRuntimeLocationSchema = z.object({
script_id: z.string(),
url: z.string().nullable(),
line_number: z.number().int().min(0),
column_number: z.number().int().min(0).nullable(),
line_number: z.number().int().min(0).describe(RESOURCE_LINE),
column_number: z.number().int().min(0).nullable().describe(RESOURCE_COLUMN),
function_name: z.string().nullable(),
source_association: z.enum(["script_id", "unknown"]),
});