mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(browser): validate outbound state and settle malformed replies immediately
This commit is contained in:
parent
f643e53400
commit
258ee0d4c4
5 changed files with 57 additions and 19 deletions
|
|
@ -25,7 +25,7 @@ export const createBrowserNetwork = Effect.fn("BrowserNetwork.create")(function*
|
|||
Effect.runPromise(input.rpc["tunnel.read"]({ ...input.attachment, tunnelID }, options), { signal }),
|
||||
write: (tunnelID, data, end, signal) =>
|
||||
Effect.runPromise(
|
||||
input.rpc["tunnel.write"]({ ...input.attachment, tunnelID, data: Base64.encode(data), end }, options),
|
||||
input.rpc["tunnel.write"]({ ...input.attachment, tunnelID, data: Base64.encode(data), ...(end !== undefined && { end }) }, options),
|
||||
{ signal },
|
||||
),
|
||||
close: (tunnelID) =>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { describe, expect, test } from "bun:test"
|
||||
import { Browser } from "@opencode/plugin-browser/rpc"
|
||||
import { invalidReply, wireOutcome, wireState } from "./wire"
|
||||
import { SessionNotFoundError } from "@opencode/protocol/errors"
|
||||
import {
|
||||
attachmentFailure,
|
||||
|
|
@ -87,6 +88,21 @@ test("a restored preview retries the failed attempt when the server returns", ()
|
|||
const restored = entry(state).tabs.get(tabA)?.tab
|
||||
expect(restored?.url).toBe("workspace://ses_x/report.html")
|
||||
expect(restored?.loadError).toBeUndefined()
|
||||
expect(Object.hasOwn(restored ?? {}, "loadError")).toBe(false)
|
||||
})
|
||||
|
||||
test("tab inventories and command replies cross the same JSON boundary, including after restore", () => {
|
||||
for (const url of ["about:blank", "file:///project/index.html", "workspace://session/index.html", "artifact://art_report/"]) {
|
||||
const failed = { ...tab(tabA, url), loadError: "ERR_FAILED" }
|
||||
const restored = entry(attached({tabs:[failed], focusedTabID:tabA})).tabs.get(tabA)!.tab
|
||||
for (const t of [failed, restored]) {
|
||||
const state = { tabs: [t], focusedTabID: tabA }
|
||||
expect(wireState(state)).toEqual(state)
|
||||
expect(wireOutcome({type:"success",result:{value:state,files:[]}}).type).toBe("success")
|
||||
}
|
||||
}
|
||||
// SAFETY: simulate malformed runtime data from a browser operation, not a type-safe caller.
|
||||
expect(wireOutcome({type:"success",result:{value:{missing:undefined},files:[]}} as unknown as Browser.Outcome)).toEqual(invalidReply)
|
||||
})
|
||||
|
||||
test("an offline registration returns the saved inventory for retry without forgetting it", () => {
|
||||
|
|
|
|||
|
|
@ -480,7 +480,7 @@ function createConnecting(event: Extract<PaneMachineEvent, { readonly type: "reg
|
|||
{
|
||||
status: "unloaded",
|
||||
// A restored page's first navigation takes the next generation.
|
||||
tab: { ...tab, loadError: undefined, loading: false, canGoBack: false, canGoForward: false, generation: tab.generation + 1 },
|
||||
tab: { id: tab.id, url: tab.url, title: tab.title, loading: false, canGoBack: false, canGoForward: false, generation: tab.generation + 1 },
|
||||
},
|
||||
]),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ import { reusableFile, workspaceFileURL } from "./link"
|
|||
import { createPreviews } from "./profile/previews"
|
||||
import { remotePreview } from "./remote-preview"
|
||||
import { artifactPages } from "./artifact-preview"
|
||||
import { invalidReply, wireOutcome, wireState } from "./wire"
|
||||
|
||||
type Target = { readonly server: string; readonly session: string; readonly restore?: Browser.State }
|
||||
|
||||
|
|
@ -242,7 +243,11 @@ export function createBrowserPane(input: {
|
|||
|
||||
const reply = (requestID: string, outcome: Browser.Outcome) =>
|
||||
send(
|
||||
rpc.result({ ...attachment, requestID, outcome: Schema.encodeSync(Browser.Outcome)(outcome) }, options),
|
||||
rpc.result({ ...attachment, requestID, outcome: wireOutcome(outcome) }, options).pipe(
|
||||
// An older server can reject a locally valid result. Settle the same request, never rerun it.
|
||||
Effect.catchIf(error => Schema.isSchemaError(error) || ("type" in error && error.type === "rpc.invalid_input"),
|
||||
() => rpc.result({ ...attachment, requestID, outcome: invalidReply }, options)),
|
||||
),
|
||||
)
|
||||
|
||||
// The window hears every event at once; it never waits on the server, which can stall for
|
||||
|
|
@ -250,15 +255,19 @@ export function createBrowserPane(input: {
|
|||
// inventory resolves every tab ID, so a state is retried until it arrives or the attachment
|
||||
// ends; the results queued behind it then never name a tab the server lacks. "unavailable"
|
||||
// means the server already dropped this attachment, which attach reports.
|
||||
handle.sendState = (state) =>
|
||||
handle.sendState = (inventory) => {
|
||||
const state = wireState(inventory)
|
||||
|
||||
if (!state) return send(Effect.logWarning("Browser state is not a valid wire inventory"))
|
||||
send(
|
||||
rpc.state({ ...attachment, state }, options).pipe(
|
||||
Effect.retry({
|
||||
while: (error) => !("type" in error && error.type === "unavailable"),
|
||||
while: (error) => !Schema.isSchemaError(error) && !("type" in error && ["unavailable", "rpc.invalid_input"].includes(error.type)),
|
||||
schedule: Schedule.min([Schedule.exponential("250 millis"), Schedule.spaced("10 seconds")]),
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const run = (requestID: string, abort: AbortController) =>
|
||||
rpc.command({ ...attachment, requestID }, options).pipe(
|
||||
|
|
@ -283,21 +292,14 @@ export function createBrowserPane(input: {
|
|||
),
|
||||
// An operation this desktop cannot decode comes from a newer plugin; answer it so
|
||||
// the agent does not wait out the server's timeout.
|
||||
Effect.tapError((error) =>
|
||||
Effect.sync(() => {
|
||||
if (!Schema.isSchemaError(error)) return
|
||||
reply(requestID, {
|
||||
type: "failure",
|
||||
code: "unsupported",
|
||||
message:
|
||||
"This desktop app does not support the requested browser operation. Ask the user to update the desktop app, or use another operation.",
|
||||
})
|
||||
}),
|
||||
),
|
||||
// A request that vanished (cancelled before retrieval) fails only that request.
|
||||
// Transport loss surfaces through the event stream and attach call instead.
|
||||
Effect.catchCause((cause) =>
|
||||
abort.signal.aborted ? Effect.void : Effect.logWarning("Browser command failed", cause),
|
||||
Effect.sync(() => {
|
||||
if (abort.signal.aborted) return
|
||||
const error = Option.getOrUndefined(Cause.findErrorOption(cause))
|
||||
reply(requestID, Schema.isSchemaError(error) ? {
|
||||
type: "failure", code: "unsupported", message: "This desktop cannot decode the requested browser operation. Check the desktop and server versions.",
|
||||
} : invalidReply)
|
||||
}),
|
||||
),
|
||||
Effect.forkScoped,
|
||||
)
|
||||
|
|
|
|||
20
packages/gui-plugins/src/browser/wire.ts
Normal file
20
packages/gui-plugins/src/browser/wire.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { Browser } from "@opencode/plugin-browser/rpc"
|
||||
import { Option, Schema } from "effect"
|
||||
|
||||
/** Do not echo schema diagnostics: they can contain page text and file contents. A command may already have run. */
|
||||
export const invalidReply = {
|
||||
type: "failure",
|
||||
code: "invalid_result",
|
||||
message: "The desktop could not encode the browser result. Inspect the tab before retrying the operation.",
|
||||
} as const
|
||||
|
||||
export function wireOutcome(outcome: Browser.Outcome) {
|
||||
return Option.getOrElse(Schema.encodeOption(Browser.Outcome)(outcome), () => invalidReply)
|
||||
}
|
||||
|
||||
/** State also appears inside the JSON value returned by tabs.list: validate both contracts. */
|
||||
export function wireState(state: Browser.State) {
|
||||
const encoded = Option.getOrUndefined(Schema.encodeOption(Browser.State)(state))
|
||||
|
||||
return encoded && Schema.is(Schema.Json)(encoded) ? encoded : undefined
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue