From f9dcdf7f6dcdf2979693a9f179eda2b9995d57ae Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 01:19:58 +0530 Subject: refactor: close the remaining unguarded type assertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Types the event-queue iterator's TReturn as undefined so the terminal result needs no cast; declares the isError field the pi runtime reads by duck-typing; binds socket to a local const in the daemon transport; replaces the @ts-ignore on the optional sharp import with a non-literal specifier so src/ still typechecks where sharp was not installed. Adds src/util/guards.ts as the single home for narrowing over unknown — isRecord plus scalar/array readers — and converts the two private isObject copies in cdp/types.ts and daemon/protocol.ts along with every inline `as Record` in domains/js.ts and domains/render.ts. Gives Accessibility.getFullAXTree a real result schema, closing the last unvalidated CDP boundary: only nodeId is required, every object carries additionalProperties, and each AX value stays Unknown, so the tree decodes as a whole or fails as a whole rather than being filtered. Guards the five tool-result `details` readers, the daemon attach response, the persisted session state, the browser-namespace flag, and the fs errno narrowing, each feeding an already-present failure path. The daemon-transport socket binding is not a bug fix: send() runs synchronously inside sendWithTimeout's promise executor, so socket cannot be reassigned between the check and the write. It removes the assertion and makes the write immune to a later refactor that adds an await. Two assertions survive by design and are documented in the task report: the never->unknown re-widening in util/tool.ts, which the type-erased tool registry makes unavoidable, and the AsyncFunction constructor lookup in domains/js.ts, which JavaScript exposes no typed binding for. Co-Authored-By: Claude Opus 5 (1M context) --- src/domains/console.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/domains/console.ts') diff --git a/src/domains/console.ts b/src/domains/console.ts index 9696cd9..c38da2a 100644 --- a/src/domains/console.ts +++ b/src/domains/console.ts @@ -5,6 +5,7 @@ import { type Result, ok } from "../util/result"; import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool"; import { applyTruncation } from "../util/truncate"; import type { ConsoleRecord, ConsoleLevel } from "../cdp/console-buffer"; +import { asArrayOf, asBoolean, asNumber, asRecord, asString } from "../util/guards"; const ConsoleArgs = Type.Object({ levels: Type.Optional( @@ -106,6 +107,50 @@ type ConsoleDetails = { nextCursor?: number; }; +const CONSOLE_LEVELS: ReadonlyArray = ["log", "info", "warn", "error", "debug"]; + +const asConsoleLevel = (v: unknown): ConsoleLevel | undefined => + CONSOLE_LEVELS.find((l) => l === v); + +const asConsoleRecord = (v: unknown): ConsoleRecord | undefined => { + const o = asRecord(v); + if (o === undefined) return undefined; + const level = asConsoleLevel(o["level"]); + const text = asString(o["text"]); + if (level === undefined || text === undefined) return undefined; + const url = asString(o["url"]); + const lineNumber = asNumber(o["lineNumber"]); + const stackTrace = asString(o["stackTrace"]); + return { + seq: asNumber(o["seq"]) ?? 0, + timestampMs: asNumber(o["timestampMs"]) ?? 0, + level, + text, + source: o["source"] === "log-entry" ? "log-entry" : "console-api", + ...(url !== undefined ? { url } : {}), + ...(lineNumber !== undefined ? { lineNumber } : {}), + ...(stackTrace !== undefined ? { stackTrace } : {}), + }; +}; + +const asConsoleDetails = (v: unknown): ConsoleDetails | undefined => { + const o = asRecord(v); + if (o === undefined) return undefined; + const records = asArrayOf(o["records"], asConsoleRecord); + if (records === undefined) return undefined; + const fullOutputPath = asString(o["fullOutputPath"]); + const nextCursor = asNumber(o["nextCursor"]); + return { + total: asNumber(o["total"]) ?? records.length, + returned: asNumber(o["returned"]) ?? records.length, + bufferOverflowed: asBoolean(o["bufferOverflowed"]) ?? false, + truncated: asBoolean(o["truncated"]) ?? false, + records, + ...(fullOutputPath !== undefined ? { fullOutputPath } : {}), + ...(nextCursor !== undefined ? { nextCursor } : {}), + }; +}; + export const consoleTool = defineBrowserTool({ name: "browser_console", label: "Browser Console", @@ -156,7 +201,7 @@ export const consoleTool = defineBrowserTool({ }, renderResult(result, expanded, theme) { - const details = result.details as ConsoleDetails | undefined; + const details = asConsoleDetails(result.details); if (!details) return new Text(theme.fg("error", "console: no details"), 0, 0); const md = renderConsoleMarkdown(details.records, { -- cgit v1.2.3