fix(browser): let a newer attachment replace a stale one

This commit is contained in:
LukeParkerDev 2026-09-02 19:32:35 +10:00
parent 510d3f4b67
commit 67029d207b
2 changed files with 10 additions and 5 deletions

View file

@ -44,8 +44,10 @@ export default Plugin.define({
const browser = yield* Effect.acquireRelease(
Effect.gen(function* () {
const closed = yield* Deferred.make<void>()
if (!active || browsers.has(input.sessionID))
return yield* Effect.fail(call.error("unavailable", "Browser is unavailable.", {}))
if (!active) return yield* Effect.fail(call.error("unavailable", "Browser is unavailable.", {}))
// The newest desktop attachment wins so a re-register that races the
// previous connection's teardown does not leave the session detached.
yield* close(input.sessionID)
const browser: Attachment = {
connectionID: input.connectionID,
state: null,

View file

@ -113,10 +113,13 @@ test(
expect(yield* host.execute({ type: "open" }).pipe(Effect.flip)).toMatchObject({
message: "No desktop browser is connected.",
})
const stale = yield* host.attach("stale")
// A newer attachment for the same session replaces the previous one.
const attached = yield* host.attach("first")
expect(
yield* host.rpc.attach({ ...attached.input, connectionID: "duplicate" }, options).pipe(Effect.flip),
).toMatchObject({ type: "unavailable" })
yield* Fiber.join(stale.lifetime).pipe(Effect.timeout("5 seconds"))
expect(yield* host.rpc.state({ ...stale.input, state }, options).pipe(Effect.flip)).toMatchObject({
type: "unavailable",
})
const other = Location.Ref.make({ directory: AbsolutePath.make(path.join(host.location.directory, "other")) })
yield* Effect.promise(() => mkdir(other.directory))
yield* host.opencode.plugin.list({ location: other })