aboutsummaryrefslogtreecommitdiff
path: root/src/state.ts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-28 01:19:58 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commitf9dcdf7f6dcdf2979693a9f179eda2b9995d57ae (patch)
tree763ca6f879a693f56572ab256d33769d6d6bb0ad /src/state.ts
parentb9c77bb6f9b8b83574489344ef6efd207a611e98 (diff)
refactor: close the remaining unguarded type assertions
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<string, unknown>` 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) <noreply@anthropic.com>
Diffstat (limited to 'src/state.ts')
-rw-r--r--src/state.ts19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/state.ts b/src/state.ts
index d4b1534..601f7e0 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -6,6 +6,7 @@
*/
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
+import { asArrayOf, asNumber, asString, isRecord } from "./util/guards";
export type BrowserState = {
readonly namespace: string;
@@ -31,13 +32,29 @@ export const persistState = (pi: ExtensionAPI, state: BrowserState): void => {
* If `currentNamespace` is supplied (e.g. from the --browser-namespace flag),
* it overrides whatever is in the persisted entry.
*/
+const asPersistedState = (v: unknown): Partial<BrowserState> | undefined => {
+ if (!isRecord(v)) return undefined;
+ const namespace = asString(v["namespace"]);
+ const remoteBrowserId = asString(v["remoteBrowserId"]);
+ const ownedTargetIds = asArrayOf(v["ownedTargetIds"], asString);
+ const harnessWindowTargetId = asString(v["harnessWindowTargetId"]);
+ const harnessWindowId = asNumber(v["harnessWindowId"]);
+ return {
+ ...(namespace !== undefined ? { namespace } : {}),
+ ...(remoteBrowserId !== undefined ? { remoteBrowserId } : {}),
+ ...(ownedTargetIds !== undefined ? { ownedTargetIds } : {}),
+ ...(harnessWindowTargetId !== undefined ? { harnessWindowTargetId } : {}),
+ ...(harnessWindowId !== undefined ? { harnessWindowId } : {}),
+ };
+};
+
export const restoreState = (ctx: ExtensionContext, currentNamespace?: string): BrowserState => {
const branchEntries = ctx.sessionManager.getBranch();
const fallback = defaultState(currentNamespace);
for (let i = branchEntries.length - 1; i >= 0; i--) {
const entry = branchEntries[i];
if (entry?.type === "custom" && entry.customType === "browser-harness-state") {
- const data = entry.data as Partial<BrowserState> | undefined;
+ const data = asPersistedState(entry.data);
if (data) {
return {
...fallback,