From b349da92e69b6eb2b83d1b23efc2859c40e763ec Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:06:31 +0100 Subject: [PATCH 1/4] test: cover retrying a linked thread read Co-authored-by: GPT 6.2 Sol --- .../src/state/peerLinks.test.ts | 31 +++++- .../linked-thread-retry-reads-again.e2e.ts | 104 ++++++++++++++++++ 2 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 scripts/scenarios/linked-thread-retry-reads-again.e2e.ts diff --git a/packages/client-runtime/src/state/peerLinks.test.ts b/packages/client-runtime/src/state/peerLinks.test.ts index 75430e340fbff..dd39162df8a4c 100644 --- a/packages/client-runtime/src/state/peerLinks.test.ts +++ b/packages/client-runtime/src/state/peerLinks.test.ts @@ -1,5 +1,6 @@ import { EnvironmentId, + PeerLinkThreadReadError, WS_METHODS, type OrchestrationV2ThreadHistoryPage, type OrchestrationV2ThreadStreamItem, @@ -15,6 +16,7 @@ import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as SubscriptionRef from "effect/SubscriptionRef"; import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; +import { TestClock } from "effect/testing"; import { EnvironmentRegistry } from "../connection/registry.ts"; import { EnvironmentSupervisor } from "../connection/supervisor.ts"; @@ -25,7 +27,7 @@ import { createPeerLinkEnvironmentAtoms, linkThroughRoutes } from "./peerLinks.t // regression: #40544 earlier messages vanished when a linked thread's view opened again it.effect( - "keeps loaded messages when a linked thread's view opens again on the same snapshot", + "keeps loaded messages when a linked thread retries and reopens on the same snapshot", () => Effect.gen(function* () { const recent = v2MessageRow("Recent message", 2); @@ -38,7 +40,7 @@ it.effect( hasMoreHistory: true, }); const subscriptions = yield* Queue.unbounded<{ - items: Queue.Queue; + items: Queue.Queue; stopped: Deferred.Deferred; }>(); const requested: string[] = []; @@ -46,7 +48,10 @@ it.effect( [WS_METHODS.peerLinksSubscribeThread]: () => Stream.unwrap( Effect.gen(function* () { - const items = yield* Queue.unbounded(); + const items = yield* Queue.unbounded< + OrchestrationV2ThreadStreamItem, + PeerLinkThreadReadError + >(); const stopped = yield* Deferred.make(); yield* Effect.addFinalizer(() => Deferred.succeed(stopped, undefined)); yield* Queue.offer(subscriptions, { items, stopped }); @@ -102,8 +107,26 @@ it.effect( older.sourceItemId, recent.sourceItemId, ]); + const unreachable = new PeerLinkThreadReadError({ + reason: "unreachable", + label: "Linked", + message: "The linked machine is temporarily unreachable.", + }); + yield* Queue.fail(first.items, unreachable); + expect((yield* Queue.take(views)).error).toBe(unreachable); + + registry.refresh(links.threadView(target)); + yield* TestClock.adjust(0); + const retry = yield* Queue.poll(subscriptions); + if (Option.isNone(retry)) throw new Error("Refresh did not start a new linked thread read"); + yield* Queue.offer(retry.value.items, snapshot("before-recent")); + const recovered = yield* Queue.take(views).pipe( + Effect.repeat({ until: (view) => view.error === null }), + ); + expect(visibleMessages(recovered)).toEqual([older.sourceItemId, recent.sourceItemId]); + unmount(); - yield* Deferred.await(first.stopped); + yield* Deferred.await(retry.value.stopped); open(); const reopened = yield* Queue.take(subscriptions); diff --git a/scripts/scenarios/linked-thread-retry-reads-again.e2e.ts b/scripts/scenarios/linked-thread-retry-reads-again.e2e.ts new file mode 100644 index 0000000000000..3ab31726bcb23 --- /dev/null +++ b/scripts/scenarios/linked-thread-retry-reads-again.e2e.ts @@ -0,0 +1,104 @@ +/// +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; + +import { T3_HOME } from "../e2e.config.ts"; +import { SECOND_HOME, startSecondServer } from "./second-server.ts"; +import { environmentId, makeState, rpcClient, runPromise, test, WEB } from "./t3.ts"; + +const decodePairing = Schema.decodeEffect( + Schema.fromJsonString(Schema.Struct({ credential: Schema.String })), +); + +test( + "Try again immediately reads a linked thread once it is reachable", + WEB, + async ({ t3, app, browser, screen }) => { + await t3.project("primary"); + const origin = app.baseUrl ?? ""; + const server = startSecondServer(origin); + try { + const { host } = await server.ready; + await runPromise( + Effect.gen(function* () { + const primary = yield* rpcClient(T3_HOME, origin); + const remote = yield* rpcClient(SECOND_HOME, host); + for (const client of [primary, remote]) { + yield* client["server.updateSettings"]({ patch: { linkedEnvironmentsEnabled: true } }); + } + const pairing = yield* makeState(SECOND_HOME).cli([ + "auth", + "pairing", + "create", + "--json", + ]); + const { credential } = yield* decodePairing(pairing.stdout); + yield* primary["peerLinks.link"]({ + url: host, + pairingCode: credential, + access: "approval-required", + }); + }).pipe(Effect.scoped), + ); + + const remote = makeState(SECOND_HOME).state; + const project = await remote.project("linked"); + const prompt = "Read the linked design notes"; + const thread = await remote.thread({ project, title: "Design notes", messages: [prompt] }); + const via = await runPromise(environmentId(origin)); + const linked = await runPromise(environmentId(host)); + + // Drop only the first subscription, as if the linked machine were briefly unreachable. + // Every later request goes to the real server and reads the seeded thread. + await browser.addInitScript(() => { + let failed = false; + const send = WebSocket.prototype.send; + WebSocket.prototype.send = function (data) { + if (failed || typeof data !== "string") return send.call(this, data); + const parsed = JSON.parse(data); + const messages = Array.isArray(parsed) ? parsed : [parsed]; + const request = messages.find( + (message) => message?._tag === "Request" && message.tag === "peerLinks.subscribeThread", + ); + if (!request) return send.call(this, data); + failed = true; + const others = messages.filter((message) => message !== request); + if (others.length > 0) send.call(this, JSON.stringify(others)); + setTimeout(() => { + this.dispatchEvent( + new MessageEvent("message", { + data: JSON.stringify({ + _tag: "Exit", + requestId: request.id, + exit: { + _tag: "Failure", + cause: [ + { + _tag: "Fail", + error: { + _tag: "PeerLinkThreadReadError", + reason: "unreachable", + label: "Scenario", + message: "The linked machine is temporarily unreachable.", + }, + }, + ], + }, + }), + }), + ); + }, 0); + }; + }); + + await t3.open(`/linked/${via}/${linked}/${thread.threadId}`); + await screen.getByRole("button", "Try again", { exact: true }).tap(); + await app.screenshot("after-trying-again"); + // The automatic retry waits 15 seconds. This checks the user's explicit retry. + await expect(screen.getByText(prompt, { exact: true })).toBeVisible({ timeout: 4_000 }); + } finally { + await server.stop(); + } + }, +); From 97e7bf32bc84f2fa3bb934570585dce3707ef13b Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:07:29 +0100 Subject: [PATCH 2/4] fix: restart linked thread reads when trying again Co-authored-by: GPT 6.2 Sol --- apps/web/src/state/linkedThreadView.ts | 19 +++++++++++-------- .../client-runtime/src/state/peerLinks.ts | 17 ++++++++++------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/apps/web/src/state/linkedThreadView.ts b/apps/web/src/state/linkedThreadView.ts index adeb858c23cab..b186dfa6454a4 100644 --- a/apps/web/src/state/linkedThreadView.ts +++ b/apps/web/src/state/linkedThreadView.ts @@ -107,14 +107,17 @@ const tabViewAtom = Atom.family((key: string) => { const view = peerLinkEnvironment.threadView( targetOf({ viaEnvironmentId, environmentId, threadId }), ); - return Atom.make((get) => { - const live = get(view); - const title = AsyncResult.getOrElse(live, () => null)?.thread.projection?.thread.title; - if (title && panelRef !== null) { - useRightPanelStore.getState().setLinkedThreadTitle(panelRef, surfaceId, title); - } - return live; - }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`linked-thread-tab-view:${key}`)); + return Atom.readable( + (get) => { + const live = get(view); + const title = AsyncResult.getOrElse(live, () => null)?.thread.projection?.thread.title; + if (title && panelRef !== null) { + useRightPanelStore.getState().setLinkedThreadTitle(panelRef, surfaceId, title); + } + return live; + }, + (refresh) => refresh(view), + ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`linked-thread-tab-view:${key}`)); }); /** diff --git a/packages/client-runtime/src/state/peerLinks.ts b/packages/client-runtime/src/state/peerLinks.ts index fd9d269bfd726..a7af1ac15b4c9 100644 --- a/packages/client-runtime/src/state/peerLinks.ts +++ b/packages/client-runtime/src/state/peerLinks.ts @@ -87,13 +87,16 @@ export function createPeerLinkEnvironmentAtoms( ), ) .pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-data:peer-links:thread-stream:${key}`)); - return Atom.make((get) => { - const live = get(stream); - if (AsyncResult.isSuccess(live)) { - get.set(kept, keepLinkedThreadView(get.once(kept), live.value)); - } - return live; - }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-data:peer-links:thread-view:${key}`)); + return Atom.readable( + (get) => { + const live = get(stream); + if (AsyncResult.isSuccess(live)) { + get.set(kept, keepLinkedThreadView(get.once(kept), live.value)); + } + return live; + }, + (refresh) => refresh(stream), + ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-data:peer-links:thread-view:${key}`)); }); return { list, From b2b40a33d12a535b852927f465473b8b0122d149 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:09:19 +0100 Subject: [PATCH 3/4] test: mark the linked thread scenario timer as browser-side Co-authored-by: GPT 6.2 Sol --- scripts/scenarios/linked-thread-retry-reads-again.e2e.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/scenarios/linked-thread-retry-reads-again.e2e.ts b/scripts/scenarios/linked-thread-retry-reads-again.e2e.ts index 3ab31726bcb23..fd24c39b74dea 100644 --- a/scripts/scenarios/linked-thread-retry-reads-again.e2e.ts +++ b/scripts/scenarios/linked-thread-retry-reads-again.e2e.ts @@ -65,6 +65,7 @@ test( failed = true; const others = messages.filter((message) => message !== request); if (others.length > 0) send.call(this, JSON.stringify(others)); + // @effect-diagnostics-next-line globalTimers:off - This callback is injected into the browser. setTimeout(() => { this.dispatchEvent( new MessageEvent("message", { From 17944a4598c1135f0537e2a0e6520c4bc2218a21 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:24:51 +0100 Subject: [PATCH 4/4] chore: credit the models that worked on this change Co-authored-by: Claude Opus 5.5